clang - 使用 clang 预处理器将所有宏声明作为字符串查找?
问题描述
#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”,“总和”]
有什么方法可以使用 clang AST 获取此列表?
我发现我们可以使用 clangs https://clang.llvm.org/doxygen/classclang_1_1Preprocessor.html预处理器类来获取宏的迭代器,但即使这样也不会为我产生任何输出。我在下面的代码中使用了它。我在这里想念什么?
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;
}
解决方案
您可以使用以下命令编译它:
clang++ -E -dM -nostdlib file.cpp
推荐阅读
- spring-boot - JBoss Fuse 与微服务
- android - Flutter:应用程序在热重载后不断回到初始路线
- scala - Scala,ZIO - 如何测试效果是否返回成功?
- pytorch - 在 pytorch 库中寻找数学运算的实现。(如torch.add、troch.mm等)
- node.js - 如何刷新 JSON 文件以重新加载我的 Discord JS 机器人的配置/其他 json 文件
- go - Go 中是否有 Python 的 `dir()` 和 `help()` 之类的实用程序?
- pandas - Pandas read_csv 将“”附加到某些列
- spring - Spring无法注册用户
- java - 匕首问题 - 生成用例有错误
- python - 在 Pandas 中从 read_excel 设置 Python 变量