From 40887ca99c2e0e33794d4312c71160ec4682dedd Mon Sep 17 00:00:00 2001 From: Simon Martin Date: Sat, 10 Aug 2013 23:28:02 +0200 Subject: [PATCH] Ticket #4703: Trim macro parameters. --- lib/preprocessor.cpp | 19 +++++++++++++++++-- test/testpreprocessor.cpp | 7 +++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 7981d0f52..a83d8d1f2 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -2275,6 +2275,21 @@ static void skipstring(const std::string &line, std::string::size_type &pos) } } +/** + * Remove heading and trailing whitespaces from the input parameter. + * #param s The string to trim. + */ +static std::string trim(const std::string& s) +{ + std::string::size_type beg = s.find_first_not_of(" \t"); + if (beg == std::string::npos) + return s; + std::string::size_type end = s.find_last_not_of(" \t"); + if (end == std::string::npos) + return s.substr(beg); + return s.substr(beg, end - beg + 1); +} + /** * @brief get parameters from code. For example 'foo(1,2)' => '1','2' * @param line in: The code @@ -2319,7 +2334,7 @@ static void getparams(const std::string &line, --parlevel; if (parlevel <= 0) { endFound = true; - params.push_back(par); + params.push_back(trim(par)); break; } } @@ -2342,7 +2357,7 @@ static void getparams(const std::string &line, // new parameter if (parlevel == 1 && line[pos] == ',') { - params.push_back(par); + params.push_back(trim(par)); par = ""; } diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 89a216329..96b95322f 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -178,6 +178,7 @@ private: TEST_CASE(macro_simple13); TEST_CASE(macro_simple14); TEST_CASE(macro_simple15); + TEST_CASE(macro_simple16); // #4703: Macro parameters not trimmed TEST_CASE(macroInMacro1); TEST_CASE(macroInMacro2); TEST_CASE(macro_mismatch); @@ -1927,6 +1928,12 @@ private: ASSERT_EQUALS("\n$\"foo\"\n", OurPreprocessor::expandMacros(filedata)); } + void macro_simple16() { // # 4703 + const char filedata[] = "#define MACRO( A, B, C ) class A##B##C##Creator {};\n" + "MACRO( B\t, U , G )"; + ASSERT_EQUALS("\n$class BUGCreator{};", OurPreprocessor::expandMacros(filedata)); + } + void macroInMacro1() { { const char filedata[] = "#define A(m) long n = m; n++;\n"