c++ - OCLint ASTMatcher 规则。匹配 NS_ENUM

标签 c++ clang abstract-syntax-tree oclint libtooling

我正在尝试创建一个匹配 typedef enumtypedef NS_ENUM 声明的 OCLint 规则,但收效甚微。 我有一个 Objective-C 文件 (TestClass.m),其中包含以下枚举声明:

typedef NS_ENUM(NSInteger, TestEnum) {
    TestEnumNone,
    TestEnumSome,
    TestEnumAll
};

typedef enum {
    othertestvalue = 0,
    othertestvalue1,
    othertestvalue2
} OtherTestEnum;

使用此命令转储 AST:

clang -Xclang -ast-dump -fsyntax-only Classes/TestClass.m -- | grep Enum

给我这个包含这个的输出:

|-TypedefDecl 0x7f9d3accd630 <col:1, col:28> col:28 TestEnum 'enum TestEnum':'enum TestEnum'
|-EnumDecl 0x7f9d3accd6a8 prev 0x7f9d3accd530 </System/Library/Frameworks/CoreFoundation.framework/Headers/CFAvailability.h:171:57, Classes/TestClass.m:71:1> line:67:28 TestEnum 'NSInteger':'long'
| |-EnumConstantDecl 0x7f9d3accd738 <line:68:5> col:5 TestEnumNone 'NSInteger':'long'
| |-EnumConstantDecl 0x7f9d3accd788 <line:69:5> col:5 TestEnumSome 'NSInteger':'long'
| `-EnumConstantDecl 0x7f9d3accd7d8 <line:70:5> col:5 TestEnumAll 'NSInteger':'long'
|-EnumDecl 0x7f9d3accd828 <line:73:9, line:77:1> line:73:9
| |-EnumConstantDecl 0x7f9d3accd900 <line:74:5, col:22> col:5 othertestvalue 'int'
| |-EnumConstantDecl 0x7f9d3accd950 <line:75:5> col:5 othertestvalue1 'int'
| `-EnumConstantDecl 0x7f9d3accd9a0 <line:76:5> col:5 othertestvalue2 'int'
|-TypedefDecl 0x7f9d3accda40 <line:73:1, line:77:3> col:3 OtherTestEnum 'enum OtherTestEnum':'OtherTestEnum'

我有一个 ASTMatcherRule (ObjCNsEnumRule),我试图在其中匹配 typedef enum 以及 typedef NS_ENUM 这是其代码:

#include "oclint/AbstractASTMatcherRule.h"
#include "oclint/RuleSet.h"

using namespace std;
using namespace clang;
using namespace clang::ast_matchers;
using namespace oclint;

class ObjCNsEnumRuleRule : public AbstractASTMatcherRule
{
public:
virtual const string name() const override
{
    return "obj c ns enum rule";
}

virtual int priority() const override
{
    return 3;
}

virtual void callback(const MatchFinder::MatchResult &result) override
{
  const EnumDecl *enumDecl = result.Nodes.getNodeAs<EnumDecl>("enum");
  if (enumDecl) {
    addViolation(enumDecl, this, "Found enum");
  }
}

virtual void setUpMatcher() override
{
  addMatcher(enumDecl().bind("enum"));
}

};

static RuleSet rules(new ObjCNsEnumRuleRule());

但是,当我运行这条规则时,我只获得了 typedef enum 声明的输出。

Classes/TestClass.m:73:9: obj c ns enum rule P3 Found enum

我在这里做错了什么?两个枚举都出现在 AST 转储中,但在 OCLint 规则中只有一个匹配。

编辑

我认为这可能与显示在不同源文件中定义的 NS_ENUMEnumDecl 的 AST 转储有关(可能是因为 NS_ENUM 宏),因为我可以匹配 typedef,但不能匹配 enumdecl。

最佳答案

目前在 oclint 中似乎没有办法做到这一点。宏不会暴露给 ASTVisitorASTMatcher 规则的 oclint 规则。看这里:https://github.com/oclint/oclint/issues/148

我最终将其实现为一个简单的 SourceCodeReaderRule,如下所示:

#include "oclint/AbstractSourceCodeReaderRule.h"
#include "oclint/RuleSet.h"
#include <iostream>
#include <string>
#include <regex>

using namespace std;
using namespace oclint;

class TypedefEnumStatementRule : public AbstractSourceCodeReaderRule
{
public:
virtual const string name() const override {
    return "typedef enum statement";
}

virtual int priority() const override {
    return 1;
}

virtual void eachLine(int lineNumber, string line) override {
    regex rgx("typedef\\senum");
    smatch match;
    if (regex_search(line, rgx, regex_constants::match_continuous)) {
        string description = "Enums should not be declared with 'typedef enum' use 'typedef NS_ENUM' instead";
        addViolation(lineNumber, 1, lineNumber, 1, this, description);
    }
}
};

static RuleSet rules(new TypedefEnumStatementRule());

关于c++ - OCLint ASTMatcher 规则。匹配 NS_ENUM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29754500/

相关文章:

c++ - __FUNCTION__ 和 friend 在 Xcode 中表现得很奇怪

clang - 如何从 github 源编译 memcached?

python - 如何编写 Python 调试器/编辑器

c++ - 调用模板时调用重载函数

c++ - 如何将元组从 Python 3.6 传递到 C++

c++ - GCC C++11 删除移动可分配类的复制分配会阻止 std::sort 编译?

llvm - llvm 中的自动矢量化

c++ - 在 Clang AST 中查找声明的父级

c++ - 使用 clang AST Matcher 匹配私有(private)类成员

c++ - MS SQL 存储过程使用 ODBC 返回结果集