3

我在libastmatchers使用Postgres 源时遇到了 CLang 的问题:它找不到包含文件。仅当从两个文件创建 CLangTool 时才会重现此错误。如果是为单独的文件创建的,则没有错误并且功能匹配成功。

完整错误:

/home/myuser/postgres/src/conditional.c:1:10: fatal error: 'pg_config_ext.h' file not found
#include "pg_config_ext.h"
         ^~~~~~~~~~~~~~~~~
1 error generated.
Error while processing /home/myuser/postgres/src/conditional.c.

目录结构:

postgres/
|-- compile_commands.json
|-- pg_config_ext.h
`-- src
    |-- backend
    |   `-- nodeHash.c
    `-- conditional.c

文件内容:

  • nodeHash.c 为空;
  • 条件.c:
#include "pg_config_ext.h"
  • pg_config_ext.h:
int f();
  • compile_commands.json:
[ {
        "arguments": [
            "clang",
            "-c",
            "-I..",
            "/home/myuser/postgres/src/conditional.c"
        ],
        "directory": "/home/myuser/postgres/src",
        "file": "/home/myuser/postgres/src/conditional.c"
    },
    {
        "arguments": [
            "clang",
            "-c",
            "-I../..",
            "/home/myuser/postgres/src/backend/nodeHash.c"
        ],
        "directory": "/home/myuser/postgres/src/backend",
        "file": "/home/myuser/postgres/src/backend/nodeHash.c"
    }
]
  • CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)
project(untitled)

set(CMAKE_CXX_STANDARD 17)

find_package(Clang REQUIRED)
include_directories(${CLANG_INCLUDE_DIRS})
add_definitions(${CLANG_DEFINITIONS})

add_executable(clang_error main.cpp)
target_link_libraries(clang_error PUBLIC clangTooling clangBasic clangASTMatchers)
  • 主.cpp:
#include <clang/ASTMatchers/ASTMatchFinder.h>
#include <clang/Frontend/FrontendActions.h>
#include <clang/Tooling/CommonOptionsParser.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Tooling/Tooling.h>

#include <string>
#include <vector>
#include <memory>
#include <iostream>

using namespace clang::ast_matchers;
using namespace clang;
using namespace clang::tooling;
using namespace std;

string prefix = "/home/myuser/";
string ccPath = prefix + "postgres/";
vector<string> files = {
        prefix + "postgres/src/backend/nodeHash.c", // if you comment out this line, there will be no error
        prefix + "postgres/src/conditional.c"
};

class Fetcher : public MatchFinder::MatchCallback {
public:
    void run(const MatchFinder::MatchResult &Result) override {
        if (const auto *FS = Result.Nodes.getNodeAs<FunctionDecl>("function")) {
            std::cout << "Matched"; // Matches only if one file, not two
        }
    }
};

int main() {
    unique_ptr<clang::tooling::ClangTool> clangTool;
    unique_ptr<Fetcher> fetcherInstance;
    MatchFinder finder;

    static const DeclarationMatcher functionMatcher = functionDecl().bind("function");

    string errMsg;
    shared_ptr<clang::tooling::CompilationDatabase> cDb = clang::tooling::CompilationDatabase::autoDetectFromDirectory(ccPath, errMsg);
    std::cout << errMsg; // No output
    clangTool = std::make_unique<ClangTool>(*cDb, files);
    fetcherInstance = std::make_unique<Fetcher>();
    finder.addMatcher(functionMatcher, fetcherInstance.get());

    clangTool->run(newFrontendActionFactory(&finder).get());
}

我正在使用 CLang 版本10.0.0

也许,是同一个问题,但没有答案。

4

0 回答 0