When I write standalone tool based on ClangTool I ususally use CommonOptionsParser.
CommonOptionParser provides option --extra-arg-before= that allow me to pass include directories to my clang-based tool.
For example: ./tool --extra-arg-before=’/home/user/llvm_9/llvm_9-build/lib/clang/9.0.0/include -p /my/build/path /path/to/source.cpp. This invocation allow me to avoid preprocessor errors/
In general, my question is
how can I match AST nodes in A) source code in StringRef
B) source code in file
without run ClangTool instance?
The problem is that I need include header files in source files for analysis.
For example code:
#define SOME_CODE R"(
auto M = compoundStmt(hasAncestor(functionDecl(hasName(fooName1)))).bind(“bindStr”)
std::unique_ptr Unit = buildASTFromCode(SOME_CODE , “pseudoFile.cpp”)
auto MatchRes = match(M , Unit->getASTContext());
Node *Result = const_cast<Node *>(MatchRes.template getNodeAs(“bindStr”));
So, when I write #include <stdio.h> in src file and run this non-ClangTool-based matcher this leads to errors:
In file included from pseudoFile.cpp:2:
/usr/include/stdio.h:33:10: fatal error: ‘stddef.h’ file not found
How can we pass some extra args (same as CommonOptionsParser in ClangTool-based tool)?
Maybe the solution is to use buildASTFromCodeWithArgs or passing compile database entry for this source file then calling match method?