Debian Codegraph @

gcc/testsuite/libgdiagnostics.dg/test-labelled-ranges.cc

raw
1 /* C++ example of multiple locations, with labelling of ranges.
2
3 Intended output is similar to:
4
5 PATH/test-labelled-ranges.cc:19:6: error: mismatching types: 'int' and 'const char *'
6 19 | 42 + "foo"
7 | ~~ ^ ~~~~~
8 | | |
9 | int const char *
10
11 along with the equivalent in SARIF. */
12
13 #include "libgdiagnostics++.h"
14 #include "test-helpers++.h"
15
16 /*
17 _________11111111112
18 12345678901234567890
19 42 + "foo"
20 */
21 const int line_num = __LINE__ - 2;
22
23 int
24 main ()
25 {
26 FILE *sarif_outfile;
27 libgdiagnostics::manager mgr;
28 mgr.set_tool_name ("test-labelled-ranges.cc.exe");
29
30 libgdiagnostics::file file = mgr.new_file (__FILE__, "c");
31
32 mgr.add_text_sink (stderr, DIAGNOSTIC_COLORIZE_IF_TTY);
33 sarif_outfile = fopen ("test-labelled-ranges.cc.sarif", "w");
34 if (sarif_outfile)
35 mgr.add_sarif_sink (sarif_outfile, file, DIAGNOSTIC_SARIF_VERSION_2_1_0);
36
37 auto loc_operator = mgr.new_location_from_file_line_column (file, line_num, 6);
38
39 auto d (mgr.begin_diagnostic (DIAGNOSTIC_LEVEL_ERROR));
40 d.set_location (loc_operator);
41 d.add_location_with_label (make_range (mgr, file, line_num, 3, 4),
42 "int");
43 d.add_location_with_label (make_range (mgr, file, line_num, 8, 12),
44 "const char *");
45 d.finish ("mismatching types: %qs and %qs", "int", "const char *");
46
47 return 0;
48 }
49
50 /* Check the output from the text sink. */
51 /* { dg-regexp "\[^\n\r\]+test-labelled-ranges.cc:19:6: error: mismatching types: 'int' and 'const char \\*'" } */
52 /* { dg-begin-multiline-output "" }
53 19 | 42 + "foo"
54 | ~~ ^ ~~~~~
55 | | |
56 | int const char *
57 { dg-end-multiline-output "" } */
58
59 /* Verify that some JSON was written to a file with the expected name:
60 { dg-final { verify-sarif-file } } */
61
62 /* Use a Python script to verify various properties about the generated
63 .sarif file:
64 { dg-final { run-sarif-pytest test-labelled-ranges.cc "test-labelled-ranges.py" } } */