From 541e7034ad0e73b092514e888e17ba98b4facad7 Mon Sep 17 00:00:00 2001 From: laurens Date: Sat, 13 Jun 2020 12:43:57 +0200 Subject: [PATCH] Remove comments before start of inline-function-parsing --- lib/cmock_header_parser.rb | 17 +++++-- test/unit/cmock_header_parser_test.rb | 65 +++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 7 deletions(-) diff --git a/lib/cmock_header_parser.rb b/lib/cmock_header_parser.rb index 8638a04..aab9c05 100644 --- a/lib/cmock_header_parser.rb +++ b/lib/cmock_header_parser.rb @@ -61,6 +61,15 @@ class CMockHeaderParser private if $ThisIsOnlyATest.nil? ################ + # Remove C/C++ comments from a string + # +source+:: String which will have the comments removed + def remove_comments_from_source(source) + # remove comments (block and line, in three steps to ensure correct precedence) + source.gsub!(/(? 1 @@ -119,6 +128,9 @@ class CMockHeaderParser # let's clean up the encoding in case they've done anything weird with the characters we might find source = source.force_encoding('ISO-8859-1').encode('utf-8', :replace => nil) + # Comments can contain words that will trigger the parser (static|inline|) + remove_comments_from_source(source) + # smush multiline macros into single line (checking for continuation character at end of line '\') # 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, @@ -205,10 +217,7 @@ class CMockHeaderParser # smush multiline macros into single line (checking for continuation character at end of line '\') source.gsub!(/\s*\\\s*/m, ' ') - # remove comments (block and line, in three steps to ensure correct precedence) - source.gsub!(/(? 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))\n" + + "#pragma GCC diagnostic push\n" + + "#endif\n" + + "#if !defined(__clang__)\n" + + "#pragma GCC diagnostic ignored \"-Wpragmas\"\n" + + "#endif\n" + + "#pragma GCC diagnostic ignored \"-Wunknown-pragmas\"\n" + + "#pragma GCC diagnostic ignored \"-Wduplicate-decl-specifier\"\n" + + "#endif\n" + + "\n" + + "struct my_struct {\n" + + "int a;\n" + + "int b;\n" + + "int b;\n" + + "char c;\n" + + "};\n" + + "int my_function(int a);\n" + + "int my_better_function(struct my_struct *s);\n" + + "\n" + + "#endif _NOINCLUDES\n" + + assert_equal(expected, @parser.transform_inline_functions(source)) end it "Transform inline functions changes inline functions to function declarations" do @@ -2133,7 +2164,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do "#include \"cmock.h\"\n" + "#include \"YetAnotherHeader.h\"\n" + "\n" + - "/* Ignore the following warnings since we are copying code */\n" + + "\n" + # Removed comment "#if defined(__GNUC__) && !defined(__ICC) && !defined(__TMS470__)\n" + "#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))\n" + "#pragma GCC diagnostic push\n" + @@ -2714,4 +2745,32 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do assert_equal(expected, @parser.transform_inline_functions(source)) end + it "Transform inline functions will ignore comments containing static" do + source = + "typedef struct {\n" + + "const uint32_t var1;\n" + + "const uint16_t var2; // comment with the word static in it\n" + + "} struct1;\n" + + "\n" + + "typedef struct {\n" + + "const uint32_t var1;\n" + + "const uint16_t var2;\n" + + "} struct2;\n" + + expected = + "typedef struct {\n" + + "const uint32_t var1;\n" + + "const uint16_t var2; \n" + + "} struct1;\n" + + "\n" + + "typedef struct {\n" + + "const uint32_t var1;\n" + + "const uint16_t var2;\n" + + "} struct2;\n" + + @parser.treat_inlines = :include + assert_equal(expected, @parser.transform_inline_functions(source)) + end + + end