Skip to content

Commit

Permalink
Ticket danmar#4703: Trim macro parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
simartin committed Aug 11, 2013
1 parent 56d8073 commit 40887ca
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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 = "";
}

Expand Down
7 changes: 7 additions & 0 deletions test/testpreprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class TestPreprocessor : public TestFixture {
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);
Expand Down Expand Up @@ -1927,6 +1928,12 @@ class TestPreprocessor : public TestFixture {
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"
Expand Down

0 comments on commit 40887ca

Please sign in to comment.