From 1416e17c89da90d52ac5c9d86b9006a482d377dd Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Fri, 8 Jun 2012 05:48:06 +0000 Subject: [PATCH] [libclang] Don't crash when saving a PCH from a prefix header that does not exist. rdar://11607033 llvm-svn: 158193 --- clang/include/clang/Frontend/FrontendAction.h | 2 +- clang/lib/Frontend/ASTUnit.cpp | 11 +++++++++-- clang/lib/Frontend/FrontendAction.cpp | 6 ++++-- clang/test/Index/pch-with-errors.c | 4 +++- clang/tools/libclang/CIndex.cpp | 2 ++ 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/clang/include/clang/Frontend/FrontendAction.h b/clang/include/clang/Frontend/FrontendAction.h index 6839028f9784..c0056de5cae3 100644 --- a/clang/include/clang/Frontend/FrontendAction.h +++ b/clang/include/clang/Frontend/FrontendAction.h @@ -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. diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp index d6bdae4aafe1..1ef5ba864eb9 100644 --- a/clang/lib/Frontend/ASTUnit.cpp +++ b/clang/lib/Frontend/ASTUnit.cpp @@ -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); diff --git a/clang/lib/Frontend/FrontendAction.cpp b/clang/lib/Frontend/FrontendAction.cpp index 1ff32921d9d7..fb53c71fa9b9 100644 --- a/clang/lib/Frontend/FrontendAction.cpp +++ b/clang/lib/Frontend/FrontendAction.cpp @@ -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() { diff --git a/clang/test/Index/pch-with-errors.c b/clang/test/Index/pch-with-errors.c index be8728eb723b..2d396134e5f2 100644 --- a/clang/test/Index/pch-with-errors.c +++ b/clang/test/Index/pch-with-errors.c @@ -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 diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp index a508b772e531..8eaf4ecf0c90 100644 --- a/clang/tools/libclang/CIndex.cpp +++ b/clang/tools/libclang/CIndex.cpp @@ -2692,6 +2692,8 @@ int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName, ASTUnit *CXXUnit = static_cast(TU->TUData); ASTUnit::ConcurrencyCheck Check(*CXXUnit); + if (!CXXUnit->hasSema()) + return CXSaveError_InvalidTU; SaveTranslationUnitInfo STUI = { TU, FileName, options, CXSaveError_None };