From: Bruno Ricci Date: Thu, 11 Jun 2020 16:29:15 +0000 (+0100) Subject: [clang][NFC] Assert that the enumerator value of {Type,ArrayType,UnaryExprOrType... X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=efb0413a5cf9b1481c9b6169c8685f8d71f6de84;p=platform%2Fupstream%2Fllvm.git [clang][NFC] Assert that the enumerator value of {Type,ArrayType,UnaryExprOrType,Expression}Traits is valid and does not overflow in the bit-field for its storage in more places. This is a follow-up to 78e636b3f2f0b0487130b31fade4f95ab179a18c. NFC. --- diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h index 670c0fe..d31f582 100644 --- a/clang/include/clang/AST/Expr.h +++ b/clang/include/clang/AST/Expr.h @@ -2509,7 +2509,11 @@ public: SourceLocation rp) : Expr(UnaryExprOrTypeTraitExprClass, resultType, VK_RValue, OK_Ordinary), OpLoc(op), RParenLoc(rp) { + assert(ExprKind <= UETT_Last && "invalid enum value!"); UnaryExprOrTypeTraitExprBits.Kind = ExprKind; + assert(static_cast(ExprKind) == + UnaryExprOrTypeTraitExprBits.Kind && + "UnaryExprOrTypeTraitExprBits.Kind overflow!"); UnaryExprOrTypeTraitExprBits.IsType = true; Argument.Ty = TInfo; setDependence(computeDependence(this)); @@ -2526,7 +2530,12 @@ public: UnaryExprOrTypeTrait getKind() const { return static_cast(UnaryExprOrTypeTraitExprBits.Kind); } - void setKind(UnaryExprOrTypeTrait K) { UnaryExprOrTypeTraitExprBits.Kind = K;} + void setKind(UnaryExprOrTypeTrait K) { + assert(K <= UETT_Last && "invalid enum value!"); + UnaryExprOrTypeTraitExprBits.Kind = K; + assert(static_cast(K) == UnaryExprOrTypeTraitExprBits.Kind && + "UnaryExprOrTypeTraitExprBits.Kind overflow!"); + } bool isArgumentType() const { return UnaryExprOrTypeTraitExprBits.IsType; } QualType getArgumentType() const { diff --git a/clang/include/clang/AST/ExprCXX.h b/clang/include/clang/AST/ExprCXX.h index 379f762..82036a2 100644 --- a/clang/include/clang/AST/ExprCXX.h +++ b/clang/include/clang/AST/ExprCXX.h @@ -2749,6 +2749,8 @@ public: : Expr(ArrayTypeTraitExprClass, ty, VK_RValue, OK_Ordinary), ATT(att), Value(value), Dimension(dimension), Loc(loc), RParen(rparen), QueriedType(queried) { + assert(att <= ATT_Last && "invalid enum value!"); + assert(static_cast(att) == ATT && "ATT overflow!"); setDependence(computeDependence(this)); } @@ -2813,6 +2815,8 @@ public: : Expr(ExpressionTraitExprClass, resultType, VK_RValue, OK_Ordinary), ET(et), Value(value), Loc(loc), RParen(rparen), QueriedExpression(queried) { + assert(et <= ET_Last && "invalid enum value!"); + assert(static_cast(et) == ET && "ET overflow!"); setDependence(computeDependence(this)); } diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp index 71cc159..89eb8e9 100644 --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -1535,7 +1535,10 @@ UnaryExprOrTypeTraitExpr::UnaryExprOrTypeTraitExpr( SourceLocation op, SourceLocation rp) : Expr(UnaryExprOrTypeTraitExprClass, resultType, VK_RValue, OK_Ordinary), OpLoc(op), RParenLoc(rp) { + assert(ExprKind <= UETT_Last && "invalid enum value!"); UnaryExprOrTypeTraitExprBits.Kind = ExprKind; + assert(static_cast(ExprKind) == UnaryExprOrTypeTraitExprBits.Kind && + "UnaryExprOrTypeTraitExprBits.Kind overflow!"); UnaryExprOrTypeTraitExprBits.IsType = false; Argument.Ex = E; setDependence(computeDependence(this)); diff --git a/clang/lib/AST/ExprCXX.cpp b/clang/lib/AST/ExprCXX.cpp index 9d28555..9d4df28 100644 --- a/clang/lib/AST/ExprCXX.cpp +++ b/clang/lib/AST/ExprCXX.cpp @@ -1579,6 +1579,7 @@ TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind, SourceLocation RParenLoc, bool Value) : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary), Loc(Loc), RParenLoc(RParenLoc) { + assert(Kind <= TT_Last && "invalid enum value!"); TypeTraitExprBits.Kind = Kind; assert(static_cast(Kind) == TypeTraitExprBits.Kind && "TypeTraitExprBits.Kind overflow!");