llcppsigfetch:fix macro expansion underlying type #858
+268
−15
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix luoliwoshang#78
Desc
一个宏定义在另外一个头文件中,当头文件其中一个类型引用后,可以获得其Underlying类型,但是为了判断其实际的匿名性质,访问其定义节点,对其Token序列化,返回Token序列为空,导致无法通过Token序列获取其实际的匿名性质。
最小复现
llgo/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/preprocess.go
Line 45 in 0697605
fsid.h
typedef.h
对于这个类型声明节点,其range 仍然为原始宏名称的开始和结束的位置.
luoliwoshang/CTest@d564aa5
Resolve(1) Fail
https://discourse.llvm.org/t/querying-information-about-preprocessing-directives-in-libclang/19007/3
这里提到可以通过 clang_getCursorReferenced 来 将从宏实例化游标映射宏定义。
对于这个实际的
StructDecl Cursor
定义,实际上就是从宏定义来的,那么尝试去获得宏对应的StructDecl
的clang_getCursorReferenced
是否为一个宏定义,发现获取的仅仅是一个同样的StructDecl
。所以该方案失败。同样的
clang_getCursorDefinition
的并不奏效Resolve(2) (Not Perfect) **Current r
对于整个AST树节点遍历发现,在定义前,会存在CXCursor_MacroExpansion 宏展开节点,对于一个宏展开节点,他的源文件范围与Typedef中引用的宏对应的结构体声明的范围一致。
luoliwoshang/CTest@5dd9fb9
获取宏展开的节点信息并进行缓存,对于序列化为空的一个类型引用,则可以认为是一个宏展开,那么就可以通过比对当前Token为空的声明节点的偏移量和宏展开节点的偏移量,如果一致,那么就可以认为是同一个节点,则可以通过宏的序列获得实际的声明Token序列。
即流程为收集宏展开节点与宏定义映射->Token序列为空的类型声明->检查是否为一个宏展开->通过映射关系找到实际的宏->获得其Token序列继续检查其实际的匿名性质
当前这个方案能解决目前这个情况
typedef.h
但是如果存在一个用例,这个宏再嵌套一层,就也不能正确识别了,这块可能会再结合clang的预处理来完善这个修复。