【问题标题】:libclang ,clang_Cursor_isAnonymous what is an anonymous declaration?libclang ,clang_Cursor_isAnonymous 什么是匿名声明?
【发布时间】:2015-11-07 11:40:04
【问题描述】:

我很难理解什么是匿名结构声明(或联合)。

从此链接: 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

这意味着我真的不知道这是标题中的 cmets:

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

【问题讨论】:

    标签: c llvm-clang libclang


    【解决方案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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 2016-05-08
      • 2019-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多