<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">When I use clang in order to parse the
      same file, I 'm not able to find anonymous struct or union decl:<br>
      even if I use the anonymous examples of the documentation.<br>
      <br>
      <br>
      class MyASTConsumer : public clang::ASTConsumer
      <br>
      {<br>
       public:<br>
       MyASTConsumer() : clang::ASTConsumer() { }<br>
       virtual ~MyASTConsumer() { }<br>
       virtual void HandleTagDeclDefinition( clang::TagDecl * d) <br>
       {<br>
         if (d->isStruct())<br>
           std::cout << "struct" << std::endl;<br>
         if (d->isUnion())<br>
           std::cout << "union" << std::endl;<br>
         clang::RecordDecl *r; r = llvm::cast<clang::recorddecl>(d);<br>
           if( r->isAnonymousStructOrUnion() )<br>
           {<br>
             std::cout << "RecordDecl Anonymous : " <<
        std::endl; <br>
           }<br>
           }<br>
         };<br>
        <br>
        int main()
        <br>
        {<br>
          using clang::CompilerInstance;<br>
          using clang::TargetOptions;<br>
          using clang::TargetInfo;<br>
          using clang::FileEntry;<br>
          using clang::Token;<br>
          using clang::ASTContext;<br>
          using clang::ASTConsumer;<br>
          using clang::Parser;<br>
          using clang::DiagnosticOptions;<br>
          using clang::TextDiagnosticPrinter;<br>
          CompilerInstance ci;<br>
          DiagnosticOptions diagnosticOptions;<br>
          ci.createDiagnostics();<br>
          std::shared_ptr<clang::targetoptions> pto = std::make_shared<clang::targetoptions>();<br>
              pto->Triple = llvm::sys::getDefaultTargetTriple();<br>
              TargetInfo *pti =
            TargetInfo::CreateTargetInfo(ci.getDiagnostics(), pto);<br>
              ci.setTarget(pti);<br>
              ci.createFileManager();<br>
              ci.createSourceManager(ci.getFileManager());<br>
              ci.createPreprocessor(clang::TU_Complete);<br>
              ci.getPreprocessorOpts().UsePredefines = false;<br>
              MyASTConsumer *astConsumer = new MyASTConsumer();<br>
              ci.setASTConsumer(llvm::make_unique<clang::astconsumer>(*astConsumer));<br>
                ci.createASTContext();<br>
                const FileEntry *pFile =
              ci.getFileManager().getFile("record_decls.c");<br>
                clang::FileID id =
              ci.getSourceManager().createFileID(pFile,
              clang::SourceLocation(), clang::SrcMgr::C_User);<br>
                ci.getSourceManager().setMainFileID(id);<br>
               
              ci.getDiagnosticClient().BeginSourceFile(ci.getLangOpts(),
              &ci.getPreprocessor());<br>
                clang::ParseAST(ci.getPreprocessor(), astConsumer,
              ci.getASTContext());<br>
                ci.getDiagnosticClient().EndSourceFile();<br>
                return 0;<br>
               }</clang::astconsumer></clang::targetoptions></clang::targetoptions></clang::recorddecl><br>
      <br>
      <br>
      On 07/11/2015 14:18, cedlemo via cfe-dev wrote:<br>
    </div>
    <blockquote cite="mid:563DFA28.2090106@gmx.com" type="cite">I have
      some difficulties to understand what are anonymous structures
      declaration (or union) .
      <br>
      <br>
      From this link:
<a class="moz-txt-link-freetext" href="http://clang.llvm.org/doxygen/classclang_1_1RecordDecl.html#ad2dd151523eecb8d15149cc0937c3dff">http://clang.llvm.org/doxygen/classclang_1_1RecordDecl.html#ad2dd151523eecb8d15149cc0937c3dff</a><br>
      <br>
      this is an anonymous declaration
      <br>
      <br>
      union { int i; float f; };
      <br>
      <br>
      and not this
      <br>
      <br>
      union X { int i; float f; };
      <br>
      union { int i; float f; } obj;
      <br>
      <br>
      <br>
      But if I make some tests:
      <br>
      <br>
      <br>
      <br>
      #include <stdio.h>
      <br>
      #include "clang-c/Index.h"
      <br>
      /*
      <br>
      compile with:
      <br>
      clang -lclang -o anonymous_record_decl_check
      anonymous_record_decl_check.c
      <br>
      */
      <br>
      static enum CXChildVisitResult
      <br>
      visitor(CXCursor cursor, CXCursor parent, CXClientData data)
      <br>
      {
      <br>
        CXSourceLocation loc;
      <br>
        CXFile file;
      <br>
        unsigned line;
      <br>
        unsigned column;
      <br>
        unsigned offset;
      <br>
        if (clang_getCursorKind(cursor) == CXCursor_StructDecl)
      <br>
        {
      <br>
          printf("sentinel %d\n", __LINE__);
      <br>
          loc = clang_getCursorLocation(cursor);
      <br>
          clang_getSpellingLocation(loc,
      <br>
                                    &file,
      <br>
                                    &line,
      <br>
                                    &column,
      <br>
                                    &offset);
      <br>
      <br>
          printf("sentinel %d\n", __LINE__);
      <br>
          printf("line: %d anon ? %d\n", line,
      clang_Cursor_isAnonymous(cursor));
      <br>
        }
      <br>
      <br>
        if (clang_getCursorKind(cursor) == CXCursor_UnionDecl)
      <br>
        {
      <br>
          printf("sentinel %d\n", __LINE__);
      <br>
          loc = clang_getCursorLocation(cursor);
      <br>
          printf("sentinel %d\n", __LINE__);
      <br>
          clang_getSpellingLocation(loc,
      <br>
                                    &file,
      <br>
                                    &line,
      <br>
                                    &column,
      <br>
                                    &offset);
      <br>
          printf("line: %d anon ? %d\n", line,
      clang_Cursor_isAnonymous(cursor));
      <br>
        }
      <br>
        return CXChildVisit_Recurse; // visit complete AST recursivly
      <br>
      }
      <br>
      <br>
      int main(int argc, char *argv[]) {
      <br>
        CXIndex Index = clang_createIndex(0, 1);
      <br>
        CXTranslationUnit TU =
      clang_createTranslationUnitFromSourceFile(Index,
      <br>
      "record_decls.c",
      <br>
                                                            0,
      <br>
                                                            0,
      <br>
                                                            0,
      <br>
                                                            0);
      <br>
      <br>
        clang_visitChildren(clang_getTranslationUnitCursor(TU), visitor,
      0);
      <br>
      <br>
        clang_disposeTranslationUnit(TU);
      <br>
        clang_disposeIndex(Index);
      <br>
        return 0;
      <br>
      }
      <br>
      <br>
      <br>
      with the file that is parsed ( record_decls.c ):
      <br>
      <br>
      struct { int a; char b; };
      <br>
      struct tata { int c; double e;};
      <br>
      union { int i; float f; };
      <br>
      union X { int i; float f; };
      <br>
      <br>
      <br>
      <br>
      I have this output:
      <br>
      <br>
      sentinel 17
      <br>
      sentinel 25
      <br>
      line: 1 anon ? 0
      <br>
      sentinel 17
      <br>
      sentinel 25
      <br>
      line: 2 anon ? 0
      <br>
      sentinel 31
      <br>
      sentinel 33
      <br>
      line: 3 anon ? 0
      <br>
      sentinel 31
      <br>
      sentinel 33
      <br>
      line: 4 anon ? 0
      <br>
      <br>
      <br>
      Which means that all my declarations return the same value form
      clang_Cursor_isAnonymous
      <br>
      <br>
      Which means, well I don't really know, here is the comments from
      the headers:
      <br>
      <br>
      /**
      <br>
       * \brief Determine whether the given cursor represents an
      anonymous record
      <br>
       * declaration.
      <br>
       */
      <br>
      <br>
      Any ideas?
      <br>
      <br>
      <br>
      cedlemo
      <br>
      _______________________________________________
      <br>
      cfe-dev mailing list
      <br>
      <a class="moz-txt-link-abbreviated" href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>
      <br>
      <a class="moz-txt-link-freetext" href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a>
      <br>
    </blockquote>
    <br>
  </body>
</html>