Skip to content

Commit f2e9c2d

Browse files
committed
[NFC] Extract a utility routine to parse a UUID string literal.
1 parent 74c1431 commit f2e9c2d

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
@@ -3651,6 +3651,25 @@ bool Parser::parseConventionAttributeInternal(
36513651
return false;
36523652
}
36533653

3654+
bool Parser::parseUUIDString(UUID &uuid, Diag<> diagnostic) {
3655+
if (!Tok.is(tok::string_literal)) {
3656+
diagnose(Tok, diagnostic);
3657+
return true;
3658+
}
3659+
3660+
bool failed = true;
3661+
auto literalText = Tok.getText().slice(1, Tok.getText().size() - 1);
3662+
llvm::SmallString<UUID::StringBufferSize> text(literalText);
3663+
if (auto id = UUID::fromString(text.c_str())) {
3664+
uuid = *id;
3665+
failed = false;
3666+
} else {
3667+
diagnose(Tok, diagnostic);
3668+
}
3669+
consumeToken(tok::string_literal);
3670+
return failed;
3671+
}
3672+
36543673
/// \verbatim
36553674
/// attribute-type:
36563675
/// 'noreturn'
@@ -3840,24 +3859,12 @@ ParserStatus Parser::parseTypeAttribute(TypeAttributes &Attributes,
38403859
// Parse the opened existential ID string in parens
38413860
SourceLoc beginLoc = Tok.getLoc(), idLoc, endLoc;
38423861
if (consumeIfNotAtStartOfLine(tok::l_paren)) {
3843-
if (Tok.is(tok::string_literal)) {
3844-
UUID openedID;
3845-
idLoc = Tok.getLoc();
3846-
auto literalText = Tok.getText().slice(1, Tok.getText().size() - 1);
3847-
llvm::SmallString<UUID::StringBufferSize> text(literalText);
3848-
if (auto openedID = UUID::fromString(text.c_str())) {
3849-
Attributes.OpenedID = openedID;
3850-
} else {
3851-
diagnose(Tok, diag::opened_attribute_id_value);
3852-
}
3853-
consumeToken();
3854-
} else {
3855-
diagnose(Tok, diag::opened_attribute_id_value);
3856-
}
3857-
3858-
if (Tok.is(tok::comma)) {
3859-
consumeToken(tok::comma);
3862+
idLoc = Tok.getLoc();
3863+
UUID id;
3864+
if (!parseUUIDString(id, diag::opened_attribute_id_value))
3865+
Attributes.OpenedID = id;
38603866

3867+
if (consumeIf(tok::comma)) {
38613868
auto constraintType = parseType(diag::expected_type);
38623869
if (constraintType.isNonNull())
38633870
Attributes.ConstraintType = constraintType.getPtrOrNull();

0 commit comments

Comments
 (0)