平时不用这个,试了试。无心干活啊
#include <stdio.h>
#include <clang-c/Index.h>
// libclang 的 C 接口不支持直接访问 C++ 接口中 clang::IfStmt 的 else 分支结构,因此这里改为数子节点个数
// C++ 接口功能更齐全,不过写相关类接口更冗长
enum CXChildVisitResult visit_if(CXCursor cursor, CXCursor parent, CXClientData client_data)
{
int *p_nchildren = (int *)client_data;
++*p_nchildren;
return CXChildVisit_Continue;
}
enum CXChildVisitResult visit(CXCursor cursor, CXCursor parent, CXClientData client_data)
{
if (clang_getCursorKind(cursor) == CXCursor_IfStmt)
{
int nchildren = 0;
clang_visitChildren(cursor, visit_if, &nchildren);
CXString file_name = clang_getTranslationUnitSpelling(clang_Cursor_getTranslationUnit(cursor));
CXFile file;
unsigned start_line, start_column, start_offset;
CXSourceRange cursor_range = clang_getCursorExtent(cursor);
clang_getExpansionLocation(clang_getRangeStart(cursor_range), &file, &start_line, &start_column, &start_offset);
if (nchildren > 2)
{
printf("if-else at %s:%d:%d\n", clang_getCString(file_name), start_line, start_column);
}
else
{
printf("if at %s:%d:%d\n", clang_getCString(file_name), start_line, start_column);
}
}
return CXChildVisit_Recurse;
}
int main()
{
CXIndex index = clang_createIndex(0, 0);
CXTranslationUnit unit = clang_parseTranslationUnit(index, "iffinder.c", NULL, 0, NULL, 0, CXTranslationUnit_SingleFileParse);
if (unit == NULL)
{
printf("parse error\n");
return 0;
}
CXCursor cursor = clang_getTranslationUnitCursor(unit);
clang_visitChildren(cursor, visit, NULL);
return 0;
}
【 在 wjhtingerx 的大作中提到: 】
: 明白了,感谢大神
:
:
: ...................
--
修改:milksea FROM 114.254.9.*
FROM 114.254.9.*