From 9f614790073999c2da69101c77b783a90c1af5ca Mon Sep 17 00:00:00 2001 From: =?utf8?q?Timm=20B=C3=A4der?= Date: Thu, 17 Nov 2022 12:12:11 +0100 Subject: [PATCH] [clang][Parser][NFC] Simplify ParseParenExprOrCondition Differential Revision: https://reviews.llvm.org/D138194 --- clang/include/clang/Parse/Parser.h | 4 ++-- clang/lib/Parse/ParseStmt.cpp | 33 +++++++++++++-------------------- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h index 22d68e0..997a649 100644 --- a/clang/include/clang/Parse/Parser.h +++ b/clang/include/clang/Parse/Parser.h @@ -2109,8 +2109,8 @@ private: bool ParseParenExprOrCondition(StmtResult *InitStmt, Sema::ConditionResult &CondResult, SourceLocation Loc, Sema::ConditionKind CK, - bool MissingOK, SourceLocation *LParenLoc, - SourceLocation *RParenLoc); + SourceLocation &LParenLoc, + SourceLocation &RParenLoc); StmtResult ParseIfStatement(SourceLocation *TrailingElseLoc); StmtResult ParseSwitchStatement(SourceLocation *TrailingElseLoc); StmtResult ParseWhileStatement(SourceLocation *TrailingElseLoc); diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp index 348a07f..8610abb 100644 --- a/clang/lib/Parse/ParseStmt.cpp +++ b/clang/lib/Parse/ParseStmt.cpp @@ -1270,20 +1270,20 @@ StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) { /// should try to recover harder. It returns false if the condition is /// successfully parsed. Note that a successful parse can still have semantic /// errors in the condition. -/// Additionally, if LParenLoc and RParenLoc are non-null, it will assign -/// the location of the outer-most '(' and ')', respectively, to them. +/// Additionally, it will assign the location of the outer-most '(' and ')', +/// to LParenLoc and RParenLoc, respectively. bool Parser::ParseParenExprOrCondition(StmtResult *InitStmt, Sema::ConditionResult &Cond, SourceLocation Loc, - Sema::ConditionKind CK, bool MissingOK, - SourceLocation *LParenLoc, - SourceLocation *RParenLoc) { + Sema::ConditionKind CK, + SourceLocation &LParenLoc, + SourceLocation &RParenLoc) { BalancedDelimiterTracker T(*this, tok::l_paren); T.consumeOpen(); SourceLocation Start = Tok.getLocation(); if (getLangOpts().CPlusPlus) { - Cond = ParseCXXCondition(InitStmt, Loc, CK, MissingOK); + Cond = ParseCXXCondition(InitStmt, Loc, CK, false); } else { ExprResult CondExpr = ParseExpression(); @@ -1292,7 +1292,7 @@ bool Parser::ParseParenExprOrCondition(StmtResult *InitStmt, Cond = Sema::ConditionError(); else Cond = Actions.ActOnCondition(getCurScope(), Loc, CondExpr.get(), CK, - MissingOK); + /*MissingOK=*/false); } // If the parser was confused by the condition and we don't have a ')', try to @@ -1312,18 +1312,13 @@ bool Parser::ParseParenExprOrCondition(StmtResult *InitStmt, Actions.PreferredConditionType(CK)); if (!CondExpr.isInvalid()) Cond = Actions.ActOnCondition(getCurScope(), Loc, CondExpr.get(), CK, - MissingOK); + /*MissingOK=*/false); } // Either the condition is valid or the rparen is present. T.consumeClose(); - - if (LParenLoc != nullptr) { - *LParenLoc = T.getOpenLocation(); - } - if (RParenLoc != nullptr) { - *RParenLoc = T.getCloseLocation(); - } + LParenLoc = T.getOpenLocation(); + RParenLoc = T.getCloseLocation(); // Check for extraneous ')'s to catch things like "if (foo())) {". We know // that all callers are looking for a statement after the condition, so ")" @@ -1499,7 +1494,7 @@ StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) { if (ParseParenExprOrCondition(&InitStmt, Cond, IfLoc, IsConstexpr ? Sema::ConditionKind::ConstexprIf : Sema::ConditionKind::Boolean, - /*MissingOK=*/false, &LParen, &RParen)) + LParen, RParen)) return StmtError(); if (IsConstexpr) @@ -1694,8 +1689,7 @@ StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) { SourceLocation LParen; SourceLocation RParen; if (ParseParenExprOrCondition(&InitStmt, Cond, SwitchLoc, - Sema::ConditionKind::Switch, - /*MissingOK=*/false, &LParen, &RParen)) + Sema::ConditionKind::Switch, LParen, RParen)) return StmtError(); StmtResult Switch = Actions.ActOnStartOfSwitchStmt( @@ -1785,8 +1779,7 @@ StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) { SourceLocation LParen; SourceLocation RParen; if (ParseParenExprOrCondition(nullptr, Cond, WhileLoc, - Sema::ConditionKind::Boolean, - /*MissingOK=*/false, &LParen, &RParen)) + Sema::ConditionKind::Boolean, LParen, RParen)) return StmtError(); // C99 6.8.5p5 - In C99, the body of the while statement is a scope, even if -- 2.7.4