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)); }