From 154fe9812faddcd94568a64aee5f3cb0d47003d9 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Wed, 23 Dec 2009 22:04:40 +0000 Subject: [PATCH] There is no such thing as typeinfo for a cv-qualified type. Assert that this is true when mangling, then fix up the various places in Sema and/or CodeGen that need to remove qualifiers. Addresses a linking issue when building LLVM with Clang. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@92064 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGException.cpp | 9 +++++++-- lib/CodeGen/CGExprCXX.cpp | 6 ++++-- lib/CodeGen/Mangle.cpp | 1 + lib/Sema/SemaExprCXX.cpp | 11 +++++++++-- test/CodeGenCXX/try-catch.cpp | 13 +++++++++++++ 5 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 test/CodeGenCXX/try-catch.cpp diff --git a/lib/CodeGen/CGException.cpp b/lib/CodeGen/CGException.cpp index caf0668d64..30499157f1 100644 --- a/lib/CodeGen/CGException.cpp +++ b/lib/CodeGen/CGException.cpp @@ -367,7 +367,9 @@ void CodeGenFunction::EmitStartEHSpec(const Decl *D) { for (unsigned i = 0; i < Proto->getNumExceptions(); ++i) { QualType Ty = Proto->getExceptionType(i); - llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(Ty.getNonReferenceType()); + QualType ExceptType + = Ty.getNonReferenceType().getUnqualifiedType(); + llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType); SelectorArgs.push_back(EHType); } if (Proto->getNumExceptions()) @@ -506,8 +508,11 @@ void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) { const CXXCatchStmt *C = S.getHandler(i); VarDecl *CatchParam = C->getExceptionDecl(); if (CatchParam) { + // C++ [except.handle]p3 indicates that top-level cv-qualifiers + // are ignored. + QualType CaughtType = C->getCaughtType().getNonReferenceType(); llvm::Value *EHTypeInfo - = CGM.GetAddrOfRTTIDescriptor(C->getCaughtType().getNonReferenceType()); + = CGM.GetAddrOfRTTIDescriptor(CaughtType.getUnqualifiedType()); SelectorArgs.push_back(EHTypeInfo); } else { // null indicates catch all diff --git a/lib/CodeGen/CGExprCXX.cpp b/lib/CodeGen/CGExprCXX.cpp index bfdff985cf..54e6b0141f 100644 --- a/lib/CodeGen/CGExprCXX.cpp +++ b/lib/CodeGen/CGExprCXX.cpp @@ -548,8 +548,10 @@ llvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *V, assert(SrcTy->isRecordType() && "Src type must be record type!"); assert(DestTy->isRecordType() && "Dest type must be record type!"); - llvm::Value *SrcArg = CGM.GetAddrOfRTTIDescriptor(SrcTy); - llvm::Value *DestArg = CGM.GetAddrOfRTTIDescriptor(DestTy); + llvm::Value *SrcArg + = CGM.GetAddrOfRTTIDescriptor(SrcTy.getUnqualifiedType()); + llvm::Value *DestArg + = CGM.GetAddrOfRTTIDescriptor(DestTy.getUnqualifiedType()); V = Builder.CreateBitCast(V, PtrToInt8Ty); V = Builder.CreateCall4(CGM.CreateRuntimeFunction(FTy, "__dynamic_cast"), diff --git a/lib/CodeGen/Mangle.cpp b/lib/CodeGen/Mangle.cpp index 687ff3e618..367c868ba9 100644 --- a/lib/CodeGen/Mangle.cpp +++ b/lib/CodeGen/Mangle.cpp @@ -1586,6 +1586,7 @@ void MangleContext::mangleCXXCtorVtable(const CXXRecordDecl *RD, int64_t Offset, void MangleContext::mangleCXXRTTI(QualType Ty, llvm::SmallVectorImpl &Res) { // ::= TI # typeinfo structure + assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers"); CXXNameMangler Mangler(*this, Res); Mangler.getStream() << "_ZTI"; Mangler.mangleType(Ty); diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp index afb5c54d65..06f8e7aed8 100644 --- a/lib/Sema/SemaExprCXX.cpp +++ b/lib/Sema/SemaExprCXX.cpp @@ -137,8 +137,15 @@ Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprArg E) { /// CheckCXXThrowOperand - Validate the operand of a throw. bool Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *&E) { // C++ [except.throw]p3: - // [...] adjusting the type from "array of T" or "function returning T" - // to "pointer to T" or "pointer to function returning T", [...] + // A throw-expression initializes a temporary object, called the exception + // object, the type of which is determined by removing any top-level + // cv-qualifiers from the static type of the operand of throw and adjusting + // the type from "array of T" or "function returning T" to "pointer to T" + // or "pointer to function returning T", [...] + if (E->getType().hasQualifiers()) + ImpCastExprToType(E, E->getType().getUnqualifiedType(), CastExpr::CK_NoOp, + E->isLvalue(Context) == Expr::LV_Valid); + DefaultFunctionArrayConversion(E); // If the type of the exception would be an incomplete type or a pointer diff --git a/test/CodeGenCXX/try-catch.cpp b/test/CodeGenCXX/try-catch.cpp new file mode 100644 index 0000000000..2b5f3232d1 --- /dev/null +++ b/test/CodeGenCXX/try-catch.cpp @@ -0,0 +1,13 @@ +// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o - -fexceptions | FileCheck %s + +struct X { }; + +const X g(); + +void f() { + try { + throw g(); + // CHECK: @_ZTI1X to i8 + } catch (const X x) { + } +}