| 1 |
/* Example of emitting an error with an associated note. |
| 2 |
|
| 3 |
Intended output is similar to: |
| 4 |
|
| 5 |
PATH/test-error-with-note.c:18:11: error: can't find 'foo.h' |
| 6 |
6 | #include <foo.h> |
| 7 |
| ^~~~~ |
| 8 |
PATH/test-error-with-note.c:18:11: note: have you looked behind the couch? |
| 9 |
|
| 10 |
along with the equivalent in SARIF. */ |
| 11 |
|
| 12 |
#include "libgdiagnostics.h" |
| 13 |
#include "test-helpers.h" |
| 14 |
|
| 15 |
/* |
| 16 |
_________111111111122 |
| 17 |
123456789012345678901 |
| 18 |
#include <foo.h> |
| 19 |
*/ |
| 20 |
const int line_num = __LINE__ - 2; |
| 21 |
|
| 22 |
int |
| 23 |
main () |
| 24 |
{ |
| 25 |
begin_test ("test-error-with-note.c.exe", |
| 26 |
"test-error-with-note.c.sarif", |
| 27 |
__FILE__, "c"); |
| 28 |
|
| 29 |
const diagnostic_physical_location *loc_start |
| 30 |
= diagnostic_manager_new_location_from_file_line_column (diag_mgr, |
| 31 |
main_file, |
| 32 |
line_num, |
| 33 |
11); |
| 34 |
const diagnostic_physical_location *loc_end |
| 35 |
= diagnostic_manager_new_location_from_file_line_column (diag_mgr, |
| 36 |
main_file, |
| 37 |
line_num, |
| 38 |
15); |
| 39 |
const diagnostic_physical_location *loc_range |
| 40 |
= diagnostic_manager_new_location_from_range (diag_mgr, |
| 41 |
loc_start, |
| 42 |
loc_start, |
| 43 |
loc_end); |
| 44 |
|
| 45 |
/* begin quoted source */ |
| 46 |
diagnostic_manager_begin_group (diag_mgr); |
| 47 |
|
| 48 |
diagnostic *err = diagnostic_begin (diag_mgr, |
| 49 |
DIAGNOSTIC_LEVEL_ERROR); |
| 50 |
diagnostic_set_location (err, loc_range); |
| 51 |
diagnostic_finish (err, "can't find %qs", "foo.h"); |
| 52 |
|
| 53 |
diagnostic *note = diagnostic_begin (diag_mgr, DIAGNOSTIC_LEVEL_NOTE); |
| 54 |
diagnostic_set_location (note, loc_range); |
| 55 |
diagnostic_finish (note, "have you looked behind the couch?"); |
| 56 |
|
| 57 |
diagnostic_manager_end_group (diag_mgr); |
| 58 |
/* end quoted source */ |
| 59 |
|
| 60 |
return end_test (); |
| 61 |
}; |
| 62 |
|
| 63 |
/* Verify the output from the text sink. |
| 64 |
{ dg-regexp "\[^\n\r\]+test-error-with-note.c:18:11: error: can't find 'foo.h'" } |
| 65 |
{ dg-begin-multiline-output "" } |
| 66 |
18 | #include <foo.h> |
| 67 |
| ^~~~~ |
| 68 |
{ dg-end-multiline-output "" } |
| 69 |
{ dg-regexp "\[^\n\r\]+test-error-with-note.c:18:11: note: have you looked behind the couch." } */ |
| 70 |
|
| 71 |
/* Verify that some JSON was written to a file with the expected name: |
| 72 |
{ dg-final { verify-sarif-file } } */ |
| 73 |
|
| 74 |
/* Use a Python script to verify various properties about the generated |
| 75 |
.sarif file: |
| 76 |
{ dg-final { run-sarif-pytest test-error-with-note.c "test-error-with-note-c.py" } } */ |