authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-08-10 17:09:09-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-09-19 09:37:30-07:00
log68a338cc1037531b4ba548a4c181afeb957fab9a
treea8d0b4c3d1566e2c3b97f6a28694ae924ae8851e
parent3882ce4f4bc78154dffe0768d0200a1bde067af7

update ar and clang C++ files to LLVM 17


4 files changed, 74 insertions(+), 123 deletions(-)

src/zig_clang_cc1_main.cpp+15-13
......@@ -213,9 +213,7 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
213213 bool Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(),
214214 Argv, Diags, Argv0);
215215
216 if (Clang->getFrontendOpts().TimeTrace ||
217 !Clang->getFrontendOpts().TimeTracePath.empty()) {
218 Clang->getFrontendOpts().TimeTrace = 1;
216 if (!Clang->getFrontendOpts().TimeTracePath.empty()) {
219217 llvm::timeTraceProfilerInitialize(
220218 Clang->getFrontendOpts().TimeTraceGranularity, Argv0);
221219 }
......@@ -257,17 +255,21 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
257255 llvm::TimerGroup::clearAll();
258256
259257 if (llvm::timeTraceProfilerEnabled()) {
260 SmallString<128> Path(Clang->getFrontendOpts().OutputFile);
261 llvm::sys::path::replace_extension(Path, "json");
262 if (!Clang->getFrontendOpts().TimeTracePath.empty()) {
263 // replace the suffix to '.json' directly
264 SmallString<128> TracePath(Clang->getFrontendOpts().TimeTracePath);
265 if (llvm::sys::fs::is_directory(TracePath))
266 llvm::sys::path::append(TracePath, llvm::sys::path::filename(Path));
267 Path.assign(TracePath);
268 }
258 // It is possible that the compiler instance doesn't own a file manager here
259 // if we're compiling a module unit. Since the file manager are owned by AST
260 // when we're compiling a module unit. So the file manager may be invalid
261 // here.
262 //
263 // It should be fine to create file manager here since the file system
264 // options are stored in the compiler invocation and we can recreate the VFS
265 // from the compiler invocation.
266 if (!Clang->hasFileManager())
267 Clang->createFileManager(createVFSFromCompilerInvocation(
268 Clang->getInvocation(), Clang->getDiagnostics()));
269
269270 if (auto profilerOutput = Clang->createOutputFile(
270 Path.str(), /*Binary=*/false, /*RemoveFileOnSignal=*/false,
271 Clang->getFrontendOpts().TimeTracePath, /*Binary=*/false,
272 /*RemoveFileOnSignal=*/false,
271273 /*useTemporary=*/false)) {
272274 llvm::timeTraceProfilerWrite(*profilerOutput);
273275 profilerOutput.reset();
src/zig_clang_cc1as_main.cpp+16-7
......@@ -19,8 +19,8 @@
1919#include "clang/Frontend/TextDiagnosticPrinter.h"
2020#include "clang/Frontend/Utils.h"
2121#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/StringExtras.h"
2223#include "llvm/ADT/StringSwitch.h"
23#include "llvm/ADT/Triple.h"
2424#include "llvm/IR/DataLayout.h"
2525#include "llvm/MC/MCAsmBackend.h"
2626#include "llvm/MC/MCAsmInfo.h"
......@@ -44,7 +44,6 @@
4444#include "llvm/Support/ErrorHandling.h"
4545#include "llvm/Support/FileSystem.h"
4646#include "llvm/Support/FormattedStream.h"
47#include "llvm/Support/Host.h"
4847#include "llvm/Support/MemoryBuffer.h"
4948#include "llvm/Support/Path.h"
5049#include "llvm/Support/Process.h"
......@@ -53,6 +52,8 @@
5352#include "llvm/Support/TargetSelect.h"
5453#include "llvm/Support/Timer.h"
5554#include "llvm/Support/raw_ostream.h"
55#include "llvm/TargetParser/Host.h"
56#include "llvm/TargetParser/Triple.h"
5657#include <memory>
5758#include <optional>
5859#include <system_error>
......@@ -97,7 +98,7 @@ struct AssemblerInvocation {
9798 std::string DwarfDebugFlags;
9899 std::string DwarfDebugProducer;
99100 std::string DebugCompilationDir;
100 std::map<const std::string, const std::string> DebugPrefixMap;
101 llvm::SmallVector<std::pair<std::string, std::string>, 0> DebugPrefixMap;
101102 llvm::DebugCompressionType CompressDebugSections =
102103 llvm::DebugCompressionType::None;
103104 std::string MainFileName;
......@@ -142,6 +143,10 @@ struct AssemblerInvocation {
142143 /// Whether to emit DWARF unwind info.
143144 EmitDwarfUnwindType EmitDwarfUnwind;
144145
146 // Whether to emit compact-unwind for non-canonical entries.
147 // Note: maybe overriden by other constraints.
148 unsigned EmitCompactUnwindNonCanonical : 1;
149
145150 /// The name of the relocation model to use.
146151 std::string RelocationModel;
147152
......@@ -181,6 +186,7 @@ public:
181186 DwarfVersion = 0;
182187 EmbedBitcode = 0;
183188 EmitDwarfUnwind = EmitDwarfUnwindType::Default;
189 EmitCompactUnwindNonCanonical = false;
184190 }
185191
186192 static bool CreateFromArgs(AssemblerInvocation &Res,
......@@ -275,8 +281,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
275281
276282 for (const auto &Arg : Args.getAllArgValues(OPT_fdebug_prefix_map_EQ)) {
277283 auto Split = StringRef(Arg).split('=');
278 Opts.DebugPrefixMap.insert(
279 {std::string(Split.first), std::string(Split.second)});
284 Opts.DebugPrefixMap.emplace_back(Split.first, Split.second);
280285 }
281286
282287 // Frontend Options
......@@ -349,6 +354,9 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
349354 .Case("default", EmitDwarfUnwindType::Default);
350355 }
351356
357 Opts.EmitCompactUnwindNonCanonical =
358 Args.hasArg(OPT_femit_compact_unwind_non_canonical);
359
352360 Opts.AsSecureLogFile = Args.getLastArgValue(OPT_as_secure_log_file);
353361
354362 return Success;
......@@ -384,8 +392,8 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
384392 MemoryBuffer::getFileOrSTDIN(Opts.InputFile, /*IsText=*/true);
385393
386394 if (std::error_code EC = Buffer.getError()) {
387 Error = EC.message();
388 return Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
395 return Diags.Report(diag::err_fe_error_reading)
396 << Opts.InputFile << EC.message();
389397 }
390398
391399 SourceMgr SrcMgr;
......@@ -402,6 +410,7 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
402410
403411 MCTargetOptions MCOptions;
404412 MCOptions.EmitDwarfUnwind = Opts.EmitDwarfUnwind;
413 MCOptions.EmitCompactUnwindNonCanonical = Opts.EmitCompactUnwindNonCanonical;
405414 MCOptions.AsSecureLogFile = Opts.AsSecureLogFile;
406415
407416 std::unique_ptr<MCAsmInfo> MAI(
src/zig_clang_driver.cpp+37-63
......@@ -36,8 +36,8 @@
3636#include "llvm/Support/CrashRecoveryContext.h"
3737#include "llvm/Support/ErrorHandling.h"
3838#include "llvm/Support/FileSystem.h"
39#include "llvm/Support/Host.h"
4039#include "llvm/Support/InitLLVM.h"
40#include "llvm/Support/LLVMDriver.h"
4141#include "llvm/Support/Path.h"
4242#include "llvm/Support/PrettyStackTrace.h"
4343#include "llvm/Support/Process.h"
......@@ -48,6 +48,7 @@
4848#include "llvm/Support/TargetSelect.h"
4949#include "llvm/Support/Timer.h"
5050#include "llvm/Support/raw_ostream.h"
51#include "llvm/TargetParser/Host.h"
5152#include <memory>
5253#include <optional>
5354#include <set>
......@@ -209,6 +210,9 @@ extern int cc1_main(ArrayRef<const char *> Argv, const char *Argv0,
209210 void *MainAddr);
210211extern int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0,
211212 void *MainAddr);
213extern int cc1gen_reproducer_main(ArrayRef<const char *> Argv,
214 const char *Argv0, void *MainAddr,
215 const llvm::ToolContext &);
212216
213217static void insertTargetAndModeArgs(const ParsedClangName &NameParts,
214218 SmallVectorImpl<const char *> &ArgVector,
......@@ -303,6 +307,9 @@ static bool SetBackdoorDriverOutputsFromEnvVars(Driver &TheDriver) {
303307 TheDriver.CCPrintProcessStats =
304308 checkEnvVar<bool>("CC_PRINT_PROC_STAT", "CC_PRINT_PROC_STAT_FILE",
305309 TheDriver.CCPrintStatReportFilename);
310 TheDriver.CCPrintInternalStats =
311 checkEnvVar<bool>("CC_PRINT_INTERNAL_STAT", "CC_PRINT_INTERNAL_STAT_FILE",
312 TheDriver.CCPrintInternalStatReportFilename);
306313
307314 return true;
308315}
......@@ -339,7 +346,8 @@ static void SetInstallDir(SmallVectorImpl<const char *> &argv,
339346 TheDriver.setInstalledDir(InstalledPathParent);
340347}
341348
342static int ExecuteCC1Tool(SmallVectorImpl<const char *> &ArgV) {
349static int ExecuteCC1Tool(SmallVectorImpl<const char *> &ArgV,
350 const llvm::ToolContext &ToolContext) {
343351 // If we call the cc1 tool from the clangDriver library (through
344352 // Driver::CC1Main), we need to clean up the options usage count. The options
345353 // are currently global, and they might have been used previously by the
......@@ -358,28 +366,22 @@ static int ExecuteCC1Tool(SmallVectorImpl<const char *> &ArgV) {
358366 return cc1_main(ArrayRef(ArgV).slice(1), ArgV[0], GetExecutablePathVP);
359367 if (Tool == "-cc1as")
360368 return cc1as_main(ArrayRef(ArgV).slice(2), ArgV[0], GetExecutablePathVP);
369 if (Tool == "-cc1gen-reproducer")
370 return cc1gen_reproducer_main(ArrayRef(ArgV).slice(2), ArgV[0],
371 GetExecutablePathVP, ToolContext);
361372 // Reject unknown tools.
362373 llvm::errs() << "error: unknown integrated tool '" << Tool << "'. "
363374 << "Valid tools include '-cc1' and '-cc1as'.\n";
364375 return 1;
365376}
366377
367extern "C" int ZigClang_main(int Argc, const char **Argv);
368int ZigClang_main(int Argc, const char **Argv) {
378int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContext) {
369379 noteBottomOfStack();
370 // ZIG PATCH: On Windows, InitLLVM calls GetCommandLineW(),
371 // and overwrites the args. We don't want it to do that,
372 // and we also don't need the signal handlers it installs
373 // (we have our own already), so we just use llvm_shutdown_obj
374 // instead.
375 // llvm::InitLLVM X(Argc, Argv);
376 llvm::llvm_shutdown_obj X;
377
380 llvm::InitLLVM X(Argc, Argv);
378381 llvm::setBugReportMsg("PLEASE submit a bug report to " BUG_REPORT_URL
379382 " and include the crash backtrace, preprocessed "
380383 "source, and associated run script.\n");
381 size_t argv_offset = (strcmp(Argv[1], "-cc1") == 0 || strcmp(Argv[1], "-cc1as") == 0) ? 0 : 1;
382 SmallVector<const char *, 256> Args(Argv + argv_offset, Argv + Argc);
384 SmallVector<const char *, 256> Args(Argv, Argv + Argc);
383385
384386 if (llvm::sys::Process::FixupStandardFileDescriptors())
385387 return 1;
......@@ -389,55 +391,20 @@ int ZigClang_main(int Argc, const char **Argv) {
389391 llvm::BumpPtrAllocator A;
390392 llvm::StringSaver Saver(A);
391393
392 // Parse response files using the GNU syntax, unless we're in CL mode. There
393 // are two ways to put clang in CL compatibility mode: Args[0] is either
394 // clang-cl or cl, or --driver-mode=cl is on the command line. The normal
395 // command line parsing can't happen until after response file parsing, so we
396 // have to manually search for a --driver-mode=cl argument the hard way.
397 // Finally, our -cc1 tools don't care which tokenization mode we use because
398 // response files written by clang will tokenize the same way in either mode.
394 const char *ProgName =
395 ToolContext.NeedsPrependArg ? ToolContext.PrependArg : ToolContext.Path;
396
399397 bool ClangCLMode =
400 IsClangCL(getDriverMode(Args[0], llvm::ArrayRef(Args).slice(1)));
401 enum { Default, POSIX, Windows } RSPQuoting = Default;
402 for (const char *F : Args) {
403 if (strcmp(F, "--rsp-quoting=posix") == 0)
404 RSPQuoting = POSIX;
405 else if (strcmp(F, "--rsp-quoting=windows") == 0)
406 RSPQuoting = Windows;
407 }
398 IsClangCL(getDriverMode(ProgName, llvm::ArrayRef(Args).slice(1)));
408399
409 // Determines whether we want nullptr markers in Args to indicate response
410 // files end-of-lines. We only use this for the /LINK driver argument with
411 // clang-cl.exe on Windows.
412 bool MarkEOLs = ClangCLMode;
413
414 llvm::cl::TokenizerCallback Tokenizer;
415 if (RSPQuoting == Windows || (RSPQuoting == Default && ClangCLMode))
416 Tokenizer = &llvm::cl::TokenizeWindowsCommandLine;
417 else
418 Tokenizer = &llvm::cl::TokenizeGNUCommandLine;
419
420 if (MarkEOLs && Args.size() > 1 && StringRef(Args[1]).startswith("-cc1"))
421 MarkEOLs = false;
422 llvm::cl::ExpansionContext ECtx(A, Tokenizer);
423 ECtx.setMarkEOLs(MarkEOLs);
424 if (llvm::Error Err = ECtx.expandResponseFiles(Args)) {
400 if (llvm::Error Err = expandResponseFiles(Args, ClangCLMode, A)) {
425401 llvm::errs() << toString(std::move(Err)) << '\n';
426402 return 1;
427403 }
428404
429 // Handle -cc1 integrated tools, even if -cc1 was expanded from a response
430 // file.
431 auto FirstArg = llvm::find_if(llvm::drop_begin(Args),
432 [](const char *A) { return A != nullptr; });
433 if (FirstArg != Args.end() && StringRef(*FirstArg).startswith("-cc1")) {
434 // If -cc1 came from a response file, remove the EOL sentinels.
435 if (MarkEOLs) {
436 auto newEnd = std::remove(Args.begin(), Args.end(), nullptr);
437 Args.resize(newEnd - Args.begin());
438 }
439 return ExecuteCC1Tool(Args);
440 }
405 // Handle -cc1 integrated tools.
406 if (Args.size() >= 2 && StringRef(Args[1]).startswith("-cc1"))
407 return ExecuteCC1Tool(Args, ToolContext);
441408
442409 // Handle options that need handling before the real command line parsing in
443410 // Driver::BuildCompilation()
......@@ -483,9 +450,7 @@ int ZigClang_main(int Argc, const char **Argv) {
483450 ApplyQAOverride(Args, OverrideStr, SavedStrings);
484451 }
485452
486 // Pass local param `Argv[0]` as fallback.
487 // See https://github.com/ziglang/zig/pull/3292 .
488 std::string Path = GetExecutablePath(Argv[0], CanonicalPrefixes);
453 std::string Path = GetExecutablePath(ToolContext.Path, CanonicalPrefixes);
489454
490455 // Whether the cc1 tool should be called inside the current process, or if we
491456 // should spawn a new clang subprocess (old behavior).
......@@ -503,7 +468,7 @@ int ZigClang_main(int Argc, const char **Argv) {
503468
504469 TextDiagnosticPrinter *DiagClient
505470 = new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
506 FixupDiagPrefixExeName(DiagClient, Path);
471 FixupDiagPrefixExeName(DiagClient, ProgName);
507472
508473 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
509474
......@@ -521,8 +486,15 @@ int ZigClang_main(int Argc, const char **Argv) {
521486
522487 Driver TheDriver(Path, llvm::sys::getDefaultTargetTriple(), Diags);
523488 SetInstallDir(Args, TheDriver, CanonicalPrefixes);
524 auto TargetAndMode = ToolChain::getTargetAndModeFromProgramName(Args[0]);
489 auto TargetAndMode = ToolChain::getTargetAndModeFromProgramName(ProgName);
525490 TheDriver.setTargetAndMode(TargetAndMode);
491 // If -canonical-prefixes is set, GetExecutablePath will have resolved Path
492 // to the llvm driver binary, not clang. In this case, we need to use
493 // PrependArg which should be clang-*. Checking just CanonicalPrefixes is
494 // safe even in the normal case because PrependArg will be null so
495 // setPrependArg will be a no-op.
496 if (ToolContext.NeedsPrependArg || CanonicalPrefixes)
497 TheDriver.setPrependArg(ToolContext.PrependArg);
526498
527499 insertTargetAndModeArgs(TargetAndMode, Args, SavedStrings);
528500
......@@ -530,7 +502,9 @@ int ZigClang_main(int Argc, const char **Argv) {
530502 return 1;
531503
532504 if (!UseNewCC1Process) {
533 TheDriver.CC1Main = &ExecuteCC1Tool;
505 TheDriver.CC1Main = [ToolContext](SmallVectorImpl<const char *> &ArgV) {
506 return ExecuteCC1Tool(ArgV, ToolContext);
507 };
534508 // Ensure the CC1Command actually catches cc1 crashes
535509 llvm::CrashRecoveryContext::Enable();
536510 }
src/zig_llvm-ar.cpp+6-40
......@@ -13,20 +13,11 @@
1313
1414#include "llvm/ADT/StringExtras.h"
1515#include "llvm/ADT/StringSwitch.h"
16#include "llvm/ADT/Triple.h"
1716#include "llvm/BinaryFormat/Magic.h"
1817#include "llvm/IR/LLVMContext.h"
1918#include "llvm/Object/Archive.h"
2019#include "llvm/Object/ArchiveWriter.h"
21#include "llvm/Object/COFFImportFile.h"
22#include "llvm/Object/ELFObjectFile.h"
23#include "llvm/Object/IRObjectFile.h"
24#include "llvm/Object/MachO.h"
25#include "llvm/Object/ObjectFile.h"
2620#include "llvm/Object/SymbolicFile.h"
27#include "llvm/Object/TapiFile.h"
28#include "llvm/Object/Wasm.h"
29#include "llvm/Object/XCOFFObjectFile.h"
3021#include "llvm/Support/Chrono.h"
3122#include "llvm/Support/CommandLine.h"
3223#include "llvm/Support/ConvertUTF.h"
......@@ -34,8 +25,8 @@
3425#include "llvm/Support/FileSystem.h"
3526#include "llvm/Support/Format.h"
3627#include "llvm/Support/FormatVariadic.h"
37#include "llvm/Support/Host.h"
3828#include "llvm/Support/InitLLVM.h"
29#include "llvm/Support/LLVMDriver.h"
3930#include "llvm/Support/LineIterator.h"
4031#include "llvm/Support/MemoryBuffer.h"
4132#include "llvm/Support/Path.h"
......@@ -45,6 +36,8 @@
4536#include "llvm/Support/ToolOutputFile.h"
4637#include "llvm/Support/WithColor.h"
4738#include "llvm/Support/raw_ostream.h"
39#include "llvm/TargetParser/Host.h"
40#include "llvm/TargetParser/Triple.h"
4841#include "llvm/ToolDrivers/llvm-dlltool/DlltoolDriver.h"
4942#include "llvm/ToolDrivers/llvm-lib/LibDriver.h"
5043
......@@ -646,31 +639,12 @@ static bool shouldCreateArchive(ArchiveOperation Op) {
646639 llvm_unreachable("Missing entry in covered switch.");
647640}
648641
649static bool is64BitSymbolicFile(SymbolicFile &Obj) {
650 if (auto *IRObj = dyn_cast<IRObjectFile>(&Obj))
651 return Triple(IRObj->getTargetTriple()).isArch64Bit();
652 if (isa<COFFObjectFile>(Obj) || isa<COFFImportFile>(Obj))
653 return false;
654 if (XCOFFObjectFile *XCOFFObj = dyn_cast<XCOFFObjectFile>(&Obj))
655 return XCOFFObj->is64Bit();
656 if (isa<WasmObjectFile>(Obj))
657 return false;
658 if (TapiFile *Tapi = dyn_cast<TapiFile>(&Obj))
659 return Tapi->is64Bit();
660 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj))
661 return MachO->is64Bit();
662 if (ELFObjectFileBase *ElfO = dyn_cast<ELFObjectFileBase>(&Obj))
663 return ElfO->getBytesInAddress() == 8;
664
665 fail("unsupported file format");
666}
667
668642static bool isValidInBitMode(Binary &Bin) {
669643 if (BitMode == BitModeTy::Bit32_64 || BitMode == BitModeTy::Any)
670644 return true;
671645
672646 if (SymbolicFile *SymFile = dyn_cast<SymbolicFile>(&Bin)) {
673 bool Is64Bit = is64BitSymbolicFile(*SymFile);
647 bool Is64Bit = SymFile->is64Bit();
674648 if ((Is64Bit && (BitMode == BitModeTy::Bit32)) ||
675649 (!Is64Bit && (BitMode == BitModeTy::Bit64)))
676650 return false;
......@@ -1452,16 +1426,8 @@ static int ranlib_main(int argc, char **argv) {
14521426 return 0;
14531427}
14541428
1455extern "C" int ZigLlvmAr_main(int argc, char **argv);
1456int ZigLlvmAr_main(int argc, char **argv) {
1457 // ZIG PATCH: On Windows, InitLLVM calls GetCommandLineW(),
1458 // and overwrites the args. We don't want it to do that,
1459 // and we also don't need the signal handlers it installs
1460 // (we have our own already), so we just use llvm_shutdown_obj
1461 // instead.
1462 // InitLLVM X(argc, argv);
1463 llvm::llvm_shutdown_obj X;
1464
1429int llvm_ar_main(int argc, char **argv, const llvm::ToolContext &) {
1430 InitLLVM X(argc, argv);
14651431 ToolName = argv[0];
14661432
14671433 llvm::InitializeAllTargetInfos();