summaryrefslogtreecommitdiffstats
path: root/scripts/fixheaders
blob: 73c7b581de28c70aea2bf0a8850ac85590db5e8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#! /usr/bin/env perl

# this script is written by Stephan Kulow <coolo@kde.org> with
# much help from Sirtaj Singh Kang <ssk@physics.unimelb.edu.au>

if ($ARGV[0]) {
  $topdir = $ARGV[0];
} else {
  $topdir=`pwd`;
}
chomp $topdir;

$lastdir = '.';


# this is the important part. Left from the '=>' you find the regular
# expression for the g++ output and right from '=>' the header file to
# include
%messages = 
(
 'implicit declaration of function `int i18n\(\.\.\.\)\'' => "klocale",
 '`i18n\' undeclared \(first use this function\)' => "klocale",
 'variable `class QPixmap \S*\' has initializer but incomplete type' => "qpixmap",
 '`kapp\' undeclared \(first use this function\)' => "kapplication",
 'no matching function for call to `TDELocale::' => "klocale",
 '`klocale\' undeclared \(first use this function\)' => "klocale",
 'no matching function for call to `QPopupMenu::' => "qpopupmenu",
 '`QTextStream\' undeclared \(first use this function\)' => "qtextstream",
 '`QTextStream\' was not declared in this scope' => "qtextstream",
 'incomplete type `QSocketNotifier\'' => "qsocketnotifier",
 'no matching function for call to `TDEConfig' => "tdeconfig",
 'variable `class TDEConfig \S*\' has initializer but incomplete type' => "tdeconfig",
 'implicit declaration of function `int kdDebug' => "kdebug",
 'implicit declaration of function `int kdWarning' => "kdebug",
 '`QFile\' undeclared \(first use this function' => "qfile",
 'variable `QFile \S*\' has initializer but incomplete type' => "qfile",
 'type `TDEConfigBase\' is not a base type for type `TDEConfig' => "tdeconfig",
 'invalid use of undefined type `class QAccel' => "qaccel",
 'invalid use of undefined type `class TDEAboutData' => "kaboutdata",
 'incomplete type `TDEAboutData\'' => "kaboutdata",
 'incomplete type `QGrid\'' => "qgrid",
 'invalid use of undefined type `class QGrid\'' => "qgrid",
 'aggregate `class TDEConfig \S*\' has incomplete type' => "tdeconfig",
 '`stderr\' undeclared \(first use this function' => "stdio",
 'invalid use of undefined type `class TDEConfig' => "tdeconfig",
 'implicit declaration of function `int f?printf' => "stdio",
 'no method `TDEGlobal::' => "kglobal",
 '`TDEGlobal\' undeclared \(first use this function\)' => "kglobal",
 'implicit declaration of function `int locate\(\.\.\.\)' => "kstddirs",
 '`locate\' undeclared \(first use this function\)' => "kstddirs",
 'no matching function for call to `TDEStandardDirs' => "kstddirs",
 'no method `TDEStandardDirs::' => "kstddirs",
 'variable `class QFile \S*\' has initializer but incomplete type' => "qfile",
 'implicit declaration of function `int ICON\(\.\.\.\)' => "kiconloader",
 '`QMessageBox\' undeclared \(first use this function\)' => "qmessagebox",
 'no matching function for call to `QBoxLayout::QBoxLayout' => "qlayout",
 '`QUriDrag\' undeclared \(first use this function\)' => "qdragobject",
 '`kdDebug\' undeclared \(first use this function\)' => "kdebug",
 '`kdWarning\' undeclared \(first use this function\)' => "kdebug",
 'no matching function for call to `KMenuBar::insertItem\(QString, TDEPopupMenu' => "tdepopupmenu",
 'no matching function for call to `KMenuBar::' => "kmenubar",
 'invalid use of undefined type `class QPointArray' => "qpointarray",
 'variable `QPainter \S*\' has initializer but incomplete type' => "qpainter",
 'invalid use of undefined type `class QRegExp' => "qregexp",
 'invalid use of undefined type `class QPushButton' => "qpushbutton",
 'cannot convert `QPushButton \*\' to `QButton \*' => "qpushbutton",
 'invalid use of undefined type `class QButton' => "qbutton",
 '`QButton\' undeclared \(first use this function\)' => "qbutton",
 'no method `QCursor::pos' => "qcursor",
 '`DesktopIcon\' undeclared \(first use this function\)' => "kiconloader",
 '`BarIcon\' undeclared \(first use this function\)' => "kiconloader",
 '`SmallIcon\' undeclared \(first use this function\)' => "kiconloader",
 '`UserIcon\' undeclared \(first use this function\)' => "kiconloader",
 'implicit declaration of function `int UserIcon\(...\)\'' => "kiconloader",
 '`TDEIcon\' undeclared \(first use this function\)' => "kiconloader",
 'invalid use of undefined type `class TDEIconLoader' => "kiconloader",
 'invalid use of undefined type `class TDEInstance' => "kinstance",
 'invalid use of undefined type `class DCOPClient' => "dcopclient",
 '`DCOPClient\' undeclared \(first use this function\)' => "dcopclient",
 'invalid use of undefined type `class KStatusBar\'' => "kstatusbar",
 'invalid use of undefined type `class QLabel\'' => "qlabel",
 'invalid use of undefined type `class QImage\'' => "qimage",
 'invalid use of undefined type `class QImageIO\'' => "qimage",
 'invalid use of undefined type `class QLineEdit\'' => "qlineedit",
 'invalid use of undefined type `class QComboBox\'' => "qcombobox",
 'invalid use of undefined type `class QStyle\'' => "qstyle",
 'invalid use of undefined type `class TDEPopupMenu\'' => "tdepopupmenu",
 'invalid use of undefined type `class QPopupMenu\'' => "qpopupmenu",
 'cannot convert `TDEPopupMenu \*\' to `QPopupMenu \*' => "tdepopupmenu",
 'aggregate `QPopupMenu \S*\' has incomplete type' => "qpopupmenu",
 'invalid use of undefined type `class KURL' => "kurl",
 'no method `QApplication::' => "qapplication",
 'no method `QFile::' => "qfile",
 'error: \'Q3CString\' is used as a type' => "q3cstring",
 'error: ISO C\+\+ forbids declaration of \`Q3CString\' with' => "q3cstring",
 'error: incomplete type \'QPixmap\' cannot be used' => 'qpixmap',
 'error: incomplete type `Q3ValueList' => 'q3valuelist',
 'error: variable `Q3ValueList<' => 'q3valuelist',
 'error: `Q3PointArray\' undeclared' => 'q3pointarray',
 'error: invalid use of undefined type \`struct QColor' => 'qcolor',
 'error: `QX11Info::' => 'qx11info_x11',
 'error: incomplete type \'QX11Info' => 'qx11info_x11',
 'error: \'Q3AsciiDi' => 'q3asciidict'
);

# Initial values are simply to get into the while
$exitcode=2; # exit-code from last 'make' command
$addedone=1; # 1 when something has been fixed, means we can try again

while ( $exitcode != 0 && $addedone != 0 )
{
 $addedone = 0;

 %changes = ();
 open(INPUT,"makeobj -j10 -k 2>&1 |") || die "Couldn't run makeobj";
 while(<INPUT>)
 {
  if (/make.*Entering directory \`(.+)\'/) {
    $lastdir = $1;
    $lastdir =~ s/^$topdir\///;
    print STDERR "entering $lastdir\n";
    next;
  }
  if (/^([^:]*):\d*: (.*)$/) {
    $file = $1;
    $line = $2;
    if ($file !~ m,^\/,) {
      $file = "$lastdir/$file";
    }
    #print STDERR "file=$file\n";
  } else {
    # This could be a continuation line
    if ( defined $line && $line ne "" ) {
      $line .= $_;
      #print STDERR "file still $file\n";
      #print STDERR "line=$line\n";
    } else {
      # Compilation line, or other unparsable line -> ignore
      print STDOUT $_;
      next;
    }
  }
  
  #print STDERR "Already having changes for $file\n" if defined($changes{$file});
  next if defined($changes{$file});

  print STDOUT $_;

  foreach $message (keys %messages) {
    if ($line =~ /$message/) {
      $changes{$file} = $messages{$message};
      $addedone = fixFile($file, $messages{$message});
    }
  }
  
  if (defined($changes{$file})) { $file=""; $line=""; }
 } # end of while(<INPUT>)
 close(INPUT);
 $exitcode=($?>>8);
} # end of main while

sub fixFile
{
  my( $file, $adding ) = @_;
  
  my $lastinclude = "";

  # read file
  open ( FILE, "$file" ) || die "Can't read $file";
  
  my $flines;
  my $cpplevel = 0;

  while (<FILE>) {
    $flines .= $_;
    if ($_ =~ m/^#if/ && $_ !~ m/^#ifn/) {
       $cpplevel = $cpplevel + 1;
    }
    if ($_ =~ m/^#endif/) {
       $cpplevel = $cpplevel - 1;
    }
    if ($cpplevel == 0 && $_ =~ m/^(#include\s*[\"<]q\S*\.h[\">]\S*)/) {
       $lastinclude = $1;
    }
  }
  
  close FILE;
  
  if (!$lastinclude) {
    print STDERR "ERROR: no include found in $file! (tried to add $adding.h)\n";
    return 0;
  }

  if ($flines =~ m/#include\s*[\"<]$adding\.h[\">]\s*\n/) {
    print STDERR "ERROR: $adding.h already included in $file!\n";
    return 0;
  }
  if ($flines =~ /(\n$lastinclude)/) {
    $flines =~ s/$lastinclude(.*\n)/$lastinclude$1#include <$adding.h>\n/;
    print STDERR "ADDED <$adding.h> after \"$lastinclude\" in $file\n";
  } else {
    print STDERR "ERROR: can't find $lastinclude in $file\n";
    return 0;
  }
	    
  # write file
      
  rename($file, "$file.old");
  open( FILE, ">$file" ) || die "Can't write to $file";
  print FILE $flines;    
  close FILE;
  return 1;
}