| 1 |
/* C++ example of emitting an error. |
| 2 |
|
| 3 |
Intended output is similar to: |
| 4 |
|
| 5 |
PATH/test-error.cc:16:8: error: can't find 'foo' |
| 6 |
16 | PRINT "hello world!"; |
| 7 |
| ^~~~~~~~~~~~ |
| 8 |
|
| 9 |
along with the equivalent in SARIF. */ |
| 10 |
|
| 11 |
#include "libgdiagnostics++.h" |
| 12 |
|
| 13 |
/* |
| 14 |
_________111111111122 |
| 15 |
123456789012345678901 |
| 16 |
PRINT "hello world!"; |
| 17 |
*/ |
| 18 |
const int line_num = __LINE__ - 2; |
| 19 |
|
| 20 |
int |
| 21 |
main () |
| 22 |
{ |
| 23 |
libgdiagnostics::manager mgr; |
| 24 |
|
| 25 |
auto file = mgr.new_file (__FILE__, "c"); |
| 26 |
|
| 27 |
mgr.add_text_sink (stderr, DIAGNOSTIC_COLORIZE_IF_TTY); |
| 28 |
|
| 29 |
auto loc_start = mgr.new_location_from_file_line_column (file, line_num, 8); |
| 30 |
auto loc_end = mgr.new_location_from_file_line_column (file, line_num, 19); |
| 31 |
auto loc_range = mgr.new_location_from_range (loc_start, |
| 32 |
loc_start, |
| 33 |
loc_end); |
| 34 |
|
| 35 |
libgdiagnostics::diagnostic d (mgr.begin_diagnostic (DIAGNOSTIC_LEVEL_ERROR)); |
| 36 |
d.set_location (loc_range); |
| 37 |
d.finish ("can't find %qs", "foo"); |
| 38 |
|
| 39 |
return 0; |
| 40 |
}; |
| 41 |
|
| 42 |
/* Verify the output from the text sink. |
| 43 |
{ dg-regexp "\[^\n\r\]+test-error.cc:16:8: error: can't find 'foo'" } |
| 44 |
{ dg-begin-multiline-output "" } |
| 45 |
16 | PRINT "hello world!"; |
| 46 |
| ^~~~~~~~~~~~ |
| 47 |
{ dg-end-multiline-output "" } */ |