From f2fbc904c8454162f35c4ef7e97b3ba55dd56a2d Mon Sep 17 00:00:00 2001 From: Joseph Hickey Date: Tue, 28 Sep 2021 09:06:58 -0400 Subject: [PATCH] Fix #1166, recognize ifdef __cplusplus Add feature to generate_stubs.pl input phase to recognize and skip over `#ifdef __cplusplus` blocks. This makes it so it can be used on C headers which are C++-enabled too. --- ut_assert/scripts/generate_stubs.pl | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/ut_assert/scripts/generate_stubs.pl b/ut_assert/scripts/generate_stubs.pl index 4a0792a24..32879ab16 100755 --- a/ut_assert/scripts/generate_stubs.pl +++ b/ut_assert/scripts/generate_stubs.pl @@ -110,6 +110,7 @@ my $file = ""; my $file_boilerplate; my $file_variadic; + my @ifdef_level = (1); # All header files start with some legal boilerplate comments # Take the first one and save it, so it can be put into the output. @@ -125,7 +126,31 @@ # so it will be in a single "line" in the result. chomp if (s/\\$//); } - push(@lines, $_); + + # detect "#ifdef" lines - some may need to be recognized. + # at the very least, any C++-specific bits need to be skipped. + # for now this just specifically looks for __cplusplus + if (/^\#(if\w+)\s+(.*)$/) { + my $check = $1; + my $cond = $2; + my $result = $ifdef_level[0]; + + if ($cond eq "__cplusplus" && $check eq "ifdef") { + $result = 0; + } + + unshift(@ifdef_level, $result); + } + elsif (/^\#else/) { + # invert the last preprocessor condition + $ifdef_level[0] = $ifdef_level[0] ^ $ifdef_level[1]; + } + elsif (/^\#endif/) { + shift(@ifdef_level); + } + elsif ($ifdef_level[0]) { + push(@lines, $_) ; + } } close(HDR); @@ -164,7 +189,6 @@ next if (/\btypedef\b/); # ignore typedefs next if (/\bstatic inline\b/); # ignore - # discard "extern" qualifier # (but other qualifiers like "const" are OK and should be preserved, as # it is part of return type). @@ -408,4 +432,3 @@ print "Generated $stubfile\n"; } -