From e2473314b54de3259b1bb6f417e9af509f0d50ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 9 Dec 2009 19:14:07 +0100 Subject: [PATCH] Fixed #1024 (Preprocessor: doesn't expand macro in macro) --- lib/tokenize.cpp | 46 +++++++++++++++++++++++++++++---------- test/testpreprocessor.cpp | 7 ++++++ 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 66376e66d..50845a402 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -302,21 +302,26 @@ void Tokenizer::createTokens(std::istream &code) } // If token contains # characters, split it up - std::string temp; - for (std::string::size_type i = 0; i < CurrentToken.length(); ++i) + if (CurrentToken.find("##") == std::string::npos) + addtoken(CurrentToken.c_str(), lineno, FileIndex); + else { - if (CurrentToken[i] == '#' && CurrentToken.length() + 1 > i && CurrentToken[i+1] == '#') + std::string temp; + for (std::string::size_type i = 0; i < CurrentToken.length(); ++i) { - addtoken(temp.c_str(), lineno, FileIndex); - temp.clear(); - addtoken("##", lineno, FileIndex); - ++i; + if (CurrentToken[i] == '#' && CurrentToken.length() + 1 > i && CurrentToken[i+1] == '#') + { + addtoken(temp.c_str(), lineno, FileIndex); + temp.clear(); + addtoken("##", lineno, FileIndex); + ++i; + } + else + temp += CurrentToken[i]; } - else - temp += CurrentToken[i]; + addtoken(temp.c_str(), lineno, FileIndex); } - addtoken(temp.c_str(), lineno, FileIndex); CurrentToken.clear(); if (ch == '\n') @@ -341,8 +346,25 @@ void Tokenizer::createTokens(std::istream &code) CurrentToken += ch; } - addtoken(CurrentToken.c_str(), lineno, FileIndex); - + if (CurrentToken.find("##") == std::string::npos) + addtoken(CurrentToken.c_str(), lineno, FileIndex); + else + { + std::string temp; + for (std::string::size_type i = 0; i < CurrentToken.length(); ++i) + { + if (CurrentToken[i] == '#' && CurrentToken.length() + 1 > i && CurrentToken[i+1] == '#') + { + addtoken(temp.c_str(), lineno, FileIndex); + temp.clear(); + addtoken("##", lineno, FileIndex); + ++i; + } + else + temp += CurrentToken[i]; + } + addtoken(temp.c_str(), lineno, FileIndex); + } } void Tokenizer::simplifyTypedef() diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 288019195..9410c1e76 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -1200,6 +1200,13 @@ private: // Compare results.. ASSERT_EQUALS("\nfoo_20=20;\n", OurPreprocessor::expandMacros(filedata2)); + + const char filedata3[] = "#define ABCD 123\n" + "#define A(B) A##B\n" + "A(BCD)\n"; + + // Compare results.. + ASSERT_EQUALS("\n\n123\n", OurPreprocessor::expandMacros(filedata3)); }