Skip to content

Commit a15b152

Browse files
committed
Better no eof newline warning
1 parent df97c30 commit a15b152

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
@@ -277,14 +277,7 @@ int main(int argc, char **argv)
277277
break;
278278
case simplecpp::Output::PORTABILITY_BACKSLASH:
279279
case simplecpp::Output::PORTABILITY_LINE_DIRECTIVE:
280-
std::cerr << "portability: ";
281-
break;
282280
case simplecpp::Output::PORTABILITY_NO_EOF_NEWLINE:
283-
if (simplecpp::getCStd(dui.std) == simplecpp::CUnknown) {
284-
// Only UB for c code, suppress for c++ code
285-
// If no standard is specified then prefer to have a false negative
286-
continue;
287-
}
288281
std::cerr << "portability: ";
289282
break;
290283
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 || (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
@@ -3480,13 +3480,21 @@ static void readfile_no_eof_newline()
34803480
const char code[] = "\\\n";
34813481
simplecpp::OutputList outputList;
34823482
readfile(code, sizeof(code)-1, {}, &outputList);
3483-
ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file.\n", toString(outputList));
3483+
ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file is undefined behavior in C.\n", toString(outputList));
3484+
}
3485+
{
3486+
const char code[] = "\\\n";
3487+
simplecpp::DUI dui;
3488+
dui.std = "c++03";
3489+
simplecpp::OutputList outputList;
3490+
readfile(code, sizeof(code)-1, dui, &outputList);
3491+
ASSERT_EQUALS("", toString(outputList));
34843492
}
34853493
{
34863494
const char code[] = "#define A";
34873495
simplecpp::OutputList outputList;
34883496
readfile(code, sizeof(code)-1, {}, &outputList);
3489-
ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file.\n", toString(outputList));
3497+
ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file is undefined behavior in C.\n", toString(outputList));
34903498
}
34913499
{
34923500
const char code[] = "#define A\n";
@@ -3498,13 +3506,13 @@ static void readfile_no_eof_newline()
34983506
const char code[] = "#define A\\";
34993507
simplecpp::OutputList outputList;
35003508
readfile(code, sizeof(code)-1, {}, &outputList);
3501-
ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file.\n", toString(outputList));
3509+
ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file is undefined behavior in C.\n", toString(outputList));
35023510
}
35033511
{
35043512
const char code[] = "// comment";
35053513
simplecpp::OutputList outputList;
35063514
readfile(code, sizeof(code)-1, {}, &outputList);
3507-
ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file.\n", toString(outputList));
3515+
ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file is undefined behavior in C.\n", toString(outputList));
35083516
}
35093517
{
35103518
const char code[] = "// comment\n";
@@ -3516,7 +3524,7 @@ static void readfile_no_eof_newline()
35163524
const char code[] = "/* comment \n comment */";
35173525
simplecpp::OutputList outputList;
35183526
readfile(code, sizeof(code)-1, {}, &outputList);
3519-
ASSERT_EQUALS("file0,2,portability_no_eof_newline,No newline at end of file.\n", toString(outputList));
3527+
ASSERT_EQUALS("file0,2,portability_no_eof_newline,No newline at end of file is undefined behavior in C.\n", toString(outputList));
35203528
}
35213529
{
35223530
const char code[] = "/* comment \n comment */\n";

0 commit comments

Comments
 (0)