From 69cd7e0c4c6437c53bf8fe4e380a77ab57e0a6bb Mon Sep 17 00:00:00 2001 From: davedets Date: Mon, 8 Jun 2026 19:44:01 -0700 Subject: [PATCH] Fixes necessary to compile z3 included in clang-tidy via FetchContents. (#9768) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The page https://github.com/Z3Prover/z3/blob/master/README-CMake.md#adding-z3-as-a-dependency-to-a-cmake-project advises using the CMake FetchContent feature to include z3 as source into other CMake project. I'm trying to do this to use Z3 within a ClangTidy checker. This is one of a series of PR's aimed at getting Z3 to compile cleanly when included this way. This initial PR fixes all the errors, allowing the compilation to succeed. Subsequent diffs will address warnings. I tested only the CMake compilation, on a Mac. *Missing Z3_THROWs* Update z3++.h to use Z3_THROW in a couple of places. Clang compiles with exceptions disabled so we get messages like: ``` /Users/daviddetlefs/llvm-project/build_dbg/_deps/z3-src/src/api/c++/z3++.h:4928:17: error: cannot use 'throw' with exceptions disabled4928 | throw exception("rcf_num objects from different contexts"); ``` NOTE TO REVIEWERS: I'm not complete clear on the usage conventions for Z3_THROW. With exception disabled, it seems like the throwing function will just continue. If there's somethign else that should be done, like setting some error state, please let me know. *CMake component name collision* There was an error at the CMake level, a name collision (on "opt"). Apparently CMake components are named using a flat namespace, so it's easy to see how this could occur. It seems to me that the right global way to fix this would be to encourage people to use some form of "qualified name" convention in naming their component. The fix I chose was a local version of this, changing the Z3 component name to z3_opt. (It didn't seem feasible to make the change in clang.) NOTE TO REVIEWERS: If you think this is OK, please let me know if a) You'd like me to also change the name of the opt directory, to keep thecomponent-name == directory-name invariant, and b) You'd like me to make this z3_ change more globally, to future-proof (somewhat) against similar component name collisions. --- src/api/CMakeLists.txt | 2 +- src/api/c++/z3++.h | 6 +++--- src/opt/CMakeLists.txt | 2 +- src/shell/CMakeLists.txt | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/api/CMakeLists.txt b/src/api/CMakeLists.txt index c1e78f473..c6ad57c70 100644 --- a/src/api/CMakeLists.txt +++ b/src/api/CMakeLists.txt @@ -66,7 +66,7 @@ z3_add_component(api z3_replayer.cpp ${full_path_generated_files} COMPONENT_DEPENDENCIES - opt + z3_opt euf portfolio realclosure diff --git a/src/api/c++/z3++.h b/src/api/c++/z3++.h index f66bd1f76..9e8cc6796 100644 --- a/src/api/c++/z3++.h +++ b/src/api/c++/z3++.h @@ -4925,7 +4925,7 @@ namespace z3 { void check_context(rcf_num const& other) const { if (m_ctx != other.m_ctx) { - throw exception("rcf_num objects from different contexts"); + Z3_THROW(exception("rcf_num objects from different contexts")); } } @@ -5105,9 +5105,9 @@ namespace z3 { */ inline std::vector rcf_roots(context& c, std::vector const& coeffs) { if (coeffs.empty()) { - throw exception("polynomial coefficients cannot be empty"); + Z3_THROW(exception("polynomial coefficients cannot be empty")); } - + unsigned n = static_cast(coeffs.size()); std::vector a(n); std::vector roots(n); diff --git a/src/opt/CMakeLists.txt b/src/opt/CMakeLists.txt index 21075d88c..85b716306 100644 --- a/src/opt/CMakeLists.txt +++ b/src/opt/CMakeLists.txt @@ -1,4 +1,4 @@ -z3_add_component(opt +z3_add_component(z3_opt SOURCES maxcore.cpp maxlex.cpp diff --git a/src/shell/CMakeLists.txt b/src/shell/CMakeLists.txt index c0e9c8505..22c071966 100644 --- a/src/shell/CMakeLists.txt +++ b/src/shell/CMakeLists.txt @@ -8,7 +8,7 @@ set (shell_object_files "") # We are only using these dependencies to enforce a build # order. We don't use this list for actual linking. -set(shell_deps api extra_cmds opt sat) +set(shell_deps api extra_cmds z3_opt sat) z3_expand_dependencies(shell_expanded_deps ${shell_deps}) get_property(Z3_LIBZ3_COMPONENTS_LIST GLOBAL PROPERTY Z3_LIBZ3_COMPONENTS) foreach (component ${Z3_LIBZ3_COMPONENTS_LIST})