Skip to content

[Parser][NFC] Move the core parsing of an attribute into a separate method #107300

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 3 commits into from
Sep 6, 2024
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
3 changes: 3 additions & 0 deletions clang/include/clang/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -2943,6 +2943,9 @@ class Parser : public CodeCompletionHandler {
return false;
}

bool ParseSingleGNUAttribute(ParsedAttributes &Attrs, SourceLocation &EndLoc,
LateParsedAttrList *LateAttrs = nullptr,
Declarator *D = nullptr);
void ParseGNUAttributes(ParsedAttributes &Attrs,
LateParsedAttrList *LateAttrs = nullptr,
Declarator *D = nullptr);
Expand Down
139 changes: 82 additions & 57 deletions clang/lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,86 @@ void Parser::ParseAttributes(unsigned WhichAttrKinds, ParsedAttributes &Attrs,
} while (MoreToParse);
}

/// ParseSingleGNUAttribute - Parse a single GNU attribute.
///
/// [GNU] attrib:
/// empty
/// attrib-name
/// attrib-name '(' identifier ')'
/// attrib-name '(' identifier ',' nonempty-expr-list ')'
/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
///
/// [GNU] attrib-name:
/// identifier
/// typespec
/// typequal
/// storageclass
bool Parser::ParseSingleGNUAttribute(ParsedAttributes &Attrs,
SourceLocation &EndLoc,
LateParsedAttrList *LateAttrs,
Declarator *D) {
IdentifierInfo *AttrName = Tok.getIdentifierInfo();
if (!AttrName)
return true;

SourceLocation AttrNameLoc = ConsumeToken();

if (Tok.isNot(tok::l_paren)) {
Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
ParsedAttr::Form::GNU());
return false;
}

bool LateParse = false;
if (!LateAttrs)
LateParse = false;
else if (LateAttrs->lateAttrParseExperimentalExtOnly()) {
// The caller requested that this attribute **only** be late
// parsed for `LateAttrParseExperimentalExt` attributes. This will
// only be late parsed if the experimental language option is enabled.
LateParse = getLangOpts().ExperimentalLateParseAttributes &&
IsAttributeLateParsedExperimentalExt(*AttrName);
} else {
// The caller did not restrict late parsing to only
// `LateAttrParseExperimentalExt` attributes so late parse
// both `LateAttrParseStandard` and `LateAttrParseExperimentalExt`
// attributes.
LateParse = IsAttributeLateParsedExperimentalExt(*AttrName) ||
IsAttributeLateParsedStandard(*AttrName);
}

// Handle "parameterized" attributes
if (!LateParse) {
ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, &EndLoc, nullptr,
SourceLocation(), ParsedAttr::Form::GNU(), D);
return false;
}

// Handle attributes with arguments that require late parsing.
LateParsedAttribute *LA =
new LateParsedAttribute(this, *AttrName, AttrNameLoc);
LateAttrs->push_back(LA);

// Attributes in a class are parsed at the end of the class, along
// with other late-parsed declarations.
if (!ClassStack.empty() && !LateAttrs->parseSoon())
getCurrentClass().LateParsedDeclarations.push_back(LA);

// Be sure ConsumeAndStoreUntil doesn't see the start l_paren, since it
// recursively consumes balanced parens.
LA->Toks.push_back(Tok);
ConsumeParen();
// Consume everything up to and including the matching right parens.
ConsumeAndStoreUntil(tok::r_paren, LA->Toks, /*StopAtSemi=*/true);

Token Eof;
Eof.startToken();
Eof.setLocation(Tok.getLocation());
LA->Toks.push_back(Eof);

return false;
}

/// ParseGNUAttributes - Parse a non-empty attributes list.
///
/// [GNU] attributes:
Expand Down Expand Up @@ -223,64 +303,9 @@ void Parser::ParseGNUAttributes(ParsedAttributes &Attrs,
AttributeCommonInfo::Syntax::AS_GNU);
break;
}
IdentifierInfo *AttrName = Tok.getIdentifierInfo();
if (!AttrName)
break;

SourceLocation AttrNameLoc = ConsumeToken();

if (Tok.isNot(tok::l_paren)) {
Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
ParsedAttr::Form::GNU());
continue;
}

bool LateParse = false;
if (!LateAttrs)
LateParse = false;
else if (LateAttrs->lateAttrParseExperimentalExtOnly()) {
// The caller requested that this attribute **only** be late
// parsed for `LateAttrParseExperimentalExt` attributes. This will
// only be late parsed if the experimental language option is enabled.
LateParse = getLangOpts().ExperimentalLateParseAttributes &&
IsAttributeLateParsedExperimentalExt(*AttrName);
} else {
// The caller did not restrict late parsing to only
// `LateAttrParseExperimentalExt` attributes so late parse
// both `LateAttrParseStandard` and `LateAttrParseExperimentalExt`
// attributes.
LateParse = IsAttributeLateParsedExperimentalExt(*AttrName) ||
IsAttributeLateParsedStandard(*AttrName);
}

// Handle "parameterized" attributes
if (!LateParse) {
ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, &EndLoc, nullptr,
SourceLocation(), ParsedAttr::Form::GNU(), D);
continue;
}

// Handle attributes with arguments that require late parsing.
LateParsedAttribute *LA =
new LateParsedAttribute(this, *AttrName, AttrNameLoc);
LateAttrs->push_back(LA);

// Attributes in a class are parsed at the end of the class, along
// with other late-parsed declarations.
if (!ClassStack.empty() && !LateAttrs->parseSoon())
getCurrentClass().LateParsedDeclarations.push_back(LA);

// Be sure ConsumeAndStoreUntil doesn't see the start l_paren, since it
// recursively consumes balanced parens.
LA->Toks.push_back(Tok);
ConsumeParen();
// Consume everything up to and including the matching right parens.
ConsumeAndStoreUntil(tok::r_paren, LA->Toks, /*StopAtSemi=*/true);

Token Eof;
Eof.startToken();
Eof.setLocation(Tok.getLocation());
LA->Toks.push_back(Eof);
if (ParseSingleGNUAttribute(Attrs, EndLoc, LateAttrs, D))
break;
} while (Tok.is(tok::comma));

if (ExpectAndConsume(tok::r_paren))
Expand Down
Loading