Implement -fno-validate-pch at the -cc1 level, which suppresses most

of the usual consistency checks used to determine when a precompiled
header is incompatible with the translation unit it's being loaded
into.

Enable this option when loading a precompiled preamble, because the
preamble loader will be performing all of this checking itself. Enable
the preamble-based test now that it's working.

This option is also useful for debugging Clang's PCH
(<rdar://problem/7532213>).

llvm-svn: 109475
This commit is contained in:
Douglas Gregor 2010-07-27 00:27:13 +00:00
parent 66c5eef182
commit ce3a8293a0
10 changed files with 61 additions and 28 deletions

View File

@ -441,6 +441,8 @@ def fpascal_strings : Flag<"-fpascal-strings">,
HelpText<"Recognize and construct Pascal-style string literals">; HelpText<"Recognize and construct Pascal-style string literals">;
def fno_rtti : Flag<"-fno-rtti">, def fno_rtti : Flag<"-fno-rtti">,
HelpText<"Disable generation of rtti information">; HelpText<"Disable generation of rtti information">;
def fno_validate_pch : Flag<"-fno-validate-pch">,
HelpText<"Disable validation of precompiled headers">;
def fshort_wchar : Flag<"-fshort-wchar">, def fshort_wchar : Flag<"-fshort-wchar">,
HelpText<"Force wchar_t to be a short unsigned int">; HelpText<"Force wchar_t to be a short unsigned int">;
def static_define : Flag<"-static-define">, def static_define : Flag<"-static-define">,

View File

@ -502,13 +502,15 @@ public:
/// Create an external AST source to read a PCH file and attach it to the AST /// Create an external AST source to read a PCH file and attach it to the AST
/// context. /// context.
void createPCHExternalASTSource(llvm::StringRef Path); void createPCHExternalASTSource(llvm::StringRef Path,
bool DisablePCHValidation);
/// Create an external AST source to read a PCH file. /// Create an external AST source to read a PCH file.
/// ///
/// \return - The new object on success, or null on failure. /// \return - The new object on success, or null on failure.
static ExternalASTSource * static ExternalASTSource *
createPCHExternalASTSource(llvm::StringRef Path, const std::string &Sysroot, createPCHExternalASTSource(llvm::StringRef Path, const std::string &Sysroot,
bool DisablePCHValidation,
Preprocessor &PP, ASTContext &Context); Preprocessor &PP, ASTContext &Context);
/// Get the PCH reader, if any. /// Get the PCH reader, if any.

View File

@ -409,6 +409,10 @@ private:
/// precompiled header. /// precompiled header.
const char *isysroot; const char *isysroot;
/// \brief Whether to disable the normal validation performed on precompiled
/// headers when they are loaded.
bool DisableValidation;
/// \brief Mapping from switch-case IDs in the PCH file to /// \brief Mapping from switch-case IDs in the PCH file to
/// switch-case statements. /// switch-case statements.
std::map<unsigned, SwitchCase *> SwitchCaseStmts; std::map<unsigned, SwitchCase *> SwitchCaseStmts;
@ -599,7 +603,12 @@ public:
/// \param isysroot If non-NULL, the system include path specified by the /// \param isysroot If non-NULL, the system include path specified by the
/// user. This is only used with relocatable PCH files. If non-NULL, /// user. This is only used with relocatable PCH files. If non-NULL,
/// a relocatable PCH file will use the default path "/". /// a relocatable PCH file will use the default path "/".
PCHReader(Preprocessor &PP, ASTContext *Context, const char *isysroot = 0); ///
/// \param DisableValidation If true, the PCH reader will suppress most
/// of its regular consistency checking, allowing the use of precompiled
/// headers that cannot be determined to be compatible.
PCHReader(Preprocessor &PP, ASTContext *Context, const char *isysroot = 0,
bool DisableValidation = false);
/// \brief Load the PCH file without using any pre-initialized Preprocessor. /// \brief Load the PCH file without using any pre-initialized Preprocessor.
/// ///
@ -618,8 +627,13 @@ public:
/// \param isysroot If non-NULL, the system include path specified by the /// \param isysroot If non-NULL, the system include path specified by the
/// user. This is only used with relocatable PCH files. If non-NULL, /// user. This is only used with relocatable PCH files. If non-NULL,
/// a relocatable PCH file will use the default path "/". /// a relocatable PCH file will use the default path "/".
PCHReader(SourceManager &SourceMgr, FileManager &FileMgr, ///
Diagnostic &Diags, const char *isysroot = 0); /// \param DisableValidation If true, the PCH reader will suppress most
/// of its regular consistency checking, allowing the use of precompiled
/// headers that cannot be determined to be compatible.
PCHReader(SourceManager &SourceMgr, FileManager &FileMgr,
Diagnostic &Diags, const char *isysroot = 0,
bool DisableValidation = false);
~PCHReader(); ~PCHReader();
/// \brief Load the precompiled header designated by the given file /// \brief Load the precompiled header designated by the given file

View File

@ -43,6 +43,10 @@ public:
/// The implicit PCH included at the start of the translation unit, or empty. /// The implicit PCH included at the start of the translation unit, or empty.
std::string ImplicitPCHInclude; std::string ImplicitPCHInclude;
/// \brief When true, disables most of the normal validation performed on
/// precompiled headers.
bool DisablePCHValidation;
/// \brief If non-zero, the implicit PCH include is actually a precompiled /// \brief If non-zero, the implicit PCH include is actually a precompiled
/// preamble that covers this number of bytes in the main source file. /// preamble that covers this number of bytes in the main source file.
/// ///
@ -113,6 +117,7 @@ public:
public: public:
PreprocessorOptions() : UsePredefines(true), DetailedRecord(false), PreprocessorOptions() : UsePredefines(true), DetailedRecord(false),
DisablePCHValidation(false),
PrecompiledPreambleBytes(0, true), PrecompiledPreambleBytes(0, true),
RetainRemappedFileBuffers(false) { } RetainRemappedFileBuffers(false) { }

View File

@ -394,6 +394,7 @@ bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) {
PreprocessorOpts.PrecompiledPreambleBytes.second PreprocessorOpts.PrecompiledPreambleBytes.second
= PreambleEndsAtStartOfLine; = PreambleEndsAtStartOfLine;
PreprocessorOpts.ImplicitPCHInclude = PreambleFile.str(); PreprocessorOpts.ImplicitPCHInclude = PreambleFile.str();
PreprocessorOpts.DisablePCHValidation = true;
} }
llvm::OwningPtr<TopLevelDeclTrackerAction> Act; llvm::OwningPtr<TopLevelDeclTrackerAction> Act;
@ -415,9 +416,11 @@ bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) {
Act->EndSourceFile(); Act->EndSourceFile();
// Remove the overridden buffer we used for the preamble. // Remove the overridden buffer we used for the preamble.
if (OverrideMainBuffer) if (OverrideMainBuffer) {
PreprocessorOpts.eraseRemappedFile( PreprocessorOpts.eraseRemappedFile(
PreprocessorOpts.remapped_file_buffer_end() - 1); PreprocessorOpts.remapped_file_buffer_end() - 1);
PreprocessorOpts.DisablePCHValidation = true;
}
Clang.takeDiagnosticClient(); Clang.takeDiagnosticClient();
@ -426,9 +429,11 @@ bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) {
error: error:
// Remove the overridden buffer we used for the preamble. // Remove the overridden buffer we used for the preamble.
if (OverrideMainBuffer) if (OverrideMainBuffer) {
PreprocessorOpts.eraseRemappedFile( PreprocessorOpts.eraseRemappedFile(
PreprocessorOpts.remapped_file_buffer_end() - 1); PreprocessorOpts.remapped_file_buffer_end() - 1);
PreprocessorOpts.DisablePCHValidation = true;
}
Clang.takeSourceManager(); Clang.takeSourceManager();
Clang.takeFileManager(); Clang.takeFileManager();

View File

@ -250,9 +250,11 @@ void CompilerInstance::createASTContext() {
// ExternalASTSource // ExternalASTSource
void CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path) { void CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path,
bool DisablePCHValidation) {
llvm::OwningPtr<ExternalASTSource> Source; llvm::OwningPtr<ExternalASTSource> Source;
Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot, Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot,
DisablePCHValidation,
getPreprocessor(), getASTContext())); getPreprocessor(), getASTContext()));
// Remember the PCHReader, but in a non-owning way. // Remember the PCHReader, but in a non-owning way.
Reader = static_cast<PCHReader*>(Source.get()); Reader = static_cast<PCHReader*>(Source.get());
@ -262,11 +264,13 @@ void CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path) {
ExternalASTSource * ExternalASTSource *
CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path, CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path,
const std::string &Sysroot, const std::string &Sysroot,
bool DisablePCHValidation,
Preprocessor &PP, Preprocessor &PP,
ASTContext &Context) { ASTContext &Context) {
llvm::OwningPtr<PCHReader> Reader; llvm::OwningPtr<PCHReader> Reader;
Reader.reset(new PCHReader(PP, &Context, Reader.reset(new PCHReader(PP, &Context,
Sysroot.empty() ? 0 : Sysroot.c_str())); Sysroot.empty() ? 0 : Sysroot.c_str(),
DisablePCHValidation));
switch (Reader->ReadPCH(Path)) { switch (Reader->ReadPCH(Path)) {
case PCHReader::Success: case PCHReader::Success:

View File

@ -1353,7 +1353,8 @@ static void ParsePreprocessorArgs(PreprocessorOptions &Opts, ArgList &Args,
Opts.TokenCache = Opts.ImplicitPTHInclude; Opts.TokenCache = Opts.ImplicitPTHInclude;
Opts.UsePredefines = !Args.hasArg(OPT_undef); Opts.UsePredefines = !Args.hasArg(OPT_undef);
Opts.DetailedRecord = Args.hasArg(OPT_detailed_preprocessing_record); Opts.DetailedRecord = Args.hasArg(OPT_detailed_preprocessing_record);
Opts.DisablePCHValidation = Args.hasArg(OPT_fno_validate_pch);
if (const Arg *A = Args.getLastArg(OPT_preamble_bytes_EQ)) { if (const Arg *A = Args.getLastArg(OPT_preamble_bytes_EQ)) {
llvm::StringRef Value(A->getValue(Args)); llvm::StringRef Value(A->getValue(Args));
size_t Comma = Value.find(','); size_t Comma = Value.find(',');

View File

@ -118,7 +118,8 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) { if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
assert(hasPCHSupport() && "This action does not have PCH support!"); assert(hasPCHSupport() && "This action does not have PCH support!");
CI.createPCHExternalASTSource( CI.createPCHExternalASTSource(
CI.getPreprocessorOpts().ImplicitPCHInclude); CI.getPreprocessorOpts().ImplicitPCHInclude,
CI.getPreprocessorOpts().DisablePCHValidation);
if (!CI.getASTContext().getExternalSource()) if (!CI.getASTContext().getExternalSource())
goto failure; goto failure;
} }

View File

@ -414,13 +414,14 @@ void PCHValidator::ReadCounter(unsigned Value) {
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
PCHReader::PCHReader(Preprocessor &PP, ASTContext *Context, PCHReader::PCHReader(Preprocessor &PP, ASTContext *Context,
const char *isysroot) const char *isysroot, bool DisableValidation)
: Listener(new PCHValidator(PP, *this)), DeserializationListener(0), : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context), Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
Consumer(0), MethodPoolLookupTable(0), MethodPoolLookupTableData(0), Consumer(0), MethodPoolLookupTable(0), MethodPoolLookupTableData(0),
TotalSelectorsInMethodPool(0), SelectorOffsets(0), TotalSelectorsInMethodPool(0), SelectorOffsets(0),
TotalNumSelectors(0), isysroot(isysroot), NumStatHits(0), NumStatMisses(0), TotalNumSelectors(0), isysroot(isysroot),
DisableValidation(DisableValidation), NumStatHits(0), NumStatMisses(0),
NumSLocEntriesRead(0), TotalNumSLocEntries(0), NumStatementsRead(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0), NumStatementsRead(0),
TotalNumStatements(0), NumMacrosRead(0), NumMethodPoolSelectorsRead(0), TotalNumStatements(0), NumMacrosRead(0), NumMethodPoolSelectorsRead(0),
NumMethodPoolMisses(0), TotalNumMacros(0), NumLexicalDeclContextsRead(0), NumMethodPoolMisses(0), TotalNumMacros(0), NumLexicalDeclContextsRead(0),
@ -430,12 +431,14 @@ PCHReader::PCHReader(Preprocessor &PP, ASTContext *Context,
} }
PCHReader::PCHReader(SourceManager &SourceMgr, FileManager &FileMgr, PCHReader::PCHReader(SourceManager &SourceMgr, FileManager &FileMgr,
Diagnostic &Diags, const char *isysroot) Diagnostic &Diags, const char *isysroot,
bool DisableValidation)
: DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr), : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0), Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
MethodPoolLookupTable(0), MethodPoolLookupTableData(0), MethodPoolLookupTable(0), MethodPoolLookupTableData(0),
TotalSelectorsInMethodPool(0), SelectorOffsets(0), TotalSelectorsInMethodPool(0), SelectorOffsets(0),
TotalNumSelectors(0), isysroot(isysroot), NumStatHits(0), NumStatMisses(0), TotalNumSelectors(0), isysroot(isysroot),
DisableValidation(DisableValidation), NumStatHits(0), NumStatMisses(0),
NumSLocEntriesRead(0), TotalNumSLocEntries(0), NumStatementsRead(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0), NumStatementsRead(0),
TotalNumStatements(0), NumMacrosRead(0), NumMethodPoolSelectorsRead(0), TotalNumStatements(0), NumMacrosRead(0), NumMethodPoolSelectorsRead(0),
NumMethodPoolMisses(0), TotalNumMacros(0), NumLexicalDeclContextsRead(0), NumMethodPoolMisses(0), TotalNumMacros(0), NumLexicalDeclContextsRead(0),
@ -1019,14 +1022,15 @@ PCHReader::PCHReadResult PCHReader::ReadSLocEntryRecord(unsigned ID) {
return Failure; return Failure;
} }
if ((off_t)Record[4] != File->getSize() if (!DisableValidation &&
((off_t)Record[4] != File->getSize()
#if !defined(LLVM_ON_WIN32) #if !defined(LLVM_ON_WIN32)
// In our regression testing, the Windows file system seems to // In our regression testing, the Windows file system seems to
// have inconsistent modification times that sometimes // have inconsistent modification times that sometimes
// erroneously trigger this error-handling path. // erroneously trigger this error-handling path.
|| (time_t)Record[5] != File->getModificationTime() || (time_t)Record[5] != File->getModificationTime()
#endif #endif
) { )) {
Diag(diag::err_fe_pch_file_modified) Diag(diag::err_fe_pch_file_modified)
<< Filename; << Filename;
return Failure; return Failure;
@ -1481,7 +1485,7 @@ PCHReader::ReadPCHBlock(PerFileData &F) {
break; break;
case pch::METADATA: { case pch::METADATA: {
if (Record[0] != pch::VERSION_MAJOR) { if (Record[0] != pch::VERSION_MAJOR && !DisableValidation) {
Diag(Record[0] < pch::VERSION_MAJOR? diag::warn_pch_version_too_old Diag(Record[0] < pch::VERSION_MAJOR? diag::warn_pch_version_too_old
: diag::warn_pch_version_too_new); : diag::warn_pch_version_too_new);
return IgnorePCH; return IgnorePCH;
@ -1501,7 +1505,7 @@ PCHReader::ReadPCHBlock(PerFileData &F) {
Error("CHAINED_METADATA is not first record in block"); Error("CHAINED_METADATA is not first record in block");
return Failure; return Failure;
} }
if (Record[0] != pch::VERSION_MAJOR) { if (Record[0] != pch::VERSION_MAJOR && !DisableValidation) {
Diag(Record[0] < pch::VERSION_MAJOR? diag::warn_pch_version_too_old Diag(Record[0] < pch::VERSION_MAJOR? diag::warn_pch_version_too_old
: diag::warn_pch_version_too_new); : diag::warn_pch_version_too_new);
return IgnorePCH; return IgnorePCH;
@ -1536,7 +1540,7 @@ PCHReader::ReadPCHBlock(PerFileData &F) {
break; break;
case pch::LANGUAGE_OPTIONS: case pch::LANGUAGE_OPTIONS:
if (ParseLanguageOptions(Record)) if (ParseLanguageOptions(Record) && !DisableValidation)
return IgnorePCH; return IgnorePCH;
break; break;
@ -1704,7 +1708,7 @@ PCHReader::ReadPCHBlock(PerFileData &F) {
case pch::VERSION_CONTROL_BRANCH_REVISION: { case pch::VERSION_CONTROL_BRANCH_REVISION: {
const std::string &CurBranch = getClangFullRepositoryVersion(); const std::string &CurBranch = getClangFullRepositoryVersion();
llvm::StringRef PCHBranch(BlobStart, BlobLen); llvm::StringRef PCHBranch(BlobStart, BlobLen);
if (llvm::StringRef(CurBranch) != PCHBranch) { if (llvm::StringRef(CurBranch) != PCHBranch && !DisableValidation) {
Diag(diag::warn_pch_different_branch) << PCHBranch << CurBranch; Diag(diag::warn_pch_different_branch) << PCHBranch << CurBranch;
return IgnorePCH; return IgnorePCH;
} }
@ -1759,7 +1763,7 @@ PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
} }
// Check the predefines buffers. // Check the predefines buffers.
if (CheckPredefinesBuffers()) if (!DisableValidation && CheckPredefinesBuffers())
return IgnorePCH; return IgnorePCH;
if (PP) { if (PP) {

View File

@ -2,12 +2,7 @@
#include "preamble.h" #include "preamble.h"
int wibble(int); int wibble(int);
// FIXME: Turn on use of preamble files
// RUN: %clang -x c-header -o %t.pch %S/Inputs/prefix.h // RUN: %clang -x c-header -o %t.pch %S/Inputs/prefix.h
// RUN: c-index-test -test-load-source-reparse 5 local -I %S/Inputs -include %t %s | FileCheck %s // RUN: CINDEXTEST_EDITING=1 c-index-test -test-load-source-reparse 5 local -I %S/Inputs -include %t %s | FileCheck %s
// CHECK: preamble.h:1:5: FunctionDecl=bar:1:5 Extent=[1:5 - 1:13]
// CHECK: preamble.h:1:12: ParmDecl=:1:12 (Definition) Extent=[1:9 - 1:13]
// CHECK: preamble.c:3:5: FunctionDecl=wibble:3:5 Extent=[3:5 - 3:16] // CHECK: preamble.c:3:5: FunctionDecl=wibble:3:5 Extent=[3:5 - 3:16]
// CHECK: preamble.c:3:15: ParmDecl=:3:15 (Definition) Extent=[3:12 - 3:16] // CHECK: preamble.c:3:15: ParmDecl=:3:15 (Definition) Extent=[3:12 - 3:16]