(Not sure if this is the right forum.)
Using the Clang Python API I am trying to figure out if a reference to a C identifier is to a global definition - i.e. the scope of the identifier is more than to just a single file or sub-part thereof. I have a list of globals (by name) that I need to take special steps regarding, but must not do these steps if the global is shadowed by a local, a parameter, etc. Of course, in most cases the global is defined in a different translation unit, so the compiler does not know where it is defined - however it can know that it is not defined in this translation unit.
Most such identifiers would be regular variables/functions/etc. (i.e. referenced by CursorKind.DECL_REF_EXPR), but other could be struct/union/enum types or typedefs (CursorKind.TYPE_REF). Macro references have different but analogous issues (CursorKind.MACRO_INSTANTIATION).
For an “ordinary identifier” reference to global, the code could become aware of it via an include or a local “extern” declaration. Neither StorageClass nor LinkageKind have any valid value in the reference instance (i.e. neither indicate “extern”).
So how do I interrogate the reference to find this information?
If this can’t be solved in the Clang Python API, but can be solved in the Clang C API, then please provide the information on that.
Code outline:
clang_index = clang.cindex.Index.create()
trans_unit = clang_index.parse(filename, options=clang.cindex.TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD)
((code that recurses through nodes starting with trans_unit.cursor))
if node.kind == clang.cindex.CursorKind.DECL_REF_EXPR:
defn = node.get_definition()
is_global = >>> what do I do here? <<<
if is_global:
((perform operations on this reference))
elif node.kind == clang.cindex.CursorKind.TYPE_REF:
>>> need similar determination here <<<
...
elif node.kind == clang.cindex.CursorKind.MACRO_INSTANTIATION:
>>> in what file was macro defined? <<<
...