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