[cfe-commits] r38647 - in /cfe/cfe/trunk: Lex/Preprocessor.cpp include/clang/Lex/Preprocessor.h

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:23:30 PDT 2007


Author: sabre
Date: Wed Jul 11 11:23:29 2007
New Revision: 38647

URL: http://llvm.org/viewvc/llvm-project?rev=38647&view=rev
Log:
Implement the multiple-include file optimization.

Modified:
    cfe/cfe/trunk/Lex/Preprocessor.cpp
    cfe/cfe/trunk/include/clang/Lex/Preprocessor.h

Modified: cfe/cfe/trunk/Lex/Preprocessor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Lex/Preprocessor.cpp?rev=38647&r1=38646&r2=38647&view=diff

==============================================================================
--- cfe/cfe/trunk/Lex/Preprocessor.cpp (original)
+++ cfe/cfe/trunk/Lex/Preprocessor.cpp Wed Jul 11 11:23:29 2007
@@ -56,7 +56,7 @@
   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 @@
   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 @@
   }
   
   // 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 @@
     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());

Modified: cfe/cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=38647&r1=38646&r2=38647&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/cfe/trunk/include/clang/Lex/Preprocessor.h Wed Jul 11 11:23:29 2007
@@ -181,8 +181,13 @@
     /// 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 @@
   // 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:





More information about the cfe-commits mailing list