【问题标题】:Determining macro expansion for VarDecl in clang在 clang 中确定 VarDecl 的宏扩展
【发布时间】:2020-12-03 01:03:37
【问题描述】:

我是 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 visitor 方法,它遍历每个变量声明以执行适当的 lint 检查:

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

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

【问题讨论】:

    标签: c++ c clang oclint


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-16
      相关资源
      最近更新 更多