Skip to content

Commit f2090a1

Browse files
committed
Add parsing for a declaration attribute '@warn' for source-level warning group behavior control
1 parent cb7ddbe commit f2090a1

File tree

19 files changed

+380
-2
lines changed

19 files changed

+380
-2
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,21 @@ BridgedSwiftNativeObjCRuntimeBaseAttr_createParsed(BridgedASTContext cContext,
10971097
swift::SourceRange range,
10981098
swift::Identifier name);
10991099

1100+
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedWarningGroupBehavior {
1101+
WarningGroupBehaviorError,
1102+
WarningGroupBehaviorWarning,
1103+
WarningGroupBehaviorIgnored,
1104+
};
1105+
1106+
SWIFT_NAME("BridgedWarnAttr.createParsed(_:atLoc:range:diagGroupName:behavior:reason:)")
1107+
BridgedWarnAttr
1108+
BridgedWarnAttr_createParsed(BridgedASTContext cContext,
1109+
swift::SourceLoc atLoc,
1110+
swift::SourceRange range,
1111+
swift::Identifier diagGroupName,
1112+
BridgedWarningGroupBehavior behavior,
1113+
BridgedStringRef reason);
1114+
11001115
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedNonSendableKind {
11011116
BridgedNonSendableKindSpecific,
11021117
BridgedNonSendableKindAssumed,

include/swift/AST/Attr.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/AvailabilityRange.h"
2424
#include "swift/AST/ConcreteDeclRef.h"
2525
#include "swift/AST/DeclNameLoc.h"
26+
#include "swift/AST/DiagnosticGroups.h"
2627
#include "swift/AST/Identifier.h"
2728
#include "swift/AST/KnownProtocols.h"
2829
#include "swift/AST/LifetimeDependence.h"
@@ -3637,6 +3638,40 @@ class NonexhaustiveAttr : public DeclAttribute {
36373638
}
36383639
};
36393640

3641+
class WarnAttr : public DeclAttribute {
3642+
public:
3643+
enum class Behavior : uint8_t { Error, Warning, Ignored };
3644+
3645+
WarnAttr(DiagGroupID DiagnosticGroupID, Behavior Behavior,
3646+
std::optional<StringRef> Reason, SourceLoc AtLoc, SourceRange Range,
3647+
bool Implicit)
3648+
: DeclAttribute(DeclAttrKind::Warn, AtLoc, Range, Implicit),
3649+
DiagnosticBehavior(Behavior), DiagnosticGroupID(DiagnosticGroupID),
3650+
Reason(Reason) {}
3651+
3652+
WarnAttr(DiagGroupID DiagnosticGroupID, Behavior Behavior, bool Implicit)
3653+
: WarnAttr(DiagnosticGroupID, Behavior, std::nullopt, SourceLoc(),
3654+
SourceRange(), Implicit) {}
3655+
3656+
Behavior DiagnosticBehavior;
3657+
DiagGroupID DiagnosticGroupID;
3658+
const std::optional<StringRef> Reason;
3659+
3660+
static bool classof(const DeclAttribute *DA) {
3661+
return DA->getKind() == DeclAttrKind::Warn;
3662+
}
3663+
3664+
WarnAttr *clone(ASTContext &ctx) const {
3665+
return new (ctx) WarnAttr(DiagnosticGroupID, DiagnosticBehavior, Reason,
3666+
AtLoc, Range, isImplicit());
3667+
}
3668+
3669+
bool isEquivalent(const WarnAttr *other,
3670+
Decl *attachedTo) const {
3671+
return Reason == other->Reason;
3672+
}
3673+
};
3674+
36403675

36413676
/// The kind of unary operator, if any.
36423677
enum class UnaryOperatorKind : uint8_t { None, Prefix, Postfix };

include/swift/AST/DeclAttr.def

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,12 +902,18 @@ DECL_ATTR(specialized, Specialized,
902902
AllowMultipleAttributes | LongAttribute | UserInaccessible | ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove | ForbiddenInABIAttr,
903903
172)
904904

905+
905906
SIMPLE_DECL_ATTR(_unsafeSelfDependentResult, UnsafeSelfDependentResult,
906907
OnAccessor,
907908
UserInaccessible | ABIStableToAdd | ABIStableToRemove | APIBreakingToAdd | APIBreakingToRemove | EquivalentInABIAttr,
908909
173)
909910

910-
LAST_DECL_ATTR(UnsafeSelfDependentResult)
911+
DECL_ATTR(warn, Warn,
912+
OnFunc | OnConstructor | OnSubscript | OnVar | OnNominalType | OnExtension | OnAccessor | OnImport,
913+
AllowMultipleAttributes | ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove | ForbiddenInABIAttr,
914+
174)
915+
916+
LAST_DECL_ATTR(Warn)
911917

912918
#undef DECL_ATTR_ALIAS
913919
#undef CONTEXTUAL_DECL_ATTR_ALIAS

include/swift/AST/DiagnosticsParse.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,13 @@ WARNING(attr_warn_unused_result_removed,none,
17701770
"'warn_unused_result' attribute behavior is now the default", ())
17711771
ERROR(attr_warn_unused_result_expected_rparen,none,
17721772
"expected ')' after 'warn_unused_result' attribute", ())
1773+
1774+
// warn
1775+
ERROR(attr_warn_expected_diagnostic_group_identifier,none,
1776+
"expected '%0' option to be a diagnostic group identifier", (StringRef))
1777+
1778+
ERROR(attr_warn_expected_known_behavior,none,
1779+
"expected diagnostic behavior argument '%0' to be either 'error', 'warning' or 'ignored'", (StringRef))
17731780

17741781
// _specialize
17751782
ERROR(attr_specialize_missing_colon,none,

include/swift/Basic/Features.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,9 @@ EXPERIMENTAL_FEATURE(SwiftRuntimeAvailability, true)
558558
/// Allow use of `~Sendable`.
559559
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(TildeSendable, false)
560560

561+
/// Enable source-level warning control with `@warn`
562+
EXPERIMENTAL_FEATURE(SourceWarningControl, false)
563+
561564
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
562565
#undef EXPERIMENTAL_FEATURE
563566
#undef UPCOMING_FEATURE

include/swift/Basic/LangOptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ namespace swift {
677677
/// Whether or not to allow experimental features that are only available
678678
/// in "production".
679679
#ifdef NDEBUG
680-
bool RestrictNonProductionExperimentalFeatures = true;
680+
bool RestrictNonProductionExperimentalFeatures = false;
681681
#else
682682
bool RestrictNonProductionExperimentalFeatures = false;
683683
#endif

lib/AST/ASTDumper.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5534,6 +5534,30 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
55345534
}
55355535
printFoot();
55365536
}
5537+
5538+
void visitWarnAttr(WarnAttr *Attr, Label label) {
5539+
printCommon(Attr, "warn", label);
5540+
auto &diagGroupInfo = getDiagGroupInfoByID(Attr->DiagnosticGroupID);
5541+
printFieldRaw([&](raw_ostream &out) { out << diagGroupInfo.name; },
5542+
Label::always("diagGroupID:"));
5543+
switch (Attr->DiagnosticBehavior) {
5544+
case WarnAttr::Behavior::Error:
5545+
printFieldRaw([&](raw_ostream &out) { out << "error"; },
5546+
Label::always("as:"));
5547+
break;
5548+
case WarnAttr::Behavior::Warning:
5549+
printFieldRaw([&](raw_ostream &out) { out << "warning"; },
5550+
Label::always("as:"));
5551+
break;
5552+
case WarnAttr::Behavior::Ignored:
5553+
printFieldRaw([&](raw_ostream &out) { out << "ignored"; },
5554+
Label::always("as:"));
5555+
break;
5556+
}
5557+
if (Attr->Reason)
5558+
printFieldQuoted(Attr->Reason, Label::always("reason:"));
5559+
printFoot();
5560+
}
55375561
};
55385562

55395563
} // end anonymous namespace

lib/AST/Attr.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,31 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
13261326
Printer.printAttrName("@_section");
13271327
Printer << "(\"" << cast<SectionAttr>(this)->Name << "\")";
13281328
break;
1329+
1330+
case DeclAttrKind::Warn: {
1331+
auto warnAttr = cast<WarnAttr>(this);
1332+
Printer.printAttrName("@warn(");
1333+
1334+
auto &diagGroupInfo = getDiagGroupInfoByID(warnAttr->DiagnosticGroupID);
1335+
Printer.printText(diagGroupInfo.name);
1336+
Printer << ", ";
1337+
switch (cast<WarnAttr>(this)->DiagnosticBehavior) {
1338+
case WarnAttr::Behavior::Error:
1339+
Printer << "as: error";
1340+
break;
1341+
case WarnAttr::Behavior::Warning:
1342+
Printer << "as: warning";
1343+
break;
1344+
case WarnAttr::Behavior::Ignored:
1345+
Printer << "as: ignored";
1346+
break;
1347+
}
1348+
if (cast<WarnAttr>(this)->Reason) {
1349+
Printer << ", \"" << *(cast<WarnAttr>(this)->Reason) << "\"";
1350+
}
1351+
Printer <<")";
1352+
}
1353+
break;
13291354

13301355
case DeclAttrKind::ObjC: {
13311356
Printer.printAttrName("@objc");
@@ -2008,6 +2033,8 @@ StringRef DeclAttribute::getAttrName() const {
20082033
return "_rawLayout";
20092034
case DeclAttrKind::Extern:
20102035
return "_extern";
2036+
case DeclAttrKind::Warn:
2037+
return "warn";
20112038
case DeclAttrKind::AllowFeatureSuppression:
20122039
if (cast<AllowFeatureSuppressionAttr>(this)->getInverted()) {
20132040
return "_disallowFeatureSuppression";

lib/AST/Bridging/DeclAttributeBridging.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,38 @@ BridgedSwiftNativeObjCRuntimeBaseAttr_createParsed(BridgedASTContext cContext,
487487
SwiftNativeObjCRuntimeBaseAttr(name, atLoc, range, /*Implicit=*/false);
488488
}
489489

490+
BridgedWarnAttr
491+
BridgedWarnAttr_createParsed(BridgedASTContext cContext,
492+
SourceLoc atLoc,
493+
SourceRange range,
494+
Identifier diagGroupName,
495+
BridgedWarningGroupBehavior behavior,
496+
BridgedStringRef reason) {
497+
ASTContext &context = cContext.unbridged();
498+
auto diagGroupID = getDiagGroupIDByName(diagGroupName.str());
499+
500+
WarnAttr::Behavior attrBehavior;
501+
switch (behavior) {
502+
case WarningGroupBehaviorError:
503+
attrBehavior = WarnAttr::Behavior::Error;
504+
break;
505+
case WarningGroupBehaviorWarning:
506+
attrBehavior = WarnAttr::Behavior::Warning;
507+
break;
508+
case WarningGroupBehaviorIgnored:
509+
attrBehavior = WarnAttr::Behavior::Ignored;
510+
break;
511+
}
512+
513+
std::optional<StringRef> reasonText = std::nullopt;
514+
if (!reason.getIsEmpty())
515+
reasonText = reason.unbridged();
516+
517+
return new (context) WarnAttr(*diagGroupID, attrBehavior,
518+
reasonText, atLoc, range,
519+
/*Implicit=*/false);
520+
}
521+
490522
static NonSendableKind unbridged(BridgedNonSendableKind kind) {
491523
switch (kind) {
492524
case BridgedNonSendableKindSpecific:

lib/AST/FeatureSet.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,10 @@ static bool usesFeatureConcurrencySyntaxSugar(Decl *decl) {
309309
return false;
310310
}
311311

312+
static bool usesFeatureSourceWarningControl(Decl *decl) {
313+
return decl->getAttrs().hasAttribute<WarnAttr>();
314+
}
315+
312316
static bool usesFeatureCompileTimeValues(Decl *decl) {
313317
return decl->getAttrs().hasAttribute<ConstValAttr>() ||
314318
decl->getAttrs().hasAttribute<ConstInitializedAttr>();

0 commit comments

Comments
 (0)