Strip out nested pairs of braces in order to protect against function definitions in the header files (inline or otherwise).

This commit is contained in:
Mark VanderVoord
2016-03-17 07:52:19 -04:00
parent a20b14a575
commit 4ced2e4710
2 changed files with 60 additions and 3 deletions
+4 -3
View File
@@ -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
+56
View File
@@ -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" +