|
| 1 | +# Copyright 2014-2017 Insight Software Consortium. |
| 2 | +# Copyright 2004-2009 Roman Yakovenko. |
| 3 | +# Distributed under the Boost Software License, Version 1.0. |
| 4 | +# See http://www.boost.org/LICENSE_1_0.txt |
| 5 | + |
| 6 | +from pygccxml import utils |
| 7 | +from pygccxml import declarations |
| 8 | +from pygccxml import parser |
| 9 | + |
| 10 | +import os |
| 11 | +import sys |
| 12 | +import warnings |
| 13 | +warnings.simplefilter("error", Warning) |
| 14 | +# Find out the file location within the sources tree |
| 15 | +this_module_dir_path = os.path.abspath( |
| 16 | + os.path.dirname(sys.modules[__name__].__file__)) |
| 17 | + |
| 18 | +# Find out the c++ parser |
| 19 | +generator_path, generator_name = utils.find_xml_generator() |
| 20 | + |
| 21 | +# Configure the xml generator |
| 22 | +xml_generator_config = parser.xml_generator_configuration_t( |
| 23 | + xml_generator_path=generator_path, |
| 24 | + xml_generator=generator_name) |
| 25 | + |
| 26 | +# The c++ file we want to parse |
| 27 | +filename = "example.hpp" |
| 28 | +filename = this_module_dir_path + "/" + filename |
| 29 | + |
| 30 | +decls = parser.parse([filename], xml_generator_config) |
| 31 | +global_namespace = declarations.get_global_namespace(decls) |
| 32 | + |
| 33 | +c1 = global_namespace.variable("c1") |
| 34 | +print(str(c1), type(c1)) |
| 35 | +# > 'c1 [variable]', <class 'pygccxml.declarations.variable.variable_t'> |
| 36 | + |
| 37 | +print(str(c1.decl_type), type(c1.decl_type)) |
| 38 | +# > 'int const', <class 'pygccxml.declarations.cpptypes.const_t'> |
| 39 | + |
| 40 | +base = declarations.remove_const(c1.decl_type) |
| 41 | +print(str(base), type(base)) |
| 42 | +# > 'int', <class 'pygccxml.declarations.cpptypes.int_t'> |
| 43 | + |
| 44 | +c2 = global_namespace.variable("c2") |
| 45 | +print(str(c2.decl_type), type(c2.decl_type)) |
| 46 | +# > 'int const', <class 'pygccxml.declarations.cpptypes.const_t'> |
| 47 | +# Even if the declaration was defined as 'const int', pygccxml will always |
| 48 | +# output the const qualifier (and some other qualifiers) on the right hand |
| 49 | +# side (by convention). |
| 50 | + |
| 51 | +cv1 = global_namespace.variable("cv1") |
| 52 | +print(str(cv1.decl_type), type(cv1.decl_type)) |
| 53 | +# > 'int const volatile', <class 'pygccxml.declarations.cpptypes.volatile_t'> |
| 54 | + |
| 55 | +# Remove one level: |
| 56 | +base = declarations.remove_volatile(cv1.decl_type) |
| 57 | +print(str(base), type(base)) |
| 58 | +# > 'int const', <class 'pygccxml.declarations.cpptypes.const_t'> |
| 59 | + |
| 60 | +# Remove the second level: |
| 61 | +base = declarations.remove_const(base) |
| 62 | +print(str(base), type(base)) |
| 63 | +# > 'int', <class 'pygccxml.declarations.cpptypes.int_t'> |
| 64 | + |
| 65 | +# We can also directly do this in one step: |
| 66 | +base = declarations.remove_cv(cv1.decl_type) |
| 67 | +print(str(base), type(base)) |
| 68 | +# > 'int', <class 'pygccxml.declarations.cpptypes.int_t'> |
| 69 | + |
| 70 | +# As for consts, the const and volatile are on the right hand side |
| 71 | +# (by convention), and always in the same order |
| 72 | +cv2 = global_namespace.variable("cv2") |
| 73 | +print(str(cv2.decl_type), type(cv2.decl_type)) |
| 74 | +# > 'int const volatile', <class 'pygccxml.declarations.cpptypes.volatile_t'> |
| 75 | + |
| 76 | +# And a last example with a pointer_t: |
| 77 | +cptr1 = global_namespace.variable("cptr1") |
| 78 | +print(str(cptr1.decl_type), type(cptr1.decl_type)) |
| 79 | +# > 'int const * const', <class 'pygccxml.declarations.cpptypes.const_t'>) |
| 80 | + |
| 81 | +base = declarations.remove_const(cptr1.decl_type) |
| 82 | +print(str(base), type(base)) |
| 83 | +# > 'int const *', <class 'pygccxml.declarations.cpptypes.pointer_t'> |
| 84 | + |
| 85 | +base = declarations.remove_pointer(base) |
| 86 | +print(str(base), type(base)) |
| 87 | +# > 'int const', <class 'pygccxml.declarations.cpptypes.const_t'>) |
| 88 | + |
| 89 | +base = declarations.remove_const(base) |
| 90 | +print(str(base), type(base)) |
| 91 | +# > 'int', <class 'pygccxml.declarations.cpptypes.int_t'>) |
0 commit comments