Skip to content

[SIL/Parser] Emit diagnostics for more bad attrs. #39350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,12 @@ ERROR(expected_sil_tuple_index,none,
ERROR(invalid_index_subset,none,
"invalid index subset; expected '[SU]+' where 'S' represents set indices "
"and 'U' represents unset indices", ())
ERROR(sil_invalid_attribute_for_instruction,none,
"The attribute '%0' is invalid for the instruction '%1'.",
(StringRef, StringRef))
ERROR(sil_invalid_attribute_for_expected,none,
"Invalid attribute '%0' (expected '%1').",
(StringRef, StringRef))

// SIL Values
ERROR(sil_value_redefinition,none,
Expand Down
45 changes: 16 additions & 29 deletions lib/SIL/Parser/ParseSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -841,9 +841,13 @@ static bool parseSILOptional(StringRef &Result, SILParser &SP) {
/// Parse an option attribute ('[' Expected ']')?
static bool parseSILOptional(bool &Result, SILParser &SP, StringRef Expected) {
StringRef Optional;
if (parseSILOptional(Optional, SP)) {
if (Optional != Expected)
SourceLoc Loc;
if (parseSILOptional(Optional, Loc, SP)) {
if (Optional != Expected) {
SP.P.diagnose(Loc, diag::sil_invalid_attribute_for_expected, Optional,
Expected);
return true;
}
Result = true;
}
return false;
Expand Down Expand Up @@ -3279,14 +3283,8 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
SourceLoc AddrLoc;

bool isLexical = false;
StringRef attributeName;

if (parseSILOptional(attributeName, *this)) {
if (attributeName.equals("lexical"))
isLexical = true;
else
return true;
}
if (parseSILOptional(isLexical, *this, "lexical"))
return true;

if (parseTypedValueRef(Val, AddrLoc, B) ||
parseSILDebugLocation(InstLoc, B))
Expand Down Expand Up @@ -4146,29 +4144,18 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
bool hasDynamicLifetime = false;
bool isLexical = false;

while (P.consumeIf(tok::l_square)) {
Identifier ident;
SourceLoc identLoc;
if (parseSILIdentifier(ident, identLoc,
diag::expected_in_attribute_list)) {
if (P.consumeIf(tok::r_square)) {
continue;
} else {
return true;
}
}
StringRef attr = ident.str();

if (attr == "dynamic_lifetime") {
StringRef attributeName;
SourceLoc attributeLoc;
while (parseSILOptional(attributeName, attributeLoc, *this)) {
if (attributeName == "dynamic_lifetime")
hasDynamicLifetime = true;
} else if (attr == "lexical") {
else if (attributeName == "lexical")
isLexical = true;
} else {
else {
P.diagnose(attributeLoc, diag::sil_invalid_attribute_for_instruction,
attributeName, "alloc_stack");
return true;
}

if (!P.consumeIf(tok::r_square))
return true;
}

SILType Ty;
Expand Down