C++ OCLint astmacher规则。匹配NS_枚举

C++ OCLint astmacher规则。匹配NS_枚举,c++,clang,abstract-syntax-tree,oclint,libtooling,C++,Clang,Abstract Syntax Tree,Oclint,Libtooling,我正在尝试创建一个OCLint规则,该规则同时匹配typedef enum和typedef NS_enum声明,但收效甚微。 我有一个Objective-C文件(TestClass.m),其中包含以下枚举声明: typedef NS_ENUM(NSInteger, TestEnum) { TestEnumNone, TestEnumSome, TestEnumAll }; typedef enum { othertestvalue = 0, othert

我正在尝试创建一个OCLint规则,该规则同时匹配
typedef enum
typedef NS_enum
声明,但收效甚微。 我有一个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'
但是,当我运行此规则时,我只获得
typedef enum
声明的输出

Classes/TestClass.m:73:9: obj c ns enum rule P3 Found enum
我做错了什么?两个枚举都显示在AST转储中,但在OCLint规则中只有一个匹配

编辑


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

在oclint中目前似乎没有这样做的方法。宏不暴露于
ASTVisitor
ASTMatcher
规则的oclint规则。请看这里:

我最终实现了一个简单的
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());
#包括“oclint/AbstractSourceCodeReaderRule.h”
#包括“oclint/RuleSet.h”
#包括
#包括
#包括
使用名称空间std;
使用名称空间oclint;
类TypeDefensumStatementRule:public AbstractSourceCodeReaderRule
{
公众:
虚拟常量字符串名称()常量重写{
返回“typedef枚举语句”;
}
虚int priority()常量重写{
返回1;
}
虚空eachLine(整数行号、字符串行)重写{
正则表达式rgx(“typedef\\senum”);
小比赛;
if(正则表达式搜索(行、rgx、正则表达式常量::匹配连续)){
string description=“不应使用'typedef enum'声明枚举,而应使用'typedef NS_enum';
addViolation(行号,1,行号,1,此,描述);
}
}
};
静态规则集规则(新的TypeDefensionStatementRule());

oclint 0.13.1也存在同样的问题,没有为NS_ENUM获取EnumDecl。
#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());