summaryrefslogtreecommitdiffstats
path: root/clang/Lex/MacroExpander.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2006-07-26 05:22:49 +0000
committerChris Lattner <sabre@nondot.org>2006-07-26 05:22:49 +0000
commitc1410dc1e616841a8483a4849ecdc664df0d7219 (patch)
treefd16bb87f6465fd888f48d4d4fdff097de4aa69a /clang/Lex/MacroExpander.cpp
parent6fc08bc77dcef105acf197836f40b3a96e8daa01 (diff)
downloadbcm5719-llvm-c1410dc1e616841a8483a4849ecdc664df0d7219.tar.gz
bcm5719-llvm-c1410dc1e616841a8483a4849ecdc664df0d7219.zip
Change MacroArgs to allocate space for the unexpanded tokens immediately after
the MacroArgs object itself. This is a bit more efficient and will be even more so shortly. llvm-svn: 38756
Diffstat (limited to 'clang/Lex/MacroExpander.cpp')
-rw-r--r--clang/Lex/MacroExpander.cpp45
1 files changed, 36 insertions, 9 deletions
diff --git a/clang/Lex/MacroExpander.cpp b/clang/Lex/MacroExpander.cpp
index cd802ee4e8a..24aab614c8a 100644
--- a/clang/Lex/MacroExpander.cpp
+++ b/clang/Lex/MacroExpander.cpp
@@ -24,12 +24,37 @@ using namespace clang;
// MacroArgs Implementation
//===----------------------------------------------------------------------===//
-MacroArgs::MacroArgs(const MacroInfo *MI, std::vector<LexerToken> &UnexpArgs) {
+/// MacroArgs ctor function - This destroys the vector passed in.
+MacroArgs *MacroArgs::create(const MacroInfo *MI,
+ const std::vector<LexerToken> &UnexpArgTokens) {
assert(MI->isFunctionLike() &&
"Can't have args for an object-like macro!");
- UnexpArgTokens.swap(UnexpArgs);
+
+ // Allocate memory for the MacroArgs object with the lexer tokens at the end.
+ unsigned NumToks = UnexpArgTokens.size();
+ MacroArgs *Result = (MacroArgs*)malloc(sizeof(MacroArgs) +
+ NumToks*sizeof(LexerToken));
+ // Construct the macroargs object.
+ new (Result) MacroArgs(NumToks);
+
+ // Copy the actual unexpanded tokens to immediately after the result ptr.
+ if (NumToks)
+ memcpy(const_cast<LexerToken*>(Result->getUnexpArgument(0)),
+ &UnexpArgTokens[0], NumToks*sizeof(LexerToken));
+
+ return Result;
+}
+
+/// destroy - Destroy and deallocate the memory for this object.
+///
+void MacroArgs::destroy() {
+ // Run the dtor to deallocate the vectors.
+ this->~MacroArgs();
+ // Release the memory for the object.
+ free(this);
}
+
/// getArgLength - Given a pointer to an expanded or unexpanded argument,
/// return the number of tokens, not counting the EOF, that make up the
/// argument.
@@ -44,11 +69,13 @@ unsigned MacroArgs::getArgLength(const LexerToken *ArgPtr) {
/// getUnexpArgument - Return the unexpanded tokens for the specified formal.
///
const LexerToken *MacroArgs::getUnexpArgument(unsigned Arg) const {
- // Scan to find Arg.
- const LexerToken *Start = &UnexpArgTokens[0];
+ // The unexpanded argument tokens start immediately after the MacroArgs object
+ // in memory.
+ const LexerToken *Start = (const LexerToken *)(this+1);
const LexerToken *Result = Start;
+ // Scan to find Arg.
for (; Arg; ++Result) {
- assert(Result < Start+UnexpArgTokens.size() && "Invalid arg #");
+ assert(Result < Start+NumUnexpArgTokens && "Invalid arg #");
if (Result->getKind() == tok::eof)
--Arg;
}
@@ -75,11 +102,11 @@ bool MacroArgs::ArgNeedsPreexpansion(const LexerToken *ArgTok) const {
/// argument.
const std::vector<LexerToken> &
MacroArgs::getPreExpArgument(unsigned Arg, Preprocessor &PP) {
- assert(Arg < UnexpArgTokens.size() && "Invalid argument number!");
+ assert(Arg < NumUnexpArgTokens && "Invalid argument number!");
// If we have already computed this, return it.
if (PreExpArgTokens.empty())
- PreExpArgTokens.resize(UnexpArgTokens.size());
+ PreExpArgTokens.resize(NumUnexpArgTokens);
std::vector<LexerToken> &Result = PreExpArgTokens[Arg];
if (!Result.empty()) return Result;
@@ -189,7 +216,7 @@ static LexerToken StringifyArgument(const LexerToken *ArgToks,
/// that has been 'stringified' as required by the # operator.
const LexerToken &MacroArgs::getStringifiedArgument(unsigned ArgNo,
Preprocessor &PP) {
- assert(ArgNo < UnexpArgTokens.size() && "Invalid argument number!");
+ assert(ArgNo < NumUnexpArgTokens && "Invalid argument number!");
if (StringifiedArgs.empty()) {
StringifiedArgs.resize(getNumArguments());
memset(&StringifiedArgs[0], 0,
@@ -252,7 +279,7 @@ MacroExpander::~MacroExpander() {
delete [] MacroTokens;
// MacroExpander owns its formal arguments.
- delete ActualArgs;
+ if (ActualArgs) ActualArgs->destroy();
}
/// Expand the arguments of a function-like macro so that we can quickly
OpenPOWER on IntegriCloud