[cfe-commits] r94769 - /cfe/trunk/tools/CIndex/CIndex.cpp

Douglas Gregor dgregor at apple.com
Thu Jan 28 16:47:48 PST 2010


Author: dgregor
Date: Thu Jan 28 18:47:48 2010
New Revision: 94769

URL: http://llvm.org/viewvc/llvm-project?rev=94769&view=rev
Log:
Harden the CIndex implementation a bit, so that it does not assert
when given bad inputs.

Modified:
    cfe/trunk/tools/CIndex/CIndex.cpp

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

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Thu Jan 28 18:47:48 2010
@@ -286,7 +286,10 @@
 
   // Move the end of the input range to the end of the last token in that
   // range.
-  R.setEnd(TU->getPreprocessor().getLocForEndOfToken(R.getEnd(), 1));
+  SourceLocation NewEnd
+    = TU->getPreprocessor().getLocForEndOfToken(R.getEnd(), 1);
+  if (NewEnd.isValid())
+    R.setEnd(NewEnd);
   return RangeCompare(TU->getSourceManager(), R, RegionOfInterest);
 }
 
@@ -903,21 +906,24 @@
 }
 
 void clang_disposeIndex(CXIndex CIdx) {
-  assert(CIdx && "Passed null CXIndex");
-  delete static_cast<CIndexer *>(CIdx);
+  if (CIdx)
+    delete static_cast<CIndexer *>(CIdx);
 }
 
 void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
-  assert(CIdx && "Passed null CXIndex");
-  CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
-  CXXIdx->setUseExternalASTGeneration(value);
+  if (CIdx) {
+    CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
+    CXXIdx->setUseExternalASTGeneration(value);
+  }
 }
 
 CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
                                               const char *ast_filename,
                                              CXDiagnosticCallback diag_callback,
                                               CXClientData diag_client_data) {
-  assert(CIdx && "Passed null CXIndex");
+  if (!CIdx)
+    return 0;
+  
   CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
 
   // Configure the diagnostics.
@@ -941,7 +947,9 @@
                                           struct CXUnsavedFile *unsaved_files,
                                           CXDiagnosticCallback diag_callback,
                                           CXClientData diag_client_data) {
-  assert(CIdx && "Passed null CXIndex");
+  if (!CIdx)
+    return 0;
+  
   CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
 
   // Configure the diagnostics.
@@ -1096,12 +1104,14 @@
 }
 
 void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
-  assert(CTUnit && "Passed null CXTranslationUnit");
-  delete static_cast<ASTUnit *>(CTUnit);
+  if (CTUnit)
+    delete static_cast<ASTUnit *>(CTUnit);
 }
 
 CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
-  assert(CTUnit && "Passed null CXTranslationUnit");
+  if (!CTUnit)
+    return CIndexer::createCXString("");
+  
   ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
   return CIndexer::createCXString(CXXUnit->getOriginalSourceFileName().c_str(),
                                   true);
@@ -1246,7 +1256,6 @@
   if (!SFile)
     return 0;
   
-  assert(SFile && "Passed null CXFile");
   FileEntry *FEnt = static_cast<FileEntry *>(SFile);
   return FEnt->getName();
 }
@@ -1255,7 +1264,6 @@
   if (!SFile)
     return 0;
   
-  assert(SFile && "Passed null CXFile");
   FileEntry *FEnt = static_cast<FileEntry *>(SFile);
   return FEnt->getModificationTime();
 }
@@ -1339,7 +1347,6 @@
 }
     
 CXString clang_getCursorSpelling(CXCursor C) {
-  assert(getCursorDecl(C) && "CXCursor has null decl");
   if (clang_isTranslationUnit(C.kind))
     return clang_getTranslationUnitSpelling(C.data[2]);
 





More information about the cfe-commits mailing list