This has been asked twice already one answer seems very popular:
How to use compile_commands.json with clang python bindings?
This other one not as much: How to use compile_commands.json with llvm clang (version 7.0.1) python bindings?
However neither solution seems to work. If you try the most popular solution, i.e. if you do this:
index = clang.cindex.Index.create()
compdb = clang.cindex.CompilationDatabase.fromDirectory('/home/makogan/neverengine_personal/build/')
file_args = compdb.getCompileCommands(path)
translation_unit = index.parse(path, file_args)
You will get this error:
Traceback (most recent call last):
File "/home/makogan/neverengine_personal/Source/../Scripts/notebook_generator.py", line 178, in <module>
main()
File "/home/makogan/neverengine_personal/Source/../Scripts/notebook_generator.py", line 175, in main
CreateNotebookFromHeader(args.path, args.out_path)
File "/home/makogan/neverengine_personal/Source/../Scripts/notebook_generator.py", line 165, in CreateNotebookFromHeader
nb['cells'] = GetHeaderCellPrototypes(path)
File "/home/makogan/neverengine_personal/Source/../Scripts/notebook_generator.py", line 148, in GetHeaderCellPrototypes
tokens = ExtractTokensOfInterest(path)
File "/home/makogan/neverengine_personal/Source/../Scripts/notebook_generator.py", line 54, in ExtractTokensOfInterest
translation_unit = index.parse(path, file_args)
File "/home/makogan/.local/lib/python3.9/site-packages/clang/cindex.py", line 2688, in parse
return TranslationUnit.from_source(path, args, unsaved_files, options,
File "/home/makogan/.local/lib/python3.9/site-packages/clang/cindex.py", line 2783, in from_source
args_array = (c_char_p * len(args))(*[b(x) for x in args])
File "/home/makogan/.local/lib/python3.9/site-packages/clang/cindex.py", line 2783, in <listcomp>
args_array = (c_char_p * len(args))(*[b(x) for x in args])
File "/home/makogan/.local/lib/python3.9/site-packages/clang/cindex.py", line 109, in b
return x.encode('utf8')
AttributeError: 'CompileCommand' object has no attribute 'encode'
If you try the second option:
I.e. this:
print( list(iter(file_args).next().arguments))
You get this error:
AttributeError: 'iterator' object has no attribute 'next'
How are people getting that first solution to work for them?
The correct way seems to be doing this:
index = clang.cindex.Index.create()
token_dict = {}
compdb = clang.cindex.CompilationDatabase.fromDirectory('/home/makogan/neverengine_personal/build/')
commands = compdb.getCompileCommands(path)
file_args = []
for command in commands:
for argument in command.arguments:
file_args.append(argument)
translation_unit = index.parse(path, file_args)