Merge branch 'master' of git@github.com:danmar/cppcheck

This commit is contained in:
Kimmo Varis 2009-06-19 00:11:17 +03:00
commit 1619b6cb0f
2 changed files with 44 additions and 6 deletions

View File

@ -901,6 +901,9 @@ public:
_params.push_back(tok->str()); _params.push_back(tok->str());
} }
} }
else if (Token::Match(tokens(), "%var% ( . . . )"))
_variadic = true;
} }
} }
@ -926,7 +929,36 @@ public:
bool code(const std::vector<std::string> &params2, std::string &macrocode) const bool code(const std::vector<std::string> &params2, std::string &macrocode) const
{ {
if (_params.empty()) if (_params.empty() && _variadic)
{
std::string s;
for (unsigned int i = 0; i < params2.size(); ++i)
{
if (i > 0)
s += ",";
s += params2[i];
}
macrocode = _macro.substr(1 + _macro.find(")"));
if (macrocode.empty())
return true;
std::string::size_type pos = 0;
// Remove leading spaces
if ((pos = macrocode.find_first_not_of(" ")) > 0)
macrocode.erase(0,pos);
// Remove ending newline
if ((pos = macrocode.find_first_of("\r\n")) != std::string::npos)
macrocode.erase(pos);
// Replace "__VA_ARGS__" with parameters
while ((pos = macrocode.find("__VA_ARGS__")) != std::string::npos)
{
macrocode.erase(pos, 11);
macrocode.insert(pos, s);
}
}
else if (_params.empty())
{ {
std::string::size_type pos = _macro.find(" "); std::string::size_type pos = _macro.find(" ");
if (pos == std::string::npos) if (pos == std::string::npos)
@ -1110,11 +1142,11 @@ std::string Preprocessor::expandMacros(std::string code, const std::string &file
if (code.substr(pos1, macro.name().length()) != macro.name()) if (code.substr(pos1, macro.name().length()) != macro.name())
continue; continue;
// Previous char must not be alphanumeric or '_' // Previous char must not be alphanumeric nor '_'
if (pos1 != 0 && (std::isalnum(code[pos1-1]) || code[pos1-1] == '_')) if (pos1 != 0 && (std::isalnum(code[pos1-1]) || code[pos1-1] == '_'))
continue; continue;
// The char after the macroname must not be alphanumeric or '_' // The char after the macroname must not be alphanumeric nor '_'
if (pos1 + macro.name().length() < code.length()) if (pos1 + macro.name().length() < code.length())
{ {
std::string::size_type pos2 = pos1 + macro.name().length(); std::string::size_type pos2 = pos1 + macro.name().length();
@ -1129,7 +1161,7 @@ std::string Preprocessor::expandMacros(std::string code, const std::string &file
unsigned int numberOfNewlines = 0; unsigned int numberOfNewlines = 0;
if (macro.params().size()) if (macro.variadic() || macro.params().size())
{ {
if (code[pos2] == ' ') if (code[pos2] == ' ')
pos2++; pos2++;
@ -1224,7 +1256,7 @@ std::string Preprocessor::expandMacros(std::string code, const std::string &file
const std::string macrocode(std::string(numberOfNewlines, '\n') + tempMacro); const std::string macrocode(std::string(numberOfNewlines, '\n') + tempMacro);
// Insert macro code.. // Insert macro code..
if (!macro.params().empty()) if (macro.variadic() || !macro.params().empty())
++pos2; ++pos2;
code.erase(pos1, pos2 - pos1); code.erase(pos1, pos2 - pos1);

View File

@ -114,6 +114,7 @@ private:
TEST_CASE(preprocessor_include_in_str); TEST_CASE(preprocessor_include_in_str);
TEST_CASE(fmt1); TEST_CASE(fmt1);
TEST_CASE(fmt2); TEST_CASE(fmt2);
TEST_CASE(fmt3);
TEST_CASE(multi_character_character); TEST_CASE(multi_character_character);
TEST_CASE(stringify); TEST_CASE(stringify);
@ -820,7 +821,12 @@ private:
ASSERT_EQUALS("\nprintf(\"hello\");", actual); ASSERT_EQUALS("\nprintf(\"hello\");", actual);
} }
void fmt3()
{
const char filedata[] = "#define FRED(...) { fred(__VA_ARGS__); }\n"
"FRED(123)";
ASSERT_EQUALS("\n{ fred(123); }", OurPreprocessor::expandMacros(filedata));
}