diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index 6109829cf20a6..0e964b7f13722 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -4471,6 +4471,14 @@ void FieldDecl::setLazyInClassInitializer(LazyDeclStmtPtr NewInit) { unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const { assert(isBitField() && "not a bitfield"); + + // Try to avoid evaluating the expression in the overwhelmingly + // common case that we have a ConstantExpr. + if (const auto *CE = dyn_cast(getBitWidth())) { + assert(CE->hasAPValueResult()); + return CE->getResultAsAPSInt().getZExtValue(); + } + return getBitWidth()->EvaluateKnownConstInt(Ctx).getZExtValue(); }