Index: clang/test/Analysis/scan-build/lit.local.cfg =================================================================== --- clang/test/Analysis/scan-build/lit.local.cfg +++ clang/test/Analysis/scan-build/lit.local.cfg @@ -7,6 +7,8 @@ use_lit_shell = os.environ.get("LIT_USE_INTERNAL_SHELL") config.test_format = lit.formats.ShTest(use_lit_shell == "0") +clang_path = config.clang if config.is_llvm_driver else os.path.realpath(config.clang) + config.substitutions.append(('%scan-build', '\'%s\' --use-analyzer=%s ' % ( lit.util.which('scan-build', @@ -15,4 +17,4 @@ 'tools', 'scan-build', 'bin')), - os.path.realpath(config.clang)))) + clang_path))) Index: clang/test/CMakeLists.txt =================================================================== --- clang/test/CMakeLists.txt +++ clang/test/CMakeLists.txt @@ -15,6 +15,7 @@ LLVM_ENABLE_PER_TARGET_RUNTIME_DIR LLVM_ENABLE_THREADS LLVM_WITH_Z3 + LLVM_TOOL_LLVM_DRIVER_BUILD ) configure_lit_site_cfg( Index: clang/test/CodeGen/debug-info-codeview-buildinfo.c =================================================================== --- clang/test/CodeGen/debug-info-codeview-buildinfo.c +++ clang/test/CodeGen/debug-info-codeview-buildinfo.c @@ -11,7 +11,7 @@ // CHECK: 0x[[PWD:.+]] | LF_STRING_ID [size = {{.+}}] ID: , String: [[PWDVAL:.+]] // CHECK: 0x[[FILEPATH:.+]] | LF_STRING_ID [size = {{.+}}] ID: , String: [[FILEPATHVAL:.+[\\/]debug-info-codeview-buildinfo.c]] // CHECK: 0x[[ZIPDB:.+]] | LF_STRING_ID [size = {{.+}}] ID: , String: -// CHECK: 0x[[TOOL:.+]] | LF_STRING_ID [size = {{.+}}] ID: , String: [[TOOLVAL:.+[\\/]clang.*]] +// CHECK: 0x[[TOOL:.+]] | LF_STRING_ID [size = {{.+}}] ID: , String: [[TOOLVAL:.+[\\/][clang|llvm].*]] // CHECK: 0x[[CMDLINE:.+]] | LF_STRING_ID [size = {{.+}}] ID: , String: "-cc1 // CHECK: 0x{{.+}} | LF_BUILDINFO [size = {{.+}}] // CHECK: 0x[[PWD]]: `[[PWDVAL]]` Index: clang/test/Driver/amdgpu-toolchain.c =================================================================== --- clang/test/Driver/amdgpu-toolchain.c +++ clang/test/Driver/amdgpu-toolchain.c @@ -12,5 +12,5 @@ // RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx906 -nogpulib \ // RUN: -flto %s 2>&1 | FileCheck -check-prefix=LTO %s -// LTO: clang{{.*}} "-flto=full" +// LTO: {{clang|llvm}}{{.*}} "-flto=full" // LTO: ld.lld{{.*}} Index: clang/test/Driver/check-time-trace.cpp =================================================================== --- clang/test/Driver/check-time-trace.cpp +++ clang/test/Driver/check-time-trace.cpp @@ -27,7 +27,7 @@ // CHECK-NEXT: "pid": // CHECK-NEXT: "tid": // CHECK-NEXT: "ts": -// CHECK: "name": "clang{{.*}}" +// CHECK: "name": "{{clang|llvm}}{{.*}}" // CHECK: "name": "process_name" // CHECK: "name": "thread_name" Index: clang/test/Driver/flang/multiple-inputs-mixed.f90 =================================================================== --- clang/test/Driver/flang/multiple-inputs-mixed.f90 +++ clang/test/Driver/flang/multiple-inputs-mixed.f90 @@ -3,5 +3,5 @@ ! RUN: %clang --driver-mode=flang -### -fsyntax-only %S/Inputs/one.f90 %S/Inputs/other.c 2>&1 | FileCheck --check-prefixes=CHECK-SYNTAX-ONLY %s ! CHECK-SYNTAX-ONLY-LABEL: "{{[^"]*}}flang-new{{[^"/]*}}" "-fc1" ! CHECK-SYNTAX-ONLY: "{{[^"]*}}/Inputs/one.f90" -! CHECK-SYNTAX-ONLY-LABEL: "{{[^"]*}}clang{{[^"/]*}}" "-cc1" +! CHECK-SYNTAX-ONLY-LABEL: "{{[^"]*}}{{clang|llvm}}{{[^"/]*}}" "-cc1" ! CHECK-SYNTAX-ONLY: "{{[^"]*}}/Inputs/other.c" Index: clang/test/Driver/hipspv-device-libs.hip =================================================================== --- clang/test/Driver/hipspv-device-libs.hip +++ clang/test/Driver/hipspv-device-libs.hip @@ -19,7 +19,7 @@ // RUN: --hip-device-lib=a.bc --hip-device-lib=b.bc %s \ // RUN: 2>&1 | FileCheck --check-prefixes=ALL,HIP-DEV-LIB %s -// ALL: {{"[^"]*clang[^"]*"}} +// ALL: {{"[^"]*[clang|llvm][^"]*"}} // HIP-PATH: "-mlink-builtin-bitcode" {{".*/hipspv/lib/hip-device-lib/hipspv-spirv64.bc"}} // HIP-DEV-LIB-PATH-NOT: "-mlink-builtin-bitcode" {{".*/hipspv/lib/hip-device-lib/hipspv-spirv64.bc"}} // HIP-DEV-LIB-PATH: "-mlink-builtin-bitcode" {{".*/hipspv-dev-lib/hipspv-spirv64.bc"}} Index: clang/test/Driver/hipspv-toolchain-rdc.hip =================================================================== --- clang/test/Driver/hipspv-toolchain-rdc.hip +++ clang/test/Driver/hipspv-toolchain-rdc.hip @@ -8,7 +8,7 @@ // RUN: 2>&1 | FileCheck %s // Emit objects for host side path -// CHECK: [[CLANG:".*clang.*"]] "-cc1" "-triple" "x86_64-unknown-linux-gnu" +// CHECK: [[CLANG:".*[clang|llvm].*"]] "-cc1" "-triple" "x86_64-unknown-linux-gnu" // CHECK-SAME: "-aux-triple" "spirv64" // CHECK-SAME: "-emit-obj" // CHECK-SAME: "-fgpu-rdc" Index: clang/test/Driver/hipspv-toolchain.hip =================================================================== --- clang/test/Driver/hipspv-toolchain.hip +++ clang/test/Driver/hipspv-toolchain.hip @@ -5,7 +5,7 @@ // RUN: --hip-path=%S/Inputs/hipspv -nohipwrapperinc %s \ // RUN: 2>&1 | FileCheck %s -// CHECK: [[CLANG:".*clang.*"]] "-cc1" "-triple" "spirv64" +// CHECK: [[CLANG:".*[clang|llvm].*"]] "-cc1" "-triple" "spirv64" // CHECK-SAME: "-aux-triple" "{{.*}}" "-emit-llvm-bc" // CHECK-SAME: "-fcuda-is-device" // CHECK-SAME: "-fcuda-allow-variadic-functions" Index: clang/test/Driver/ignore-xcoff-visibility.cpp =================================================================== --- clang/test/Driver/ignore-xcoff-visibility.cpp +++ clang/test/Driver/ignore-xcoff-visibility.cpp @@ -1,6 +1,6 @@ // RUN: %clang -### -target powerpc-unknown-aix -mignore-xcoff-visibility -S %s 2> %t.log // RUN: FileCheck -check-prefix=CHECK %s < %t.log - CHECK: {{.*}}clang{{.*}}" "-cc1" + CHECK: {{.*}}{{clang|llvm}}{{.*}}" "-cc1" CHECK: "-mignore-xcoff-visibility" // RUN: not %clang -mignore-xcoff-visibility -target powerpc-unknown-linux %s 2>&1 | \ Index: clang/test/Driver/no-canonical-prefixes.c =================================================================== --- clang/test/Driver/no-canonical-prefixes.c +++ clang/test/Driver/no-canonical-prefixes.c @@ -27,7 +27,7 @@ // // FIXME: This should really be '.real'. // CANONICAL: InstalledDir: {{.*}}.fake -// CANONICAL: {{[/|\\]*}}clang{{.*}}" -cc1 +// CANONICAL: {{[/|\\]*}}{{clang|llvm}}{{.*}}" -cc1 // // NON-CANONICAL: InstalledDir: .{{$}} // NON-CANONICAL: test-clang" -cc1 Index: clang/test/Driver/ps4-ps5-header-search.c =================================================================== --- clang/test/Driver/ps4-ps5-header-search.c +++ clang/test/Driver/ps4-ps5-header-search.c @@ -7,7 +7,7 @@ // RUN: %clang -isysroot %S/Inputs/scei-ps4_tree -target x86_64-scei-ps4 -E -v %s 2>&1 | FileCheck %s --check-prefix=SYSROOTPS4 // RUN: %clang -isysroot %S/Inputs/scei-ps4_tree -target x86_64-sie-ps5 -E -v %s 2>&1 | FileCheck %s --check-prefix=SYSROOTPS4 -// SYSROOTPS4: "{{[^"]*}}clang{{[^"]*}}" +// SYSROOTPS4: "{{[^"]*}}{{clang|llvm}}{{[^"]*}}" // SYSROOTPS4: Inputs/scei-ps4_tree/target/include{{$}} // SYSROOTPS4: Inputs/scei-ps4_tree/target/include_common{{$}} // SYSROOTPS4-NOT: /usr/include Index: clang/test/lit.site.cfg.py.in =================================================================== --- clang/test/lit.site.cfg.py.in +++ clang/test/lit.site.cfg.py.in @@ -38,6 +38,7 @@ config.clang_vendor_uti = "@CLANG_VENDOR_UTI@" config.llvm_external_lit = path(r"@LLVM_EXTERNAL_LIT@") config.standalone_build = @CLANG_BUILT_STANDALONE@ +config.is_llvm_driver = @LLVM_TOOL_LLVM_DRIVER_BUILD@ import lit.llvm lit.llvm.initialize(lit_config, config) Index: clang/tools/driver/driver.cpp =================================================================== --- clang/tools/driver/driver.cpp +++ clang/tools/driver/driver.cpp @@ -37,6 +37,7 @@ #include "llvm/Support/FileSystem.h" #include "llvm/Support/Host.h" #include "llvm/Support/InitLLVM.h" +#include "llvm/Support/LLVMDriver.h" #include "llvm/Support/Path.h" #include "llvm/Support/PrettyStackTrace.h" #include "llvm/Support/Process.h" @@ -269,12 +270,15 @@ } static void FixupDiagPrefixExeName(TextDiagnosticPrinter *DiagClient, - const std::string &Path) { + const std::string &Path, + llvm::StringRef Argv0) { // If the clang binary happens to be named cl.exe for compatibility reasons, // use clang-cl.exe as the prefix to avoid confusion between clang and MSVC. StringRef ExeBasename(llvm::sys::path::stem(Path)); if (ExeBasename.equals_insensitive("cl")) ExeBasename = "clang-cl"; + if (llvm::IsLLVMDriver) + ExeBasename = llvm::sys::path::stem(Argv0); DiagClient->setPrefix(std::string(ExeBasename)); } @@ -450,7 +454,7 @@ TextDiagnosticPrinter *DiagClient = new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts); - FixupDiagPrefixExeName(DiagClient, Path); + FixupDiagPrefixExeName(DiagClient, Path, Args[0]); IntrusiveRefCntPtr DiagID(new DiagnosticIDs()); Index: clang/tools/scan-build/libexec/ccc-analyzer =================================================================== --- clang/tools/scan-build/libexec/ccc-analyzer +++ clang/tools/scan-build/libexec/ccc-analyzer @@ -201,7 +201,7 @@ $line =~ s/^\s+|\s+$//g; my @items = quotewords('\s+', 0, $line); my $cmd = shift @items; - die "cannot find 'clang' in 'clang' command\n" if (!($cmd =~ /clang/)); + die "cannot find 'clang' in 'clang' command\n" if (!($cmd =~ /clang/ || basename($cmd) =~ /llvm/)); return \@items; } Index: llvm/cmake/modules/llvm-driver-template.cpp.in =================================================================== --- llvm/cmake/modules/llvm-driver-template.cpp.in +++ llvm/cmake/modules/llvm-driver-template.cpp.in @@ -6,6 +6,13 @@ // //===----------------------------------------------------------------------===// +namespace llvm { + +extern const bool IsLLVMDriver = false; + +} // namespace llvm + + int @TOOL_NAME@_main(int argc, char **argv); int main(int argc, char **argv) { return @TOOL_NAME@_main(argc, argv); } Index: llvm/include/llvm/Support/LLVMDriver.h =================================================================== --- /dev/null +++ llvm/include/llvm/Support/LLVMDriver.h @@ -0,0 +1,17 @@ +//===- LLVMDriver.h ---------------------------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// The llvm-driver is an executable which statically links in many llvm tools +// into one. This can be useful for tools which need to behave slightly +// differently under that environment. +// +//===----------------------------------------------------------------------===// + +namespace llvm { +extern const bool IsLLVMDriver; +} // namespace llvm Index: llvm/lib/Support/Path.cpp =================================================================== --- llvm/lib/Support/Path.cpp +++ llvm/lib/Support/Path.cpp @@ -1202,18 +1202,10 @@ #include "Windows/Path.inc" #endif -bool IsLLVMDriver = false; - namespace llvm { namespace sys { namespace fs { -std::string getMainExecutable(const char *Argv0, void *MainAddr) { - if (IsLLVMDriver) - return sys::path::stem(Argv0).str(); - return getMainExecutableImpl(Argv0, MainAddr); -} - TempFile::TempFile(StringRef Name, int FD) : TmpName(std::string(Name)), FD(FD) {} TempFile::TempFile(TempFile &&Other) { *this = std::move(Other); } Index: llvm/lib/Support/Unix/Path.inc =================================================================== --- llvm/lib/Support/Unix/Path.inc +++ llvm/lib/Support/Unix/Path.inc @@ -194,7 +194,7 @@ /// GetMainExecutable - Return the path to the main executable, given the /// value of argv[0] from program startup. -std::string getMainExecutableImpl(const char *argv0, void *MainAddr) { +std::string getMainExecutable(const char *argv0, void *MainAddr) { #if defined(__APPLE__) // On OS X the executable path is saved to the stack by dyld. Reading it // from there is much faster than calling dladdr, especially for large Index: llvm/lib/Support/Windows/Path.inc =================================================================== --- llvm/lib/Support/Windows/Path.inc +++ llvm/lib/Support/Windows/Path.inc @@ -130,7 +130,7 @@ const file_t kInvalidFile = INVALID_HANDLE_VALUE; -std::string getMainExecutableImpl(const char *argv0, void *MainExecAddr) { +std::string getMainExecutable(const char *argv0, void *MainExecAddr) { SmallVector PathName; PathName.resize_for_overwrite(PathName.capacity()); DWORD Size = ::GetModuleFileNameW(NULL, PathName.data(), PathName.size()); Index: llvm/tools/llvm-driver/llvm-driver.cpp =================================================================== --- llvm/tools/llvm-driver/llvm-driver.cpp +++ llvm/tools/llvm-driver/llvm-driver.cpp @@ -11,9 +11,16 @@ #include "llvm/ADT/StringSwitch.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/LLVMDriver.h" #include "llvm/Support/Path.h" #include "llvm/Support/WithColor.h" +namespace llvm { + +const bool IsLLVMDriver = true; + +} + using namespace llvm; #define LLVM_DRIVER_TOOL(tool, entry) int entry##_main(int argc, char **argv); @@ -24,6 +31,14 @@ #include "LLVMDriverTools.def" ; +constexpr bool isClangDefined() { +#define LLVM_DRIVER_TOOL(tool, entry) \ + if constexpr (std::string_view(#entry) == "clang") \ + return true; +#include "LLVMDriverTools.def" + return false; +} + static void printHelpMessage() { llvm::outs() << "OVERVIEW: llvm toolchain driver\n\n" << "USAGE: llvm [subcommand] [options]\n\n" @@ -47,6 +62,14 @@ return 0; } + if constexpr (isClangDefined()) { + if (Argc > 1) { + StringRef Argv1 = Argv[1]; + if (Argv1 == "-cc1" || Argv1 == "-cc1as" || Argv1 == "-cc1gen-reproducer") + return clang_main(Argc, Argv); + } + } + StringRef Stem = sys::path::stem(ToolName); auto Is = [=](StringRef Tool) { auto IsImpl = [=](StringRef Stem) { @@ -72,9 +95,4 @@ return 1; } -extern bool IsLLVMDriver; - -int main(int Argc, char **Argv) { - IsLLVMDriver = true; - return findTool(Argc, Argv); -} +int main(int Argc, char **Argv) { return findTool(Argc, Argv); }