1

我是 C++ 的初学者,试图用 clang 对 Objective-C 代码进行 lint。我知道在使用 AST 访问节点和属性之前首先扩展宏。

我有一个名为 的宏NIL_CHECK,用于许多文件。在执行 lint 时,我想跳过扩展/使用此宏的行的变量声明。

例如,这个例子中的第一行应该被 linted,而第二行需要被跳过,这样当有一个宏扩展时就不会抛出误报:

// Must be checked
NSDictionary *playerParams = @{ @"videoId" : videoId, @"playerVars" : playerVars }; 

// Must be skipped since there's a macro
PlayerProfile *const playerProfile = [[PlayerProfile alloc] initWithData:NIL_CHECK(playerParams)]; 

这是VisitVarDecl访问者方法,它遍历每个变量声明以执行适当的 lint 检查:

    bool VisitVarDecl(VarDecl *node) {
        if (isCollectionType(node -> getType()) && !hasTypeArguments(node -> getType())) {
            addViolation(node, this, description(node -> getNameAsString()));
        }
        return true;
    }

如何确定宏并跳过此类变量声明?

4

1 回答 1

0

是 Valeriy 的一个很好的答案,我认为它涵盖了您想要实现的目标。

总结一下:您想NIL_CHECK在 VarDecl 中找到字符串,当您访问 AST 时,该字符串已经被扩展。您的源代码中的原文可以通过Lexer. 您可以使用完整的 varDecl expr 的位置或仅使用包含宏的部分。然后可以从getSourceTextLexer 返回的字符串中检测到宏名称。

于 2020-12-05T11:38:21.640 回答