2009-10-11 13:03:14 +04:00
|
|
|
//===--- SemaExceptionSpec.cpp - C++ Exception Specifications ---*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file provides Sema routines for C++ exception specification testing.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Sema.h"
|
|
|
|
#include "clang/AST/CXXInheritance.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/ExprCXX.h"
|
2010-03-24 10:14:45 +03:00
|
|
|
#include "clang/AST/TypeLoc.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2010-02-12 10:32:17 +03:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2009-10-11 13:03:14 +04:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
|
|
|
|
static const FunctionProtoType *GetUnderlyingFunction(QualType T)
|
|
|
|
{
|
|
|
|
if (const PointerType *PtrTy = T->getAs<PointerType>())
|
|
|
|
T = PtrTy->getPointeeType();
|
|
|
|
else if (const ReferenceType *RefTy = T->getAs<ReferenceType>())
|
|
|
|
T = RefTy->getPointeeType();
|
2009-10-14 18:38:54 +04:00
|
|
|
else if (const MemberPointerType *MPTy = T->getAs<MemberPointerType>())
|
|
|
|
T = MPTy->getPointeeType();
|
2009-10-11 13:03:14 +04:00
|
|
|
return T->getAs<FunctionProtoType>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CheckSpecifiedExceptionType - Check if the given type is valid in an
|
|
|
|
/// exception specification. Incomplete types, or pointers to incomplete types
|
|
|
|
/// other than void are not allowed.
|
|
|
|
bool Sema::CheckSpecifiedExceptionType(QualType T, const SourceRange &Range) {
|
|
|
|
|
2009-12-10 21:13:52 +03:00
|
|
|
// This check (and the similar one below) deals with issue 437, that changes
|
|
|
|
// C++ 9.2p2 this way:
|
|
|
|
// Within the class member-specification, the class is regarded as complete
|
|
|
|
// within function bodies, default arguments, exception-specifications, and
|
|
|
|
// constructor ctor-initializers (including such things in nested classes).
|
|
|
|
if (T->isRecordType() && T->getAs<RecordType>()->isBeingDefined())
|
|
|
|
return false;
|
|
|
|
|
2009-10-11 13:03:14 +04:00
|
|
|
// C++ 15.4p2: A type denoted in an exception-specification shall not denote
|
|
|
|
// an incomplete type.
|
2009-10-14 18:59:48 +04:00
|
|
|
if (RequireCompleteType(Range.getBegin(), T,
|
|
|
|
PDiag(diag::err_incomplete_in_exception_spec) << /*direct*/0 << Range))
|
|
|
|
return true;
|
2009-10-11 13:03:14 +04:00
|
|
|
|
|
|
|
// C++ 15.4p2: A type denoted in an exception-specification shall not denote
|
|
|
|
// an incomplete type a pointer or reference to an incomplete type, other
|
|
|
|
// than (cv) void*.
|
|
|
|
int kind;
|
|
|
|
if (const PointerType* IT = T->getAs<PointerType>()) {
|
|
|
|
T = IT->getPointeeType();
|
|
|
|
kind = 1;
|
|
|
|
} else if (const ReferenceType* IT = T->getAs<ReferenceType>()) {
|
|
|
|
T = IT->getPointeeType();
|
|
|
|
kind = 2;
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
|
2009-12-10 21:13:52 +03:00
|
|
|
// Again as before
|
|
|
|
if (T->isRecordType() && T->getAs<RecordType>()->isBeingDefined())
|
|
|
|
return false;
|
|
|
|
|
2009-10-14 18:59:48 +04:00
|
|
|
if (!T->isVoidType() && RequireCompleteType(Range.getBegin(), T,
|
2009-12-10 21:13:52 +03:00
|
|
|
PDiag(diag::err_incomplete_in_exception_spec) << kind << Range))
|
2009-10-14 18:59:48 +04:00
|
|
|
return true;
|
2009-10-11 13:03:14 +04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer
|
|
|
|
/// to member to a function with an exception specification. This means that
|
|
|
|
/// it is invalid to add another level of indirection.
|
|
|
|
bool Sema::CheckDistantExceptionSpec(QualType T) {
|
|
|
|
if (const PointerType *PT = T->getAs<PointerType>())
|
|
|
|
T = PT->getPointeeType();
|
|
|
|
else if (const MemberPointerType *PT = T->getAs<MemberPointerType>())
|
|
|
|
T = PT->getPointeeType();
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const FunctionProtoType *FnT = T->getAs<FunctionProtoType>();
|
|
|
|
if (!FnT)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return FnT->hasExceptionSpec();
|
|
|
|
}
|
|
|
|
|
2010-02-12 10:32:17 +03:00
|
|
|
bool Sema::CheckEquivalentExceptionSpec(FunctionDecl *Old, FunctionDecl *New) {
|
2010-03-24 10:14:45 +03:00
|
|
|
bool MissingExceptionSpecification = false;
|
2010-02-12 10:32:17 +03:00
|
|
|
bool MissingEmptyExceptionSpecification = false;
|
2010-03-30 03:34:08 +04:00
|
|
|
if (!CheckEquivalentExceptionSpec(PDiag(diag::err_mismatched_exception_spec),
|
|
|
|
PDiag(diag::note_previous_declaration),
|
2010-02-12 10:32:17 +03:00
|
|
|
Old->getType()->getAs<FunctionProtoType>(),
|
|
|
|
Old->getLocation(),
|
|
|
|
New->getType()->getAs<FunctionProtoType>(),
|
|
|
|
New->getLocation(),
|
2010-03-24 10:14:45 +03:00
|
|
|
&MissingExceptionSpecification,
|
2010-02-12 10:32:17 +03:00
|
|
|
&MissingEmptyExceptionSpecification))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// The failure was something other than an empty exception
|
|
|
|
// specification; return an error.
|
2010-03-24 10:14:45 +03:00
|
|
|
if (!MissingExceptionSpecification && !MissingEmptyExceptionSpecification)
|
2010-02-12 10:32:17 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// The new function declaration is only missing an empty exception
|
|
|
|
// specification "throw()". If the throw() specification came from a
|
|
|
|
// function in a system header that has C linkage, just add an empty
|
|
|
|
// exception specification to the "new" declaration. This is an
|
|
|
|
// egregious workaround for glibc, which adds throw() specifications
|
|
|
|
// to many libc functions as an optimization. Unfortunately, that
|
|
|
|
// optimization isn't permitted by the C++ standard, so we're forced
|
|
|
|
// to work around it here.
|
2010-03-24 10:14:45 +03:00
|
|
|
if (MissingEmptyExceptionSpecification &&
|
|
|
|
isa<FunctionProtoType>(New->getType()) &&
|
|
|
|
(Old->getLocation().isInvalid() ||
|
|
|
|
Context.getSourceManager().isInSystemHeader(Old->getLocation())) &&
|
2010-02-12 10:32:17 +03:00
|
|
|
Old->isExternC()) {
|
|
|
|
const FunctionProtoType *NewProto
|
|
|
|
= cast<FunctionProtoType>(New->getType());
|
|
|
|
QualType NewType = Context.getFunctionType(NewProto->getResultType(),
|
|
|
|
NewProto->arg_type_begin(),
|
|
|
|
NewProto->getNumArgs(),
|
|
|
|
NewProto->isVariadic(),
|
|
|
|
NewProto->getTypeQuals(),
|
|
|
|
true, false, 0, 0,
|
2010-03-31 00:24:48 +04:00
|
|
|
NewProto->getExtInfo());
|
2010-02-12 10:32:17 +03:00
|
|
|
New->setType(NewType);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-24 10:14:45 +03:00
|
|
|
if (MissingExceptionSpecification && isa<FunctionProtoType>(New->getType())) {
|
|
|
|
const FunctionProtoType *NewProto
|
|
|
|
= cast<FunctionProtoType>(New->getType());
|
|
|
|
const FunctionProtoType *OldProto
|
|
|
|
= Old->getType()->getAs<FunctionProtoType>();
|
|
|
|
|
|
|
|
// Update the type of the function with the appropriate exception
|
|
|
|
// specification.
|
|
|
|
QualType NewType = Context.getFunctionType(NewProto->getResultType(),
|
|
|
|
NewProto->arg_type_begin(),
|
|
|
|
NewProto->getNumArgs(),
|
|
|
|
NewProto->isVariadic(),
|
|
|
|
NewProto->getTypeQuals(),
|
|
|
|
OldProto->hasExceptionSpec(),
|
|
|
|
OldProto->hasAnyExceptionSpec(),
|
|
|
|
OldProto->getNumExceptions(),
|
|
|
|
OldProto->exception_begin(),
|
2010-03-31 00:24:48 +04:00
|
|
|
NewProto->getExtInfo());
|
2010-03-24 10:14:45 +03:00
|
|
|
New->setType(NewType);
|
|
|
|
|
|
|
|
// If exceptions are disabled, suppress the warning about missing
|
|
|
|
// exception specifications for new and delete operators.
|
|
|
|
if (!getLangOptions().Exceptions) {
|
|
|
|
switch (New->getDeclName().getCXXOverloadedOperator()) {
|
|
|
|
case OO_New:
|
|
|
|
case OO_Array_New:
|
|
|
|
case OO_Delete:
|
|
|
|
case OO_Array_Delete:
|
|
|
|
if (New->getDeclContext()->isTranslationUnit())
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warn about the lack of exception specification.
|
|
|
|
llvm::SmallString<128> ExceptionSpecString;
|
|
|
|
llvm::raw_svector_ostream OS(ExceptionSpecString);
|
|
|
|
OS << "throw(";
|
|
|
|
bool OnFirstException = true;
|
|
|
|
for (FunctionProtoType::exception_iterator E = OldProto->exception_begin(),
|
|
|
|
EEnd = OldProto->exception_end();
|
|
|
|
E != EEnd;
|
|
|
|
++E) {
|
|
|
|
if (OnFirstException)
|
|
|
|
OnFirstException = false;
|
|
|
|
else
|
|
|
|
OS << ", ";
|
|
|
|
|
|
|
|
OS << E->getAsString(Context.PrintingPolicy);
|
|
|
|
}
|
|
|
|
OS << ")";
|
|
|
|
OS.flush();
|
|
|
|
|
|
|
|
SourceLocation AfterParenLoc;
|
|
|
|
if (TypeSourceInfo *TSInfo = New->getTypeSourceInfo()) {
|
|
|
|
TypeLoc TL = TSInfo->getTypeLoc();
|
|
|
|
if (const FunctionTypeLoc *FTLoc = dyn_cast<FunctionTypeLoc>(&TL))
|
|
|
|
AfterParenLoc = PP.getLocForEndOfToken(FTLoc->getRParenLoc());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AfterParenLoc.isInvalid())
|
|
|
|
Diag(New->getLocation(), diag::warn_missing_exception_specification)
|
|
|
|
<< New << OS.str();
|
|
|
|
else {
|
|
|
|
// FIXME: This will get more complicated with C++0x
|
|
|
|
// late-specified return types.
|
|
|
|
Diag(New->getLocation(), diag::warn_missing_exception_specification)
|
|
|
|
<< New << OS.str()
|
2010-03-31 21:46:05 +04:00
|
|
|
<< FixItHint::CreateInsertion(AfterParenLoc, " " + OS.str().str());
|
2010-03-24 10:14:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Old->getLocation().isInvalid())
|
|
|
|
Diag(Old->getLocation(), diag::note_previous_declaration);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-12 10:32:17 +03:00
|
|
|
Diag(New->getLocation(), diag::err_mismatched_exception_spec);
|
|
|
|
Diag(Old->getLocation(), diag::note_previous_declaration);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-10-11 13:03:14 +04:00
|
|
|
/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
|
|
|
|
/// exception specifications. Exception specifications are equivalent if
|
|
|
|
/// they allow exactly the same set of exception types. It does not matter how
|
|
|
|
/// that is achieved. See C++ [except.spec]p2.
|
|
|
|
bool Sema::CheckEquivalentExceptionSpec(
|
|
|
|
const FunctionProtoType *Old, SourceLocation OldLoc,
|
|
|
|
const FunctionProtoType *New, SourceLocation NewLoc) {
|
2010-03-30 03:34:08 +04:00
|
|
|
return CheckEquivalentExceptionSpec(
|
|
|
|
PDiag(diag::err_mismatched_exception_spec),
|
|
|
|
PDiag(diag::note_previous_declaration),
|
2009-10-11 13:03:14 +04:00
|
|
|
Old, OldLoc, New, NewLoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
|
|
|
|
/// exception specifications. Exception specifications are equivalent if
|
|
|
|
/// they allow exactly the same set of exception types. It does not matter how
|
|
|
|
/// that is achieved. See C++ [except.spec]p2.
|
2010-03-24 10:14:45 +03:00
|
|
|
bool Sema::CheckEquivalentExceptionSpec(const PartialDiagnostic &DiagID,
|
|
|
|
const PartialDiagnostic & NoteID,
|
|
|
|
const FunctionProtoType *Old,
|
|
|
|
SourceLocation OldLoc,
|
|
|
|
const FunctionProtoType *New,
|
|
|
|
SourceLocation NewLoc,
|
|
|
|
bool *MissingExceptionSpecification,
|
|
|
|
bool *MissingEmptyExceptionSpecification) {
|
2010-05-28 12:37:35 +04:00
|
|
|
// Just completely ignore this under -fno-exceptions.
|
|
|
|
if (!getLangOptions().Exceptions)
|
|
|
|
return false;
|
|
|
|
|
2010-03-24 10:14:45 +03:00
|
|
|
if (MissingExceptionSpecification)
|
|
|
|
*MissingExceptionSpecification = false;
|
|
|
|
|
2010-02-12 10:32:17 +03:00
|
|
|
if (MissingEmptyExceptionSpecification)
|
|
|
|
*MissingEmptyExceptionSpecification = false;
|
|
|
|
|
2009-10-11 13:03:14 +04:00
|
|
|
bool OldAny = !Old->hasExceptionSpec() || Old->hasAnyExceptionSpec();
|
|
|
|
bool NewAny = !New->hasExceptionSpec() || New->hasAnyExceptionSpec();
|
|
|
|
if (OldAny && NewAny)
|
|
|
|
return false;
|
|
|
|
if (OldAny || NewAny) {
|
2010-03-24 10:14:45 +03:00
|
|
|
if (MissingExceptionSpecification && Old->hasExceptionSpec() &&
|
2010-02-12 10:32:17 +03:00
|
|
|
!New->hasExceptionSpec()) {
|
2010-03-24 10:14:45 +03:00
|
|
|
// The old type has an exception specification of some sort, but
|
|
|
|
// the new type does not.
|
|
|
|
*MissingExceptionSpecification = true;
|
|
|
|
|
|
|
|
if (MissingEmptyExceptionSpecification &&
|
|
|
|
!Old->hasAnyExceptionSpec() && Old->getNumExceptions() == 0) {
|
|
|
|
// The old type has a throw() exception specification and the
|
|
|
|
// new type has no exception specification, and the caller asked
|
|
|
|
// to handle this itself.
|
|
|
|
*MissingEmptyExceptionSpecification = true;
|
|
|
|
}
|
|
|
|
|
2010-02-12 10:32:17 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-10-11 13:03:14 +04:00
|
|
|
Diag(NewLoc, DiagID);
|
2009-10-14 20:09:29 +04:00
|
|
|
if (NoteID.getDiagID() != 0)
|
2009-10-11 13:03:14 +04:00
|
|
|
Diag(OldLoc, NoteID);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Success = true;
|
|
|
|
// Both have a definite exception spec. Collect the first set, then compare
|
|
|
|
// to the second.
|
2009-10-14 19:06:25 +04:00
|
|
|
llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes;
|
2009-10-11 13:03:14 +04:00
|
|
|
for (FunctionProtoType::exception_iterator I = Old->exception_begin(),
|
|
|
|
E = Old->exception_end(); I != E; ++I)
|
2009-10-14 19:06:25 +04:00
|
|
|
OldTypes.insert(Context.getCanonicalType(*I).getUnqualifiedType());
|
2009-10-11 13:03:14 +04:00
|
|
|
|
|
|
|
for (FunctionProtoType::exception_iterator I = New->exception_begin(),
|
2009-10-11 13:11:23 +04:00
|
|
|
E = New->exception_end(); I != E && Success; ++I) {
|
2009-10-14 19:06:25 +04:00
|
|
|
CanQualType TypePtr = Context.getCanonicalType(*I).getUnqualifiedType();
|
2009-10-11 13:11:23 +04:00
|
|
|
if(OldTypes.count(TypePtr))
|
|
|
|
NewTypes.insert(TypePtr);
|
|
|
|
else
|
|
|
|
Success = false;
|
|
|
|
}
|
2009-10-11 13:03:14 +04:00
|
|
|
|
2009-10-11 13:11:23 +04:00
|
|
|
Success = Success && OldTypes.size() == NewTypes.size();
|
2009-10-11 13:03:14 +04:00
|
|
|
|
|
|
|
if (Success) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Diag(NewLoc, DiagID);
|
2009-10-14 20:09:29 +04:00
|
|
|
if (NoteID.getDiagID() != 0)
|
2009-10-11 13:03:14 +04:00
|
|
|
Diag(OldLoc, NoteID);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CheckExceptionSpecSubset - Check whether the second function type's
|
|
|
|
/// exception specification is a subset (or equivalent) of the first function
|
|
|
|
/// type. This is used by override and pointer assignment checks.
|
2009-10-14 20:09:29 +04:00
|
|
|
bool Sema::CheckExceptionSpecSubset(
|
|
|
|
const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
|
2009-10-11 13:03:14 +04:00
|
|
|
const FunctionProtoType *Superset, SourceLocation SuperLoc,
|
|
|
|
const FunctionProtoType *Subset, SourceLocation SubLoc) {
|
2010-05-28 12:37:35 +04:00
|
|
|
|
|
|
|
// Just auto-succeed under -fno-exceptions.
|
|
|
|
if (!getLangOptions().Exceptions)
|
|
|
|
return false;
|
|
|
|
|
2009-10-11 13:03:14 +04:00
|
|
|
// FIXME: As usual, we could be more specific in our error messages, but
|
|
|
|
// that better waits until we've got types with source locations.
|
|
|
|
|
|
|
|
if (!SubLoc.isValid())
|
|
|
|
SubLoc = SuperLoc;
|
|
|
|
|
|
|
|
// If superset contains everything, we're done.
|
|
|
|
if (!Superset->hasExceptionSpec() || Superset->hasAnyExceptionSpec())
|
|
|
|
return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
|
|
|
|
|
|
|
|
// It does not. If the subset contains everything, we've failed.
|
|
|
|
if (!Subset->hasExceptionSpec() || Subset->hasAnyExceptionSpec()) {
|
|
|
|
Diag(SubLoc, DiagID);
|
2009-10-14 20:09:29 +04:00
|
|
|
if (NoteID.getDiagID() != 0)
|
2009-10-11 13:03:14 +04:00
|
|
|
Diag(SuperLoc, NoteID);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Neither contains everything. Do a proper comparison.
|
|
|
|
for (FunctionProtoType::exception_iterator SubI = Subset->exception_begin(),
|
|
|
|
SubE = Subset->exception_end(); SubI != SubE; ++SubI) {
|
|
|
|
// Take one type from the subset.
|
|
|
|
QualType CanonicalSubT = Context.getCanonicalType(*SubI);
|
2009-10-14 18:38:54 +04:00
|
|
|
// Unwrap pointers and references so that we can do checks within a class
|
|
|
|
// hierarchy. Don't unwrap member pointers; they don't have hierarchy
|
|
|
|
// conversions on the pointee.
|
2009-10-11 13:03:14 +04:00
|
|
|
bool SubIsPointer = false;
|
|
|
|
if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>())
|
|
|
|
CanonicalSubT = RefTy->getPointeeType();
|
|
|
|
if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) {
|
|
|
|
CanonicalSubT = PtrTy->getPointeeType();
|
|
|
|
SubIsPointer = true;
|
|
|
|
}
|
|
|
|
bool SubIsClass = CanonicalSubT->isRecordType();
|
First part of changes to eliminate problems with cv-qualifiers and
sugared types. The basic problem is that our qualifier accessors
(getQualifiers, getCVRQualifiers, isConstQualified, etc.) only look at
the current QualType and not at any qualifiers that come from sugared
types, meaning that we won't see these qualifiers through, e.g.,
typedefs:
typedef const int CInt;
typedef CInt Self;
Self.isConstQualified() currently returns false!
Various bugs (e.g., PR5383) have cropped up all over the front end due
to such problems. I'm addressing this problem by splitting each
qualifier accessor into two versions:
- the "local" version only returns qualifiers on this particular
QualType instance
- the "normal" version that will eventually combine qualifiers from this
QualType instance with the qualifiers on the canonical type to
produce the full set of qualifiers.
This commit adds the local versions and switches a few callers from
the "normal" version (e.g., isConstQualified) over to the "local"
version (e.g., isLocalConstQualified) when that is the right thing to
do, e.g., because we're printing or serializing the qualifiers. Also,
switch a bunch of
Context.getCanonicalType(T1).getUnqualifiedType() == Context.getCanonicalType(T2).getQualifiedType()
expressions over to
Context.hasSameUnqualifiedType(T1, T2)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@88969 91177308-0d34-0410-b5e6-96231b3b80d8
2009-11-17 00:35:15 +03:00
|
|
|
CanonicalSubT = CanonicalSubT.getLocalUnqualifiedType();
|
2009-10-11 13:03:14 +04:00
|
|
|
|
|
|
|
CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
|
|
|
|
/*DetectVirtual=*/false);
|
|
|
|
|
|
|
|
bool Contained = false;
|
|
|
|
// Make sure it's in the superset.
|
|
|
|
for (FunctionProtoType::exception_iterator SuperI =
|
|
|
|
Superset->exception_begin(), SuperE = Superset->exception_end();
|
|
|
|
SuperI != SuperE; ++SuperI) {
|
|
|
|
QualType CanonicalSuperT = Context.getCanonicalType(*SuperI);
|
|
|
|
// SubT must be SuperT or derived from it, or pointer or reference to
|
|
|
|
// such types.
|
|
|
|
if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>())
|
|
|
|
CanonicalSuperT = RefTy->getPointeeType();
|
|
|
|
if (SubIsPointer) {
|
|
|
|
if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>())
|
|
|
|
CanonicalSuperT = PtrTy->getPointeeType();
|
|
|
|
else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
First part of changes to eliminate problems with cv-qualifiers and
sugared types. The basic problem is that our qualifier accessors
(getQualifiers, getCVRQualifiers, isConstQualified, etc.) only look at
the current QualType and not at any qualifiers that come from sugared
types, meaning that we won't see these qualifiers through, e.g.,
typedefs:
typedef const int CInt;
typedef CInt Self;
Self.isConstQualified() currently returns false!
Various bugs (e.g., PR5383) have cropped up all over the front end due
to such problems. I'm addressing this problem by splitting each
qualifier accessor into two versions:
- the "local" version only returns qualifiers on this particular
QualType instance
- the "normal" version that will eventually combine qualifiers from this
QualType instance with the qualifiers on the canonical type to
produce the full set of qualifiers.
This commit adds the local versions and switches a few callers from
the "normal" version (e.g., isConstQualified) over to the "local"
version (e.g., isLocalConstQualified) when that is the right thing to
do, e.g., because we're printing or serializing the qualifiers. Also,
switch a bunch of
Context.getCanonicalType(T1).getUnqualifiedType() == Context.getCanonicalType(T2).getQualifiedType()
expressions over to
Context.hasSameUnqualifiedType(T1, T2)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@88969 91177308-0d34-0410-b5e6-96231b3b80d8
2009-11-17 00:35:15 +03:00
|
|
|
CanonicalSuperT = CanonicalSuperT.getLocalUnqualifiedType();
|
2009-10-11 13:03:14 +04:00
|
|
|
// If the types are the same, move on to the next type in the subset.
|
|
|
|
if (CanonicalSubT == CanonicalSuperT) {
|
|
|
|
Contained = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise we need to check the inheritance.
|
|
|
|
if (!SubIsClass || !CanonicalSuperT->isRecordType())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Paths.clear();
|
|
|
|
if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths))
|
|
|
|
continue;
|
|
|
|
|
2010-05-22 00:29:55 +04:00
|
|
|
if (Paths.isAmbiguous(Context.getCanonicalType(CanonicalSuperT)))
|
2009-10-11 13:03:14 +04:00
|
|
|
continue;
|
|
|
|
|
2010-02-10 12:31:12 +03:00
|
|
|
// Do this check from a context without privileges.
|
2010-03-16 08:22:47 +03:00
|
|
|
switch (CheckBaseClassAccess(SourceLocation(),
|
2010-02-10 12:31:12 +03:00
|
|
|
CanonicalSuperT, CanonicalSubT,
|
|
|
|
Paths.front(),
|
2010-03-16 08:22:47 +03:00
|
|
|
/*Diagnostic*/ 0,
|
2010-02-10 12:31:12 +03:00
|
|
|
/*ForceCheck*/ true,
|
2010-03-16 08:22:47 +03:00
|
|
|
/*ForceUnprivileged*/ true)) {
|
2010-02-10 12:31:12 +03:00
|
|
|
case AR_accessible: break;
|
|
|
|
case AR_inaccessible: continue;
|
|
|
|
case AR_dependent:
|
|
|
|
llvm_unreachable("access check dependent for unprivileged context");
|
|
|
|
break;
|
|
|
|
case AR_delayed:
|
|
|
|
llvm_unreachable("access check delayed in non-declaration");
|
|
|
|
break;
|
|
|
|
}
|
2009-10-11 13:03:14 +04:00
|
|
|
|
|
|
|
Contained = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!Contained) {
|
|
|
|
Diag(SubLoc, DiagID);
|
2009-10-14 20:09:29 +04:00
|
|
|
if (NoteID.getDiagID() != 0)
|
2009-10-11 13:03:14 +04:00
|
|
|
Diag(SuperLoc, NoteID);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We've run half the gauntlet.
|
|
|
|
return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool CheckSpecForTypesEquivalent(Sema &S,
|
2009-10-14 20:09:29 +04:00
|
|
|
const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
|
2009-10-11 13:03:14 +04:00
|
|
|
QualType Target, SourceLocation TargetLoc,
|
|
|
|
QualType Source, SourceLocation SourceLoc)
|
|
|
|
{
|
|
|
|
const FunctionProtoType *TFunc = GetUnderlyingFunction(Target);
|
|
|
|
if (!TFunc)
|
|
|
|
return false;
|
|
|
|
const FunctionProtoType *SFunc = GetUnderlyingFunction(Source);
|
|
|
|
if (!SFunc)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc,
|
|
|
|
SFunc, SourceLoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CheckParamExceptionSpec - Check if the parameter and return types of the
|
|
|
|
/// two functions have equivalent exception specs. This is part of the
|
|
|
|
/// assignment and override compatibility check. We do not check the parameters
|
|
|
|
/// of parameter function pointers recursively, as no sane programmer would
|
|
|
|
/// even be able to write such a function type.
|
2009-10-14 20:09:29 +04:00
|
|
|
bool Sema::CheckParamExceptionSpec(const PartialDiagnostic & NoteID,
|
2009-10-11 13:03:14 +04:00
|
|
|
const FunctionProtoType *Target, SourceLocation TargetLoc,
|
|
|
|
const FunctionProtoType *Source, SourceLocation SourceLoc)
|
|
|
|
{
|
2009-10-14 20:09:29 +04:00
|
|
|
if (CheckSpecForTypesEquivalent(*this,
|
2010-03-30 03:34:08 +04:00
|
|
|
PDiag(diag::err_deep_exception_specs_differ) << 0,
|
|
|
|
PDiag(),
|
2009-10-11 13:03:14 +04:00
|
|
|
Target->getResultType(), TargetLoc,
|
|
|
|
Source->getResultType(), SourceLoc))
|
|
|
|
return true;
|
|
|
|
|
2009-10-14 20:09:29 +04:00
|
|
|
// We shouldn't even be testing this unless the arguments are otherwise
|
2009-10-11 13:03:14 +04:00
|
|
|
// compatible.
|
|
|
|
assert(Target->getNumArgs() == Source->getNumArgs() &&
|
|
|
|
"Functions have different argument counts.");
|
|
|
|
for (unsigned i = 0, E = Target->getNumArgs(); i != E; ++i) {
|
2009-10-14 20:09:29 +04:00
|
|
|
if (CheckSpecForTypesEquivalent(*this,
|
2010-03-30 03:34:08 +04:00
|
|
|
PDiag(diag::err_deep_exception_specs_differ) << 1,
|
|
|
|
PDiag(),
|
2009-10-11 13:03:14 +04:00
|
|
|
Target->getArgType(i), TargetLoc,
|
|
|
|
Source->getArgType(i), SourceLoc))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType)
|
|
|
|
{
|
|
|
|
// First we check for applicability.
|
|
|
|
// Target type must be a function, function pointer or function reference.
|
|
|
|
const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType);
|
|
|
|
if (!ToFunc)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// SourceType must be a function or function pointer.
|
|
|
|
const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType());
|
|
|
|
if (!FromFunc)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Now we've got the correct types on both sides, check their compatibility.
|
|
|
|
// This means that the source of the conversion can only throw a subset of
|
|
|
|
// the exceptions of the target, and any exception specs on arguments or
|
|
|
|
// return types must be equivalent.
|
2010-03-30 03:34:08 +04:00
|
|
|
return CheckExceptionSpecSubset(PDiag(diag::err_incompatible_exception_specs),
|
|
|
|
PDiag(), ToFunc,
|
|
|
|
From->getSourceRange().getBegin(),
|
2009-10-11 13:03:14 +04:00
|
|
|
FromFunc, SourceLocation());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New,
|
|
|
|
const CXXMethodDecl *Old) {
|
2010-03-30 03:34:08 +04:00
|
|
|
return CheckExceptionSpecSubset(PDiag(diag::err_override_exception_spec),
|
|
|
|
PDiag(diag::note_overridden_virtual_function),
|
2009-10-11 13:03:14 +04:00
|
|
|
Old->getType()->getAs<FunctionProtoType>(),
|
|
|
|
Old->getLocation(),
|
|
|
|
New->getType()->getAs<FunctionProtoType>(),
|
|
|
|
New->getLocation());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace clang
|