| 1 |
/* C++ example of a fix-it hint, including patch generation. |
| 2 |
|
| 3 |
Intended output is similar to: |
| 4 |
|
| 5 |
PATH/test-fix-it-hint.cc:19:13: error: unknown field 'colour'; did you mean 'color' |
| 6 |
19 | return p->colour; |
| 7 |
| ^~~~~~ |
| 8 |
| color |
| 9 |
|
| 10 |
along with the equivalent in SARIF, and a generated patch (on stderr) to |
| 11 |
make the change. */ |
| 12 |
|
| 13 |
#include "libgdiagnostics++.h" |
| 14 |
#include "test-helpers++.h" |
| 15 |
|
| 16 |
/* |
| 17 |
_________11111111112 |
| 18 |
12345678901234567890 |
| 19 |
return p->colour; |
| 20 |
*/ |
| 21 |
const int line_num = __LINE__ - 2; |
| 22 |
|
| 23 |
int |
| 24 |
main () |
| 25 |
{ |
| 26 |
libgdiagnostics::manager mgr; |
| 27 |
|
| 28 |
auto file = mgr.new_file (__FILE__, "c"); |
| 29 |
|
| 30 |
mgr.add_text_sink (stderr, DIAGNOSTIC_COLORIZE_IF_TTY); |
| 31 |
|
| 32 |
auto loc_token = make_range (mgr, file, line_num, 13, 18); |
| 33 |
|
| 34 |
auto d = mgr.begin_diagnostic (DIAGNOSTIC_LEVEL_ERROR); |
| 35 |
d.set_location (loc_token); |
| 36 |
|
| 37 |
d.add_fix_it_hint_replace (loc_token, "color"); |
| 38 |
|
| 39 |
d.finish ("unknown field %qs; did you mean %qs", "colour", "color"); |
| 40 |
|
| 41 |
mgr.write_patch (stderr); |
| 42 |
|
| 43 |
return 0; |
| 44 |
} |
| 45 |
|
| 46 |
/* Verify the output from the text sink. |
| 47 |
{ dg-regexp "\[^\n\r\]+test-fix-it-hint.cc:19:13: error: unknown field 'colour'; did you mean 'color'" } |
| 48 |
{ dg-begin-multiline-output "" } |
| 49 |
19 | return p->colour; |
| 50 |
| ^~~~~~ |
| 51 |
| color |
| 52 |
{ dg-end-multiline-output "" } */ |
| 53 |
|
| 54 |
/* Verify the output from diagnostic_manager_write_patch. |
| 55 |
We expect the patch to begin with a header, containing this |
| 56 |
source filename, via an absolute path. |
| 57 |
Given the path, we can only capture it via regexps. */ |
| 58 |
/* { dg-regexp "\\-\\-\\- .*" } */ |
| 59 |
/* { dg-regexp "\\+\\+\\+ .*" } */ |
| 60 |
/* Use #if 0/#endif rather than comments, to allow the text to contain |
| 61 |
a comment. */ |
| 62 |
#if 0 |
| 63 |
{ dg-begin-multiline-output "" } |
| 64 |
@@ -16,7 +16,7 @@ |
| 65 |
/* |
| 66 |
_________11111111112 |
| 67 |
12345678901234567890 |
| 68 |
- return p->colour; |
| 69 |
+ return p->color; |
| 70 |
*/ |
| 71 |
const int line_num = __LINE__ - 2; |
| 72 |
|
| 73 |
{ dg-end-multiline-output "" } |
| 74 |
#endif |