Skip to content

Commit fbf60b7

Browse files
committed
Properly compute whether statement expressions can throw, rather than
conservatively assuming they always can. Also fix cases where we would not consider the computation of a VLA type when determining whether an expression can throw. We don't yet properly determine whether a VLA can throw, but no longer incorrectly claim it can never throw.
1 parent 0ec1e99 commit fbf60b7

File tree

5 files changed

+339
-62
lines changed

5 files changed

+339
-62
lines changed

clang/include/clang/AST/Stmt.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,6 +1995,10 @@ class IfStmt final
19951995
bool isConstexpr() const { return IfStmtBits.IsConstexpr; }
19961996
void setConstexpr(bool C) { IfStmtBits.IsConstexpr = C; }
19971997

1998+
/// If this is an 'if constexpr', determine which substatement will be taken.
1999+
/// Otherwise, or if the condition is value-dependent, returns None.
2000+
Optional<const Stmt*> getNondiscardedCase(const ASTContext &Ctx) const;
2001+
19982002
bool isObjCAvailabilityCheck() const;
19992003

20002004
SourceLocation getBeginLoc() const { return getIfLoc(); }

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,7 @@ class Sema final {
15991599
DeclarationNameInfo GetNameFromUnqualifiedId(const UnqualifiedId &Name);
16001600
static QualType GetTypeFromParser(ParsedType Ty,
16011601
TypeSourceInfo **TInfo = nullptr);
1602-
CanThrowResult canThrow(const Expr *E);
1602+
CanThrowResult canThrow(const Stmt *E);
16031603
const FunctionProtoType *ResolveExceptionSpec(SourceLocation Loc,
16041604
const FunctionProtoType *FPT);
16051605
void UpdateExceptionSpec(FunctionDecl *FD,

clang/lib/AST/Stmt.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,12 @@ bool IfStmt::isObjCAvailabilityCheck() const {
908908
return isa<ObjCAvailabilityCheckExpr>(getCond());
909909
}
910910

911+
Optional<const Stmt*> IfStmt::getNondiscardedCase(const ASTContext &Ctx) const {
912+
if (!isConstexpr() || getCond()->isValueDependent())
913+
return None;
914+
return !getCond()->EvaluateKnownConstInt(Ctx) ? getElse() : getThen();
915+
}
916+
911917
ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
912918
Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
913919
SourceLocation RP)

0 commit comments

Comments
 (0)