Bug Summary

File:clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
Warning:line 148, column 9
Value stored to 'ThrownType' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name ExceptionAnalyzer.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/build-llvm/tools/clang/stage2-bins -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I tools/clang/tools/extra/clang-tidy/utils -I /build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/clang-tools-extra/clang-tidy/utils -I tools/clang/tools/extra/clang-tidy -I /build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/clang/include -I tools/clang/include -I include -I /build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fmacro-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/= -O3 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-01-19-134126-35450-1 -x c++ /build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
1//===--- ExceptionAnalyzer.cpp - clang-tidy -------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "ExceptionAnalyzer.h"
10
11namespace clang {
12namespace tidy {
13namespace utils {
14
15void ExceptionAnalyzer::ExceptionInfo::registerException(
16 const Type *ExceptionType) {
17 assert(ExceptionType != nullptr && "Only valid types are accepted")(static_cast <bool> (ExceptionType != nullptr &&
"Only valid types are accepted") ? void (0) : __assert_fail (
"ExceptionType != nullptr && \"Only valid types are accepted\""
, "clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp",
17, __extension__ __PRETTY_FUNCTION__))
;
18 Behaviour = State::Throwing;
19 ThrownExceptions.insert(ExceptionType);
20}
21
22void ExceptionAnalyzer::ExceptionInfo::registerExceptions(
23 const Throwables &Exceptions) {
24 if (Exceptions.size() == 0)
25 return;
26 Behaviour = State::Throwing;
27 ThrownExceptions.insert(Exceptions.begin(), Exceptions.end());
28}
29
30ExceptionAnalyzer::ExceptionInfo &ExceptionAnalyzer::ExceptionInfo::merge(
31 const ExceptionAnalyzer::ExceptionInfo &Other) {
32 // Only the following two cases require an update to the local
33 // 'Behaviour'. If the local entity is already throwing there will be no
34 // change and if the other entity is throwing the merged entity will throw
35 // as well.
36 // If one of both entities is 'Unknown' and the other one does not throw
37 // the merged entity is 'Unknown' as well.
38 if (Other.Behaviour == State::Throwing)
39 Behaviour = State::Throwing;
40 else if (Other.Behaviour == State::Unknown && Behaviour == State::NotThrowing)
41 Behaviour = State::Unknown;
42
43 ContainsUnknown = ContainsUnknown || Other.ContainsUnknown;
44 ThrownExceptions.insert(Other.ThrownExceptions.begin(),
45 Other.ThrownExceptions.end());
46 return *this;
47}
48
49static bool isBaseOf(const Type *DerivedType, const Type *BaseType) {
50 const auto *DerivedClass = DerivedType->getAsCXXRecordDecl();
51 const auto *BaseClass = BaseType->getAsCXXRecordDecl();
52 if (!DerivedClass || !BaseClass)
53 return false;
54
55 return !DerivedClass->forallBases(
56 [BaseClass](const CXXRecordDecl *Cur) { return Cur != BaseClass; });
57}
58
59bool ExceptionAnalyzer::ExceptionInfo::filterByCatch(const Type *BaseClass) {
60 llvm::SmallVector<const Type *, 8> TypesToDelete;
61 for (const Type *T : ThrownExceptions) {
62 if (T == BaseClass || isBaseOf(T, BaseClass))
63 TypesToDelete.push_back(T);
64 }
65
66 for (const Type *T : TypesToDelete)
67 ThrownExceptions.erase(T);
68
69 reevaluateBehaviour();
70 return TypesToDelete.size() > 0;
71}
72
73ExceptionAnalyzer::ExceptionInfo &
74ExceptionAnalyzer::ExceptionInfo::filterIgnoredExceptions(
75 const llvm::StringSet<> &IgnoredTypes, bool IgnoreBadAlloc) {
76 llvm::SmallVector<const Type *, 8> TypesToDelete;
77 // Note: Using a 'SmallSet' with 'llvm::remove_if()' is not possible.
78 // Therefore this slightly hacky implementation is required.
79 for (const Type *T : ThrownExceptions) {
80 if (const auto *TD = T->getAsTagDecl()) {
81 if (TD->getDeclName().isIdentifier()) {
82 if ((IgnoreBadAlloc &&
83 (TD->getName() == "bad_alloc" && TD->isInStdNamespace())) ||
84 (IgnoredTypes.count(TD->getName()) > 0))
85 TypesToDelete.push_back(T);
86 }
87 }
88 }
89 for (const Type *T : TypesToDelete)
90 ThrownExceptions.erase(T);
91
92 reevaluateBehaviour();
93 return *this;
94}
95
96void ExceptionAnalyzer::ExceptionInfo::clear() {
97 Behaviour = State::NotThrowing;
98 ContainsUnknown = false;
99 ThrownExceptions.clear();
100}
101
102void ExceptionAnalyzer::ExceptionInfo::reevaluateBehaviour() {
103 if (ThrownExceptions.size() == 0)
104 if (ContainsUnknown)
105 Behaviour = State::Unknown;
106 else
107 Behaviour = State::NotThrowing;
108 else
109 Behaviour = State::Throwing;
110}
111
112ExceptionAnalyzer::ExceptionInfo ExceptionAnalyzer::throwsException(
113 const FunctionDecl *Func,
114 llvm::SmallSet<const FunctionDecl *, 32> &CallStack) {
115 if (CallStack.count(Func))
116 return ExceptionInfo::createNonThrowing();
117
118 if (const Stmt *Body = Func->getBody()) {
119 CallStack.insert(Func);
120 ExceptionInfo Result =
121 throwsException(Body, ExceptionInfo::Throwables(), CallStack);
122 CallStack.erase(Func);
123 return Result;
124 }
125
126 auto Result = ExceptionInfo::createUnknown();
127 if (const auto *FPT = Func->getType()->getAs<FunctionProtoType>()) {
128 for (const QualType &Ex : FPT->exceptions())
129 Result.registerException(Ex.getTypePtr());
130 }
131 return Result;
132}
133
134/// Analyzes a single statement on it's throwing behaviour. This is in principle
135/// possible except some 'Unknown' functions are called.
136ExceptionAnalyzer::ExceptionInfo ExceptionAnalyzer::throwsException(
137 const Stmt *St, const ExceptionInfo::Throwables &Caught,
138 llvm::SmallSet<const FunctionDecl *, 32> &CallStack) {
139 auto Results = ExceptionInfo::createNonThrowing();
140 if (!St)
141 return Results;
142
143 if (const auto *Throw = dyn_cast<CXXThrowExpr>(St)) {
144 if (const auto *ThrownExpr = Throw->getSubExpr()) {
145 const auto *ThrownType =
146 ThrownExpr->getType()->getUnqualifiedDesugaredType();
147 if (ThrownType->isReferenceType())
148 ThrownType = ThrownType->castAs<ReferenceType>()
Value stored to 'ThrownType' is never read
149 ->getPointeeType()
150 ->getUnqualifiedDesugaredType();
151 Results.registerException(
152 ThrownExpr->getType()->getUnqualifiedDesugaredType());
153 } else
154 // A rethrow of a caught exception happens which makes it possible
155 // to throw all exception that are caught in the 'catch' clause of
156 // the parent try-catch block.
157 Results.registerExceptions(Caught);
158 } else if (const auto *Try = dyn_cast<CXXTryStmt>(St)) {
159 ExceptionInfo Uncaught =
160 throwsException(Try->getTryBlock(), Caught, CallStack);
161 for (unsigned I = 0; I < Try->getNumHandlers(); ++I) {
162 const CXXCatchStmt *Catch = Try->getHandler(I);
163
164 // Everything is catched through 'catch(...)'.
165 if (!Catch->getExceptionDecl()) {
166 ExceptionInfo Rethrown = throwsException(
167 Catch->getHandlerBlock(), Uncaught.getExceptionTypes(), CallStack);
168 Results.merge(Rethrown);
169 Uncaught.clear();
170 } else {
171 const auto *CaughtType =
172 Catch->getCaughtType()->getUnqualifiedDesugaredType();
173 if (CaughtType->isReferenceType()) {
174 CaughtType = CaughtType->castAs<ReferenceType>()
175 ->getPointeeType()
176 ->getUnqualifiedDesugaredType();
177 }
178
179 // If the caught exception will catch multiple previously potential
180 // thrown types (because it's sensitive to inheritance) the throwing
181 // situation changes. First of all filter the exception types and
182 // analyze if the baseclass-exception is rethrown.
183 if (Uncaught.filterByCatch(CaughtType)) {
184 ExceptionInfo::Throwables CaughtExceptions;
185 CaughtExceptions.insert(CaughtType);
186 ExceptionInfo Rethrown = throwsException(Catch->getHandlerBlock(),
187 CaughtExceptions, CallStack);
188 Results.merge(Rethrown);
189 }
190 }
191 }
192 Results.merge(Uncaught);
193 } else if (const auto *Call = dyn_cast<CallExpr>(St)) {
194 if (const FunctionDecl *Func = Call->getDirectCallee()) {
195 ExceptionInfo Excs = throwsException(Func, CallStack);
196 Results.merge(Excs);
197 }
198 } else {
199 for (const Stmt *Child : St->children()) {
200 ExceptionInfo Excs = throwsException(Child, Caught, CallStack);
201 Results.merge(Excs);
202 }
203 }
204 return Results;
205}
206
207ExceptionAnalyzer::ExceptionInfo
208ExceptionAnalyzer::analyzeImpl(const FunctionDecl *Func) {
209 ExceptionInfo ExceptionList;
210
211 // Check if the function has already been analyzed and reuse that result.
212 if (FunctionCache.count(Func) == 0) {
213 llvm::SmallSet<const FunctionDecl *, 32> CallStack;
214 ExceptionList = throwsException(Func, CallStack);
215
216 // Cache the result of the analysis. This is done prior to filtering
217 // because it is best to keep as much information as possible.
218 // The results here might be relevant to different analysis passes
219 // with different needs as well.
220 FunctionCache.insert(std::make_pair(Func, ExceptionList));
221 } else
222 ExceptionList = FunctionCache[Func];
223
224 return ExceptionList;
225}
226
227ExceptionAnalyzer::ExceptionInfo
228ExceptionAnalyzer::analyzeImpl(const Stmt *Stmt) {
229 llvm::SmallSet<const FunctionDecl *, 32> CallStack;
230 return throwsException(Stmt, ExceptionInfo::Throwables(), CallStack);
231}
232
233template <typename T>
234ExceptionAnalyzer::ExceptionInfo
235ExceptionAnalyzer::analyzeDispatch(const T *Node) {
236 ExceptionInfo ExceptionList = analyzeImpl(Node);
237
238 if (ExceptionList.getBehaviour() == State::NotThrowing ||
239 ExceptionList.getBehaviour() == State::Unknown)
240 return ExceptionList;
241
242 // Remove all ignored exceptions from the list of exceptions that can be
243 // thrown.
244 ExceptionList.filterIgnoredExceptions(IgnoredExceptions, IgnoreBadAlloc);
245
246 return ExceptionList;
247}
248
249ExceptionAnalyzer::ExceptionInfo
250ExceptionAnalyzer::analyze(const FunctionDecl *Func) {
251 return analyzeDispatch(Func);
252}
253
254ExceptionAnalyzer::ExceptionInfo
255ExceptionAnalyzer::analyze(const Stmt *Stmt) {
256 return analyzeDispatch(Stmt);
257}
258
259} // namespace utils
260} // namespace tidy
261
262} // namespace clang