| 1 |
/* Example of emitting a warning with an execution path. |
| 2 |
|
| 3 |
TODO: |
| 4 |
|
| 5 |
Intended output is similar to: |
| 6 |
|
| 7 |
along with the equivalent in SARIF. */ |
| 8 |
|
| 9 |
#include "libgdiagnostics.h" |
| 10 |
#include "test-helpers.h" |
| 11 |
|
| 12 |
/* |
| 13 |
_________111111111122222222223333333333444444444455555555556 |
| 14 |
123456789012345678901234567890123456789012345678901234567890 |
| 15 |
begin fake source |
| 16 |
PyObject * |
| 17 |
make_a_list_of_random_ints_badly(PyObject *self, |
| 18 |
PyObject *args) |
| 19 |
{ |
| 20 |
PyObject *list, *item; |
| 21 |
long count, i; |
| 22 |
|
| 23 |
if (!PyArg_ParseTuple(args, "i", &count)) { |
| 24 |
return NULL; |
| 25 |
} |
| 26 |
|
| 27 |
list = PyList_New(0); |
| 28 |
|
| 29 |
for (i = 0; i < count; i++) { |
| 30 |
item = PyLong_FromLong(random()); |
| 31 |
PyList_Append(list, item); |
| 32 |
} |
| 33 |
|
| 34 |
return list; |
| 35 |
} |
| 36 |
end fake source |
| 37 |
*/ |
| 38 |
const int final_line_num = __LINE__ - 4; /* line of "return list;" */ |
| 39 |
|
| 40 |
/* begin line consts */ |
| 41 |
const int line_num_call_to_PyList_New = final_line_num - 7; |
| 42 |
const int line_num_for_loop = final_line_num - 5; |
| 43 |
const int line_num_call_to_PyList_Append = final_line_num - 3; |
| 44 |
/* end line consts */ |
| 45 |
|
| 46 |
int |
| 47 |
main () |
| 48 |
{ |
| 49 |
begin_test ("test-warning-with-path.c.exe", |
| 50 |
"test-warning-with-path.c.sarif", |
| 51 |
__FILE__, "c"); |
| 52 |
|
| 53 |
/* begin full example */ |
| 54 |
/* begin create phys locs */ |
| 55 |
const diagnostic_physical_location *loc_call_to_PyList_New |
| 56 |
= make_range (diag_mgr, main_file, line_num_call_to_PyList_New, 10, 22); |
| 57 |
const diagnostic_physical_location *loc_for_cond |
| 58 |
= make_range (diag_mgr, main_file, line_num_for_loop, 15, 23); |
| 59 |
const diagnostic_physical_location *loc_call_to_PyList_Append |
| 60 |
= make_range (diag_mgr, main_file, line_num_call_to_PyList_Append, 5, 29); |
| 61 |
/* end create phys locs */ |
| 62 |
|
| 63 |
/* begin create logical locs */ |
| 64 |
const char *funcname = "make_a_list_of_random_ints_badly"; |
| 65 |
const diagnostic_logical_location *logical_loc |
| 66 |
= diagnostic_manager_new_logical_location (diag_mgr, |
| 67 |
DIAGNOSTIC_LOGICAL_LOCATION_KIND_FUNCTION, |
| 68 |
NULL, /* parent */ |
| 69 |
funcname, |
| 70 |
funcname, |
| 71 |
funcname); |
| 72 |
/* end create logical locs */ |
| 73 |
|
| 74 |
diagnostic *d = diagnostic_begin (diag_mgr, |
| 75 |
DIAGNOSTIC_LEVEL_WARNING); |
| 76 |
diagnostic_set_location (d, loc_call_to_PyList_Append); |
| 77 |
diagnostic_set_logical_location (d, logical_loc); |
| 78 |
|
| 79 |
/* begin path creation */ |
| 80 |
diagnostic_execution_path *path = diagnostic_add_execution_path (d); |
| 81 |
|
| 82 |
diagnostic_event_id alloc_event_id |
| 83 |
= diagnostic_execution_path_add_event (path, |
| 84 |
loc_call_to_PyList_New, |
| 85 |
logical_loc, 0, |
| 86 |
"when %qs fails, returning NULL", |
| 87 |
"PyList_New"); |
| 88 |
diagnostic_execution_path_add_event (path, |
| 89 |
loc_for_cond, |
| 90 |
logical_loc, 0, |
| 91 |
"when %qs", "i < count"); |
| 92 |
diagnostic_execution_path_add_event (path, |
| 93 |
loc_call_to_PyList_Append, |
| 94 |
logical_loc, 0, |
| 95 |
"when calling %qs, passing NULL from %@ as argument %i", |
| 96 |
"PyList_Append", &alloc_event_id, 1); |
| 97 |
/* end path creation */ |
| 98 |
|
| 99 |
diagnostic_finish (d, |
| 100 |
"passing NULL as argument %i to %qs" |
| 101 |
" which requires a non-NULL parameter", |
| 102 |
1, "PyList_Append"); |
| 103 |
/* end full example */ |
| 104 |
|
| 105 |
return end_test (); |
| 106 |
}; |
| 107 |
|
| 108 |
/* Check the output from the text sink. |
| 109 |
{ dg-begin-multiline-output "" } |
| 110 |
In function 'make_a_list_of_random_ints_badly': |
| 111 |
{ dg-end-multiline-output "" } |
| 112 |
{ dg-regexp "\[^\n\r\]+test-warning-with-path.c:31:5: warning: passing NULL as argument 1 to 'PyList_Append' which requires a non-NULL parameter" } |
| 113 |
{ dg-begin-multiline-output "" } |
| 114 |
31 | PyList_Append(list, item); |
| 115 |
| ^~~~~~~~~~~~~~~~~~~~~~~~~ |
| 116 |
'make_a_list_of_random_ints_badly': events 1-3 |
| 117 |
27 | list = PyList_New(0); |
| 118 |
| ^~~~~~~~~~~~~ |
| 119 |
| | |
| 120 |
| (1) when 'PyList_New' fails, returning NULL |
| 121 |
28 | |
| 122 |
29 | for (i = 0; i < count; i++) { |
| 123 |
| ~~~~~~~~~ |
| 124 |
| | |
| 125 |
| (2) when 'i < count' |
| 126 |
30 | item = PyLong_FromLong(random()); |
| 127 |
31 | PyList_Append(list, item); |
| 128 |
| ~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 129 |
| | |
| 130 |
| (3) when calling 'PyList_Append', passing NULL from (1) as argument 1 |
| 131 |
{ dg-end-multiline-output "" } */ |
| 132 |
|
| 133 |
/* Verify that some JSON was written to a file with the expected name: |
| 134 |
{ dg-final { verify-sarif-file } } */ |
| 135 |
|
| 136 |
/* Use a Python script to verify various properties about the generated |
| 137 |
.sarif file: |
| 138 |
{ dg-final { run-sarif-pytest test-warning-with-path.c "test-warning-with-path-c.py" } } */ |