Hi, I am using libclang to parse C source code into AST nodes. When I try get the string of the nodes, it doesn’t work for many types of nodes, like statements, expressions, it only works for function declarations and variable declarations.
Below are my sample code:
enum CXCursorKind kind = clang_getCursorKind(cursor);
switch (kind) {
case CXCursor_FunctionDecl:
CXString funcDeclStr = clang_getCursorSpelling(cursor);
const char* funcStr = clang_getCString(funcDeclStr);
printf(“%s\n”, funcStr);
clang_disposeString(funcDeclStr);
break;
case CXCursor_WhileStmt:
CXString whileStmtStr = clang_getCursorSpelling(cursor);
const char* whileStr = clang_getCString(whileStmtStr);
printf(“%s\n”, whileStr);
clang_disposeString(whileStmtStr);
break;
When it comes to function declaration, the string of the function name can be successfully printed. But for while statements, it couldn’t print out anything. I wonder if I used libclang incorrectly or if libclang doesn’t support getting the string of statements or expressions.