Debian Codegraph @

gcc/testsuite/libgdiagnostics.dg/test-metadata.c

raw
1 /* Example of setting a CWE and adding extra metadata.
2
3 Intended output is similar to:
4
5 PATH/test-metadata.c:21:3: warning: never use 'gets' [CWE-242] [STR34-C]
6 21 | gets (buf);
7 | ^~~~~~~~~~
8
9 where the metadata tags are linkified in a sufficiently capable terminal,
10 along with the equivalent in SARIF. */
11
12 #include "libgdiagnostics.h"
13 #include "test-helpers.h"
14
15 /* Placeholder source:
16 _________11111111112
17 12345678901234567890
18 void test_cwe (void)
19 {
20 char buf[1024];
21 gets (buf);
22 }
23 */
24 const int line_num = __LINE__ - 3;
25
26 int
27 main ()
28 {
29 begin_test ("FooChecker",
30 "test-metadata.c.sarif",
31 __FILE__, "c");
32
33 diagnostic_manager_set_full_name (diag_mgr, "FooChecker 0.1 (en_US)");
34 diagnostic_manager_set_version_string (diag_mgr, "0.1");
35 diagnostic_manager_set_version_url (diag_mgr, "https://www.example.com/0.1/");
36
37 const diagnostic_physical_location *loc_token
38 = make_range (diag_mgr, main_file, line_num, 3, 12);
39 diagnostic *d = diagnostic_begin (diag_mgr,
40 DIAGNOSTIC_LEVEL_WARNING);
41 diagnostic_set_location (d, loc_token);
42 diagnostic_set_cwe (d, 242); /* CWE-242: Use of Inherently Dangerous Function. */
43 diagnostic_add_rule (d, "STR34-C", "https://example.com/");
44
45 diagnostic_finish (d, "never use %qs", "gets");
46
47 return end_test ();
48 }
49
50 /* { dg-regexp "\[^\n\r\]+test-metadata.c:21:3: warning: never use 'gets' \\\[CWE-242\\\] \\\[STR34-C\\\]" } */
51 /* { dg-begin-multiline-output "" }
52 21 | gets (buf);
53 | ^~~~~~~~~~
54 { dg-end-multiline-output "" } */
55
56 /* Verify that some JSON was written to a file with the expected name:
57 { dg-final { verify-sarif-file } } */
58
59 /* Use a Python script to verify various properties about the generated
60 .sarif file:
61 { dg-final { run-sarif-pytest test-metadata.c "test-metadata-c.py" } } */