[clang] [clang] Register all LLVM targets in AllClangUnitTest main (PR #144428)
Reid Kleckner via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 19 10:16:03 PDT 2025
https://github.com/rnk updated https://github.com/llvm/llvm-project/pull/144428
>From afb050103754e6b4656c68da8ddfb6b1a4e03e5d Mon Sep 17 00:00:00 2001
From: Reid Kleckner <rnk at google.com>
Date: Mon, 16 Jun 2025 20:08:12 +0000
Subject: [PATCH 1/5] [clang] Register all LLVM targets in AllClangUnitTest
main
Addresses feedback in https://github.com/llvm/llvm-project/pull/134196#issuecomment-2970715875
Makes the tests less sensitive to target registration from unrelated
test fixtures by registering everything up front.
---
clang/unittests/AllClangUnitTests.cpp | 24 ++++++++++++++++++++++++
clang/unittests/CMakeLists.txt | 1 +
2 files changed, 25 insertions(+)
create mode 100644 clang/unittests/AllClangUnitTests.cpp
diff --git a/clang/unittests/AllClangUnitTests.cpp b/clang/unittests/AllClangUnitTests.cpp
new file mode 100644
index 0000000000000..1726cabc8107c
--- /dev/null
+++ b/clang/unittests/AllClangUnitTests.cpp
@@ -0,0 +1,24 @@
+//===- clang/unittests/AllClangUnitTests.cpp ------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/TargetSelect.h"
+
+// This custom main entry point for the AllClangUnitTests binary registers all
+// tests on startup, so the tests don't become sensitive to target registration
+// within the test suite.
+int main(int argc, char **argv) {
+ ::testing::InitGoogleTest(&argc, argv);
+ llvm::cl::ParseCommandLineOptions(argc, argv);
+
+ llvm::InitializeAllTargets();
+ llvm::InitializeAllTargetMCs();
+
+ return RUN_ALL_TESTS();
+}
diff --git a/clang/unittests/CMakeLists.txt b/clang/unittests/CMakeLists.txt
index aef28f914b640..54c781a35c20c 100644
--- a/clang/unittests/CMakeLists.txt
+++ b/clang/unittests/CMakeLists.txt
@@ -117,6 +117,7 @@ get_property(LINK_LIBS GLOBAL PROPERTY CLANG_UNITTEST_LINK_LIBS)
get_property(LLVM_COMPONENTS GLOBAL PROPERTY CLANG_UNITTEST_LLVM_COMPONENTS)
add_distinct_clang_unittest(AllClangUnitTests
${SRCS}
+ AllClangUnitTests.cpp
CLANG_LIBS
${CLANG_LIBS}
LINK_LIBS
>From f5accc33950191c2e6eebe8327e247e2a4177d3e Mon Sep 17 00:00:00 2001
From: Reid Kleckner <rnk at google.com>
Date: Mon, 16 Jun 2025 20:15:11 +0000
Subject: [PATCH 2/5] reorder headers to pacify format checker
---
clang/unittests/AllClangUnitTests.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/unittests/AllClangUnitTests.cpp b/clang/unittests/AllClangUnitTests.cpp
index 1726cabc8107c..d1c8da12d5c84 100644
--- a/clang/unittests/AllClangUnitTests.cpp
+++ b/clang/unittests/AllClangUnitTests.cpp
@@ -6,9 +6,9 @@
//
//===----------------------------------------------------------------------===//
-#include "gtest/gtest.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/TargetSelect.h"
+#include "gtest/gtest.h"
// This custom main entry point for the AllClangUnitTests binary registers all
// tests on startup, so the tests don't become sensitive to target registration
>From 1f57ce24f87a22a2975beb928892f4aa6b983f81 Mon Sep 17 00:00:00 2001
From: Reid Kleckner <rnk at google.com>
Date: Tue, 17 Jun 2025 23:37:25 +0000
Subject: [PATCH 3/5] Add additional target registration hooks to match cc1
---
clang/unittests/AllClangUnitTests.cpp | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/clang/unittests/AllClangUnitTests.cpp b/clang/unittests/AllClangUnitTests.cpp
index d1c8da12d5c84..279e51b41d5bf 100644
--- a/clang/unittests/AllClangUnitTests.cpp
+++ b/clang/unittests/AllClangUnitTests.cpp
@@ -17,8 +17,14 @@ int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
llvm::cl::ParseCommandLineOptions(argc, argv);
+ // Initialize all levels of target components. Keep this in sync with
+ // cc1_main.
+ llvm::InitializeAllTargetInfos();
llvm::InitializeAllTargets();
llvm::InitializeAllTargetMCs();
+ llvm::InitializeAllAsmPrinters();
+ llvm::InitializeAllAsmParsers();
+
return RUN_ALL_TESTS();
}
>From 5d9b64763f367f5fe0b5544ea50705e6872d2386 Mon Sep 17 00:00:00 2001
From: Reid Kleckner <rnk at google.com>
Date: Tue, 17 Jun 2025 23:48:56 +0000
Subject: [PATCH 4/5] pacify formatter
---
clang/unittests/AllClangUnitTests.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang/unittests/AllClangUnitTests.cpp b/clang/unittests/AllClangUnitTests.cpp
index 279e51b41d5bf..71d3ec32aa054 100644
--- a/clang/unittests/AllClangUnitTests.cpp
+++ b/clang/unittests/AllClangUnitTests.cpp
@@ -25,6 +25,5 @@ int main(int argc, char **argv) {
llvm::InitializeAllAsmPrinters();
llvm::InitializeAllAsmParsers();
-
return RUN_ALL_TESTS();
}
>From 893096a66e661879988e13735bf3b7e2680f9be8 Mon Sep 17 00:00:00 2001
From: Reid Kleckner <rnk at google.com>
Date: Thu, 19 Jun 2025 17:15:36 +0000
Subject: [PATCH 5/5] Rewrite this to use a dynamic initializer to register all
target layers on startup instead of overriding regular unit test main
---
clang/unittests/AllClangUnitTests.cpp | 23 +++++++++++------------
1 file changed, 11 insertions(+), 12 deletions(-)
diff --git a/clang/unittests/AllClangUnitTests.cpp b/clang/unittests/AllClangUnitTests.cpp
index 71d3ec32aa054..6f33230a617c1 100644
--- a/clang/unittests/AllClangUnitTests.cpp
+++ b/clang/unittests/AllClangUnitTests.cpp
@@ -6,24 +6,23 @@
//
//===----------------------------------------------------------------------===//
-#include "llvm/Support/CommandLine.h"
#include "llvm/Support/TargetSelect.h"
-#include "gtest/gtest.h"
-// This custom main entry point for the AllClangUnitTests binary registers all
-// tests on startup, so the tests don't become sensitive to target registration
-// within the test suite.
-int main(int argc, char **argv) {
- ::testing::InitGoogleTest(&argc, argv);
- llvm::cl::ParseCommandLineOptions(argc, argv);
+namespace {
+struct RegisterAllLLVMTargets {
+ RegisterAllLLVMTargets();
+} gv;
+} // namespace
- // Initialize all levels of target components. Keep this in sync with
- // cc1_main.
+// This dynamic initializer initializes all layers (TargetInfo, MC, CodeGen,
+// AsmPrinter, etc) of all LLVM targets. This matches what cc1_main does on
+// startup, and prevents tests from initializing some of the Target layers,
+// which can interfere with tests that assume that lower target layers are
+// registered if the TargetInfo is registered.
+RegisterAllLLVMTargets::RegisterAllLLVMTargets() {
llvm::InitializeAllTargetInfos();
llvm::InitializeAllTargets();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmPrinters();
llvm::InitializeAllAsmParsers();
-
- return RUN_ALL_TESTS();
}
More information about the cfe-commits
mailing list