#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;
}
##
您可以使用此命令进行编译: