]> granicus.if.org Git - clang/commitdiff
make the token lexer allocate its temporary token buffers for
authorChris Lattner <sabre@nondot.org>
Wed, 4 Mar 2009 06:50:57 +0000 (06:50 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 4 Mar 2009 06:50:57 +0000 (06:50 +0000)
preexpanded macro arguments from the preprocessor's bump pointer.
This reduces # mallocs from 12444 to 11792.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@66025 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Lex/Preprocessor.h
include/clang/Lex/TokenLexer.h
lib/Lex/TokenLexer.cpp

index ae7d3b6220da14adc994cac84c72eed1b1b8d130..4f5def5c5169f915d8f4536b6f654b6c65b7f19e 100644 (file)
@@ -205,6 +205,8 @@ public:
 
   IdentifierTable &getIdentifierTable() { return Identifiers; }
   SelectorTable &getSelectorTable() { return Selectors; }
+  llvm::BumpPtrAllocator &getPreprocessorAllocator() { return BP; }
+  
   
   void setPTHManager(PTHManager* pm);
 
index a3004b1c04b6632f80b3bf86c7bc6bbe79b58416..c0a61cf93ee55c84641aa9616382d7ce2179582c 100644 (file)
@@ -42,7 +42,10 @@ class TokenLexer {
 
   /// Tokens - This is the pointer to an array of tokens that the macro is
   /// defined to, with arguments expanded for function-like macros.  If this is
-  /// a token stream, these are the tokens we are returning.
+  /// a token stream, these are the tokens we are returning.  This points into
+  /// the macro definition we are lexing from, a scratch buffer allocated from
+  /// the preprocessor's bump pointer allocator, or some other buffer that we
+  /// may or may not own (depending on OwnsTokens).
   const Token *Tokens;
   
   /// NumTokens - This is the length of the Tokens array.
index f0e2fbdfa627e4b413b9dc9574b3b5cb525dbb4e..898b3a780dd248a0d4050b16b9111bf2209ea40c 100644 (file)
@@ -88,6 +88,7 @@ void TokenLexer::destroy() {
   if (OwnsTokens) {
     delete [] Tokens;
     Tokens = 0;
+    OwnsTokens = false;
   }
   
   // TokenLexer owns its formal arguments.
@@ -264,13 +265,19 @@ void TokenLexer::ExpandFunctionArguments() {
   
   // If anything changed, install this as the new Tokens list.
   if (MadeChange) {
+    assert(!OwnsTokens && "This would leak if we already own the token list");
     // This is deleted in the dtor.
     NumTokens = ResultToks.size();
-    Token *Res = new Token[ResultToks.size()];
+    llvm::BumpPtrAllocator &Alloc = PP.getPreprocessorAllocator();
+    Token *Res =
+      static_cast<Token *>(Alloc.Allocate(sizeof(Token)*ResultToks.size(),
+                                          llvm::alignof<Token>()));
     if (NumTokens)
       memcpy(Res, &ResultToks[0], NumTokens*sizeof(Token));
     Tokens = Res;
-    OwnsTokens = true;
+    
+    // The preprocessor bump pointer owns these tokens, not us.
+    OwnsTokens = false;
   }
 }