Skip to content

Commit 176f8d7

Browse files
NagyDonatAnthony Tran
authored andcommitted
[NFC][analyzer] Remove Z3-as-constraint-manager hacks from lit test code (llvm#145731)
Before this commit the LIT test framework of the static analyzer had a file called `analyzer_test.py` which implemented a tricky system for selecting the constraint manager: - (A) Test files without `REQUIRES: z3` were executed with the default range-based constraint manager. - (B) If clang was built with Z3 support _and_ `USE_Z3_SOLVER=1` was passed to the test run, the test was executed with Z3 as the constraint manager. (There was support for executing the same RUN line twice if both conditions were satisfied.) Unfortunately, using Z3 as the constraint manager does not work in practice (very slow and causes many crashes), so the (B) pathway became unused (or was never truly used?) and became broken due to bit rot. (In the CI bots the analyzer is built without Z3 support, so only the pathway (A) is used.) This commit removes `analyzer_test.py` (+ related logic in other build files + the test `z3/enabled.c` which just tested that `analyzer_test.py` is active), because it tries to implement a feature that we don't need (only one constraint manager is functional) and its code is so complicated and buggy that it isn't useful as a starting point for future development. The fact that this logic was broken implied that tests with `REQUIRES: z3` were not executed during normal testing, so they were also affected by bit rot. Unfortunately this also affected the tests of the `z3-crosscheck` mode (aka Z3 refutation) which also depends on Z3 but uses Z3 in a different way which is actually stable and functional. In this commit I'm fixing most of the `REQUIRES: z3` tests that were broken by straightforward issues. Two test files, `PR37855.c` and `z3-crosscheck.c` were affected by more complex issues, so I marked them as `XFAIL` for now. We're planning to fix them with follow-up commits in the foreseeable future. For additional background information see also the discourse thread https://discourse.llvm.org/t/taking-ownership-of-clang-test-analysis/84689
1 parent 57082f4 commit 176f8d7

19 files changed

+22
-130
lines changed

clang/docs/analyzer/developer-docs/DebugChecks.rst

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -317,15 +317,10 @@ ExprInspection checks
317317
The value can be represented either as a range set or as a concrete integer.
318318
For the rest of the types function prints ``n/a`` (aka not available).
319319

320-
**Note:** This function will print nothing for clang built with Z3 constraint manager.
321-
This may cause crashes of your tests. To manage this use one of the test constraining
322-
techniques:
323-
324-
* llvm-lit commands ``REQUIRES no-z3`` or ``UNSUPPORTED z3`` `See for details. <https://llvm.org/docs/TestingGuide.html#constraining-test-execution>`_
325-
326-
* a preprocessor directive ``#ifndef ANALYZER_CM_Z3``
327-
328-
* a clang command argument ``-analyzer-constraints=range``
320+
**Note:** This function will print nothing when clang uses Z3 as the
321+
constraint manager (which is an unsupported and badly broken analysis mode
322+
that's distinct from the supported and stable "Z3 refutation" aka "Z3
323+
crosscheck" mode).
329324

330325
Example usage::
331326

clang/test/Analysis/PR37855.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// RUN: %clang_cc1 -analyze -analyzer-checker=core -w -analyzer-config crosscheck-with-z3=true -verify %s
33
// REQUIRES: z3
44

5+
// XFAIL: *
6+
57
typedef struct o p;
68
struct o {
79
struct {

clang/test/Analysis/analyzer_test.py

Lines changed: 0 additions & 53 deletions
This file was deleted.

clang/test/Analysis/bool-assignment.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ void test_BOOL_initialization(int y) {
4343
return;
4444
}
4545
if (y > 200 && y < 250) {
46-
#ifdef ANALYZER_CM_Z3
47-
BOOL x = y; // expected-warning {{Assignment of a non-Boolean value}}
48-
#else
4946
BOOL x = y; // no-warning
50-
#endif
5147
return;
5248
}
5349
if (y >= 127 && y < 150) {

clang/test/Analysis/cstring-addrspace.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// RUN: %clang_analyze_cc1 -triple amdgcn-unknown-unknown \
2-
// RUN: -analyze -analyzer-checker=core,alpha.unix.cstring \
3-
// RUN: -analyze -analyzer-checker=debug.ExprInspection \
2+
// RUN: -analyzer-checker=core,alpha.unix.cstring,debug.ExprInspection \
43
// RUN: -analyzer-config crosscheck-with-z3=true -verify %s \
54
// RUN: -Wno-incompatible-library-redeclaration
65
// REQUIRES: z3

clang/test/Analysis/lit.local.cfg

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
# -*- Python -*- vim: set ft=python ts=4 sw=4 expandtab tw=79:
2-
from lit.llvm.subst import ToolSubst
3-
import site
2+
import lit.formats
43

5-
# Load the custom analyzer test format, which runs the test again with Z3 if it
6-
# is available.
7-
site.addsitedir(os.path.dirname(__file__))
8-
import analyzer_test
9-
10-
config.test_format = analyzer_test.AnalyzerTest(
11-
config.test_format.execute_external, config.use_z3_solver
12-
)
4+
config.test_format = lit.formats.ShTest(config.test_format.execute_external)
135

146
# Filtering command used by Clang Analyzer tests (when comparing .plist files
157
# with reference output)

clang/test/Analysis/ptr-arith.c

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,7 @@ void comparisons_imply_size(int *lhs, int *rhs) {
221221
}
222222

223223
clang_analyzer_eval(lhs <= rhs); // expected-warning{{TRUE}}
224-
// FIXME: In Z3ConstraintManager, ptrdiff_t is mapped to signed bitvector. However, this does not directly imply the unsigned comparison.
225-
#ifdef ANALYZER_CM_Z3
226-
clang_analyzer_eval((rhs - lhs) >= 0); // expected-warning{{UNKNOWN}}
227-
#else
228224
clang_analyzer_eval((rhs - lhs) >= 0); // expected-warning{{TRUE}}
229-
#endif
230225
clang_analyzer_eval((rhs - lhs) > 0); // expected-warning{{UNKNOWN}}
231226

232227
if (lhs >= rhs) {
@@ -236,11 +231,7 @@ void comparisons_imply_size(int *lhs, int *rhs) {
236231

237232
clang_analyzer_eval(lhs == rhs); // expected-warning{{FALSE}}
238233
clang_analyzer_eval(lhs < rhs); // expected-warning{{TRUE}}
239-
#ifdef ANALYZER_CM_Z3
240-
clang_analyzer_eval((rhs - lhs) > 0); // expected-warning{{UNKNOWN}}
241-
#else
242234
clang_analyzer_eval((rhs - lhs) > 0); // expected-warning{{TRUE}}
243-
#endif
244235
}
245236

246237
void size_implies_comparison(int *lhs, int *rhs) {
@@ -251,11 +242,7 @@ void size_implies_comparison(int *lhs, int *rhs) {
251242
return;
252243
}
253244

254-
#ifdef ANALYZER_CM_Z3
255-
clang_analyzer_eval(lhs <= rhs); // expected-warning{{UNKNOWN}}
256-
#else
257245
clang_analyzer_eval(lhs <= rhs); // expected-warning{{TRUE}}
258-
#endif
259246
clang_analyzer_eval((rhs - lhs) >= 0); // expected-warning{{TRUE}}
260247
clang_analyzer_eval((rhs - lhs) > 0); // expected-warning{{UNKNOWN}}
261248

@@ -265,11 +252,7 @@ void size_implies_comparison(int *lhs, int *rhs) {
265252
}
266253

267254
clang_analyzer_eval(lhs == rhs); // expected-warning{{FALSE}}
268-
#ifdef ANALYZER_CM_Z3
269-
clang_analyzer_eval(lhs < rhs); // expected-warning{{UNKNOWN}}
270-
#else
271255
clang_analyzer_eval(lhs < rhs); // expected-warning{{TRUE}}
272-
#endif
273256
clang_analyzer_eval((rhs - lhs) > 0); // expected-warning{{TRUE}}
274257
}
275258

clang/test/Analysis/reference.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -123,26 +123,10 @@ S getS();
123123
S *getSP();
124124

125125
void testReferenceAddress(int &x) {
126-
// FIXME: Move non-zero reference assumption out of RangeConstraintManager.cpp:422
127-
#ifdef ANALYZER_CM_Z3
128-
clang_analyzer_eval(&x != 0); // expected-warning{{UNKNOWN}}
129-
clang_analyzer_eval(&ref() != 0); // expected-warning{{UNKNOWN}}
130-
#else
131126
clang_analyzer_eval(&x != 0); // expected-warning{{TRUE}}
132127
clang_analyzer_eval(&ref() != 0); // expected-warning{{TRUE}}
133-
#endif
134-
135-
#ifdef ANALYZER_CM_Z3
136-
clang_analyzer_eval(&getS().x != 0); // expected-warning{{UNKNOWN}}
137-
#else
138128
clang_analyzer_eval(&getS().x != 0); // expected-warning{{TRUE}}
139-
#endif
140-
141-
#ifdef ANALYZER_CM_Z3
142-
clang_analyzer_eval(&getSP()->x != 0); // expected-warning{{UNKNOWN}}
143-
#else
144129
clang_analyzer_eval(&getSP()->x != 0); // expected-warning{{TRUE}}
145-
#endif
146130
}
147131
}
148132

clang/test/Analysis/unary-sym-expr-z3-refutation.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ void k(long L) {
2929
int h = g + 1;
3030
int j;
3131
j += -h < 0; // should not crash
32-
// expected-warning@-1{{garbage}}
32+
// expected-warning@-1{{The left expression of the compound assignment uses uninitialized memory [core.uninitialized.Assign]}}
3333
}

clang/test/Analysis/z3-crosscheck-max-attempts.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// CHECK: crosscheck-with-z3-max-attempts-per-query = 3
55

66
// RUN: rm -rf %t && mkdir %t
7-
// RUN: %host_cxx -shared -fPIC \
7+
// RUN: %host_cxx -shared -fPIC -I %z3_include_dir \
88
// RUN: %S/z3/Inputs/MockZ3_solver_check.cpp \
99
// RUN: -o %t/MockZ3_solver_check.so
1010

clang/test/Analysis/z3-crosscheck.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -analyze -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-config crosscheck-with-z3=true -verify %s
33
// REQUIRES: z3
44

5+
// XFAIL: *
6+
57
void clang_analyzer_dump(float);
68

79
int foo(int x)
@@ -64,7 +66,7 @@ void floatUnaryNegInEq(int h, int l) {
6466
clang_analyzer_dump((float)l); // expected-warning-re {{(float) (reg_${{[0-9]+}}<int l>)}}
6567
if (-(float)h != (float)l) { // should not crash
6668
j += 10;
67-
// expected-warning@-1{{garbage}}
69+
// expected-warning@-1{{The left expression of the compound assignment uses uninitialized memory [core.uninitialized.Assign]}}
6870
}
6971
}
7072

@@ -74,7 +76,7 @@ void floatUnaryLNotInEq(int h, int l) {
7476
clang_analyzer_dump((float)l); // expected-warning-re {{(float) (reg_${{[0-9]+}}<int l>)}}
7577
if ((!(float)h) != (float)l) { // should not crash
7678
j += 10;
77-
// expected-warning@-1{{garbage}}
79+
// expected-warning@-1{{The left expression of the compound assignment uses uninitialized memory [core.uninitialized.Assign]}}
7880
}
7981
}
8082

clang/test/Analysis/z3/D83660.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: rm -rf %t && mkdir %t
2-
// RUN: %host_cxx -shared -fPIC \
2+
// RUN: %host_cxx -shared -fPIC -I %z3_include_dir \
33
// RUN: %S/Inputs/MockZ3_solver_check.cpp \
44
// RUN: -o %t/MockZ3_solver_check.so
55
//

clang/test/Analysis/z3/crosscheck-statistics.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
// REQUIRES: z3
66

7-
// expected-error@1 {{Z3 refutation rate:1/2}}
8-
97
int accepting(int n) {
108
if (n == 4) {
119
n = n / (n-4); // expected-warning {{Division by zero}}

clang/test/Analysis/z3/enabled.c

Lines changed: 0 additions & 3 deletions
This file was deleted.

clang/test/CMakeLists.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,6 @@ if(LLVM_INCLUDE_SPIRV_TOOLS_TESTS)
124124
)
125125
endif()
126126

127-
set(CLANG_TEST_PARAMS
128-
USE_Z3_SOLVER=0
129-
)
130-
131127
if( NOT CLANG_BUILT_STANDALONE )
132128
list(APPEND CLANG_TEST_DEPS
133129
llvm-config
@@ -195,13 +191,11 @@ set_target_properties(clang-test-depends PROPERTIES FOLDER "Clang/Tests")
195191
add_lit_testsuite(check-clang "Running the Clang regression tests"
196192
${CMAKE_CURRENT_BINARY_DIR}
197193
#LIT ${LLVM_LIT}
198-
PARAMS ${CLANG_TEST_PARAMS}
199194
DEPENDS ${CLANG_TEST_DEPS}
200195
ARGS ${CLANG_TEST_EXTRA_ARGS}
201196
)
202197

203198
add_lit_testsuites(CLANG ${CMAKE_CURRENT_SOURCE_DIR}
204-
PARAMS ${CLANG_TEST_PARAMS}
205199
DEPENDS ${CLANG_TEST_DEPS}
206200
FOLDER "Clang tests/Suites"
207201
)

clang/test/lit.cfg.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ def have_host_clang_repl_cuda():
175175

176176
if config.clang_staticanalyzer_z3:
177177
config.available_features.add("z3")
178+
config.substitutions.append(
179+
("%z3_include_dir", config.clang_staticanalyzer_z3_include_dir)
180+
)
178181
else:
179182
config.available_features.add("no-z3")
180183

clang/test/lit.site.cfg.py.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ config.clang_default_pie_on_linux = @CLANG_DEFAULT_PIE_ON_LINUX@
2727
config.clang_default_cxx_stdlib = "@CLANG_DEFAULT_CXX_STDLIB@"
2828
config.clang_staticanalyzer = @CLANG_ENABLE_STATIC_ANALYZER@
2929
config.clang_staticanalyzer_z3 = @LLVM_WITH_Z3@
30+
config.clang_staticanalyzer_z3_include_dir = "@Z3_INCLUDE_DIR@"
3031
config.clang_enable_cir = @CLANG_ENABLE_CIR@
3132
config.clang_examples = @CLANG_BUILD_EXAMPLES@
3233
config.enable_shared = @ENABLE_SHARED@
@@ -39,7 +40,6 @@ config.reverse_iteration = @LLVM_ENABLE_REVERSE_ITERATION@
3940
config.host_arch = "@HOST_ARCH@"
4041
config.perl_executable = "@PERL_EXECUTABLE@"
4142
config.python_executable = "@Python3_EXECUTABLE@"
42-
config.use_z3_solver = lit_config.params.get('USE_Z3_SOLVER', "@USE_Z3_SOLVER@")
4343
config.has_plugins = @CLANG_PLUGIN_SUPPORT@
4444
config.clang_vendor_uti = "@CLANG_VENDOR_UTI@"
4545
config.llvm_external_lit = path(r"@LLVM_EXTERNAL_LIT@")

llvm/utils/gn/secondary/clang/test/BUILD.gn

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ write_lit_config("lit_site_cfg") {
7777
"LLVM_USE_SANITIZER=",
7878
"LLVM_VERSION_MAJOR=$llvm_version_major",
7979
"Python3_EXECUTABLE=$python_path",
80-
"USE_Z3_SOLVER=",
8180
"PPC_LINUX_DEFAULT_IEEELONGDOUBLE=0",
8281
]
8382

llvm/utils/lit/lit/llvm/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,8 @@ def use_clang(
629629
ToolSubst(
630630
"%clang_analyze_cc1",
631631
command="%clang_cc1",
632-
extra_args=["-analyze", "%analyze", "-setup-static-analyzer"]
632+
# -setup-static-analyzer ensures that __clang_analyzer__ is defined
633+
extra_args=["-analyze", "-setup-static-analyzer"]
633634
+ additional_flags,
634635
),
635636
ToolSubst(

0 commit comments

Comments
 (0)