From 4ced2e471025a5be3bd4c9f98d30d3c16f1a8fea Mon Sep 17 00:00:00 2001 From: Mark VanderVoord Date: Thu, 17 Mar 2016 07:52:19 -0400 Subject: [PATCH] Strip out nested pairs of braces in order to protect against function definitions in the header files (inline or otherwise). --- lib/cmock_header_parser.rb | 7 ++-- test/unit/cmock_header_parser_test.rb | 56 +++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/lib/cmock_header_parser.rb b/lib/cmock_header_parser.rb index eb36f2e..0f08bbf 100644 --- a/lib/cmock_header_parser.rb +++ b/lib/cmock_header_parser.rb @@ -93,12 +93,13 @@ class CMockHeaderParser "#{functype} #{$2.strip}(#{$3});" end + # remove nested pairs of braces because no function declarations will be inside of them (leave outer pair for function definition detection) + while source.gsub!(/\{[^\{\}]*\{[^\{\}]*\}[^\{\}]*\}/m, '{ }') + end + # remove function definitions by stripping off the arguments right now source.gsub!(/\([^\)]*\)\s*\{[^\}]*\}/m, ";") - # remove pairs of braces because no function declarations will be inside of them - #source.gsub!(/\{[^\}]*\}/m, '') - #drop extra white space to make the rest go faster source.gsub!(/^\s+/, '') # remove extra white space from beginning of line source.gsub!(/\s+$/, '') # remove extra white space from end of line diff --git a/test/unit/cmock_header_parser_test.rb b/test/unit/cmock_header_parser_test.rb index 9949762..5dc4483 100644 --- a/test/unit/cmock_header_parser_test.rb +++ b/test/unit/cmock_header_parser_test.rb @@ -311,6 +311,34 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) end + it "remove function definitions with nested braces but keep function declarations" do + source = + "uint32 func_with_decl_a(unsigned int);\n" + + "uint32 func_with_decl_a(unsigned int a) {\n" + + " while (stuff) {\n" + + " not_a_definition1(void);\n" + + " }\n" + + " not_a_definition2(blah, bleh);\n" + + " return a;\n" + + "}\n" + + "uint32 func_with_decl_b(unsigned int);\n" + + "uint32 func_with_decl_b(unsigned int a)\n" + + "{\n" + + " bar((unsigned int) a);\n" + + " stripme(a);\n" + + "}\n" + + expected = + [ + "uint32 func_with_decl_a(unsigned int)", + "uint32 func_with_decl_a", #okay. it's not going to be interpretted as another function + "uint32 func_with_decl_b(unsigned int)", + "uint32 func_with_decl_b", #okay. it's not going to be interpretted as another function + ] + + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + end + it "remove a fully defined inline function" do source = "inline void foo(unsigned int a) { oranges = a; }\n" + @@ -359,6 +387,34 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do end end + it "remove a fully defined inline function that contains nested braces" do + source = + "inline void bar(unsigned int a)\n" + + "{" + + " apples(bananas, grapes);\n" + + " if (bananas == a)\n" + + " {\n" + + " oranges(a);\n" + + " grapes = a;\n" + + " }\n" + + " grapefruit(bananas, grapes);\n" + + "}" + + # ensure it's expected type of exception + assert_raises RuntimeError do + @parser.parse("module", source) + end + + assert_equal([], @parser.funcs) + + # verify exception message + begin + @parser.parse("module", source) + rescue RuntimeError => e + assert_equal("ERROR: No function prototypes found!", e.message) + end + end + it "remove just inline functions if externs to be included" do source = " extern uint32 foobar(unsigned int);\n" +