[cfe-dev] libclang , clang_Cursor_isAnonymous what is an anonymous declaration

cedlemo via cfe-dev cfe-dev at lists.llvm.org
Sat Nov 7 05:18:32 PST 2015


I have some difficulties to understand what are anonymous structures 
declaration (or union) .

 From this link: 
http://clang.llvm.org/doxygen/classclang_1_1RecordDecl.html#ad2dd151523eecb8d15149cc0937c3dff

this is an anonymous declaration

union { int i; float f; };

and not this

union X { int i; float f; };
union { int i; float f; } obj;


But if I make some tests:



#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;
}


with the file that is parsed ( 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; };



I have this output:

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


Which means that all my declarations return the same value form 
clang_Cursor_isAnonymous

Which means, well I don't really know, here is the comments from the 
headers:

/**
  * \brief Determine whether the given cursor represents an anonymous record
  * declaration.
  */

Any ideas?


cedlemo



More information about the cfe-dev mailing list