diff --git a/clang/include/clang/AST/ExprCXX.h b/clang/include/clang/AST/ExprCXX.h --- a/clang/include/clang/AST/ExprCXX.h +++ b/clang/include/clang/AST/ExprCXX.h @@ -840,6 +840,8 @@ /// evaluated, per C++11 [expr.typeid]p3. bool isPotentiallyEvaluated() const; + bool isMostDerived(ASTContext &Context) const; + bool isTypeOperand() const { return Operand.is(); } /// Retrieves the type operand of this typeid() expression after diff --git a/clang/lib/AST/ExprCXX.cpp b/clang/lib/AST/ExprCXX.cpp --- a/clang/lib/AST/ExprCXX.cpp +++ b/clang/lib/AST/ExprCXX.cpp @@ -146,6 +146,20 @@ return false; } +bool CXXTypeidExpr::isMostDerived(ASTContext &Context) const { + if (isTypeOperand()) + return false; + + const Expr *E = getExprOperand()->IgnoreParenNoopCasts(Context); + if (auto *DRE = dyn_cast(E)) { + QualType Ty = DRE->getDecl()->getType(); + if (!Ty->isPointerType() && !Ty->isReferenceType()) + return true; + } + + return false; +} + QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const { assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)"); Qualifiers Quals; diff --git a/clang/lib/CodeGen/CGExprCXX.cpp b/clang/lib/CodeGen/CGExprCXX.cpp --- a/clang/lib/CodeGen/CGExprCXX.cpp +++ b/clang/lib/CodeGen/CGExprCXX.cpp @@ -2199,7 +2199,8 @@ // polymorphic class type, the result refers to a std::type_info object // representing the type of the most derived object (that is, the dynamic // type) to which the glvalue refers. - if (E->isPotentiallyEvaluated()) + // If the operand is already most derived object, no need to look up vtable. + if (E->isPotentiallyEvaluated() && !E->isMostDerived(getContext())) return EmitTypeidFromVTable(*this, E->getExprOperand(), StdTypeInfoPtrTy); diff --git a/clang/test/CodeGenCXX/microsoft-abi-typeid.cpp b/clang/test/CodeGenCXX/microsoft-abi-typeid.cpp --- a/clang/test/CodeGenCXX/microsoft-abi-typeid.cpp +++ b/clang/test/CodeGenCXX/microsoft-abi-typeid.cpp @@ -46,9 +46,11 @@ const std::type_info* test5_typeid() { return &typeid(v); } // CHECK: define dso_local %struct.type_info* @"?test5_typeid@@YAPBUtype_info@@XZ"() -// CHECK: [[RT:%.*]] = call i8* @__RTtypeid(i8* bitcast (%struct.V* @"?v@@3UV@@A" to i8*)) -// CHECK-NEXT: [[RET:%.*]] = bitcast i8* [[RT]] to %struct.type_info* -// CHECK-NEXT: ret %struct.type_info* [[RET]] +// CHECK: ret %struct.type_info* bitcast (%rtti.TypeDescriptor7* @"??_R0?AUV@@@8" to %struct.type_info*) + +const std::type_info *test6_typeid() { return &typeid((V &)v); } +// CHECK: define dso_local %struct.type_info* @"?test6_typeid@@YAPBUtype_info@@XZ"() +// CHECK: ret %struct.type_info* bitcast (%rtti.TypeDescriptor7* @"??_R0?AUV@@@8" to %struct.type_info*) namespace PR26329 { struct Polymorphic { diff --git a/clang/test/SemaCXX/constant-expression-cxx2a.cpp b/clang/test/SemaCXX/constant-expression-cxx2a.cpp --- a/clang/test/SemaCXX/constant-expression-cxx2a.cpp +++ b/clang/test/SemaCXX/constant-expression-cxx2a.cpp @@ -309,7 +309,7 @@ static_assert(&B2().ti2 == &typeid(B2)); extern B2 extern_b2; // expected-note@+1 {{typeid applied to object 'extern_b2' whose dynamic type is not constant}} - static_assert(&typeid(extern_b2) == &typeid(B2)); // expected-error {{constant expression}} + static_assert(&typeid(*&extern_b2) == &typeid(B2)); // expected-error {{constant expression}} constexpr B2 b2; constexpr const B &b1 = b2;