Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
gcc-python-plugin/gcc-python-attribute.c at master · Python-Repository-Hub/gcc-python-plugin · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
Python-Repository-Hub
/
gcc-python-plugin
Public
forked from
davidmalcolm/gcc-python-plugin
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Projects
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
gcc-python-plugin
/
gcc-python-attribute.c
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
266 lines (222 loc) · 7.47 KB
master
Breadcrumbs
gcc-python-plugin
/
gcc-python-attribute.c
Copy path
Top
File metadata and controls
Code
Blame
266 lines (222 loc) · 7.47 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
Copyright 2011 David Malcolm <dmalcolm@redhat.com>
Copyright 2011 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"
#include
"tree.h"
#include
"diagnostic.h"
#include
"plugin.h"
#if
(
GCC_VERSION
>=
4009
)
/* GCC 4.9 moved debug_tree here: */
#include
"print-tree.h"
#endif
/*
Attribute handling
*/
/* Dictionary mapping string attribute names to callables */
static
PyObject
*
attribute_dict
;
/*
Helper function when a custom attribute is encountered, for generating
the arguments to the Python callback.
The args to the function call will be the node, plus the args of the
attribute:
(node, arg0, arg1, ...)
*/
PyObject
*
make_args_for_attribute_callback
(
tree
node
,
tree
args
)
{
PyObject
*
list_args
=
NULL
;
PyObject
*
py_args
=
NULL
;
PyObject
*
py_node
=
NULL
;
Py_ssize_t
i
;
/* Walk "args" (a tree_list), converting to a python list of wrappers */
list_args
=
PyGcc_TreeMakeListFromTreeList
(
args
);
if
(!
list_args
) {
goto
error
;
}
py_args
=
PyTuple_New
(
1
+
PyList_Size
(
list_args
));
if
(!
py_args
) {
goto
error
;
}
py_node
=
PyGccTree_New
(
gcc_private_make_tree
(
node
));
if
(!
py_node
) {
goto
error
;
}
PyTuple_SET_ITEM
(
py_args
,
0
,
py_node
);
for
(
i
=
0
;
i
<
PyList_Size
(
list_args
);
i
++
) {
PyObject
*
arg
=
PyList_GetItem
(
list_args
,
i
);
Py_INCREF
(
arg
);
PyTuple_SET_ITEM
(
py_args
,
i
+
1
,
arg
);
}
Py_DECREF
(
list_args
);
return
py_args
;
error
:
Py_XDECREF
(
list_args
);
Py_XDECREF
(
py_args
);
Py_XDECREF
(
py_node
);
return
NULL
;
}
static
tree
handle_python_attribute
(
tree
*
node
,
tree
name
,
tree
args
,
int
flags
,
bool
*
no_add_attrs
)
{
PyObject
*
callable
;
/* Debug code: */
if
(
0
) {
printf
(
"handle_python_attribute called\n"
);
fprintf
(
stderr
,
"node: "
);
debug_tree
(
*
node
);
/* the site of the attribute e.g. a var_decl */
fprintf
(
stderr
,
"name: "
);
debug_tree
(
name
);
/* an identifier_node e.g. "custom_attribute_without_args" */
fprintf
(
stderr
,
"args: "
);
debug_tree
(
args
);
/* if present, a tree_list, e.g. of constants */
fprintf
(
stderr
,
"flags: %i\n"
,
flags
);
fprintf
(
stderr
,
"and here!\n"
);
}
/*
How do we get to the attribute?
This code:
const struct attribute_spec *spec = lookup_attribute_spec (name);
suggests that attributes must have unique names, so keep a dict mapping
strings to callables
*/
assert
(
IDENTIFIER_NODE
==
TREE_CODE
(
name
));
callable
=
PyDict_GetItemString
(
attribute_dict
,
IDENTIFIER_POINTER
(
name
));
assert
(
callable
);
{
PyGILState_STATE
gstate
;
PyObject
*
py_args
=
NULL
;
PyObject
*
result
=
NULL
;
gstate
=
PyGILState_Ensure
();
/*
The args to the function call will be the node, plus the args of the
attribute:
*/
py_args
=
make_args_for_attribute_callback
(
*
node
,
args
);
if
(!
py_args
) {
goto
cleanup
;
}
result
=
PyObject_Call
(
callable
,
py_args
,
NULL
);
if
(!
result
) {
/* Treat an unhandled Python error as a compilation error: */
error
(
"Unhandled Python exception raised within %s attribute handler"
,
IDENTIFIER_POINTER
(
name
));
PyErr_PrintEx
(
1
);
}
/* (the result is ignored) */
cleanup
:
Py_XDECREF
(
py_args
);
Py_XDECREF
(
result
);
PyGILState_Release
(
gstate
);
}
return
NULL
;
// FIXME
}
PyObject
*
PyGcc_RegisterAttribute
(
PyObject
*
self
,
PyObject
*
args
,
PyObject
*
kwargs
)
{
const
char
*
name
;
int
min_length
;
int
max_length
;
int
decl_required
;
int
type_required
;
int
function_type_required
;
PyObject
*
callable
;
struct
attribute_spec
*
attr
;
const
char
*
keywords
[]
=
{
"name"
,
"min_length"
,
"max_length"
,
"decl_required"
,
"type_required"
,
"function_type_required"
,
"callable"
,
NULL
};
if
(!
PyArg_ParseTupleAndKeywords
(
args
,
kwargs
,
"siiiiiO:register_attribute"
, (
char
*
*
)
keywords
,
&
name
,
&
min_length
,
&
max_length
,
&
decl_required
,
&
type_required
,
&
function_type_required
,
&
callable
)) {
return
NULL
;
}
/*
"struct attribute_spec" is declared in gcc/tree.h
register_attribute() is called by GCC for various attrs stored in
tables of global data e.g.:
const struct attribute_spec lto_attribute_table[]
Hence we must malloc the data, so that it persists for the rest of the
lifetime of the process
We get a "handler" callback, it gets passed the name of the attribute,
so maybe we can map names to callables.
*/
attr
=
PyMem_New
(
struct
attribute_spec
,
1
);
if
(!
attr
) {
return
PyErr_NoMemory
();
}
/* Clear it first, for safety: */
memset
(
attr
,
0
,
sizeof
(
struct
attribute_spec
));
/*
Populate "attr"
Annoyingly, all of the fields are marked as "const" within
struct attribute_spec, so we have to cast away the constness, leading
to the following deeply ugly code:
*/
*
(
char
*
*
)
&
attr
->
name
=
PyGcc_strdup
(
name
);
if
(!
attr
->
name
) {
PyMem_Free
(
attr
);
return
PyErr_NoMemory
();
}
*
(
int
*
)
&
attr
->
min_length
=
min_length
;
*
(
int
*
)
&
attr
->
max_length
=
max_length
;
*
(
bool
*
)
&
attr
->
decl_required
=
decl_required
;
*
(
bool
*
)
&
attr
->
type_required
=
type_required
;
*
(
bool
*
)
&
attr
->
function_type_required
=
function_type_required
;
*
(
tree
(
*
*
) (
tree
*
node
,
tree
name
,
tree
args
,
int
flags
,
bool
*
no_add_attrs
))
&
attr
->
handler
=
handle_python_attribute
;
/*
Associate the user-supplied callable with the given name, so that
handle_python_attribute knows which one to call:
*/
if
(!
attribute_dict
) {
attribute_dict
=
PyDict_New
();
if
(!
attribute_dict
) {
PyMem_Free
((
char
*
)
attr
->
name
);
PyMem_Free
(
attr
);
return
NULL
;
}
}
assert
(
attribute_dict
);
if
(
-1
==
PyDict_SetItemString
(
attribute_dict
,
name
,
callable
)) {
PyMem_Free
((
char
*
)
attr
->
name
);
PyMem_Free
(
attr
);
return
NULL
;
}
/*
OK, call into GCC to register the attribute.
(register_attribute doesn't have a return value; failures appear
to be fatal)
*/
register_attribute
(
attr
);
Py_RETURN_NONE
;
}
/*
PEP-7
Local variables:
c-basic-offset: 4
indent-tabs-mode: nil
End:
*/
You can’t perform that action at this time.