authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-01-17 05:19:20+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-04-25 21:49:39+02:00
log11807110357b6434e4ee2ae8b3b6948ea45caccb
tree7602d0e0613dda946ac5a282ee4f840692fbce6a
parent2b6ab594358cead1b9b97c062715b5c313edc391
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

zig cc: update driver files to LLVM 22


5 files changed, 98 insertions(+), 69 deletions(-)

src/zig_clang_cc1_main.cpp+26-27
...@@ -12,19 +12,20 @@...@@ -12,19 +12,20 @@
12//12//
13//===----------------------------------------------------------------------===//13//===----------------------------------------------------------------------===//
1414
15#include "clang/Basic/DiagnosticFrontend.h"
15#include "clang/Basic/Stack.h"16#include "clang/Basic/Stack.h"
16#include "clang/Basic/TargetOptions.h"17#include "clang/Basic/TargetOptions.h"
17#include "clang/CodeGen/ObjectFilePCHContainerWriter.h"18#include "clang/CodeGen/ObjectFilePCHContainerWriter.h"
18#include "clang/Config/config.h"19#include "clang/Config/config.h"
20#include "clang/Driver/Driver.h"
19#include "clang/Driver/DriverDiagnostic.h"21#include "clang/Driver/DriverDiagnostic.h"
20#include "clang/Driver/Options.h"
21#include "clang/Frontend/CompilerInstance.h"22#include "clang/Frontend/CompilerInstance.h"
22#include "clang/Frontend/CompilerInvocation.h"23#include "clang/Frontend/CompilerInvocation.h"
23#include "clang/Frontend/FrontendDiagnostic.h"
24#include "clang/Frontend/TextDiagnosticBuffer.h"24#include "clang/Frontend/TextDiagnosticBuffer.h"
25#include "clang/Frontend/TextDiagnosticPrinter.h"25#include "clang/Frontend/TextDiagnosticPrinter.h"
26#include "clang/Frontend/Utils.h"26#include "clang/Frontend/Utils.h"
27#include "clang/FrontendTool/Utils.h"27#include "clang/FrontendTool/Utils.h"
28#include "clang/Options/Options.h"
28#include "clang/Serialization/ObjectFilePCHContainerReader.h"29#include "clang/Serialization/ObjectFilePCHContainerReader.h"
29#include "llvm/ADT/Statistic.h"30#include "llvm/ADT/Statistic.h"
30#include "llvm/ADT/StringExtras.h"31#include "llvm/ADT/StringExtras.h"
...@@ -38,6 +39,7 @@...@@ -38,6 +39,7 @@
38#include "llvm/Support/BuryPointer.h"39#include "llvm/Support/BuryPointer.h"
39#include "llvm/Support/Compiler.h"40#include "llvm/Support/Compiler.h"
40#include "llvm/Support/ErrorHandling.h"41#include "llvm/Support/ErrorHandling.h"
42#include "llvm/Support/IOSandbox.h"
41#include "llvm/Support/ManagedStatic.h"43#include "llvm/Support/ManagedStatic.h"
42#include "llvm/Support/Path.h"44#include "llvm/Support/Path.h"
43#include "llvm/Support/Process.h"45#include "llvm/Support/Process.h"
...@@ -217,7 +219,7 @@ static int PrintEnabledExtensions(const TargetOptions& TargetOpts) {...@@ -217,7 +219,7 @@ static int PrintEnabledExtensions(const TargetOptions& TargetOpts) {
217int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {219int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
218 ensureSufficientStack();220 ensureSufficientStack();
219221
220 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());222 IntrusiveRefCntPtr<DiagnosticIDs> DiagID = DiagnosticIDs::create();
221223
222 // Register the support for object-file-wrapped Clang modules.224 // Register the support for object-file-wrapped Clang modules.
223 auto PCHOps = std::make_shared<PCHContainerOperations>();225 auto PCHOps = std::make_shared<PCHContainerOperations>();
...@@ -269,12 +271,17 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {...@@ -269,12 +271,17 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
269 if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&271 if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
270 Clang->getHeaderSearchOpts().ResourceDir.empty())272 Clang->getHeaderSearchOpts().ResourceDir.empty())
271 Clang->getHeaderSearchOpts().ResourceDir =273 Clang->getHeaderSearchOpts().ResourceDir =
272 CompilerInvocation::GetResourcesPath(Argv0, MainAddr);274 GetResourcesPath(Argv0, MainAddr);
275
276 /// Create the actual file system.
277 auto VFS = [] {
278 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
279 return llvm::vfs::getRealFileSystem();
280 }();
281 Clang->createVirtualFileSystem(std::move(VFS), DiagsBuffer);
273282
274 // Create the actual diagnostics engine.283 // Create the actual diagnostics engine.
275 Clang->createDiagnostics(*llvm::vfs::getRealFileSystem());284 Clang->createDiagnostics();
276 if (!Clang->hasDiagnostics())
277 return 1;
278285
279 // Set an error handler, so that any LLVM backend diagnostics go through our286 // Set an error handler, so that any LLVM backend diagnostics go through our
280 // error handler.287 // error handler.
...@@ -299,29 +306,21 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {...@@ -299,29 +306,21 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
299306
300 // If any timers were active but haven't been destroyed yet, print their307 // If any timers were active but haven't been destroyed yet, print their
301 // results now. This happens in -disable-free mode.308 // results now. This happens in -disable-free mode.
302 std::unique_ptr<raw_ostream> IOFile = llvm::CreateInfoOutputFile();309 {
303 if (Clang->getCodeGenOpts().TimePassesJson) {310 // This isn't a formal input or output of the compiler.
304 *IOFile << "{\n";311 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
305 llvm::TimerGroup::printAllJSONValues(*IOFile, "");312 std::unique_ptr<raw_ostream> IOFile = llvm::CreateInfoOutputFile();
306 *IOFile << "\n}\n";313 if (Clang->getCodeGenOpts().TimePassesJson) {
307 } else {314 *IOFile << "{\n";
308 llvm::TimerGroup::printAll(*IOFile);315 llvm::TimerGroup::printAllJSONValues(*IOFile, "");
316 *IOFile << "\n}\n";
317 } else if (!Clang->getCodeGenOpts().TimePassesStatsFile) {
318 llvm::TimerGroup::printAll(*IOFile);
319 }
320 llvm::TimerGroup::clearAll();
309 }321 }
310 llvm::TimerGroup::clearAll();
311322
312 if (llvm::timeTraceProfilerEnabled()) {323 if (llvm::timeTraceProfilerEnabled()) {
313 // It is possible that the compiler instance doesn't own a file manager here
314 // if we're compiling a module unit. Since the file manager are owned by AST
315 // when we're compiling a module unit. So the file manager may be invalid
316 // here.
317 //
318 // It should be fine to create file manager here since the file system
319 // options are stored in the compiler invocation and we can recreate the VFS
320 // from the compiler invocation.
321 if (!Clang->hasFileManager())
322 Clang->createFileManager(createVFSFromCompilerInvocation(
323 Clang->getInvocation(), Clang->getDiagnostics()));
324
325 if (auto profilerOutput = Clang->createOutputFile(324 if (auto profilerOutput = Clang->createOutputFile(
326 Clang->getFrontendOpts().TimeTracePath, /*Binary=*/false,325 Clang->getFrontendOpts().TimeTracePath, /*Binary=*/false,
327 /*RemoveFileOnSignal=*/false,326 /*RemoveFileOnSignal=*/false,
src/zig_clang_cc1as_main.cpp+48-27
...@@ -12,12 +12,12 @@...@@ -12,12 +12,12 @@
12//===----------------------------------------------------------------------===//12//===----------------------------------------------------------------------===//
1313
14#include "clang/Basic/Diagnostic.h"14#include "clang/Basic/Diagnostic.h"
15#include "clang/Basic/DiagnosticFrontend.h"
15#include "clang/Basic/DiagnosticOptions.h"16#include "clang/Basic/DiagnosticOptions.h"
16#include "clang/Driver/DriverDiagnostic.h"17#include "clang/Driver/DriverDiagnostic.h"
17#include "clang/Driver/Options.h"
18#include "clang/Frontend/FrontendDiagnostic.h"
19#include "clang/Frontend/TextDiagnosticPrinter.h"18#include "clang/Frontend/TextDiagnosticPrinter.h"
20#include "clang/Frontend/Utils.h"19#include "clang/Frontend/Utils.h"
20#include "clang/Options/Options.h"
21#include "llvm/ADT/STLExtras.h"21#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/StringExtras.h"22#include "llvm/ADT/StringExtras.h"
23#include "llvm/ADT/StringSwitch.h"23#include "llvm/ADT/StringSwitch.h"
...@@ -45,6 +45,7 @@...@@ -45,6 +45,7 @@
45#include "llvm/Support/ErrorHandling.h"45#include "llvm/Support/ErrorHandling.h"
46#include "llvm/Support/FileSystem.h"46#include "llvm/Support/FileSystem.h"
47#include "llvm/Support/FormattedStream.h"47#include "llvm/Support/FormattedStream.h"
48#include "llvm/Support/IOSandbox.h"
48#include "llvm/Support/MemoryBuffer.h"49#include "llvm/Support/MemoryBuffer.h"
49#include "llvm/Support/Path.h"50#include "llvm/Support/Path.h"
50#include "llvm/Support/Process.h"51#include "llvm/Support/Process.h"
...@@ -59,8 +60,7 @@...@@ -59,8 +60,7 @@
59#include <optional>60#include <optional>
60#include <system_error>61#include <system_error>
61using namespace clang;62using namespace clang;
62using namespace clang::driver;63using namespace clang::options;
63using namespace clang::driver::options;
64using namespace llvm;64using namespace llvm;
65using namespace llvm::opt;65using namespace llvm::opt;
6666
...@@ -71,8 +71,8 @@ struct AssemblerInvocation {...@@ -71,8 +71,8 @@ struct AssemblerInvocation {
71 /// @name Target Options71 /// @name Target Options
72 /// @{72 /// @{
7373
74 /// The name of the target triple to assemble for.74 /// The target triple to assemble for.
75 std::string Triple;75 llvm::Triple Triple;
7676
77 /// If given, the name of the target CPU to determine which instructions77 /// If given, the name of the target CPU to determine which instructions
78 /// are legal.78 /// are legal.
...@@ -163,6 +163,10 @@ struct AssemblerInvocation {...@@ -163,6 +163,10 @@ struct AssemblerInvocation {
163 LLVM_PREFERRED_TYPE(bool)163 LLVM_PREFERRED_TYPE(bool)
164 unsigned EmitCompactUnwindNonCanonical : 1;164 unsigned EmitCompactUnwindNonCanonical : 1;
165165
166 // Whether to emit sframe unwind sections.
167 LLVM_PREFERRED_TYPE(bool)
168 unsigned EmitSFrameUnwind : 1;
169
166 LLVM_PREFERRED_TYPE(bool)170 LLVM_PREFERRED_TYPE(bool)
167 unsigned Crel : 1;171 unsigned Crel : 1;
168 LLVM_PREFERRED_TYPE(bool)172 LLVM_PREFERRED_TYPE(bool)
...@@ -192,9 +196,12 @@ struct AssemblerInvocation {...@@ -192,9 +196,12 @@ struct AssemblerInvocation {
192 std::string AsSecureLogFile;196 std::string AsSecureLogFile;
193 /// @}197 /// @}
194198
199 void setTriple(llvm::StringRef Str) {
200 Triple = llvm::Triple(llvm::Triple::normalize(Str));
201 }
202
195public:203public:
196 AssemblerInvocation() {204 AssemblerInvocation() {
197 Triple = "";
198 NoInitialTextSection = 0;205 NoInitialTextSection = 0;
199 InputFile = "-";206 InputFile = "-";
200 OutputPath = "-";207 OutputPath = "-";
...@@ -261,7 +268,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,...@@ -261,7 +268,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
261 // Construct the invocation.268 // Construct the invocation.
262269
263 // Target Options270 // Target Options
264 Opts.Triple = llvm::Triple::normalize(Args.getLastArgValue(OPT_triple));271 Opts.setTriple(Args.getLastArgValue(OPT_triple));
265 if (Arg *A = Args.getLastArg(options::OPT_darwin_target_variant_triple))272 if (Arg *A = Args.getLastArg(options::OPT_darwin_target_variant_triple))
266 Opts.DarwinTargetVariantTriple = llvm::Triple(A->getValue());273 Opts.DarwinTargetVariantTriple = llvm::Triple(A->getValue());
267 if (Arg *A = Args.getLastArg(OPT_darwin_target_variant_sdk_version_EQ)) {274 if (Arg *A = Args.getLastArg(OPT_darwin_target_variant_sdk_version_EQ)) {
...@@ -278,7 +285,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,...@@ -278,7 +285,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
278285
279 // Use the default target triple if unspecified.286 // Use the default target triple if unspecified.
280 if (Opts.Triple.empty())287 if (Opts.Triple.empty())
281 Opts.Triple = llvm::sys::getDefaultTargetTriple();288 Opts.setTriple(llvm::sys::getDefaultTargetTriple());
282289
283 // Language Options290 // Language Options
284 Opts.IncludePaths = Args.getAllArgValues(OPT_I);291 Opts.IncludePaths = Args.getAllArgValues(OPT_I);
...@@ -385,6 +392,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,...@@ -385,6 +392,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
385392
386 Opts.EmitCompactUnwindNonCanonical =393 Opts.EmitCompactUnwindNonCanonical =
387 Args.hasArg(OPT_femit_compact_unwind_non_canonical);394 Args.hasArg(OPT_femit_compact_unwind_non_canonical);
395 Opts.EmitSFrameUnwind = Args.hasArg(OPT_gsframe);
388 Opts.Crel = Args.hasArg(OPT_crel);396 Opts.Crel = Args.hasArg(OPT_crel);
389 Opts.ImplicitMapsyms = Args.hasArg(OPT_mmapsyms_implicit);397 Opts.ImplicitMapsyms = Args.hasArg(OPT_mmapsyms_implicit);
390 Opts.X86RelaxRelocations = !Args.hasArg(OPT_mrelax_relocations_no);398 Opts.X86RelaxRelocations = !Args.hasArg(OPT_mrelax_relocations_no);
...@@ -414,15 +422,19 @@ getOutputStream(StringRef Path, DiagnosticsEngine &Diags, bool Binary) {...@@ -414,15 +422,19 @@ getOutputStream(StringRef Path, DiagnosticsEngine &Diags, bool Binary) {
414}422}
415423
416static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,424static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
417 DiagnosticsEngine &Diags) {425 DiagnosticsEngine &Diags,
426 IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
418 // Get the target specific parser.427 // Get the target specific parser.
419 std::string Error;428 std::string Error;
420 const Target *TheTarget = TargetRegistry::lookupTarget(Opts.Triple, Error);429 const Target *TheTarget = TargetRegistry::lookupTarget(Opts.Triple, Error);
421 if (!TheTarget)430 if (!TheTarget)
422 return Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;431 return Diags.Report(diag::err_target_unknown_triple) << Opts.Triple.str();
423432
424 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =433 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = [&] {
425 MemoryBuffer::getFileOrSTDIN(Opts.InputFile, /*IsText=*/true);434 // FIXME(sandboxing): Make this a proper input file.
435 auto BypassSandbox = sys::sandbox::scopedDisable();
436 return MemoryBuffer::getFileOrSTDIN(Opts.InputFile, /*IsText=*/true);
437 }();
426438
427 if (std::error_code EC = Buffer.getError()) {439 if (std::error_code EC = Buffer.getError()) {
428 return Diags.Report(diag::err_fe_error_reading)440 return Diags.Report(diag::err_fe_error_reading)
...@@ -437,6 +449,7 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,...@@ -437,6 +449,7 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
437 // Record the location of the include directories so that the lexer can find449 // Record the location of the include directories so that the lexer can find
438 // it later.450 // it later.
439 SrcMgr.setIncludeDirs(Opts.IncludePaths);451 SrcMgr.setIncludeDirs(Opts.IncludePaths);
452 SrcMgr.setVirtualFileSystem(VFS);
440453
441 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));454 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
442 assert(MRI && "Unable to create target register info!");455 assert(MRI && "Unable to create target register info!");
...@@ -445,11 +458,13 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,...@@ -445,11 +458,13 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
445 MCOptions.MCRelaxAll = Opts.RelaxAll;458 MCOptions.MCRelaxAll = Opts.RelaxAll;
446 MCOptions.EmitDwarfUnwind = Opts.EmitDwarfUnwind;459 MCOptions.EmitDwarfUnwind = Opts.EmitDwarfUnwind;
447 MCOptions.EmitCompactUnwindNonCanonical = Opts.EmitCompactUnwindNonCanonical;460 MCOptions.EmitCompactUnwindNonCanonical = Opts.EmitCompactUnwindNonCanonical;
461 MCOptions.EmitSFrameUnwind = Opts.EmitSFrameUnwind;
448 MCOptions.MCSaveTempLabels = Opts.SaveTemporaryLabels;462 MCOptions.MCSaveTempLabels = Opts.SaveTemporaryLabels;
449 MCOptions.Crel = Opts.Crel;463 MCOptions.Crel = Opts.Crel;
450 MCOptions.ImplicitMapSyms = Opts.ImplicitMapsyms;464 MCOptions.ImplicitMapSyms = Opts.ImplicitMapsyms;
451 MCOptions.X86RelaxRelocations = Opts.X86RelaxRelocations;465 MCOptions.X86RelaxRelocations = Opts.X86RelaxRelocations;
452 MCOptions.X86Sse2Avx = Opts.X86Sse2Avx;466 MCOptions.X86Sse2Avx = Opts.X86Sse2Avx;
467 MCOptions.MCNoExecStack = Opts.NoExecStack;
453 MCOptions.CompressDebugSections = Opts.CompressDebugSections;468 MCOptions.CompressDebugSections = Opts.CompressDebugSections;
454 MCOptions.AsSecureLogFile = Opts.AsSecureLogFile;469 MCOptions.AsSecureLogFile = Opts.AsSecureLogFile;
455470
...@@ -477,7 +492,10 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,...@@ -477,7 +492,10 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
477492
478 std::unique_ptr<MCSubtargetInfo> STI(493 std::unique_ptr<MCSubtargetInfo> STI(
479 TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS));494 TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS));
480 assert(STI && "Unable to create subtarget info!");495 if (!STI) {
496 return Diags.Report(diag::err_fe_unable_to_create_subtarget)
497 << Opts.CPU << FS.empty() << FS;
498 }
481499
482 MCContext Ctx(Triple(Opts.Triple), MAI.get(), MRI.get(), STI.get(), &SrcMgr,500 MCContext Ctx(Triple(Opts.Triple), MAI.get(), MRI.get(), STI.get(), &SrcMgr,
483 &MCOptions);501 &MCOptions);
...@@ -509,9 +527,8 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,...@@ -509,9 +527,8 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
509 Ctx.setCompilationDir(Opts.DebugCompilationDir);527 Ctx.setCompilationDir(Opts.DebugCompilationDir);
510 else {528 else {
511 // If no compilation dir is set, try to use the current directory.529 // If no compilation dir is set, try to use the current directory.
512 SmallString<128> CWD;530 if (auto CWD = VFS->getCurrentWorkingDirectory())
513 if (!sys::fs::current_path(CWD))531 Ctx.setCompilationDir(*CWD);
514 Ctx.setCompilationDir(CWD);
515 }532 }
516 if (!Opts.DebugPrefixMap.empty())533 if (!Opts.DebugPrefixMap.empty())
517 for (const auto &KV : Opts.DebugPrefixMap)534 for (const auto &KV : Opts.DebugPrefixMap)
...@@ -577,7 +594,6 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,...@@ -577,7 +594,6 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
577 Triple T(Opts.Triple);594 Triple T(Opts.Triple);
578 Str.reset(TheTarget->createMCObjectStreamer(595 Str.reset(TheTarget->createMCObjectStreamer(
579 T, Ctx, std::move(MAB), std::move(OW), std::move(CE), *STI));596 T, Ctx, std::move(MAB), std::move(OW), std::move(CE), *STI));
580 Str->initSections(Opts.NoExecStack, *STI);
581 if (T.isOSBinFormatMachO() && T.isOSDarwin()) {597 if (T.isOSBinFormatMachO() && T.isOSDarwin()) {
582 Triple *TVT = Opts.DarwinTargetVariantTriple598 Triple *TVT = Opts.DarwinTargetVariantTriple
583 ? &*Opts.DarwinTargetVariantTriple599 ? &*Opts.DarwinTargetVariantTriple
...@@ -605,7 +621,7 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,...@@ -605,7 +621,7 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
605 std::unique_ptr<MCTargetAsmParser> TAP(621 std::unique_ptr<MCTargetAsmParser> TAP(
606 TheTarget->createMCAsmParser(*STI, *Parser, *MCII, MCOptions));622 TheTarget->createMCAsmParser(*STI, *Parser, *MCII, MCOptions));
607 if (!TAP)623 if (!TAP)
608 Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;624 Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple.str();
609625
610 // Set values for symbols, if any.626 // Set values for symbols, if any.
611 for (auto &S : Opts.SymbolDefs) {627 for (auto &S : Opts.SymbolDefs) {
...@@ -627,8 +643,9 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,...@@ -627,8 +643,9 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
627}643}
628644
629static bool ExecuteAssembler(AssemblerInvocation &Opts,645static bool ExecuteAssembler(AssemblerInvocation &Opts,
630 DiagnosticsEngine &Diags) {646 DiagnosticsEngine &Diags,
631 bool Failed = ExecuteAssemblerImpl(Opts, Diags);647 IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
648 bool Failed = ExecuteAssemblerImpl(Opts, Diags, VFS);
632649
633 // Delete output file if there were errors.650 // Delete output file if there were errors.
634 if (Failed) {651 if (Failed) {
...@@ -662,8 +679,12 @@ int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {...@@ -662,8 +679,12 @@ int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
662 TextDiagnosticPrinter *DiagClient =679 TextDiagnosticPrinter *DiagClient =
663 new TextDiagnosticPrinter(errs(), DiagOpts);680 new TextDiagnosticPrinter(errs(), DiagOpts);
664 DiagClient->setPrefix("clang -cc1as");681 DiagClient->setPrefix("clang -cc1as");
665 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());682 DiagnosticsEngine Diags(DiagnosticIDs::create(), DiagOpts, DiagClient);
666 DiagnosticsEngine Diags(DiagID, DiagOpts, DiagClient);683
684 auto VFS = [] {
685 auto BypassSandbox = sys::sandbox::scopedDisable();
686 return vfs::getRealFileSystem();
687 }();
667688
668 // Set an error handler, so that any LLVM backend diagnostics go through our689 // Set an error handler, so that any LLVM backend diagnostics go through our
669 // error handler.690 // error handler.
...@@ -679,8 +700,7 @@ int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {...@@ -679,8 +700,7 @@ int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
679 getDriverOptTable().printHelp(700 getDriverOptTable().printHelp(
680 llvm::outs(), "clang -cc1as [options] file...",701 llvm::outs(), "clang -cc1as [options] file...",
681 "Clang Integrated Assembler", /*ShowHidden=*/false,702 "Clang Integrated Assembler", /*ShowHidden=*/false,
682 /*ShowAllAliases=*/false,703 /*ShowAllAliases=*/false, llvm::opt::Visibility(options::CC1AsOption));
683 llvm::opt::Visibility(driver::options::CC1AsOption));
684704
685 return 0;705 return 0;
686 }706 }
...@@ -703,11 +723,12 @@ int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {...@@ -703,11 +723,12 @@ int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
703 for (unsigned i = 0; i != NumArgs; ++i)723 for (unsigned i = 0; i != NumArgs; ++i)
704 Args[i + 1] = Asm.LLVMArgs[i].c_str();724 Args[i + 1] = Asm.LLVMArgs[i].c_str();
705 Args[NumArgs + 1] = nullptr;725 Args[NumArgs + 1] = nullptr;
706 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());726 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get(), /*Overview=*/"",
727 /*Errs=*/nullptr, /*VFS=*/VFS.get());
707 }728 }
708729
709 // Execute the invocation, unless there were parsing errors.730 // Execute the invocation, unless there were parsing errors.
710 bool Failed = Diags.hasErrorOccurred() || ExecuteAssembler(Asm, Diags);731 bool Failed = Diags.hasErrorOccurred() || ExecuteAssembler(Asm, Diags, VFS);
711732
712 // If any timers were active but haven't been destroyed yet, print their733 // If any timers were active but haven't been destroyed yet, print their
713 // results now.734 // results now.
src/zig_clang_driver.cpp+22-14
...@@ -18,13 +18,13 @@...@@ -18,13 +18,13 @@
18#include "clang/Config/config.h"18#include "clang/Config/config.h"
19#include "clang/Driver/Compilation.h"19#include "clang/Driver/Compilation.h"
20#include "clang/Driver/DriverDiagnostic.h"20#include "clang/Driver/DriverDiagnostic.h"
21#include "clang/Driver/Options.h"
22#include "clang/Driver/ToolChain.h"21#include "clang/Driver/ToolChain.h"
23#include "clang/Frontend/ChainedDiagnosticConsumer.h"22#include "clang/Frontend/ChainedDiagnosticConsumer.h"
24#include "clang/Frontend/CompilerInvocation.h"23#include "clang/Frontend/CompilerInvocation.h"
25#include "clang/Frontend/SerializedDiagnosticPrinter.h"24#include "clang/Frontend/SerializedDiagnosticPrinter.h"
26#include "clang/Frontend/TextDiagnosticPrinter.h"25#include "clang/Frontend/TextDiagnosticPrinter.h"
27#include "clang/Frontend/Utils.h"26#include "clang/Frontend/Utils.h"
27#include "clang/Options/Options.h"
28#include "llvm/ADT/ArrayRef.h"28#include "llvm/ADT/ArrayRef.h"
29#include "llvm/ADT/SmallString.h"29#include "llvm/ADT/SmallString.h"
30#include "llvm/ADT/SmallVector.h"30#include "llvm/ADT/SmallVector.h"
...@@ -38,6 +38,7 @@...@@ -38,6 +38,7 @@
38#include "llvm/Support/CrashRecoveryContext.h"38#include "llvm/Support/CrashRecoveryContext.h"
39#include "llvm/Support/ErrorHandling.h"39#include "llvm/Support/ErrorHandling.h"
40#include "llvm/Support/FileSystem.h"40#include "llvm/Support/FileSystem.h"
41#include "llvm/Support/IOSandbox.h"
41#include "llvm/Support/LLVMDriver.h"42#include "llvm/Support/LLVMDriver.h"
42#include "llvm/Support/Path.h"43#include "llvm/Support/Path.h"
43#include "llvm/Support/PrettyStackTrace.h"44#include "llvm/Support/PrettyStackTrace.h"
...@@ -201,7 +202,8 @@ static void FixupDiagPrefixExeName(TextDiagnosticPrinter *DiagClient,...@@ -201,7 +202,8 @@ static void FixupDiagPrefixExeName(TextDiagnosticPrinter *DiagClient,
201}202}
202203
203static int ExecuteCC1Tool(SmallVectorImpl<const char *> &ArgV,204static int ExecuteCC1Tool(SmallVectorImpl<const char *> &ArgV,
204 const llvm::ToolContext &ToolContext) {205 const llvm::ToolContext &ToolContext,
206 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
205 // If we call the cc1 tool from the clangDriver library (through207 // If we call the cc1 tool from the clangDriver library (through
206 // Driver::CC1Main), we need to clean up the options usage count. The options208 // Driver::CC1Main), we need to clean up the options usage count. The options
207 // are currently global, and they might have been used previously by the209 // are currently global, and they might have been used previously by the
...@@ -209,7 +211,8 @@ static int ExecuteCC1Tool(SmallVectorImpl<const char *> &ArgV,...@@ -209,7 +211,8 @@ static int ExecuteCC1Tool(SmallVectorImpl<const char *> &ArgV,
209 llvm::cl::ResetAllOptionOccurrences();211 llvm::cl::ResetAllOptionOccurrences();
210212
211 llvm::BumpPtrAllocator A;213 llvm::BumpPtrAllocator A;
212 llvm::cl::ExpansionContext ECtx(A, llvm::cl::TokenizeGNUCommandLine);214 llvm::cl::ExpansionContext ECtx(A, llvm::cl::TokenizeGNUCommandLine,
215 VFS.get());
213 if (llvm::Error Err = ECtx.expandResponseFiles(ArgV)) {216 if (llvm::Error Err = ECtx.expandResponseFiles(ArgV)) {
214 llvm::errs() << toString(std::move(Err)) << '\n';217 llvm::errs() << toString(std::move(Err)) << '\n';
215 return 1;218 return 1;
...@@ -249,14 +252,22 @@ static int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContex...@@ -249,14 +252,22 @@ static int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContex
249 bool ClangCLMode =252 bool ClangCLMode =
250 IsClangCL(getDriverMode(ProgName, llvm::ArrayRef(Args).slice(1)));253 IsClangCL(getDriverMode(ProgName, llvm::ArrayRef(Args).slice(1)));
251254
252 if (llvm::Error Err = expandResponseFiles(Args, ClangCLMode, A)) {255 auto VFS = llvm::vfs::getRealFileSystem();
256
257 if (llvm::Error Err = expandResponseFiles(Args, ClangCLMode, A, VFS.get())) {
253 llvm::errs() << toString(std::move(Err)) << '\n';258 llvm::errs() << toString(std::move(Err)) << '\n';
254 return 1;259 return 1;
255 }260 }
256261
257 // Handle -cc1 integrated tools.262 // Handle -cc1 integrated tools.
258 if (Args.size() >= 2 && StringRef(Args[1]).starts_with("-cc1"))263 if (Args.size() >= 2 && StringRef(Args[1]).starts_with("-cc1")) {
259 return ExecuteCC1Tool(Args, ToolContext);264 // Note that this only enables the sandbox for direct -cc1 invocations and
265 // out-of-process -cc1 invocations launched by the driver. For in-process
266 // -cc1 invocations launched by the driver, the sandbox is enabled in
267 // CC1Command::Execute() for better crash recovery.
268 auto EnableSandbox = llvm::sys::sandbox::scopedEnable();
269 return ExecuteCC1Tool(Args, ToolContext, VFS);
270 }
260271
261 // Handle options that need handling before the real command line parsing in272 // Handle options that need handling before the real command line parsing in
262 // Driver::BuildCompilation()273 // Driver::BuildCompilation()
...@@ -326,9 +337,7 @@ static int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContex...@@ -326,9 +337,7 @@ static int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContex
326 new TextDiagnosticPrinter(llvm::errs(), *DiagOpts);337 new TextDiagnosticPrinter(llvm::errs(), *DiagOpts);
327 FixupDiagPrefixExeName(DiagClient, ProgName);338 FixupDiagPrefixExeName(DiagClient, ProgName);
328339
329 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());340 DiagnosticsEngine Diags(DiagnosticIDs::create(), *DiagOpts, DiagClient);
330
331 DiagnosticsEngine Diags(DiagID, *DiagOpts, DiagClient);
332341
333 if (!DiagOpts->DiagnosticSerializationFile.empty()) {342 if (!DiagOpts->DiagnosticSerializationFile.empty()) {
334 auto SerializedConsumer =343 auto SerializedConsumer =
...@@ -338,7 +347,6 @@ static int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContex...@@ -338,7 +347,6 @@ static int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContex
338 Diags.takeClient(), std::move(SerializedConsumer)));347 Diags.takeClient(), std::move(SerializedConsumer)));
339 }348 }
340349
341 auto VFS = llvm::vfs::getRealFileSystem();
342 ProcessWarningOptions(Diags, *DiagOpts, *VFS, /*ReportDiags=*/false);350 ProcessWarningOptions(Diags, *DiagOpts, *VFS, /*ReportDiags=*/false);
343351
344 Driver TheDriver(Path, llvm::sys::getDefaultTargetTriple(), Diags,352 Driver TheDriver(Path, llvm::sys::getDefaultTargetTriple(), Diags,
...@@ -358,10 +366,10 @@ static int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContex...@@ -358,10 +366,10 @@ static int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContex
358 if (!SetBackdoorDriverOutputsFromEnvVars(TheDriver))366 if (!SetBackdoorDriverOutputsFromEnvVars(TheDriver))
359 return 1;367 return 1;
360368
361 auto ExecuteCC1WithContext =369 auto ExecuteCC1WithContext = [&ToolContext,
362 [&ToolContext](SmallVectorImpl<const char *> &ArgV) {370 &VFS](SmallVectorImpl<const char *> &ArgV) {
363 return ExecuteCC1Tool(ArgV, ToolContext);371 return ExecuteCC1Tool(ArgV, ToolContext, VFS);
364 };372 };
365 if (!UseNewCC1Process) {373 if (!UseNewCC1Process) {
366 TheDriver.CC1Main = ExecuteCC1WithContext;374 TheDriver.CC1Main = ExecuteCC1WithContext;
367 // Ensure the CC1Command actually catches cc1 crashes375 // Ensure the CC1Command actually catches cc1 crashes
src/zig_llvm-ar.cpp+1
...@@ -12,6 +12,7 @@...@@ -12,6 +12,7 @@
12//===----------------------------------------------------------------------===//12//===----------------------------------------------------------------------===//
1313
14#include "llvm/ADT/StringExtras.h"14#include "llvm/ADT/StringExtras.h"
15#include "llvm/ADT/StringMap.h"
15#include "llvm/ADT/StringSwitch.h"16#include "llvm/ADT/StringSwitch.h"
16#include "llvm/BinaryFormat/Magic.h"17#include "llvm/BinaryFormat/Magic.h"
17#include "llvm/IR/LLVMContext.h"18#include "llvm/IR/LLVMContext.h"
src/zig_llvm.cpp+1-1
...@@ -439,7 +439,7 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi...@@ -439,7 +439,7 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi
439439
440void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit) {440void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit) {
441 static OptBisect opt_bisect;441 static OptBisect opt_bisect;
442 opt_bisect.setLimit(limit);442 opt_bisect.setIntervals({0, limit});
443 unwrap(context_ref)->setOptPassGate(opt_bisect);443 unwrap(context_ref)->setOptPassGate(opt_bisect);
444}444}
445445