0

I need to get information about macro instantiations in my cpp files. Here is example of what I need to do:

#define TEST_PARAM_MACRO( Type ) \
   static Type var;

#define TEST_NAMESPACE_MACRO( Name ) \
   void Name() {}

namespace somewhere::sometimes
{

class MyClass
{
};

// I want to get full type of MyClass, somewhere::sometimes::MyClass
TEST_PARAM_MACRO( MyClass );

// I want to get info that macro was instantiated inside somewhere::sometimes namespace
TEST_NAMESPACE_MACRO( SomeFunc );
}

I can find all macro instantiations with python script

import sys
import clang.cindex


def traverse(node):
    for child in node.get_children():
        traverse(child)
    if node.kind == clang.cindex.CursorKind.MACRO_INSTANTIATION:
        print('Found %s Type %s DATA %s Extent %s [line=%s, col=%s]' % (node.displayname, node.kind, node.data, node.extent, node.location.line, node.location.column))


clang.cindex.Config.set_library_path('/opt/clang-7.0.1/lib64')
index = clang.cindex.Index.create()
tu = index.parse(sys.argv[1], options=clang.cindex.TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD, args=['-std=c++17'])
traverse(tu.cursor)

But MACRO_INSTANTIATION nodes are at top-level, so I can't find, where they were instantiated and I can't figure out how to get parameter info.

Is there a proper way to do it?

clang version 7.0.1 Python clang module version 4.0

Crazy Sage
  • 414
  • 2
  • 14

0 Answers0