Skip to content

Commit 40887ca

Browse files
committed
Ticket danmar#4703: Trim macro parameters.
1 parent 56d8073 commit 40887ca

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/preprocessor.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,6 +2275,21 @@ static void skipstring(const std::string &line, std::string::size_type &pos)
22752275
}
22762276
}
22772277

2278+
/**
2279+
* Remove heading and trailing whitespaces from the input parameter.
2280+
* #param s The string to trim.
2281+
*/
2282+
static std::string trim(const std::string& s)
2283+
{
2284+
std::string::size_type beg = s.find_first_not_of(" \t");
2285+
if (beg == std::string::npos)
2286+
return s;
2287+
std::string::size_type end = s.find_last_not_of(" \t");
2288+
if (end == std::string::npos)
2289+
return s.substr(beg);
2290+
return s.substr(beg, end - beg + 1);
2291+
}
2292+
22782293
/**
22792294
* @brief get parameters from code. For example 'foo(1,2)' => '1','2'
22802295
* @param line in: The code
@@ -2319,7 +2334,7 @@ static void getparams(const std::string &line,
23192334
--parlevel;
23202335
if (parlevel <= 0) {
23212336
endFound = true;
2322-
params.push_back(par);
2337+
params.push_back(trim(par));
23232338
break;
23242339
}
23252340
}
@@ -2342,7 +2357,7 @@ static void getparams(const std::string &line,
23422357

23432358
// new parameter
23442359
if (parlevel == 1 && line[pos] == ',') {
2345-
params.push_back(par);
2360+
params.push_back(trim(par));
23462361
par = "";
23472362
}
23482363

test/testpreprocessor.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class TestPreprocessor : public TestFixture {
178178
TEST_CASE(macro_simple13);
179179
TEST_CASE(macro_simple14);
180180
TEST_CASE(macro_simple15);
181+
TEST_CASE(macro_simple16); // #4703: Macro parameters not trimmed
181182
TEST_CASE(macroInMacro1);
182183
TEST_CASE(macroInMacro2);
183184
TEST_CASE(macro_mismatch);
@@ -1927,6 +1928,12 @@ class TestPreprocessor : public TestFixture {
19271928
ASSERT_EQUALS("\n$\"foo\"\n", OurPreprocessor::expandMacros(filedata));
19281929
}
19291930

1931+
void macro_simple16() { // # 4703
1932+
const char filedata[] = "#define MACRO( A, B, C ) class A##B##C##Creator {};\n"
1933+
"MACRO( B\t, U , G )";
1934+
ASSERT_EQUALS("\n$class BUGCreator{};", OurPreprocessor::expandMacros(filedata));
1935+
}
1936+
19301937
void macroInMacro1() {
19311938
{
19321939
const char filedata[] = "#define A(m) long n = m; n++;\n"

0 commit comments

Comments
 (0)