[cfe-dev] Getting absolute paths of clang::Decls with AllTUsToolExecutor and VFS

E via cfe-dev cfe-dev at lists.llvm.org
Tue Dec 29 23:20:30 PST 2020


I have a program that uses ASTMatchers to find clang::Decls and print the absolute path of the files they're defined in. The path where the declaration is made is found using:

std::string path = decl->getASTContext().getSourceManager().getFilename(decl->getLocation()).str();

The path is then canonicalized into an absolute path, which uses the current working directory of the process. This works very well under ClangTool in single threaded mode, and I thank the LibTooling contributors for their excellent work.

To speed up the tool, I have used AllTUsToolExecutor executor to parallelize execution. However, this returns invalid paths from the line of code above. I suspect the cause is the inability of AllTUsToolExecutor to change its working directory due to the usage of VFS, which does not happen under the physical file system that ClangTool uses.

Take the following compile_commands.json file for example.

  "directory": "/home/e/proj/build",
  "command": "clang++ /home/proj/src/main.cpp",
  "file": "/home/e/proj/src/main.cpp"

When using ClangTool, the cwd of my ASTMatcher will be /home/e/proj/build and path (from above) will be "../src/main.cpp". This is then canonicalized and turned into the absolute path /home/e/proj/src/main.cpp.

When using AllTUsToolExecutor, the cwd of my ASTMatcher will be /home/e/proj and a VFS will be used. path (from above) will be "../src/main.cpp" once again, but since the cwd is different the full path will be . This is then turned into the absolute path /home/e/proj/../src/main.cpp, which does not exist!

Ultimately I am looking for a way to get the absolute path of a clang::Decl while also using AllTUsToolExecutor. Unfortunately, it seems like the builtin functionality does not work due to the nuances of the VFS system.

I apologize for the long text and appreciate any responses. Thank you.

More information about the cfe-dev mailing list