gpt4 book ai didi

clang - fatal error : 'stddef.h' file not found when using clang-llvm ASTMatcher

转载 作者:行者123 更新时间:2023-12-03 16:52:26 27 4
gpt4 key购买 nike

我刚开始使用 ASTMatcher 并遵循教程中的代码 - https://github.com/peter-can-talk/cppnow-2017 .在这里,可以使用以下命令运行工具 clang-variables:

cd code/clang-variables 
docker run -it -v $PWD:/home clang
root@5196c095092d:/home# ./clang-variables test.cpp -- -std=c++14

如果我使用另一个源文件,而不是 test.cpp 文件,则会出现以下错误:
fatal error: 'stddef.h' file not found
#include <stddef.h>
^~~~~~~~~~

我知道我的源文件有这些需要包含的头文件。我尝试将它们包含在 Makefile 中,如下所示,但错误仍然存​​在:
clang-variables: $(TARGET).cpp
$(CXX) $(HEADERS) $(LDFLAGS) $(CXXFLAGS) $(TARGET).cpp $(LIBS) -o $(TARGET) -I$(START_DIR)/source -I$(HOME_ROOT)/extern/include

编译时没有错误。所以我想知道,是否可以将包含文件作为 ASTMatcher 的参数提及?请在下面找到代码:
// 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());
}

我很感激任何帮助。

最佳答案

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 .

关于clang - fatal error : 'stddef.h' file not found when using clang-llvm ASTMatcher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48829339/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com