Getting absolute paths of clang::Decls with AllTUsToolExecutor and VFS


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.

I have found the solution for my problem, and would like to describe
it here for the benefit of other users who may come across this.

This problem has already been solved in clangd, where they
define the function getCanonicalPath(), which you can find at
the link below.

In effect, this function defers to whatever VFS is being used
by the current ClangTool, and uses that to get the _true_
absolute path regardless of the current working dir of the
process is.

Apologies for cluttering the mailing list, I hope my solution helps.
Sometimes talking out loud helps you find your problem :slight_smile:

Thank you.