Ticket #4703: Trim macro parameters.

This commit is contained in:
Simon Martin 2013-08-10 23:28:02 +02:00
parent 56d8073d0f
commit 40887ca99c
2 changed files with 24 additions and 2 deletions

View File

@ -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 = "";
}

View File

@ -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"