[cfe-commits] r81908 - in /cfe/trunk: include/clang-c/Index.h tools/CIndex/CIndex.cpp tools/CIndex/CIndex.exports tools/c-index-test/c-index-test.c

Steve Naroff snaroff at apple.com
Tue Sep 15 13:25:34 PDT 2009


Author: snaroff
Date: Tue Sep 15 15:25:34 2009
New Revision: 81908

URL: http://llvm.org/viewvc/llvm-project?rev=81908&view=rev
Log:
- clang_getCursor(): Replace asserts with error codes (CXCursor_InvalidFile, CXCursor_NoDeclFound).
- Add predicate clang_isInvalid().
- Implement clang_getCursorFromDecl().

Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/tools/CIndex/CIndex.cpp
    cfe/trunk/tools/CIndex/CIndex.exports
    cfe/trunk/tools/c-index-test/c-index-test.c

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=81908&r1=81907&r2=81908&view=diff

==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Tue Sep 15 15:25:34 2009
@@ -37,8 +37,6 @@
 
 /* Cursors represent declarations, definitions, and references. */
 enum CXCursorKind {
- CXCursor_Invalid                       = 0,
- 
  /* Declarations */
  CXCursor_FirstDecl                     = 1,
  CXCursor_TypedefDecl                   = 2,
@@ -76,7 +74,14 @@
  CXCursor_ObjCMessageRef                = 42,
  CXCursor_ObjCSelectorRef               = 43,
  CXCursor_ObjCClassRef                  = 44,
- CXCursor_LastRef                       = 44
+ CXCursor_LastRef                       = 44,
+ 
+ /* Error conditions */
+ CXCursor_FirstInvalid                  = 70,
+ CXCursor_InvalidFile                   = 70,
+ CXCursor_NoDeclFound                   = 71,
+ CXCursor_NotImplemented                = 72,
+ CXCursor_LastInvalid                   = 72
 };
 
 /* A cursor into the CXTranslationUnit. */
@@ -172,6 +177,7 @@
 unsigned clang_isDeclaration(enum CXCursorKind);
 unsigned clang_isReference(enum CXCursorKind);
 unsigned clang_isDefinition(enum CXCursorKind);
+unsigned clang_isInvalid(enum CXCursorKind);
 
 unsigned clang_getCursorLine(CXCursor);
 unsigned clang_getCursorColumn(CXCursor);

Modified: cfe/trunk/tools/CIndex/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.cpp?rev=81908&r1=81907&r2=81908&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Tue Sep 15 15:25:34 2009
@@ -265,10 +265,6 @@
 //
 // CXDecl Operations.
 //
-CXCursor clang_getCursorFromDecl(CXDecl)
-{
-  return CXCursor();
-}
 CXEntity clang_getEntityFromDecl(CXDecl)
 {
   return 0;
@@ -347,6 +343,9 @@
    case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
    case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
    case CXCursor_ObjCClassRef: return "ObjCClassRef";
+   case CXCursor_InvalidFile: return "InvalidFile";
+   case CXCursor_NoDeclFound: return "NoDeclFound";
+   case CXCursor_NotImplemented: return "NotImplemented";
    default: return "<not implemented>";
   }
 }
@@ -370,7 +369,7 @@
     }
     default: break;
   }
-  return CXCursor_Invalid;
+  return CXCursor_NotImplemented;
 }
 //
 // CXCursor Operations.
@@ -383,21 +382,39 @@
   
   FileManager &FMgr = CXXUnit->getFileManager();
   const FileEntry *File = FMgr.getFile(source_name, 
-                                       source_name+strlen(source_name));
-  assert(File && "clang_getCursor(): FileManager returned 0");
-  
+                                       source_name+strlen(source_name));  
+  if (!File) {
+    CXCursor C = { CXCursor_InvalidFile, 0 };
+    return C;
+  }
   SourceLocation SLoc = 
     CXXUnit->getSourceManager().getLocation(File, line, column);
                                                                 
   ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc);
   
   Decl *Dcl = ALoc.getDecl();
-  assert(Dcl && "clang_getCursor(): ASTLocation has a null decl");
+  if (Dcl) {  
+    CXCursor C = { TranslateKind(Dcl), Dcl };
+    return C;
+  }
+  CXCursor C = { CXCursor_NoDeclFound, 0 };
+  return C;
+}
+
+CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
+{
+  assert(AnonDecl && "Passed null CXDecl");
+  NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
   
-  CXCursor C = { TranslateKind(Dcl), Dcl };
+  CXCursor C = { TranslateKind(ND), ND };
   return C;
 }
 
+unsigned clang_isInvalid(enum CXCursorKind K)
+{
+  return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
+}
+
 unsigned clang_isDeclaration(enum CXCursorKind K)
 {
   return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;

Modified: cfe/trunk/tools/CIndex/CIndex.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.exports?rev=81908&r1=81907&r2=81908&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.exports (original)
+++ cfe/trunk/tools/CIndex/CIndex.exports Tue Sep 15 15:25:34 2009
@@ -17,6 +17,7 @@
 _clang_isDeclaration
 _clang_isReference
 _clang_isDefinition
+_clang_isInvalid
 _clang_getCursorSpelling
 _clang_getCursorKindSpelling
 _clang_getTranslationUnitSpelling

Modified: cfe/trunk/tools/c-index-test/c-index-test.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=81908&r1=81907&r2=81908&view=diff

==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Tue Sep 15 15:25:34 2009
@@ -5,8 +5,11 @@
 #include <string.h>
 
 static void PrintCursor(CXCursor Cursor) {
-  printf("%s => %s ", clang_getCursorKindSpelling(Cursor.kind),
-                       clang_getCursorSpelling(Cursor));
+  if (clang_isInvalid(Cursor.kind))
+    printf("Invalid Cursor => %s\n", clang_getCursorKindSpelling(Cursor.kind));
+  else
+    printf("%s => %s ", clang_getCursorKindSpelling(Cursor.kind),
+                        clang_getCursorSpelling(Cursor));
 }
 
 static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter) 
@@ -47,8 +50,10 @@
     /* methodSignature - returns a cursor of type ObjCInstanceMethodDecl */
     C = clang_getCursor(TU, "/System/Library/Frameworks/Foundation.framework/Headers/NSInvocation.h", 22, 1);
     PrintCursor(C);
+    C = clang_getCursor(TU, "Large.m", 5, 18);
+    PrintCursor(C);
   } else if (argc == 3) {
-    enum CXCursorKind K = CXCursor_Invalid;
+    enum CXCursorKind K = CXCursor_NotImplemented;
     
     if (!strcmp(argv[2], "all")) {
       clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);





More information about the cfe-commits mailing list