aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/clang/Lex/Preprocessor.h2
-rw-r--r--include/clang/Lex/TokenLexer.h5
-rw-r--r--lib/Lex/TokenLexer.cpp11
3 files changed, 15 insertions, 3 deletions
diff --git a/include/clang/Lex/Preprocessor.h b/include/clang/Lex/Preprocessor.h
index ae7d3b6220..4f5def5c51 100644
--- a/include/clang/Lex/Preprocessor.h
+++ b/include/clang/Lex/Preprocessor.h
@@ -205,6 +205,8 @@ public:
IdentifierTable &getIdentifierTable() { return Identifiers; }
SelectorTable &getSelectorTable() { return Selectors; }
+ llvm::BumpPtrAllocator &getPreprocessorAllocator() { return BP; }
+
void setPTHManager(PTHManager* pm);
diff --git a/include/clang/Lex/TokenLexer.h b/include/clang/Lex/TokenLexer.h
index a3004b1c04..c0a61cf93e 100644
--- a/include/clang/Lex/TokenLexer.h
+++ b/include/clang/Lex/TokenLexer.h
@@ -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.
diff --git a/lib/Lex/TokenLexer.cpp b/lib/Lex/TokenLexer.cpp
index f0e2fbdfa6..898b3a780d 100644
--- a/lib/Lex/TokenLexer.cpp
+++ b/lib/Lex/TokenLexer.cpp
@@ -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;
}
}