| 1 |
/* Example of a fix-it hint, including patch generation. |
| 2 |
|
| 3 |
Intended output is similar to: |
| 4 |
|
| 5 |
PATH/test-fix-it-hint.c: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 |
begin_test ("test-fix-it-hint.c.exe", |
| 27 |
"test-fix-it-hint.c.sarif", |
| 28 |
__FILE__, "c"); |
| 29 |
|
| 30 |
const diagnostic_physical_location *loc_token |
| 31 |
= make_range (diag_mgr, main_file, line_num, 13, 18); |
| 32 |
|
| 33 |
/* begin quoted source */ |
| 34 |
diagnostic *d = diagnostic_begin (diag_mgr, |
| 35 |
DIAGNOSTIC_LEVEL_ERROR); |
| 36 |
diagnostic_set_location (d, loc_token); |
| 37 |
|
| 38 |
diagnostic_add_fix_it_hint_replace (d, loc_token, "color"); |
| 39 |
|
| 40 |
diagnostic_finish (d, "unknown field %qs; did you mean %qs", "colour", "color"); |
| 41 |
/* end quoted source */ |
| 42 |
|
| 43 |
diagnostic_manager_write_patch (diag_mgr, stderr); |
| 44 |
|
| 45 |
return end_test (); |
| 46 |
} |
| 47 |
|
| 48 |
/* Verify the output from the text sink. |
| 49 |
{ dg-regexp "\[^\n\r\]+test-fix-it-hint.c:19:13: error: unknown field 'colour'; did you mean 'color'" } |
| 50 |
{ dg-begin-multiline-output "" } |
| 51 |
19 | return p->colour; |
| 52 |
| ^~~~~~ |
| 53 |
| color |
| 54 |
{ dg-end-multiline-output "" } */ |
| 55 |
|
| 56 |
/* Verify the output from diagnostic_manager_write_patch. |
| 57 |
We expect the patch to begin with a header, containing this |
| 58 |
source filename, via an absolute path. |
| 59 |
Given the path, we can only capture it via regexps. */ |
| 60 |
/* { dg-regexp "\\-\\-\\- .*" } */ |
| 61 |
/* { dg-regexp "\\+\\+\\+ .*" } */ |
| 62 |
/* Use #if 0/#endif rather than comments, to allow the text to contain |
| 63 |
a comment. */ |
| 64 |
#if 0 |
| 65 |
{ dg-begin-multiline-output "" } |
| 66 |
@@ -16,7 +16,7 @@ |
| 67 |
/* |
| 68 |
_________11111111112 |
| 69 |
12345678901234567890 |
| 70 |
- return p->colour; |
| 71 |
+ return p->color; |
| 72 |
*/ |
| 73 |
const int line_num = __LINE__ - 2; |
| 74 |
|
| 75 |
{ dg-end-multiline-output "" } |
| 76 |
#endif |
| 77 |
|
| 78 |
/* Verify that some JSON was written to a file with the expected name: |
| 79 |
{ dg-final { verify-sarif-file } } */ |
| 80 |
|
| 81 |
/* Use a Python script to verify various properties about the generated |
| 82 |
.sarif file: |
| 83 |
{ dg-final { run-sarif-pytest test-fix-it-hint.c "test-fix-it-hint-c.py" } } */ |