Skip to content

Commit f65ff0e

Browse files
committed
Address code review feedback and improve an error message.
This change addresses code review feedback to rename some variables and add a comment. It also improves the error message for bounds(a,b) when a and b are omitted. We would get two error messages at the same location, one saying that an expression was expected and the other saying that a ',' was expected. The improvement is to print the error message for the ',' only if nothing went wrong trying to parse the lower bound.
1 parent d85873e commit f65ff0e

File tree

6 files changed

+20
-11
lines changed

6 files changed

+20
-11
lines changed

include/clang/Sema/DeclSpec.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,7 +1288,7 @@ struct DeclaratorChunk {
12881288
unsigned ExceptionSpecLocEnd;
12891289

12901290
/// The location of the ':' for the return bounds expression in the source
1291-
unsigned ReturnBoundsColon;
1291+
unsigned ReturnBoundsColonLoc;
12921292

12931293
/// Params - This is a pointer to a new[]'d array of ParamInfo objects that
12941294
/// describe the parameters specified by this function declarator. null if
@@ -1359,8 +1359,8 @@ struct DeclaratorChunk {
13591359
return SourceLocation::getFromRawEncoding(RParenLoc);
13601360
}
13611361

1362-
SourceLocation getReturnBoundsColon() const {
1363-
return SourceLocation::getFromRawEncoding(ReturnBoundsColon);
1362+
SourceLocation getReturnBoundsColonLoc() const {
1363+
return SourceLocation::getFromRawEncoding(ReturnBoundsColonLoc);
13641364
}
13651365

13661366
SourceLocation getExceptionSpecLocBeg() const {

lib/Parse/ParseDecl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5747,6 +5747,8 @@ void Parser::ParseFunctionDeclarator(Declarator &D,
57475747
}
57485748
}
57495749

5750+
// Parse optional Checked C bounds expression with the form:
5751+
// ':' bounds-expression.
57505752
if (getLangOpts().CheckedC && Tok.is(tok::colon)) {
57515753
BoundsColonLoc = Tok.getLocation();
57525754
ConsumeToken();

lib/Parse/ParseExpr.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2795,10 +2795,8 @@ ExprResult Parser::ParseBoundsExpression() {
27952795
ExprResult LowerBound =
27962796
Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
27972797

2798-
if (ExpectAndConsume(tok::comma))
2799-
// We didn't find a comma, so don't try to parse the upper bounds expression.
2800-
Result = ExprError();
2801-
else {
2798+
if (Tok.getKind() == tok::comma) {
2799+
ConsumeToken();
28022800
ExprResult UpperBound =
28032801
Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
28042802
if (LowerBound.isInvalid() || UpperBound.isInvalid())
@@ -2807,6 +2805,15 @@ ExprResult Parser::ParseBoundsExpression() {
28072805
Result = Actions.ActOnRangeBoundsExpr(BoundsKWLoc, LowerBound.get(),
28082806
UpperBound.get(),
28092807
Tok.getLocation());
2808+
} else {
2809+
// We didn't find a comma. Only issue an error message if the
2810+
// LowerBound expression is valid. Otherwise, we already issued an
2811+
// error message when parsing the lower bound. Emitting an error
2812+
// message here could be spurious or confusing.
2813+
if (!LowerBound.isInvalid()) {
2814+
Diag(Tok, diag::err_expected) << tok::comma;
2815+
}
2816+
Result = ExprError();
28102817
}
28112818
} // if (!FoundNullaryOperator)
28122819
} else {

lib/Parse/ParseExprCXX.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@ ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
12221222
NoexceptExpr.get() : nullptr,
12231223
/*ExceptionSpecTokens*/nullptr,
12241224
LParenLoc, FunLocalRangeEnd,
1225-
/*ReturnBoundsColon=*/NoLoc,
1225+
/*ReturnBoundsColonLoc=*/NoLoc,
12261226
/*ReturnBoundsExpr=*/nullptr,
12271227
D,
12281228
TrailingReturnType),
@@ -1294,7 +1294,7 @@ ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
12941294
/*NoexceptExpr=*/nullptr,
12951295
/*ExceptionSpecTokens=*/nullptr,
12961296
DeclLoc, DeclEndLoc,
1297-
/*ReturnBoundsColon=*/NoLoc,
1297+
/*ReturnBoundsColonLoc=*/NoLoc,
12981298
/*ReturnBoundsExpr=*/nullptr,
12991299
D,
13001300
TrailingReturnType),

lib/Sema/DeclSpec.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto,
212212
I.Fun.HasTrailingReturnType = TrailingReturnType.isUsable() ||
213213
TrailingReturnType.isInvalid();
214214
I.Fun.TrailingReturnType = TrailingReturnType.get();
215-
I.Fun.ReturnBoundsColon = ReturnBoundsColonLoc.getRawEncoding();
215+
I.Fun.ReturnBoundsColonLoc = ReturnBoundsColonLoc.getRawEncoding();
216216
I.Fun.ReturnBounds = ReturnBoundsExpr;
217217

218218
assert(I.Fun.TypeQuals == TypeQuals && "bitfield overflow");

lib/Sema/SemaDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11628,7 +11628,7 @@ NamedDecl *Sema::ImplicitlyDefineFunction(SourceLocation Loc,
1162811628
/*NoexceptExpr=*/nullptr,
1162911629
/*ExceptionSpecTokens=*/nullptr,
1163011630
Loc, Loc,
11631-
/*ReturnBoundsColon=*/NoLoc,
11631+
/*ReturnBoundsColonLoc=*/NoLoc,
1163211632
/*ReturnBoundsExpr=*/nullptr, D),
1163311633
DS.getAttributes(),
1163411634
SourceLocation());

0 commit comments

Comments
 (0)