Fix ticket #181 (#pragma causes wrong line numbers)

http://apps.sourceforge.net/trac/cppcheck/ticket/181
This commit is contained in:
Reijo Tomperi 2009-03-15 14:23:12 +02:00
parent c385b3e045
commit bb3316a45b
2 changed files with 26 additions and 5 deletions

View File

@ -449,17 +449,20 @@ std::string Preprocessor::getcode(const std::string &filedata, std::string cfg)
} }
if (line.find("#file \"") == 0 || if (line.find("#file \"") == 0 ||
line.find("#endfile") == 0) line.find("#endfile") == 0 ||
line.find("#define") == 0)
{ {
// We must not remove #file tags or line numbers // We must not remove #file tags or line numbers
// are corrupted. File tags are removed by the tokenizer. // are corrupted. File tags are removed by the tokenizer.
} }
else if (!match || else if (!match ||
line.find("#if") == 0 || line[0] == '#')
line.find("#else") == 0 || {
line.find("#elif") == 0 || // Remove #if, #else, #pragma etc, leaving only
line.find("#endif") == 0) // #define, #file and #endfile. and also lines
// which are not part of this configuration.
line = ""; line = "";
}
ret << line << "\n"; ret << line << "\n";
} }

View File

@ -108,6 +108,7 @@ private:
TEST_CASE(stringify); TEST_CASE(stringify);
TEST_CASE(ifdefwithfile); TEST_CASE(ifdefwithfile);
TEST_CASE(pragma);
} }
@ -750,6 +751,23 @@ private:
ASSERT_EQUALS("\n\"abc\"", actual); ASSERT_EQUALS("\n\"abc\"", actual);
} }
void pragma()
{
const char filedata[] = "#pragma once\n"
"void f()\n"
"{\n"
"}\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Preprocessor preprocessor;
preprocessor.preprocess(istr, actual, "file.c");
// Compare results..
ASSERT_EQUALS(1, static_cast<unsigned int>(actual.size()));
ASSERT_EQUALS("\nvoid f()\n{\n}\n", actual[""]);
}
}; };
REGISTER_TEST(TestPreprocessor) REGISTER_TEST(TestPreprocessor)