File: gcc-python-pretty-printer.c

package info (click to toggle)
gcc-python-plugin 0.17-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 46,268 kB
  • sloc: python: 15,434; ansic: 11,462; makefile: 576; xml: 449; cpp: 137; pascal: 75; sh: 7
file content (128 lines) | stat: -rw-r--r-- 3,318 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
   Copyright 2011, 2013 David Malcolm <dmalcolm@redhat.com>
   Copyright 2011, 2013 Red Hat, Inc.

   This is free software: you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see
   <http://www.gnu.org/licenses/>.
*/

#include <Python.h>
#include "gcc-python.h"
#include "gcc-python-wrappers.h"
#if (GCC_VERSION >= 4009)
/* Needed for placement new */
#include <new>
#endif

PyObject*
PyGccPrettyPrinter_New(void)
{
    struct PyGccPrettyPrinter *obj;

    obj = PyObject_New(struct PyGccPrettyPrinter, &PyGccPrettyPrinter_TypeObj);
    if (!obj) {
	return NULL;
    }
    
    //printf("PyGccPrettyPrinter_New\n");

    /* Gross hack for getting at a FILE* ; rewrite using fopencookie? */
    obj->buf[0] = '\0';
    obj->file_ptr = fmemopen(obj->buf, sizeof(obj->buf), "w");

#if (GCC_VERSION >= 4009)
    /* GCC 4.9 eliminated pp_construct in favor of a C++ ctor.
       Use placement new to run it on obj->pp.  */
    new ((void*)&obj->pp)
        pretty_printer(
# if (GCC_VERSION < 8003)
                       /* GCC 8.3 eliminated the "prefix" param.  */
                       NULL,
# endif
                       0);
#else
    pp_construct(&obj->pp, /* prefix */NULL, /* line-width */0);
#endif
    pp_needs_newline(&obj->pp) = false;
    pp_translate_identifiers(&obj->pp) = false;

    /* Connect the pp to the (FILE*): */
    obj->pp.buffer->stream = obj->file_ptr;

    //printf("PyGccPrettyPrinter_New returning: %p\n", obj);
    
    return (PyObject*)obj;
}

pretty_printer*
PyGccPrettyPrinter_as_pp(PyObject *obj)
{
    struct PyGccPrettyPrinter *ppobj;

    /* FIXME: */
    assert(Py_TYPE(obj) == &PyGccPrettyPrinter_TypeObj);
    ppobj = (struct PyGccPrettyPrinter *)obj;

    return &ppobj->pp;
}

PyObject*
PyGccPrettyPrinter_as_string(PyObject *obj)
{
    struct PyGccPrettyPrinter *ppobj;
    int len;

    /* FIXME: */
    assert(Py_TYPE(obj) == &PyGccPrettyPrinter_TypeObj);
    ppobj = (struct PyGccPrettyPrinter *)obj;

    /* Flush the pp first.  This forcibly adds a trailing newline: */
    pp_flush(&ppobj->pp);

    /* Convert to a python string, leaving off the trailing newline: */
    len = strlen(ppobj->buf);
    assert(len > 0);
    if ('\n' == ppobj->buf[len - 1]) {
	return PyGccString_FromString_and_size(ppobj->buf,
						      len - 1);
    } else {
	return PyGccString_FromString(ppobj->buf);
    }
}

void
PyGccPrettyPrinter_dealloc(PyObject *obj)
{
    struct PyGccPrettyPrinter *ppobj;

    /* FIXME: */
    assert(Py_TYPE(obj) == &PyGccPrettyPrinter_TypeObj);
    ppobj = (struct PyGccPrettyPrinter *)obj;

    /* Close the (FILE*), if open: */
    if (ppobj->file_ptr) {
	fclose(ppobj->file_ptr);
	ppobj->file_ptr = NULL;
    }

    Py_TYPE(obj)->tp_free(obj);
}

/*
  PEP-7  
Local variables:
c-basic-offset: 4
indent-tabs-mode: nil
End:
*/