1

I am having a problem. I had a python script that was setup using the (ancient) clang-3.4, which was taking in header files (.h), parsed them and got all the macros (#define) together with comments after the define on the same line.

E.g. the full following line was parsed:

#define SOME_DEFINE 10 /* IN_SomeDefine */

The problem is that with newer versions of clang (in my case, clang-11), this doesn't work anymore and I want to port it somehow.

The relevant part of code from the script is:

index = Index.create()
tu = index.Parse(None, [filename] + args,
                 options=TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD)

macro_defs = [m for m in tu.cursor.get_children()
              if m.kind in (CursorKind.MACRO_INSTANTIATION, CursorKind.MACRO_DEFINITION)]

for macro in macro_defs:
    tokens = list(macro.get_tokens())
    # process tokens here

Before, the "macro" cursor had all tokens from the macro definition line, so the tokens were : #define, SOME_DEFINE and /* IN_SomeDefine */. Now, with the latest clang version, the tokens are only : #define and SOME_DEFINE. The thing is that I need the full line to be parsed in the cursor.

I need to make pairs from the macro value and the comment "value" ... basically, from my example, I would have : key = 10 and value = IN_SomeDefine.

Any help would be much appreciated - I have already searched all around but didn't seem to find anything that solved the problem.

The "args" passed in the "index.parse" instruction are some include directives ("-I") and some definition directives ("-D"). P.S. I have also tried to pass "-fparse-all-comments", "-CC", "-C" - these don't seem to work.

I also have to mention that I am working in python virtual env and the python version is 2.7 .

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
w0wy
  • 21
  • 3
  • `/* IN_SomeDefine */` is a **comment** (and indeed they should have change logic about comment). – Jarod42 Mar 10 '21 at 10:02
  • You might be interested by [retrieving-comments-using-python-libclang](https://stackoverflow.com/questions/19079070/retrieving-comments-using-python-libclang). – Jarod42 Mar 10 '21 at 10:05

0 Answers0