首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Clang LibTooling -如何使用DependencyCollector

Clang LibTooling -如何使用DependencyCollector
EN

Stack Overflow用户
提问于 2017-09-18 13:13:48
回答 1查看 316关注 0票数 2

我试图在我的工具中使用Clang的DependencyCollector类来列出文件中的所有依赖项,比如test.cpp

这是我的节目:

代码语言:javascript
复制
#include "stdafx.h"
#include <iostream>
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/CommandLine.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/Utils.h"

using namespace std;
using namespace clang::tooling;
using namespace clang;
using namespace llvm;

static cl::OptionCategory MyToolCategory("my-tool options");
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
static cl::extrahelp MoreHelp("\nMore help text...");

class myDependencyCollector : public DependencyCollector {
private:
public:
    bool sawDependency(StringRef Filename, bool FromModule, bool IsSystem, bool IsModuleFile, bool IsMissing) {
        if (Filename == "stdafx.h" || IsSystem) { 
            return false; 
        } else {
            return true;
        }
    }
    bool needSystemDependencies() {
        return false;
    }
};

class DependencyAction : public PreprocessOnlyAction {
private:
    myDependencyCollector *col;
public:
    virtual bool usesPreprocessOnly() const {
        return true;
    }
    bool BeginSourceFileAction(CompilerInstance &ci) {
        Preprocessor &pp = ci.getPreprocessor();
        col = new myDependencyCollector();
        col->attachToPreprocessor(pp);
        return true;
    }

    void ExecuteAction() {
    }

    virtual void EndSourceFileAction() {
        llvm::ArrayRef<string> arr = col->getDependencies();
        int size = arr.size();
        for (int i = 0; i < size; i = i+1) {
            cout << arr[i] << endl;
        }
    }
};

int main(int argc, const char **argv)
{
    CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
    ClangTool Tool(OptionsParser.getCompilations(), OptionsParser.getSourcePathList());
    int result = Tool.run(newFrontendActionFactory<DependencyAction>().get());
    return result;
}

现在,如果我在上面运行程序,例如文件test.cpp

代码语言:javascript
复制
#include <iostream>
#include "test.h"
void do_math(int *x) {
    *x += 5;
}

int main(void) {
    int result = -1, val = 4;
    do_math(&val);
    return result;
}

这个程序找不到任何内容。

如果有人能帮我,那就太好了,因为在网上搜索了几个小时之后,我一直找不到答案。

EN

回答 1

Stack Overflow用户

发布于 2021-12-29 16:12:01

问题是,您用一个空体覆盖了class PreprocessOnlyAction中的class PreprocessOnlyAction方法。如果删除该行:

代码语言:javascript
复制
void ExecuteAction() {}

一切都如期而至。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46280220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档