diff --git a/lib/cmock_header_parser.rb b/lib/cmock_header_parser.rb index c87ac8a..472ab92 100644 --- a/lib/cmock_header_parser.rb +++ b/lib/cmock_header_parser.rb @@ -93,6 +93,12 @@ class CMockHeaderParser "#{functype} #{$2.strip}(#{$3});" 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 @@ -101,7 +107,7 @@ class CMockHeaderParser source.gsub!(/\s+/, ' ') # remove remaining extra white space #split lines on semicolons and remove things that are obviously not what we are looking for - src_lines = source.split(/\s*;\s*/) + src_lines = source.split(/\s*;\s*/).uniq src_lines.delete_if {|line| line.strip.length == 0} # remove blank lines src_lines.delete_if {|line| !(line =~ /[\w\s\*]+\(+\s*\*[\*\s]*[\w\s]+(?:\[[\w\s]*\]\s*)+\)+\s*\((?:[\w\s\*]*,?)*\s*\)/).nil?} #remove function pointer arrays if (@treat_externs == :include) @@ -109,8 +115,6 @@ class CMockHeaderParser else src_lines.delete_if {|line| !(line =~ /(?:^|\s+)(?:extern|inline)\s+/).nil?} # remove inline and extern functions end - src_lines.delete_if {|line| !(line =~ /\{/).nil? } # remove lines with opening braces { because this isn't a declaration, it's a definition! - src_lines.map!{|line| line.gsub(/.*\}/,'')} #remove braces left at the beginning of lines src_lines.delete_if {|line| line.empty? } #drop empty lines end diff --git a/test/unit/cmock_header_parser_test.rb b/test/unit/cmock_header_parser_test.rb index 0c60e47..ccde240 100644 --- a/test/unit/cmock_header_parser_test.rb +++ b/test/unit/cmock_header_parser_test.rb @@ -294,15 +294,18 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do "uint32 func_with_decl_a(unsigned int);\n" + "uint32 func_with_decl_a(unsigned int a) { return a; }\n" + "uint32 func_with_decl_b(unsigned int);\n" + - "uint32 func_with_decl_b(unsigned int)\n" + + "uint32 func_with_decl_b(unsigned int a)\n" + "{\n" + - " bar(unsigned int);\n" + + " bar((unsigned int) a);\n" + + " stripme(a);\n" + "}\n" expected = [ "uint32 func_with_decl_a(unsigned int)", - "uint32 func_with_decl_b(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}) @@ -332,6 +335,30 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do end end + it "remove a fully defined inline function that is multiple lines" do + source = + "inline void bar(unsigned int a)\n" + + "{" + + " bananas = a;\n" + + " grapes = a;\n" + + " apples(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" +