我用llvm libtooling编程,代码里解析一个cpp文件,遍历var,打印ast节点信息,如下第二段代码,string类型ast是invalid,int类型ast节点是正确的为什么呢。被解析的cpp code包含string头文件了,第三段代码
bool Visitor::VisitVarDecl(VarDecl *VD)
{
SourceLocation srcLoc = VD->getLocation();
if (srcMgr.isInSystemHeader(srcMgr.getExpansionLoc(VD->getBeginLoc()))) {
return true;
}
if (srcMgr.getFileCharacteristic(srcLoc) == SrcMgr::C_System ||
srcMgr.getFileCharacteristic(srcLoc) == SrcMgr::C_ExternCSystem) {
return true;
}
VD->dump();
return true;
}
'''
'''
VarDecl 0x55d65df37a68 </root/h30038243/k30056123/test/tmp_case5/e.cpp:16:2, col:14> col:14 invalid s2 'std::string':'std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >'
VarDecl 0x55d65df37b18 </root/h30038243/k30056123/test/tmp_case5/e.cpp:17:2, col:9> col:9 invalid s1 'std::string':'std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >'
VarDecl 0x55d65df37ba8 </root/h30038243/k30056123/test/tmp_case5/e.cpp:18:2, col:9> col:9 invalid s3 'std::string':'std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >'
VarDecl 0x55bda4199000 </home/cpp_parser/llvm-project-llvmorg-15.0.4/clang/tools/syn-analyze/test/tmp_case5/e.cpp:20:2, col:10> col:6 a 'int' cinit
`-IntegerLiteral 0x55bda4199068 <col:10> 'int' 1
'''
'''
#include "e.h"
#include <memory>
#include <string>
using namespace std;
int main(int argc, const char *argv[]){
std::string s2 = "abc";
string s1 = func();
string s3 = "a";
int a = 1;
int b = func2();
int64_t c = 2;
return 0;
}
why VD->dump() show string invalid vartype? but int type show no invalid
没尝试,无解决思路
添加环境变量就可以了