Debian Codegraph @

gcc/testsuite/libgdiagnostics.dg/test-logical-location.c

raw
1 /* Example of using a logical location.
2
3 Intended output is similar to:
4
5 In function 'test_qualified_name':
6 PATH/test-error-with-note.c:18:8: error: can't find 'foo'
7 18 | PRINT "hello world!";
8 | ^~~~~~~~~~~~
9
10 along with the equivalent in SARIF. */
11
12 #include "libgdiagnostics.h"
13 #include "test-helpers.h"
14
15 /* Placeholder source:
16 _________111111111122
17 123456789012345678901
18 PRINT "hello world!";
19 */
20 const int line_num = __LINE__ - 2;
21
22 #include <assert.h>
23
24 int
25 main ()
26 {
27 begin_test ("test-logical-location.c.exe",
28 "test-logical-location.c.sarif",
29 __FILE__, "c");
30
31 const diagnostic_physical_location *loc_start
32 = diagnostic_manager_new_location_from_file_line_column (diag_mgr,
33 main_file,
34 line_num,
35 8);
36 const diagnostic_physical_location *loc_end
37 = diagnostic_manager_new_location_from_file_line_column (diag_mgr,
38 main_file,
39 line_num,
40 19);
41 const diagnostic_physical_location *loc_range
42 = diagnostic_manager_new_location_from_range (diag_mgr,
43 loc_start,
44 loc_start,
45 loc_end);
46
47 /* begin quoted source */
48 diagnostic *d = diagnostic_begin (diag_mgr,
49 DIAGNOSTIC_LEVEL_ERROR);
50 diagnostic_set_location (d, loc_range);
51
52 const diagnostic_logical_location *logical_loc
53 = diagnostic_manager_new_logical_location (diag_mgr,
54 DIAGNOSTIC_LOGICAL_LOCATION_KIND_FUNCTION,
55 NULL, /* parent */
56 "test_short_name",
57 "test_qualified_name",
58 "test_decorated_name");
59
60 diagnostic_set_logical_location (d, logical_loc);
61
62 diagnostic_finish (d, "can't find %qs", "foo");
63 /* end quoted source */
64
65 /* Verify that creating a diagnostic_logical_location with equal values
66 yields the same instance. */
67 const diagnostic_logical_location *dup
68 = diagnostic_manager_new_logical_location (diag_mgr,
69 DIAGNOSTIC_LOGICAL_LOCATION_KIND_FUNCTION,
70 NULL, /* parent */
71 "test_short_name",
72 "test_qualified_name",
73 "test_decorated_name");
74 assert (dup == logical_loc);
75
76 return end_test ();
77 }
78
79 /* Check the output from the text sink. */
80 /* { dg-begin-multiline-output "" }
81 In function 'test_qualified_name':
82 { dg-end-multiline-output "" } */
83 /* { dg-regexp "\[^\n\r\]+test-logical-location.c:18:8: error: can't find 'foo'" } */
84 /* { dg-begin-multiline-output "" }
85 18 | PRINT "hello world!";
86 | ^~~~~~~~~~~~
87 { dg-end-multiline-output "" } */
88
89 /* Verify that some JSON was written to a file with the expected name:
90 { dg-final { verify-sarif-file } } */
91
92 /* Use a Python script to verify various properties about the generated
93 .sarif file:
94 { dg-final { run-sarif-pytest test-logical-location.c "test-logical-location-c.py" } } */