Skip to content

Parse bounds declarations for variable declarations. #18

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 2 commits into from
Jul 15, 2016
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
26 changes: 25 additions & 1 deletion lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1622,8 +1622,13 @@ bool Parser::MightBeDeclarator(unsigned Context) {
// At namespace scope, 'identifier:' is probably a typo for 'identifier::'
// and in block scope it's probably a label. Inside a class definition,
// this is a bit-field.
//
// For Checked C 'identifier:' is a valid start to a declarator because
// it may be followed by a bounds expression declaring the bounds of
// identifier.
return Context == Declarator::MemberContext ||
(getLangOpts().CPlusPlus && Context == Declarator::FileContext);
(getLangOpts().CPlusPlus && Context == Declarator::FileContext) ||
getLangOpts().CheckedC;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CheckedC [](start = 27, length = 8)

For posterity, it might be helpful to update the comment above to explain why there's a special-case here for Checked C. (I assume it's for the ':' bounds expr syntax for namespace-scope variable declarations.)


case tok::identifier: // Possible virt-specifier.
return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Expand Down Expand Up @@ -1936,6 +1941,8 @@ bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
/// [C++] declarator initializer[opt]
/// [Checked C] declarator ':' bounds-expression
/// [Checked C] declarator ':' bounds-expression '=' initializer
///
/// [C++] initializer:
/// [C++] '=' initializer-clause
Expand Down Expand Up @@ -2018,6 +2025,23 @@ Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(

bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();

// Parse the optional Checked C bounds expression.
if (getLangOpts().CheckedC && Tok.is(tok::colon)) {
ConsumeToken();
ExprResult Bounds = ParseBoundsExpression();
if (Bounds.isInvalid())
SkipUntil(tok::comma, tok::equal, StopAtSemi | StopBeforeMatch);

VarDecl *ThisVarDecl = dyn_cast<VarDecl>(ThisDecl);
if (ThisVarDecl) {
if (Bounds.isInvalid())
Actions.ActOnInvalidBoundsExpr(ThisVarDecl);
else
Actions.ActOnBoundsExpr(ThisVarDecl, cast<BoundsExpr>(Bounds.get()));
} else
llvm_unreachable("Unexpected decl type");
}

// Parse declarator '=' initializer.
// If a '==' or '+=' is found, suggest a fixit to '='.
if (isTokenEqualOrEqualTypo()) {
Expand Down
35 changes: 21 additions & 14 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2794,20 +2794,20 @@ ExprResult Parser::ParseBoundsExpression() {
// Look for e1 "," e2
ExprResult LowerBound =
Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
if (LowerBound.isInvalid())
Result = ExprError();
else if (ExpectAndConsume(tok::comma))
Result = ExprError();
else {
ExprResult UpperBound =
Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
if (UpperBound.isInvalid())
Result = ExprError();
else
Result = Actions.ActOnRangeBoundsExpr(BoundsKWLoc, LowerBound.get(),
UpperBound.get(),
Tok.getLocation());
}

if (ExpectAndConsume(tok::comma))
// We didn't find a comma, so don't try to parse the upper bounds expression.
Result = ExprError();
else {
ExprResult UpperBound =
Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
if (LowerBound.isInvalid() || UpperBound.isInvalid())
Result = ExprError();
else
Result = Actions.ActOnRangeBoundsExpr(BoundsKWLoc, LowerBound.get(),
UpperBound.get(),
Tok.getLocation());
}
} // if (!FoundNullaryOperator)
} else {
// The identifier is not a valid contextual keyword for the start of a
Expand All @@ -2817,6 +2817,13 @@ ExprResult Parser::ParseBoundsExpression() {
Result = ExprError();
}

// Result could be invalid because of a syntax error or a semantic checking
// error. We don't know which. Skip tokens until a right paren is found.
// If this was only a semantic checking error, the input will already be at
// a right paren, so skipping will be do nothing.
if (Result.isInvalid())
SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);

PT.consumeClose();
return Result;
}
Expand Down