Skip to content

Commit 5d81c8a

Browse files
glankkdanmar
andauthored
Improve no_eof_newline warning (#692)
Co-authored-by: Daniel Marjamäki <daniel.marjamaki@gmail.com>
1 parent 1537e9e commit 5d81c8a

3 files changed

Lines changed: 15 additions & 14 deletions

File tree

main.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,7 @@ int main(int argc, char **argv)
278278
break;
279279
case simplecpp::Output::PORTABILITY_BACKSLASH:
280280
case simplecpp::Output::PORTABILITY_LINE_DIRECTIVE:
281-
std::cerr << "portability: ";
282-
break;
283281
case simplecpp::Output::PORTABILITY_NO_EOF_NEWLINE:
284-
if (simplecpp::getCStd(dui.std) == simplecpp::CUnknown) {
285-
// Only UB for c code, suppress for c++ code
286-
// If no standard is specified then prefer to have a false negative
287-
continue;
288-
}
289282
std::cerr << "portability: ";
290283
break;
291284
case simplecpp::Output::UNHANDLED_CHAR_ERROR:

simplecpp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,11 +1022,11 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
10221022
location.adjust(currentToken);
10231023
}
10241024

1025-
if (!trailing_nl && outputList) {
1025+
if ((cstd != CUnknown || cppstd == CPPUnknown) && !trailing_nl && outputList) {
10261026
Output err{
10271027
Output::PORTABILITY_NO_EOF_NEWLINE,
10281028
location,
1029-
"No newline at end of file."
1029+
"No newline at end of file is undefined behavior in C."
10301030
};
10311031
outputList->emplace_back(std::move(err));
10321032
}

test.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3508,13 +3508,21 @@ static void readfile_no_eof_newline()
35083508
const char code[] = "\\\n";
35093509
simplecpp::OutputList outputList;
35103510
readfile(code, sizeof(code)-1, {}, &outputList);
3511-
ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file.\n", toString(outputList));
3511+
ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file is undefined behavior in C.\n", toString(outputList));
3512+
}
3513+
{
3514+
const char code[] = "\\\n";
3515+
simplecpp::DUI dui;
3516+
dui.std = "c++03";
3517+
simplecpp::OutputList outputList;
3518+
readfile(code, sizeof(code)-1, dui, &outputList);
3519+
ASSERT_EQUALS("", toString(outputList));
35123520
}
35133521
{
35143522
const char code[] = "#define A";
35153523
simplecpp::OutputList outputList;
35163524
readfile(code, sizeof(code)-1, {}, &outputList);
3517-
ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file.\n", toString(outputList));
3525+
ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file is undefined behavior in C.\n", toString(outputList));
35183526
}
35193527
{
35203528
const char code[] = "#define A\n";
@@ -3526,13 +3534,13 @@ static void readfile_no_eof_newline()
35263534
const char code[] = "#define A\\";
35273535
simplecpp::OutputList outputList;
35283536
readfile(code, sizeof(code)-1, {}, &outputList);
3529-
ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file.\n", toString(outputList));
3537+
ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file is undefined behavior in C.\n", toString(outputList));
35303538
}
35313539
{
35323540
const char code[] = "// comment";
35333541
simplecpp::OutputList outputList;
35343542
readfile(code, sizeof(code)-1, {}, &outputList);
3535-
ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file.\n", toString(outputList));
3543+
ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file is undefined behavior in C.\n", toString(outputList));
35363544
}
35373545
{
35383546
const char code[] = "// comment\n";
@@ -3544,7 +3552,7 @@ static void readfile_no_eof_newline()
35443552
const char code[] = "/* comment \n comment */";
35453553
simplecpp::OutputList outputList;
35463554
readfile(code, sizeof(code)-1, {}, &outputList);
3547-
ASSERT_EQUALS("file0,2,portability_no_eof_newline,No newline at end of file.\n", toString(outputList));
3555+
ASSERT_EQUALS("file0,2,portability_no_eof_newline,No newline at end of file is undefined behavior in C.\n", toString(outputList));
35483556
}
35493557
{
35503558
const char code[] = "/* comment \n comment */\n";

0 commit comments

Comments
 (0)