Commit 4b33b132 authored by Patrik Stridvall's avatar Patrik Stridvall Committed by Alexandre Julliard

- Begun implementation of a C statements parser.

- More reorganizations and fixes.
parent b8dd37d2
......@@ -143,6 +143,17 @@ sub argument_documentations {
return $$argument_documentations;
}
sub statements_line {
my $self = shift;
my $statements_line = \${$self->{STATEMENTS_LINE}};
local $_ = shift;
if(defined($_)) { $$statements_line = $_; }
return $$statements_line;
}
sub statements {
my $self = shift;
my $statements = \${$self->{STATEMENTS}};
......
......@@ -38,13 +38,13 @@ while(<IN>) {
if($message) {
if($file && $line) {
if($directory && $directory ne ".") {
if($directory && $directory ne "." && $file !~ m%^/%) {
$output->write(&file_normalize("$directory/$file") . ":$line: $message\n");
} else {
$output->write("$file:$line: $message\n");
}
} elsif($file) {
if($directory && $directory ne ".") {
if($directory && $directory ne "." && $file !~ m%^/%) {
$output->write(&file_normalize("$directory/$file") . ": $message\n");
} else {
$output->write("$file: $message\n");
......
......@@ -131,7 +131,7 @@ sub line {
error("line");
} elsif($tool eq "bison" && /^conflicts:\s+\d+\s+shift\/reduce$/) {
# Nothing
} elsif($tool eq "gcc" && /^In file included from (.+?):(\d+):$/) {
} elsif($tool eq "gcc" && /^(?:In file included |\s*)from (.+?):(\d+)[,:]$/) {
# Nothing
} elsif($tool =~ /^gcc|ld$/ && s/^(.+?\.o(?:\(.*?\))?):\s*//) {
ld_output($1, $_)
......
package winapi_fixup_editor;
use strict;
use options qw($options);
use output qw($output);
use winapi qw($win16api $win32api @winapis);
use util;
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my $self = {};
bless ($self, $class);
my $file = \${$self->{FILE}};
$$file = shift;
return $self;
}
sub add_trigger {
my $self = shift;
my $triggers = \%{$self->{TRIGGERS}};
my $line = shift;
my $action = shift;
if(!defined($$triggers{$line})) {
$$triggers{$line} = [];
}
push @{$$triggers{$line}}, $action;
}
sub replace {
my $self = shift;
my $begin_line = shift;
my $begin_column = shift;
my $end_line = shift;
my $end_column = shift;
my $replace = shift;
my $file = \${$self->{FILE}};
my $line = $begin_line;
my $action = {};
$self->add_trigger($begin_line, {
type => "replace",
begin_line => $begin_line,
begin_column => $begin_column,
end_line => $end_line,
end_column => $end_column,
replace => $replace
});
}
sub flush {
my $self = shift;
my $file = \${$self->{FILE}};
my $triggers = \%{$self->{TRIGGERS}};
my $editor = sub {
local *IN = shift;
local *OUT = shift;
my $modified = 0;
my $again = 0;
my $lookahead = 0;
my $lookahead_count = 0;
LINE: while($again || defined(my $current = <IN>)) {
if(!$again) {
chomp $current;
if($lookahead) {
$lookahead = 0;
$_ .= "\n" . $current;
$lookahead_count++;
} else {
$_ = $current;
$lookahead_count = 0;
}
} else {
$lookahead_count = 0;
$again = 0;
}
my $line = $. - $lookahead_count;
foreach my $action (@{$$triggers{$line}}) {
if($. < $action->{end_line}) {
$lookahead = 1;
next LINE;
}
my $type = $action->{type};
my $begin_line = $action->{begin_line};
my $begin_column = $action->{begin_column};
my $end_line = $action->{end_line};
my $end_column = $action->{end_column};
if($type eq "replace") {
my $replace = $action->{replace};
my @lines = split(/\n/, $_);
if($#lines < 0) {
@lines = ($_);
}
my $begin = "";
my $column = 0;
$_ = $lines[0];
while($column < $begin_column - 1 && s/^.//) {
$begin .= $&;
if($& eq "\t") {
$column = $column + 8 - $column % 8;
} else {
$column++;
}
}
my $column2 = 0;
$_ = $lines[$#lines];
while($column2 < $end_column && s/^.//) {
if($& eq "\t") {
$column2 = $column2 + 8 - $column2 % 8;
} else {
$column2++;
}
}
my $end = $_;
$_ = "$begin$replace$end";
if($options->modify) {
$modified = 1;
} else {
$output->write("$$file:$begin_line.$begin_column-$end_line.$end_column: $replace\n");
}
}
}
print OUT "$_\n";
}
return $modified;
};
my $modified = 0;
if(1) {
$modified = edit_file($$file, $editor);
}
if(!$modified) {
$self->flush_old;
}
}
########################################################################
# Hack for backward compabillity
#
my %insert_line;
my %substitute_line;
my %delete_line;
my %spec_file;
sub flush_old {
my $self = shift;
my $file = ${$self->{FILE}};
my $editor = sub {
local *IN = shift;
local *OUT = shift;
my $modified = 0;
while(<IN>) {
chomp;
my $line;
$line = $insert_line{$.};
if(defined($line)) {
if($options->modify) {
$_ = "$line$_";
$modified = 1;
} else {
my $line2 = $line; chomp($line2);
my @line2 = split(/\n/, $line2);
if($#line2 > 0) {
$output->write("$file: $.: insert: \\\n");
foreach my $line2 (@line2) {
$output->write("'$line2'\n");
}
} else {
$output->write("$file: $.: insert: '$line2'\n");
}
}
}
my $search = $substitute_line{$.}{search};
my $replace = $substitute_line{$.}{replace};
if(defined($search) && defined($replace)) {
my $modified2 = 0;
if(s/$search/$replace/) {
if($options->modify) {
$modified = 1;
}
$modified2 = 1;
}
if(!$options->modify || !$modified2) {
my $search2;
my $replace2;
if(!$modified2) {
$search2 = "unmatched search";
$replace2 = "unmatched replace";
} else {
$search2 = "search";
$replace2 = "replace";
}
$output->write("$file: $.: $search2 : '$search'\n");
my @replace2 = split(/\n/, $replace);
if($#replace2 > 0) {
$output->write("$file: $.: $replace2: \\\n");
foreach my $replace2 (@replace2) {
$output->write("'$replace2'\n");
}
} else {
$output->write("$file: $.: $replace2: '$replace'\n");
}
}
}
$line = $delete_line{$.};
if(defined($line)) {
if(/$line/) {
if($options->modify) {
$modified = 1;
next;
} else {
$output->write("$file: $.: delete: '$line'\n");
}
} else {
$output->write("$file: $.: unmatched delete: '$line'\n");
}
}
print OUT "$_\n";
}
return $modified;
};
my $n = 0;
while(defined(each %insert_line)) { $n++; }
while(defined(each %substitute_line)) { $n++; }
while(defined(each %delete_line)) { $n++; }
if($n > 0) {
edit_file($file, $editor);
}
foreach my $module (sort(keys(%spec_file))) {
my $file;
foreach my $winapi (@winapis) {
$file = ($winapi->module_file($module) || $file);
}
if(defined($file)) {
$file = file_normalize($file);
}
my @substitutes = @{$spec_file{$module}};
my $editor = sub {
local *IN = shift;
local *OUT = shift;
my $modified = 0;
while(<IN>) {
chomp;
my @substitutes2 = ();
foreach my $substitute (@substitutes) {
my $search = $substitute->{search};
my $replace = $substitute->{replace};
if(s/$search/$replace/) {
if($options->modify) {
$modified = 1;
} else {
$output->write("$file: search : '$search'\n");
$output->write("$file: replace: '$replace'\n");
}
next;
} else {
push @substitutes2, $substitute;
}
}
@substitutes = @substitutes2;
print OUT "$_\n";
}
return $modified;
};
if(defined($file)) {
edit_file($file, $editor);
} else {
$output->write("$module: doesn't have any spec file\n");
}
if($#substitutes >= 0) {
foreach my $substitute (@substitutes) {
my $search = $substitute->{search};
my $replace = $substitute->{replace};
$output->write("$file: unmatched search : '$search'\n");
$output->write("$file: unmatched replace: '$replace'\n");
}
}
}
%insert_line = ();
%substitute_line = ();
%delete_line = ();
%spec_file = ();
}
sub delete_line {
my $self = shift;
my $line = shift;
my $pattern = shift;
$delete_line{$line} = $pattern;
}
sub insert_line {
my $self = shift;
my $line = shift;
my $insert = shift;
$insert_line{$line} = $insert;
}
sub substitute_line {
my $self = shift;
my $line = shift;
my $search = shift;
my $replace = shift;
$substitute_line{$line}{search} = $search;
$substitute_line{$line}{replace} = $replace;
}
sub replace_spec_file {
my $self = shift;
my $module = shift;
my $search = shift;
my $replace = shift;
my $substitute = {};
$substitute->{search} = $search;
$substitute->{replace} = $replace;
if(!defined($spec_file{$module})) {
$spec_file{$module} = [];
}
push @{$spec_file{$module}}, $substitute;
}
1;
......@@ -28,6 +28,8 @@ my %options_long = (
"documentation-name" => { default => 1, parent => "documentation", description => "documentation name fixup" },
"documentation-ordinal" => { default => 1, parent => "documentation", description => "documentation ordinal fixup" },
"documentation-wrong" => { default => 1, parent => "documentation", description => "documentation wrong fixup" },
"statements" => { default => 1, parent => "local", description => "statements fixup" },
"statements-windowsx" => { default => 1, parent => "local", description => "statements windowsx fixup" },
"stub" => { default => 0, parent => "local", description => "stub fixup" },
"global" => { default => 1, description => "global fixup" },
......
package winapi_fixup_statements;
use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw();
@EXPORT_OK = qw(&fixup_statements);
use options qw($options);
use output qw($output);
use c_parser;
sub fixup_function_call {
my $name = shift;
my @arguments = @{(shift)};;
return "$name(" . join(", ", @arguments) . ")";
}
sub _parse_makelong {
my $value = shift;
my $low;
my $high;
if($value =~ /^
(?:\(\w+\)\s*)?
MAKE(?:LONG|LPARAM|LRESULT|WPARAM)\s*
\(\s*(.*?)\s*,\s*(.*?)\s*\)$/sx)
{
$low = $1;
$high = $2;
} elsif($value =~ /^(?:\(\w+\)\s*)?0L?$/) {
$low = "0";
$high = "0";
} else {
$low = "($value) & 0xffff";
$high = "($value) << 16";
}
return ($low, $high);
}
sub fixup_function_call_2_forward_wm_call {
my $name = shift;
(my $hwnd, my $msg, my $wparam, my $lparam) = @{(shift)};
if($msg =~ /^(?:WM_BEGINDRAG|WM_ENTERMENULOOP|WM_EXITMENULOOP|WM_HELP|
WM_ISACTIVEICON|WM_LBTRACKPOINT|WM_NEXTMENU)$/x)
{
return undef;
}
my $suffix;
$name =~ /([AW])?$/;
if(defined($1)) {
$suffix = $1;
} else {
$suffix = "";
}
$wparam =~ s/^\(WPARAM\)//;
$lparam =~ s/^\(LPARAM\)//;
my @arguments;
if(0) {
# Nothing
} elsif($msg =~ /^WM_COMMAND$/) {
(my $id, my $code_notify) = _parse_makelong($wparam);
my $hwndctl = $lparam;
@arguments = ($id, $hwndctl, $code_notify);
} elsif($msg =~ /^WM_(?:COPY|CUT|PASTE)$/) {
@arguments = ();
} elsif($msg =~ /^WM_(?:CHARTO|VKEYTO)ITEM$/) {
(my $key, my $caret) = _parse_makelong($wparam);
my $hwndctl = $lparam;
@arguments = ($key, $hwndctl, $caret);
} elsif($msg =~ /^WM_(?:COMPARE|DELETE|DRAW|MEASURE)ITEM$/) {
@arguments = ($lparam);
} elsif($msg =~ s/^WM_GETTEXT$/$&$suffix/) {
@arguments = ($wparam, $lparam);
} elsif($msg =~ /^WM_INITMENU$/) {
my $hmenu = $wparam;
@arguments = ($hmenu);
} elsif($msg =~ /^WM_INITMENUPOPUP$/) {
my $hmenu = $wparam;
(my $item, my $system_menu) = _parse_makelong($lparam);
@arguments = ($hmenu, $item, $system_menu);
} elsif($msg =~ /^WM_MENUCHAR$/) {
(my $ch, my $flags) = _parse_makelong($wparam);
my $hmenu = $lparam;
@arguments = ($ch, $flags, $hmenu);
} elsif($msg =~ /^WM_MENUSELECT$/) {
(my $item, my $flags) = _parse_makelong($wparam);
my $hmenu = $lparam;
my $hmenu_popup = "NULL"; # FIXME: Is this really correct?
@arguments = ($hmenu, $item, $hmenu_popup, $flags);
} elsif($msg =~ s/^WM_(NC)?LBUTTONDBLCLK$/WM_$1LBUTTONDOWN/) {
my $double_click = "TRUE";
my $key_flags = $wparam;
(my $x, my $y) = _parse_makelong($lparam);
@arguments = ($double_click, $x, $y, $key_flags);
} elsif($msg =~ /^WM_(NC)?LBUTTONDOWN$/) {
my $double_click = "FALSE";
my $key_flags = $wparam;
(my $x, my $y) = _parse_makelong($lparam);
@arguments = ($double_click, $x, $y, $key_flags);
} elsif($msg =~ /^WM_LBUTTONUP$/) {
my $key_flags = $wparam;
(my $x, my $y) = _parse_makelong($lparam);
@arguments = ($x, $y, $key_flags);
} elsif($msg =~ /^WM_SETCURSOR$/) {
my $hwnd_cursor = $wparam;
(my $code_hit_test, my $msg2) = _parse_makelong($lparam);
@arguments = ($hwnd_cursor, $code_hit_test, $msg2);
} elsif($msg =~ s/^WM_SETTEXT$/$&$suffix/) {
my $text = $lparam;
@arguments = ($text);
} elsif($msg =~ /^WM_(?:SYS)?KEYDOWN$/) {
my $vk = $wparam;
(my $repeat, my $flags) = _parse_makelong($lparam);
@arguments = ($vk, $repeat, $flags);
} else {
@arguments = ($wparam, $lparam);
}
unshift @arguments, $hwnd;
return "FORWARD_" . $msg . "(" . join(", ", @arguments) . ", $name)";
}
sub fixup_statements {
my $function = shift;
my $editor = shift;
my $linkage = $function->linkage;
my $internal_name = $function->internal_name;
my $statements_line = $function->statements_line;
my $statements = $function->statements;
if(($linkage eq "extern" && !defined($statements)) ||
($linkage eq "" && !defined($statements)))
{
return;
}
if($options->statements_windowsx && defined($statements)) {
my $found_function_call = sub {
my $begin_line = shift;
my $begin_column = shift;
my $end_line = shift;
my $end_column = shift;
my $name = shift;
my $arguments = shift;
foreach my $argument (@$arguments) {
$argument =~ s/^\s*(.*?)\s*$/$1/;
}
if($options->statements_windowsx &&
$name =~ /^(?:DefWindowProc|SendMessage)[AW]$/ &&
$$arguments[1] =~ /^WM_\w+$/)
{
fixup_replace(\&fixup_function_call_2_forward_wm_call, $editor,
$begin_line, $begin_column, $end_line, $end_column,
$name, $arguments);
} elsif(0) {
$output->write("$begin_line.$begin_column-$end_line.$end_column: " .
"$name(" . join(", ", @$arguments) . ")\n");
}
};
my $line = $statements_line;
my $column = 1;
if(!&c_parser::parse_c_statements(\$statements, \$line, \$column, $found_function_call)) {
$output->write("error: can't parse statements\n");
}
}
}
sub fixup_replace {
my $function = shift;
my $editor = shift;
my $begin_line = shift;
my $begin_column = shift;
my $end_line = shift;
my $end_column = shift;
my $replace = &$function(@_);
if(defined($replace)) {
$editor->replace($begin_line, $begin_column, $end_line, $end_column, $replace);
}
}
1;
......@@ -6,7 +6,6 @@ DWORD
HANDLE
HBITMAP
HDROP
HGLOBAL
HMENU
HICON
HINSTANCE
......
......@@ -20,19 +20,17 @@ DEVMODEA *
DOCINFOA *
DWORD *
INT *
LPCVOID
LPDEVMODEA
LPINT
LPLOGFONTW
LPSIZE
LPVOID
POINT *
RECT *
TEXTMETRICW *
void *
%ptr # --forbidden
SEGPTR
%str
LPCSTR
......
......@@ -56,10 +56,6 @@ WINDOWPOS *
WND *
void *
%ptr # --forbidden
SEGPTR
%str
LPCSTR
......
......@@ -30,6 +30,7 @@ sub parse_c_file {
my $argument_types;
my $argument_names;
my $argument_documentations;
my $statements_line;
my $statements;
$function_begin = sub {
......@@ -64,6 +65,7 @@ sub parse_c_file {
};
$function_end = sub {
$statements_line = shift;
$statements = shift;
my $function = &$function_create_callback();
......@@ -90,6 +92,7 @@ sub parse_c_file {
if(defined($argument_documentations)) {
$function->argument_documentations([@$argument_documentations]);
}
$function->statements_line($statements_line);
$function->statements($statements);
&$function_found_callback($function);
......@@ -127,6 +130,7 @@ sub parse_c_file {
my %regs_entrypoints;
my @comment_lines = ();
my @comments = ();
my $statements_line;
my $statements;
my $level = 0;
my $extern_c = 0;
......@@ -163,9 +167,15 @@ sub parse_c_file {
}
# remove C comments
if(s/^(.*?)(\/\*.*?\*\/)(.*)$/$1 $3/s) {
if(/^(.*?)(\/\*(.*?)\*\/)(.*)$/s) {
my @lines = split(/\n/, $2);
push @comment_lines, $.;
push @comments, $2;
push @comments, $2;
if($#lines <= 0) {
$_ = "$1 $4";
} else {
$_ = $1 . ("\n" x $#lines) . $4;
}
$again = 1;
next;
}
......@@ -175,23 +185,25 @@ sub parse_c_file {
}
# remove C++ comments
while(s/^(.*?)\/\/.*?$/$1\n/s) { $again = 1 }
while(s/^(.*?)\/\/.*?$/$1/s) { $again = 1 }
if($again) { next; }
# remove empty rows
if(/^\s*$/) { next; }
# remove preprocessor directives
if(s/^\s*\#/\#/m) {
if(/^\\#.*?\\$/m) {
if(s/^\s*\#/\#/s) {
if(/^\#.*?\\$/s) {
$lookahead = 1;
next;
} elsif(s/^\#\s*(.*?)(\s+(.*?))?\s*$//m) {
} elsif(s/^\#\s*(\w+)((?:\s+(.*?))?\s*)$//s) {
my @lines = split(/\n/, $2);
if($#lines > 0) {
$_ = "\n" x $#lines;
}
if(defined($3)) {
&$preprocessor_found_callback($1, $3);
} else {
&$preprocessor_found_callback($1, "");
}
$again = 1;
next;
}
}
......@@ -282,6 +294,7 @@ sub parse_c_file {
$line .= "{";
print "+1: \{$_\n" if $options->debug >= 2;
$level++;
$statements .= $line;
} elsif(s/^\}//) {
$_ = $'; $again = 1;
$line .= "}" if $level > 1;
......@@ -291,15 +304,14 @@ sub parse_c_file {
$extern_c = 0;
$level = 0;
}
}
if($line !~ /^\s*$/) {
$statements .= $line;
} else {
$statements .= "$line\n";
}
if($level == 0) {
if($in_function) {
&$function_end($statements);
&$function_end($statements_line, $statements);
$statements = undef;
} elsif($in_type) {
if(/^\s*(?:WINE_PACKED\s+)?((?:\*\s*)?\w+\s*(?:\s*,\s*(?:\*+\s*)?\w+)*\s*);/s) {
......@@ -404,8 +416,9 @@ sub parse_c_file {
$function_line, $linkage, $return_type, $calling_convention, $name,
\@argument_types,\@argument_names,\@argument_documentations);
if($level == 0) {
&$function_end(undef);
&$function_end(undef, undef);
}
$statements_line = $.;
$statements = "";
} elsif(/__ASM_GLOBAL_FUNC\(\s*(.*?)\s*,/s) {
my @lines = split(/\n/, $&);
......@@ -415,7 +428,7 @@ sub parse_c_file {
&$function_begin($documentation_line, $documentation,
$function_line, "", "void", "__asm", $1);
&$function_end("");
&$function_end($., "");
} elsif(/WAVEIN_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
my @lines = split(/\n/, $&);
my $function_line = $. - scalar(@lines) + 1;
......@@ -425,10 +438,10 @@ sub parse_c_file {
my @arguments32 = ("HWAVEIN");
&$function_begin($documentation_line, $documentation,
$function_line, "", "UINT16", "WINAPI", "waveIn" . $1 . "16", \@arguments16);
&$function_end("");
&$function_end($., "");
&$function_begin($documentation_line, $documentation,
$function_line, "", "UINT", "WINAPI", "waveIn" . $1, \@arguments32);
&$function_end("");
&$function_end($., "");
} elsif(/WAVEOUT_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
my @lines = split(/\n/, $&);
my $function_line = $. - scalar(@lines) + 1;
......@@ -439,10 +452,10 @@ sub parse_c_file {
my @arguments32 = ("HWAVEOUT");
&$function_begin($documentation_line, $documentation,
$function_line, "", "UINT16", "WINAPI", "waveOut" . $1 . "16", \@arguments16);
&$function_end("");
&$function_end($., "");
&$function_begin($documentation_line, $documentation,
$function_line, "", "UINT", "WINAPI", "waveOut" . $1, \@arguments32);
&$function_end("");
&$function_end($., "");
} elsif(/WAVEOUT_SHORTCUT_(1|2)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
my @lines = split(/\n/, $&);
my $function_line = $. - scalar(@lines) + 1;
......@@ -454,19 +467,19 @@ sub parse_c_file {
my @arguments32 = ("HWAVEOUT", $4);
&$function_begin($documentation_line, $documentation,
$function_line, "", "UINT16", "WINAPI", "waveOut" . $2 . "16", \@arguments16);
&$function_end("");
&$function_end($., "");
&$function_begin($documentation_line, $documentation,
$function_line, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
&$function_end("");
&$function_end($., "");
} elsif($1 eq 2) {
my @arguments16 = ("UINT16", $4);
my @arguments32 = ("UINT", $4);
&$function_begin($documentation_line, $documentation,
$function_line, "", "UINT16", "WINAPI", "waveOut". $2 . "16", \@arguments16);
&$function_end("");
&$function_end($., "");
&$function_begin($documentation_line, $documentation,
$function_line, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
&$function_end("");
&$function_end($., "");
}
} elsif(/DEFINE_REGS_ENTRYPOINT_\d+\(\s*(\S*)\s*,\s*([^\s,\)]*).*?\)/s) {
$_ = $'; $again = 1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment