8 changes: 7 additions & 1 deletion clang/tools/scan-build/libexec/ccc-analyzer
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,13 @@ sub GetCCArgs {
$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/));
# If this is the llvm-driver the internal command will look like "llvm clang ...".
# Later this will be invoked like "clang clang ...", so skip over it.
if (basename($cmd) =~ /llvm/) {
die "Expected first arg to llvm driver to be 'clang'" if $items[0] ne "clang";
shift @items;
}
return \@items;
}

Expand Down
3 changes: 2 additions & 1 deletion lld/tools/lld/lld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#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"
#include "llvm/Support/Process.h"
Expand Down Expand Up @@ -214,7 +215,7 @@ static unsigned inTestVerbosity() {
return v;
}

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

Expand Down
9 changes: 7 additions & 2 deletions llvm/cmake/modules/llvm-driver-template.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
//
//===----------------------------------------------------------------------===//

int @TOOL_NAME@_main(int argc, char **argv);
#include "llvm/Support/LLVMDriver.h"
#include "llvm/ADT/ArrayRef.h"

int main(int argc, char **argv) { return @TOOL_NAME@_main(argc, argv); }
int @TOOL_NAME@_main(int argc, char **, const llvm::ToolContext &);

int main(int argc, char **argv) {
return @TOOL_NAME@_main(argc, argv, {argv[0], nullptr, false});
}
29 changes: 29 additions & 0 deletions llvm/include/llvm/Support/LLVMDriver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===- 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
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SUPPORT_LLVMDRIVER_H
#define LLVM_SUPPORT_LLVMDRIVER_H

#include "llvm/ADT/SmallVector.h"

namespace llvm {

struct ToolContext {
const char *Path;
const char *PrependArg;
// PrependArg will be added unconditionally by the llvm-driver, but
// NeedsPrependArg will be false if Path is adequate to reinvoke the tool.
// This is useful if realpath is ever called on Path, in which case it will
// point to the llvm-driver executable, where PrependArg will be needed to
// invoke the correct tool.
bool NeedsPrependArg;
};

} // namespace llvm

#endif
8 changes: 0 additions & 8 deletions llvm/lib/Support/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1202,18 +1202,10 @@ Error readNativeFileToEOF(file_t FileHandle, SmallVectorImpl<char> &Buffer,
#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); }
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Support/Unix/Path.inc
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ static char *getprogpath(char ret[PATH_MAX], const char *bin) {

/// 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
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Support/Windows/Path.inc
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ namespace fs {

const file_t kInvalidFile = INVALID_HANDLE_VALUE;

std::string getMainExecutableImpl(const char *argv0, void *MainExecAddr) {
std::string getMainExecutable(const char *argv0, void *MainExecAddr) {
SmallVector<wchar_t, MAX_PATH> PathName;
PathName.resize_for_overwrite(PathName.capacity());
DWORD Size = ::GetModuleFileNameW(NULL, PathName.data(), PathName.size());
Expand Down
2 changes: 1 addition & 1 deletion llvm/tools/dsymutil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ if(APPLE)
target_link_libraries(dsymutil PRIVATE "-framework CoreFoundation")
endif(APPLE)

target_link_libraries(dsymutil PRIVATE ${LLVM_ATOMIC_LIB})
# target_link_libraries(dsymutil PRIVATE ${LLVM_ATOMIC_LIB})
1 change: 1 addition & 0 deletions llvm/tools/dsymutil/dsymutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "llvm/Support/FileCollector.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/ThreadPool.h"
Expand Down
3 changes: 2 additions & 1 deletion llvm/tools/llvm-ar/llvm-ar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#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"
#include "llvm/Support/Path.h"
Expand Down Expand Up @@ -1425,7 +1426,7 @@ static int ranlib_main(int argc, char **argv) {
return 0;
}

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

Expand Down
3 changes: 2 additions & 1 deletion llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#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"
#include "llvm/TargetParser/Host.h"
Expand Down Expand Up @@ -145,7 +146,7 @@ static void demangleLine(llvm::raw_ostream &OS, StringRef Mangled, bool Split) {
OS.flush();
}

int llvm_cxxfilt_main(int argc, char **argv) {
int llvm_cxxfilt_main(int argc, char **argv, const llvm::ToolContext &) {
InitLLVM X(argc, argv);
BumpPtrAllocator A;
StringSaver Saver(A);
Expand Down
23 changes: 13 additions & 10 deletions llvm/tools/llvm-driver/llvm-driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
#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"

using namespace llvm;

#define LLVM_DRIVER_TOOL(tool, entry) int entry##_main(int argc, char **argv);
#define LLVM_DRIVER_TOOL(tool, entry) \
int entry##_main(int argc, char **argv, const llvm::ToolContext &);
#include "LLVMDriverTools.def"

constexpr char subcommands[] =
Expand All @@ -34,7 +36,7 @@ static void printHelpMessage() {
<< "OPTIONS:\n\n --help - Display this message";
}

static int findTool(int Argc, char **Argv) {
static int findTool(int Argc, char **Argv, const char *Argv0) {
if (!Argc) {
printHelpMessage();
return 1;
Expand All @@ -60,21 +62,22 @@ static int findTool(int Argc, char **Argv) {
return false;
};

auto MakeDriverArgs = [=]() -> llvm::ToolContext {
if (ToolName != Argv0)
return {Argv0, ToolName.data(), true};
return {Argv0, sys::path::filename(Argv0).data(), false};
};

#define LLVM_DRIVER_TOOL(tool, entry) \
if (Is(tool)) \
return entry##_main(Argc, Argv);
return entry##_main(Argc, Argv, MakeDriverArgs());
#include "LLVMDriverTools.def"

if (Is("llvm"))
return findTool(Argc - 1, Argv + 1);
return findTool(Argc - 1, Argv + 1, Argv0);

printHelpMessage();
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, Argv[0]); }
3 changes: 2 additions & 1 deletion llvm/tools/llvm-ifs/llvm-ifs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "llvm/Support/Errc.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FileOutputBuffer.h"
#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/VersionTuple.h"
Expand Down Expand Up @@ -382,7 +383,7 @@ static DriverConfig parseArgs(int argc, char *const *argv) {
return Config;
}

int llvm_ifs_main(int argc, char **argv) {
int llvm_ifs_main(int argc, char **argv, const llvm::ToolContext &) {
DriverConfig Config = parseArgs(argc, argv);

if (Config.InputFilePaths.empty())
Expand Down
3 changes: 2 additions & 1 deletion llvm/tools/llvm-lipo/llvm-lipo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#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"
#include "llvm/TargetParser/Triple.h"
Expand Down Expand Up @@ -723,7 +724,7 @@ replaceSlices(LLVMContext &LLVMCtx,
exit(EXIT_SUCCESS);
}

int llvm_lipo_main(int argc, char **argv) {
int llvm_lipo_main(int argc, char **argv, const llvm::ToolContext &) {
InitLLVM X(argc, argv);
llvm::InitializeAllTargetInfos();
llvm::InitializeAllTargetMCs();
Expand Down
3 changes: 2 additions & 1 deletion llvm/tools/llvm-mt/llvm-mt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#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"
#include "llvm/Support/PrettyStackTrace.h"
Expand Down Expand Up @@ -82,7 +83,7 @@ static void error(Error EC) {
});
}

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

CvtResOptTable T;
Expand Down
3 changes: 2 additions & 1 deletion llvm/tools/llvm-nm/llvm-nm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#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"
#include "llvm/Support/Signals.h"
Expand Down Expand Up @@ -2286,7 +2287,7 @@ exportSymbolNamesFromFiles(const std::vector<std::string> &InputFilenames) {
printExportSymbolList(SymbolList);
}

int llvm_nm_main(int argc, char **argv) {
int llvm_nm_main(int argc, char **argv, const llvm::ToolContext &) {
InitLLVM X(argc, argv);
BumpPtrAllocator A;
StringSaver Saver(A);
Expand Down
3 changes: 2 additions & 1 deletion llvm/tools/llvm-objcopy/llvm-objcopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#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"
#include "llvm/Support/Process.h"
Expand Down Expand Up @@ -223,7 +224,7 @@ static Error executeObjcopy(ConfigManager &ConfigMgr) {
return Error::success();
}

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

Expand Down
4 changes: 3 additions & 1 deletion llvm/tools/llvm-profdata/llvm-profdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#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"
#include "llvm/Support/Path.h"
Expand Down Expand Up @@ -2988,7 +2989,8 @@ static int show_main(int argc, const char *argv[]) {
return showMemProfProfile(Filename, ProfiledBinary, SFormat, OS);
}

int llvm_profdata_main(int argc, char **argvNonConst) {
int llvm_profdata_main(int argc, char **argvNonConst,
const llvm::ToolContext &) {
const char **argv = const_cast<const char **>(argvNonConst);
InitLLVM X(argc, argv);

Expand Down
3 changes: 2 additions & 1 deletion llvm/tools/llvm-rc/llvm-rc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#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"
#include "llvm/Support/PrettyStackTrace.h"
Expand Down Expand Up @@ -734,7 +735,7 @@ void doCvtres(std::string Src, std::string Dest, std::string TargetTriple) {

} // anonymous namespace

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

Expand Down
3 changes: 2 additions & 1 deletion llvm/tools/llvm-readobj/llvm-readobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#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"
#include "llvm/Support/WithColor.h"
Expand Down Expand Up @@ -632,7 +633,7 @@ std::unique_ptr<ScopedPrinter> createWriter() {
return std::make_unique<ScopedPrinter>(fouts());
}

int llvm_readobj_main(int argc, char **argv) {
int llvm_readobj_main(int argc, char **argv, const llvm::ToolContext &) {
InitLLVM X(argc, argv);
BumpPtrAllocator A;
StringSaver Saver(A);
Expand Down
3 changes: 2 additions & 1 deletion llvm/tools/llvm-size/llvm-size.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#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"
#include "llvm/Support/raw_ostream.h"
Expand Down Expand Up @@ -869,7 +870,7 @@ static void printBerkeleyTotals() {
<< "(TOTALS)\n";
}

int llvm_size_main(int argc, char **argv) {
int llvm_size_main(int argc, char **argv, const llvm::ToolContext &) {
InitLLVM X(argc, argv);
BumpPtrAllocator A;
StringSaver Saver(A);
Expand Down