Running clang tools on .ast files

Hello everyone,

We are working on a ClangTool that analyzes C/C++ projects on the clang AST level.
Our goal is to first store ASTs of the translation units on disk s.t. we later read and analyze them with our tool.
clang++ file.cpp -emit-ast -o file.ast
we generate and store AST files.

However, when reading in .ast files with the tool we run into two problems:

  • If you move source/header files the .ast was generated from to a different location, the tool complains that it can no longer find those. It seems that the source file location is hardcoded in absolute form inside the AST file, which is especially bad if you want to build AST and use the tool on different machines. E.g.
    fatal error: malformed or corrupted AST file: ‘could not find file ‘/home/fba_code/source/lib/memdebug.h’ referenced by AST file ‘/home/fba_code/source/lib/altsvc.ast’’
    1 error generated.

  • mtime changed: analyzing some a.ast, whose source the tool is able to find, it prompts that a header b.h it depends on has been modified, and that we should rebuilt b.ast. E.g.
    fatal error: file ‘/home/fba_code/source/lib/memdebug.h’ has been modified since the AST file ‘/home/fba_code/source/lib/altsvc.ast’ was built: mtime changed
    note: please rebuild precompiled header ‘/home/fba_code/source/lib/altsvc.ast’
    1 error generated.

Are there any flags we can use when doing -emit-ast s.t. the AST files no longer depend on the source files or are otherwise “relocatable”?


1 Like

There’s certainly something we do that can make clang header modules standalone - including the source inside the module. But I don’t know the name of the flags to do that…