Skip to content

Commit 76ce3a5

Browse files
committed
Add parsing for a declaration attribute '@warn' for source-level warning group behavior control
1 parent 6d4c516 commit 76ce3a5

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
@@ -1149,6 +1149,21 @@ BridgedSwiftNativeObjCRuntimeBaseAttr_createParsed(BridgedASTContext cContext,
11491149
swift::SourceRange range,
11501150
swift::Identifier name);
11511151

1152+
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedWarningGroupBehavior {
1153+
WarningGroupBehaviorError,
1154+
WarningGroupBehaviorWarning,
1155+
WarningGroupBehaviorIgnored,
1156+
};
1157+
1158+
SWIFT_NAME("BridgedWarnAttr.createParsed(_:atLoc:range:diagGroupName:behavior:reason:)")
1159+
BridgedWarnAttr
1160+
BridgedWarnAttr_createParsed(BridgedASTContext cContext,
1161+
swift::SourceLoc atLoc,
1162+
swift::SourceRange range,
1163+
swift::Identifier diagGroupName,
1164+
BridgedWarningGroupBehavior behavior,
1165+
BridgedStringRef reason);
1166+
11521167
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedNonSendableKind {
11531168
BridgedNonSendableKindSpecific,
11541169
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/ExportKind.h"
2728
#include "swift/AST/Identifier.h"
2829
#include "swift/AST/KnownProtocols.h"
@@ -3678,6 +3679,40 @@ class NonexhaustiveAttr : public DeclAttribute {
36783679
}
36793680
};
36803681

3682+
class WarnAttr : public DeclAttribute {
3683+
public:
3684+
enum class Behavior : uint8_t { Error, Warning, Ignored };
3685+
3686+
WarnAttr(DiagGroupID DiagnosticGroupID, Behavior Behavior,
3687+
std::optional<StringRef> Reason, SourceLoc AtLoc, SourceRange Range,
3688+
bool Implicit)
3689+
: DeclAttribute(DeclAttrKind::Warn, AtLoc, Range, Implicit),
3690+
DiagnosticBehavior(Behavior), DiagnosticGroupID(DiagnosticGroupID),
3691+
Reason(Reason) {}
3692+
3693+
WarnAttr(DiagGroupID DiagnosticGroupID, Behavior Behavior, bool Implicit)
3694+
: WarnAttr(DiagnosticGroupID, Behavior, std::nullopt, SourceLoc(),
3695+
SourceRange(), Implicit) {}
3696+
3697+
Behavior DiagnosticBehavior;
3698+
DiagGroupID DiagnosticGroupID;
3699+
const std::optional<StringRef> Reason;
3700+
3701+
static bool classof(const DeclAttribute *DA) {
3702+
return DA->getKind() == DeclAttrKind::Warn;
3703+
}
3704+
3705+
WarnAttr *clone(ASTContext &ctx) const {
3706+
return new (ctx) WarnAttr(DiagnosticGroupID, DiagnosticBehavior, Reason,
3707+
AtLoc, Range, isImplicit());
3708+
}
3709+
3710+
bool isEquivalent(const WarnAttr *other,
3711+
Decl *attachedTo) const {
3712+
return Reason == other->Reason;
3713+
}
3714+
};
3715+
36813716

36823717
/// The kind of unary operator, if any.
36833718
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
@@ -905,12 +905,18 @@ DECL_ATTR(specialized, Specialized,
905905
AllowMultipleAttributes | LongAttribute | UserInaccessible | ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove | ForbiddenInABIAttr,
906906
172)
907907

908+
908909
SIMPLE_DECL_ATTR(_unsafeSelfDependentResult, UnsafeSelfDependentResult,
909910
OnAccessor,
910911
UserInaccessible | ABIStableToAdd | ABIStableToRemove | APIBreakingToAdd | APIBreakingToRemove | EquivalentInABIAttr,
911912
173)
912913

913-
LAST_DECL_ATTR(UnsafeSelfDependentResult)
914+
DECL_ATTR(warn, Warn,
915+
OnFunc | OnConstructor | OnDestructor | OnSubscript | OnVar | OnNominalType | OnExtension | OnAccessor | OnImport,
916+
AllowMultipleAttributes | ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove | ForbiddenInABIAttr,
917+
174)
918+
919+
LAST_DECL_ATTR(Warn)
914920

915921
#undef DECL_ATTR_ALIAS
916922
#undef CONTEXTUAL_DECL_ATTR_ALIAS

include/swift/AST/DiagnosticsParse.def

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

17721779
// _specialize
17731780
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
@@ -564,6 +564,9 @@ EXPERIMENTAL_FEATURE(CheckImplementationOnlyStrict, false)
564564
/// Check that use sites have the required @_spi import for operators.
565565
EXPERIMENTAL_FEATURE(EnforceSPIOperatorGroup, true)
566566

567+
/// Enable source-level warning control with `@warn`
568+
EXPERIMENTAL_FEATURE(SourceWarningControl, true)
569+
567570
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
568571
#undef EXPERIMENTAL_FEATURE
569572
#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
@@ -5610,6 +5610,30 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
56105610
}
56115611
printFoot();
56125612
}
5613+
5614+
void visitWarnAttr(WarnAttr *Attr, Label label) {
5615+
printCommon(Attr, "warn", label);
5616+
auto &diagGroupInfo = getDiagGroupInfoByID(Attr->DiagnosticGroupID);
5617+
printFieldRaw([&](raw_ostream &out) { out << diagGroupInfo.name; },
5618+
Label::always("diagGroupID:"));
5619+
switch (Attr->DiagnosticBehavior) {
5620+
case WarnAttr::Behavior::Error:
5621+
printFieldRaw([&](raw_ostream &out) { out << "error"; },
5622+
Label::always("as:"));
5623+
break;
5624+
case WarnAttr::Behavior::Warning:
5625+
printFieldRaw([&](raw_ostream &out) { out << "warning"; },
5626+
Label::always("as:"));
5627+
break;
5628+
case WarnAttr::Behavior::Ignored:
5629+
printFieldRaw([&](raw_ostream &out) { out << "ignored"; },
5630+
Label::always("as:"));
5631+
break;
5632+
}
5633+
if (Attr->Reason)
5634+
printFieldQuoted(Attr->Reason, Label::always("reason:"));
5635+
printFoot();
5636+
}
56135637
};
56145638

56155639
} // 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");
@@ -2017,6 +2042,8 @@ StringRef DeclAttribute::getAttrName() const {
20172042
return "_rawLayout";
20182043
case DeclAttrKind::Extern:
20192044
return "_extern";
2045+
case DeclAttrKind::Warn:
2046+
return "warn";
20202047
case DeclAttrKind::AllowFeatureSuppression:
20212048
if (cast<AllowFeatureSuppressionAttr>(this)->getInverted()) {
20222049
return "_disallowFeatureSuppression";

lib/AST/Bridging/DeclAttributeBridging.cpp

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

503+
BridgedWarnAttr
504+
BridgedWarnAttr_createParsed(BridgedASTContext cContext,
505+
SourceLoc atLoc,
506+
SourceRange range,
507+
Identifier diagGroupName,
508+
BridgedWarningGroupBehavior behavior,
509+
BridgedStringRef reason) {
510+
ASTContext &context = cContext.unbridged();
511+
auto diagGroupID = getDiagGroupIDByName(diagGroupName.str());
512+
513+
WarnAttr::Behavior attrBehavior;
514+
switch (behavior) {
515+
case WarningGroupBehaviorError:
516+
attrBehavior = WarnAttr::Behavior::Error;
517+
break;
518+
case WarningGroupBehaviorWarning:
519+
attrBehavior = WarnAttr::Behavior::Warning;
520+
break;
521+
case WarningGroupBehaviorIgnored:
522+
attrBehavior = WarnAttr::Behavior::Ignored;
523+
break;
524+
}
525+
526+
std::optional<StringRef> reasonText = std::nullopt;
527+
if (!reason.getIsEmpty())
528+
reasonText = reason.unbridged();
529+
530+
return new (context) WarnAttr(*diagGroupID, attrBehavior,
531+
reasonText, atLoc, range,
532+
/*Implicit=*/false);
533+
}
534+
503535
static NonSendableKind unbridged(BridgedNonSendableKind kind) {
504536
switch (kind) {
505537
case BridgedNonSendableKindSpecific:

lib/AST/FeatureSet.cpp

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

314+
static bool usesFeatureSourceWarningControl(Decl *decl) {
315+
return decl->getAttrs().hasAttribute<WarnAttr>();
316+
}
317+
314318
static bool usesFeatureCompileTimeValues(Decl *decl) {
315319
return decl->getAttrs().hasAttribute<ConstValAttr>() ||
316320
decl->getAttrs().hasAttribute<ConstInitializedAttr>();

0 commit comments

Comments
 (0)