Skip to content

Commit c908e41

Browse files
committed
don't warn about const structs
1 parent c4a6546 commit c908e41

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

generate-tree-c.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@ def add_complex_getter(name, doc):
388388
add_simple_getter('%s_equivalent' % qual,
389389
'PyGccTree_New(gcc_private_make_tree(build_qualified_type(self->t.inner, TYPE_QUAL_%s)))' % qual.upper(),
390390
'The gcc.Type for the %s version of this type' % qual)
391+
if tree_type.SYM == 'RECORD_TYPE':
392+
add_simple_getter('const',
393+
'PyBool_FromLong(TYPE_READONLY(self->t.inner))',
394+
"Boolean: does this type have the 'const' modifier?")
391395

392396
if tree_type.SYM == 'INTEGER_TYPE':
393397
add_simple_getter('unsigned',

tests/examples/find-global-state/input.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,14 @@ int test6()
5757
{
5858
return bar.f;
5959
}
60+
61+
struct banana {
62+
int f;
63+
};
64+
65+
const struct banana a_banana;
66+
67+
int test7()
68+
{
69+
return a_banana.f;
70+
}

tests/examples/find-global-state/script.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@
2020

2121
DEBUG=0
2222

23+
def is_const(type_):
24+
if DEBUG:
25+
type_.debug()
26+
27+
if hasattr(type_, 'const'):
28+
if type_.const:
29+
return True
30+
31+
# Don't bother warning about an array of const e.g.
32+
# const char []
33+
if isinstance(type_, gcc.ArrayType):
34+
item_type = type_.dereference
35+
if is_const(item_type):
36+
return True
37+
38+
2339
class StateFinder:
2440
def __init__(self):
2541
# Locate all declarations of variables holding "global" state:
@@ -28,13 +44,14 @@ def __init__(self):
2844
for var in gcc.get_variables():
2945
type_ = var.decl.type
3046

31-
# Don't bother warning about an array of const e.g.
32-
# const char []
33-
if isinstance(type_, gcc.ArrayType):
34-
item_type = type_.dereference
35-
if hasattr(item_type, 'const'):
36-
if item_type.const:
37-
continue
47+
if DEBUG:
48+
print('var.decl: %r' % var.decl)
49+
print(type_)
50+
51+
# Don't bother warning about const data:
52+
if is_const(type_):
53+
continue
54+
3855
self.global_decls.add(var.decl)
3956
if DEBUG:
4057
print('self.global_decls: %r' % self.global_decls)

0 commit comments

Comments
 (0)