From 3caf511b8f5e266787b93530ae2c16542716c93f Mon Sep 17 00:00:00 2001 From: alufers Date: Wed, 26 Apr 2023 14:16:41 +0200 Subject: [PATCH] fix: Don't smush macros which have an escaped empty line at the end nanopb has a habit of generating macros which end with an escaped empty line (for example for messages which are empty). They look like this: #define some_msg_t_FIELDLIST(X, a) \ #define some_msg_t_CALLBACK NULL This caused CMock to strip all of the newlines after the backslash instead of only one, which the backslash was escaping. This in turn caused both the macros to be in one line, causing a compile error in the generated mock. --- lib/cmock_header_parser.rb | 2 +- test/unit/cmock_header_parser_test.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/cmock_header_parser.rb b/lib/cmock_header_parser.rb index 491e467..1522434 100644 --- a/lib/cmock_header_parser.rb +++ b/lib/cmock_header_parser.rb @@ -137,7 +137,7 @@ class CMockHeaderParser # If the user uses a macro to declare an inline function, # smushing the macros makes it easier to recognize them as a macro and if required, # remove them later on in this function - source.gsub!(/\s*\\\s*/m, ' ') + source.gsub!(/\s*\\(\n|\s*)/m, ' ') # Just looking for static|inline in the gsub is a bit too aggressive (functions that are named like this, ...), so we try to be a bit smarter # Instead, look for an inline pattern (f.e. "static inline") and parse it. diff --git a/test/unit/cmock_header_parser_test.rb b/test/unit/cmock_header_parser_test.rb index 3437de3..43832ff 100644 --- a/test/unit/cmock_header_parser_test.rb +++ b/test/unit/cmock_header_parser_test.rb @@ -2867,5 +2867,19 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do assert_equal(expected, @parser.transform_inline_functions(source)) end + it "Transform macros with escapes of empty lines" do + source = + "#define some_msg_t_FIELDLIST(X, a) \\\n" + + "\n" + + "#define some_msg_t_CALLBACK NULL\n" + + expected = + "#define some_msg_t_FIELDLIST(X, a) \n" + + "#define some_msg_t_CALLBACK NULL\n" + + @parser.treat_inlines = :include + assert_equal(expected, @parser.transform_inline_functions(source)) + end + end