Debian Codegraph @

gcc/testsuite/libgdiagnostics.dg/test-helpers.h

raw
1 /* Common utility code shared between test cases. */
2
3 #ifndef TEST_HELPERS_H
4 #define TEST_HELPERS_H
5
6 const diagnostic_physical_location *
7 make_range (diagnostic_manager *diag_mgr,
8 const diagnostic_file *file,
9 diagnostic_line_num_t line_num,
10 diagnostic_column_num_t start_column,
11 diagnostic_column_num_t end_column)
12 {
13 const diagnostic_physical_location *loc_start
14 = diagnostic_manager_new_location_from_file_line_column (diag_mgr,
15 file,
16 line_num,
17 start_column);
18 const diagnostic_physical_location *loc_end
19 = diagnostic_manager_new_location_from_file_line_column (diag_mgr,
20 file,
21 line_num,
22 end_column);
23 return diagnostic_manager_new_location_from_range (diag_mgr,
24 loc_start,
25 loc_start,
26 loc_end);
27 }
28
29 /* A begin_test/end_test pair to consolidate the code shared by tests:
30 create a diagnostic_manager, a main file, a text sink, and a SARIF sink,
31 and clean these up after emitting zero or more diagnostics. */
32
33 static diagnostic_manager *diag_mgr;
34 static const diagnostic_file *main_file;
35 static FILE *sarif_outfile;
36
37 static void
38 begin_test (const char *tool_name,
39 const char *sarif_output_name,
40 const char *main_file_name,
41 const char *source_language)
42 {
43 diag_mgr = diagnostic_manager_new ();
44
45 /* We need to set this for generated .sarif files to validate
46 against the schema. */
47 diagnostic_manager_set_tool_name (diag_mgr, tool_name);
48
49 main_file = diagnostic_manager_new_file (diag_mgr,
50 main_file_name,
51 source_language);
52
53 diagnostic_manager_add_text_sink (diag_mgr, stderr,
54 DIAGNOSTIC_COLORIZE_IF_TTY);
55 sarif_outfile = fopen (sarif_output_name, "w");
56 if (sarif_outfile)
57 diagnostic_manager_add_sarif_sink (diag_mgr,
58 sarif_outfile,
59 main_file,
60 DIAGNOSTIC_SARIF_VERSION_2_1_0);
61 }
62
63 static int
64 end_test (void)
65 {
66 diagnostic_manager_release (diag_mgr);
67 if (sarif_outfile)
68 fclose (sarif_outfile);
69 return 0;
70 }
71
72 #endif /* #ifndef TEST_HELPERS_H */