Skip to content

Commit 0b38780

Browse files
Merge pull request swiftlang#39350 from nate-chandler/sil/attribute-diagnostics
[SIL/Parser] Emit diagnostics for more bad attrs.
2 parents 9fb54e9 + 7fa7cc9 commit 0b38780

File tree

2 files changed

+22
-29
lines changed

2 files changed

+22
-29
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,12 @@ ERROR(expected_sil_tuple_index,none,
500500
ERROR(invalid_index_subset,none,
501501
"invalid index subset; expected '[SU]+' where 'S' represents set indices "
502502
"and 'U' represents unset indices", ())
503+
ERROR(sil_invalid_attribute_for_instruction,none,
504+
"The attribute '%0' is invalid for the instruction '%1'.",
505+
(StringRef, StringRef))
506+
ERROR(sil_invalid_attribute_for_expected,none,
507+
"Invalid attribute '%0' (expected '%1').",
508+
(StringRef, StringRef))
503509

504510
// SIL Values
505511
ERROR(sil_value_redefinition,none,

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -841,9 +841,13 @@ static bool parseSILOptional(StringRef &Result, SILParser &SP) {
841841
/// Parse an option attribute ('[' Expected ']')?
842842
static bool parseSILOptional(bool &Result, SILParser &SP, StringRef Expected) {
843843
StringRef Optional;
844-
if (parseSILOptional(Optional, SP)) {
845-
if (Optional != Expected)
844+
SourceLoc Loc;
845+
if (parseSILOptional(Optional, Loc, SP)) {
846+
if (Optional != Expected) {
847+
SP.P.diagnose(Loc, diag::sil_invalid_attribute_for_expected, Optional,
848+
Expected);
846849
return true;
850+
}
847851
Result = true;
848852
}
849853
return false;
@@ -3279,14 +3283,8 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
32793283
SourceLoc AddrLoc;
32803284

32813285
bool isLexical = false;
3282-
StringRef attributeName;
3283-
3284-
if (parseSILOptional(attributeName, *this)) {
3285-
if (attributeName.equals("lexical"))
3286-
isLexical = true;
3287-
else
3288-
return true;
3289-
}
3286+
if (parseSILOptional(isLexical, *this, "lexical"))
3287+
return true;
32903288

32913289
if (parseTypedValueRef(Val, AddrLoc, B) ||
32923290
parseSILDebugLocation(InstLoc, B))
@@ -4146,29 +4144,18 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
41464144
bool hasDynamicLifetime = false;
41474145
bool isLexical = false;
41484146

4149-
while (P.consumeIf(tok::l_square)) {
4150-
Identifier ident;
4151-
SourceLoc identLoc;
4152-
if (parseSILIdentifier(ident, identLoc,
4153-
diag::expected_in_attribute_list)) {
4154-
if (P.consumeIf(tok::r_square)) {
4155-
continue;
4156-
} else {
4157-
return true;
4158-
}
4159-
}
4160-
StringRef attr = ident.str();
4161-
4162-
if (attr == "dynamic_lifetime") {
4147+
StringRef attributeName;
4148+
SourceLoc attributeLoc;
4149+
while (parseSILOptional(attributeName, attributeLoc, *this)) {
4150+
if (attributeName == "dynamic_lifetime")
41634151
hasDynamicLifetime = true;
4164-
} else if (attr == "lexical") {
4152+
else if (attributeName == "lexical")
41654153
isLexical = true;
4166-
} else {
4154+
else {
4155+
P.diagnose(attributeLoc, diag::sil_invalid_attribute_for_instruction,
4156+
attributeName, "alloc_stack");
41674157
return true;
41684158
}
4169-
4170-
if (!P.consumeIf(tok::r_square))
4171-
return true;
41724159
}
41734160

41744161
SILType Ty;

0 commit comments

Comments
 (0)