Fix deleting of next line in user supplied inline function pattern

If we have a 'empty' macro, f.e.:
\#if <something>
\#define MY_LIBRARY_INLINE
\#endif
and using the user pattern 'MY_LIBRARY_INLINE', we would get the
following effect:
match = 'MY_LIBRARY_INLINE\n' <--- NOTE THAT THE NEWLINE IS PART OF THE MATCH
post-match = '#endif\n' <--- NO BEGINNING NEWLINE SINCE IT IS PART OF
THE MATCH ABOVE

And lead to the new header:
\#if <something>
Which gives a compilation error since the preprocessor-if is not
terminated properly.

The root cause is that we add a any-whitespace-character regex to the
user regex.
This any-whitespace-character regex was added only to cleanup the
 whitespaces after a user regex.

So the next line will be deleted if we check for any whitespace
character (THIS INCLUDES A NEWLINE!) after the user regex.
But this had the side effect that when matching a user regex, the newline was
also seen as part of the match. Which in the case of an empty macro
 would mean that in the cleanup of the post-match, we would delete the
 line immediately after the empty macro (\#endif in the example above).
So instead of matching with every whitespace character (which includes
newlines!), we explicitly only check for whitespaces.

Taking the example above, this has the desired effect:
match = 'MY_LIBRARY_INLINE' <--- NOTE THAT THE NEWLINE IS NO LONGER PART OF THE MATCH
post-match = '\n#endif\n' <--- BEGINNING NEWLINE NOW

Now the cleanup after a define will see the beginning newline in the
post-match and only remove that newline and now the preprocessor-if is
terminated properly
This commit is contained in:
laurens
2020-01-11 14:43:01 +01:00
committed by laurensmiers
parent cf0e55b6e4
commit faceb864b8
2 changed files with 14 additions and 4 deletions
+4 -2
View File
@@ -107,10 +107,12 @@ class CMockHeaderParser
square_bracket_pair_regex_format = /\{[^\{\}]*\}/ # Regex to match one whole block enclosed by two square brackets
# Convert user provided string patterns to regex
# Use word bounderies before and after the user regex to limit matching to actual word iso part of a word
@inline_function_patterns.each do |user_format_string|
user_regex = Regexp.new(user_format_string)
cleanup_spaces_after_user_regex = /\s*/
inline_function_regex_formats << Regexp.new(user_regex.source + cleanup_spaces_after_user_regex.source)
word_boundary_before_user_regex = /\b/
cleanup_spaces_after_user_regex = /[ ]*\b/
inline_function_regex_formats << Regexp.new(word_boundary_before_user_regex.source + user_regex.source + cleanup_spaces_after_user_regex.source)
end
puts "INLINE REGEXS"
+10 -2
View File
@@ -2154,8 +2154,13 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do
assert_equal(expected, @parser.transform_inline_functions(source))
end
it "Transform inline functions TODO" do
it "Transform inline functions limits deleting user macro to actual line/word" do
source =
"#if defined (FORCE_INLINE)\n" +
"#define MY_LIBRARY_INLINE static __inline__ __attribute__ ((always_inline))\n" +
"#else\n" +
"#define MY_LIBRARY_INLINE\n" +
"#endif\n" +
"#define INLINE static __inline__ __attribute__ ((always_inline))\n" +
"#define INLINE_TWO \\\nstatic\\\ninline\n" +
"INLINE uint16_t _somefunc (uint32_t a)\n" +
@@ -2177,13 +2182,16 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do
"#define INLINE_THREE \\\nstatic\\\ninline"
expected =
"#if defined (FORCE_INLINE)\n" +
"#else\n" +
"#endif\n" +
"uint16_t _somefunc (uint32_t a);\n" +
"uint16_t _somefunc_0 (uint32_t a);\n" +
"uint16_t _somefunc_1 (uint32_t a);\n" +
"uint16_t _somefunc_2(uint32_t a);\n"
@parser.treat_inlines = :include
@parser.inline_function_patterns = ['INLINE_THREE', 'INLINE_TWO', 'INLINE', 'static __inline__ __attribute__ \(\(always_inline\)\)', 'static __inline__']
@parser.inline_function_patterns = ['MY_LIBRARY_INLINE', 'INLINE_THREE', 'INLINE_TWO', 'INLINE', 'static __inline__ __attribute__ \(\(always_inline\)\)', 'static __inline__']
assert_equal(expected, @parser.transform_inline_functions(source))
end