Debian Codegraph @

gcc/testsuite/libgdiagnostics.dg/test-write-sarif-to-file.c

raw
1 /* Example of writing diagnostics as SARIF to a file. */
2
3 #include "libgdiagnostics.h"
4
5 /*
6 _________111111111122
7 123456789012345678901
8 PRINT "hello world!";
9 */
10 const int line_num = __LINE__ - 2;
11
12 int
13 main ()
14 {
15 FILE *sarif_outfile = fopen ("test-write-sarif-to-file.c.sarif", "w");
16 if (!sarif_outfile)
17 return -1;
18
19 diagnostic_manager *diag_mgr = diagnostic_manager_new ();
20 diagnostic_manager_set_tool_name (diag_mgr, "test-write-sarif-to-file.c.exe");
21
22 const diagnostic_file *file = diagnostic_manager_new_file (diag_mgr, __FILE__, "c");
23
24 diagnostic_manager_add_sarif_sink (diag_mgr, sarif_outfile, file,
25 DIAGNOSTIC_SARIF_VERSION_2_1_0);
26
27 const diagnostic_physical_location *loc_start
28 = diagnostic_manager_new_location_from_file_line_column (diag_mgr, file, line_num, 8);
29 const diagnostic_physical_location *loc_end
30 = diagnostic_manager_new_location_from_file_line_column (diag_mgr, file, line_num, 19);
31 const diagnostic_physical_location *loc_range
32 = diagnostic_manager_new_location_from_range (diag_mgr,
33 loc_start,
34 loc_start,
35 loc_end);
36
37 diagnostic *d = diagnostic_begin (diag_mgr,
38 DIAGNOSTIC_LEVEL_ERROR);
39 diagnostic_set_location (d, loc_range);
40
41 diagnostic_finish (d, "can't find %qs", "foo");
42
43 diagnostic_manager_release (diag_mgr);
44
45 fclose (sarif_outfile);
46
47 return 0;
48 };
49
50 /* Verify that some JSON was written to a file with the expected name:
51 { dg-final { verify-sarif-file } } */
52
53 /* Use a Python script to verify various properties about the generated
54 .sarif file:
55 { dg-final { run-sarif-pytest test-write-sarif-to-file.c "test-write-sarif-to-file-c.py" } } */