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

llvm/tools/llvm-driver/llvm-driver.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "llvm/ADT/StringRef.h"
1111
#include "llvm/Support/CommandLine.h"
1212
#include "llvm/Support/ErrorHandling.h"
13+
#include "llvm/Support/InitLLVM.h"
1314
#include "llvm/Support/LLVMDriver.h"
1415
#include "llvm/Support/Path.h"
1516
#include "llvm/Support/WithColor.h"
@@ -79,4 +80,7 @@ static int findTool(int Argc, char **Argv, const char *Argv0) {
7980
return 1;
8081
}
8182

82-
int main(int Argc, char **Argv) { return findTool(Argc, Argv, Argv[0]); }
83+
int main(int Argc, char **Argv) {
84+
llvm::InitLLVM X(Argc, Argv);
85+
return findTool(Argc, Argv, Argv[0]);
86+
}

llvm/tools/llvm-dwp/llvm-dwp.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "llvm/Option/Option.h"
2828
#include "llvm/Support/CommandLine.h"
2929
#include "llvm/Support/FileSystem.h"
30-
#include "llvm/Support/InitLLVM.h"
3130
#include "llvm/Support/LLVMDriver.h"
3231
#include "llvm/Support/MemoryBuffer.h"
3332
#include "llvm/Support/TargetSelect.h"
@@ -120,8 +119,6 @@ static Expected<Triple> readTargetTriple(StringRef FileName) {
120119
}
121120

122121
int llvm_dwp_main(int argc, char **argv, const llvm::ToolContext &) {
123-
InitLLVM X(argc, argv);
124-
125122
DwpOptTable Tbl;
126123
llvm::BumpPtrAllocator A;
127124
llvm::StringSaver Saver{A};

llvm/tools/llvm-libtool-darwin/llvm-libtool-darwin.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "llvm/Option/ArgList.h"
2424
#include "llvm/Option/Option.h"
2525
#include "llvm/Support/CommandLine.h"
26-
#include "llvm/Support/InitLLVM.h"
2726
#include "llvm/Support/LLVMDriver.h"
2827
#include "llvm/Support/LineIterator.h"
2928
#include "llvm/Support/TargetSelect.h"
@@ -727,7 +726,6 @@ static Expected<Config> parseCommandLine(int Argc, char **Argv) {
727726
}
728727

729728
int llvm_libtool_darwin_main(int Argc, char **Argv, const llvm::ToolContext &) {
730-
InitLLVM X(Argc, Argv);
731729
Expected<Config> ConfigOrErr = parseCommandLine(Argc, Argv);
732730
if (!ConfigOrErr) {
733731
WithColor::defaultErrorHandler(ConfigOrErr.takeError());

llvm/tools/llvm-lipo/llvm-lipo.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "llvm/Support/CommandLine.h"
2727
#include "llvm/Support/Error.h"
2828
#include "llvm/Support/FileOutputBuffer.h"
29-
#include "llvm/Support/InitLLVM.h"
3029
#include "llvm/Support/LLVMDriver.h"
3130
#include "llvm/Support/TargetSelect.h"
3231
#include "llvm/Support/WithColor.h"
@@ -724,7 +723,6 @@ replaceSlices(LLVMContext &LLVMCtx,
724723
}
725724

726725
int llvm_lipo_main(int argc, char **argv, const llvm::ToolContext &) {
727-
InitLLVM X(argc, argv);
728726
llvm::InitializeAllTargetInfos();
729727
llvm::InitializeAllTargetMCs();
730728
llvm::InitializeAllAsmParsers();

llvm/tools/llvm-ml/llvm-ml.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include "llvm/Support/FileUtilities.h"
3535
#include "llvm/Support/FormatVariadic.h"
3636
#include "llvm/Support/FormattedStream.h"
37-
#include "llvm/Support/InitLLVM.h"
3837
#include "llvm/Support/LLVMDriver.h"
3938
#include "llvm/Support/MemoryBuffer.h"
4039
#include "llvm/Support/Path.h"
@@ -187,7 +186,6 @@ static int AssembleInput(StringRef ProgName, const Target *TheTarget,
187186
}
188187

189188
int llvm_ml_main(int Argc, char **Argv, const llvm::ToolContext &) {
190-
InitLLVM X(Argc, Argv);
191189
StringRef ProgName = sys::path::filename(Argv[0]);
192190

193191
// Initialize targets and assembly printers/parsers.

llvm/tools/llvm-mt/llvm-mt.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "llvm/Option/Option.h"
1717
#include "llvm/Support/Error.h"
1818
#include "llvm/Support/FileOutputBuffer.h"
19-
#include "llvm/Support/InitLLVM.h"
2019
#include "llvm/Support/LLVMDriver.h"
2120
#include "llvm/Support/MemoryBuffer.h"
2221
#include "llvm/Support/Path.h"
@@ -77,8 +76,6 @@ static void error(Error EC) {
7776
}
7877

7978
int llvm_mt_main(int Argc, char **Argv, const llvm::ToolContext &) {
80-
InitLLVM X(Argc, Argv);
81-
8279
CvtResOptTable T;
8380
unsigned MAI, MAC;
8481
ArrayRef<const char *> ArgsArr = ArrayRef(Argv + 1, Argc - 1);

llvm/tools/llvm-nm/llvm-nm.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
#include "llvm/Support/CommandLine.h"
4343
#include "llvm/Support/FileSystem.h"
4444
#include "llvm/Support/Format.h"
45-
#include "llvm/Support/InitLLVM.h"
4645
#include "llvm/Support/LLVMDriver.h"
4746
#include "llvm/Support/MemoryBuffer.h"
4847
#include "llvm/Support/Program.h"
@@ -2405,7 +2404,6 @@ exportSymbolNamesFromFiles(const std::vector<std::string> &InputFilenames) {
24052404
}
24062405

24072406
int llvm_nm_main(int argc, char **argv, const llvm::ToolContext &) {
2408-
InitLLVM X(argc, argv);
24092407
BumpPtrAllocator A;
24102408
StringSaver Saver(A);
24112409
NmOptTable Tbl;

llvm/tools/llvm-objcopy/llvm-objcopy.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
#include "llvm/Support/ErrorHandling.h"
4343
#include "llvm/Support/ErrorOr.h"
4444
#include "llvm/Support/FileUtilities.h"
45-
#include "llvm/Support/InitLLVM.h"
4645
#include "llvm/Support/LLVMDriver.h"
4746
#include "llvm/Support/Memory.h"
4847
#include "llvm/Support/Path.h"
@@ -224,7 +223,6 @@ static Error executeObjcopy(ConfigManager &ConfigMgr) {
224223
}
225224

226225
int llvm_objcopy_main(int argc, char **argv, const llvm::ToolContext &) {
227-
InitLLVM X(argc, argv);
228226
ToolName = argv[0];
229227

230228
// Expand response files.

llvm/tools/llvm-objdump/llvm-objdump.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
#include "llvm/Support/Format.h"
7272
#include "llvm/Support/FormatVariadic.h"
7373
#include "llvm/Support/GraphWriter.h"
74-
#include "llvm/Support/InitLLVM.h"
7574
#include "llvm/Support/LLVMDriver.h"
7675
#include "llvm/Support/MemoryBuffer.h"
7776
#include "llvm/Support/SourceMgr.h"
@@ -3417,7 +3416,6 @@ static void parseObjdumpOptions(const llvm::opt::InputArgList &InputArgs) {
34173416

34183417
int llvm_objdump_main(int argc, char **argv, const llvm::ToolContext &) {
34193418
using namespace llvm;
3420-
InitLLVM X(argc, argv);
34213419

34223420
ToolName = argv[0];
34233421
std::unique_ptr<CommonOptTable> T;

llvm/tools/llvm-profdata/llvm-profdata.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include "llvm/Support/FileSystem.h"
3131
#include "llvm/Support/Format.h"
3232
#include "llvm/Support/FormattedStream.h"
33-
#include "llvm/Support/InitLLVM.h"
3433
#include "llvm/Support/LLVMDriver.h"
3534
#include "llvm/Support/MD5.h"
3635
#include "llvm/Support/MemoryBuffer.h"
@@ -3176,7 +3175,6 @@ static int order_main(int argc, const char *argv[]) {
31763175
int llvm_profdata_main(int argc, char **argvNonConst,
31773176
const llvm::ToolContext &) {
31783177
const char **argv = const_cast<const char **>(argvNonConst);
3179-
InitLLVM X(argc, argv);
31803178

31813179
StringRef ProgName(sys::path::filename(argv[0]));
31823180

llvm/tools/llvm-rc/llvm-rc.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "llvm/Support/Error.h"
2727
#include "llvm/Support/FileSystem.h"
2828
#include "llvm/Support/FileUtilities.h"
29-
#include "llvm/Support/InitLLVM.h"
3029
#include "llvm/Support/LLVMDriver.h"
3130
#include "llvm/Support/MemoryBuffer.h"
3231
#include "llvm/Support/Path.h"
@@ -731,7 +730,6 @@ void doCvtres(std::string Src, std::string Dest, std::string TargetTriple) {
731730
} // anonymous namespace
732731

733732
int llvm_rc_main(int Argc, char **Argv, const llvm::ToolContext &) {
734-
InitLLVM X(Argc, Argv);
735733
ExitOnErr.setBanner("llvm-rc: ");
736734

737735
char **DashDash = std::find_if(Argv + 1, Argv + Argc,

llvm/tools/llvm-readobj/llvm-readobj.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
#include "llvm/Support/Errc.h"
4343
#include "llvm/Support/FileSystem.h"
4444
#include "llvm/Support/FormatVariadic.h"
45-
#include "llvm/Support/InitLLVM.h"
4645
#include "llvm/Support/LLVMDriver.h"
4746
#include "llvm/Support/Path.h"
4847
#include "llvm/Support/ScopedPrinter.h"
@@ -633,7 +632,6 @@ std::unique_ptr<ScopedPrinter> createWriter() {
633632
}
634633

635634
int llvm_readobj_main(int argc, char **argv, const llvm::ToolContext &) {
636-
InitLLVM X(argc, argv);
637635
BumpPtrAllocator A;
638636
StringSaver Saver(A);
639637
ReadobjOptTable Tbl;

llvm/tools/llvm-size/llvm-size.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "llvm/Support/CommandLine.h"
2626
#include "llvm/Support/FileSystem.h"
2727
#include "llvm/Support/Format.h"
28-
#include "llvm/Support/InitLLVM.h"
2928
#include "llvm/Support/LLVMDriver.h"
3029
#include "llvm/Support/MemoryBuffer.h"
3130
#include "llvm/Support/WithColor.h"
@@ -863,7 +862,6 @@ static void printBerkeleyTotals() {
863862
}
864863

865864
int llvm_size_main(int argc, char **argv, const llvm::ToolContext &) {
866-
InitLLVM X(argc, argv);
867865
BumpPtrAllocator A;
868866
StringSaver Saver(A);
869867
SizeOptTable Tbl;

llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include "llvm/Support/Debug.h"
3535
#include "llvm/Support/Errc.h"
3636
#include "llvm/Support/FileSystem.h"
37-
#include "llvm/Support/InitLLVM.h"
3837
#include "llvm/Support/LLVMDriver.h"
3938
#include "llvm/Support/Path.h"
4039
#include "llvm/Support/StringSaver.h"
@@ -462,7 +461,6 @@ static void filterMarkup(const opt::InputArgList &Args, LLVMSymbolizer &Symboliz
462461
}
463462

464463
int llvm_symbolizer_main(int argc, char **argv, const llvm::ToolContext &) {
465-
InitLLVM X(argc, argv);
466464
sys::InitializeCOMRAII COM(sys::COMThreadingMode::MultiThreaded);
467465

468466
ToolName = argv[0];

llvm/tools/sancov/sancov.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include "llvm/Support/Errc.h"
3737
#include "llvm/Support/ErrorOr.h"
3838
#include "llvm/Support/FileSystem.h"
39-
#include "llvm/Support/InitLLVM.h"
4039
#include "llvm/Support/JSON.h"
4140
#include "llvm/Support/LLVMDriver.h"
4241
#include "llvm/Support/MD5.h"
@@ -1215,8 +1214,6 @@ static void parseArgs(int Argc, char **Argv) {
12151214
}
12161215

12171216
int sancov_main(int Argc, char **Argv, const llvm::ToolContext &) {
1218-
llvm::InitLLVM X(Argc, Argv);
1219-
12201217
llvm::InitializeAllTargetInfos();
12211218
llvm::InitializeAllTargetMCs();
12221219
llvm::InitializeAllDisassemblers();

0 commit comments

Comments
 (0)