1 | <html>
|
---|
2 |
|
---|
3 | <head>
|
---|
4 | <title>GCC Bugs</title>
|
---|
5 | </head>
|
---|
6 |
|
---|
7 | <body>
|
---|
8 | <h1>GCC Bugs</h1>
|
---|
9 |
|
---|
10 | <p>The latest version of this document is always available at
|
---|
11 | <a href="http://gcc.gnu.org/bugs.html">http://gcc.gnu.org/bugs.html</a>.</p>
|
---|
12 |
|
---|
13 | <hr />
|
---|
14 |
|
---|
15 | <h2>Table of Contents</h2>
|
---|
16 | <ul>
|
---|
17 | <li><a href="#report">Reporting Bugs</a>
|
---|
18 | <ul>
|
---|
19 | <li><a href="#need">What we need</a></li>
|
---|
20 | <li><a href="#dontwant">What we DON'T want</a></li>
|
---|
21 | <li><a href="#where">Where to post it</a></li>
|
---|
22 | <li><a href="#detailed">Detailed bug reporting instructions</a></li>
|
---|
23 | <li><a href="#gnat">Detailed bug reporting instructions for GNAT</a></li>
|
---|
24 | <li><a href="#pch">Detailed bug reporting instructions when using a precompiled header</a></li>
|
---|
25 | </ul>
|
---|
26 | </li>
|
---|
27 | <li><a href="#known">Frequently Reported Bugs in GCC</a>
|
---|
28 | <ul>
|
---|
29 | <li><a href="#cxx">C++</a>
|
---|
30 | <ul>
|
---|
31 | <li><a href="#missing">Missing features</a></li>
|
---|
32 | <li><a href="#fixed34">Bugs fixed in the 3.4 series</a></li>
|
---|
33 | </ul>
|
---|
34 | </li>
|
---|
35 | <li><a href="#fortran">Fortran</a></li>
|
---|
36 | </ul>
|
---|
37 | </li>
|
---|
38 | <li><a href="#nonbugs">Non-bugs</a>
|
---|
39 | <ul>
|
---|
40 | <li><a href="#nonbugs_general">General</a></li>
|
---|
41 | <li><a href="#nonbugs_c">C</a></li>
|
---|
42 | <li><a href="#nonbugs_cxx">C++</a>
|
---|
43 | <ul>
|
---|
44 | <li><a href="#upgrading">Common problems when upgrading the compiler</a></li>
|
---|
45 | </ul>
|
---|
46 | </li>
|
---|
47 | </ul>
|
---|
48 | </li>
|
---|
49 | </ul>
|
---|
50 |
|
---|
51 | <hr />
|
---|
52 |
|
---|
53 | <h1><a name="report">Reporting Bugs</a></h1>
|
---|
54 |
|
---|
55 | <p>The main purpose of a bug report is to enable us to fix the bug. The
|
---|
56 | most important prerequisite for this is that the report must be complete and
|
---|
57 | self-contained, which we explain in detail below.</p>
|
---|
58 |
|
---|
59 | <p>Before you report a bug, please check the
|
---|
60 | <a href="#known">list of well-known bugs</a> and, <strong>if possible
|
---|
61 | in any way, try a current development snapshot</strong>.
|
---|
62 | If you want to report a bug with versions of GCC before 3.1 we strongly
|
---|
63 | recommend upgrading to the current release first.</p>
|
---|
64 |
|
---|
65 | <p>Before reporting that GCC compiles your code incorrectly, please
|
---|
66 | compile it with <code>gcc -Wall</code> and see whether this shows
|
---|
67 | anything wrong with your code that could be the cause instead of a bug
|
---|
68 | in GCC.</p>
|
---|
69 |
|
---|
70 | <h2>Summarized bug reporting instructions</h2>
|
---|
71 |
|
---|
72 | <p>After this summary, you'll find detailed bug reporting
|
---|
73 | instructions, that explain how to obtain some of the information
|
---|
74 | requested in this summary.</p>
|
---|
75 |
|
---|
76 | <h3><a name="need">What we need</a></h3>
|
---|
77 |
|
---|
78 | <p>Please include in your bug report all of the following items, the first
|
---|
79 | three of which can be obtained from the output of <code>gcc -v</code>:</p>
|
---|
80 |
|
---|
81 | <ul>
|
---|
82 | <li>the exact version of GCC;</li>
|
---|
83 | <li>the system type;</li>
|
---|
84 | <li>the options given when GCC was configured/built;</li>
|
---|
85 | <li>the complete command line that triggers the bug;</li>
|
---|
86 | <li>the compiler output (error messages, warnings, etc.); and</li>
|
---|
87 | <li>the <em>preprocessed</em> file (<code>*.i*</code>) that triggers the
|
---|
88 | bug, generated by adding <code>-save-temps</code> to the complete
|
---|
89 | compilation command, or, in the case of a bug report for the GNAT front end,
|
---|
90 | a complete set of source files (see below).</li>
|
---|
91 | </ul>
|
---|
92 |
|
---|
93 | <h3><a name="dontwant">What we do <strong>not</strong> want</a></h3>
|
---|
94 |
|
---|
95 | <ul>
|
---|
96 | <li>A source file that <code>#include</code>s header files that are left
|
---|
97 | out of the bug report (see above)</li>
|
---|
98 |
|
---|
99 | <li>That source file and a collection of header files.</li>
|
---|
100 |
|
---|
101 | <li>An attached archive (tar, zip, shar, whatever) containing all
|
---|
102 | (or some :-) of the above.</li>
|
---|
103 |
|
---|
104 | <li>A code snippet that won't cause the compiler to produce the
|
---|
105 | exact output mentioned in the bug report (e.g., a snippet with just
|
---|
106 | a few lines around the one that <b>apparently</b> triggers the bug,
|
---|
107 | with some pieces replaced with ellipses or comments for extra
|
---|
108 | obfuscation :-)</li>
|
---|
109 |
|
---|
110 | <li>The location (URL) of the package that failed to build (we won't
|
---|
111 | download it, anyway, since you've already given us what we need to
|
---|
112 | duplicate the bug, haven't you? :-)</li>
|
---|
113 |
|
---|
114 | <li>An error that occurs only some of the times a certain file is
|
---|
115 | compiled, such that retrying a sufficient number of times results in
|
---|
116 | a successful compilation; this is a symptom of a hardware problem,
|
---|
117 | not of a compiler bug (sorry)</li>
|
---|
118 |
|
---|
119 | <li>E-mail messages that complement previous, incomplete bug
|
---|
120 | reports. Post a new, self-contained, full bug report instead, if
|
---|
121 | possible as a follow-up to the original bug report</li>
|
---|
122 |
|
---|
123 | <li>Assembly files (<code>*.s</code>) produced by the compiler, or any
|
---|
124 | binary files, such as object files, executables, core files, or
|
---|
125 | precompiled header files</li>
|
---|
126 |
|
---|
127 | <li>Duplicate bug reports, or reports of bugs already fixed in the
|
---|
128 | development tree, especially those that have already been reported
|
---|
129 | as fixed last week :-)</li>
|
---|
130 |
|
---|
131 | <li>Bugs in the assembler, the linker or the C library. These are
|
---|
132 | separate projects, with separate mailing lists and different bug
|
---|
133 | reporting procedures</li>
|
---|
134 |
|
---|
135 | <li>Bugs in releases or snapshots of GCC not issued by the GNU
|
---|
136 | Project. Report them to whoever provided you with the release</li>
|
---|
137 |
|
---|
138 | <li>Questions about the correctness or the expected behavior of
|
---|
139 | certain constructs that are not GCC extensions. Ask them in forums
|
---|
140 | dedicated to the discussion of the programming language</li>
|
---|
141 | </ul>
|
---|
142 |
|
---|
143 | <h3><a name="where">Where to post it</a></h3>
|
---|
144 |
|
---|
145 | <p>Please submit your bug report directly to the
|
---|
146 | <a href="http://gcc.gnu.org/bugzilla/">GCC bug database</a>.
|
---|
147 | Alternatively, you can use the <code>gccbug</code> script that mails your bug
|
---|
148 | report to the bug database.
|
---|
149 | <br />
|
---|
150 | Only if all this is absolutely impossible, mail all information to
|
---|
151 | <a href="mailto:gcc-bugs@gcc.gnu.org">gcc-bugs@gcc.gnu.org</a>.</p>
|
---|
152 |
|
---|
153 | <h2><a name="detailed">Detailed bug reporting instructions</a></h2>
|
---|
154 |
|
---|
155 | <p>Please refer to the <a href="#gnat">next section</a> when reporting
|
---|
156 | bugs in GNAT, the Ada compiler, or to the <a href="#pch">one after
|
---|
157 | that</a> when reporting bugs that appear when using a precompiled header.</p>
|
---|
158 |
|
---|
159 | <p>In general, all the information we need can be obtained by
|
---|
160 | collecting the command line below, as well as its output and the
|
---|
161 | preprocessed file it generates.</p>
|
---|
162 |
|
---|
163 | <blockquote><p><code>gcc -v -save-temps <i>all-your-options
|
---|
164 | source-file</i></code></p></blockquote>
|
---|
165 |
|
---|
166 | <p>Typically the preprocessed file (extension <code>.i</code> for C or
|
---|
167 | <code>.ii</code> for C++, and <code>.f</code> if the preprocessor is used on
|
---|
168 | Fortran files) will be large, so please compress the
|
---|
169 | resulting file with one of the popular compression programs such as
|
---|
170 | bzip2, gzip, zip or compress (in
|
---|
171 | decreasing order of preference). Use maximum compression
|
---|
172 | (<code>-9</code>) if available. Please include the compressed
|
---|
173 | preprocessor output in your bug report, even if the source code is
|
---|
174 | freely available elsewhere; it makes the job of our volunteer testers
|
---|
175 | much easier.</p>
|
---|
176 |
|
---|
177 | <p>The <b>only</b> excuses to not send us the preprocessed sources are
|
---|
178 | (i) if you've found a bug in the preprocessor, (ii) if you've reduced
|
---|
179 | the testcase to a small file that doesn't include any other file or
|
---|
180 | (iii) if the bug appears only when using precompiled headers. If you
|
---|
181 | can't post the preprocessed sources because they're proprietary code,
|
---|
182 | then try to create a small file that triggers the same problem.</p>
|
---|
183 |
|
---|
184 | <p>Since we're supposed to be able to re-create the assembly output
|
---|
185 | (extension <code>.s</code>), you usually should not include
|
---|
186 | it in the bug report, although you may want to post parts of it to
|
---|
187 | point out assembly code you consider to be wrong.</p>
|
---|
188 |
|
---|
189 | <p>Whether to use MIME attachments or <code>uuencode</code> is up to
|
---|
190 | you. In any case, make sure the compiler command line, version and
|
---|
191 | error output are in plain text, so that we don't have to decode the
|
---|
192 | bug report in order to tell who should take care of it. A meaningful
|
---|
193 | subject indicating language and platform also helps.</p>
|
---|
194 |
|
---|
195 | <p>Please avoid posting an archive (.tar, .shar or .zip); we generally
|
---|
196 | need just a single file to reproduce the bug (the .i/.ii/.f preprocessed
|
---|
197 | file), and, by storing it in an archive, you're just making our
|
---|
198 | volunteers' jobs harder. Only when your bug report requires multiple
|
---|
199 | source files to be reproduced should you use an archive. This is, for example,
|
---|
200 | the case if you are using <code>INCLUDE</code> directives in Fortran code,
|
---|
201 | which are not processed by the preprocessor, but the compiler. In that case,
|
---|
202 | we need the main file and all <code>INCLUDE</code>d files. In any case,
|
---|
203 | make sure the compiler version, error message, etc, are included in
|
---|
204 | the body of your bug report as plain text, even if needlessly
|
---|
205 | duplicated as part of an archive.</p>
|
---|
206 |
|
---|
207 | <p>If you fail to supply enough information for a bug report to be
|
---|
208 | reproduced, someone will probably ask you to post additional
|
---|
209 | information (or just ignore your bug report, if they're in a bad day,
|
---|
210 | so try to get it right on the first posting :-). In this case, please
|
---|
211 | post the additional information to the bug reporting mailing list, not
|
---|
212 | just to the person who requested it, unless explicitly told so. If
|
---|
213 | possible, please include in this follow-up all the information you had
|
---|
214 | supplied in the incomplete bug report (including the preprocessor
|
---|
215 | output), so that the new bug report is self-contained.</p>
|
---|
216 |
|
---|
217 | <h2><a name="gnat">Detailed bug reporting instructions for GNAT</a></h2>
|
---|
218 |
|
---|
219 | <p>See the <a href="#detailed">previous section</a> for bug reporting
|
---|
220 | instructions for GCC language implementations other than Ada.</p>
|
---|
221 |
|
---|
222 | <p>Bug reports have to contain at least the following information in
|
---|
223 | order to be useful:</p>
|
---|
224 |
|
---|
225 | <ul>
|
---|
226 | <li>the exact version of GCC, as shown by "<code>gcc -v</code>";</li>
|
---|
227 | <li>the system type;</li>
|
---|
228 | <li>the options when GCC was configured/built;</li>
|
---|
229 | <li>the exact command line passed to the <code>gcc</code> program
|
---|
230 | triggering the bug
|
---|
231 | (not just the flags passed to <code>gnatmake</code>, but
|
---|
232 | <code>gnatmake</code> prints the parameters it passed to <code>gcc</code>)</li>
|
---|
233 | <li>a collection of source files for reproducing the bug,
|
---|
234 | preferably a minimal set (see below);</li>
|
---|
235 | <li>a description of the expected behavior;</li>
|
---|
236 | <li>a description of actual behavior.</li>
|
---|
237 | </ul>
|
---|
238 |
|
---|
239 | <p>If your code depends on additional source files (usually package
|
---|
240 | specifications), submit the source code for these compilation units in
|
---|
241 | a single file that is acceptable input to <code>gnatchop</code>,
|
---|
242 | i.e. contains no non-Ada text. If the compilation terminated
|
---|
243 | normally, you can usually obtain a list of dependencies using the
|
---|
244 | "<code>gnatls -d <i>main_unit</i></code>" command, where
|
---|
245 | <code><i>main_unit</i></code> is the file name of the main compilation
|
---|
246 | unit (which is also passed to <code>gcc</code>).</p>
|
---|
247 |
|
---|
248 | <p>If you report a bug which causes the compiler to print a bug box,
|
---|
249 | include that bug box in your report, and do not forget to send all the
|
---|
250 | source files listed after the bug box along with your report.</p>
|
---|
251 |
|
---|
252 | <p>If you use <code>gnatprep</code>, be sure to send in preprocessed
|
---|
253 | sources (unless you have to report a bug in <code>gnatprep</code>).</p>
|
---|
254 |
|
---|
255 | <p>When you have checked that your report meets these criteria, please
|
---|
256 | submit it according to our <a href="#where">generic instructions</a>.
|
---|
257 | (If you use a mailing list for reporting, please include an
|
---|
258 | "<code>[Ada]</code>" tag in the subject.)</p>
|
---|
259 |
|
---|
260 | <h2><a name="pch">Detailed bug reporting instructions when using a
|
---|
261 | precompiled header</a></h2>
|
---|
262 |
|
---|
263 | <p>If you're encountering a bug when using a precompiled header, the
|
---|
264 | first thing to do is to delete the precompiled header, and try running
|
---|
265 | the same GCC command again. If the bug happens again, the bug doesn't
|
---|
266 | really involve precompiled headers, please report it without using
|
---|
267 | them by following the instructions <a href="#detailed">above</a>.</p>
|
---|
268 |
|
---|
269 | <p>If you've found a bug while <i>building</i> a precompiled header
|
---|
270 | (for instance, the compiler crashes), follow the usual instructions
|
---|
271 | <a href="#detailed">above</a>.</p>
|
---|
272 |
|
---|
273 | <p>If you've found a real precompiled header bug, what we'll need to
|
---|
274 | reproduce it is the sources to build the precompiled header (as a
|
---|
275 | single <code>.i</code> file), the source file that uses the
|
---|
276 | precompiled header, any other headers that source file includes, and
|
---|
277 | the command lines that you used to build the precompiled header and to
|
---|
278 | use it.</p>
|
---|
279 |
|
---|
280 | <p>Please <strong>don't</strong> send us the actual precompiled
|
---|
281 | header. It is likely to be very large and we can't use it to
|
---|
282 | reproduce the problem.</p>
|
---|
283 |
|
---|
284 | <hr />
|
---|
285 |
|
---|
286 | <h1><a name="known">Frequently Reported Bugs in GCC</a></h1>
|
---|
287 |
|
---|
288 | <p>This is a list of bugs in GCC that are reported very often, but not
|
---|
289 | yet fixed. While it is certainly better to fix bugs instead of documenting
|
---|
290 | them, this document might save people the effort of writing a bug report
|
---|
291 | when the bug is already well-known.</p>
|
---|
292 |
|
---|
293 | <p>There are many reasons why a reported bug doesn't get fixed.
|
---|
294 | It might be difficult to fix, or fixing it might break compatibility.
|
---|
295 | Often, reports get a low priority when there is a simple work-around.
|
---|
296 | In particular, bugs caused by invalid code have a simple work-around:
|
---|
297 | <em>fix the code</em>.</p>
|
---|
298 |
|
---|
299 | <hr />
|
---|
300 |
|
---|
301 | <h2><a name="cxx">C++</a></h2>
|
---|
302 |
|
---|
303 | <h3><a name="missing">Missing features</a></h3>
|
---|
304 |
|
---|
305 | <dl>
|
---|
306 |
|
---|
307 | <dt>The <code>export</code> keyword is not implemented.</dt>
|
---|
308 | <dd><p>Most C++ compilers (G++ included) do not yet implement
|
---|
309 | <code>export</code>, which is necessary for separate compilation of
|
---|
310 | template declarations and definitions. Without <code>export</code>, a
|
---|
311 | template definition must be in scope to be used. The obvious
|
---|
312 | workaround is simply to place all definitions in the header
|
---|
313 | itself. Alternatively, the compilation unit containing template
|
---|
314 | definitions may be included from the header.</p></dd>
|
---|
315 |
|
---|
316 | </dl>
|
---|
317 |
|
---|
318 | <h3><a name="fixed34">Bugs fixed in the 3.4 series</a></h3>
|
---|
319 |
|
---|
320 | <p>The following bugs are present up to (and including) GCC 3.3.x.
|
---|
321 | They have been fixed in 3.4.0.</p>
|
---|
322 |
|
---|
323 | <dl>
|
---|
324 |
|
---|
325 | <dt>Two-stage name-lookup.</dt>
|
---|
326 |
|
---|
327 | <dd><p>GCC did not implement two-stage name-lookup (also see
|
---|
328 | <a href="#new34">below</a>).</p></dd>
|
---|
329 |
|
---|
330 | <dt>Covariant return types.</dt>
|
---|
331 |
|
---|
332 | <dd><p>GCC did not implement non-trivial covariant returns.</p></dd>
|
---|
333 |
|
---|
334 | <dt>Parse errors for "simple" code.</dt>
|
---|
335 |
|
---|
336 | <dd><p>GCC gave parse errors for seemingly simple code, such as</p>
|
---|
337 |
|
---|
338 | <blockquote><pre>
|
---|
339 | struct A
|
---|
340 | {
|
---|
341 | A();
|
---|
342 | A(int);
|
---|
343 | };
|
---|
344 |
|
---|
345 | struct B
|
---|
346 | {
|
---|
347 | B(A);
|
---|
348 | B(A,A);
|
---|
349 | void foo();
|
---|
350 | };
|
---|
351 |
|
---|
352 | A bar()
|
---|
353 | {
|
---|
354 | B b(A(),A(1)); // Variable b, initialized with two temporaries
|
---|
355 | B(A(2)).foo(); // B temporary, initialized with A temporary
|
---|
356 | return (A()); // return A temporary
|
---|
357 | }
|
---|
358 | </pre></blockquote>
|
---|
359 |
|
---|
360 | <p>Although being valid code, each of the three lines with a comment was
|
---|
361 | rejected by GCC. The work-arounds for older compiler versions proposed
|
---|
362 | below do not change the semantics of the programs at all.</p>
|
---|
363 |
|
---|
364 | <p>The problem in the first case was that GCC started to parse the
|
---|
365 | declaration of <code>b</code> as a function called <code>b</code> returning
|
---|
366 | <code>B</code>, taking a function returning <code>A</code> as an argument.
|
---|
367 | When it encountered the <code>1</code>, it was too late. To show the
|
---|
368 | compiler that this should be really an expression, a comma operator with
|
---|
369 | a dummy argument could be used:</p>
|
---|
370 |
|
---|
371 | <blockquote><pre>
|
---|
372 | B b((0,A()),A(1));
|
---|
373 | </pre></blockquote>
|
---|
374 |
|
---|
375 | <p>The work-around for simpler cases like the second one was to add
|
---|
376 | additional parentheses around the expressions that were mistaken as
|
---|
377 | declarations:</p>
|
---|
378 |
|
---|
379 | <blockquote><pre>
|
---|
380 | (B(A(2))).foo();
|
---|
381 | </pre></blockquote>
|
---|
382 |
|
---|
383 | <p>In the third case, however, additional parentheses were causing
|
---|
384 | the problems: The compiler interpreted <code>A()</code> as a function
|
---|
385 | (taking no arguments, returning <code>A</code>), and <code>(A())</code>
|
---|
386 | as a cast lacking an expression to be casted, hence the parse error.
|
---|
387 | The work-around was to omit the parentheses:</p>
|
---|
388 |
|
---|
389 | <blockquote><pre>
|
---|
390 | return A();
|
---|
391 | </pre></blockquote>
|
---|
392 |
|
---|
393 | <p>This problem occured in a number of variants; in <code>throw</code>
|
---|
394 | statements, people also frequently put the object in parentheses.</p></dd>
|
---|
395 |
|
---|
396 | </dl>
|
---|
397 |
|
---|
398 | <hr />
|
---|
399 |
|
---|
400 | <h2><a name="fortran">Fortran</a></h2>
|
---|
401 |
|
---|
402 | <p>Fortran bugs are documented in the G77 manual rather than
|
---|
403 | explicitly listed here. Please see
|
---|
404 | <a href="http://gcc.gnu.org/onlinedocs/g77/Trouble.html">Known Causes of
|
---|
405 | Trouble with GNU Fortran</a> in the G77 manual.</p>
|
---|
406 |
|
---|
407 | <hr />
|
---|
408 |
|
---|
409 | <h1><a name="nonbugs">Non-bugs</a></h1>
|
---|
410 |
|
---|
411 | <p>The following are not actually bugs, but are reported often
|
---|
412 | enough to warrant a mention here.</p>
|
---|
413 |
|
---|
414 | <p>It is not always a bug in the compiler, if code which "worked" in a
|
---|
415 | previous version, is now rejected. Earlier versions of GCC sometimes were
|
---|
416 | less picky about standard conformance and accepted invalid source code.
|
---|
417 | In addition, programming languages themselves change, rendering code
|
---|
418 | invalid that used to be conforming (this holds especially for C++).
|
---|
419 | In either case, you should update your code to match recent language
|
---|
420 | standards.</p>
|
---|
421 |
|
---|
422 | <hr />
|
---|
423 |
|
---|
424 | <h2><a name="nonbugs_general">General</a></h2>
|
---|
425 |
|
---|
426 | <dl>
|
---|
427 | <dt>Problems with floating point numbers - the
|
---|
428 | <a href="http://gcc.gnu.org/PR323">most often reported non-bug</a>.</dt>
|
---|
429 | <dd><p>In a number of cases, GCC appears to perform floating point
|
---|
430 | computations incorrectly. For example, the C++ program</p>
|
---|
431 | <blockquote><pre>
|
---|
432 | #include <iostream>
|
---|
433 |
|
---|
434 | int main()
|
---|
435 | {
|
---|
436 | double a = 0.5;
|
---|
437 | double b = 0.01;
|
---|
438 | std::cout << (int)(a / b) << std::endl;
|
---|
439 | return 0;
|
---|
440 | }
|
---|
441 | </pre></blockquote>
|
---|
442 | <p>might print 50 on some systems and optimization levels, and 49 on
|
---|
443 | others.</p>
|
---|
444 |
|
---|
445 | <p>This is the result of <em>rounding</em>: The computer cannot
|
---|
446 | represent all real numbers exactly, so it has to use
|
---|
447 | approximations. When computing with approximation, the computer needs
|
---|
448 | to round to the nearest representable number.</p>
|
---|
449 |
|
---|
450 | <p>This is not a bug in the compiler, but an inherent limitation of
|
---|
451 | the floating point types. Please study
|
---|
452 | <a href="http://www.validlab.com/goldberg/paper.ps">this paper</a>
|
---|
453 | for more information.</p></dd>
|
---|
454 | </dl>
|
---|
455 |
|
---|
456 | <hr />
|
---|
457 |
|
---|
458 | <h2><a name="nonbugs_c">C</a></h2>
|
---|
459 |
|
---|
460 | <dl>
|
---|
461 | <dt>Casting does not work as expected when optimization is turned on.</dt>
|
---|
462 |
|
---|
463 | <dd><p>This is often caused by a violation of aliasing rules, which are part
|
---|
464 | of the ISO C standard. These rules say that a program is invalid if you try
|
---|
465 | to access a variable through a pointer of an incompatible type. This is
|
---|
466 | happening in the following example where a short is accessed through a
|
---|
467 | pointer to integer (the code assumes 16-bit <code>short</code>s and 32-bit
|
---|
468 | <code>int</code>s):</p>
|
---|
469 | <blockquote><pre>
|
---|
470 | #include <stdio.h>
|
---|
471 |
|
---|
472 | int main()
|
---|
473 | {
|
---|
474 | short a[2];
|
---|
475 |
|
---|
476 | a[0]=0x1111;
|
---|
477 | a[1]=0x1111;
|
---|
478 |
|
---|
479 | *(int *)a = 0x22222222; /* violation of aliasing rules */
|
---|
480 |
|
---|
481 | printf("%x %x\n", a[0], a[1]);
|
---|
482 | return 0;
|
---|
483 | }
|
---|
484 | </pre></blockquote>
|
---|
485 | <p>The aliasing rules were designed to allow compilers more aggressive
|
---|
486 | optimization. Basically, a compiler can assume that all changes to variables
|
---|
487 | happen through pointers or references to variables of a type compatible to
|
---|
488 | the accessed variable. Dereferencing a pointer that violates the aliasing
|
---|
489 | rules results in undefined behavior.</p>
|
---|
490 |
|
---|
491 | <p>In the case above, the compiler may assume that no access through an
|
---|
492 | integer pointer can change the array <code>a</code>, consisting of shorts.
|
---|
493 | Thus, <code>printf</code> may be called with the original values of
|
---|
494 | <code>a[0]</code> and <code>a[1]</code>. What really happens is up to
|
---|
495 | the compiler and may change with architecture and optimization level.</p>
|
---|
496 |
|
---|
497 | <p>Recent versions of GCC turn on the option <code>-fstrict-aliasing</code>
|
---|
498 | (which allows alias-based optimizations) by default with <code>-O2</code>.
|
---|
499 | And some architectures then really print "1111 1111" as result. Without
|
---|
500 | optimization the executable will generate the "expected" output
|
---|
501 | "2222 2222".</p>
|
---|
502 |
|
---|
503 | <p>To disable optimizations based on alias-analysis for faulty legacy code,
|
---|
504 | the option <code>-fno-strict-aliasing</code> can be used as a work-around.</p>
|
---|
505 |
|
---|
506 | <p>The option <code>-Wstrict-aliasing</code> (which is included in
|
---|
507 | <code>-Wall</code>) warns about some - but not all - cases of violation
|
---|
508 | of aliasing rules when <code>-fstrict-aliasing</code> is active.</p>
|
---|
509 |
|
---|
510 | <p>To fix the code above, you can use a <code>union</code> instead of a
|
---|
511 | cast (note that this is a GCC extension which might not work with other
|
---|
512 | compilers):</p>
|
---|
513 | <blockquote><pre>
|
---|
514 | #include <stdio.h>
|
---|
515 |
|
---|
516 | int main()
|
---|
517 | {
|
---|
518 | union
|
---|
519 | {
|
---|
520 | short a[2];
|
---|
521 | int i;
|
---|
522 | } u;
|
---|
523 |
|
---|
524 | u.a[0]=0x1111;
|
---|
525 | u.a[1]=0x1111;
|
---|
526 |
|
---|
527 | u.i = 0x22222222;
|
---|
528 |
|
---|
529 | printf("%x %x\n", u.a[0], u.a[1]);
|
---|
530 | return 0;
|
---|
531 | }
|
---|
532 | </pre></blockquote>
|
---|
533 | <p>Now the result will always be "2222 2222".</p>
|
---|
534 |
|
---|
535 | <p>For some more insight into the subject, please have a look at
|
---|
536 | <a href="http://mail-index.NetBSD.org/tech-kern/2003/08/11/0001.html">this
|
---|
537 | article</a>.</p></dd>
|
---|
538 |
|
---|
539 |
|
---|
540 | <dt>Cannot use preprocessor directive in macro arguments.</dt>
|
---|
541 | <dd><p>Let me guess... you used an older version of GCC to compile code
|
---|
542 | that looks something like this:</p>
|
---|
543 | <blockquote><pre>
|
---|
544 | memcpy(dest, src,
|
---|
545 | #ifdef PLATFORM1
|
---|
546 | 12
|
---|
547 | #else
|
---|
548 | 24
|
---|
549 | #endif
|
---|
550 | );
|
---|
551 | </pre></blockquote>
|
---|
552 | <p>and you got a whole pile of error messages:</p>
|
---|
553 | <blockquote><pre>
|
---|
554 | test.c:11: warning: preprocessing directive not recognized within macro arg
|
---|
555 | test.c:11: warning: preprocessing directive not recognized within macro arg
|
---|
556 | test.c:11: warning: preprocessing directive not recognized within macro arg
|
---|
557 | test.c: In function `foo':
|
---|
558 | test.c:6: undefined or invalid # directive
|
---|
559 | test.c:8: undefined or invalid # directive
|
---|
560 | test.c:9: parse error before `24'
|
---|
561 | test.c:10: undefined or invalid # directive
|
---|
562 | </pre></blockquote>
|
---|
563 |
|
---|
564 | <p>This is because your C library's <code><string.h></code> happens
|
---|
565 | to define <code>memcpy</code> as a macro - which is perfectly legitimate.
|
---|
566 | In recent versions of glibc, for example, <code>printf</code> is among those
|
---|
567 | functions which are implemented as macros.</p>
|
---|
568 |
|
---|
569 | <p>Versions of GCC prior to 3.3 did not allow you to put <code>#ifdef</code>
|
---|
570 | (or any other preprocessor directive) inside the arguments of a macro. The
|
---|
571 | code therefore would not compile.</p>
|
---|
572 |
|
---|
573 | <p>As of GCC 3.3 this kind of construct is always accepted and the
|
---|
574 | preprocessor will probably do what you expect, but see the manual for
|
---|
575 | detailed semantics.</p>
|
---|
576 |
|
---|
577 | <p>However, this kind of code is not portable. It is "undefined behavior"
|
---|
578 | according to the C standard; that means different compilers may do
|
---|
579 | different things with it. It is always possible to rewrite code which
|
---|
580 | uses conditionals inside macros so that it doesn't. You could write
|
---|
581 | the above example</p>
|
---|
582 | <blockquote><pre>
|
---|
583 | #ifdef PLATFORM1
|
---|
584 | memcpy(dest, src, 12);
|
---|
585 | #else
|
---|
586 | memcpy(dest, src, 24);
|
---|
587 | #endif
|
---|
588 | </pre></blockquote>
|
---|
589 | <p>This is a bit more typing, but I personally think it's better style
|
---|
590 | in addition to being more portable.</p></dd>
|
---|
591 |
|
---|
592 |
|
---|
593 | <dt>Cannot initialize a static variable with <code>stdin</code>.</dt>
|
---|
594 | <dd><p>This has nothing to do with GCC, but people ask us about it a
|
---|
595 | lot. Code like this:</p>
|
---|
596 |
|
---|
597 | <blockquote><pre>
|
---|
598 | #include <stdio.h>
|
---|
599 |
|
---|
600 | FILE *yyin = stdin;
|
---|
601 | </pre></blockquote>
|
---|
602 |
|
---|
603 | <p>will not compile with GNU libc, because <code>stdin</code> is not a
|
---|
604 | constant. This was done deliberately, to make it easier to maintain
|
---|
605 | binary compatibility when the type <code>FILE</code> needs to be changed.
|
---|
606 | It is surprising for people used to traditional Unix C libraries, but it
|
---|
607 | is permitted by the C standard.</p>
|
---|
608 |
|
---|
609 | <p>This construct commonly occurs in code generated by old versions of
|
---|
610 | lex or yacc. We suggest you try regenerating the parser with a
|
---|
611 | current version of flex or bison, respectively. In your own code, the
|
---|
612 | appropriate fix is to move the initialization to the beginning of
|
---|
613 | main.</p>
|
---|
614 |
|
---|
615 | <p>There is a common misconception that the GCC developers are
|
---|
616 | responsible for GNU libc. These are in fact two entirely separate
|
---|
617 | projects; please check the
|
---|
618 | <a href="http://www.gnu.org/software/libc/">GNU libc web pages</a>
|
---|
619 | for details.
|
---|
620 | </p></dd>
|
---|
621 | </dl>
|
---|
622 |
|
---|
623 | <hr />
|
---|
624 |
|
---|
625 | <h2><a name="nonbugs_cxx">C++</a></h2>
|
---|
626 |
|
---|
627 | <dl>
|
---|
628 | <dt>Nested classes can access private members and types of the containing
|
---|
629 | class.</dt>
|
---|
630 |
|
---|
631 | <dd><p>Defect report 45 clarifies that nested classes are members of the
|
---|
632 | class they are nested in, and so are granted access to private members of
|
---|
633 | that class.</p></dd>
|
---|
634 |
|
---|
635 | <dt>G++ emits two copies of constructors and destructors.</dt>
|
---|
636 |
|
---|
637 | <dd><p>In general there are <em>three</em> types of constructors (and
|
---|
638 | destructors).</p>
|
---|
639 | <ol>
|
---|
640 | <li>The complete object constructor/destructor.</li>
|
---|
641 | <li>The base object constructor/destructor.</li>
|
---|
642 | <li>The allocating constructor/deallocating destructor.</li>
|
---|
643 | </ol>
|
---|
644 | <p>The first two are different, when virtual base classes are involved.
|
---|
645 | </p></dd>
|
---|
646 |
|
---|
647 | <dt>Global destructors are not run in the correct order.</dt>
|
---|
648 |
|
---|
649 | <dd><p>Global destructors should be run in the reverse order of their
|
---|
650 | constructors <em>completing</em>. In most cases this is the same as
|
---|
651 | the reverse order of constructors <em>starting</em>, but sometimes it
|
---|
652 | is different, and that is important. You need to compile and link your
|
---|
653 | programs with <code>--use-cxa-atexit</code>. We have not turned this
|
---|
654 | switch on by default, as it requires a <code>cxa</code> aware runtime
|
---|
655 | library (<code>libc</code>, <code>glibc</code>, or equivalent).</p></dd>
|
---|
656 |
|
---|
657 | <dt>Classes in exception specifiers must be complete types.</dt>
|
---|
658 |
|
---|
659 | <dd><p>[15.4]/1 tells you that you cannot have an incomplete type, or
|
---|
660 | pointer to incomplete (other than <code><i>cv</i> void *</code>) in
|
---|
661 | an exception specification.</p></dd>
|
---|
662 |
|
---|
663 | <dt>Exceptions don't work in multithreaded applications.</dt>
|
---|
664 |
|
---|
665 | <dd><p>You need to rebuild g++ and libstdc++ with
|
---|
666 | <code>--enable-threads</code>. Remember, C++ exceptions are not like
|
---|
667 | hardware interrupts. You cannot throw an exception in one thread and
|
---|
668 | catch it in another. You cannot throw an exception from a signal
|
---|
669 | handler and catch it in the main thread.</p></dd>
|
---|
670 |
|
---|
671 | <dt>Templates, scoping, and digraphs.</dt>
|
---|
672 |
|
---|
673 | <dd><p>If you have a class in the global namespace, say named <code>X</code>,
|
---|
674 | and want to give it as a template argument to some other class, say
|
---|
675 | <code>std::vector</code>, then <code>std::vector<::X></code>
|
---|
676 | fails with a parser error.</p>
|
---|
677 |
|
---|
678 | <p>The reason is that the standard mandates that the sequence
|
---|
679 | <code><:</code> is treated as if it were the token <code>[</code>.
|
---|
680 | (There are several such combinations of characters - they are called
|
---|
681 | <em>digraphs</em>.) Depending on the version, the compiler then reports
|
---|
682 | a parse error before the character <code>:</code> (the colon before
|
---|
683 | <code>X</code>) or a missing closing bracket <code>]</code>.</p>
|
---|
684 |
|
---|
685 | <p>The simplest way to avoid this is to write <code>std::vector<
|
---|
686 | ::X></code>, i.e. place a space between the opening angle bracket
|
---|
687 | and the scope operator.</p></dd>
|
---|
688 |
|
---|
689 |
|
---|
690 | <dt><a name="cxx_rvalbind">Copy constructor access check while
|
---|
691 | initializing a reference.</a></dt>
|
---|
692 |
|
---|
693 | <dd><p>Consider this code:</p>
|
---|
694 |
|
---|
695 | <blockquote><pre>
|
---|
696 | class A
|
---|
697 | {
|
---|
698 | public:
|
---|
699 | A();
|
---|
700 |
|
---|
701 | private:
|
---|
702 | A(const A&); // private copy ctor
|
---|
703 | };
|
---|
704 |
|
---|
705 | A makeA(void);
|
---|
706 | void foo(const A&);
|
---|
707 |
|
---|
708 | void bar(void)
|
---|
709 | {
|
---|
710 | foo(A()); // error, copy ctor is not accessible
|
---|
711 | foo(makeA()); // error, copy ctor is not accessible
|
---|
712 |
|
---|
713 | A a1;
|
---|
714 | foo(a1); // OK, a1 is a lvalue
|
---|
715 | }</pre></blockquote>
|
---|
716 |
|
---|
717 | <p>Starting with GCC 3.4.0, binding an rvalue to a const reference requires
|
---|
718 | an accessible copy constructor. This might be surprising at first sight,
|
---|
719 | especially since most popular compilers do not correctly implement this
|
---|
720 | rule.</p>
|
---|
721 |
|
---|
722 | <p>The C++ Standard says that a temporary object should be created in
|
---|
723 | this context and its contents filled with a copy of the object we are
|
---|
724 | trying to bind to the reference; it also says that the temporary copy
|
---|
725 | can be elided, but the semantic constraints (eg. accessibility) of the
|
---|
726 | copy constructor still have to be checked.</p>
|
---|
727 |
|
---|
728 | <p>For further information, you can consult the following paragraphs of
|
---|
729 | the C++ standard: [dcl.init.ref]/5, bullet 2, sub-bullet 1, and
|
---|
730 | [class.temporary]/2.</p></dd>
|
---|
731 | </dl>
|
---|
732 |
|
---|
733 | <h3><a name="upgrading">Common problems when upgrading the compiler</a></h3>
|
---|
734 |
|
---|
735 | <h4>ABI changes</h4>
|
---|
736 |
|
---|
737 | <p>The C++ application binary interface (ABI) consists of two
|
---|
738 | components: the first defines how the elements of classes are laid
|
---|
739 | out, how functions are called, how function names are mangled, etc;
|
---|
740 | the second part deals with the internals of the objects in libstdc++.
|
---|
741 | Although we strive for a non-changing ABI, so far we have had to
|
---|
742 | modify it with each major release. If you change your compiler to a
|
---|
743 | different major release <em>you must recompile all libraries that
|
---|
744 | contain C++ code</em>. If you fail to do so you risk getting linker
|
---|
745 | errors or malfunctioning programs. Some of our Java support libraries
|
---|
746 | also contain C++ code, so you might want to recompile all libraries to
|
---|
747 | be safe. It should not be necessary to recompile if you have changed
|
---|
748 | to a bug-fix release of the same version of the compiler; bug-fix
|
---|
749 | releases are careful to avoid ABI changes. See also the
|
---|
750 | <a href="onlinedocs/gcc/Compatibility.html">compatibility section</a>
|
---|
751 | of the GCC manual.</p>
|
---|
752 |
|
---|
753 | <p>Remark: A major release is designated by a change to the first or second
|
---|
754 | component of the two- or three-part version number. A minor (bug-fix)
|
---|
755 | release is designated by a change to the third component only. Thus GCC
|
---|
756 | 3.2 and 3.3 are major releases, while 3.3.1 and 3.3.2 are bug-fix releases
|
---|
757 | for GCC 3.3. With the 3.4 series we are introducing a new naming scheme;
|
---|
758 | the first release of this series is 3.4.0 instead of just 3.4.</p>
|
---|
759 |
|
---|
760 | <h4>Standard conformance</h4>
|
---|
761 |
|
---|
762 | <p>With each release, we try to make G++ conform closer to the ISO C++ standard
|
---|
763 | (available at
|
---|
764 | <a href="http://www.ncits.org/cplusplus.htm">http://www.ncits.org/cplusplus.htm</a>).
|
---|
765 | We have also implemented some of the core and library defect reports
|
---|
766 | (available at
|
---|
767 | <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html">http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html</a>
|
---|
768 | &
|
---|
769 | <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html">http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html</a>
|
---|
770 | respectively).</p>
|
---|
771 |
|
---|
772 | <p>Non-conforming legacy code that worked with older versions of GCC may be
|
---|
773 | rejected by more recent compilers. There is no command-line switch to ensure
|
---|
774 | compatibility in general, because trying to parse standard-conforming and
|
---|
775 | old-style code at the same time would render the C++ frontend unmaintainable.
|
---|
776 | However, some non-conforming constructs are allowed when the command-line
|
---|
777 | option <code>-fpermissive</code> is used.</p>
|
---|
778 |
|
---|
779 | <p>Two milestones in standard conformance are GCC 3.0 (including a major
|
---|
780 | overhaul of the standard library) and the 3.4.0 version (with its new C++
|
---|
781 | parser).</p>
|
---|
782 |
|
---|
783 | <h4>New in GCC 3.0</h4>
|
---|
784 |
|
---|
785 | <ul>
|
---|
786 |
|
---|
787 | <li>The standard library is much more conformant, and uses the
|
---|
788 | <code>std::</code> namespace (which is now a real namespace, not an
|
---|
789 | alias for <code>::</code>).</li>
|
---|
790 |
|
---|
791 | <li>The standard header files for the c library don't end with
|
---|
792 | <code>.h</code>, but begin with <code>c</code> (i.e.
|
---|
793 | <code><cstdlib></code> rather than <code><stdlib.h></code>).
|
---|
794 | The <code>.h</code> names are still available, but are deprecated.</li>
|
---|
795 |
|
---|
796 | <li><code><strstream></code> is deprecated, use
|
---|
797 | <code><sstream></code> instead.</li>
|
---|
798 |
|
---|
799 | <li><code>streambuf::seekoff</code> &
|
---|
800 | <code>streambuf::seekpos</code> are private, instead use
|
---|
801 | <code>streambuf::pubseekoff</code> &
|
---|
802 | <code>streambuf::pubseekpos</code> respectively.</li>
|
---|
803 |
|
---|
804 | <li>If <code>std::operator << (std::ostream &, long long)</code>
|
---|
805 | doesn't exist, you need to recompile libstdc++ with
|
---|
806 | <code>--enable-long-long</code>.</li>
|
---|
807 |
|
---|
808 | </ul>
|
---|
809 |
|
---|
810 | <p>If you get lots of errors about things like <code>cout</code> not being
|
---|
811 | found, you've most likely forgotten to tell the compiler to look in the
|
---|
812 | <code>std::</code> namespace. There are several ways to do this:</p>
|
---|
813 |
|
---|
814 | <ul>
|
---|
815 |
|
---|
816 | <li>Say <code>std::cout</code> at the call. This is the most explicit
|
---|
817 | way of saying what you mean.</li>
|
---|
818 |
|
---|
819 | <li>Say <code>using std::cout;</code> somewhere before the call. You
|
---|
820 | will need to do this for each function or type you wish to use from the
|
---|
821 | standard library.</li>
|
---|
822 |
|
---|
823 | <li>Say <code>using namespace std;</code> somewhere before the call.
|
---|
824 | This is the quick-but-dirty fix. This brings the <em>whole</em> of the
|
---|
825 | <code>std::</code> namespace into scope. <em>Never</em> do this in a
|
---|
826 | header file, as every user of your header file will be affected by this
|
---|
827 | decision.</li>
|
---|
828 |
|
---|
829 | </ul>
|
---|
830 |
|
---|
831 | <h4><a name="new34">New in GCC 3.4.0</a></h4>
|
---|
832 |
|
---|
833 | <p>The new parser brings a lot of improvements, especially concerning
|
---|
834 | name-lookup.</p>
|
---|
835 |
|
---|
836 | <ul>
|
---|
837 |
|
---|
838 | <li>The "implicit typename" extension got removed (it was already deprecated
|
---|
839 | since GCC 3.1), so that the following code is now rejected, see [14.6]:
|
---|
840 | <blockquote><pre>
|
---|
841 | template <typename> struct A
|
---|
842 | {
|
---|
843 | typedef int X;
|
---|
844 | };
|
---|
845 |
|
---|
846 | template <typename T> struct B
|
---|
847 | {
|
---|
848 | A<T>::X x; // error
|
---|
849 | typename A<T>::X y; // OK
|
---|
850 | };
|
---|
851 |
|
---|
852 | B<void> b;
|
---|
853 | </pre></blockquote></li>
|
---|
854 |
|
---|
855 | <li>For similar reasons, the following code now requires the
|
---|
856 | <code>template</code> keyword, see [14.2]:
|
---|
857 | <blockquote><pre>
|
---|
858 | template <typename> struct A
|
---|
859 | {
|
---|
860 | template <int> struct X {};
|
---|
861 | };
|
---|
862 |
|
---|
863 | template <typename T> struct B
|
---|
864 | {
|
---|
865 | typename A<T>::X<0> x; // error
|
---|
866 | typename A<T>::template X<0> y; // OK
|
---|
867 | };
|
---|
868 |
|
---|
869 | B<void> b;
|
---|
870 | </pre></blockquote></li>
|
---|
871 |
|
---|
872 | <li>We now have two-stage name-lookup, so that the following code is
|
---|
873 | rejected, see [14.6]/9:
|
---|
874 | <blockquote><pre>
|
---|
875 | template <typename T> int foo()
|
---|
876 | {
|
---|
877 | return i; // error
|
---|
878 | }
|
---|
879 | </pre></blockquote></li>
|
---|
880 |
|
---|
881 | <li>This also affects members of base classes, see [14.6.2]:
|
---|
882 | <blockquote><pre>
|
---|
883 | template <typename> struct A
|
---|
884 | {
|
---|
885 | int i, j;
|
---|
886 | };
|
---|
887 |
|
---|
888 | template <typename T> struct B : A<T>
|
---|
889 | {
|
---|
890 | int foo1() { return i; } // error
|
---|
891 | int foo2() { return this->i; } // OK
|
---|
892 | int foo3() { return B<T>::i; } // OK
|
---|
893 | int foo4() { return A<T>::i; } // OK
|
---|
894 |
|
---|
895 | using A<T>::j;
|
---|
896 | int foo5() { return j; } // OK
|
---|
897 | };
|
---|
898 | </pre></blockquote></li>
|
---|
899 |
|
---|
900 | </ul>
|
---|
901 |
|
---|
902 | <p>In addition to the problems listed above, the manual contains a section on
|
---|
903 | <a href="http://gcc.gnu.org/onlinedocs/gcc/C---Misunderstandings.html">Common
|
---|
904 | Misunderstandings with GNU C++</a>.</p>
|
---|
905 |
|
---|
906 | </body>
|
---|
907 | </html>
|
---|