我想在源文件中获取主要功能的AST(假设有一个),以从中构建控制流程图。我在这里找到了生成并遍历AST的代码:https://shaharmike.com/cpp/libclang/。 但是问题是它进入了所有包含的文件中。我也找到了这个主题:Clang AST visitor,avoid traversing include files。但是似乎在clang10中做了一些更改,建议的解决方案现在不起作用。也许还有其他方法可以获取AST的构建控制流程图?唯一的要求-它必须可以使用C ++源代码。
longxin0118 回答:如何使用clang仅获取主要功能的AST
阅读the documentation告诉我,clang_visitChildren
只会“返回”指向返回CXChildVisit_Recurse
所指向的内容。因此,您的访问函数应检查游标的种类并返回CXChildVisit_Continue
,直到到达名称等于main
的函数定义为止。 main
的作用由您决定,但我建议返回CXChildVisit_Break
。
因此,感谢Botje和本文(https://www.phototalks.idv.tw/academic/?p=1932),我找到了解决方案。无论如何,这都不是最好的代码,但是它可以工作。希望对其他人有帮助。
#include <iostream>
#include <clang-c/Index.h>
#include <string.h>
using namespace std;
ostream& operator<<(ostream& stream,const CXString& str)
{
stream << clang_getCString(str);
clang_disposeString(str);
return stream;
}
std::string getCursorKindName( CXCursorKind cursorKind )
{
CXString kindName = clang_getCursorKindSpelling( cursorKind );
std::string result = clang_getCString( kindName );
clang_disposeString( kindName );
return result;
}
std::string getCursorSpelling( CXCursor cursor )
{
CXString cursorSpelling = clang_getCursorSpelling( cursor );
std::string result = clang_getCString( cursorSpelling );
clang_disposeString( cursorSpelling );
return result;
}
CXChildVisitResult visitor1( CXCursor cursor,CXCursor /* parent */,CXClientData clientData )
{
CXSourceLocation location = clang_getCursorLocation( cursor );
unsigned int locationstring =0;
clang_getSpellingLocation ( location,NULL,&locationstring,NULL);
if( clang_Location_isFromMainFile( location ) == 0 )
return CXChildVisit_Continue;
CXCursorKind kind = clang_getCursorKind(cursor);
std::string str2 ("main");
CXCursorKind cursorKind = clang_getCursorKind( cursor );
unsigned int curLevel = *( reinterpret_cast<unsigned int*>( clientData ) );
unsigned int nextLevel = curLevel + 1;
std::cout << std::string( curLevel,'-' ) << " " << getCursorKindName(
cursorKind ) << " (" << getCursorSpelling( cursor ) << ") ";
std::cout << locationstring ;
std::cout << endl;
clang_visitChildren( cursor,visitor1,&nextLevel );
return CXChildVisit_Continue;
}
CXChildVisitResult visitor( CXCursor cursor,CXClientData clientData )
{
CXSourceLocation location = clang_getCursorLocation( cursor );
if( clang_Location_isFromMainFile( location ) == 0 )
return CXChildVisit_Continue;
CXCursorKind kind = clang_getCursorKind(cursor);
std::string str2 ("main");
CXCursorKind cursorKind = clang_getCursorKind( cursor );
unsigned int curLevel = *( reinterpret_cast<unsigned int*>( clientData ) );
unsigned int nextLevel = curLevel + 1;
if (!((str2.compare(getCursorSpelling(cursor)))))
{
std::cout << std::string( curLevel,'-' ) << " " << getCursorKindName(
cursorKind ) << " (" << getCursorSpelling( cursor ) << ")\n";
clang_visitChildren( cursor,&nextLevel );
return CXChildVisit_Continue;
}
else
{
return CXChildVisit_Continue;
}
}
int main()
{
CXIndex index = clang_createIndex(0,0);
CXTranslationUnit unit = clang_parseTranslationUnit(
index,<source_file_name>,nullptr,CXTranslationUnit_None);
CXCursor cursor = clang_getTranslationUnitCursor(unit);
unsigned int treeLevel = 0;
clang_visitChildren( cursor,visitor,&treeLevel );
clang_disposeTranslationUnit(unit);
clang_disposeIndex(index);
}