【问题标题】:Get list of methods in class using clang使用clang获取类中的方法列表
【发布时间】:2014-02-19 10:49:56
【问题描述】:

在常见的 IDE(选择一个)中,您通常有一个大纲视图,显示特定类的方法列表。

假设我在IFoo.h 中有一个 C++ 接口类,如下所示:

#ifndef IFOO_H_
#define IFOO_H_
class IFoo { 
    public:
        virtual ~IFoo() {}
        virtual void bar() = 0;
};
#endif

如何(以编程方式)使用 clang 库为上面的 IFoo.h 文件获取这样的 IDE 大纲列表?首先,如果我能获得函数名称列表会有所帮助。

我特别打算使用clang,因此非常感谢任何有关如何使用clang分析我的头文件的帮助。

同时我会在这里看一下clang教程:https://github.com/loarabia/Clang-tutorial

提前感谢您的帮助。

【问题讨论】:

    标签: c++ parsing clang code-inspection outliner


    【解决方案1】:

    我浏览了这个教程http://clang.llvm.org/docs/LibASTMatchersTutorial.html 并在那里发现了一些非常有用的东西,这就是我想出的:

    我必须将我的文件从 IFoo.h 重命名为 IFoo.hpp 才能被检测为 Cxx 而不是 C 代码。

    我必须使用-x c++ 调用我的程序才能让我的IFoo.h 文件被识别为C++ 代码而不是C 代码(默认情况下clang 将*.h 文件解释为C:

    ~/Development/llvm-build/bin/mytool ~/IFoo.h -- -x c++
    

    这是我从提供的类中转储所有虚函数的代码:

    // Declares clang::SyntaxOnlyAction.
    #include "clang/Frontend/FrontendActions.h"
    #include "clang/Tooling/CommonOptionsParser.h"
    #include "clang/Tooling/Tooling.h"
    #include "clang/ASTMatchers/ASTMatchers.h"
    // Declares llvm::cl::extrahelp.
    #include "llvm/Support/CommandLine.h"
    
    #include "clang/ASTMatchers/ASTMatchers.h"
    #include "clang/ASTMatchers/ASTMatchFinder.h"
    
    #include <cstdio>
    
    using namespace clang;
    using namespace clang::ast_matchers;
    using namespace clang::tooling;
    using namespace llvm;
    
    DeclarationMatcher methodMatcher = methodDecl(isVirtual()).bind("methods");
    
    class MethodPrinter : public MatchFinder::MatchCallback {
    public :
      virtual void run(const MatchFinder::MatchResult &Result) {
        if (const CXXMethodDecl *md = Result.Nodes.getNodeAs<clang::CXXMethodDecl>("methods")) {    
          md->dump();
        }
      }
    };
    
    // CommonOptionsParser declares HelpMessage with a description of the common
    // command-line options related to the compilation database and input files.
    // It's nice to have this help message in all tools.
    static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
    
    // A help message for this specific tool can be added afterwards.
    static cl::extrahelp MoreHelp("\nMore help text...");
    
    int main(int argc, const char **argv) {    
      cl::OptionCategory cat("myname", "mydescription");
      CommonOptionsParser optionsParser(argc, argv, cat, 0);    
    
      ClangTool tool(optionsParser.getCompilations(), optionsParser.getSourcePathList());
    
      MethodPrinter printer;
      MatchFinder finder;
      finder.addMatcher(methodMatcher, &printer);
      return tool.run(newFrontendActionFactory(&finder));
    }
    

    当传递IFoo.h 文件时,输出如下所示:

    CXXDestructorDecl 0x1709c30 <~/IFoo.h:5:3, col:20> ~IFoo 'void (void)' virtual
    `-CompoundStmt 0x1758128 <col:19, col:20>
    CXXMethodDecl 0x1757e60 <~/IFoo.h:6:3, col:24> bar 'void (void)' virtual pure
    

    【讨论】:

    • 您是否调查过如何检查特定访问级别的方法?我不确定如何只检查公共方法。具体来说,我需要最后一个公共方法的位置。
    • 我将在今天晚些时候或明天早上回到我的电脑前向您展示如何操作。我现在手头只有一部手机。
    • 我在这里发布了这个问题:stackoverflow.com/questions/24564494/… 如果你想回答。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 2010-11-19
    • 2016-03-24
    • 2011-09-22
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多