Fix #706 (false positive: invalid number of character ((), can't process file)

http://sourceforge.net/apps/trac/cppcheck/ticket/706
This commit is contained in:
Reijo Tomperi 2009-09-21 23:27:06 +03:00
parent 0fff5a23bf
commit 4d2a8608a8
2 changed files with 28 additions and 8 deletions

View File

@ -1276,19 +1276,24 @@ public:
* To avoid name collisions, we will rename macro variables by * To avoid name collisions, we will rename macro variables by
* adding _prefix in front of the name of each variable. * adding _prefix in front of the name of each variable.
* Returns the macro with converted names * Returns the macro with converted names
* @return e.g. "A(__cppcheck__x) foo(__cppcheck__x);" * @result If return value is false, this is not touched. If
* return value is true, this will contain new macro line
* (all that comes after #define) e.g.
* "A(__cppcheck__x) foo(__cppcheck__x);"
* @return true if code needs to be changed, false is no changes
* are required.
*/ */
std::string renameMacroVariables() bool renameMacroVariables(std::string &result)
{ {
// No variables // No variables
if (_params.size() == 0) if (_params.size() == 0)
return _macro; return false;
// Already renamed // Already renamed
if (_params[0].compare(0, _prefix.length(), _prefix) == 0) if (_params[0].compare(0, _prefix.length(), _prefix) == 0)
return _macro; return false;
std::string result; result = "";
result.append(_name); result.append(_name);
result.append("("); result.append("(");
std::vector<std::string> values; std::vector<std::string> values;
@ -1304,7 +1309,7 @@ public:
std::string temp; std::string temp;
this->code(values, temp); this->code(values, temp);
result.append(temp); result.append(temp);
return result; return true;
} }
const Token *tokens() const const Token *tokens() const
@ -1596,8 +1601,16 @@ std::string Preprocessor::expandMacros(std::string code, const std::string &file
startOfLine += 8; startOfLine += 8;
PreprocessorMacro tempMacro(code.substr(startOfLine, endOfLine - startOfLine)); PreprocessorMacro tempMacro(code.substr(startOfLine, endOfLine - startOfLine));
code.erase(startOfLine, endOfLine - startOfLine); std::string tempMacroCode;
code.insert(startOfLine, tempMacro.renameMacroVariables()); if (tempMacro.renameMacroVariables(tempMacroCode))
{
// Change the macro and then start again from the start
// of the line, as code has changed.
code.erase(startOfLine, endOfLine - startOfLine);
code.insert(startOfLine, tempMacroCode);
pos1 = startOfLine;
continue;
}
} }
unsigned int numberOfNewlines = 0; unsigned int numberOfNewlines = 0;

View File

@ -904,6 +904,13 @@ private:
"DBG(\"hello: %d\",3);"; "DBG(\"hello: %d\",3);";
ASSERT_EQUALS("\n\nprintf(\"hello: %d\",3);", OurPreprocessor::expandMacros(filedata)); ASSERT_EQUALS("\n\nprintf(\"hello: %d\",3);", OurPreprocessor::expandMacros(filedata));
} }
{
const char filedata[] = "#define A 4\n"
"#define B(a) a,A\n"
"B(2);";
ASSERT_EQUALS("\n\n2,4;", OurPreprocessor::expandMacros(filedata));
}
} }
void macro_mismatch() void macro_mismatch()