在已经匹配了一些东西之后,有什么方法可以与 libclang 匹配者"restart matching"?
Is there any way to "restart matching" with libclang matchers after already having matched something?
class __attribute__((annotate("some_important_string"))) Foo {
public:
void do_something();
};
我想找到具有特定注释的类中的所有公共方法。我设置了一个匹配器来获取带有注释的类:
Matcher.addMatcher(cxxRecordDecl(anyOf(isStruct(), isClass()),
hasAttr(attr::Annotate)).bind("class")
,但似乎没有任何方法来匹配注释的确切字符串。
因此,在我的MatchCallback::run方法中,我通过获取属性dyn_cast
到AnnotateAttr
并检查注释字符串ref:
auto &attrs = decl->getAttrs();
...
auto attribute_attr = dyn_cast<AnnotateAttr>(attr);
auto string_ref = attribute_attr->getAnnotation();
,但现在我更愿意做匹配,而不是AST树遍历。我的理解是匹配器比树遍历函数更稳定,而且它们似乎也更直接。
所以有任何方法回到使用匹配器后过滤类的注释字符串?
我目前基于以下代码进行过滤,它工作正常,但这不是我想要的方式:
if (method->getAccess() != AS_public) {
if (PRINT_SKIPPED_EXPORT_REASONS) printf("%s**%s is not public, skippingn", indentation.c_str(), method_name.c_str());
return;
}
if (method->isOverloadedOperator()) {
if (PRINT_SKIPPED_EXPORT_REASONS) printf("%s**skipping overloaded operator %sn", indentation.c_str(), method_name.c_str());
return;
}
if (dyn_cast<CXXConstructorDecl>(method)) {
if (PRINT_SKIPPED_EXPORT_REASONS) printf("%s**skipping constructor %sn", indentation.c_str(), method_name.c_str());
return;
}
if (dyn_cast<CXXDestructorDecl>(method)) {
if (PRINT_SKIPPED_EXPORT_REASONS) printf("%s**skipping destructor %sn", indentation.c_str(), method_name.c_str());
return;
}
if (method->isPure()) {
assert(method->isVirtual());
if (PRINT_SKIPPED_EXPORT_REASONS) printf("%s**skipping pure virtual %sn", indentation.c_str(), method_name.c_str());
return;
}
我不确定Clang AST Matchers API目前(截至3.9)是否支持重新启动匹配过程。据我所知,MatchFinder
包括匹配节点MatchFinder的方法。match,并匹配整个翻译单元MatchFinder。而不是从提供的节点递归地匹配。
解决这个问题的一个方法是简单地让你的匹配器更复杂,并在一个地方嵌入你想要做的所有匹配(如下)。
#include <string>
#include <iostream>
#include "clang/AST/AST.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Refactoring.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;
using namespace clang::ast_matchers;
using namespace clang::driver;
using namespace clang::tooling;
static llvm::cl::OptionCategory ToolingSampleCategory("Matcher Sample");
class Handler : public MatchFinder::MatchCallback {
public:
Handler()
{}
void run(const MatchFinder::MatchResult &Result) override {
if(const CXXRecordDecl* klass = Result.Nodes.getNodeAs<CXXRecordDecl>("klass"))
{
for(auto it : klass->attrs())
{
Attr& attr = (*it);
auto annotatedAttr = dyn_cast<AnnotateAttr>(&attr);
if((annotatedAttr == nullptr) || (std::string(annotatedAttr->getAnnotation()) != "meh"))
{
return;
}
}
}
if(const CXXMethodDecl* mthd = Result.Nodes.getNodeAs<CXXMethodDecl>("mthd"))
{
std::cout << mthd->getNameInfo().getAsString() << std::endl;
}
}
};
int main(int argc, const char **argv) {
CommonOptionsParser op(argc, argv, ToolingSampleCategory);
RefactoringTool Tool(op.getCompilations(), op.getSourcePathList());
Handler handler;
MatchFinder finder;
finder.addMatcher(
cxxRecordDecl(
hasAttr(attr::Annotate),
forEach(
cxxMethodDecl(
isPublic(),
unless(isPure()),
unless(cxxConstructorDecl()),
unless(cxxDestructorDecl())).bind("mthd")
)).bind("klass"), &handler);
Tool.run(newFrontendActionFactory(&finder).get());
return 0;
}
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 通过方法访问结构
- 最小硬币更换问题(自上而下方法)
- C++为构建时间获取QDateTime的可靠方法
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 使用std::函数映射对象方法
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- C++从另一个类访问公共静态向量的正确方法是什么
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 没有为自己的结构调用列表推回方法
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 在类定义之后定义一个私有方法
- 枚举环境变量的惯用C++14/C++17方法
- 在已经匹配了一些东西之后,有什么方法可以与 libclang 匹配者"restart matching"?