Handle inline function declarations when mocking inline functions

When a inline function was declared in a file, we would find the
declaration and remove the function body. However, since it is a
declaration, there is NO function body, so we were deleting a random
piece of code that was between square brackets in the file.

To properly handle this, we have to detect if we are dealing with a
function declaration or a function definition.
If we are dealing with a function declaration, a semicolon
will come BEFORE the first square bracket.
If we are dealing with a function definition, a square bracket will
come BEFORE the first semicolon (the first semicolon will be in the
inline function body, so between the square brackets).
So we determine the location of the first semicolon and the first
square bracket after the function name and apply the logic described
above to handle function declarations.

If we are dealing with a function declaration, we don't do anything,
we just move to the next match.
This will result in redeclarations of the inline function, but this is
allowed in C and I'd rather not touch the file anymore than necessary.
This commit is contained in:
laurens
2020-01-14 22:16:32 +01:00
committed by laurensmiers
parent 972814622f
commit 550e141c59
2 changed files with 17 additions and 12 deletions
+7 -12
View File
@@ -2083,30 +2083,25 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do
assert_equal(expected, @parser.parse("module", source)[:functions])
end
it "Transform inline functions takes user provided patterns into account" do
it "Transform inline functions does not touch inline function declarations" do
source =
"static inline int dummy_func_decl(int a, char b, float c);\n" + # First declaration user pattern
"static inline int staticinlinefunc(struct my_struct *s)\n" + # 'normal' inline pattern
"{\n" +
" return s->a;\n" +
" return dummy_func_decl(1, 1, 1);\n" +
"}\n" +
"static __inline__ int dummy_func_2(int a, char b, float c) {\n" + # First user pattern
" c += 3.14;\n" +
" b -= 32;\n" +
" return a + (int)(b) + (int)c;\n" +
"}\n" +
"static __inline__ __attribute__ ((always_inline)) uint16_t attributealwaysinlinefuncname(void) {\n" + # Second user pattern
" return (uint16_t)(42);\n" +
"static inline int dummy_func_decl(int a, char b, float c) {\n" + # Second user pattern
" return 42;\n" +
"}\n" +
"\n"
expected =
"int dummy_func_decl(int a, char b, float c);\n" +
"int staticinlinefunc(struct my_struct *s);\n" +
"int dummy_func_2(int a, char b, float c);\n" +
"uint16_t attributealwaysinlinefuncname(void);\n" +
"int dummy_func_decl(int a, char b, float c);\n" +
"\n"
@parser.treat_inlines = :include
@parser.inline_function_patterns = ['static __inline__ __attribute__ \(\(always_inline\)\)', 'static __inline__', 'static inline']
assert_equal(expected, @parser.transform_inline_functions(source))
end