1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
//===--- ExceptionSpecAnalyzer.cpp - clang-tidy ---------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "ExceptionSpecAnalyzer.h"
#include "clang/AST/Expr.h"
namespace clang::tidy::utils {
ExceptionSpecAnalyzer::State
ExceptionSpecAnalyzer::analyze(const FunctionDecl *FuncDecl) {
ExceptionSpecAnalyzer::State State;
// Check if the function has already been analyzed and reuse that result.
const auto CacheEntry = FunctionCache.find(FuncDecl);
if (CacheEntry == FunctionCache.end()) {
State = analyzeImpl(FuncDecl);
// Cache the result of the analysis.
FunctionCache.try_emplace(FuncDecl, State);
} else
State = CacheEntry->getSecond();
return State;
}
ExceptionSpecAnalyzer::State
ExceptionSpecAnalyzer::analyzeUnresolvedOrDefaulted(
const CXXMethodDecl *MethodDecl, const FunctionProtoType *FuncProto) {
if (!FuncProto || !MethodDecl)
return State::Unknown;
const DefaultableMemberKind Kind = getDefaultableMemberKind(MethodDecl);
if (Kind == DefaultableMemberKind::None)
return State::Unknown;
return analyzeRecord(MethodDecl->getParent(), Kind, SkipMethods::Yes);
}
ExceptionSpecAnalyzer::State
ExceptionSpecAnalyzer::analyzeFieldDecl(const FieldDecl *FDecl,
DefaultableMemberKind Kind) {
if (!FDecl)
return State::Unknown;
if (const CXXRecordDecl *RecDecl =
FDecl->getType()->getUnqualifiedDesugaredType()->getAsCXXRecordDecl())
return analyzeRecord(RecDecl, Kind);
// Trivial types do not throw
if (FDecl->getType().isTrivialType(FDecl->getASTContext()))
return State::NotThrowing;
return State::Unknown;
}
ExceptionSpecAnalyzer::State
ExceptionSpecAnalyzer::analyzeBase(const CXXBaseSpecifier &Base,
DefaultableMemberKind Kind) {
const auto *RecType = Base.getType()->getAs<RecordType>();
if (!RecType)
return State::Unknown;
const auto *BaseClass = cast<CXXRecordDecl>(RecType->getDecl());
return analyzeRecord(BaseClass, Kind);
}
ExceptionSpecAnalyzer::State
ExceptionSpecAnalyzer::analyzeRecord(const CXXRecordDecl *RecordDecl,
DefaultableMemberKind Kind,
SkipMethods SkipMethods) {
if (!RecordDecl)
return State::Unknown;
// Trivial implies noexcept
if (hasTrivialMemberKind(RecordDecl, Kind))
return State::NotThrowing;
if (SkipMethods == SkipMethods::No)
for (const auto *MethodDecl : RecordDecl->methods())
if (getDefaultableMemberKind(MethodDecl) == Kind)
return analyze(MethodDecl);
for (const auto &BaseSpec : RecordDecl->bases()) {
State Result = analyzeBase(BaseSpec, Kind);
if (Result == State::Throwing || Result == State::Unknown)
return Result;
}
for (const auto &BaseSpec : RecordDecl->vbases()) {
State Result = analyzeBase(BaseSpec, Kind);
if (Result == State::Throwing || Result == State::Unknown)
return Result;
}
for (const auto *FDecl : RecordDecl->fields())
if (!FDecl->isInvalidDecl() && !FDecl->isUnnamedBitfield()) {
State Result = analyzeFieldDecl(FDecl, Kind);
if (Result == State::Throwing || Result == State::Unknown)
return Result;
}
return State::NotThrowing;
}
ExceptionSpecAnalyzer::State
ExceptionSpecAnalyzer::analyzeImpl(const FunctionDecl *FuncDecl) {
const auto *FuncProto = FuncDecl->getType()->getAs<FunctionProtoType>();
if (!FuncProto)
return State::Unknown;
const ExceptionSpecificationType EST = FuncProto->getExceptionSpecType();
if (EST == EST_Unevaluated || (EST == EST_None && FuncDecl->isDefaulted()))
return analyzeUnresolvedOrDefaulted(cast<CXXMethodDecl>(FuncDecl),
FuncProto);
return analyzeFunctionEST(FuncDecl, FuncProto);
}
ExceptionSpecAnalyzer::State
ExceptionSpecAnalyzer::analyzeFunctionEST(const FunctionDecl *FuncDecl,
const FunctionProtoType *FuncProto) {
if (!FuncDecl || !FuncProto)
return State::Unknown;
if (isUnresolvedExceptionSpec(FuncProto->getExceptionSpecType()))
return State::Unknown;
// A non defaulted destructor without the noexcept specifier is still noexcept
if (isa<CXXDestructorDecl>(FuncDecl) &&
FuncDecl->getExceptionSpecType() == EST_None)
return State::NotThrowing;
switch (FuncProto->canThrow()) {
case CT_Cannot:
return State::NotThrowing;
case CT_Dependent: {
const Expr *NoexceptExpr = FuncProto->getNoexceptExpr();
bool Result;
return (NoexceptExpr && !NoexceptExpr->isValueDependent() &&
NoexceptExpr->EvaluateAsBooleanCondition(
Result, FuncDecl->getASTContext(), true) &&
Result)
? State::NotThrowing
: State::Throwing;
}
default:
return State::Throwing;
};
}
bool ExceptionSpecAnalyzer::hasTrivialMemberKind(const CXXRecordDecl *RecDecl,
DefaultableMemberKind Kind) {
if (!RecDecl)
return false;
switch (Kind) {
case DefaultableMemberKind::DefaultConstructor:
return RecDecl->hasTrivialDefaultConstructor();
case DefaultableMemberKind::CopyConstructor:
return RecDecl->hasTrivialCopyConstructor();
case DefaultableMemberKind::MoveConstructor:
return RecDecl->hasTrivialMoveConstructor();
case DefaultableMemberKind::CopyAssignment:
return RecDecl->hasTrivialCopyAssignment();
case DefaultableMemberKind::MoveAssignment:
return RecDecl->hasTrivialMoveAssignment();
case DefaultableMemberKind::Destructor:
return RecDecl->hasTrivialDestructor();
default:
return false;
}
}
bool ExceptionSpecAnalyzer::isConstructor(DefaultableMemberKind Kind) {
switch (Kind) {
case DefaultableMemberKind::DefaultConstructor:
case DefaultableMemberKind::CopyConstructor:
case DefaultableMemberKind::MoveConstructor:
return true;
default:
return false;
}
}
bool ExceptionSpecAnalyzer::isSpecialMember(DefaultableMemberKind Kind) {
switch (Kind) {
case DefaultableMemberKind::DefaultConstructor:
case DefaultableMemberKind::CopyConstructor:
case DefaultableMemberKind::MoveConstructor:
case DefaultableMemberKind::CopyAssignment:
case DefaultableMemberKind::MoveAssignment:
case DefaultableMemberKind::Destructor:
return true;
default:
return false;
}
}
bool ExceptionSpecAnalyzer::isComparison(DefaultableMemberKind Kind) {
switch (Kind) {
case DefaultableMemberKind::CompareEqual:
case DefaultableMemberKind::CompareNotEqual:
case DefaultableMemberKind::CompareRelational:
case DefaultableMemberKind::CompareThreeWay:
return true;
default:
return false;
}
}
ExceptionSpecAnalyzer::DefaultableMemberKind
ExceptionSpecAnalyzer::getDefaultableMemberKind(const FunctionDecl *FuncDecl) {
if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FuncDecl)) {
if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(FuncDecl)) {
if (Ctor->isDefaultConstructor())
return DefaultableMemberKind::DefaultConstructor;
if (Ctor->isCopyConstructor())
return DefaultableMemberKind::CopyConstructor;
if (Ctor->isMoveConstructor())
return DefaultableMemberKind::MoveConstructor;
}
if (MethodDecl->isCopyAssignmentOperator())
return DefaultableMemberKind::CopyAssignment;
if (MethodDecl->isMoveAssignmentOperator())
return DefaultableMemberKind::MoveAssignment;
if (isa<CXXDestructorDecl>(FuncDecl))
return DefaultableMemberKind::Destructor;
}
const LangOptions &LangOpts = FuncDecl->getLangOpts();
switch (FuncDecl->getDeclName().getCXXOverloadedOperator()) {
case OO_EqualEqual:
return DefaultableMemberKind::CompareEqual;
case OO_ExclaimEqual:
return DefaultableMemberKind::CompareNotEqual;
case OO_Spaceship:
// No point allowing this if <=> doesn't exist in the current language mode.
if (!LangOpts.CPlusPlus20)
break;
return DefaultableMemberKind::CompareThreeWay;
case OO_Less:
case OO_LessEqual:
case OO_Greater:
case OO_GreaterEqual:
// No point allowing this if <=> doesn't exist in the current language mode.
if (!LangOpts.CPlusPlus20)
break;
return DefaultableMemberKind::CompareRelational;
default:
break;
}
// Not a defaultable member kind
return DefaultableMemberKind::None;
}
} // namespace clang::tidy::utils
|