使用clang预处理程序将所有宏声明查找为字符串吗?

问题描述 投票:0回答:1
#include<stdio.h>

#define engine_exhaust_gas_temperature_raw 100
#define engine_exhaust_gas_temperature_scaled 20

#define Sum(x, y) ( ( x )+ ( y ) )


int main(){

    printf("%d",engine_exhaust_gas_temperature_raw);

    return 0;
}

我正在研究MISRA C 规则5.4宏标识符应该是不同的] >>为此,我需要C程序中定义为字符串的所有宏的名称列表。

例如:在上面的代码中,我需要:

[“ engine_exhaust_gas_temperature_raw”,“ engine_exhaust_gas_temperature_scaled”,“ Sum”]

有什么方法可以使用AST来获取此列表?

我发现我们可以使用clangshttps://clang.llvm.org/doxygen/classclang_1_1Preprocessor.htmlPreprocessor类将迭代器获取到宏,但是即使这样对我也不产生任何输出。我在以下代码中使用了它。我在这里想念什么?

bool distinct_macro_identifier(CompilerInstance *C_I, ASTContext *Context){

  auto st= C_I->getPreprocessor().macro_begin()->getFirst()->getName();

  auto x= C_I->getPreprocessor().macro_begin()->first;

  llvm::outs()<<x->getName()<<"\n";

  auto p= C_I->getPreprocessor().getMacroInfo(x);

  p->dump();

  return true;
}

## #define engine_exhaust_gas_temperature_raw 100 #define engine_exhaust_gas_temperature_scaled 20 #define Sum(x,y)((x)+(y))int main(){printf(“%d”,...

clang llvm abstract-syntax-tree llvm-clang
1个回答
0
投票

您可以使用此命令进行编译:

© www.soinside.com 2019 - 2024. All rights reserved.