Debian Codegraph @

gcc/testsuite/lib/scansarif.exp

raw
1 # Copyright (C) 2000-2025 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with GCC; see the file COPYING3. If not see
15 # <http://www.gnu.org/licenses/>.
16
17 # Various utilities for scanning SARIF output, used by gcc-dg.exp and
18 # g++-dg.exp.
19 #
20 # This is largely borrowed from scanasm.exp, but tweaked to force Tcl
21 # to treat the file as UTF-8: section 3.1 of SARIF 2.1.0
22 # ("File Format" > "General") specifies: "A SARIF log file SHALL be
23 # encoded in UTF-8 [RFC3629])".
24
25 # Look for a pattern in the .sarif file produced by the compiler. See
26 # dg-scan for details.
27
28 proc scan-sarif-file { args } {
29 set testcase [testname-for-summary]
30 # The name might include a list of options; extract the file name.
31 set filename [lindex $testcase 0]
32 set output_file "[file tail $filename].sarif"
33
34 # Treat the file as UTF-8 encoded when reading it.
35 set args [append_encoding_arg $args "utf-8"]
36
37 dg-scan "scan-sarif-file" 1 $testcase $output_file $args
38 }
39
40 # Check that a pattern is not present in the .sarif file. See dg-scan
41 # for details.
42
43 proc scan-sarif-file-not { args } {
44 set testcase [testname-for-summary]
45 # The name might include a list of options; extract the file name.
46 set filename [lindex $testcase 0]
47 set output_file "[file tail $filename].sarif"
48
49 # Treat the file as UTF-8 encoded when reading it.
50 set args [append_encoding_arg $args "utf-8"]
51
52 dg-scan "scan-sarif-file-not" 0 $testcase $output_file $args
53 }
54
55 # Perform validity checks on the .sarif file produced by the compiler.
56 #
57 # Assuming python3 is available, use verify-sarif-file.py to check
58 # that the .sarif file is UTF-8 encoded and is parseable as JSON.
59 #
60 # Assuming "check-jsonschema" is available, use it to verify that the .sarif
61 # file complies with the SARIF schema.
62 #
63 # The first argument is the version of the SARIF schema to validate against
64 # If present can be "2.1" or "2.2"
65 # If absent, validate against 2.1
66 #
67 # If present, the second argument is the expected filename of the .sarif file
68
69 proc verify-sarif-file { args } {
70 global srcdir subdir
71
72 set testcase [testname-for-summary]
73 set filename [lindex $testcase 0]
74
75 set version [lindex $args 0]
76 verbose "sarif version: $version" 2
77
78 set output_file [lindex $args 1]
79 verbose "output_file: $output_file" 2
80 if { $output_file == "" } {
81 set output_file "[file tail $filename].sarif"
82 }
83
84 if { ![check_effective_target_recent_python3] } {
85 unsupported "$testcase verify-sarif-file: python3 is missing"
86 return
87 }
88
89 # Verify that the file is correctly encoded and is parseable as JSON.
90 set script_name $srcdir/lib/verify-sarif-file.py
91 set what "$testcase (test .sarif output for UTF-8-encoded parseable JSON)"
92 if [catch {exec python3 $script_name $output_file} res ] {
93 verbose "verify-sarif-file: res: $res" 2
94 fail "$what"
95 return
96 } else {
97 pass "$what"
98 }
99
100 # Verify that the file complies with the SARIF schema.
101
102 # Check that jsonschema is installed.
103 if { ![check_effective_target_check_jsonschema] } {
104 unsupported "$testcase verify-sarif-file: check-jsonschema is missing"
105 return
106 }
107
108 # Handle different versions of SARIF
109 if { $version == "" } {
110 set version "2.1"
111 }
112
113 if { $version == "2.1" } {
114 set schema_file $srcdir/lib/sarif-schema-2.1.0.json
115 } elseif { $version == "2.2" } {
116 set schema_file $srcdir/lib/sarif-schema-2.2-prerelease-2024-08-08.json
117 } else {
118 fail "unrecognized sarif version: $version"
119 return
120 }
121 verbose "schema_file: $schema_file" 2
122
123 set what "$testcase (test .sarif output against SARIF $version schema)"
124 if [catch {exec check-jsonschema --schemafile $schema_file $output_file} res ] {
125 verbose "verify-sarif-file: res: $res" 2
126 fail "$what"
127 return
128 } else {
129 pass "$what"
130 }
131 }
132
133 proc sarif-pytest-format-line { args } {
134 global subdir
135
136 set testcase [lindex $args 0]
137 set pytest_script [lindex $args 1]
138 set output_line [lindex $args 2]
139
140 set index [string first "::" $output_line]
141 set test_output [string range $output_line [expr $index + 2] [string length $output_line]]
142
143 return "$subdir/$testcase ${pytest_script}::${test_output}"
144 }
145
146 # Call by dg-final to run a pytest Python script.
147 # We pass filename of a test via SARIF_PATH environment variable.
148
149 proc run-sarif-pytest { args } {
150 global srcdir subdir
151 # Extract the test file name from the arguments.
152 set testcase [lindex $args 0]
153
154 verbose "Running SARIF $testcase in $srcdir/$subdir" 2
155 set testcase [remote_download host $testcase]
156
157 set pytest_script [lindex $args 1]
158 if { ![check_effective_target_pytest3] } {
159 unsupported "$pytest_script pytest python3 is missing"
160 return
161 }
162
163 setenv SARIF_PATH $testcase
164 set libdir "${srcdir}/lib"
165
166 # Set/prepend libdir to PYTHONPATH
167 if [info exists ::env(PYTHONPATH)] {
168 set old_PYTHONPATH $::env(PYTHONPATH)
169 setenv PYTHONPATH "${libdir}:${old_PYTHONPATH}"
170 } else {
171 setenv PYTHONPATH "${libdir}"
172 }
173
174 verbose "PYTHONPATH=[getenv PYTHONPATH]" 2
175
176 spawn -noecho python3 -m pytest --color=no -rap -s --tb=no $srcdir/$subdir/$pytest_script
177
178 if [info exists old_PYTHONPATH] {
179 setenv PYTHONPATH ${old_PYTHONPATH}
180 }
181
182 set prefix "\[^\r\n\]*"
183 expect {
184 -re "FAILED($prefix)\[^\r\n\]+\r\n" {
185 set output [sarif-pytest-format-line $testcase $pytest_script $expect_out(1,string)]
186 fail $output
187 exp_continue
188 }
189 -re "ERROR($prefix)\[^\r\n\]+\r\n" {
190 set output [sarif-pytest-format-line $testcase $pytest_script $expect_out(1,string)]
191 fail $output
192 exp_continue
193 }
194 -re "PASSED($prefix)\[^\r\n\]+\r\n" {
195 set output [sarif-pytest-format-line $testcase $pytest_script $expect_out(1,string)]
196 pass $output
197 exp_continue
198 }
199 }
200 }
201