Fix #1076 (Invalid number of character ({). Can't process file.)

http://sourceforge.net/apps/trac/cppcheck/ticket/1076
This commit is contained in:
Reijo Tomperi 2009-12-15 00:06:05 +02:00
parent 0c13f9ba5c
commit 30f789a168
2 changed files with 33 additions and 2 deletions

View File

@ -1884,8 +1884,31 @@ std::string Preprocessor::expandMacros(const std::string &code, std::string file
if (macro->variadic() || macro->nopar() || !macro->params().empty())
++pos2;
limits[macro] = line.length() - pos2;
// When A is expanded in the following code, the old limit for the
// B needs to deleted, or it will not allow us to expand B,
// which is inside the A.
// #define B(x) (
// #define A() B(xx)
// B(1) A() ) )
unsigned int macroEnd = line.length() - (pos1 + macrocode.length());
for (std::map<const PreprocessorMacro *, unsigned int>::iterator iter = limits.begin();
iter != limits.end();)
{
if (macroEnd < iter->second)
{
// We have gone past this limit, so just delete it
limits.erase(iter++);
}
else
{
// We need to adjust this limit, because the
// length of line will be changed
iter->second += macrocode.length() - (pos2 - pos1);
++iter;
}
}
limits[macro] = macroEnd;
line.erase(pos1, pos2 - pos1);
line.insert(pos1, macrocode);
pos = pos1;

View File

@ -1135,7 +1135,15 @@ private:
"#define A(name) void foo##name() { do { B(1, 2); }\n"
"A(0)\n"
"A(1)\n";
TODO_ASSERT_EQUALS("\n\nvoid foo0(){do{}while(0);}\nvoid foo1(){do{}while(0);}\n", OurPreprocessor::expandMacros(filedata));
ASSERT_EQUALS("\n\nvoid foo0(){do{}while(0);}\nvoid foo1(){do{}while(0);}\n", OurPreprocessor::expandMacros(filedata));
}
{
const char filedata[] =
"#define B(x) (\n"
"#define A() B(xx)\n"
"B(1) A() ) )\n";
ASSERT_EQUALS("\n\n( ( ) )\n", OurPreprocessor::expandMacros(filedata));
}
}