From bd3b6967afb377544daac2d1d3f9f5e0e7bd78ea Mon Sep 17 00:00:00 2001 From: Francois Gouget Date: Thu, 21 Oct 2004 19:56:04 +0000 Subject: [PATCH] Fix escaped quote handling in strings. Properly handle C and C++ comment parsing. --- tools/winapi_check/winapi_parser.pm | 70 +++++++++++++++++------------ 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/tools/winapi_check/winapi_parser.pm b/tools/winapi_check/winapi_parser.pm index 38000769b39..15ed3641b31 100644 --- a/tools/winapi_check/winapi_parser.pm +++ b/tools/winapi_check/winapi_parser.pm @@ -165,7 +165,7 @@ sub parse_c_file($$) { print STDERR "Processing file '$file' ... " if $options->verbose; open(IN, "< $file") || die ": $file: $!\n"; local $_ = ""; - while($again || defined(my $line = )) { + readmore: while($again || defined(my $line = )) { $_ = "" if !defined($_); if(!$again) { chomp $line; @@ -191,33 +191,45 @@ sub parse_c_file($$) { last; } - # remove C comments - if(s/^([^\"\/]*?(?:\"[^\"]*?\"[^\"]*?)*?)(?=\/\*)//s) { - my $prefix = $1; - if(s/^(\/\*.*?\*\/)//s) { - my @lines = split(/\n/, $1); - push @comment_lines, $.; - push @comments, $1; - &$c_comment_found_callback($. - $#lines, $., $1); - if($#lines <= 0) { - $_ = "$prefix $_"; - } else { - $_ = $prefix . ("\n" x $#lines) . $_; - } - $again = 1; - } else { - $_ = "$prefix$_"; - $lookahead = 1; - } - next; - } - - # remove C++ comments - while(s/^([^\"\/]*?(?:\"[^\"]*?\"[^\"]*?)*?)(\/\/.*?)$/$1/s) { - &$cplusplus_comment_found_callback($., $2); - $again = 1; - } - if($again) { next; } + my $prefix=""; + while ($_ ne "") + { + if (s/^([^\"\/]+|\"(?:[^\\\"]*|\\.)*\")//) + { + $prefix.=$1; + } + elsif (/^\/\*/) + { + # remove C comments + if(s/^(\/\*.*?\*\/)//s) { + my @lines = split(/\n/, $1); + push @comment_lines, $.; + push @comments, $1; + &$c_comment_found_callback($. - $#lines, $., $1); + if($#lines <= 0) { + $_ = "$prefix $_"; + } else { + $_ = $prefix . ("\n" x $#lines) . $_; + } + $again = 1; + } else { + $_ = "$prefix$_"; + $lookahead = 1; + } + next readmore; + } + elsif (s/^(\/\/.*)$//) + { + # remove C++ comments + &$cplusplus_comment_found_callback($., $1); + $again = 1; + } + elsif (s/^(.)//) + { + $prefix.=$1; + } + } + $_=$prefix; # remove preprocessor directives if(s/^\s*\#/\#/s) { @@ -652,7 +664,7 @@ sub parse_c_file($$) { $output->write("$file: $.: can't parse: '$&'\n"); } elsif(/\'[^\']*\'/s) { $_ = $'; $again = 1; - } elsif(/\"[^\"]*\"/s) { + } elsif(/\"(?:[^\\\"]*|\\.)*\"/s) { $_ = $'; $again = 1; } elsif(/;/s) { $_ = $'; $again = 1;