From bb3316a45bf3d5e58b4079b22ae22f882b493dfa Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Sun, 15 Mar 2009 14:23:12 +0200 Subject: [PATCH] Fix ticket #181 (#pragma causes wrong line numbers) http://apps.sourceforge.net/trac/cppcheck/ticket/181 --- src/preprocessor.cpp | 13 ++++++++----- test/testpreprocessor.cpp | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/preprocessor.cpp b/src/preprocessor.cpp index 6a9a5cbe4..c26d132d6 100644 --- a/src/preprocessor.cpp +++ b/src/preprocessor.cpp @@ -449,17 +449,20 @@ std::string Preprocessor::getcode(const std::string &filedata, std::string cfg) } 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 // are corrupted. File tags are removed by the tokenizer. } else if (!match || - line.find("#if") == 0 || - line.find("#else") == 0 || - line.find("#elif") == 0 || - line.find("#endif") == 0) + line[0] == '#') + { + // Remove #if, #else, #pragma etc, leaving only + // #define, #file and #endfile. and also lines + // which are not part of this configuration. line = ""; + } ret << line << "\n"; } diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 6be03d30f..362dc9c42 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -108,6 +108,7 @@ private: TEST_CASE(stringify); TEST_CASE(ifdefwithfile); + TEST_CASE(pragma); } @@ -750,6 +751,23 @@ private: 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 actual; + Preprocessor preprocessor; + preprocessor.preprocess(istr, actual, "file.c"); + + // Compare results.. + ASSERT_EQUALS(1, static_cast(actual.size())); + ASSERT_EQUALS("\nvoid f()\n{\n}\n", actual[""]); + } }; REGISTER_TEST(TestPreprocessor)