Non_002dbugs.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>Using the GNU Compiler Collection (GCC): Non-bugs</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Non-bugs">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Non-bugs">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Trouble.html#Trouble" rel="up" title="Trouble">
  30. <link href="Warnings-and-Errors.html#Warnings-and-Errors" rel="next" title="Warnings and Errors">
  31. <link href="Copy-Assignment.html#Copy-Assignment" rel="prev" title="Copy Assignment">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Non_002dbugs"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Warnings-and-Errors.html#Warnings-and-Errors" accesskey="n" rel="next">Warnings and Errors</a>, Previous: <a href="C_002b_002b-Misunderstandings.html#C_002b_002b-Misunderstandings" accesskey="p" rel="prev">C++ Misunderstandings</a>, Up: <a href="Trouble.html#Trouble" accesskey="u" rel="up">Trouble</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Certain-Changes-We-Don_0027t-Want-to-Make"></a>
  69. <h3 class="section">13.8 Certain Changes We Don&rsquo;t Want to Make</h3>
  70. <p>This section lists changes that people frequently request, but which
  71. we do not make because we think GCC is better without them.
  72. </p>
  73. <ul>
  74. <li> Checking the number and type of arguments to a function which has an
  75. old-fashioned definition and no prototype.
  76. <p>Such a feature would work only occasionally&mdash;only for calls that appear
  77. in the same file as the called function, following the definition. The
  78. only way to check all calls reliably is to add a prototype for the
  79. function. But adding a prototype eliminates the motivation for this
  80. feature. So the feature is not worthwhile.
  81. </p>
  82. </li><li> Warning about using an expression whose type is signed as a shift count.
  83. <p>Shift count operands are probably signed more often than unsigned.
  84. Warning about this would cause far more annoyance than good.
  85. </p>
  86. </li><li> Warning about assigning a signed value to an unsigned variable.
  87. <p>Such assignments must be very common; warning about them would cause
  88. more annoyance than good.
  89. </p>
  90. </li><li> Warning when a non-void function value is ignored.
  91. <p>C contains many standard functions that return a value that most
  92. programs choose to ignore. One obvious example is <code>printf</code>.
  93. Warning about this practice only leads the defensive programmer to
  94. clutter programs with dozens of casts to <code>void</code>. Such casts are
  95. required so frequently that they become visual noise. Writing those
  96. casts becomes so automatic that they no longer convey useful
  97. information about the intentions of the programmer. For functions
  98. where the return value should never be ignored, use the
  99. <code>warn_unused_result</code> function attribute (see <a href="Function-Attributes.html#Function-Attributes">Function Attributes</a>).
  100. </p>
  101. </li><li> <a name="index-fshort_002denums-3"></a>
  102. Making <samp>-fshort-enums</samp> the default.
  103. <p>This would cause storage layout to be incompatible with most other C
  104. compilers. And it doesn&rsquo;t seem very important, given that you can get
  105. the same result in other ways. The case where it matters most is when
  106. the enumeration-valued object is inside a structure, and in that case
  107. you can specify a field width explicitly.
  108. </p>
  109. </li><li> Making bit-fields unsigned by default on particular machines where &ldquo;the
  110. ABI standard&rdquo; says to do so.
  111. <p>The ISO C standard leaves it up to the implementation whether a bit-field
  112. declared plain <code>int</code> is signed or not. This in effect creates two
  113. alternative dialects of C.
  114. </p>
  115. <a name="index-fsigned_002dbitfields-1"></a>
  116. <a name="index-funsigned_002dbitfields-2"></a>
  117. <p>The GNU C compiler supports both dialects; you can specify the signed
  118. dialect with <samp>-fsigned-bitfields</samp> and the unsigned dialect with
  119. <samp>-funsigned-bitfields</samp>. However, this leaves open the question of
  120. which dialect to use by default.
  121. </p>
  122. <p>Currently, the preferred dialect makes plain bit-fields signed, because
  123. this is simplest. Since <code>int</code> is the same as <code>signed int</code> in
  124. every other context, it is cleanest for them to be the same in bit-fields
  125. as well.
  126. </p>
  127. <p>Some computer manufacturers have published Application Binary Interface
  128. standards which specify that plain bit-fields should be unsigned. It is
  129. a mistake, however, to say anything about this issue in an ABI. This is
  130. because the handling of plain bit-fields distinguishes two dialects of C.
  131. Both dialects are meaningful on every type of machine. Whether a
  132. particular object file was compiled using signed bit-fields or unsigned
  133. is of no concern to other object files, even if they access the same
  134. bit-fields in the same data structures.
  135. </p>
  136. <p>A given program is written in one or the other of these two dialects.
  137. The program stands a chance to work on most any machine if it is
  138. compiled with the proper dialect. It is unlikely to work at all if
  139. compiled with the wrong dialect.
  140. </p>
  141. <p>Many users appreciate the GNU C compiler because it provides an
  142. environment that is uniform across machines. These users would be
  143. inconvenienced if the compiler treated plain bit-fields differently on
  144. certain machines.
  145. </p>
  146. <p>Occasionally users write programs intended only for a particular machine
  147. type. On these occasions, the users would benefit if the GNU C compiler
  148. were to support by default the same dialect as the other compilers on
  149. that machine. But such applications are rare. And users writing a
  150. program to run on more than one type of machine cannot possibly benefit
  151. from this kind of compatibility.
  152. </p>
  153. <p>This is why GCC does and will treat plain bit-fields in the same
  154. fashion on all types of machines (by default).
  155. </p>
  156. <p>There are some arguments for making bit-fields unsigned by default on all
  157. machines. If, for example, this becomes a universal de facto standard,
  158. it would make sense for GCC to go along with it. This is something
  159. to be considered in the future.
  160. </p>
  161. <p>(Of course, users strongly concerned about portability should indicate
  162. explicitly in each bit-field whether it is signed or not. In this way,
  163. they write programs which have the same meaning in both C dialects.)
  164. </p>
  165. </li><li> <a name="index-ansi-3"></a>
  166. <a name="index-std-3"></a>
  167. Undefining <code>__STDC__</code> when <samp>-ansi</samp> is not used.
  168. <p>Currently, GCC defines <code>__STDC__</code> unconditionally. This provides
  169. good results in practice.
  170. </p>
  171. <p>Programmers normally use conditionals on <code>__STDC__</code> to ask whether
  172. it is safe to use certain features of ISO C, such as function
  173. prototypes or ISO token concatenation. Since plain <code>gcc</code> supports
  174. all the features of ISO C, the correct answer to these questions is
  175. &ldquo;yes&rdquo;.
  176. </p>
  177. <p>Some users try to use <code>__STDC__</code> to check for the availability of
  178. certain library facilities. This is actually incorrect usage in an ISO
  179. C program, because the ISO C standard says that a conforming
  180. freestanding implementation should define <code>__STDC__</code> even though it
  181. does not have the library facilities. &lsquo;<samp>gcc -ansi -pedantic</samp>&rsquo; is a
  182. conforming freestanding implementation, and it is therefore required to
  183. define <code>__STDC__</code>, even though it does not come with an ISO C
  184. library.
  185. </p>
  186. <p>Sometimes people say that defining <code>__STDC__</code> in a compiler that
  187. does not completely conform to the ISO C standard somehow violates the
  188. standard. This is illogical. The standard is a standard for compilers
  189. that claim to support ISO C, such as &lsquo;<samp>gcc -ansi</samp>&rsquo;&mdash;not for other
  190. compilers such as plain <code>gcc</code>. Whatever the ISO C standard says
  191. is relevant to the design of plain <code>gcc</code> without <samp>-ansi</samp> only
  192. for pragmatic reasons, not as a requirement.
  193. </p>
  194. <p>GCC normally defines <code>__STDC__</code> to be 1, and in addition
  195. defines <code>__STRICT_ANSI__</code> if you specify the <samp>-ansi</samp> option,
  196. or a <samp>-std</samp> option for strict conformance to some version of ISO C.
  197. On some hosts, system include files use a different convention, where
  198. <code>__STDC__</code> is normally 0, but is 1 if the user specifies strict
  199. conformance to the C Standard. GCC follows the host convention when
  200. processing system include files, but when processing user files it follows
  201. the usual GNU C convention.
  202. </p>
  203. </li><li> Undefining <code>__STDC__</code> in C++.
  204. <p>Programs written to compile with C++-to-C translators get the
  205. value of <code>__STDC__</code> that goes with the C compiler that is
  206. subsequently used. These programs must test <code>__STDC__</code>
  207. to determine what kind of C preprocessor that compiler uses:
  208. whether they should concatenate tokens in the ISO C fashion
  209. or in the traditional fashion.
  210. </p>
  211. <p>These programs work properly with GNU C++ if <code>__STDC__</code> is defined.
  212. They would not work otherwise.
  213. </p>
  214. <p>In addition, many header files are written to provide prototypes in ISO
  215. C but not in traditional C. Many of these header files can work without
  216. change in C++ provided <code>__STDC__</code> is defined. If <code>__STDC__</code>
  217. is not defined, they will all fail, and will all need to be changed to
  218. test explicitly for C++ as well.
  219. </p>
  220. </li><li> Deleting &ldquo;empty&rdquo; loops.
  221. <p>Historically, GCC has not deleted &ldquo;empty&rdquo; loops under the
  222. assumption that the most likely reason you would put one in a program is
  223. to have a delay, so deleting them will not make real programs run any
  224. faster.
  225. </p>
  226. <p>However, the rationale here is that optimization of a nonempty loop
  227. cannot produce an empty one. This held for carefully written C compiled
  228. with less powerful optimizers but is not always the case for carefully
  229. written C++ or with more powerful optimizers.
  230. Thus GCC will remove operations from loops whenever it can determine
  231. those operations are not externally visible (apart from the time taken
  232. to execute them, of course). In case the loop can be proved to be finite,
  233. GCC will also remove the loop itself.
  234. </p>
  235. <p>Be aware of this when performing timing tests, for instance the
  236. following loop can be completely removed, provided
  237. <code>some_expression</code> can provably not change any global state.
  238. </p>
  239. <div class="smallexample">
  240. <pre class="smallexample">{
  241. int sum = 0;
  242. int ix;
  243. for (ix = 0; ix != 10000; ix++)
  244. sum += some_expression;
  245. }
  246. </pre></div>
  247. <p>Even though <code>sum</code> is accumulated in the loop, no use is made of
  248. that summation, so the accumulation can be removed.
  249. </p>
  250. </li><li> Making side effects happen in the same order as in some other compiler.
  251. <a name="index-side-effects_002c-order-of-evaluation"></a>
  252. <a name="index-order-of-evaluation_002c-side-effects"></a>
  253. <p>It is never safe to depend on the order of evaluation of side effects.
  254. For example, a function call like this may very well behave differently
  255. from one compiler to another:
  256. </p>
  257. <div class="smallexample">
  258. <pre class="smallexample">void func (int, int);
  259. int i = 2;
  260. func (i++, i++);
  261. </pre></div>
  262. <p>There is no guarantee (in either the C or the C++ standard language
  263. definitions) that the increments will be evaluated in any particular
  264. order. Either increment might happen first. <code>func</code> might get the
  265. arguments &lsquo;<samp>2, 3</samp>&rsquo;, or it might get &lsquo;<samp>3, 2</samp>&rsquo;, or even &lsquo;<samp>2, 2</samp>&rsquo;.
  266. </p>
  267. </li><li> Making certain warnings into errors by default.
  268. <p>Some ISO C testsuites report failure when the compiler does not produce
  269. an error message for a certain program.
  270. </p>
  271. <a name="index-pedantic_002derrors-2"></a>
  272. <p>ISO C requires a &ldquo;diagnostic&rdquo; message for certain kinds of invalid
  273. programs, but a warning is defined by GCC to count as a diagnostic. If
  274. GCC produces a warning but not an error, that is correct ISO C support.
  275. If testsuites call this &ldquo;failure&rdquo;, they should be run with the GCC
  276. option <samp>-pedantic-errors</samp>, which will turn these warnings into
  277. errors.
  278. </p>
  279. </li></ul>
  280. <hr>
  281. <div class="header">
  282. <p>
  283. Next: <a href="Warnings-and-Errors.html#Warnings-and-Errors" accesskey="n" rel="next">Warnings and Errors</a>, Previous: <a href="C_002b_002b-Misunderstandings.html#C_002b_002b-Misunderstandings" accesskey="p" rel="prev">C++ Misunderstandings</a>, Up: <a href="Trouble.html#Trouble" accesskey="u" rel="up">Trouble</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  284. </div>
  285. </body>
  286. </html>