0

I wrote my own clang tool following https://clang.llvm.org/docs/LibASTMatchersTutorial.html

The purpose of the tool is to generate diagrams based on specific source files. Until now as a prototype I worked with some basic cpp code which didn't have any dependencies. However the target project is large and uses CMake, leading to include errors when I run the tool (as expected).

I found this question with a similar problem: clang tool : include path, however due to the scale of the project I think supplying the include paths one by one like that is not really viable.

Is it possible to somehow reuse the CMake structure to feed in include paths, or recursively look for headers inside the root folder?

Rngaddict
  • 3
  • 2
  • 1
    Generate compile_commands.json and parse it using your tool, to extract include paths for each file, just like clangd? Could you explain clerer what do you mean by "reuse the CMake structure" (what is "the CMake structure"? In what way do you want to "reuse" it?) to "feed" ("feed" in what way?) include paths (what include paths?)? To "recursively look for headers", just `find` the headers, no? And, also CMAKE_CXX_COMPILER_LAUNCHER, but it really depends on how your tool works. – KamilCuk Nov 09 '21 at 13:29
  • Thanks for the hints! – Rngaddict Nov 09 '21 at 14:51
  • 1. I'll have to double check if compile_commands.json is available.. we build the project using a python script and I'm generally not familiar with it. 2. By CMake structure I meant the CMakeLists.txt and .cmake files which contain all the source files used for the build. I figured this could somehow be used, but I don't exactly have an idea on how. 3. Searching within the clang tool could work. I'm a bit worried about layered dependencies - the .hpps referred in the .cpp I pass to the tool will have further layers of dependencies. I guess it should be possible to find them. – Rngaddict Nov 09 '21 at 15:01
  • `target_link_libraries` and `target_include_directories` combined with the appropriate visibilities should allow you make the appropriate include directories available to dependencies. – fabian Nov 09 '21 at 18:17
  • Could you elaborate on this a bit please? I'm quite inexperienced with clang and compilers in general. I don't really understand how this applies, since I'm processing a single source file with an executable. Where do the CMake flags come in? – Rngaddict Nov 10 '21 at 11:34

2 Answers2

0

A colleague explained to me that configuring CMake and parsing the compile_commands.json into the tool is the way to go. Just now I was able to do this and I have the compile_commands.json, however I have to do some further research on how exactly I can parse that.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Rngaddict
  • 3
  • 2
0

The correct way to achieve this is indeed by creating the compile_commands.json file by setting the cmake option CMAKE_EXPORT_COMPILE_COMMANDS=ON. To parse it to your clang tool, you need to use the command line parameter -p <BUILD_PATH> where <BUILD_PATH> is the path to the compile_commands.json file. And as a hint: do not provide a double dash -- as a command line option to your tool since then the tool will not use the compilation database in the compile_commands.json but is instead expecting extra arguments for the compiler after the --.

mpeschke
  • 308
  • 3
  • 11