Skip to content

Commit

Permalink
[llvm-driver] Fix usage of InitLLVM on Windows (llvm#76306)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aganea authored Jan 12, 2024
1 parent 4cee0e3 commit 3c6f47d
Show file tree
Hide file tree
Showing 25 changed files with 12 additions and 48 deletions.
2 changes: 0 additions & 2 deletions clang/tools/clang-scan-deps/ClangScanDeps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/Program.h"
Expand Down Expand Up @@ -699,7 +698,6 @@ static std::string getModuleCachePath(ArrayRef<std::string> Args) {
// form specified command line after the positional parameter "--".
static std::unique_ptr<tooling::CompilationDatabase>
getCompilationDataBase(int argc, char **argv, std::string &ErrorMessage) {
llvm::InitLLVM X(argc, argv);
ParseArgs(argc, argv);

if (!CompilationDB.empty())
Expand Down
2 changes: 0 additions & 2 deletions clang/tools/driver/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include "llvm/Support/CrashRecoveryContext.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/PrettyStackTrace.h"
Expand Down Expand Up @@ -377,7 +376,6 @@ static int ExecuteCC1Tool(SmallVectorImpl<const char *> &ArgV,

int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContext) {
noteBottomOfStack();
llvm::InitLLVM X(Argc, Argv);
llvm::setBugReportMsg("PLEASE submit a bug report to " BUG_REPORT_URL
" and include the crash backtrace, preprocessed "
"source, and associated run script.\n");
Expand Down
1 change: 0 additions & 1 deletion lld/Common/DriverDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "llvm/ADT/Twine.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/CrashRecoveryContext.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Process.h"
#include "llvm/TargetParser/Host.h"
Expand Down
2 changes: 0 additions & 2 deletions lld/tools/lld/lld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include "llvm/ADT/Twine.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/CrashRecoveryContext.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/PluginLoader.h"
Expand Down Expand Up @@ -74,7 +73,6 @@ LLD_HAS_DRIVER(macho)
LLD_HAS_DRIVER(wasm)

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

if (::getenv("FORCE_LLD_DIAGNOSTICS_CRASH")) {
Expand Down
2 changes: 2 additions & 0 deletions llvm/cmake/modules/llvm-driver-template.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

#include "llvm/Support/LLVMDriver.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/InitLLVM.h"

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

int main(int argc, char **argv) {
llvm::InitLLVM X(argc, argv);
return @TOOL_NAME@_main(argc, argv, {argv[0], nullptr, false});
}
5 changes: 5 additions & 0 deletions llvm/lib/Support/InitLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ using namespace llvm::sys;

InitLLVM::InitLLVM(int &Argc, const char **&Argv,
bool InstallPipeSignalExitHandler) {
#ifndef NDEBUG
static std::atomic<bool> Initialized{false};
assert(!Initialized && "InitLLVM was already initialized!");
Initialized = true;
#endif
#ifdef __MVS__
// Bring stdin/stdout/stderr into a known state.
sys::AddSignalHandler(CleanupStdHandles, nullptr);
Expand Down
3 changes: 0 additions & 3 deletions llvm/tools/dsymutil/dsymutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include "llvm/Support/FileCollector.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/TargetSelect.h"
Expand Down Expand Up @@ -608,8 +607,6 @@ getOutputFileName(StringRef InputFile, const DsymutilOptions &Options) {
}

int dsymutil_main(int argc, char **argv, const llvm::ToolContext &) {
InitLLVM X(argc, argv);

// Parse arguments.
DsymutilOptTable T;
unsigned MAI;
Expand Down
2 changes: 0 additions & 2 deletions llvm/tools/llvm-ar/llvm-ar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/LineIterator.h"
#include "llvm/Support/MemoryBuffer.h"
Expand Down Expand Up @@ -1500,7 +1499,6 @@ static int ranlib_main(int argc, char **argv) {
}

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

llvm::InitializeAllTargetInfos();
Expand Down
2 changes: 0 additions & 2 deletions llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "llvm/Option/ArgList.h"
#include "llvm/Option/Option.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/WithColor.h"
#include "llvm/Support/raw_ostream.h"
Expand Down Expand Up @@ -146,7 +145,6 @@ static void demangleLine(llvm::raw_ostream &OS, StringRef Mangled, bool Split) {
}

int llvm_cxxfilt_main(int argc, char **argv, const llvm::ToolContext &) {
InitLLVM X(argc, argv);
BumpPtrAllocator A;
StringSaver Saver(A);
CxxfiltOptTable Tbl;
Expand Down
2 changes: 0 additions & 2 deletions llvm/tools/llvm-debuginfod/llvm-debuginfod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "llvm/Option/ArgList.h"
#include "llvm/Option/Option.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/ThreadPool.h"

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

int llvm_debuginfod_main(int argc, char **argv, const llvm::ToolContext &) {
InitLLVM X(argc, argv);
HTTPClient::initialize();
parseArgs(argc, argv);

Expand Down
6 changes: 5 additions & 1 deletion llvm/tools/llvm-driver/llvm-driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/WithColor.h"
Expand Down Expand Up @@ -79,4 +80,7 @@ static int findTool(int Argc, char **Argv, const char *Argv0) {
return 1;
}

int main(int Argc, char **Argv) { return findTool(Argc, Argv, Argv[0]); }
int main(int Argc, char **Argv) {
llvm::InitLLVM X(Argc, Argv);
return findTool(Argc, Argv, Argv[0]);
}
3 changes: 0 additions & 3 deletions llvm/tools/llvm-dwp/llvm-dwp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "llvm/Option/Option.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/TargetSelect.h"
Expand Down Expand Up @@ -120,8 +119,6 @@ static Expected<Triple> readTargetTriple(StringRef FileName) {
}

int llvm_dwp_main(int argc, char **argv, const llvm::ToolContext &) {
InitLLVM X(argc, argv);

DwpOptTable Tbl;
llvm::BumpPtrAllocator A;
llvm::StringSaver Saver{A};
Expand Down
2 changes: 0 additions & 2 deletions llvm/tools/llvm-libtool-darwin/llvm-libtool-darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "llvm/Option/ArgList.h"
#include "llvm/Option/Option.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/LineIterator.h"
#include "llvm/Support/TargetSelect.h"
Expand Down Expand Up @@ -727,7 +726,6 @@ static Expected<Config> parseCommandLine(int Argc, char **Argv) {
}

int llvm_libtool_darwin_main(int Argc, char **Argv, const llvm::ToolContext &) {
InitLLVM X(Argc, Argv);
Expected<Config> ConfigOrErr = parseCommandLine(Argc, Argv);
if (!ConfigOrErr) {
WithColor::defaultErrorHandler(ConfigOrErr.takeError());
Expand Down
2 changes: 0 additions & 2 deletions llvm/tools/llvm-lipo/llvm-lipo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FileOutputBuffer.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/WithColor.h"
Expand Down Expand Up @@ -724,7 +723,6 @@ replaceSlices(LLVMContext &LLVMCtx,
}

int llvm_lipo_main(int argc, char **argv, const llvm::ToolContext &) {
InitLLVM X(argc, argv);
llvm::InitializeAllTargetInfos();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmParsers();
Expand Down
2 changes: 0 additions & 2 deletions llvm/tools/llvm-ml/llvm-ml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
Expand Down Expand Up @@ -187,7 +186,6 @@ static int AssembleInput(StringRef ProgName, const Target *TheTarget,
}

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

// Initialize targets and assembly printers/parsers.
Expand Down
3 changes: 0 additions & 3 deletions llvm/tools/llvm-mt/llvm-mt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "llvm/Option/Option.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FileOutputBuffer.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
Expand Down Expand Up @@ -77,8 +76,6 @@ static void error(Error EC) {
}

int llvm_mt_main(int Argc, char **Argv, const llvm::ToolContext &) {
InitLLVM X(Argc, Argv);

CvtResOptTable T;
unsigned MAI, MAC;
ArrayRef<const char *> ArgsArr = ArrayRef(Argv + 1, Argc - 1);
Expand Down
2 changes: 0 additions & 2 deletions llvm/tools/llvm-nm/llvm-nm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Program.h"
Expand Down Expand Up @@ -2405,7 +2404,6 @@ exportSymbolNamesFromFiles(const std::vector<std::string> &InputFilenames) {
}

int llvm_nm_main(int argc, char **argv, const llvm::ToolContext &) {
InitLLVM X(argc, argv);
BumpPtrAllocator A;
StringSaver Saver(A);
NmOptTable Tbl;
Expand Down
2 changes: 0 additions & 2 deletions llvm/tools/llvm-objcopy/llvm-objcopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/Memory.h"
#include "llvm/Support/Path.h"
Expand Down Expand Up @@ -224,7 +223,6 @@ static Error executeObjcopy(ConfigManager &ConfigMgr) {
}

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

// Expand response files.
Expand Down
2 changes: 0 additions & 2 deletions llvm/tools/llvm-objdump/llvm-objdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
#include "llvm/Support/Format.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/GraphWriter.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/SourceMgr.h"
Expand Down Expand Up @@ -3417,7 +3416,6 @@ static void parseObjdumpOptions(const llvm::opt::InputArgList &InputArgs) {

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

ToolName = argv[0];
std::unique_ptr<CommonOptTable> T;
Expand Down
2 changes: 0 additions & 2 deletions llvm/tools/llvm-profdata/llvm-profdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/MD5.h"
#include "llvm/Support/MemoryBuffer.h"
Expand Down Expand Up @@ -3176,7 +3175,6 @@ static int order_main(int argc, const char *argv[]) {
int llvm_profdata_main(int argc, char **argvNonConst,
const llvm::ToolContext &) {
const char **argv = const_cast<const char **>(argvNonConst);
InitLLVM X(argc, argv);

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

Expand Down
2 changes: 0 additions & 2 deletions llvm/tools/llvm-rc/llvm-rc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "llvm/Support/Error.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
Expand Down Expand Up @@ -731,7 +730,6 @@ void doCvtres(std::string Src, std::string Dest, std::string TargetTriple) {
} // anonymous namespace

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

char **DashDash = std::find_if(Argv + 1, Argv + Argc,
Expand Down
2 changes: 0 additions & 2 deletions llvm/tools/llvm-readobj/llvm-readobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#include "llvm/Support/Errc.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/ScopedPrinter.h"
Expand Down Expand Up @@ -633,7 +632,6 @@ std::unique_ptr<ScopedPrinter> createWriter() {
}

int llvm_readobj_main(int argc, char **argv, const llvm::ToolContext &) {
InitLLVM X(argc, argv);
BumpPtrAllocator A;
StringSaver Saver(A);
ReadobjOptTable Tbl;
Expand Down
2 changes: 0 additions & 2 deletions llvm/tools/llvm-size/llvm-size.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/WithColor.h"
Expand Down Expand Up @@ -863,7 +862,6 @@ static void printBerkeleyTotals() {
}

int llvm_size_main(int argc, char **argv, const llvm::ToolContext &) {
InitLLVM X(argc, argv);
BumpPtrAllocator A;
StringSaver Saver(A);
SizeOptTable Tbl;
Expand Down
2 changes: 0 additions & 2 deletions llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include "llvm/Support/Debug.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/StringSaver.h"
Expand Down Expand Up @@ -462,7 +461,6 @@ static void filterMarkup(const opt::InputArgList &Args, LLVMSymbolizer &Symboliz
}

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

ToolName = argv[0];
Expand Down
3 changes: 0 additions & 3 deletions llvm/tools/sancov/sancov.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include "llvm/Support/Errc.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/MD5.h"
Expand Down Expand Up @@ -1215,8 +1214,6 @@ static void parseArgs(int Argc, char **Argv) {
}

int sancov_main(int Argc, char **Argv, const llvm::ToolContext &) {
llvm::InitLLVM X(Argc, Argv);

llvm::InitializeAllTargetInfos();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllDisassemblers();
Expand Down

0 comments on commit 3c6f47d

Please sign in to comment.