]> granicus.if.org Git - clang/commitdiff
[libclang] Don't crash when saving a PCH from a prefix header
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Fri, 8 Jun 2012 05:48:06 +0000 (05:48 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Fri, 8 Jun 2012 05:48:06 +0000 (05:48 +0000)
that does not exist.

rdar://11607033

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@158193 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Frontend/FrontendAction.h
lib/Frontend/ASTUnit.cpp
lib/Frontend/FrontendAction.cpp
test/Index/pch-with-errors.c
tools/libclang/CIndex.cpp

index 6839028f97841b027eda320fc4776606f15040cf..c0056de5cae34f928de53dab303613608e9aebc5 100644 (file)
@@ -188,7 +188,7 @@ public:
   bool BeginSourceFile(CompilerInstance &CI, const FrontendInputFile &Input);
 
   /// Execute - Set the source managers main input file, and run the action.
-  void Execute();
+  bool Execute();
 
   /// EndSourceFile - Perform any per-file post processing, deallocate per-file
   /// objects, and run statistics and output file cleanup code.
index d6bdae4aafe154bca9ca4510248cc55577707838..1ef5ba864eb9d7f2750413fa7711bde0cf083a08 100644 (file)
@@ -1133,7 +1133,8 @@ bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) {
                                StoredDiagnostics);
   }
 
-  Act->Execute();
+  if (!Act->Execute())
+    goto error;
 
   transferASTDataFromCompilerInstance(*Clang);
   
@@ -1795,7 +1796,13 @@ ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(CompilerInvocation *CI,
                                            AST->getCurrentTopLevelHashValue()));
     Clang->setASTConsumer(new MultiplexConsumer(Consumers));
   }
-  Act->Execute();
+  if (!Act->Execute()) {
+    AST->transferASTDataFromCompilerInstance(*Clang);
+    if (OwnAST && ErrAST)
+      ErrAST->swap(OwnAST);
+
+    return 0;
+  }
 
   // Steal the created target, context, and preprocessor.
   AST->transferASTDataFromCompilerInstance(*Clang);
index 1ff32921d9d77d88474dbfb56dc723b31545e755..fb53c71fa9b9226ddb6fbdf663d97aa873956b48 100644 (file)
@@ -315,7 +315,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
   return false;
 }
 
-void FrontendAction::Execute() {
+bool FrontendAction::Execute() {
   CompilerInstance &CI = getCompilerInstance();
 
   // Initialize the main file entry. This needs to be delayed until after PCH
@@ -325,7 +325,7 @@ void FrontendAction::Execute() {
                                     getCurrentInput().IsSystem
                                       ? SrcMgr::C_System
                                       : SrcMgr::C_User))
-      return;
+      return false;
   }
 
   if (CI.hasFrontendTimer()) {
@@ -333,6 +333,8 @@ void FrontendAction::Execute() {
     ExecuteAction();
   }
   else ExecuteAction();
+
+  return true;
 }
 
 void FrontendAction::EndSourceFile() {
index be8728eb723b6eb95ef4401119c13900ae6ff723..2d396134e5f2a1cde74baea6cd7b22528f2b5ed2 100644 (file)
@@ -38,5 +38,7 @@ void foo(void) {
 // CHECK-INDEX: [indexEntityReference]: kind: function | name: erroneous
 
 // RUN: %clang -fsyntax-only %s -include %t.h 2>&1 | FileCheck -check-prefix=PCH-ERR %s
-
 // PCH-ERR: error: PCH file contains compiler errors
+
+// RUN: c-index-test -write-pch %t.pch foobar.c 2>&1 | FileCheck -check-prefix=NONEXISTENT %s
+// NONEXISTENT: Unable to load translation unit
index a508b772e531a2a5f13dbc01952dbf4bb6ecbcea..8eaf4ecf0c90b131c2fc9845eaa9866290dbae91 100644 (file)
@@ -2692,6 +2692,8 @@ int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName,
 
   ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
   ASTUnit::ConcurrencyCheck Check(*CXXUnit);
+  if (!CXXUnit->hasSema())
+    return CXSaveError_InvalidTU;
 
   SaveTranslationUnitInfo STUI = { TU, FileName, options, CXSaveError_None };