From cc9abe8e182b8c0fc6bd77b2b75d1ac4e0bfbf81 Mon Sep 17 00:00:00 2001 From: mvandervoord Date: Sun, 19 Sep 2010 02:17:29 +0000 Subject: [PATCH] - added support for stripping out things like gcc __attributes__ - moved targets into a subdirectory called targets, like unity git-svn-id: http://cmock.svn.sourceforge.net/svnroot/cmock/trunk@188 bf332499-1b4d-0410-844d-d2d48d5cc64c --- lib/cmock_config.rb | 1 + lib/cmock_header_parser.rb | 4 ++- rakefile_helper.rb | 2 +- gcc.yml => targets/gcc.yml | 0 iar_v4.yml => targets/iar_arm_v4.yml | 0 iar_v5.yml => targets/iar_arm_v5.yml | 0 test/unit/cmock_header_parser_test.rb | 45 +++++++++++++++++++++++++++ 7 files changed, 50 insertions(+), 2 deletions(-) rename gcc.yml => targets/gcc.yml (100%) rename iar_v4.yml => targets/iar_arm_v4.yml (100%) rename iar_v5.yml => targets/iar_arm_v5.yml (100%) diff --git a/lib/cmock_config.rb b/lib/cmock_config.rb index 9332e05..c196337 100644 --- a/lib/cmock_config.rb +++ b/lib/cmock_config.rb @@ -13,6 +13,7 @@ class CMockConfig :mock_prefix => 'Mock', :plugins => [], :includes => [], + :strippables => ['(?:__attribute__\s*\(+.*?\)+)'], :attributes => ['__ramfunc', '__irq', '__fiq', 'register', 'extern'], :enforce_strict_ordering => false, :unity_helper => false, diff --git a/lib/cmock_header_parser.rb b/lib/cmock_header_parser.rb index 6475f0a..f18dcf5 100644 --- a/lib/cmock_header_parser.rb +++ b/lib/cmock_header_parser.rb @@ -10,6 +10,7 @@ class CMockHeaderParser def initialize(cfg) @funcs = [] + @c_strippables = cfg.strippables @c_attributes = (['const'] + cfg.attributes).uniq @treat_as_void = (['void'] + cfg.treat_as_void).uniq @declaration_parse_matcher = /([\d\w\s\*\(\),\[\]]+??)\(([\d\w\s\*\(\),\.\[\]]*)\)$/m @@ -71,9 +72,10 @@ class CMockHeaderParser # forward declared structs are removed before struct definitions so they don't mess up real thing later. we leave structs keywords in function prototypes source.gsub!(/^[\w\s]*struct[^;\{\}\(\)]+;/m, '') # remove forward declared structs source.gsub!(/^[\w\s]*(enum|union|struct|typepdef)[\w\s]*\{[^\}]+\}[\w\s\*\,]*;/m, '') # remove struct, union, and enum definitions and typedefs with braces - source.gsub!(/(\W)(register|auto|static|restrict)(\W)/, '\1\3') # remove problem keywords + source.gsub!(/(\W)(?:register|auto|static|restrict)(\W)/, '\1\2') # remove problem keywords source.gsub!(/\s*=\s*['"a-zA-Z0-9_\.]+\s*/, '') # remove default value statements from argument lists source.gsub!(/^(?:[\w\s]*\W)?typedef\W.*/, '') # remove typedef statements + source.gsub!(/(^|\W+)(?:#{@c_strippables.join('|')})(?=$|\W+)/,'\1') unless @c_strippables.empty? # remove known attributes slated to be stripped #scan for functions which return function pointers, because they are a pain source.gsub!(/([\w\s\*]+)\(*\(\s*\*([\w\s\*]+)\s*\(([\w\s\*,]*)\)\)\s*\(([\w\s\*,]*)\)\)*/) do |m| diff --git a/rakefile_helper.rb b/rakefile_helper.rb index 0ff9d95..3ab67ea 100644 --- a/rakefile_helper.rb +++ b/rakefile_helper.rb @@ -21,7 +21,7 @@ module RakefileHelpers def load_configuration(config_file) $cfg_file = config_file - $cfg = YAML.load(File.read($cfg_file)) + $cfg = YAML.load(File.read('targets/' + $cfg_file)) $colour_output = false unless $cfg['colour'] end diff --git a/gcc.yml b/targets/gcc.yml similarity index 100% rename from gcc.yml rename to targets/gcc.yml diff --git a/iar_v4.yml b/targets/iar_arm_v4.yml similarity index 100% rename from iar_v4.yml rename to targets/iar_arm_v4.yml diff --git a/iar_v5.yml b/targets/iar_arm_v5.yml similarity index 100% rename from iar_v5.yml rename to targets/iar_arm_v5.yml diff --git a/test/unit/cmock_header_parser_test.rb b/test/unit/cmock_header_parser_test.rb index ac91d44..a7906b8 100644 --- a/test/unit/cmock_header_parser_test.rb +++ b/test/unit/cmock_header_parser_test.rb @@ -14,6 +14,7 @@ class CMockHeaderParserTest < Test::Unit::TestCase def setup create_mocks :config @test_name = 'test_file.h' + @config.expect.strippables.returns(['(?:__attribute__\s*\(+.*?\)+)']) @config.expect.attributes.returns(['__ramfunc', 'funky_attrib']) @config.expect.treat_as_void.returns(['MY_FUNKY_VOID']) @config.expect.treat_as.returns({ "BANJOS" => "INT", "TUBAS" => "HEX16"} ) @@ -1121,5 +1122,49 @@ class CMockHeaderParserTest < Test::Unit::TestCase }] assert_equal(expected, @parser.parse("module", source)[:functions]) end + + should "extract functions with strippable confusing junk like gcc attributes" do + source = "int LaverneAndShirley(int Lenny, int Squiggy) __attribute__((weak)) __attribute__ ((deprecated));\n" + expected = [{ :var_arg=>nil, + :return=> { :type => "int", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :str => "int cmock_to_return", + :void? => false + }, + :name=>"LaverneAndShirley", + :modifier=>"", + :contains_ptr? => false, + :args=>[ {:type=>"int", :name=>"Lenny", :ptr? => false, :const? => false}, + {:type=>"int", :name=>"Squiggy", :ptr? => false, :const? => false} + ], + :args_string=>"int Lenny, int Squiggy", + :args_call=>"Lenny, Squiggy" + }] + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + should "extract functions with strippable confusing junk like gcc attributes with parenthesis" do + source = "int TheCosbyShow(int Cliff, int Claire) __attribute__((weak, alias (\"__f\"));\n" + expected = [{ :var_arg=>nil, + :return=> { :type => "int", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :str => "int cmock_to_return", + :void? => false + }, + :name=>"TheCosbyShow", + :modifier=>"", + :contains_ptr? => false, + :args=>[ {:type=>"int", :name=>"Cliff", :ptr? => false, :const? => false}, + {:type=>"int", :name=>"Claire", :ptr? => false, :const? => false} + ], + :args_string=>"int Cliff, int Claire", + :args_call=>"Cliff, Claire" + }] + assert_equal(expected, @parser.parse("module", source)[:functions]) + end end