diff --git a/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp --- a/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp @@ -40,7 +40,7 @@ } } if (const auto *TypeIDExpr = dyn_cast(&Node)) - return !TypeIDExpr->isPotentiallyEvaluated(); + return !TypeIDExpr->isPotentiallyEvaluated(Finder->getASTContext()); return false; } diff --git a/clang/include/clang/AST/EvaluatedExprVisitor.h b/clang/include/clang/AST/EvaluatedExprVisitor.h --- a/clang/include/clang/AST/EvaluatedExprVisitor.h +++ b/clang/include/clang/AST/EvaluatedExprVisitor.h @@ -77,7 +77,7 @@ } void VisitCXXTypeidExpr(PTR(CXXTypeidExpr) E) { - if (E->isPotentiallyEvaluated()) + if (E->isPotentiallyEvaluated(Context)) return this->Visit(E->getExprOperand()); } 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 @@ -814,17 +814,18 @@ private: llvm::PointerUnion Operand; SourceRange Range; + bool isMSABI; public: CXXTypeidExpr(QualType Ty, TypeSourceInfo *Operand, SourceRange R) : Expr(CXXTypeidExprClass, Ty, VK_LValue, OK_Ordinary), Operand(Operand), - Range(R) { + Range(R), isMSABI(false) { setDependence(computeDependence(this)); } CXXTypeidExpr(QualType Ty, Expr *Operand, SourceRange R) : Expr(CXXTypeidExprClass, Ty, VK_LValue, OK_Ordinary), Operand(Operand), - Range(R) { + Range(R), isMSABI(false) { setDependence(computeDependence(this)); } @@ -838,7 +839,7 @@ /// Determine whether this typeid has a type operand which is potentially /// evaluated, per C++11 [expr.typeid]p3. - bool isPotentiallyEvaluated() const; + bool isPotentiallyEvaluated(const ASTContext &Context) const; bool isTypeOperand() const { return Operand.is(); } diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -3477,7 +3477,7 @@ case CXXTypeidExprClass: // typeid might throw if its subexpression is potentially-evaluated, so has // side-effects in that case whether or not its subexpression does. - return cast(this)->isPotentiallyEvaluated(); + return cast(this)->isPotentiallyEvaluated(Ctx); case CXXConstructExprClass: case CXXTemporaryObjectExprClass: { 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 @@ -131,14 +131,20 @@ return Result; } -bool CXXTypeidExpr::isPotentiallyEvaluated() const { +bool CXXTypeidExpr::isPotentiallyEvaluated(const ASTContext &Context) const { if (isTypeOperand()) return false; + Expr *E = getExprOperand()->IgnoreParenNoopCasts(Context); + if (auto *DRE = dyn_cast(E)) { + QualType Ty = DRE->getDecl()->getType(); + if (!Ty->isPointerType() && !Ty->isReferenceType()) + return false; + } + // C++11 [expr.typeid]p3: // When typeid is applied to an expression other than a glvalue of // polymorphic class type, [...] the expression is an unevaluated operand. - const Expr *E = getExprOperand(); if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl()) if (RD->isPolymorphic() && E->isGLValue()) return true; diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -7969,7 +7969,7 @@ bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { TypeInfoLValue TypeInfo; - if (!E->isPotentiallyEvaluated()) { + if (!E->isPotentiallyEvaluated(Info.Ctx)) { if (E->isTypeOperand()) TypeInfo = TypeInfoLValue(E->getTypeOperand(Info.Ctx).getTypePtr()); else diff --git a/clang/lib/Analysis/ExprMutationAnalyzer.cpp b/clang/lib/Analysis/ExprMutationAnalyzer.cpp --- a/clang/lib/Analysis/ExprMutationAnalyzer.cpp +++ b/clang/lib/Analysis/ExprMutationAnalyzer.cpp @@ -40,7 +40,7 @@ cxxTypeidExpr; AST_MATCHER(CXXTypeidExpr, isPotentiallyEvaluated) { - return Node.isPotentiallyEvaluated(); + return Node.isPotentiallyEvaluated(Finder->getASTContext()); } const ast_matchers::internal::VariadicDynCastAllOfMatcherisPotentiallyEvaluated()) + if (E->isPotentiallyEvaluated(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;