From a15b15264be2365741ab1377414cd5397338d683 Mon Sep 17 00:00:00 2001 From: glank Date: Fri, 24 Jul 2026 15:17:43 +0200 Subject: [PATCH 1/2] Better no eof newline warning --- main.cpp | 7 ------- simplecpp.cpp | 4 ++-- test.cpp | 18 +++++++++++++----- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/main.cpp b/main.cpp index c1621544..03b52a93 100644 --- a/main.cpp +++ b/main.cpp @@ -277,14 +277,7 @@ int main(int argc, char **argv) break; case simplecpp::Output::PORTABILITY_BACKSLASH: case simplecpp::Output::PORTABILITY_LINE_DIRECTIVE: - std::cerr << "portability: "; - break; case simplecpp::Output::PORTABILITY_NO_EOF_NEWLINE: - if (simplecpp::getCStd(dui.std) == simplecpp::CUnknown) { - // Only UB for c code, suppress for c++ code - // If no standard is specified then prefer to have a false negative - continue; - } std::cerr << "portability: "; break; case simplecpp::Output::UNHANDLED_CHAR_ERROR: diff --git a/simplecpp.cpp b/simplecpp.cpp index 48243b6e..67c2bd66 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1022,11 +1022,11 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename, location.adjust(currentToken); } - if (!trailing_nl && outputList) { + if ((cstd != CUnknown || (cstd == CUnknown && cppstd == CPPUnknown)) && !trailing_nl && outputList) { Output err{ Output::PORTABILITY_NO_EOF_NEWLINE, location, - "No newline at end of file." + "No newline at end of file is undefined behavior in C." }; outputList->emplace_back(std::move(err)); } diff --git a/test.cpp b/test.cpp index 6b482c69..a20ef6de 100644 --- a/test.cpp +++ b/test.cpp @@ -3480,13 +3480,21 @@ static void readfile_no_eof_newline() const char code[] = "\\\n"; simplecpp::OutputList outputList; readfile(code, sizeof(code)-1, {}, &outputList); - ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file.\n", toString(outputList)); + ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file is undefined behavior in C.\n", toString(outputList)); + } + { + const char code[] = "\\\n"; + simplecpp::DUI dui; + dui.std = "c++03"; + simplecpp::OutputList outputList; + readfile(code, sizeof(code)-1, dui, &outputList); + ASSERT_EQUALS("", toString(outputList)); } { const char code[] = "#define A"; simplecpp::OutputList outputList; readfile(code, sizeof(code)-1, {}, &outputList); - ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file.\n", toString(outputList)); + ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file is undefined behavior in C.\n", toString(outputList)); } { const char code[] = "#define A\n"; @@ -3498,13 +3506,13 @@ static void readfile_no_eof_newline() const char code[] = "#define A\\"; simplecpp::OutputList outputList; readfile(code, sizeof(code)-1, {}, &outputList); - ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file.\n", toString(outputList)); + ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file is undefined behavior in C.\n", toString(outputList)); } { const char code[] = "// comment"; simplecpp::OutputList outputList; readfile(code, sizeof(code)-1, {}, &outputList); - ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file.\n", toString(outputList)); + ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file is undefined behavior in C.\n", toString(outputList)); } { const char code[] = "// comment\n"; @@ -3516,7 +3524,7 @@ static void readfile_no_eof_newline() const char code[] = "/* comment \n comment */"; simplecpp::OutputList outputList; readfile(code, sizeof(code)-1, {}, &outputList); - ASSERT_EQUALS("file0,2,portability_no_eof_newline,No newline at end of file.\n", toString(outputList)); + ASSERT_EQUALS("file0,2,portability_no_eof_newline,No newline at end of file is undefined behavior in C.\n", toString(outputList)); } { const char code[] = "/* comment \n comment */\n"; From c635e40f4f822e676494f6df6409a14f7f665cae Mon Sep 17 00:00:00 2001 From: glankk Date: Fri, 24 Jul 2026 18:42:33 +0200 Subject: [PATCH 2/2] Update simplecpp.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel Marjamäki --- simplecpp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index 67c2bd66..f313c819 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1022,7 +1022,7 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename, location.adjust(currentToken); } - if ((cstd != CUnknown || (cstd == CUnknown && cppstd == CPPUnknown)) && !trailing_nl && outputList) { + if ((cstd != CUnknown || cppstd == CPPUnknown) && !trailing_nl && outputList) { Output err{ Output::PORTABILITY_NO_EOF_NEWLINE, location,