[cfe-commits] r49973 - in /cfe/trunk: Driver/HTMLPrint.cpp Driver/clang.cpp include/clang/Lex/Preprocessor.h lib/Lex/Preprocessor.cpp

Chris Lattner sabre at nondot.org
Sat Apr 19 16:09:32 PDT 2008


Author: lattner
Date: Sat Apr 19 18:09:31 2008
New Revision: 49973

URL: http://llvm.org/viewvc/llvm-project?rev=49973&view=rev
Log:
simplify ownership of the predefines buffer.

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

Modified: cfe/trunk/Driver/HTMLPrint.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/HTMLPrint.cpp?rev=49973&r1=49972&r2=49973&view=diff

==============================================================================
--- cfe/trunk/Driver/HTMLPrint.cpp (original)
+++ cfe/trunk/Driver/HTMLPrint.cpp Sat Apr 19 18:09:31 2008
@@ -67,7 +67,7 @@
   // for example.
   
   if (PP) html::SyntaxHighlight(R, FileID, *PP);
-  if (PPF) html::HighlightMacros(R, FileID, *PP);  
+  if (PPF) html::HighlightMacros(R, FileID, *PP);
   html::EscapeText(R, FileID, false, true);
   
   // Open the output.

Modified: cfe/trunk/Driver/clang.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/clang.cpp?rev=49973&r1=49972&r2=49973&view=diff

==============================================================================
--- cfe/trunk/Driver/clang.cpp (original)
+++ cfe/trunk/Driver/clang.cpp Sat Apr 19 18:09:31 2008
@@ -588,10 +588,7 @@
 ///
 static unsigned InitializePreprocessor(Preprocessor &PP,
                                        bool InitializeSourceMgr, 
-                                       const std::string &InFile,
-                                       std::vector<char> &PredefineBuffer) {
-  
-
+                                       const std::string &InFile) {
   FileManager &FileMgr = PP.getFileManager();
   
   // Figure out where to get and map in the main file.
@@ -615,6 +612,8 @@
     }
   }
 
+  std::vector<char> PredefineBuffer;
+
   // Add macros from the command line.
   unsigned d = 0, D = D_macros.size();
   unsigned u = 0, U = U_macros.size();
@@ -631,13 +630,9 @@
   for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i)
     AddImplicitInclude(PredefineBuffer, ImplicitIncludes[i]);
   
-  // Null terminate PredefinedBuffer and add it.  We actually need to make a
-  // copy because PP will own the string.
+  // Null terminate PredefinedBuffer and add it.
   PredefineBuffer.push_back(0);
-  
-  char* predefines = new char[PredefineBuffer.size()];
-  std::copy(PredefineBuffer.begin(), PredefineBuffer.end(), predefines);
-  PP.setPredefines(predefines);
+  PP.setPredefines(&PredefineBuffer[0]);
   
   // Once we've read this, we're done.
   return SourceMgr.getMainFileID();
@@ -1031,7 +1026,6 @@
   TargetInfo        &Target;
   SourceManager     &SourceMgr;
   HeaderSearch      &HeaderInfo;
-  std::vector<char> PredefineBuffer;
   bool              InitializeSourceMgr;
   
 public:
@@ -1046,19 +1040,15 @@
   virtual ~DriverPreprocessorFactory() {}
   
   virtual Preprocessor* CreatePreprocessor() {
-    PredefineBuffer.clear();
-
     Preprocessor* PP = new Preprocessor(Diags, LangInfo, Target,
                                         SourceMgr, HeaderInfo);
     
-    if (!InitializePreprocessor(*PP, InitializeSourceMgr, InFile,
-                                PredefineBuffer)) {
+    if (!InitializePreprocessor(*PP, InitializeSourceMgr, InFile)) {
       delete PP;
       return NULL;
     }
     
     InitializeSourceMgr = false;
-    
     return PP;
   }
 };

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

==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Sat Apr 19 18:09:31 2008
@@ -130,9 +130,9 @@
   unsigned NumFastMacroExpanded, NumTokenPaste, NumFastTokenPaste;
   unsigned NumSkipped;
   
-  /// Predefines - This pointer, if non-null, are the predefined macros that 
-  /// preprocessor should use from the command line etc.
-  const char *Predefines;
+  /// Predefines - This string is the predefined macros that preprocessor
+  /// should use from the command line etc.
+  std::string Predefines;
   
   /// TokenLexerCache - Cache macro expanders to reduce malloc traffic.
   enum { TokenLexerCacheSize = 8 };
@@ -196,11 +196,10 @@
   ///
   void setMacroInfo(IdentifierInfo *II, MacroInfo *MI);
   
-  /// setPredefines - Set the predefines for this Preprocessor.
-  ///  The Preprocessor assumes ownership of this pointer.
-  void setPredefines(const char *P) {
-    Predefines = P;
-  }
+  /// setPredefines - Set the predefines for this Preprocessor.  These
+  /// predefines are automatically injected when parsing the main file.
+  void setPredefines(const char *P) { Predefines = P; }
+  void setPredefines(const std::string &P) { Predefines = P; }
   
   /// getIdentifierInfo - Return information about the specified preprocessor
   /// identifier token.  The version of this method that takes two character

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

==============================================================================
--- cfe/trunk/lib/Lex/Preprocessor.cpp (original)
+++ cfe/trunk/lib/Lex/Preprocessor.cpp Sat Apr 19 18:09:31 2008
@@ -72,8 +72,6 @@
   // This gets unpoisoned where it is allowed.
   (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
   
-  Predefines = 0;
-  
   // Initialize the pragma handlers.
   PragmaHandlers = new PragmaNamespace(0);
   RegisterBuiltinPragmas();
@@ -112,8 +110,6 @@
   delete ScratchBuf;
 
   delete Callbacks;
-  
-  delete [] Predefines;
 }
 
 /// Diag - Forwarding function for diagnostics.  This emits a diagnostic at
@@ -463,7 +459,7 @@
   InitializePredefinedMacros(*this, PrologFile);
   
   // Add on the predefines from the driver.
-  PrologFile.insert(PrologFile.end(), Predefines,Predefines+strlen(Predefines));
+  PrologFile.insert(PrologFile.end(), Predefines.begin(), Predefines.end());
   
   // Memory buffer must end with a null byte!
   PrologFile.push_back(0);





More information about the cfe-commits mailing list