Skip to content

Commit 3c6f47d

Browse files
authored
[llvm-driver] Fix usage of InitLLVM on Windows (#76306)
Previously, some tools such as `clang` or `lld` which require strict order for certain command-line options, such as `clang -cc1` or `lld -flavor`, would not longer work on Windows, when these tools were linked as part of `llvm-driver`. This was caused by `InitLLVM` which was part of the `*_main()` function of these tools, which in turn calls `windows::GetCommandLineArguments`. That function completly replaces argc/argv by new UTF-8 contents, so any ajustements to argc/argv made by `llvm-driver` prior to calling these tools was reset. `InitLLVM` is now called by the `llvm-driver`. Any tool that participates in (or is part of) the `llvm-driver` doesn't call `InitLLVM` anymore.
1 parent 4cee0e3 commit 3c6f47d

File tree

25 files changed

+12
-48
lines changed

25 files changed

+12
-48
lines changed

clang/tools/clang-scan-deps/ClangScanDeps.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "llvm/Support/CommandLine.h"
2121
#include "llvm/Support/FileUtilities.h"
2222
#include "llvm/Support/Format.h"
23-
#include "llvm/Support/InitLLVM.h"
2423
#include "llvm/Support/JSON.h"
2524
#include "llvm/Support/LLVMDriver.h"
2625
#include "llvm/Support/Program.h"
@@ -699,7 +698,6 @@ static std::string getModuleCachePath(ArrayRef<std::string> Args) {
699698
// form specified command line after the positional parameter "--".
700699
static std::unique_ptr<tooling::CompilationDatabase>
701700
getCompilationDataBase(int argc, char **argv, std::string &ErrorMessage) {
702-
llvm::InitLLVM X(argc, argv);
703701
ParseArgs(argc, argv);
704702

705703
if (!CompilationDB.empty())

clang/tools/driver/driver.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include "llvm/Support/CrashRecoveryContext.h"
3737
#include "llvm/Support/ErrorHandling.h"
3838
#include "llvm/Support/FileSystem.h"
39-
#include "llvm/Support/InitLLVM.h"
4039
#include "llvm/Support/LLVMDriver.h"
4140
#include "llvm/Support/Path.h"
4241
#include "llvm/Support/PrettyStackTrace.h"
@@ -377,7 +376,6 @@ static int ExecuteCC1Tool(SmallVectorImpl<const char *> &ArgV,
377376

378377
int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContext) {
379378
noteBottomOfStack();
380-
llvm::InitLLVM X(Argc, Argv);
381379
llvm::setBugReportMsg("PLEASE submit a bug report to " BUG_REPORT_URL
382380
" and include the crash backtrace, preprocessed "
383381
"source, and associated run script.\n");

lld/Common/DriverDispatcher.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "llvm/ADT/Twine.h"
1717
#include "llvm/Support/CommandLine.h"
1818
#include "llvm/Support/CrashRecoveryContext.h"
19-
#include "llvm/Support/InitLLVM.h"
2019
#include "llvm/Support/Path.h"
2120
#include "llvm/Support/Process.h"
2221
#include "llvm/TargetParser/Host.h"

lld/tools/lld/lld.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include "llvm/ADT/Twine.h"
3434
#include "llvm/Support/CommandLine.h"
3535
#include "llvm/Support/CrashRecoveryContext.h"
36-
#include "llvm/Support/InitLLVM.h"
3736
#include "llvm/Support/LLVMDriver.h"
3837
#include "llvm/Support/Path.h"
3938
#include "llvm/Support/PluginLoader.h"
@@ -74,7 +73,6 @@ LLD_HAS_DRIVER(macho)
7473
LLD_HAS_DRIVER(wasm)
7574

7675
int lld_main(int argc, char **argv, const llvm::ToolContext &) {
77-
InitLLVM x(argc, argv);
7876
sys::Process::UseANSIEscapeCodes(true);
7977

8078
if (::getenv("FORCE_LLD_DIAGNOSTICS_CRASH")) {

llvm/cmake/modules/llvm-driver-template.cpp.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88

99
#include "llvm/Support/LLVMDriver.h"
1010
#include "llvm/ADT/ArrayRef.h"
11+
#include "llvm/Support/InitLLVM.h"
1112

1213
int @TOOL_NAME@_main(int argc, char **, const llvm::ToolContext &);
1314

1415
int main(int argc, char **argv) {
16+
llvm::InitLLVM X(argc, argv);
1517
return @TOOL_NAME@_main(argc, argv, {argv[0], nullptr, false});
1618
}

llvm/lib/Support/InitLLVM.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ using namespace llvm::sys;
3838

3939
InitLLVM::InitLLVM(int &Argc, const char **&Argv,
4040
bool InstallPipeSignalExitHandler) {
41+
#ifndef NDEBUG
42+
static std::atomic<bool> Initialized{false};
43+
assert(!Initialized && "InitLLVM was already initialized!");
44+
Initialized = true;
45+
#endif
4146
#ifdef __MVS__
4247
// Bring stdin/stdout/stderr into a known state.
4348
sys::AddSignalHandler(CleanupStdHandles, nullptr);

llvm/tools/dsymutil/dsymutil.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#include "llvm/Support/FileCollector.h"
3838
#include "llvm/Support/FileSystem.h"
3939
#include "llvm/Support/FormatVariadic.h"
40-
#include "llvm/Support/InitLLVM.h"
4140
#include "llvm/Support/LLVMDriver.h"
4241
#include "llvm/Support/Path.h"
4342
#include "llvm/Support/TargetSelect.h"
@@ -608,8 +607,6 @@ getOutputFileName(StringRef InputFile, const DsymutilOptions &Options) {
608607
}
609608

610609
int dsymutil_main(int argc, char **argv, const llvm::ToolContext &) {
611-
InitLLVM X(argc, argv);
612-
613610
// Parse arguments.
614611
DsymutilOptTable T;
615612
unsigned MAI;

llvm/tools/llvm-ar/llvm-ar.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "llvm/Support/FileSystem.h"
2626
#include "llvm/Support/Format.h"
2727
#include "llvm/Support/FormatVariadic.h"
28-
#include "llvm/Support/InitLLVM.h"
2928
#include "llvm/Support/LLVMDriver.h"
3029
#include "llvm/Support/LineIterator.h"
3130
#include "llvm/Support/MemoryBuffer.h"
@@ -1500,7 +1499,6 @@ static int ranlib_main(int argc, char **argv) {
15001499
}
15011500

15021501
int llvm_ar_main(int argc, char **argv, const llvm::ToolContext &) {
1503-
InitLLVM X(argc, argv);
15041502
ToolName = argv[0];
15051503

15061504
llvm::InitializeAllTargetInfos();

llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "llvm/Option/ArgList.h"
1414
#include "llvm/Option/Option.h"
1515
#include "llvm/Support/CommandLine.h"
16-
#include "llvm/Support/InitLLVM.h"
1716
#include "llvm/Support/LLVMDriver.h"
1817
#include "llvm/Support/WithColor.h"
1918
#include "llvm/Support/raw_ostream.h"
@@ -146,7 +145,6 @@ static void demangleLine(llvm::raw_ostream &OS, StringRef Mangled, bool Split) {
146145
}
147146

148147
int llvm_cxxfilt_main(int argc, char **argv, const llvm::ToolContext &) {
149-
InitLLVM X(argc, argv);
150148
BumpPtrAllocator A;
151149
StringSaver Saver(A);
152150
CxxfiltOptTable Tbl;

llvm/tools/llvm-debuginfod/llvm-debuginfod.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "llvm/Option/ArgList.h"
2323
#include "llvm/Option/Option.h"
2424
#include "llvm/Support/CommandLine.h"
25-
#include "llvm/Support/InitLLVM.h"
2625
#include "llvm/Support/LLVMDriver.h"
2726
#include "llvm/Support/ThreadPool.h"
2827

@@ -121,7 +120,6 @@ static void parseArgs(int argc, char **argv) {
121120
}
122121

123122
int llvm_debuginfod_main(int argc, char **argv, const llvm::ToolContext &) {
124-
InitLLVM X(argc, argv);
125123
HTTPClient::initialize();
126124
parseArgs(argc, argv);
127125

0 commit comments

Comments
 (0)