我不熟悉使用ASTMatcher并遵循教程中的代码 - https://github.com/peter-can-talk/cppnow-2017。在这里,可以使用以下命令运行工具clang-variables:
fatal error: 'stddef.h' file not found
#include <stddef.h>
^~~~~~~~~~
如果我使用其他源文件,而不是test.cpp文件,那么我收到以下错误:
clang-variables: $(TARGET).cpp
$(CXX) $(HEADERS) $(LDFLAGS) $(CXXFLAGS) $(TARGET).cpp $(LIBS) -o $(TARGET) -I$(START_DIR)/source -I$(HOME_ROOT)/extern/include
我知道我的源文件包含需要包含的头文件。我尝试将它们包含在Makefile中,如下所示,但错误仍然存在:
// Clang includes
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/Type.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
// LLVM includes
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/raw_ostream.h"
// Standard includes
#include <memory>
#include <string>
#include <vector>
namespace Mutator {
/// Callback class for clang-variable matches.
class MatchHandler : public clang::ast_matchers::MatchFinder::MatchCallback {
public:
using MatchResult = clang::ast_matchers::MatchFinder::MatchResult;
/// Handles the matched variable.
///
/// Checks if the name of the matched variable is either empty or prefixed
/// with `clang_` else emits a diagnostic and FixItHint.
void run(const MatchResult& Result) {
const clang::VarDecl* Variable =
Result.Nodes.getNodeAs<clang::VarDecl>("clang");
const llvm::StringRef Name = Variable->getName();
if (Name.empty() || Name.startswith("clang_")) return;
clang::DiagnosticsEngine& Engine = Result.Context->getDiagnostics();
const unsigned ID =
Engine.getCustomDiagID(clang::DiagnosticsEngine::Warning,
"found mutating variable");
/// Hint to the user to prefix the variable with 'clang_'.
const clang::FixItHint FixIt =
clang::FixItHint::CreateInsertion(Variable->getLocation(), "precision & accuracy mutation");
Engine.Report(Variable->getLocation(), ID).AddFixItHint(FixIt);
}
}; // namespace Mutator
/// Dispatches the ASTMatcher.
class Consumer : public clang::ASTConsumer {
public:
/// Creates the matcher for clang variables and dispatches it on the TU.
void HandleTranslationUnit(clang::ASTContext& Context) override {
using namespace clang::ast_matchers; // NOLINT(build/namespaces)
const auto Matcher = declaratorDecl(
isExpansionInMainFile(),
hasType(asString("int"))
).bind("clang");
/*
// clang-format off
const auto Matcher = varDecl(
isExpansionInMainFile(),
hasType(isConstQualified()), // const
hasInitializer(
hasType(cxxRecordDecl(
isLambda(), // lambda
has(functionTemplateDecl( // auto
has(cxxMethodDecl(
isNoThrow(), // noexcept
hasBody(compoundStmt(hasDescendant(gotoStmt()))) // goto
)))))))).bind("clang");
// clang-format on
*/
MatchHandler Handler;
MatchFinder MatchFinder;
MatchFinder.addMatcher(Matcher, &Handler);
MatchFinder.matchAST(Context);
}
};
/// Creates an `ASTConsumer` and logs begin and end of file processing.
class Action : public clang::ASTFrontendAction {
public:
using ASTConsumerPointer = std::unique_ptr<clang::ASTConsumer>;
ASTConsumerPointer CreateASTConsumer(clang::CompilerInstance& Compiler,
llvm::StringRef) override {
return std::make_unique<Consumer>();
}
bool BeginSourceFileAction(clang::CompilerInstance& Compiler,
llvm::StringRef Filename) override {
llvm::errs() << "Processing " << Filename << "\n\n";
return true;
}
void EndSourceFileAction() override {
llvm::errs() << "\nFinished processing file ...\n";
}
};
} // namespace Mutator
namespace {
llvm::cl::OptionCategory ToolCategory("clang-variables options");
llvm::cl::extrahelp MoreHelp(R"(
Finds all Const Lambdas, that take an Auto parameter, are declared Noexcept
and have a Goto statement inside, e.g.:
const auto lambda = [] (auto) noexcept {
bool done = true;
flip: done = !done;
if (!done) goto flip;
}
)");
llvm::cl::extrahelp
CommonHelp(clang::tooling::CommonOptionsParser::HelpMessage);
} // namespace
auto main(int argc, const char* argv[]) -> int {
using namespace clang::tooling;
CommonOptionsParser OptionsParser(argc, argv, ToolCategory);
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
const auto Action = newFrontendActionFactory<Mutator::Action>();
return Tool.run(Action.get());
}
编译时没有错误。所以我想知道,是否可以提及包含文件作为ASTMatcher的参数?请找到以下代码:
CREATE TABLE #tablename(column datatype,column1 datatype,...)
我感谢任何帮助。
答案 0 :(得分:3)
Clang工具实例化一个编译器对象以生成AST。与从分发中安装的编译器(在构建项目时调用)不同,此编译器对象未配置头路径信息。
有(至少)两种方法来添加该信息:使用标准头路径配置编译器,或将路径添加到编译数据库。
首先,您可以使用ClangTool::appendArgumentsAdjuster()
方法以编程方式添加路径。以下是来自CoARCT(https://github.com/lanl/CoARCT)中的apps / FuncLister.cc的示例:
ClangTool tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
// add header search paths to compiler
ArgumentsAdjuster ardj1 =
getInsertArgumentAdjuster(corct::clang_inc_dir1.c_str());
ArgumentsAdjuster ardj2 =
getInsertArgumentAdjuster(corct::clang_inc_dir2.c_str());
tool.appendArgumentsAdjuster(ardj1);
tool.appendArgumentsAdjuster(ardj2);
if(verbose_compiler){
ArgumentsAdjuster ardj3 = getInsertArgumentAdjuster("-v");
tool.appendArgumentsAdjuster(ardj3);
}
CoARCT通过几个步骤定义包含目录:首先,顶级CMakeLists.txt猜测目录所在的位置并将该信息放入宏中;第二,lib / utilities.h以编译器标志的形式将宏放入clang_inc_dir1/2
个字符串(即clang_inc_dir1="-I/path1/to/headers"
);然后像FuncLister.cc这样的客户端将这些作为appendArgumentsAdjuster()的参数。
添加编译器搜索路径的第二种方法是更改编译数据库中的编译器命令。如果您使用的是CMake,请将set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
添加到顶级CMakeLists.txt。这应该在构建目录中生成一个名为compile_commands.json的编译数据库文件。每个源文件都有一个条目;该条目将包括
"command":"compiler command line here"
您可以在编译命令中为要运行工具的任何文件添加-I/path/to/headers
。然后,您将使用类似
clang-variables test.cpp -p /path/to/compile_commands.json
如果您的项目未使用CMake,则此处描述数据库文件的结构:https://clang.llvm.org/docs/JSONCompilationDatabase.html。