Skip to content

Commit b42c883

Browse files
authored
[clang] Build the Z3 mock module via CMake (#146284)
Build the Z3 mock module via CMake rather than compiling it directly in tests. This ensures that the toolchain file is exported, and therefore fixes testing for Gentoo multilib. Also, it ensures that the module is compiled only once for the two tests using it. While at it, remove the related Z3 include directory and host compiler substitutions -- they are not used anymore, and the latter can't be reliably used in tests. The code is based on the existing bits for CTTestTidyModule. See #145731 (comment)
1 parent af82e14 commit b42c883

File tree

5 files changed

+25
-32
lines changed

5 files changed

+25
-32
lines changed

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@
33
// RUN: | FileCheck %s --match-full-lines
44
// CHECK: crosscheck-with-z3-max-attempts-per-query = 3
55

6-
// RUN: rm -rf %t && mkdir %t
7-
// RUN: %host_cxx -shared -fPIC -I %z3_include_dir \
8-
// RUN: %S/z3/Inputs/MockZ3_solver_check.cpp \
9-
// RUN: -o %t/MockZ3_solver_check.so
10-
11-
// DEFINE: %{mocked_clang} = \
12-
// DEFINE: LD_PRELOAD="%t/MockZ3_solver_check.so" \
13-
// DEFINE: %clang_cc1 %s -analyze -setup-static-analyzer \
14-
// DEFINE: -analyzer-config crosscheck-with-z3=true \
6+
// DEFINE: %{mocked_clang} = \
7+
// DEFINE: LD_PRELOAD="%llvmshlibdir/MockZ3SolverCheck%pluginext" \
8+
// DEFINE: %clang_cc1 %s -analyze -setup-static-analyzer \
9+
// DEFINE: -analyzer-config crosscheck-with-z3=true \
1510
// DEFINE: -analyzer-checker=core
1611

1712
// DEFINE: %{attempts} = -analyzer-config crosscheck-with-z3-max-attempts-per-query
@@ -32,7 +27,7 @@
3227
// RUN: Z3_SOLVER_RESULTS="UNDEF,UNDEF,SAT" %{mocked_clang} %{attempts}=3 -verify=accepted
3328

3429

35-
// REQUIRES: z3, z3-devel, asserts, shell, system-linux
30+
// REQUIRES: z3, z3-mock, asserts, shell, system-linux
3631

3732
// refuted-no-diagnostics
3833

clang/test/Analysis/z3/D83660.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
// RUN: rm -rf %t && mkdir %t
2-
// RUN: %host_cxx -shared -fPIC -I %z3_include_dir \
3-
// RUN: %S/Inputs/MockZ3_solver_check.cpp \
4-
// RUN: -o %t/MockZ3_solver_check.so
5-
//
61
// RUN: Z3_SOLVER_RESULTS="SAT,SAT,SAT,SAT,UNDEF" \
7-
// RUN: LD_PRELOAD="%t/MockZ3_solver_check.so" \
2+
// RUN: LD_PRELOAD="%llvmshlibdir/MockZ3SolverCheck%pluginext" \
83
// RUN: %clang_cc1 -analyze -analyzer-constraints=z3 -setup-static-analyzer \
94
// RUN: -analyzer-checker=core %s -verify
105
//
11-
// REQUIRES: z3, z3-devel, asserts, shell, system-linux
6+
// REQUIRES: z3, z3-mock, asserts, shell, system-linux
127
//
138
// Works only with the z3 constraint manager.
149
// expected-no-diagnostics

clang/test/CMakeLists.txt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,21 @@ llvm_canonicalize_cmake_booleans(
3232
# Run tests requiring Z3 headers only if LLVM was built with Z3
3333
# and the headers are available while building Clang -- the latter may
3434
# not be the case when building standalone against installed LLVM.
35-
set(TEST_WITH_Z3_DEVEL 0)
36-
if(LLVM_WITH_Z3 AND Z3_FOUND)
37-
set(TEST_WITH_Z3_DEVEL 1)
35+
set(TEST_WITH_Z3_MOCK 0)
36+
if(LLVM_WITH_Z3 AND Z3_FOUND AND CMAKE_SYSTEM_NAME MATCHES "Linux")
37+
llvm_add_library(
38+
MockZ3SolverCheck
39+
MODULE Analysis/z3/Inputs/MockZ3_solver_check.cpp
40+
DISABLE_LLVM_LINK_LLVM_DYLIB)
41+
42+
if(TARGET MockZ3SolverCheck)
43+
list(APPEND CLANG_TEST_DEPS
44+
MockZ3SolverCheck)
45+
target_include_directories(
46+
MockZ3SolverCheck
47+
PRIVATE ${Z3_INCLUDE_DIR})
48+
set(TEST_WITH_Z3_MOCK 1)
49+
endif()
3850
endif()
3951

4052
configure_lit_site_cfg(

clang/test/lit.cfg.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,8 @@ def have_host_clang_repl_cuda():
201201

202202
if config.clang_staticanalyzer_z3:
203203
config.available_features.add("z3")
204-
if config.clang_staticanalyzer_z3_devel:
205-
config.available_features.add("z3-devel")
206-
config.substitutions.append(
207-
("%z3_include_dir", config.clang_staticanalyzer_z3_include_dir)
208-
)
204+
if config.clang_staticanalyzer_z3_mock:
205+
config.available_features.add("z3-mock")
209206
else:
210207
config.available_features.add("no-z3")
211208

@@ -251,9 +248,6 @@ def have_host_clang_repl_cuda():
251248
)
252249
)
253250

254-
config.substitutions.append(("%host_cc", config.host_cc))
255-
config.substitutions.append(("%host_cxx", config.host_cxx))
256-
257251
# Determine whether the test target is compatible with execution on the host.
258252
if "aarch64" in config.host_arch:
259253
config.available_features.add("aarch64-host")

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ config.clang_tools_dir = lit_config.substitute(path(r"@CURRENT_TOOLS_DIR@"))
1717
config.clang_lib_dir = path(r"@CMAKE_LIBRARY_OUTPUT_DIRECTORY@")
1818
config.host_triple = "@LLVM_HOST_TRIPLE@"
1919
config.target_triple = "@LLVM_TARGET_TRIPLE@"
20-
config.host_cc = "@CMAKE_C_COMPILER@"
21-
config.host_cxx = "@CMAKE_CXX_COMPILER@"
2220
config.llvm_use_sanitizer = "@LLVM_USE_SANITIZER@"
2321
config.have_zlib = @LLVM_ENABLE_ZLIB@
2422
config.have_zstd = @LLVM_ENABLE_ZSTD@
@@ -27,8 +25,7 @@ config.clang_default_pie_on_linux = @CLANG_DEFAULT_PIE_ON_LINUX@
2725
config.clang_default_cxx_stdlib = "@CLANG_DEFAULT_CXX_STDLIB@"
2826
config.clang_staticanalyzer = @CLANG_ENABLE_STATIC_ANALYZER@
2927
config.clang_staticanalyzer_z3 = @LLVM_WITH_Z3@
30-
config.clang_staticanalyzer_z3_devel = @TEST_WITH_Z3_DEVEL@
31-
config.clang_staticanalyzer_z3_include_dir = "@Z3_INCLUDE_DIR@"
28+
config.clang_staticanalyzer_z3_mock = @TEST_WITH_Z3_MOCK@
3229
config.clang_enable_cir = @CLANG_ENABLE_CIR@
3330
config.clang_examples = @CLANG_BUILD_EXAMPLES@
3431
config.enable_shared = @ENABLE_SHARED@

0 commit comments

Comments
 (0)