我很难理解什么是匿名结构声明(或联合)。
从这个链接: http://clang.llvm.org/doxygen/classclang_1_1RecordDecl.html#ad2dd151523eecb8d15149cc0937c3dff
这是一个匿名声明
union { int i; float f; };
而不是这个
union X { int i; float f; };
union { int i; float f; } obj;
但如果我做一些测试:
#include <stdio.h>
#include "clang-c/Index.h"
/*
compile with:
clang -lclang -o anonymous_record_decl_check anonymous_record_decl_check.c
*/
static enum CXChildVisitResult
visitor(CXCursor cursor, CXCursor parent, CXClientData data)
{
CXSourceLocation loc;
CXFile file;
unsigned line;
unsigned column;
unsigned offset;
if (clang_getCursorKind(cursor) == CXCursor_StructDecl)
{
printf("sentinel %d\n", __LINE__);
loc = clang_getCursorLocation(cursor);
clang_getSpellingLocation(loc,
&file,
&line,
&column,
&offset);
printf("sentinel %d\n", __LINE__);
printf("line: %d anon ? %d\n", line, clang_Cursor_isAnonymous(cursor));
}
if (clang_getCursorKind(cursor) == CXCursor_UnionDecl)
{
printf("sentinel %d\n", __LINE__);
loc = clang_getCursorLocation(cursor);
printf("sentinel %d\n", __LINE__);
clang_getSpellingLocation(loc,
&file,
&line,
&column,
&offset);
printf("line: %d anon ? %d\n", line, clang_Cursor_isAnonymous(cursor));
}
return CXChildVisit_Recurse; // visit complete AST recursivly
}
int main(int argc, char *argv[]) {
CXIndex Index = clang_createIndex(0, 1);
CXTranslationUnit TU = clang_createTranslationUnitFromSourceFile(Index,
"record_decls.c",
0,
0,
0,
0);
clang_visitChildren(clang_getTranslationUnitCursor(TU), visitor, 0);
clang_disposeTranslationUnit(TU);
clang_disposeIndex(Index);
return 0;
}
使用已解析的文件(record_decls.c):
struct { int a; char b; };
struct tata { int c; double e;};
union { int i; float f; };
union X { int i; float f; };
我有这个输出:
sentinel 17
sentinel 25
line: 1 anon ? 0
sentinel 17
sentinel 25
line: 2 anon ? 0
sentinel 31
sentinel 33
line: 3 anon ? 0
sentinel 31
sentinel 33
line: 4 anon ? 0
这意味着我的所有声明都返回clang_Cursor_isAnonymous
这意味着我真的不知道这里是标题的评论:
/**
* \brief Determine whether the given cursor represents an anonymous record
* declaration.
*/
答案 0 :(得分:1)
解决方案非常简单,匿名结构或联合是c ++功能所以:
#include <stdio.h>
#include "clang-c/Index.h"
/*
compile with:
clang -lclang -o anonymous_record_decl_check anonymous_record_decl_check.c
*/
static enum CXChildVisitResult
visitor(CXCursor cursor, CXCursor parent, CXClientData data)
{
CXSourceLocation loc;
CXFile file;
unsigned line;
unsigned column;
unsigned offset;
if (clang_getCursorKind(cursor) == CXCursor_StructDecl)
{
printf("sentinel %d\n", __LINE__);
loc = clang_getCursorLocation(cursor);
clang_getSpellingLocation(loc,
&file,
&line,
&column,
&offset);
printf("sentinel %d\n", __LINE__);
printf("line: %d anon ? %d\n", line, clang_Cursor_isAnonymous(cursor));
}
if (clang_getCursorKind(cursor) == CXCursor_UnionDecl)
{
printf("sentinel %d\n", __LINE__);
loc = clang_getCursorLocation(cursor);
printf("sentinel %d\n", __LINE__);
clang_getSpellingLocation(loc,
&file,
&line,
&column,
&offset);
printf("line: %d anon ? %d\n", line, clang_Cursor_isAnonymous(cursor));
}
return CXChildVisit_Recurse; // visit complete AST recursivly
}
int main(int argc, char *argv[]) {
CXIndex Index = clang_createIndex(0, 1);
const char *args[] = { "-x", "c++" };
CXTranslationUnit TU = clang_createTranslationUnitFromSourceFile(Index,
"record_decls.c",
2,
args,
0,
0);
clang_visitChildren(clang_getTranslationUnitCursor(TU), visitor, 0);
clang_disposeTranslationUnit(TU);
clang_disposeIndex(Index);
return 0;
}