Skip to content

Commit 58968ff

Browse files
committed
Add gcc.Location.in_system_header attribute
1 parent a42e100 commit 58968ff

File tree

7 files changed

+44
-2
lines changed

7 files changed

+44
-2
lines changed

docs/basics.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,3 +542,14 @@ Working with source code
542542
.. py:attribute:: column
543543
544544
(int) Column number within source file (starting at 1, not 0)
545+
546+
.. py:attribute:: in_system_header
547+
548+
(bool) This attribute flags locations that are within a system header
549+
file. It may be of use when writing custom warnings, so that you
550+
can filter out issues in system headers, leaving just those within
551+
the user's code::
552+
553+
# Don't report on issues found in system headers:
554+
if decl.location.in_system_header:
555+
return

gcc-c-api/gcc-location.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ GCC_PUBLIC_API (bool) gcc_location_is_unknown (gcc_location loc)
5757
return UNKNOWN_LOCATION == loc.inner;
5858
}
5959

60+
GCC_IMPLEMENT_PUBLIC_API (bool) gcc_location_get_in_system_header (gcc_location loc)
61+
{
62+
return in_system_header_at (loc.inner);
63+
}
64+
6065
GCC_IMPLEMENT_PUBLIC_API (void) gcc_set_input_location (gcc_location loc)
6166
{
6267
input_location = loc.inner;

gcc-c-api/location.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333

3434
<attribute name="is_unknown" kind="bool">
3535
</attribute>
36+
37+
<attribute name="in_system_header" kind="bool">
38+
</attribute>
3639
</type>
3740

3841
<attribute name="input_location" kind="location" access="rw">

generate-location-c.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,13 @@ def generate_location():
6262
[PyGetSetDef('file', 'PyGccLocation_get_file', None, 'Name of the source file'),
6363
PyGetSetDef('line', 'PyGccLocation_get_line', None, 'Line number within source file'),
6464
PyGetSetDef('column', 'PyGccLocation_get_column', None, 'Column number within source file'),
65-
])
65+
],
66+
identifier_prefix='PyGccLocation',
67+
typename='PyGccLocation')
68+
getsettable.add_simple_getter(cu,
69+
'in_system_header',
70+
'PyBool_FromLong(gcc_location_in_system_header_at(self->loc))',
71+
'Boolean: is this location within a system header?')
6672
cu.add_defn(getsettable.c_defn())
6773

6874
pytype = PyGccWrapperTypeObject(identifier = 'PyGccLocation_TypeObj',

tests/plugin/callgraph/input.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919

2020
#include <stdio.h>
2121

22+
extern const char *name;
23+
2224
void foo()
2325
{
26+
printf ("hello %s\n", name);
2427
}
2528

2629
void bar(void)

tests/plugin/callgraph/script.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def on_pass_execution(p, fn):
3636
print('cgn:')
3737
# print(dir(cgn))
3838
print(' cgn.decl: %r' % cgn.decl)
39+
print(' cgn.decl.location.in_system_header: %r'
40+
% cgn.decl.location.in_system_header)
3941
print(' cgn.callers: %r' % cgn.callers)
4042
print(' cgn.callees: %r' % cgn.callees)
4143
for e in cgn.callers:

tests/plugin/callgraph/stdout.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,23 @@ class CallgraphEdge(__builtin__.object)
5656
| __new__ = <built-in method __new__ of gcc.WrapperMeta object>
5757
| T.__new__(S, ...) -> a new object with type S, a subtype of T
5858

59+
cgn:
60+
cgn.decl: gcc.FunctionDecl('printf')
61+
cgn.decl.location.in_system_header: True
62+
cgn.callers: [gcc.CallgraphEdge()]
63+
cgn.callees: []
64+
gcc.CallgraphEdge()
65+
e.caller: gcc.CallgraphNode()
66+
e.callee: gcc.CallgraphNode()
67+
e.call_stmt: gcc.GimpleCall() printf ("hello %s\n", name.0);
5968
cgn:
6069
cgn.decl: gcc.FunctionDecl('baz')
70+
cgn.decl.location.in_system_header: False
6171
cgn.callers: []
6272
cgn.callees: [gcc.CallgraphEdge(), gcc.CallgraphEdge(), gcc.CallgraphEdge()]
6373
cgn:
6474
cgn.decl: gcc.FunctionDecl('bar')
75+
cgn.decl.location.in_system_header: False
6576
cgn.callers: [gcc.CallgraphEdge(), gcc.CallgraphEdge(), gcc.CallgraphEdge()]
6677
cgn.callees: [gcc.CallgraphEdge(), gcc.CallgraphEdge(), gcc.CallgraphEdge()]
6778
gcc.CallgraphEdge()
@@ -78,8 +89,9 @@ e.callee: gcc.CallgraphNode()
7889
e.call_stmt: gcc.GimpleCall() bar ();
7990
cgn:
8091
cgn.decl: gcc.FunctionDecl('foo')
92+
cgn.decl.location.in_system_header: False
8193
cgn.callers: [gcc.CallgraphEdge(), gcc.CallgraphEdge(), gcc.CallgraphEdge()]
82-
cgn.callees: []
94+
cgn.callees: [gcc.CallgraphEdge()]
8395
gcc.CallgraphEdge()
8496
e.caller: gcc.CallgraphNode()
8597
e.callee: gcc.CallgraphNode()

0 commit comments

Comments
 (0)