[cfe-commits] r98981 - in /cfe/trunk: include/clang-c/Index.h lib/AST/DeclObjC.cpp tools/CIndex/CIndex.cpp tools/CIndex/CIndexUSRs.cpp tools/CIndex/CXCursor.cpp

Ted Kremenek kremenek at apple.com
Fri Mar 19 13:39:03 PDT 2010


Author: kremenek
Date: Fri Mar 19 15:39:03 2010
New Revision: 98981

URL: http://llvm.org/viewvc/llvm-project?rev=98981&view=rev
Log:
Make the CIndex API more resilient to being used on invalid code.

Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/lib/AST/DeclObjC.cpp
    cfe/trunk/tools/CIndex/CIndex.cpp
    cfe/trunk/tools/CIndex/CIndexUSRs.cpp
    cfe/trunk/tools/CIndex/CXCursor.cpp

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=98981&r1=98980&r2=98981&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Fri Mar 19 15:39:03 2010
@@ -728,7 +728,8 @@
   CXCursor_InvalidFile                   = 70,
   CXCursor_NoDeclFound                   = 71,
   CXCursor_NotImplemented                = 72,
-  CXCursor_LastInvalid                   = 72,
+  CXCursor_InvalidCode                   = 73,
+  CXCursor_LastInvalid                   = CXCursor_InvalidCode,
 
   /* Expressions */
   CXCursor_FirstExpr                     = 100,

Modified: cfe/trunk/lib/AST/DeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclObjC.cpp?rev=98981&r1=98980&r2=98981&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclObjC.cpp (original)
+++ cfe/trunk/lib/AST/DeclObjC.cpp Fri Mar 19 15:39:03 2010
@@ -743,7 +743,10 @@
 }
 
 ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
-  return getClassInterface()->FindCategoryDeclaration(getIdentifier());
+  // The class interface might be NULL if we are working with invalid code.
+  if (const ObjCInterfaceDecl *ID = getClassInterface())
+    return ID->FindCategoryDeclaration(getIdentifier());
+  return 0;
 }
 
 

Modified: cfe/trunk/tools/CIndex/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.cpp?rev=98981&r1=98980&r2=98981&view=diff
==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Fri Mar 19 15:39:03 2010
@@ -621,9 +621,10 @@
 }
 
 bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
-  if (Visit(MakeCursorObjCClassRef(D->getCategoryDecl()->getClassInterface(),
-                                   D->getLocation(), TU)))
-    return true;
+  // 'ID' could be null when dealing with invalid code.
+  if (ObjCInterfaceDecl *ID = D->getClassInterface())
+    if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU)))
+      return true;
 
   return VisitObjCImplDecl(D);
 }

Modified: cfe/trunk/tools/CIndex/CIndexUSRs.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndexUSRs.cpp?rev=98981&r1=98980&r2=98981&view=diff
==============================================================================
--- cfe/trunk/tools/CIndex/CIndexUSRs.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndexUSRs.cpp Fri Mar 19 15:39:03 2010
@@ -155,14 +155,30 @@
       break;
     case Decl::ObjCCategory: {
       ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
-      GenObjCCategory(CD->getClassInterface()->getName(),
-                      CD->getName());
+      ObjCInterfaceDecl *ID = CD->getClassInterface();
+      if (!ID) {
+        // Handle invalid code where the @interface might not
+        // have been specified.
+        // FIXME: We should be able to generate this USR even if the
+        // @interface isn't available.
+        IgnoreResults = true;
+        return;
+      }
+      GenObjCCategory(ID->getName(), CD->getName());
       break;
     }
     case Decl::ObjCCategoryImpl: {
       ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
-      GenObjCCategory(CD->getClassInterface()->getName(),
-                      CD->getName());
+      ObjCInterfaceDecl *ID = CD->getClassInterface();
+      if (!ID) {
+        // Handle invalid code where the @interface might not
+        // have been specified.
+        // FIXME: We should be able to generate this USR even if the
+        // @interface isn't available.
+        IgnoreResults = true;
+        return;
+      }
+      GenObjCCategory(ID->getName(), CD->getName());
       break;
     }
     case Decl::ObjCProtocol:
@@ -251,7 +267,7 @@
   SUG->Visit(static_cast<Decl*>(D));
 
   if (SUG->ignoreResults() || SUG.str().empty())
-    return createCXString(NULL);
+    return createCXString("");
 
   // Return a copy of the string that must be disposed by the caller.
   return createCXString(SUG.str(), true);

Modified: cfe/trunk/tools/CIndex/CXCursor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CXCursor.cpp?rev=98981&r1=98980&r2=98981&view=diff
==============================================================================
--- cfe/trunk/tools/CIndex/CXCursor.cpp (original)
+++ cfe/trunk/tools/CIndex/CXCursor.cpp Fri Mar 19 15:39:03 2010
@@ -266,7 +266,10 @@
 CXCursor cxcursor::MakeCursorObjCClassRef(ObjCInterfaceDecl *Class, 
                                           SourceLocation Loc, 
                                           ASTUnit *TU) {
-  assert(Class && TU && "Invalid arguments!");
+  // 'Class' can be null for invalid code.
+  if (!Class)
+    return MakeCXCursorInvalid(CXCursor_InvalidCode);
+  assert(TU && "Invalid arguments!");
   void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
   CXCursor C = { CXCursor_ObjCClassRef, { Class, RawLoc, TU } };
   return C;    





More information about the cfe-commits mailing list