fix case where fall through comment precedes preprocessor line
This commit is contained in:
parent
8e839a46e8
commit
cc7e05a5b0
|
@ -326,6 +326,7 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri
|
||||||
unsigned int newlines = 0;
|
unsigned int newlines = 0;
|
||||||
std::ostringstream code;
|
std::ostringstream code;
|
||||||
unsigned char previous = 0;
|
unsigned char previous = 0;
|
||||||
|
bool inPreprocessorLine = false;
|
||||||
std::vector<std::string> suppressionIDs;
|
std::vector<std::string> suppressionIDs;
|
||||||
|
|
||||||
for (std::string::size_type i = hasbom(str) ? 3U : 0U; i < str.length(); ++i)
|
for (std::string::size_type i = hasbom(str) ? 3U : 0U; i < str.length(); ++i)
|
||||||
|
@ -370,6 +371,8 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri
|
||||||
// if there has been <backspace><newline> sequences, add extra newlines..
|
// if there has been <backspace><newline> sequences, add extra newlines..
|
||||||
if (ch == '\n')
|
if (ch == '\n')
|
||||||
{
|
{
|
||||||
|
if (previous != '\\')
|
||||||
|
inPreprocessorLine = false;
|
||||||
++lineno;
|
++lineno;
|
||||||
if (newlines > 0)
|
if (newlines > 0)
|
||||||
{
|
{
|
||||||
|
@ -449,9 +452,18 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (ch == '#' && previous == '\n')
|
||||||
|
{
|
||||||
|
code << ch;
|
||||||
|
previous = ch;
|
||||||
|
inPreprocessorLine = true;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Not whitespace and not a comment. Must be code here!
|
if (!inPreprocessorLine)
|
||||||
|
{
|
||||||
|
// Not whitespace, not a comment, and not preprocessor.
|
||||||
|
// Must be code here!
|
||||||
// Add any pending inline suppressions that have accumulated.
|
// Add any pending inline suppressions that have accumulated.
|
||||||
if (!suppressionIDs.empty())
|
if (!suppressionIDs.empty())
|
||||||
{
|
{
|
||||||
|
@ -469,6 +481,7 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri
|
||||||
}
|
}
|
||||||
suppressionIDs.clear();
|
suppressionIDs.clear();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// String or char constants..
|
// String or char constants..
|
||||||
if (ch == '\"' || ch == '\'')
|
if (ch == '\"' || ch == '\'')
|
||||||
|
|
|
@ -1414,6 +1414,20 @@ private:
|
||||||
// (no pun intended).
|
// (no pun intended).
|
||||||
TODO_ASSERT_EQUALS("",
|
TODO_ASSERT_EQUALS("",
|
||||||
"[test.cpp:11]: (warning) Switch falls through case without comment\n", errout.str());
|
"[test.cpp:11]: (warning) Switch falls through case without comment\n", errout.str());
|
||||||
|
|
||||||
|
check_preprocess_suppress(
|
||||||
|
"void foo() {\n"
|
||||||
|
" switch (a) {\n"
|
||||||
|
" case 1:\n"
|
||||||
|
"#ifndef A\n"
|
||||||
|
" g();\n"
|
||||||
|
" // fall through\n"
|
||||||
|
"#endif\n"
|
||||||
|
" case 2:\n"
|
||||||
|
" break;\n"
|
||||||
|
" }\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void selfAssignment()
|
void selfAssignment()
|
||||||
|
|
Loading…
Reference in New Issue