From fe16aa31fdfaad4c38aed443d853af293714f1c4 Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Fri, 27 Sep 2013 07:04:31 +0000 Subject: [PATCH] AST: Handle qualified array types in typeid() expressions The intent of getTypeOperand() was to yield an unqualified type. However QualType::getUnqualifiedType() does not strip away qualifiers on arrays. N.B. This worked fine when typeid() was applied to an expression because we would inject as implicit cast to the unqualified array type in the AST. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@191487 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/ExprCXX.h | 4 ++-- lib/AST/ExprCXX.cpp | 16 +++++++++------- lib/AST/ItaniumMangle.cpp | 2 +- lib/AST/StmtPrinter.cpp | 4 ++-- lib/AST/StmtProfile.cpp | 4 ++-- lib/CodeGen/CGExprCXX.cpp | 4 ++-- lib/CodeGen/CGExprConstant.cpp | 2 +- test/CodeGenCXX/typeid.cpp | 3 +++ 8 files changed, 22 insertions(+), 17 deletions(-) diff --git a/include/clang/AST/ExprCXX.h b/include/clang/AST/ExprCXX.h index a8949fe18b..731d47f723 100644 --- a/include/clang/AST/ExprCXX.h +++ b/include/clang/AST/ExprCXX.h @@ -566,7 +566,7 @@ public: /// \brief Retrieves the type operand of this typeid() expression after /// various required adjustments (removing reference types, cv-qualifiers). - QualType getTypeOperand() const; + QualType getTypeOperand(ASTContext &Context) const; /// \brief Retrieve source information for the type operand. TypeSourceInfo *getTypeOperandSourceInfo() const { @@ -701,7 +701,7 @@ public: /// \brief Retrieves the type operand of this __uuidof() expression after /// various required adjustments (removing reference types, cv-qualifiers). - QualType getTypeOperand() const; + QualType getTypeOperand(ASTContext &Context) const; /// \brief Retrieve source information for the type operand. TypeSourceInfo *getTypeOperandSourceInfo() const { diff --git a/lib/AST/ExprCXX.cpp b/lib/AST/ExprCXX.cpp index 7478678bf0..c7d2f78104 100644 --- a/lib/AST/ExprCXX.cpp +++ b/lib/AST/ExprCXX.cpp @@ -40,16 +40,18 @@ bool CXXTypeidExpr::isPotentiallyEvaluated() const { return false; } -QualType CXXTypeidExpr::getTypeOperand() const { +QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const { assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)"); - return Operand.get()->getType().getNonReferenceType() - .getUnqualifiedType(); + Qualifiers Quals; + return Context.getUnqualifiedArrayType( + Operand.get()->getType().getNonReferenceType(), Quals); } -QualType CXXUuidofExpr::getTypeOperand() const { +QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const { assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)"); - return Operand.get()->getType().getNonReferenceType() - .getUnqualifiedType(); + Qualifiers Quals; + return Context.getUnqualifiedArrayType( + Operand.get()->getType().getNonReferenceType(), Quals); } // static @@ -118,7 +120,7 @@ UuidAttr *CXXUuidofExpr::GetUuidAttrOfType(QualType QT, StringRef CXXUuidofExpr::getUuidAsStringRef(ASTContext &Context) const { StringRef Uuid; if (isTypeOperand()) - Uuid = CXXUuidofExpr::GetUuidAttrOfType(getTypeOperand())->getGuid(); + Uuid = CXXUuidofExpr::GetUuidAttrOfType(getTypeOperand(Context))->getGuid(); else { // Special case: __uuidof(0) means an all-zero GUID. Expr *Op = getExprOperand(); diff --git a/lib/AST/ItaniumMangle.cpp b/lib/AST/ItaniumMangle.cpp index 3b32bdfb2b..cb3c920bcf 100644 --- a/lib/AST/ItaniumMangle.cpp +++ b/lib/AST/ItaniumMangle.cpp @@ -2879,7 +2879,7 @@ recurse: // ::= te # typeid (expression) if (TIE->isTypeOperand()) { Out << "ti"; - mangleType(TIE->getTypeOperand()); + mangleType(TIE->getTypeOperand(Context.getASTContext())); } else { Out << "te"; mangleExpression(TIE->getExprOperand()); diff --git a/lib/AST/StmtPrinter.cpp b/lib/AST/StmtPrinter.cpp index df8d2bbf09..c94ffab971 100644 --- a/lib/AST/StmtPrinter.cpp +++ b/lib/AST/StmtPrinter.cpp @@ -1307,7 +1307,7 @@ void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) { void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { OS << "typeid("; if (Node->isTypeOperand()) { - Node->getTypeOperand().print(OS, Policy); + Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); } else { PrintExpr(Node->getExprOperand()); } @@ -1317,7 +1317,7 @@ void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) { OS << "__uuidof("; if (Node->isTypeOperand()) { - Node->getTypeOperand().print(OS, Policy); + Node->getTypeOperandSourceInfo()->getType().print(OS, Policy); } else { PrintExpr(Node->getExprOperand()); } diff --git a/lib/AST/StmtProfile.cpp b/lib/AST/StmtProfile.cpp index cde95ee351..7612bef58a 100644 --- a/lib/AST/StmtProfile.cpp +++ b/lib/AST/StmtProfile.cpp @@ -812,13 +812,13 @@ void StmtProfiler::VisitCXXStdInitializerListExpr( void StmtProfiler::VisitCXXTypeidExpr(const CXXTypeidExpr *S) { VisitExpr(S); if (S->isTypeOperand()) - VisitType(S->getTypeOperand()); + VisitType(S->getTypeOperandSourceInfo()->getType()); } void StmtProfiler::VisitCXXUuidofExpr(const CXXUuidofExpr *S) { VisitExpr(S); if (S->isTypeOperand()) - VisitType(S->getTypeOperand()); + VisitType(S->getTypeOperandSourceInfo()->getType()); } void StmtProfiler::VisitMSPropertyRefExpr(const MSPropertyRefExpr *S) { diff --git a/lib/CodeGen/CGExprCXX.cpp b/lib/CodeGen/CGExprCXX.cpp index 53140218a0..9503fafb81 100644 --- a/lib/CodeGen/CGExprCXX.cpp +++ b/lib/CodeGen/CGExprCXX.cpp @@ -1589,8 +1589,8 @@ llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) { ConvertType(E->getType())->getPointerTo(); if (E->isTypeOperand()) { - llvm::Constant *TypeInfo = - CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand()); + llvm::Constant *TypeInfo = + CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand(getContext())); return Builder.CreateBitCast(TypeInfo, StdTypeInfoPtrTy); } diff --git a/lib/CodeGen/CGExprConstant.cpp b/lib/CodeGen/CGExprConstant.cpp index 431ee3c47f..2d3afc96f2 100644 --- a/lib/CodeGen/CGExprConstant.cpp +++ b/lib/CodeGen/CGExprConstant.cpp @@ -966,7 +966,7 @@ public: CXXTypeidExpr *Typeid = cast(E); QualType T; if (Typeid->isTypeOperand()) - T = Typeid->getTypeOperand(); + T = Typeid->getTypeOperand(CGM.getContext()); else T = Typeid->getExprOperand()->getType(); return CGM.GetAddrOfRTTIDescriptor(T); diff --git a/test/CodeGenCXX/typeid.cpp b/test/CodeGenCXX/typeid.cpp index f54b60d0cd..9d212905e6 100644 --- a/test/CodeGenCXX/typeid.cpp +++ b/test/CodeGenCXX/typeid.cpp @@ -27,6 +27,9 @@ extern A &a; // CHECK: @_ZN5Test14a_tiE = global const std::type_info &a_ti = typeid(a); +// CHECK: @_ZN5Test18A10_c_tiE = constant %"class.std::type_info"* bitcast ({ i8*, i8* }* @_ZTIA10_c to %"class.std::type_info"*), align 8 +const std::type_info &A10_c_ti = typeid(char const[10]); + // CHECK-LABEL: define i8* @_ZN5Test11fEv const char *f() { try { -- 2.40.0