libclang ,clang_Cursor_isAnonymous 什么是匿名声明?
libclang ,clang_Cursor_isAnonymous what is an anonymous declaration?
我很难理解什么是匿名结构声明(或联合)。
来自这个link:
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
这意味着我所有的声明 return 相同的值形式 clang_Cursor_isAnonymous
这意味着我真的不知道这是来自 headers 的评论:
/**
* \brief Determine whether the given cursor represents an anonymous record
* declaration.
*/
解决方案非常简单,匿名结构或联合是 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;
}
我很难理解什么是匿名结构声明(或联合)。
来自这个link: 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
这意味着我所有的声明 return 相同的值形式 clang_Cursor_isAnonymous
这意味着我真的不知道这是来自 headers 的评论:
/**
* \brief Determine whether the given cursor represents an anonymous record
* declaration.
*/
解决方案非常简单,匿名结构或联合是 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;
}