Skip to content

Commit 188b28b

Browse files
committed
[NFC] Extract a utility routine to parse a UUID string literal.
1 parent 2afb336 commit 188b28b

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

include/swift/Parse/Parser.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ namespace swift {
5252
class SourceManager;
5353
class TupleType;
5454
class TypeLoc;
55+
class UUID;
5556

5657
struct EnumElementInfo;
5758

@@ -1018,6 +1019,11 @@ class Parser {
10181019
parseExtendedAvailabilitySpecList(SourceLoc AtLoc, SourceLoc AttrLoc,
10191020
StringRef AttrName);
10201021

1022+
/// Parse a string literal whose contents can be interpreted as a UUID.
1023+
///
1024+
/// \returns false on success, true on error.
1025+
bool parseUUIDString(UUID &uuid, Diag<> diag);
1026+
10211027
/// Parse the Objective-C selector inside @objc
10221028
void parseObjCSelector(SmallVector<Identifier, 4> &Names,
10231029
SmallVector<SourceLoc, 4> &NameLocs,

lib/Parse/ParseDecl.cpp

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3693,6 +3693,25 @@ bool Parser::parseConventionAttributeInternal(
36933693
return false;
36943694
}
36953695

3696+
bool Parser::parseUUIDString(UUID &uuid, Diag<> diagnostic) {
3697+
if (!Tok.is(tok::string_literal)) {
3698+
diagnose(Tok, diagnostic);
3699+
return true;
3700+
}
3701+
3702+
bool failed = true;
3703+
auto literalText = Tok.getText().slice(1, Tok.getText().size() - 1);
3704+
llvm::SmallString<UUID::StringBufferSize> text(literalText);
3705+
if (auto id = UUID::fromString(text.c_str())) {
3706+
uuid = *id;
3707+
failed = false;
3708+
} else {
3709+
diagnose(Tok, diagnostic);
3710+
}
3711+
consumeToken(tok::string_literal);
3712+
return failed;
3713+
}
3714+
36963715
/// \verbatim
36973716
/// attribute-type:
36983717
/// 'noreturn'
@@ -3882,24 +3901,12 @@ ParserStatus Parser::parseTypeAttribute(TypeAttributes &Attributes,
38823901
// Parse the opened existential ID string in parens
38833902
SourceLoc beginLoc = Tok.getLoc(), idLoc, endLoc;
38843903
if (consumeIfNotAtStartOfLine(tok::l_paren)) {
3885-
if (Tok.is(tok::string_literal)) {
3886-
UUID openedID;
3887-
idLoc = Tok.getLoc();
3888-
auto literalText = Tok.getText().slice(1, Tok.getText().size() - 1);
3889-
llvm::SmallString<UUID::StringBufferSize> text(literalText);
3890-
if (auto openedID = UUID::fromString(text.c_str())) {
3891-
Attributes.OpenedID = openedID;
3892-
} else {
3893-
diagnose(Tok, diag::opened_attribute_id_value);
3894-
}
3895-
consumeToken();
3896-
} else {
3897-
diagnose(Tok, diag::opened_attribute_id_value);
3898-
}
3899-
3900-
if (Tok.is(tok::comma)) {
3901-
consumeToken(tok::comma);
3904+
idLoc = Tok.getLoc();
3905+
UUID id;
3906+
if (!parseUUIDString(id, diag::opened_attribute_id_value))
3907+
Attributes.OpenedID = id;
39023908

3909+
if (consumeIf(tok::comma)) {
39033910
auto constraintType = parseType(diag::expected_type);
39043911
if (constraintType.isNonNull())
39053912
Attributes.ConstraintType = constraintType.getPtrOrNull();

0 commit comments

Comments
 (0)