Skip to content

Commit f253ece

Browse files
committed
Remove * from decl_string when type is a function pointer #61
This does not change the type of the declaration, just the value of the decl_string attribute.
1 parent 14d7a1e commit f253ece

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

docs/examples/function-pointer/example.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,26 @@
2929
decls = parser.parse([filename], xml_generator_config)
3030
global_namespace = declarations.get_global_namespace(decls)
3131

32-
function = global_namespace.variables()[0]
32+
function_ptr = global_namespace.variables()[0]
3333

3434
# Print the name of the function pointer
35-
print(function.name)
35+
print(function_ptr.name)
3636
# > myFuncPointer
3737

38-
# Print the type of the declaration (it's just a pointer)
39-
print(type(function.decl_type))
38+
# Print the type of the declaration
39+
print(function_ptr.decl_type)
40+
# > void (*)( int,double )
41+
42+
# Print the real type of the declaration (it's just a pointer)
43+
print(type(function_ptr.decl_type))
4044
# > <class 'pygccxml.declarations.cpptypes.pointer_t'>
4145

4246
# Check if this is a function pointer
43-
print(declarations.is_calldef_pointer(function.decl_type))
47+
print(declarations.is_calldef_pointer(function_ptr.decl_type))
4448
# > True
4549

4650
# Remove the pointer part, to access the function's type
47-
f_type = declarations.remove_pointer(function.decl_type)
51+
f_type = declarations.remove_pointer(function_ptr.decl_type)
4852

4953
# Print the type
5054
print(type(f_type))

pygccxml/declarations/cpptypes.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,12 @@ def __init__(self, base):
574574
compound_t.__init__(self, base)
575575

576576
def build_decl_string(self, with_defaults=True):
577-
return self.base.build_decl_string(with_defaults) + ' *'
577+
decl_string = self.base.build_decl_string(with_defaults)
578+
if isinstance(self.base, calldef_type_t):
579+
# This is a function pointer. Do not add supplementary *
580+
return decl_string
581+
else:
582+
return decl_string + " *"
578583

579584
def _clone_impl(self):
580585
return pointer_t(self.base.clone())

unittests/test_function_pointer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_function_pointer(self):
3333
self.assertTrue(
3434
isinstance(variables[0].decl_type, declarations.pointer_t))
3535
self.assertTrue(
36-
str(variables[0].decl_type) == "void (*)( int,double ) *")
36+
str(variables[0].decl_type) == "void (*)( int,double )")
3737
self.assertTrue(
3838
declarations.is_calldef_pointer(variables[0].decl_type))
3939
self.assertTrue(

0 commit comments

Comments
 (0)