/// BeginSourceFileAction (and BeginSourceFile).
virtual void EndSourceFileAction() {}
+ /// \brief Callback at the end of processing a single input, to determine
+ /// if the output files should be erased or not.
+ ///
+ /// By default it returns true if a compiler error occurred.
+ /// This is guaranteed to only be called following a successful call to
+ /// BeginSourceFileAction (and BeginSourceFile).
+ virtual bool shouldEraseOutputFiles();
+
/// @}
public:
SmallVector<char, 128> Buffer;
llvm::BitstreamWriter Stream;
ASTWriter Writer;
+ bool AllowASTWithErrors;
+ bool HasEmittedPCH;
protected:
ASTWriter &getWriter() { return Writer; }
public:
PCHGenerator(const Preprocessor &PP, StringRef OutputFile,
clang::Module *Module,
- StringRef isysroot, raw_ostream *Out);
+ StringRef isysroot, raw_ostream *Out,
+ bool AllowASTWithErrors = false);
~PCHGenerator();
virtual void InitializeSema(Sema &S) { SemaPtr = &S; }
virtual void HandleTranslationUnit(ASTContext &Ctx);
virtual ASTMutationListener *GetASTMutationListener();
virtual ASTDeserializationListener *GetASTDeserializationListener();
+
+ bool hasEmittedPCH() const { return HasEmittedPCH; }
};
} // end namespace clang
public:
PrecompilePreambleConsumer(ASTUnit &Unit, const Preprocessor &PP,
StringRef isysroot, raw_ostream *Out)
- : PCHGenerator(PP, "", 0, isysroot, Out), Unit(Unit),
- Hash(Unit.getCurrentTopLevelHashValue()) {
+ : PCHGenerator(PP, "", 0, isysroot, Out, /*AllowASTWithErrors=*/true),
+ Unit(Unit), Hash(Unit.getCurrentTopLevelHashValue()) {
Hash = 0;
}
virtual void HandleTranslationUnit(ASTContext &Ctx) {
PCHGenerator::HandleTranslationUnit(Ctx);
- if (!Unit.getDiagnostics().hasErrorOccurred()) {
+ if (hasEmittedPCH()) {
// Translate the top-level declarations we captured during
// parsing into declaration IDs in the precompiled
// preamble. This will allow us to deserialize those top-level
class PrecompilePreambleAction : public ASTFrontendAction {
ASTUnit &Unit;
+ PrecompilePreambleConsumer *PreambleConsumer;
public:
- explicit PrecompilePreambleAction(ASTUnit &Unit) : Unit(Unit) {}
+ explicit PrecompilePreambleAction(ASTUnit &Unit)
+ : Unit(Unit), PreambleConsumer(0) {}
virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
StringRef InFile) {
CI.getPreprocessor().addPPCallbacks(
new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
- return new PrecompilePreambleConsumer(Unit, CI.getPreprocessor(), Sysroot,
- OS);
+ PreambleConsumer = new PrecompilePreambleConsumer(Unit,CI.getPreprocessor(),
+ Sysroot, OS);
+ return PreambleConsumer;
}
+ bool hasEmittedPreamblePCH() const {
+ return PreambleConsumer && PreambleConsumer->hasEmittedPCH();
+ }
+ virtual bool shouldEraseOutputFiles() { return !hasEmittedPreamblePCH(); }
+
virtual bool hasCodeCompletionSupport() const { return false; }
virtual bool hasASTFileSupport() const { return false; }
virtual TranslationUnitKind getTranslationUnitKind() { return TU_Prefix; }
Act->Execute();
Act->EndSourceFile();
- if (Diagnostics->hasErrorOccurred()) {
+ if (!Act->hasEmittedPreamblePCH()) {
// There were errors parsing the preamble, so no precompiled header was
// generated. Forget that we even tried.
// FIXME: Should we leave a note for ourselves to try again?
llvm::errs() << "\n";
}
- // Cleanup the output streams, and erase the output files if we encountered
- // an error.
- CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().hasErrorOccurred());
+ // Cleanup the output streams, and erase the output files if instructed by the
+ // FrontendAction.
+ CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
if (isCurrentFileAST()) {
CI.takeSema();
setCurrentInput(FrontendInputFile());
}
+bool FrontendAction::shouldEraseOutputFiles() {
+ return getCompilerInstance().getDiagnostics().hasErrorOccurred();
+}
+
//===----------------------------------------------------------------------===//
// Utility Actions
//===----------------------------------------------------------------------===//
StringRef OutputFile,
clang::Module *Module,
StringRef isysroot,
- raw_ostream *OS)
+ raw_ostream *OS, bool AllowASTWithErrors)
: PP(PP), OutputFile(OutputFile), Module(Module),
isysroot(isysroot.str()), Out(OS),
- SemaPtr(0), Stream(Buffer), Writer(Stream) {
+ SemaPtr(0), Stream(Buffer), Writer(Stream),
+ AllowASTWithErrors(AllowASTWithErrors),
+ HasEmittedPCH(false) {
}
PCHGenerator::~PCHGenerator() {
}
void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
- if (PP.getDiagnostics().hasErrorOccurred())
+ // Don't create a PCH if there were fatal failures during module loading.
+ if (PP.getModuleLoader().HadFatalFailure)
+ return;
+
+ bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
+ if (hasErrors && !AllowASTWithErrors)
return;
// Emit the PCH file
assert(SemaPtr && "No Sema?");
- Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot);
+ Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot, hasErrors);
// Write the generated bitstream to "Out".
Out->write((char *)&Buffer.front(), Buffer.size());
// Free up some memory, in case the process is kept alive.
Buffer.clear();
+
+ HasEmittedPCH = true;
}
ASTMutationListener *PCHGenerator::GetASTMutationListener() {
@interface I2
@end
-// RUN: env CINDEXTEST_EDITING=1 CINDEXTEST_FAILONERROR=1 \
-// RUN: c-index-test -test-load-source-reparse 1 local %s
+// RUN: env CINDEXTEST_EDITING=1 \
+// RUN: c-index-test -test-load-source-reparse 1 local %s | FileCheck %s
+
+// CHECK: preamble-reparse-with-BOM.m:2:12: ObjCInterfaceDecl=I2:2:12 Extent=[2:1 - 3:5]