Implement the multiple-include file optimization.

llvm-svn: 38647
This commit is contained in:
Chris Lattner 2006-07-04 07:26:10 +00:00
parent 371ac8a9b7
commit 3665f161ca
2 changed files with 23 additions and 5 deletions

View File

@ -56,7 +56,7 @@ Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
NumDirectives = NumIncluded = NumDefined = NumUndefined = NumPragma = 0;
NumIf = NumElse = NumEndif = 0;
NumEnteredSourceFiles = NumMacroExpanded = NumFastMacroExpanded = 0;
MaxIncludeStackDepth = 0;
MaxIncludeStackDepth = 0; NumMultiIncludeFileOptzn = 0;
NumSkipped = 0;
// Macro expansion is enabled.
@ -185,6 +185,8 @@ void Preprocessor::PrintStats() {
std::cerr << " " << NumDefined << " #define.\n";
std::cerr << " " << NumUndefined << " #undef.\n";
std::cerr << " " << NumIncluded << " #include/#include_next/#import.\n";
std::cerr << " " << NumMultiIncludeFileOptzn << " #includes skipped due to"
<< " the multi-include optimization.\n";
std::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
std::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
std::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
@ -740,10 +742,13 @@ void Preprocessor::HandleEndOfFile(LexerToken &Result, bool isEndOfMacro) {
}
// See if this file had a controlling macro.
if (CurLexer) { // Not ending a macro...
if (CurLexer) { // Not ending a macro, ignore it.
if (const IdentifierTokenInfo *ControllingMacro =
CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
;
// Okay, this has a controlling macro, remember in PerFileInfo.
if (const FileEntry *FE =
SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
getFileInfo(FE).ControllingMacro = ControllingMacro;
}
}
@ -1244,6 +1249,14 @@ void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok,
if (FileInfo.isImport)
return;
}
// Next, check to see if the file is wrapped with #ifndef guards. If so, and
// if the macro that guards it is defined, we know the #include has no effect.
if (FileInfo.ControllingMacro && FileInfo.ControllingMacro->getMacroInfo()) {
++NumMultiIncludeFileOptzn;
return;
}
// Look up the file, create a File ID for it.
unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation());

View File

@ -181,8 +181,13 @@ private:
/// already.
unsigned short NumIncludes;
/// ControllingMacro - If this file has a #ifndef XXX (or equivalent) guard
/// that protects the entire contents of the file, this is the identifier
/// for the macro that controls whether or not it has any effect.
const IdentifierTokenInfo *ControllingMacro;
PerFileInfo() : isImport(false), DirInfo(DirectoryLookup::NormalHeaderDir),
NumIncludes(0) {}
NumIncludes(0), ControllingMacro(0) {}
};
/// FileInfo - This contains all of the preprocessor-specific data about files
@ -193,7 +198,7 @@ private:
// Various statistics we track for performance analysis.
unsigned NumDirectives, NumIncluded, NumDefined, NumUndefined, NumPragma;
unsigned NumIf, NumElse, NumEndif;
unsigned NumEnteredSourceFiles, MaxIncludeStackDepth;
unsigned NumEnteredSourceFiles, MaxIncludeStackDepth,NumMultiIncludeFileOptzn;
unsigned NumMacroExpanded, NumFastMacroExpanded;
unsigned NumSkipped;
public: