clang - 编写 AST 匹配器以查找所有没有 break 语句的 case 语句

标签 clang llvm llvm-clang clang-ast-matchers clang-query

我想找到所有没有 break 语句的 case 语句。我使用 clang-query 来构建我的匹配器。我的匹配器在某些测试用例中失败。

我把简单的匹配器写成

match caseStmt(unless(has(breakStmt())))

它适用于以下测试用例

#include<stdlib.h>

int main(){

int x;
switch(x){

  case 1:
     break;

  case 2:


  default:
     x++;
}
return 0;
}

enter image description here

int main() 
{ 
    int x = 1, y = 2; 

    // Outer Switch 
    switch (x) { 

    // If x == 1 
    case 1: 

        // Nested Switch 

        switch (y) { 

        // If y == 2 
        case 2: 

            //break; 

        // If y == 3 
        case 3: 

            break; 
        } 
        break; 

    // If x == 4 
    case 4: 

        break; 

    // If x == 5 
    case 5: 

        break; 

    default: 

        break; 
    } 
    return 0; 
} 

enter image description here 不适用于以下

#include <iostream> 
using namespace std; 

int main() 
{ 
    int x = 1, y = 2; 

    // Outer Switch 
    switch (x) { 

    // If x == 1 
    case 1: 

        // Nested Switch 

        switch (y) { 

        // If y == 2 
        case 2: 
            cout << "Choice is 2"; 
            //break; 

        // If y == 3 
        case 3: 
            cout << "Choice is 3"; 
            break; 
        } 
        //break; 

    // If x == 4 
    case 4: 
        cout << "Choice is 4"; 
        break; 

    // If x == 5 
    case 5: 
        cout << "Choice is 5"; 
        break; 

    default: 
        cout << "Choice is other than 1, 2 3, 4, or 5"; 
        break; 
    } 
    return 0; 
} 

enter image description here 在上面的案例中,它显示了带有 break 语句的 case 语句以及没有 break 语句的 case 语句。

我做错了什么?请帮忙 :) 我正在关注这个 http://releases.llvm.org/8.0.0/tools/clang/docs/LibASTMatchersTutorial.html

最佳答案

不幸的是,这是行不通的:-(

case技术上是 label , 和 label只有一个语句作为其子项。如果你打印出 AST,你会看到 casebreak语句将处于同一级别:

    |   |-CaseStmt 0x5618732e1e30 <line:29:3, line:30:9>
    |   | |-IntegerLiteral 0x5618732e1e10 <line:29:8> 'int' 4
    |   | |-<<<NULL>>>
    |   | `-CallExpr 0x5618732e1f00 <line:30:5, col:9> 'void'
    |   |   `-ImplicitCastExpr 0x5618732e1ee8 <col:5> 'void (*)()' <FunctionToPointerDecay>
    |   |     `-DeclRefExpr 0x5618732e1ec0 <col:5> 'void ()' lvalue Function 0x5618732e16d0 'foo' 'void ()'
    |   |-BreakStmt 0x5618732e1f28 <line:31:5>
    |   |-CaseStmt 0x5618732e1f50 <line:34:3, line:35:9>
    |   | |-IntegerLiteral 0x5618732e1f30 <line:34:8> 'int' 5
    |   | |-<<<NULL>>>
    |   | `-CallExpr 0x5618732e2020 <line:35:5, col:9> 'void'
    |   |   `-ImplicitCastExpr 0x5618732e2008 <col:5> 'void (*)()' <FunctionToPointerDecay>
    |   |     `-DeclRefExpr 0x5618732e1fe0 <col:5> 'void ()' lvalue Function 0x5618732e16d0 'foo' 'void ()'
    |   |-BreakStmt 0x5618732e2048 <line:36:5>

在这里你可以看到CallExprCaseStmt 的 child 同时 BreakStmt不是。

注意:为了使示例更容易一些,我替换了 std::cout << "..."foo() .

您必须编写一个 更复杂的匹配器来获取cases。没有 break他们之间的陈述和以下 cases .

我希望这仍然有用。

关于clang - 编写 AST 匹配器以查找所有没有 break 语句的 case 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57384080/

相关文章:

opencl - 如何将 opencl-kernel-file(.cl) 编译为 LLVM IR

c++ - MacOS 上的 Clang 包含问题

c - 使用 `setp` 而不是 `setb` 有优势吗?

c++ - 如何在 Clang 中启用内联函数的编译?

c++ - 为依赖于 LLVM 的项目创建 CMakeLists

c++ - 在LLVM中获取全局字符串值

c - 如何从 clang 获取更细粒度的行/列调试信息?

c++ - 修复时 clang-tidy 插入多个 'override' 说明符

c++ - 如何在 LLVM 模块中插入函数

c++ - LLVM : inject debugging lines in C++ source code