Skip to content

Commit 5544dc5

Browse files
committed
Emit an error for unsupported object file formats in the triple
1 parent b18734b commit 5544dc5

File tree

4 files changed

+26
-33
lines changed

4 files changed

+26
-33
lines changed

Diff for: include/swift/AST/DiagnosticsFrontend.def

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ ERROR(error_unsupported_target_os, none,
3636
ERROR(error_unsupported_target_arch, none,
3737
"unsupported target architecture: '%0'", (StringRef))
3838

39+
ERROR(error_unsupported_object_file_format, none,
40+
"unsupported object file format: '%0'", (StringRef))
41+
3942
WARNING(warning_upcoming_feature_on_by_default, none,
4043
"upcoming feature '%0' is already enabled as of Swift version %1",
4144
(StringRef, unsigned))

Diff for: include/swift/Basic/LangOptions.h

+3-5
Original file line numberDiff line numberDiff line change
@@ -652,11 +652,9 @@ namespace swift {
652652
LangOptions();
653653

654654
/// Sets the target we are building for and updates platform conditions
655-
/// to match.
656-
///
657-
/// \returns A pair - the first element is true if the OS was invalid.
658-
/// The second element is true if the Arch was invalid.
659-
std::pair<bool, bool> setTarget(llvm::Triple triple);
655+
/// to match. Emits errors using `Diags` in case of an invalid OS, arch,
656+
/// etc.
657+
void setTarget(llvm::Triple triple, swift::DiagnosticEngine *Diags = nullptr);
660658

661659
/// Returns the minimum platform version to which code will be deployed.
662660
///

Diff for: lib/Basic/LangOptions.cpp

+17-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "swift/Basic/LangOptions.h"
1919
#include "swift/AST/DiagnosticEngine.h"
20+
#include "swift/AST/DiagnosticsFrontend.h"
2021
#include "swift/Basic/Assertions.h"
2122
#include "swift/Basic/Feature.h"
2223
#include "swift/Basic/FileTypes.h"
@@ -489,7 +490,8 @@ static bool isMultiThreadedRuntime(llvm::Triple triple) {
489490
return true;
490491
}
491492

492-
std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
493+
void LangOptions::setTarget(llvm::Triple triple,
494+
swift::DiagnosticEngine *Diags) {
493495
clearAllPlatformConditionValues();
494496
clearAtomicBitWidths();
495497

@@ -574,6 +576,11 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
574576
break;
575577
}
576578

579+
if (UnsupportedOS && Diags) {
580+
Diags->diagnose(SourceLoc(), diag::error_unsupported_target_os,
581+
Target.getOSName());
582+
}
583+
577584
bool UnsupportedArch = false;
578585

579586
// Set the "arch" platform condition.
@@ -632,8 +639,10 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
632639
}
633640
}
634641

635-
if (UnsupportedOS || UnsupportedArch)
636-
return { UnsupportedOS, UnsupportedArch };
642+
if (UnsupportedArch && Diags) {
643+
Diags->diagnose(SourceLoc(), diag::error_unsupported_target_arch,
644+
Target.getArchName());
645+
}
637646

638647
// Set the "_endian" platform condition.
639648
if (Target.isLittleEndian()) {
@@ -660,6 +669,11 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
660669
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "COFF");
661670
} else if (Target.isOSBinFormatWasm()) {
662671
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "Wasm");
672+
} else {
673+
if (Diags) {
674+
Diags->diagnose(SourceLoc(), diag::error_unsupported_object_file_format,
675+
Target.getObjectFormatTypeName(Target.getObjectFormat()));
676+
}
663677
}
664678

665679
// Set the "runtime" platform condition.
@@ -695,8 +709,6 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
695709
// If you add anything to this list, change the default size of
696710
// PlatformConditionValues to not require an extra allocation
697711
// in the common case.
698-
699-
return { false, false };
700712
}
701713

702714
llvm::StringRef swift::getPlaygroundOptionName(PlaygroundOption option) {

Diff for: lib/Frontend/CompilerInvocation.cpp

+3-23
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ void CompilerInvocation::setTargetTriple(StringRef Triple) {
409409
}
410410

411411
void CompilerInvocation::setTargetTriple(const llvm::Triple &Triple) {
412-
LangOpts.setTarget(Triple);
412+
LangOpts.setTarget(Triple, /*Diags*/nullptr);
413413
updateRuntimeLibraryPaths(SearchPathOpts, FrontendOpts, LangOpts);
414414
}
415415

@@ -1474,12 +1474,8 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
14741474
Opts.SkipNonExportableDecls = false;
14751475

14761476
llvm::Triple Target = Opts.Target;
1477-
StringRef TargetArg;
1478-
std::string TargetArgScratch;
1479-
14801477
if (const Arg *A = Args.getLastArg(OPT_target)) {
14811478
Target = llvm::Triple(A->getValue());
1482-
TargetArg = A->getValue();
14831479

14841480
const bool targetNeedsRemapping = Target.isXROS();
14851481
if (targetNeedsRemapping && Target.getOSMajorVersion() == 0) {
@@ -1506,8 +1502,6 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
15061502
if (tripleInfersSimulatorEnvironment(Target)) {
15071503
// Set the simulator environment.
15081504
Target.setEnvironment(llvm::Triple::EnvironmentType::Simulator);
1509-
TargetArgScratch = Target.str();
1510-
TargetArg = TargetArgScratch;
15111505
}
15121506
}
15131507

@@ -1562,21 +1556,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
15621556

15631557
// Must be processed after any other language options that could affect
15641558
// platform conditions.
1565-
bool UnsupportedOS, UnsupportedArch;
1566-
std::tie(UnsupportedOS, UnsupportedArch) = Opts.setTarget(Target);
1567-
1568-
SmallVector<StringRef, 3> TargetComponents;
1569-
TargetArg.split(TargetComponents, "-");
1570-
1571-
if (UnsupportedArch) {
1572-
auto TargetArgArch = TargetComponents.size() ? TargetComponents[0] : "";
1573-
Diags.diagnose(SourceLoc(), diag::error_unsupported_target_arch, TargetArgArch);
1574-
}
1575-
1576-
if (UnsupportedOS) {
1577-
auto TargetArgOS = TargetComponents.size() > 2 ? TargetComponents[2] : "";
1578-
Diags.diagnose(SourceLoc(), diag::error_unsupported_target_os, TargetArgOS);
1579-
}
1559+
Opts.setTarget(Target, &Diags);
15801560

15811561
// First, set up default minimum inlining target versions.
15821562
auto getDefaultMinimumInliningTargetVersion =
@@ -1827,7 +1807,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
18271807
Opts.enableFeature(Feature::ParserValidation);
18281808
}
18291809
#endif
1830-
return HadError || UnsupportedOS || UnsupportedArch;
1810+
return HadError || Diags.hasFatalErrorOccurred();
18311811
}
18321812

18331813
static bool ParseTypeCheckerArgs(TypeCheckerOptions &Opts, ArgList &Args,

0 commit comments

Comments
 (0)