aboutsummaryrefslogtreecommitdiff
path: root/src/zig_clang_driver.cpp
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2023-09-19 14:37:46 -0700
committerGitHub <noreply@github.com>2023-09-19 14:37:46 -0700
commita63a1c5cb9fd31a57e8371e6cba5f316bd3f2a65 (patch)
tree86c229e453d1f09734d6c89d172a471675aa7478 /src/zig_clang_driver.cpp
parentee4ced96833470f9432e6a5dc5b31534457280c0 (diff)
parentf9f9c4a083b4e268caac3feff3a848df7a4f17cb (diff)
downloadzig-a63a1c5cb9fd31a57e8371e6cba5f316bd3f2a65.tar.gz
zig-a63a1c5cb9fd31a57e8371e6cba5f316bd3f2a65.zip
Merge pull request #17202 from ziglang/llvm17
Update to LLVM 17
Diffstat (limited to 'src/zig_clang_driver.cpp')
-rw-r--r--src/zig_clang_driver.cpp87
1 files changed, 34 insertions, 53 deletions
diff --git a/src/zig_clang_driver.cpp b/src/zig_clang_driver.cpp
index 1381eb9b08..604d38acf2 100644
--- a/src/zig_clang_driver.cpp
+++ b/src/zig_clang_driver.cpp
@@ -36,8 +36,8 @@
#include "llvm/Support/CrashRecoveryContext.h"
#include "llvm/Support/ErrorHandling.h"
#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"
@@ -48,6 +48,7 @@
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/Timer.h"
#include "llvm/Support/raw_ostream.h"
+#include "llvm/TargetParser/Host.h"
#include <memory>
#include <optional>
#include <set>
@@ -303,6 +304,9 @@ static bool SetBackdoorDriverOutputsFromEnvVars(Driver &TheDriver) {
TheDriver.CCPrintProcessStats =
checkEnvVar<bool>("CC_PRINT_PROC_STAT", "CC_PRINT_PROC_STAT_FILE",
TheDriver.CCPrintStatReportFilename);
+ TheDriver.CCPrintInternalStats =
+ checkEnvVar<bool>("CC_PRINT_INTERNAL_STAT", "CC_PRINT_INTERNAL_STAT_FILE",
+ TheDriver.CCPrintInternalStatReportFilename);
return true;
}
@@ -339,7 +343,8 @@ static void SetInstallDir(SmallVectorImpl<const char *> &argv,
TheDriver.setInstalledDir(InstalledPathParent);
}
-static int ExecuteCC1Tool(SmallVectorImpl<const char *> &ArgV) {
+static int ExecuteCC1Tool(SmallVectorImpl<const char *> &ArgV,
+ const llvm::ToolContext &ToolContext) {
// If we call the cc1 tool from the clangDriver library (through
// Driver::CC1Main), we need to clean up the options usage count. The options
// are currently global, and they might have been used previously by the
@@ -364,8 +369,7 @@ static int ExecuteCC1Tool(SmallVectorImpl<const char *> &ArgV) {
return 1;
}
-extern "C" int ZigClang_main(int Argc, const char **Argv);
-int ZigClang_main(int Argc, const char **Argv) {
+static int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContext) {
noteBottomOfStack();
// ZIG PATCH: On Windows, InitLLVM calls GetCommandLineW(),
// and overwrites the args. We don't want it to do that,
@@ -389,55 +393,20 @@ int ZigClang_main(int Argc, const char **Argv) {
llvm::BumpPtrAllocator A;
llvm::StringSaver Saver(A);
- // Parse response files using the GNU syntax, unless we're in CL mode. There
- // are two ways to put clang in CL compatibility mode: Args[0] is either
- // clang-cl or cl, or --driver-mode=cl is on the command line. The normal
- // command line parsing can't happen until after response file parsing, so we
- // have to manually search for a --driver-mode=cl argument the hard way.
- // Finally, our -cc1 tools don't care which tokenization mode we use because
- // response files written by clang will tokenize the same way in either mode.
+ const char *ProgName =
+ ToolContext.NeedsPrependArg ? ToolContext.PrependArg : ToolContext.Path;
+
bool ClangCLMode =
- IsClangCL(getDriverMode(Args[0], llvm::ArrayRef(Args).slice(1)));
- enum { Default, POSIX, Windows } RSPQuoting = Default;
- for (const char *F : Args) {
- if (strcmp(F, "--rsp-quoting=posix") == 0)
- RSPQuoting = POSIX;
- else if (strcmp(F, "--rsp-quoting=windows") == 0)
- RSPQuoting = Windows;
- }
+ IsClangCL(getDriverMode(ProgName, llvm::ArrayRef(Args).slice(1)));
- // Determines whether we want nullptr markers in Args to indicate response
- // files end-of-lines. We only use this for the /LINK driver argument with
- // clang-cl.exe on Windows.
- bool MarkEOLs = ClangCLMode;
-
- llvm::cl::TokenizerCallback Tokenizer;
- if (RSPQuoting == Windows || (RSPQuoting == Default && ClangCLMode))
- Tokenizer = &llvm::cl::TokenizeWindowsCommandLine;
- else
- Tokenizer = &llvm::cl::TokenizeGNUCommandLine;
-
- if (MarkEOLs && Args.size() > 1 && StringRef(Args[1]).startswith("-cc1"))
- MarkEOLs = false;
- llvm::cl::ExpansionContext ECtx(A, Tokenizer);
- ECtx.setMarkEOLs(MarkEOLs);
- if (llvm::Error Err = ECtx.expandResponseFiles(Args)) {
+ if (llvm::Error Err = expandResponseFiles(Args, ClangCLMode, A)) {
llvm::errs() << toString(std::move(Err)) << '\n';
return 1;
}
- // Handle -cc1 integrated tools, even if -cc1 was expanded from a response
- // file.
- auto FirstArg = llvm::find_if(llvm::drop_begin(Args),
- [](const char *A) { return A != nullptr; });
- if (FirstArg != Args.end() && StringRef(*FirstArg).startswith("-cc1")) {
- // If -cc1 came from a response file, remove the EOL sentinels.
- if (MarkEOLs) {
- auto newEnd = std::remove(Args.begin(), Args.end(), nullptr);
- Args.resize(newEnd - Args.begin());
- }
- return ExecuteCC1Tool(Args);
- }
+ // Handle -cc1 integrated tools.
+ if (Args.size() >= 2 && StringRef(Args[1]).startswith("-cc1"))
+ return ExecuteCC1Tool(Args, ToolContext);
// Handle options that need handling before the real command line parsing in
// Driver::BuildCompilation()
@@ -483,9 +452,7 @@ int ZigClang_main(int Argc, const char **Argv) {
ApplyQAOverride(Args, OverrideStr, SavedStrings);
}
- // Pass local param `Argv[0]` as fallback.
- // See https://github.com/ziglang/zig/pull/3292 .
- std::string Path = GetExecutablePath(Argv[0], CanonicalPrefixes);
+ std::string Path = GetExecutablePath(ToolContext.Path, CanonicalPrefixes);
// Whether the cc1 tool should be called inside the current process, or if we
// should spawn a new clang subprocess (old behavior).
@@ -503,7 +470,7 @@ int ZigClang_main(int Argc, const char **Argv) {
TextDiagnosticPrinter *DiagClient
= new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
- FixupDiagPrefixExeName(DiagClient, Path);
+ FixupDiagPrefixExeName(DiagClient, ProgName);
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
@@ -521,8 +488,15 @@ int ZigClang_main(int Argc, const char **Argv) {
Driver TheDriver(Path, llvm::sys::getDefaultTargetTriple(), Diags);
SetInstallDir(Args, TheDriver, CanonicalPrefixes);
- auto TargetAndMode = ToolChain::getTargetAndModeFromProgramName(Args[0]);
+ auto TargetAndMode = ToolChain::getTargetAndModeFromProgramName(ProgName);
TheDriver.setTargetAndMode(TargetAndMode);
+ // If -canonical-prefixes is set, GetExecutablePath will have resolved Path
+ // to the llvm driver binary, not clang. In this case, we need to use
+ // PrependArg which should be clang-*. Checking just CanonicalPrefixes is
+ // safe even in the normal case because PrependArg will be null so
+ // setPrependArg will be a no-op.
+ if (ToolContext.NeedsPrependArg || CanonicalPrefixes)
+ TheDriver.setPrependArg(ToolContext.PrependArg);
insertTargetAndModeArgs(TargetAndMode, Args, SavedStrings);
@@ -530,7 +504,9 @@ int ZigClang_main(int Argc, const char **Argv) {
return 1;
if (!UseNewCC1Process) {
- TheDriver.CC1Main = &ExecuteCC1Tool;
+ TheDriver.CC1Main = [ToolContext](SmallVectorImpl<const char *> &ArgV) {
+ return ExecuteCC1Tool(ArgV, ToolContext);
+ };
// Ensure the CC1Command actually catches cc1 crashes
llvm::CrashRecoveryContext::Enable();
}
@@ -629,3 +605,8 @@ int ZigClang_main(int Argc, const char **Argv) {
// failing command.
return Res;
}
+
+extern "C" int ZigClang_main(int, char **);
+int ZigClang_main(int argc, char **argv) {
+ return clang_main(argc, argv, {argv[0], nullptr, false});
+}