You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Guidelines-for-Diagnostics.html 25KB

пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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-2020 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 6.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Guidelines for Diagnostics (GNU Compiler Collection (GCC) Internals)</title>
  21. <meta name="description" content="Guidelines for Diagnostics (GNU Compiler Collection (GCC) Internals)">
  22. <meta name="keywords" content="Guidelines for Diagnostics (GNU Compiler Collection (GCC) Internals)">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  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="User-Experience-Guidelines.html#User-Experience-Guidelines" rel="up" title="User Experience Guidelines">
  30. <link href="Guidelines-for-Options.html#Guidelines-for-Options" rel="next" title="Guidelines for Options">
  31. <link href="User-Experience-Guidelines.html#User-Experience-Guidelines" rel="prev" title="User Experience Guidelines">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="Guidelines-for-Diagnostics"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="Guidelines-for-Options.html#Guidelines-for-Options" accesskey="n" rel="next">Guidelines for Options</a>, Up: <a href="User-Experience-Guidelines.html#User-Experience-Guidelines" accesskey="u" rel="up">User Experience Guidelines</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>
  65. </div>
  66. <hr>
  67. <a name="Guidelines-for-Diagnostics-1"></a>
  68. <h3 class="section">28.1 Guidelines for Diagnostics</h3>
  69. <a name="index-guidelines-for-diagnostics"></a>
  70. <a name="index-diagnostics_002c-guidelines-for"></a>
  71. <a name="Talk-in-terms-of-the-user_0027s-code"></a>
  72. <h4 class="subsection">28.1.1 Talk in terms of the user&rsquo;s code</h4>
  73. <p>Diagnostics should be worded in terms of the user&rsquo;s source code, and the
  74. source language, rather than GCC&rsquo;s own implementation details.
  75. </p>
  76. <a name="Diagnostics-are-actionable"></a>
  77. <h4 class="subsection">28.1.2 Diagnostics are actionable</h4>
  78. <a name="index-diagnostics_002c-actionable"></a>
  79. <p>A good diagnostic is <em>actionable</em>: it should assist the user in
  80. taking action.
  81. </p>
  82. <p>Consider what an end user will want to do when encountering a diagnostic.
  83. </p>
  84. <p>Given an error, an end user will think: &ldquo;How do I fix this?&rdquo;
  85. </p>
  86. <p>Given a warning, an end user will think:
  87. </p>
  88. <ul>
  89. <li> &ldquo;Is this a real problem?&rdquo;
  90. </li><li> &ldquo;Do I care?&rdquo;
  91. </li><li> if they decide it&rsquo;s genuine: &ldquo;How do I fix this?&rdquo;
  92. </li></ul>
  93. <p>A good diagnostic provides pertinent information to allow the user to
  94. easily answer the above questions.
  95. </p>
  96. <a name="The-user_0027s-attention-is-important"></a>
  97. <h4 class="subsection">28.1.3 The user&rsquo;s attention is important</h4>
  98. <p>A perfect compiler would issue a warning on every aspect of the user&rsquo;s
  99. source code that ought to be fixed, and issue no other warnings.
  100. Naturally, this ideal is impossible to achieve.
  101. </p>
  102. <a name="index-signal_002dto_002dnoise-ratio-_0028metaphorical-usage-for-diagnostics_0029"></a>
  103. <a name="index-diagnostics_002c-false-positive"></a>
  104. <a name="index-diagnostics_002c-true-positive"></a>
  105. <a name="index-false-positive"></a>
  106. <a name="index-true-positive"></a>
  107. <p>Warnings should have a good <em>signal-to-noise ratio</em>: we should have few
  108. <em>false positives</em> (falsely issuing a warning when no warning is
  109. warranted) and few <em>false negatives</em> (failing to issue a warning when
  110. one <em>is</em> justified).
  111. </p>
  112. <p>Note that a false positive can mean, in practice, a warning that the
  113. user doesn&rsquo;t agree with. Ideally a diagnostic should contain enough
  114. information to allow the user to make an informed choice about whether
  115. they should care (and how to fix it), but a balance must be drawn against
  116. overloading the user with irrelevant data.
  117. </p>
  118. <a name="Precision-of-Wording"></a>
  119. <h4 class="subsection">28.1.4 Precision of Wording</h4>
  120. <p>Provide the user with details that allow them to identify what the
  121. problem is. For example, the vaguely-worded message:
  122. </p>
  123. <div class="smallexample">
  124. <pre class="smallexample">demo.c:1:1: warning: 'noinline' attribute ignored [-Wattributes]
  125. 1 | int foo __attribute__((noinline));
  126. | ^~~
  127. </pre></div>
  128. <p>doesn&rsquo;t tell the user why the attribute was ignored, or what kind of
  129. entity the compiler thought the attribute was being applied to (the
  130. source location for the diagnostic is also poor;
  131. see <a href="#input_005flocation_005fexample">discussion of <code>input_location</code></a>).
  132. A better message would be:
  133. </p>
  134. <div class="smallexample">
  135. <pre class="smallexample">demo.c:1:24: warning: attribute 'noinline' on variable 'foo' was
  136. ignored [-Wattributes]
  137. 1 | int foo __attribute__((noinline));
  138. | ~~~ ~~~~~~~~~~~~~~~^~~~~~~~~
  139. demo.c:1:24: note: attribute 'noinline' is only applicable to functions
  140. </pre></div>
  141. <p>which spells out the missing information (and fixes the location
  142. information, as discussed below).
  143. </p>
  144. <p>The above example uses a note to avoid a combinatorial explosion of possible
  145. messages.
  146. </p>
  147. <a name="Try-the-diagnostic-on-real_002dworld-code"></a>
  148. <h4 class="subsection">28.1.5 Try the diagnostic on real-world code</h4>
  149. <p>It&rsquo;s worth testing a new warning on many instances of real-world code,
  150. written by different people, and seeing what it complains about, and
  151. what it doesn&rsquo;t complain about.
  152. </p>
  153. <p>This may suggest heuristics that silence common false positives.
  154. </p>
  155. <p>It may also suggest ways to improve the precision of the message.
  156. </p>
  157. <a name="Make-mismatches-clear"></a>
  158. <h4 class="subsection">28.1.6 Make mismatches clear</h4>
  159. <p>Many diagnostics relate to a mismatch between two different places in the
  160. user&rsquo;s source code. Examples include:
  161. </p><ul>
  162. <li> a type mismatch, where the type at a usage site does not match the type
  163. at a declaration
  164. </li><li> the argument count at a call site does not match the parameter count
  165. at the declaration
  166. </li><li> something is erroneously duplicated (e.g. an error, due to breaking a
  167. uniqueness requirement, or a warning, if it&rsquo;s suggestive of a bug)
  168. </li><li> an &ldquo;opened&rdquo; syntactic construct (such as an open-parenthesis) is not
  169. closed
  170. </li></ul>
  171. <p>In each case, the diagnostic should indicate <strong>both</strong> pertinent
  172. locations (so that the user can easily see the problem and how to fix it).
  173. </p>
  174. <p>The standard way to do this is with a note (via <code>inform</code>). For
  175. example:
  176. </p>
  177. <div class="smallexample">
  178. <pre class="smallexample"> auto_diagnostic_group d;
  179. if (warning_at (loc, OPT_Wduplicated_cond,
  180. &quot;duplicated %&lt;if%&gt; condition&quot;))
  181. inform (EXPR_LOCATION (t), &quot;previously used here&quot;);
  182. </pre></div>
  183. <p>which leads to:
  184. </p>
  185. <div class="smallexample">
  186. <pre class="smallexample">demo.c: In function 'test':
  187. demo.c:5:17: warning: duplicated 'if' condition [-Wduplicated-cond]
  188. 5 | else if (flag &gt; 3)
  189. | ~~~~~^~~
  190. demo.c:3:12: note: previously used here
  191. 3 | if (flag &gt; 3)
  192. | ~~~~~^~~
  193. </pre></div>
  194. <p>The <code>inform</code> call should be guarded by the return value from the
  195. <code>warning_at</code> call so that the note isn&rsquo;t emitted when the warning
  196. is suppressed.
  197. </p>
  198. <p>For cases involving punctuation where the locations might be near
  199. each other, they can be conditionally consolidated via
  200. <code>gcc_rich_location::add_location_if_nearby</code>:
  201. </p>
  202. <div class="smallexample">
  203. <pre class="smallexample"> auto_diagnostic_group d;
  204. gcc_rich_location richloc (primary_loc);
  205. bool added secondary = richloc.add_location_if_nearby (secondary_loc);
  206. error_at (&amp;richloc, &quot;main message&quot;);
  207. if (!added secondary)
  208. inform (secondary_loc, &quot;message for secondary&quot;);
  209. </pre></div>
  210. <p>This will emit either one diagnostic with two locations:
  211. </p><div class="smallexample">
  212. <pre class="smallexample"> demo.c:42:10: error: main message
  213. (foo)
  214. ~ ^
  215. </pre></div>
  216. <p>or two diagnostics:
  217. </p>
  218. <div class="smallexample">
  219. <pre class="smallexample"> demo.c:42:4: error: main message
  220. foo)
  221. ^
  222. demo.c:40:2: note: message for secondary
  223. (
  224. ^
  225. </pre></div>
  226. <a name="Location-Information"></a>
  227. <h4 class="subsection">28.1.7 Location Information</h4>
  228. <a name="index-diagnostics_002c-locations"></a>
  229. <a name="index-location-information"></a>
  230. <a name="index-source-code_002c-location-information"></a>
  231. <a name="index-caret-1"></a>
  232. <p>GCC&rsquo;s <code>location_t</code> type can support both ordinary locations,
  233. and locations relating to a macro expansion.
  234. </p>
  235. <p>As of GCC 6, ordinary locations changed from supporting just a
  236. point in the user&rsquo;s source code to supporting three points: the
  237. <em>caret</em> location, plus a start and a finish:
  238. </p>
  239. <div class="smallexample">
  240. <pre class="smallexample"> a = foo &amp;&amp; bar;
  241. ~~~~^~~~~~
  242. | | |
  243. | | finish
  244. | caret
  245. start
  246. </pre></div>
  247. <p>Tokens coming out of libcpp have locations of the form <code>caret == start</code>,
  248. such as for <code>foo</code> here:
  249. </p>
  250. <div class="smallexample">
  251. <pre class="smallexample"> a = foo &amp;&amp; bar;
  252. ^~~
  253. | |
  254. | finish
  255. caret == start
  256. </pre></div>
  257. <p>Compound expressions should be reported using the location of the
  258. expression as a whole, rather than just of one token within it.
  259. </p>
  260. <p>For example, in <code>-Wformat</code>, rather than underlining just the first
  261. token of a bad argument:
  262. </p>
  263. <div class="smallexample">
  264. <pre class="smallexample"> printf(&quot;hello %i %s&quot;, (long)0, &quot;world&quot;);
  265. ~^ ~
  266. %li
  267. </pre></div>
  268. <p>the whole of the expression should be underlined, so that the user can
  269. easily identify what is being referred to:
  270. </p>
  271. <div class="smallexample">
  272. <pre class="smallexample"> printf(&quot;hello %i %s&quot;, (long)0, &quot;world&quot;);
  273. ~^ ~~~~~~~
  274. %li
  275. </pre></div>
  276. <p>Avoid using the <code>input_location</code> global, and the diagnostic functions
  277. that implicitly use it&mdash;use <code>error_at</code> and <code>warning_at</code> rather
  278. than <code>error</code> and <code>warning</code>, and provide the most appropriate
  279. <code>location_t</code> value available at that phase of the compilation. It&rsquo;s
  280. possible to supply secondary <code>location_t</code> values via
  281. <code>rich_location</code>.
  282. </p>
  283. <p><a name="input_005flocation_005fexample"></a>For example, in the example of imprecise wording above, generating the
  284. diagnostic using <code>warning</code>:
  285. </p>
  286. <div class="smallexample">
  287. <pre class="smallexample"> // BAD: implicitly uses <code>input_location</code>
  288. warning (OPT_Wattributes, &quot;%qE attribute ignored&quot;, name);
  289. </pre></div>
  290. <p>leads to:
  291. </p>
  292. <div class="smallexample">
  293. <pre class="smallexample">// BAD: uses <code>input_location</code>
  294. demo.c:1:1: warning: 'noinline' attribute ignored [-Wattributes]
  295. 1 | int foo __attribute__((noinline));
  296. | ^~~
  297. </pre></div>
  298. <p>which thus happened to use the location of the <code>int</code> token, rather
  299. than that of the attribute. Using <code>warning_at</code> with the location of
  300. the attribute, providing the location of the declaration in question
  301. as a secondary location, and adding a note:
  302. </p>
  303. <div class="smallexample">
  304. <pre class="smallexample"> auto_diagnostic_group d;
  305. gcc_rich_location richloc (attrib_loc);
  306. richloc.add_range (decl_loc);
  307. if (warning_at (OPT_Wattributes, &amp;richloc,
  308. &quot;attribute %qE on variable %qE was ignored&quot;, name))
  309. inform (attrib_loc, &quot;attribute %qE is only applicable to functions&quot;);
  310. </pre></div>
  311. <p>would lead to:
  312. </p>
  313. <div class="smallexample">
  314. <pre class="smallexample">// OK: use location of attribute, with a secondary location
  315. demo.c:1:24: warning: attribute 'noinline' on variable 'foo' was
  316. ignored [-Wattributes]
  317. 1 | int foo __attribute__((noinline));
  318. | ~~~ ~~~~~~~~~~~~~~~^~~~~~~~~
  319. demo.c:1:24: note: attribute 'noinline' is only applicable to functions
  320. </pre></div>
  321. <a name="Coding-Conventions"></a>
  322. <h4 class="subsection">28.1.8 Coding Conventions</h4>
  323. <p>See the <a href="https://gcc.gnu.org/codingconventions.html#Diagnostics">diagnostics section</a> of the GCC coding conventions.
  324. </p>
  325. <p>In the C++ front end, when comparing two types in a message, use &lsquo;<samp>%H</samp>&rsquo;
  326. and &lsquo;<samp>%I</samp>&rsquo; rather than &lsquo;<samp>%T</samp>&rsquo;, as this allows the diagnostics
  327. subsystem to highlight differences between template-based types.
  328. For example, rather than using &lsquo;<samp>%qT</samp>&rsquo;:
  329. </p>
  330. <div class="smallexample">
  331. <pre class="smallexample"> // BAD: a pair of %qT used in C++ front end for type comparison
  332. error_at (loc, &quot;could not convert %qE from %qT to %qT&quot;, expr,
  333. TREE_TYPE (expr), type);
  334. </pre></div>
  335. <p>which could lead to:
  336. </p>
  337. <div class="smallexample">
  338. <pre class="smallexample">error: could not convert 'map&lt;int, double&gt;()' from 'map&lt;int,double&gt;'
  339. to 'map&lt;int,int&gt;'
  340. </pre></div>
  341. <p>using &lsquo;<samp>%H</samp>&rsquo; and &lsquo;<samp>%I</samp>&rsquo; (via &lsquo;<samp>%qH</samp>&rsquo; and &lsquo;<samp>%qI</samp>&rsquo;):
  342. </p>
  343. <div class="smallexample">
  344. <pre class="smallexample"> // OK: compare types in C++ front end via %qH and %qI
  345. error_at (loc, &quot;could not convert %qE from %qH to %qI&quot;, expr,
  346. TREE_TYPE (expr), type);
  347. </pre></div>
  348. <p>allows the above output to be simplified to:
  349. </p>
  350. <div class="smallexample">
  351. <pre class="smallexample">error: could not convert 'map&lt;int, double&gt;()' from 'map&lt;[...],double&gt;'
  352. to 'map&lt;[...],int&gt;'
  353. </pre></div>
  354. <p>where the <code>double</code> and <code>int</code> are colorized to highlight them.
  355. </p>
  356. <a name="Group-logically_002drelated-diagnostics"></a>
  357. <h4 class="subsection">28.1.9 Group logically-related diagnostics</h4>
  358. <p>Use <code>auto_diagnostic_group</code> when issuing multiple related
  359. diagnostics (seen in various examples on this page). This informs the
  360. diagnostic subsystem that all diagnostics issued within the lifetime
  361. of the <code>auto_diagnostic_group</code> are related. For example,
  362. <samp>-fdiagnostics-format=json</samp> will treat the first diagnostic
  363. emitted within the group as a top-level diagnostic, and all subsequent
  364. diagnostics within the group as its children.
  365. </p>
  366. <a name="Quoting"></a>
  367. <h4 class="subsection">28.1.10 Quoting</h4>
  368. <p>Text should be quoted by either using the &lsquo;<samp>q</samp>&rsquo; modifier in a directive
  369. such as &lsquo;<samp>%qE</samp>&rsquo;, or by enclosing the quoted text in a pair of &lsquo;<samp>%&lt;</samp>&rsquo;
  370. and &lsquo;<samp>%&gt;</samp>&rsquo; directives, and never by using explicit quote characters.
  371. The directives handle the appropriate quote characters for each language
  372. and apply the correct color or highlighting.
  373. </p>
  374. <p>The following elements should be quoted in GCC diagnostics:
  375. </p>
  376. <ul>
  377. <li> Language keywords.
  378. </li><li> Tokens.
  379. </li><li> Boolean, numerical, character, and string constants that appear in the
  380. source code.
  381. </li><li> Identifiers, including function, macro, type, and variable names.
  382. </li></ul>
  383. <p>Other elements such as numbers that do not refer to numeric constants that
  384. appear in the source code should not be quoted. For example, in the message:
  385. </p>
  386. <div class="smallexample">
  387. <pre class="smallexample">argument %d of %qE must be a pointer type
  388. </pre></div>
  389. <p>since the argument number does not refer to a numerical constant in the
  390. source code it should not be quoted.
  391. </p>
  392. <a name="Spelling-and-Terminology"></a>
  393. <h4 class="subsection">28.1.11 Spelling and Terminology</h4>
  394. <p>See the <a href="https://gcc.gnu.org/codingconventions.html#Spelling
  395. Spelling">terminology and markup</a> section of the GCC coding conventions.
  396. </p>
  397. <a name="Fix_002dit-hints"></a>
  398. <h4 class="subsection">28.1.12 Fix-it hints</h4>
  399. <a name="index-fix_002dit-hints"></a>
  400. <a name="index-diagnostics-guidelines_002c-fix_002dit-hints"></a>
  401. <p>GCC&rsquo;s diagnostic subsystem can emit <em>fix-it hints</em>: small suggested
  402. edits to the user&rsquo;s source code.
  403. </p>
  404. <p>They are printed by default underneath the code in question. They
  405. can also be viewed via <samp>-fdiagnostics-generate-patch</samp> and
  406. <samp>-fdiagnostics-parseable-fixits</samp>. With the latter, an IDE
  407. ought to be able to offer to automatically apply the suggested fix.
  408. </p>
  409. <p>Fix-it hints contain code fragments, and thus they should not be marked
  410. for translation.
  411. </p>
  412. <p>Fix-it hints can be added to a diagnostic by using a <code>rich_location</code>
  413. rather than a <code>location_t</code> - the fix-it hints are added to the
  414. <code>rich_location</code> using one of the various <code>add_fixit</code> member
  415. functions of <code>rich_location</code>. They are documented with
  416. <code>rich_location</code> in <samp>libcpp/line-map.h</samp>.
  417. It&rsquo;s easiest to use the <code>gcc_rich_location</code> subclass of
  418. <code>rich_location</code> found in <samp>gcc-rich-location.h</samp>, as this
  419. implicitly supplies the <code>line_table</code> variable.
  420. </p>
  421. <p>For example:
  422. </p>
  423. <div class="smallexample">
  424. <pre class="smallexample"> if (const char *suggestion = hint.suggestion ())
  425. {
  426. gcc_rich_location richloc (location);
  427. richloc.add_fixit_replace (suggestion);
  428. error_at (&amp;richloc,
  429. &quot;%qE does not name a type; did you mean %qs?&quot;,
  430. id, suggestion);
  431. }
  432. </pre></div>
  433. <p>which can lead to:
  434. </p>
  435. <div class="smallexample">
  436. <pre class="smallexample">spellcheck-typenames.C:73:1: error: 'singed' does not name a type; did
  437. you mean 'signed'?
  438. 73 | singed char ch;
  439. | ^~~~~~
  440. | signed
  441. </pre></div>
  442. <p>Non-trivial edits can be built up by adding multiple fix-it hints to one
  443. <code>rich_location</code>. It&rsquo;s best to express the edits in terms of the
  444. locations of individual tokens. Various handy functions for adding
  445. fix-it hints for idiomatic C and C++ can be seen in
  446. <samp>gcc-rich-location.h</samp>.
  447. </p>
  448. <a name="Fix_002dit-hints-should-work"></a>
  449. <h4 class="subsubsection">28.1.12.1 Fix-it hints should work</h4>
  450. <p>When implementing a fix-it hint, please verify that the suggested edit
  451. leads to fixed, compilable code. (Unfortunately, this currently must be
  452. done by hand using <samp>-fdiagnostics-generate-patch</samp>. It would be
  453. good to have an automated way of verifying that fix-it hints actually fix
  454. the code).
  455. </p>
  456. <p>For example, a &ldquo;gotcha&rdquo; here is to forget to add a space when adding a
  457. missing reserved word. Consider a C++ fix-it hint that adds
  458. <code>typename</code> in front of a template declaration. A naive way to
  459. implement this might be:
  460. </p>
  461. <div class="smallexample">
  462. <pre class="smallexample">gcc_rich_location richloc (loc);
  463. // BAD: insertion is missing a trailing space
  464. richloc.add_fixit_insert_before (&quot;typename&quot;);
  465. error_at (&amp;richloc, &quot;need %&lt;typename%&gt; before %&lt;%T::%E%&gt; because &quot;
  466. &quot;%qT is a dependent scope&quot;,
  467. parser-&gt;scope, id, parser-&gt;scope);
  468. </pre></div>
  469. <p>When applied to the code, this might lead to:
  470. </p>
  471. <div class="smallexample">
  472. <pre class="smallexample">T::type x;
  473. </pre></div>
  474. <p>being &ldquo;corrected&rdquo; to:
  475. </p>
  476. <div class="smallexample">
  477. <pre class="smallexample">typenameT::type x;
  478. </pre></div>
  479. <p>In this case, the correct thing to do is to add a trailing space after
  480. <code>typename</code>:
  481. </p>
  482. <div class="smallexample">
  483. <pre class="smallexample">gcc_rich_location richloc (loc);
  484. // OK: note that here we have a trailing space
  485. richloc.add_fixit_insert_before (&quot;typename &quot;);
  486. error_at (&amp;richloc, &quot;need %&lt;typename%&gt; before %&lt;%T::%E%&gt; because &quot;
  487. &quot;%qT is a dependent scope&quot;,
  488. parser-&gt;scope, id, parser-&gt;scope);
  489. </pre></div>
  490. <p>leading to this corrected code:
  491. </p>
  492. <div class="smallexample">
  493. <pre class="smallexample">typename T::type x;
  494. </pre></div>
  495. <a name="Express-deletion-in-terms-of-deletion_002c-not-replacement"></a>
  496. <h4 class="subsubsection">28.1.12.2 Express deletion in terms of deletion, not replacement</h4>
  497. <p>It&rsquo;s best to express deletion suggestions in terms of deletion fix-it
  498. hints, rather than replacement fix-it hints. For example, consider this:
  499. </p>
  500. <div class="smallexample">
  501. <pre class="smallexample"> auto_diagnostic_group d;
  502. gcc_rich_location richloc (location_of (retval));
  503. tree name = DECL_NAME (arg);
  504. richloc.add_fixit_replace (IDENTIFIER_POINTER (name));
  505. warning_at (&amp;richloc, OPT_Wredundant_move,
  506. &quot;redundant move in return statement&quot;);
  507. </pre></div>
  508. <p>which is intended to e.g. replace a <code>std::move</code> with the underlying
  509. value:
  510. </p>
  511. <div class="smallexample">
  512. <pre class="smallexample"> return std::move (retval);
  513. ~~~~~~~~~~^~~~~~~~
  514. retval
  515. </pre></div>
  516. <p>where the change has been expressed as replacement, replacing
  517. with the name of the declaration.
  518. This works for simple cases, but consider this case:
  519. </p>
  520. <div class="smallexample">
  521. <pre class="smallexample">#ifdef SOME_CONFIG_FLAG
  522. # define CONFIGURY_GLOBAL global_a
  523. #else
  524. # define CONFIGURY_GLOBAL global_b
  525. #endif
  526. int fn ()
  527. {
  528. return std::move (CONFIGURY_GLOBAL /* some comment */);
  529. }
  530. </pre></div>
  531. <p>The above implementation erroneously strips out the macro and the
  532. comment in the fix-it hint:
  533. </p>
  534. <div class="smallexample">
  535. <pre class="smallexample"> return std::move (CONFIGURY_GLOBAL /* some comment */);
  536. ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  537. global_a
  538. </pre></div>
  539. <p>and thus this resulting code:
  540. </p>
  541. <div class="smallexample">
  542. <pre class="smallexample"> return global_a;
  543. </pre></div>
  544. <p>It&rsquo;s better to do deletions in terms of deletions; deleting the
  545. <code>std::move (</code> and the trailing close-paren, leading to
  546. this:
  547. </p>
  548. <div class="smallexample">
  549. <pre class="smallexample"> return std::move (CONFIGURY_GLOBAL /* some comment */);
  550. ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  551. CONFIGURY_GLOBAL /* some comment */
  552. </pre></div>
  553. <p>and thus this result:
  554. </p>
  555. <div class="smallexample">
  556. <pre class="smallexample"> return CONFIGURY_GLOBAL /* some comment */;
  557. </pre></div>
  558. <p>Unfortunately, the pertinent <code>location_t</code> values are not always
  559. available.
  560. </p>
  561. <a name="Multiple-suggestions"></a>
  562. <h4 class="subsubsection">28.1.12.3 Multiple suggestions</h4>
  563. <p>In the rare cases where you need to suggest more than one mutually
  564. exclusive solution to a problem, this can be done by emitting
  565. multiple notes and calling
  566. <code>rich_location::fixits_cannot_be_auto_applied</code> on each note&rsquo;s
  567. <code>rich_location</code>. If this is called, then the fix-it hints in
  568. the <code>rich_location</code> will be printed, but will not be added to
  569. generated patches.
  570. </p>
  571. <hr>
  572. <div class="header">
  573. <p>
  574. Next: <a href="Guidelines-for-Options.html#Guidelines-for-Options" accesskey="n" rel="next">Guidelines for Options</a>, Up: <a href="User-Experience-Guidelines.html#User-Experience-Guidelines" accesskey="u" rel="up">User Experience Guidelines</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>
  575. </div>
  576. </body>
  577. </html>