Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1 | //=- AnalysisBasedWarnings.cpp - Sema warnings based on libAnalysis -*- C++ -*-=// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines analysis_warnings::[Policy,Executor]. |
| 10 | // Together they are used by Sema to issue warnings based on inexpensive |
| 11 | // static analysis algorithms in libAnalysis. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/AnalysisBasedWarnings.h" |
John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Ted Kremenek | b8d8c4e | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 18 | #include "clang/AST/EvaluatedExprVisitor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprCXX.h" |
| 20 | #include "clang/AST/ExprObjC.h" |
Jordan Rose | 76831c6 | 2012-10-11 16:10:19 +0000 | [diff] [blame] | 21 | #include "clang/AST/ParentMap.h" |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 22 | #include "clang/AST/RecursiveASTVisitor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/AST/StmtCXX.h" |
| 24 | #include "clang/AST/StmtObjC.h" |
| 25 | #include "clang/AST/StmtVisitor.h" |
| 26 | #include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h" |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 27 | #include "clang/Analysis/Analyses/Consumed.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 28 | #include "clang/Analysis/Analyses/ReachableCode.h" |
| 29 | #include "clang/Analysis/Analyses/ThreadSafety.h" |
| 30 | #include "clang/Analysis/Analyses/UninitializedValues.h" |
George Karpenkov | 50657f6 | 2017-09-06 21:45:03 +0000 | [diff] [blame] | 31 | #include "clang/Analysis/AnalysisDeclContext.h" |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 32 | #include "clang/Analysis/CFG.h" |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 33 | #include "clang/Analysis/CFGStmtMap.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 34 | #include "clang/Basic/SourceLocation.h" |
| 35 | #include "clang/Basic/SourceManager.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 36 | #include "clang/Lex/Preprocessor.h" |
| 37 | #include "clang/Sema/ScopeInfo.h" |
| 38 | #include "clang/Sema/SemaInternal.h" |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/BitVector.h" |
Enea Zaffanella | 2f40be7 | 2013-02-15 20:09:55 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/MapVector.h" |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/SmallString.h" |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/SmallVector.h" |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 43 | #include "llvm/ADT/StringRef.h" |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 44 | #include "llvm/Support/Casting.h" |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 45 | #include <algorithm> |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 46 | #include <deque> |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 47 | #include <iterator> |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 48 | |
| 49 | using namespace clang; |
| 50 | |
| 51 | //===----------------------------------------------------------------------===// |
| 52 | // Unreachable code analysis. |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | |
| 55 | namespace { |
| 56 | class UnreachableCodeHandler : public reachable_code::Callback { |
| 57 | Sema &S; |
Alex Lorenz | 569ad73 | 2017-01-12 10:48:03 +0000 | [diff] [blame] | 58 | SourceRange PreviousSilenceableCondVal; |
| 59 | |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 60 | public: |
| 61 | UnreachableCodeHandler(Sema &s) : S(s) {} |
| 62 | |
Ted Kremenek | 1a8641c | 2014-03-15 01:26:32 +0000 | [diff] [blame] | 63 | void HandleUnreachable(reachable_code::UnreachableKind UK, |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 64 | SourceLocation L, |
| 65 | SourceRange SilenceableCondVal, |
| 66 | SourceRange R1, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 67 | SourceRange R2) override { |
Alex Lorenz | 569ad73 | 2017-01-12 10:48:03 +0000 | [diff] [blame] | 68 | // Avoid reporting multiple unreachable code diagnostics that are |
| 69 | // triggered by the same conditional value. |
| 70 | if (PreviousSilenceableCondVal.isValid() && |
| 71 | SilenceableCondVal.isValid() && |
| 72 | PreviousSilenceableCondVal == SilenceableCondVal) |
| 73 | return; |
| 74 | PreviousSilenceableCondVal = SilenceableCondVal; |
| 75 | |
Ted Kremenek | 1a8641c | 2014-03-15 01:26:32 +0000 | [diff] [blame] | 76 | unsigned diag = diag::warn_unreachable; |
| 77 | switch (UK) { |
| 78 | case reachable_code::UK_Break: |
| 79 | diag = diag::warn_unreachable_break; |
| 80 | break; |
Ted Kremenek | f3c93bb | 2014-03-20 06:07:30 +0000 | [diff] [blame] | 81 | case reachable_code::UK_Return: |
Ted Kremenek | ad8753c | 2014-03-15 05:47:06 +0000 | [diff] [blame] | 82 | diag = diag::warn_unreachable_return; |
Ted Kremenek | 1a8641c | 2014-03-15 01:26:32 +0000 | [diff] [blame] | 83 | break; |
Ted Kremenek | 1421037 | 2014-03-21 06:02:36 +0000 | [diff] [blame] | 84 | case reachable_code::UK_Loop_Increment: |
| 85 | diag = diag::warn_unreachable_loop_increment; |
| 86 | break; |
Ted Kremenek | 1a8641c | 2014-03-15 01:26:32 +0000 | [diff] [blame] | 87 | case reachable_code::UK_Other: |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | S.Diag(L, diag) << R1 << R2; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 92 | |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 93 | SourceLocation Open = SilenceableCondVal.getBegin(); |
| 94 | if (Open.isValid()) { |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 95 | SourceLocation Close = SilenceableCondVal.getEnd(); |
| 96 | Close = S.getLocForEndOfToken(Close); |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 97 | if (Close.isValid()) { |
| 98 | S.Diag(Open, diag::note_unreachable_silence) |
| 99 | << FixItHint::CreateInsertion(Open, "/* DISABLES CODE */ (") |
| 100 | << FixItHint::CreateInsertion(Close, ")"); |
| 101 | } |
| 102 | } |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 103 | } |
| 104 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 105 | } // anonymous namespace |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 106 | |
| 107 | /// CheckUnreachable - Check for unreachable code. |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 108 | static void CheckUnreachable(Sema &S, AnalysisDeclContext &AC) { |
Ted Kremenek | c1b2875 | 2014-02-25 22:35:37 +0000 | [diff] [blame] | 109 | // As a heuristic prune all diagnostics not in the main file. Currently |
| 110 | // the majority of warnings in headers are false positives. These |
| 111 | // are largely caused by configuration state, e.g. preprocessor |
| 112 | // defined code, etc. |
| 113 | // |
| 114 | // Note that this is also a performance optimization. Analyzing |
| 115 | // headers many times can be expensive. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 116 | if (!S.getSourceManager().isInMainFile(AC.getDecl()->getBeginLoc())) |
Ted Kremenek | c1b2875 | 2014-02-25 22:35:37 +0000 | [diff] [blame] | 117 | return; |
| 118 | |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 119 | UnreachableCodeHandler UC(S); |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 120 | reachable_code::FindUnreachableCode(AC, S.getPreprocessor(), UC); |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Benjamin Kramer | 3a00225 | 2015-02-16 16:53:12 +0000 | [diff] [blame] | 123 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 124 | /// Warn on logical operator errors in CFGBuilder |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 125 | class LogicalErrorHandler : public CFGCallback { |
| 126 | Sema &S; |
| 127 | |
| 128 | public: |
| 129 | LogicalErrorHandler(Sema &S) : CFGCallback(), S(S) {} |
| 130 | |
| 131 | static bool HasMacroID(const Expr *E) { |
| 132 | if (E->getExprLoc().isMacroID()) |
| 133 | return true; |
| 134 | |
| 135 | // Recurse to children. |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 136 | for (const Stmt *SubStmt : E->children()) |
| 137 | if (const Expr *SubExpr = dyn_cast_or_null<Expr>(SubStmt)) |
| 138 | if (HasMacroID(SubExpr)) |
| 139 | return true; |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 140 | |
| 141 | return false; |
| 142 | } |
| 143 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 144 | void compareAlwaysTrue(const BinaryOperator *B, bool isAlwaysTrue) override { |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 145 | if (HasMacroID(B)) |
| 146 | return; |
| 147 | |
| 148 | SourceRange DiagRange = B->getSourceRange(); |
| 149 | S.Diag(B->getExprLoc(), diag::warn_tautological_overlap_comparison) |
| 150 | << DiagRange << isAlwaysTrue; |
| 151 | } |
Jordan Rose | 7afd71e | 2014-05-20 17:31:11 +0000 | [diff] [blame] | 152 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 153 | void compareBitwiseEquality(const BinaryOperator *B, |
| 154 | bool isAlwaysTrue) override { |
Jordan Rose | 7afd71e | 2014-05-20 17:31:11 +0000 | [diff] [blame] | 155 | if (HasMacroID(B)) |
| 156 | return; |
| 157 | |
| 158 | SourceRange DiagRange = B->getSourceRange(); |
| 159 | S.Diag(B->getExprLoc(), diag::warn_comparison_bitwise_always) |
| 160 | << DiagRange << isAlwaysTrue; |
| 161 | } |
Richard Trieu | 8b0d14a | 2019-10-19 00:57:23 +0000 | [diff] [blame] | 162 | |
| 163 | void compareBitwiseOr(const BinaryOperator *B) override { |
| 164 | if (HasMacroID(B)) |
| 165 | return; |
| 166 | |
| 167 | SourceRange DiagRange = B->getSourceRange(); |
| 168 | S.Diag(B->getExprLoc(), diag::warn_comparison_bitwise_or) << DiagRange; |
| 169 | } |
| 170 | |
| 171 | static bool hasActiveDiagnostics(DiagnosticsEngine &Diags, |
| 172 | SourceLocation Loc) { |
| 173 | return !Diags.isIgnored(diag::warn_tautological_overlap_comparison, Loc) || |
| 174 | !Diags.isIgnored(diag::warn_comparison_bitwise_or, Loc); |
| 175 | } |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 176 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 177 | } // anonymous namespace |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 178 | |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 179 | //===----------------------------------------------------------------------===// |
Richard Trieu | 2f024f4 | 2013-12-21 02:33:43 +0000 | [diff] [blame] | 180 | // Check for infinite self-recursion in functions |
| 181 | //===----------------------------------------------------------------------===// |
| 182 | |
Richard Trieu | 6995de9 | 2015-08-21 03:43:09 +0000 | [diff] [blame] | 183 | // Returns true if the function is called anywhere within the CFGBlock. |
| 184 | // For member functions, the additional condition of being call from the |
| 185 | // this pointer is required. |
Duncan P. N. Exon Smith | f0eafc7 | 2015-07-23 20:11:47 +0000 | [diff] [blame] | 186 | static bool hasRecursiveCallInPath(const FunctionDecl *FD, CFGBlock &Block) { |
Richard Trieu | 6995de9 | 2015-08-21 03:43:09 +0000 | [diff] [blame] | 187 | // Process all the Stmt's in this block to find any calls to FD. |
Duncan P. N. Exon Smith | f0eafc7 | 2015-07-23 20:11:47 +0000 | [diff] [blame] | 188 | for (const auto &B : Block) { |
| 189 | if (B.getKind() != CFGElement::Statement) |
| 190 | continue; |
| 191 | |
| 192 | const CallExpr *CE = dyn_cast<CallExpr>(B.getAs<CFGStmt>()->getStmt()); |
| 193 | if (!CE || !CE->getCalleeDecl() || |
| 194 | CE->getCalleeDecl()->getCanonicalDecl() != FD) |
| 195 | continue; |
| 196 | |
| 197 | // Skip function calls which are qualified with a templated class. |
| 198 | if (const DeclRefExpr *DRE = |
| 199 | dyn_cast<DeclRefExpr>(CE->getCallee()->IgnoreParenImpCasts())) { |
| 200 | if (NestedNameSpecifier *NNS = DRE->getQualifier()) { |
| 201 | if (NNS->getKind() == NestedNameSpecifier::TypeSpec && |
| 202 | isa<TemplateSpecializationType>(NNS->getAsType())) { |
| 203 | continue; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | const CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(CE); |
| 209 | if (!MCE || isa<CXXThisExpr>(MCE->getImplicitObjectArgument()) || |
| 210 | !MCE->getMethodDecl()->isVirtual()) |
| 211 | return true; |
| 212 | } |
| 213 | return false; |
| 214 | } |
| 215 | |
Robert Widmann | 9760844 | 2018-03-22 03:16:23 +0000 | [diff] [blame] | 216 | // Returns true if every path from the entry block passes through a call to FD. |
Richard Trieu | 6995de9 | 2015-08-21 03:43:09 +0000 | [diff] [blame] | 217 | static bool checkForRecursiveFunctionCall(const FunctionDecl *FD, CFG *cfg) { |
Robert Widmann | 9760844 | 2018-03-22 03:16:23 +0000 | [diff] [blame] | 218 | llvm::SmallPtrSet<CFGBlock *, 16> Visited; |
| 219 | llvm::SmallVector<CFGBlock *, 16> WorkList; |
| 220 | // Keep track of whether we found at least one recursive path. |
| 221 | bool foundRecursion = false; |
Richard Trieu | 6995de9 | 2015-08-21 03:43:09 +0000 | [diff] [blame] | 222 | |
| 223 | const unsigned ExitID = cfg->getExit().getBlockID(); |
| 224 | |
Robert Widmann | 9760844 | 2018-03-22 03:16:23 +0000 | [diff] [blame] | 225 | // Seed the work list with the entry block. |
| 226 | WorkList.push_back(&cfg->getEntry()); |
Richard Trieu | 6995de9 | 2015-08-21 03:43:09 +0000 | [diff] [blame] | 227 | |
Robert Widmann | 9760844 | 2018-03-22 03:16:23 +0000 | [diff] [blame] | 228 | while (!WorkList.empty()) { |
| 229 | CFGBlock *Block = WorkList.pop_back_val(); |
Richard Trieu | 2f024f4 | 2013-12-21 02:33:43 +0000 | [diff] [blame] | 230 | |
Robert Widmann | 9760844 | 2018-03-22 03:16:23 +0000 | [diff] [blame] | 231 | for (auto I = Block->succ_begin(), E = Block->succ_end(); I != E; ++I) { |
| 232 | if (CFGBlock *SuccBlock = *I) { |
| 233 | if (!Visited.insert(SuccBlock).second) |
| 234 | continue; |
Richard Trieu | 2f024f4 | 2013-12-21 02:33:43 +0000 | [diff] [blame] | 235 | |
Robert Widmann | 9760844 | 2018-03-22 03:16:23 +0000 | [diff] [blame] | 236 | // Found a path to the exit node without a recursive call. |
| 237 | if (ExitID == SuccBlock->getBlockID()) |
| 238 | return false; |
Duncan P. N. Exon Smith | dccc30a | 2015-07-23 20:15:50 +0000 | [diff] [blame] | 239 | |
Robert Widmann | 9760844 | 2018-03-22 03:16:23 +0000 | [diff] [blame] | 240 | // If the successor block contains a recursive call, end analysis there. |
| 241 | if (hasRecursiveCallInPath(FD, *SuccBlock)) { |
| 242 | foundRecursion = true; |
| 243 | continue; |
Richard Trieu | 6995de9 | 2015-08-21 03:43:09 +0000 | [diff] [blame] | 244 | } |
Richard Trieu | 6995de9 | 2015-08-21 03:43:09 +0000 | [diff] [blame] | 245 | |
Robert Widmann | 9760844 | 2018-03-22 03:16:23 +0000 | [diff] [blame] | 246 | WorkList.push_back(SuccBlock); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | return foundRecursion; |
Richard Trieu | 2f024f4 | 2013-12-21 02:33:43 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static void checkRecursiveFunction(Sema &S, const FunctionDecl *FD, |
Richard Trieu | 6995de9 | 2015-08-21 03:43:09 +0000 | [diff] [blame] | 254 | const Stmt *Body, AnalysisDeclContext &AC) { |
Richard Trieu | 2f024f4 | 2013-12-21 02:33:43 +0000 | [diff] [blame] | 255 | FD = FD->getCanonicalDecl(); |
| 256 | |
| 257 | // Only run on non-templated functions and non-templated members of |
| 258 | // templated classes. |
| 259 | if (FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate && |
| 260 | FD->getTemplatedKind() != FunctionDecl::TK_MemberSpecialization) |
| 261 | return; |
| 262 | |
| 263 | CFG *cfg = AC.getCFG(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 264 | if (!cfg) return; |
Richard Trieu | 2f024f4 | 2013-12-21 02:33:43 +0000 | [diff] [blame] | 265 | |
Robert Widmann | 04306d62 | 2019-02-13 22:22:23 +0000 | [diff] [blame] | 266 | // If the exit block is unreachable, skip processing the function. |
| 267 | if (cfg->getExit().pred_empty()) |
| 268 | return; |
| 269 | |
Richard Trieu | 6995de9 | 2015-08-21 03:43:09 +0000 | [diff] [blame] | 270 | // Emit diagnostic if a recursive function call is detected for all paths. |
| 271 | if (checkForRecursiveFunctionCall(FD, cfg)) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 272 | S.Diag(Body->getBeginLoc(), diag::warn_infinite_recursive_function); |
Richard Trieu | 2f024f4 | 2013-12-21 02:33:43 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | //===----------------------------------------------------------------------===// |
Erich Keane | 89fe9c2 | 2017-06-23 20:22:19 +0000 | [diff] [blame] | 276 | // Check for throw in a non-throwing function. |
| 277 | //===----------------------------------------------------------------------===// |
Erich Keane | 89fe9c2 | 2017-06-23 20:22:19 +0000 | [diff] [blame] | 278 | |
Richard Smith | 0848210 | 2018-02-20 02:32:30 +0000 | [diff] [blame] | 279 | /// Determine whether an exception thrown by E, unwinding from ThrowBlock, |
| 280 | /// can reach ExitBlock. |
| 281 | static bool throwEscapes(Sema &S, const CXXThrowExpr *E, CFGBlock &ThrowBlock, |
| 282 | CFG *Body) { |
Erich Keane | 89fe9c2 | 2017-06-23 20:22:19 +0000 | [diff] [blame] | 283 | SmallVector<CFGBlock *, 16> Stack; |
Richard Smith | 0848210 | 2018-02-20 02:32:30 +0000 | [diff] [blame] | 284 | llvm::BitVector Queued(Body->getNumBlockIDs()); |
Erich Keane | 89fe9c2 | 2017-06-23 20:22:19 +0000 | [diff] [blame] | 285 | |
Richard Smith | 0848210 | 2018-02-20 02:32:30 +0000 | [diff] [blame] | 286 | Stack.push_back(&ThrowBlock); |
| 287 | Queued[ThrowBlock.getBlockID()] = true; |
| 288 | |
| 289 | while (!Stack.empty()) { |
| 290 | CFGBlock &UnwindBlock = *Stack.back(); |
| 291 | Stack.pop_back(); |
| 292 | |
| 293 | for (auto &Succ : UnwindBlock.succs()) { |
| 294 | if (!Succ.isReachable() || Queued[Succ->getBlockID()]) |
Erich Keane | 89fe9c2 | 2017-06-23 20:22:19 +0000 | [diff] [blame] | 295 | continue; |
| 296 | |
Richard Smith | 0848210 | 2018-02-20 02:32:30 +0000 | [diff] [blame] | 297 | if (Succ->getBlockID() == Body->getExit().getBlockID()) |
| 298 | return true; |
Erich Keane | 89fe9c2 | 2017-06-23 20:22:19 +0000 | [diff] [blame] | 299 | |
Richard Smith | 0848210 | 2018-02-20 02:32:30 +0000 | [diff] [blame] | 300 | if (auto *Catch = |
| 301 | dyn_cast_or_null<CXXCatchStmt>(Succ->getLabel())) { |
| 302 | QualType Caught = Catch->getCaughtType(); |
| 303 | if (Caught.isNull() || // catch (...) catches everything |
| 304 | !E->getSubExpr() || // throw; is considered cuaght by any handler |
| 305 | S.handlerCanCatch(Caught, E->getSubExpr()->getType())) |
| 306 | // Exception doesn't escape via this path. |
| 307 | break; |
| 308 | } else { |
| 309 | Stack.push_back(Succ); |
| 310 | Queued[Succ->getBlockID()] = true; |
Erich Keane | 89fe9c2 | 2017-06-23 20:22:19 +0000 | [diff] [blame] | 311 | } |
Richard Smith | 0848210 | 2018-02-20 02:32:30 +0000 | [diff] [blame] | 312 | } |
Erich Keane | 89fe9c2 | 2017-06-23 20:22:19 +0000 | [diff] [blame] | 313 | } |
Richard Smith | 0848210 | 2018-02-20 02:32:30 +0000 | [diff] [blame] | 314 | |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | static void visitReachableThrows( |
| 319 | CFG *BodyCFG, |
| 320 | llvm::function_ref<void(const CXXThrowExpr *, CFGBlock &)> Visit) { |
| 321 | llvm::BitVector Reachable(BodyCFG->getNumBlockIDs()); |
| 322 | clang::reachable_code::ScanReachableFromBlock(&BodyCFG->getEntry(), Reachable); |
| 323 | for (CFGBlock *B : *BodyCFG) { |
| 324 | if (!Reachable[B->getBlockID()]) |
| 325 | continue; |
| 326 | for (CFGElement &E : *B) { |
| 327 | Optional<CFGStmt> S = E.getAs<CFGStmt>(); |
| 328 | if (!S) |
| 329 | continue; |
| 330 | if (auto *Throw = dyn_cast<CXXThrowExpr>(S->getStmt())) |
| 331 | Visit(Throw, *B); |
| 332 | } |
| 333 | } |
Erich Keane | 89fe9c2 | 2017-06-23 20:22:19 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | static void EmitDiagForCXXThrowInNonThrowingFunc(Sema &S, SourceLocation OpLoc, |
| 337 | const FunctionDecl *FD) { |
Erich Keane | 7538b35 | 2017-07-05 16:43:45 +0000 | [diff] [blame] | 338 | if (!S.getSourceManager().isInSystemHeader(OpLoc) && |
| 339 | FD->getTypeSourceInfo()) { |
Erich Keane | 89fe9c2 | 2017-06-23 20:22:19 +0000 | [diff] [blame] | 340 | S.Diag(OpLoc, diag::warn_throw_in_noexcept_func) << FD; |
| 341 | if (S.getLangOpts().CPlusPlus11 && |
| 342 | (isa<CXXDestructorDecl>(FD) || |
| 343 | FD->getDeclName().getCXXOverloadedOperator() == OO_Delete || |
Erich Keane | 7538b35 | 2017-07-05 16:43:45 +0000 | [diff] [blame] | 344 | FD->getDeclName().getCXXOverloadedOperator() == OO_Array_Delete)) { |
| 345 | if (const auto *Ty = FD->getTypeSourceInfo()->getType()-> |
| 346 | getAs<FunctionProtoType>()) |
| 347 | S.Diag(FD->getLocation(), diag::note_throw_in_dtor) |
| 348 | << !isa<CXXDestructorDecl>(FD) << !Ty->hasExceptionSpec() |
| 349 | << FD->getExceptionSpecSourceRange(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 350 | } else |
Erich Keane | 7538b35 | 2017-07-05 16:43:45 +0000 | [diff] [blame] | 351 | S.Diag(FD->getLocation(), diag::note_throw_in_function) |
| 352 | << FD->getExceptionSpecSourceRange(); |
Erich Keane | 89fe9c2 | 2017-06-23 20:22:19 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
| 356 | static void checkThrowInNonThrowingFunc(Sema &S, const FunctionDecl *FD, |
| 357 | AnalysisDeclContext &AC) { |
| 358 | CFG *BodyCFG = AC.getCFG(); |
| 359 | if (!BodyCFG) |
| 360 | return; |
| 361 | if (BodyCFG->getExit().pred_empty()) |
| 362 | return; |
Richard Smith | 0848210 | 2018-02-20 02:32:30 +0000 | [diff] [blame] | 363 | visitReachableThrows(BodyCFG, [&](const CXXThrowExpr *Throw, CFGBlock &Block) { |
| 364 | if (throwEscapes(S, Throw, Block, BodyCFG)) |
| 365 | EmitDiagForCXXThrowInNonThrowingFunc(S, Throw->getThrowLoc(), FD); |
| 366 | }); |
Erich Keane | 89fe9c2 | 2017-06-23 20:22:19 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | static bool isNoexcept(const FunctionDecl *FD) { |
| 370 | const auto *FPT = FD->getType()->castAs<FunctionProtoType>(); |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 371 | if (FPT->isNothrow() || FD->hasAttr<NoThrowAttr>()) |
Erich Keane | 89fe9c2 | 2017-06-23 20:22:19 +0000 | [diff] [blame] | 372 | return true; |
| 373 | return false; |
| 374 | } |
| 375 | |
| 376 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 377 | // Check for missing return value. |
| 378 | //===----------------------------------------------------------------------===// |
| 379 | |
John McCall | 5c6ec8c | 2010-05-16 09:34:11 +0000 | [diff] [blame] | 380 | enum ControlFlowKind { |
| 381 | UnknownFallThrough, |
| 382 | NeverFallThrough, |
| 383 | MaybeFallThrough, |
| 384 | AlwaysFallThrough, |
| 385 | NeverFallThroughOrReturn |
| 386 | }; |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 387 | |
| 388 | /// CheckFallThrough - Check that we don't fall off the end of a |
| 389 | /// Statement that should return a value. |
| 390 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 391 | /// \returns AlwaysFallThrough iff we always fall off the end of the statement, |
| 392 | /// MaybeFallThrough iff we might or might not fall off the end, |
| 393 | /// NeverFallThroughOrReturn iff we never fall off the end of the statement or |
| 394 | /// return. We assume NeverFallThrough iff we never fall off the end of the |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 395 | /// statement but we may return. We assume that functions not marked noreturn |
| 396 | /// will return. |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 397 | static ControlFlowKind CheckFallThrough(AnalysisDeclContext &AC) { |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 398 | CFG *cfg = AC.getCFG(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 399 | if (!cfg) return UnknownFallThrough; |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 400 | |
| 401 | // The CFG leaves in dead things, and we don't want the dead code paths to |
| 402 | // confuse us, so we mark all live things first. |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 403 | llvm::BitVector live(cfg->getNumBlockIDs()); |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 404 | unsigned count = reachable_code::ScanReachableFromBlock(&cfg->getEntry(), |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 405 | live); |
| 406 | |
| 407 | bool AddEHEdges = AC.getAddEHEdges(); |
| 408 | if (!AddEHEdges && count != cfg->getNumBlockIDs()) |
| 409 | // When there are things remaining dead, and we didn't add EH edges |
| 410 | // from CallExprs to the catch clauses, we have to go back and |
| 411 | // mark them as live. |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 412 | for (const auto *B : *cfg) { |
| 413 | if (!live[B->getBlockID()]) { |
| 414 | if (B->pred_begin() == B->pred_end()) { |
Artem Dergachev | 4e53032 | 2019-05-24 01:34:22 +0000 | [diff] [blame] | 415 | const Stmt *Term = B->getTerminatorStmt(); |
| 416 | if (Term && isa<CXXTryStmt>(Term)) |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 417 | // When not adding EH edges from calls, catch clauses |
| 418 | // can otherwise seem dead. Avoid noting them as dead. |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 419 | count += reachable_code::ScanReachableFromBlock(B, live); |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 420 | continue; |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | // Now we know what is live, we check the live precessors of the exit block |
| 426 | // and look for fall through paths, being careful to ignore normal returns, |
| 427 | // and exceptional paths. |
| 428 | bool HasLiveReturn = false; |
| 429 | bool HasFakeEdge = false; |
| 430 | bool HasPlainEdge = false; |
| 431 | bool HasAbnormalEdge = false; |
Ted Kremenek | 5020574 | 2010-09-09 00:06:07 +0000 | [diff] [blame] | 432 | |
| 433 | // Ignore default cases that aren't likely to be reachable because all |
| 434 | // enums in a switch(X) have explicit case statements. |
| 435 | CFGBlock::FilterOptions FO; |
| 436 | FO.IgnoreDefaultsWithCoveredEnums = 1; |
| 437 | |
Fangrui Song | 99337e2 | 2018-07-20 08:19:20 +0000 | [diff] [blame] | 438 | for (CFGBlock::filtered_pred_iterator I = |
| 439 | cfg->getExit().filtered_pred_start_end(FO); |
| 440 | I.hasMore(); ++I) { |
| 441 | const CFGBlock &B = **I; |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 442 | if (!live[B.getBlockID()]) |
| 443 | continue; |
Ted Kremenek | 5d06849 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 444 | |
Chandler Carruth | 03faf78 | 2011-09-13 09:53:58 +0000 | [diff] [blame] | 445 | // Skip blocks which contain an element marked as no-return. They don't |
| 446 | // represent actually viable edges into the exit block, so mark them as |
| 447 | // abnormal. |
| 448 | if (B.hasNoReturnElement()) { |
| 449 | HasAbnormalEdge = true; |
| 450 | continue; |
| 451 | } |
| 452 | |
Ted Kremenek | 5d06849 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 453 | // Destructors can appear after the 'return' in the CFG. This is |
| 454 | // normal. We need to look pass the destructors for the return |
| 455 | // statement (if it exists). |
| 456 | CFGBlock::const_reverse_iterator ri = B.rbegin(), re = B.rend(); |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 457 | |
Chandler Carruth | 03faf78 | 2011-09-13 09:53:58 +0000 | [diff] [blame] | 458 | for ( ; ri != re ; ++ri) |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 459 | if (ri->getAs<CFGStmt>()) |
Ted Kremenek | 5d06849 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 460 | break; |
Chandler Carruth | 03faf78 | 2011-09-13 09:53:58 +0000 | [diff] [blame] | 461 | |
Ted Kremenek | 5d06849 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 462 | // No more CFGElements in the block? |
| 463 | if (ri == re) { |
Artem Dergachev | 4e53032 | 2019-05-24 01:34:22 +0000 | [diff] [blame] | 464 | const Stmt *Term = B.getTerminatorStmt(); |
| 465 | if (Term && isa<CXXTryStmt>(Term)) { |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 466 | HasAbnormalEdge = true; |
| 467 | continue; |
| 468 | } |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 469 | // A labeled empty statement, or the entry block... |
| 470 | HasPlainEdge = true; |
| 471 | continue; |
| 472 | } |
Ted Kremenek | ebe6260 | 2011-01-25 22:50:47 +0000 | [diff] [blame] | 473 | |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 474 | CFGStmt CS = ri->castAs<CFGStmt>(); |
Ted Kremenek | adfb445 | 2011-08-23 23:05:04 +0000 | [diff] [blame] | 475 | const Stmt *S = CS.getStmt(); |
Eric Fiselier | da8f9b5 | 2017-05-25 02:16:53 +0000 | [diff] [blame] | 476 | if (isa<ReturnStmt>(S) || isa<CoreturnStmt>(S)) { |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 477 | HasLiveReturn = true; |
| 478 | continue; |
| 479 | } |
| 480 | if (isa<ObjCAtThrowStmt>(S)) { |
| 481 | HasFakeEdge = true; |
| 482 | continue; |
| 483 | } |
| 484 | if (isa<CXXThrowExpr>(S)) { |
| 485 | HasFakeEdge = true; |
| 486 | continue; |
| 487 | } |
Chad Rosier | 3250302 | 2012-06-11 20:47:18 +0000 | [diff] [blame] | 488 | if (isa<MSAsmStmt>(S)) { |
| 489 | // TODO: Verify this is correct. |
| 490 | HasFakeEdge = true; |
| 491 | HasLiveReturn = true; |
| 492 | continue; |
| 493 | } |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 494 | if (isa<CXXTryStmt>(S)) { |
| 495 | HasAbnormalEdge = true; |
| 496 | continue; |
| 497 | } |
Chandler Carruth | 03faf78 | 2011-09-13 09:53:58 +0000 | [diff] [blame] | 498 | if (std::find(B.succ_begin(), B.succ_end(), &cfg->getExit()) |
| 499 | == B.succ_end()) { |
| 500 | HasAbnormalEdge = true; |
| 501 | continue; |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 502 | } |
Chandler Carruth | 03faf78 | 2011-09-13 09:53:58 +0000 | [diff] [blame] | 503 | |
| 504 | HasPlainEdge = true; |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 505 | } |
| 506 | if (!HasPlainEdge) { |
| 507 | if (HasLiveReturn) |
| 508 | return NeverFallThrough; |
| 509 | return NeverFallThroughOrReturn; |
| 510 | } |
| 511 | if (HasAbnormalEdge || HasFakeEdge || HasLiveReturn) |
| 512 | return MaybeFallThrough; |
| 513 | // This says AlwaysFallThrough for calls to functions that are not marked |
| 514 | // noreturn, that don't return. If people would like this warning to be more |
| 515 | // accurate, such functions should be marked as noreturn. |
| 516 | return AlwaysFallThrough; |
| 517 | } |
| 518 | |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 519 | namespace { |
| 520 | |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 521 | struct CheckFallThroughDiagnostics { |
| 522 | unsigned diag_MaybeFallThrough_HasNoReturn; |
| 523 | unsigned diag_MaybeFallThrough_ReturnsNonVoid; |
| 524 | unsigned diag_AlwaysFallThrough_HasNoReturn; |
| 525 | unsigned diag_AlwaysFallThrough_ReturnsNonVoid; |
| 526 | unsigned diag_NeverFallThroughOrReturn; |
Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 527 | enum { Function, Block, Lambda, Coroutine } funMode; |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 528 | SourceLocation FuncLoc; |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 529 | |
Douglas Gregor | 24f2769 | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 530 | static CheckFallThroughDiagnostics MakeForFunction(const Decl *Func) { |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 531 | CheckFallThroughDiagnostics D; |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 532 | D.FuncLoc = Func->getLocation(); |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 533 | D.diag_MaybeFallThrough_HasNoReturn = |
| 534 | diag::warn_falloff_noreturn_function; |
| 535 | D.diag_MaybeFallThrough_ReturnsNonVoid = |
| 536 | diag::warn_maybe_falloff_nonvoid_function; |
| 537 | D.diag_AlwaysFallThrough_HasNoReturn = |
| 538 | diag::warn_falloff_noreturn_function; |
| 539 | D.diag_AlwaysFallThrough_ReturnsNonVoid = |
| 540 | diag::warn_falloff_nonvoid_function; |
Douglas Gregor | 24f2769 | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 541 | |
| 542 | // Don't suggest that virtual functions be marked "noreturn", since they |
| 543 | // might be overridden by non-noreturn functions. |
| 544 | bool isVirtualMethod = false; |
| 545 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Func)) |
| 546 | isVirtualMethod = Method->isVirtual(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 547 | |
Douglas Gregor | 0de5720 | 2011-10-10 18:15:57 +0000 | [diff] [blame] | 548 | // Don't suggest that template instantiations be marked "noreturn" |
| 549 | bool isTemplateInstantiation = false; |
Ted Kremenek | 85825ae | 2011-12-01 00:59:17 +0000 | [diff] [blame] | 550 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(Func)) |
| 551 | isTemplateInstantiation = Function->isTemplateInstantiation(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 552 | |
Douglas Gregor | 0de5720 | 2011-10-10 18:15:57 +0000 | [diff] [blame] | 553 | if (!isVirtualMethod && !isTemplateInstantiation) |
Douglas Gregor | 24f2769 | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 554 | D.diag_NeverFallThroughOrReturn = |
| 555 | diag::warn_suggest_noreturn_function; |
| 556 | else |
| 557 | D.diag_NeverFallThroughOrReturn = 0; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 558 | |
Douglas Gregor | cf11eb7 | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 559 | D.funMode = Function; |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 560 | return D; |
| 561 | } |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 562 | |
Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 563 | static CheckFallThroughDiagnostics MakeForCoroutine(const Decl *Func) { |
| 564 | CheckFallThroughDiagnostics D; |
| 565 | D.FuncLoc = Func->getLocation(); |
| 566 | D.diag_MaybeFallThrough_HasNoReturn = 0; |
| 567 | D.diag_MaybeFallThrough_ReturnsNonVoid = |
| 568 | diag::warn_maybe_falloff_nonvoid_coroutine; |
| 569 | D.diag_AlwaysFallThrough_HasNoReturn = 0; |
| 570 | D.diag_AlwaysFallThrough_ReturnsNonVoid = |
| 571 | diag::warn_falloff_nonvoid_coroutine; |
| 572 | D.funMode = Coroutine; |
| 573 | return D; |
| 574 | } |
| 575 | |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 576 | static CheckFallThroughDiagnostics MakeForBlock() { |
| 577 | CheckFallThroughDiagnostics D; |
| 578 | D.diag_MaybeFallThrough_HasNoReturn = |
| 579 | diag::err_noreturn_block_has_return_expr; |
| 580 | D.diag_MaybeFallThrough_ReturnsNonVoid = |
| 581 | diag::err_maybe_falloff_nonvoid_block; |
| 582 | D.diag_AlwaysFallThrough_HasNoReturn = |
| 583 | diag::err_noreturn_block_has_return_expr; |
| 584 | D.diag_AlwaysFallThrough_ReturnsNonVoid = |
| 585 | diag::err_falloff_nonvoid_block; |
Fariborz Jahanian | 5ce2279 | 2014-04-03 23:06:35 +0000 | [diff] [blame] | 586 | D.diag_NeverFallThroughOrReturn = 0; |
Douglas Gregor | cf11eb7 | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 587 | D.funMode = Block; |
| 588 | return D; |
| 589 | } |
| 590 | |
| 591 | static CheckFallThroughDiagnostics MakeForLambda() { |
| 592 | CheckFallThroughDiagnostics D; |
| 593 | D.diag_MaybeFallThrough_HasNoReturn = |
| 594 | diag::err_noreturn_lambda_has_return_expr; |
| 595 | D.diag_MaybeFallThrough_ReturnsNonVoid = |
| 596 | diag::warn_maybe_falloff_nonvoid_lambda; |
| 597 | D.diag_AlwaysFallThrough_HasNoReturn = |
| 598 | diag::err_noreturn_lambda_has_return_expr; |
| 599 | D.diag_AlwaysFallThrough_ReturnsNonVoid = |
| 600 | diag::warn_falloff_nonvoid_lambda; |
| 601 | D.diag_NeverFallThroughOrReturn = 0; |
| 602 | D.funMode = Lambda; |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 603 | return D; |
| 604 | } |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 605 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 606 | bool checkDiagnostics(DiagnosticsEngine &D, bool ReturnsVoid, |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 607 | bool HasNoReturn) const { |
Douglas Gregor | cf11eb7 | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 608 | if (funMode == Function) { |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 609 | return (ReturnsVoid || |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 610 | D.isIgnored(diag::warn_maybe_falloff_nonvoid_function, |
| 611 | FuncLoc)) && |
| 612 | (!HasNoReturn || |
| 613 | D.isIgnored(diag::warn_noreturn_function_has_return_expr, |
| 614 | FuncLoc)) && |
| 615 | (!ReturnsVoid || |
| 616 | D.isIgnored(diag::warn_suggest_noreturn_block, FuncLoc)); |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 617 | } |
Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 618 | if (funMode == Coroutine) { |
| 619 | return (ReturnsVoid || |
| 620 | D.isIgnored(diag::warn_maybe_falloff_nonvoid_function, FuncLoc) || |
| 621 | D.isIgnored(diag::warn_maybe_falloff_nonvoid_coroutine, |
| 622 | FuncLoc)) && |
| 623 | (!HasNoReturn); |
| 624 | } |
Douglas Gregor | cf11eb7 | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 625 | // For blocks / lambdas. |
Fariborz Jahanian | 5ce2279 | 2014-04-03 23:06:35 +0000 | [diff] [blame] | 626 | return ReturnsVoid && !HasNoReturn; |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 627 | } |
| 628 | }; |
| 629 | |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 630 | } // anonymous namespace |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 631 | |
Reid Kleckner | 87a3180 | 2018-03-12 21:43:02 +0000 | [diff] [blame] | 632 | /// CheckFallThroughForBody - Check that we don't fall off the end of a |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 633 | /// function that should return a value. Check that we don't fall off the end |
| 634 | /// of a noreturn function. We assume that functions and blocks not marked |
| 635 | /// noreturn will return. |
| 636 | static void CheckFallThroughForBody(Sema &S, const Decl *D, const Stmt *Body, |
Richard Smith | 2fdd95c | 2019-05-31 00:45:09 +0000 | [diff] [blame] | 637 | QualType BlockType, |
Reid Kleckner | 87a3180 | 2018-03-12 21:43:02 +0000 | [diff] [blame] | 638 | const CheckFallThroughDiagnostics &CD, |
| 639 | AnalysisDeclContext &AC, |
| 640 | sema::FunctionScopeInfo *FSI) { |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 641 | |
| 642 | bool ReturnsVoid = false; |
| 643 | bool HasNoReturn = false; |
Reid Kleckner | 87a3180 | 2018-03-12 21:43:02 +0000 | [diff] [blame] | 644 | bool IsCoroutine = FSI->isCoroutine(); |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 645 | |
Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 646 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 647 | if (const auto *CBody = dyn_cast<CoroutineBodyStmt>(Body)) |
| 648 | ReturnsVoid = CBody->getFallthroughHandler() != nullptr; |
| 649 | else |
| 650 | ReturnsVoid = FD->getReturnType()->isVoidType(); |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 651 | HasNoReturn = FD->isNoReturn(); |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 652 | } |
Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 653 | else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 654 | ReturnsVoid = MD->getReturnType()->isVoidType(); |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 655 | HasNoReturn = MD->hasAttr<NoReturnAttr>(); |
| 656 | } |
| 657 | else if (isa<BlockDecl>(D)) { |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 658 | if (const FunctionType *FT = |
Richard Smith | 2fdd95c | 2019-05-31 00:45:09 +0000 | [diff] [blame] | 659 | BlockType->getPointeeType()->getAs<FunctionType>()) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 660 | if (FT->getReturnType()->isVoidType()) |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 661 | ReturnsVoid = true; |
| 662 | if (FT->getNoReturnAttr()) |
| 663 | HasNoReturn = true; |
| 664 | } |
| 665 | } |
| 666 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 667 | DiagnosticsEngine &Diags = S.getDiagnostics(); |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 668 | |
| 669 | // Short circuit for compilation speed. |
| 670 | if (CD.checkDiagnostics(Diags, ReturnsVoid, HasNoReturn)) |
| 671 | return; |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 672 | SourceLocation LBrace = Body->getBeginLoc(), RBrace = Body->getEndLoc(); |
Eric Fiselier | da8f9b5 | 2017-05-25 02:16:53 +0000 | [diff] [blame] | 673 | auto EmitDiag = [&](SourceLocation Loc, unsigned DiagID) { |
| 674 | if (IsCoroutine) |
Reid Kleckner | 87a3180 | 2018-03-12 21:43:02 +0000 | [diff] [blame] | 675 | S.Diag(Loc, DiagID) << FSI->CoroutinePromise->getType(); |
Eric Fiselier | da8f9b5 | 2017-05-25 02:16:53 +0000 | [diff] [blame] | 676 | else |
| 677 | S.Diag(Loc, DiagID); |
| 678 | }; |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 679 | |
| 680 | // cpu_dispatch functions permit empty function bodies for ICC compatibility. |
| 681 | if (D->getAsFunction() && D->getAsFunction()->isCPUDispatchMultiVersion()) |
| 682 | return; |
| 683 | |
Aaron Ballman | b2e2c1b | 2014-10-24 13:19:19 +0000 | [diff] [blame] | 684 | // Either in a function body compound statement, or a function-try-block. |
| 685 | switch (CheckFallThrough(AC)) { |
| 686 | case UnknownFallThrough: |
| 687 | break; |
John McCall | 5c6ec8c | 2010-05-16 09:34:11 +0000 | [diff] [blame] | 688 | |
Aaron Ballman | b2e2c1b | 2014-10-24 13:19:19 +0000 | [diff] [blame] | 689 | case MaybeFallThrough: |
| 690 | if (HasNoReturn) |
Eric Fiselier | da8f9b5 | 2017-05-25 02:16:53 +0000 | [diff] [blame] | 691 | EmitDiag(RBrace, CD.diag_MaybeFallThrough_HasNoReturn); |
Aaron Ballman | b2e2c1b | 2014-10-24 13:19:19 +0000 | [diff] [blame] | 692 | else if (!ReturnsVoid) |
Eric Fiselier | da8f9b5 | 2017-05-25 02:16:53 +0000 | [diff] [blame] | 693 | EmitDiag(RBrace, CD.diag_MaybeFallThrough_ReturnsNonVoid); |
Aaron Ballman | b2e2c1b | 2014-10-24 13:19:19 +0000 | [diff] [blame] | 694 | break; |
| 695 | case AlwaysFallThrough: |
| 696 | if (HasNoReturn) |
Eric Fiselier | da8f9b5 | 2017-05-25 02:16:53 +0000 | [diff] [blame] | 697 | EmitDiag(RBrace, CD.diag_AlwaysFallThrough_HasNoReturn); |
Aaron Ballman | b2e2c1b | 2014-10-24 13:19:19 +0000 | [diff] [blame] | 698 | else if (!ReturnsVoid) |
Eric Fiselier | da8f9b5 | 2017-05-25 02:16:53 +0000 | [diff] [blame] | 699 | EmitDiag(RBrace, CD.diag_AlwaysFallThrough_ReturnsNonVoid); |
Aaron Ballman | b2e2c1b | 2014-10-24 13:19:19 +0000 | [diff] [blame] | 700 | break; |
| 701 | case NeverFallThroughOrReturn: |
| 702 | if (ReturnsVoid && !HasNoReturn && CD.diag_NeverFallThroughOrReturn) { |
| 703 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 704 | S.Diag(LBrace, CD.diag_NeverFallThroughOrReturn) << 0 << FD; |
| 705 | } else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 706 | S.Diag(LBrace, CD.diag_NeverFallThroughOrReturn) << 1 << MD; |
| 707 | } else { |
| 708 | S.Diag(LBrace, CD.diag_NeverFallThroughOrReturn); |
Chandler Carruth | c841b6e | 2011-08-31 09:01:53 +0000 | [diff] [blame] | 709 | } |
Aaron Ballman | b2e2c1b | 2014-10-24 13:19:19 +0000 | [diff] [blame] | 710 | } |
| 711 | break; |
| 712 | case NeverFallThrough: |
| 713 | break; |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
| 717 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 718 | // -Wuninitialized |
| 719 | //===----------------------------------------------------------------------===// |
| 720 | |
Ted Kremenek | b8d8c4e | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 721 | namespace { |
Chandler Carruth | 4e02182 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 722 | /// ContainsReference - A visitor class to search for references to |
| 723 | /// a particular declaration (the needle) within any evaluated component of an |
| 724 | /// expression (recursively). |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 725 | class ContainsReference : public ConstEvaluatedExprVisitor<ContainsReference> { |
Chandler Carruth | 4e02182 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 726 | bool FoundReference; |
| 727 | const DeclRefExpr *Needle; |
| 728 | |
Ted Kremenek | b8d8c4e | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 729 | public: |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 730 | typedef ConstEvaluatedExprVisitor<ContainsReference> Inherited; |
Chandler Carruth | 4e02182 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 731 | |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 732 | ContainsReference(ASTContext &Context, const DeclRefExpr *Needle) |
| 733 | : Inherited(Context), FoundReference(false), Needle(Needle) {} |
| 734 | |
| 735 | void VisitExpr(const Expr *E) { |
Ted Kremenek | b8d8c4e | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 736 | // Stop evaluating if we already have a reference. |
Chandler Carruth | 4e02182 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 737 | if (FoundReference) |
Ted Kremenek | b8d8c4e | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 738 | return; |
Chandler Carruth | 4e02182 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 739 | |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 740 | Inherited::VisitExpr(E); |
Ted Kremenek | b8d8c4e | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 741 | } |
Chandler Carruth | 4e02182 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 742 | |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 743 | void VisitDeclRefExpr(const DeclRefExpr *E) { |
Chandler Carruth | 4e02182 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 744 | if (E == Needle) |
| 745 | FoundReference = true; |
| 746 | else |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 747 | Inherited::VisitDeclRefExpr(E); |
Ted Kremenek | b8d8c4e | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 748 | } |
Chandler Carruth | 4e02182 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 749 | |
| 750 | bool doesContainReference() const { return FoundReference; } |
Ted Kremenek | b8d8c4e | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 751 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 752 | } // anonymous namespace |
Ted Kremenek | b8d8c4e | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 753 | |
David Blaikie | e5f9a9e | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 754 | static bool SuggestInitializationFixit(Sema &S, const VarDecl *VD) { |
Fariborz Jahanian | 429fadb | 2012-03-08 00:22:50 +0000 | [diff] [blame] | 755 | QualType VariableTy = VD->getType().getCanonicalType(); |
| 756 | if (VariableTy->isBlockPointerType() && |
| 757 | !VD->hasAttr<BlocksAttr>()) { |
Nico Weber | 3c68ee9 | 2014-07-08 23:46:20 +0000 | [diff] [blame] | 758 | S.Diag(VD->getLocation(), diag::note_block_var_fixit_add_initialization) |
| 759 | << VD->getDeclName() |
| 760 | << FixItHint::CreateInsertion(VD->getLocation(), "__block "); |
Fariborz Jahanian | 429fadb | 2012-03-08 00:22:50 +0000 | [diff] [blame] | 761 | return true; |
| 762 | } |
Richard Smith | f7ec86a | 2013-09-20 00:27:40 +0000 | [diff] [blame] | 763 | |
David Blaikie | e5f9a9e | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 764 | // Don't issue a fixit if there is already an initializer. |
| 765 | if (VD->getInit()) |
| 766 | return false; |
Richard Trieu | 2cdcf82 | 2012-05-03 01:09:59 +0000 | [diff] [blame] | 767 | |
| 768 | // Don't suggest a fixit inside macros. |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 769 | if (VD->getEndLoc().isMacroID()) |
Richard Trieu | 2cdcf82 | 2012-05-03 01:09:59 +0000 | [diff] [blame] | 770 | return false; |
| 771 | |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 772 | SourceLocation Loc = S.getLocForEndOfToken(VD->getEndLoc()); |
Richard Smith | f7ec86a | 2013-09-20 00:27:40 +0000 | [diff] [blame] | 773 | |
| 774 | // Suggest possible initialization (if any). |
| 775 | std::string Init = S.getFixItZeroInitializerForType(VariableTy, Loc); |
| 776 | if (Init.empty()) |
| 777 | return false; |
| 778 | |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 779 | S.Diag(Loc, diag::note_var_fixit_add_initialization) << VD->getDeclName() |
| 780 | << FixItHint::CreateInsertion(Loc, Init); |
| 781 | return true; |
David Blaikie | e5f9a9e | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 784 | /// Create a fixit to remove an if-like statement, on the assumption that its |
| 785 | /// condition is CondVal. |
| 786 | static void CreateIfFixit(Sema &S, const Stmt *If, const Stmt *Then, |
| 787 | const Stmt *Else, bool CondVal, |
| 788 | FixItHint &Fixit1, FixItHint &Fixit2) { |
| 789 | if (CondVal) { |
| 790 | // If condition is always true, remove all but the 'then'. |
| 791 | Fixit1 = FixItHint::CreateRemoval( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 792 | CharSourceRange::getCharRange(If->getBeginLoc(), Then->getBeginLoc())); |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 793 | if (Else) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 794 | SourceLocation ElseKwLoc = S.getLocForEndOfToken(Then->getEndLoc()); |
| 795 | Fixit2 = |
| 796 | FixItHint::CreateRemoval(SourceRange(ElseKwLoc, Else->getEndLoc())); |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 797 | } |
| 798 | } else { |
| 799 | // If condition is always false, remove all but the 'else'. |
| 800 | if (Else) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 801 | Fixit1 = FixItHint::CreateRemoval(CharSourceRange::getCharRange( |
| 802 | If->getBeginLoc(), Else->getBeginLoc())); |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 803 | else |
| 804 | Fixit1 = FixItHint::CreateRemoval(If->getSourceRange()); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | /// DiagUninitUse -- Helper function to produce a diagnostic for an |
| 809 | /// uninitialized use of a variable. |
| 810 | static void DiagUninitUse(Sema &S, const VarDecl *VD, const UninitUse &Use, |
| 811 | bool IsCapturedByBlock) { |
| 812 | bool Diagnosed = false; |
| 813 | |
Richard Smith | ba8071e | 2013-09-12 18:49:10 +0000 | [diff] [blame] | 814 | switch (Use.getKind()) { |
| 815 | case UninitUse::Always: |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 816 | S.Diag(Use.getUser()->getBeginLoc(), diag::warn_uninit_var) |
Richard Smith | ba8071e | 2013-09-12 18:49:10 +0000 | [diff] [blame] | 817 | << VD->getDeclName() << IsCapturedByBlock |
| 818 | << Use.getUser()->getSourceRange(); |
| 819 | return; |
| 820 | |
| 821 | case UninitUse::AfterDecl: |
| 822 | case UninitUse::AfterCall: |
| 823 | S.Diag(VD->getLocation(), diag::warn_sometimes_uninit_var) |
| 824 | << VD->getDeclName() << IsCapturedByBlock |
| 825 | << (Use.getKind() == UninitUse::AfterDecl ? 4 : 5) |
| 826 | << const_cast<DeclContext*>(VD->getLexicalDeclContext()) |
| 827 | << VD->getSourceRange(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 828 | S.Diag(Use.getUser()->getBeginLoc(), diag::note_uninit_var_use) |
| 829 | << IsCapturedByBlock << Use.getUser()->getSourceRange(); |
Richard Smith | ba8071e | 2013-09-12 18:49:10 +0000 | [diff] [blame] | 830 | return; |
| 831 | |
| 832 | case UninitUse::Maybe: |
| 833 | case UninitUse::Sometimes: |
| 834 | // Carry on to report sometimes-uninitialized branches, if possible, |
| 835 | // or a 'may be used uninitialized' diagnostic otherwise. |
| 836 | break; |
| 837 | } |
| 838 | |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 839 | // Diagnose each branch which leads to a sometimes-uninitialized use. |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 840 | for (UninitUse::branch_iterator I = Use.branch_begin(), E = Use.branch_end(); |
| 841 | I != E; ++I) { |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 842 | assert(Use.getKind() == UninitUse::Sometimes); |
| 843 | |
| 844 | const Expr *User = Use.getUser(); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 845 | const Stmt *Term = I->Terminator; |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 846 | |
| 847 | // Information used when building the diagnostic. |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 848 | unsigned DiagKind; |
David Blaikie | 1d202a6 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 849 | StringRef Str; |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 850 | SourceRange Range; |
| 851 | |
Stefanus Du Toit | b331850 | 2013-03-01 21:41:22 +0000 | [diff] [blame] | 852 | // FixIts to suppress the diagnostic by removing the dead condition. |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 853 | // For all binary terminators, branch 0 is taken if the condition is true, |
| 854 | // and branch 1 is taken if the condition is false. |
| 855 | int RemoveDiagKind = -1; |
| 856 | const char *FixitStr = |
| 857 | S.getLangOpts().CPlusPlus ? (I->Output ? "true" : "false") |
| 858 | : (I->Output ? "1" : "0"); |
| 859 | FixItHint Fixit1, Fixit2; |
| 860 | |
Richard Smith | ba8071e | 2013-09-12 18:49:10 +0000 | [diff] [blame] | 861 | switch (Term ? Term->getStmtClass() : Stmt::DeclStmtClass) { |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 862 | default: |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 863 | // Don't know how to report this. Just fall back to 'may be used |
Richard Smith | ba8071e | 2013-09-12 18:49:10 +0000 | [diff] [blame] | 864 | // uninitialized'. FIXME: Can this happen? |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 865 | continue; |
| 866 | |
| 867 | // "condition is true / condition is false". |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 868 | case Stmt::IfStmtClass: { |
| 869 | const IfStmt *IS = cast<IfStmt>(Term); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 870 | DiagKind = 0; |
| 871 | Str = "if"; |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 872 | Range = IS->getCond()->getSourceRange(); |
| 873 | RemoveDiagKind = 0; |
| 874 | CreateIfFixit(S, IS, IS->getThen(), IS->getElse(), |
| 875 | I->Output, Fixit1, Fixit2); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 876 | break; |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 877 | } |
| 878 | case Stmt::ConditionalOperatorClass: { |
| 879 | const ConditionalOperator *CO = cast<ConditionalOperator>(Term); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 880 | DiagKind = 0; |
| 881 | Str = "?:"; |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 882 | Range = CO->getCond()->getSourceRange(); |
| 883 | RemoveDiagKind = 0; |
| 884 | CreateIfFixit(S, CO, CO->getTrueExpr(), CO->getFalseExpr(), |
| 885 | I->Output, Fixit1, Fixit2); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 886 | break; |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 887 | } |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 888 | case Stmt::BinaryOperatorClass: { |
| 889 | const BinaryOperator *BO = cast<BinaryOperator>(Term); |
| 890 | if (!BO->isLogicalOp()) |
| 891 | continue; |
| 892 | DiagKind = 0; |
| 893 | Str = BO->getOpcodeStr(); |
| 894 | Range = BO->getLHS()->getSourceRange(); |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 895 | RemoveDiagKind = 0; |
| 896 | if ((BO->getOpcode() == BO_LAnd && I->Output) || |
| 897 | (BO->getOpcode() == BO_LOr && !I->Output)) |
| 898 | // true && y -> y, false || y -> y. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 899 | Fixit1 = FixItHint::CreateRemoval( |
| 900 | SourceRange(BO->getBeginLoc(), BO->getOperatorLoc())); |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 901 | else |
| 902 | // false && y -> false, true || y -> true. |
| 903 | Fixit1 = FixItHint::CreateReplacement(BO->getSourceRange(), FixitStr); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 904 | break; |
| 905 | } |
| 906 | |
| 907 | // "loop is entered / loop is exited". |
| 908 | case Stmt::WhileStmtClass: |
| 909 | DiagKind = 1; |
| 910 | Str = "while"; |
| 911 | Range = cast<WhileStmt>(Term)->getCond()->getSourceRange(); |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 912 | RemoveDiagKind = 1; |
| 913 | Fixit1 = FixItHint::CreateReplacement(Range, FixitStr); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 914 | break; |
| 915 | case Stmt::ForStmtClass: |
| 916 | DiagKind = 1; |
| 917 | Str = "for"; |
| 918 | Range = cast<ForStmt>(Term)->getCond()->getSourceRange(); |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 919 | RemoveDiagKind = 1; |
| 920 | if (I->Output) |
| 921 | Fixit1 = FixItHint::CreateRemoval(Range); |
| 922 | else |
| 923 | Fixit1 = FixItHint::CreateReplacement(Range, FixitStr); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 924 | break; |
Richard Smith | ba8071e | 2013-09-12 18:49:10 +0000 | [diff] [blame] | 925 | case Stmt::CXXForRangeStmtClass: |
| 926 | if (I->Output == 1) { |
| 927 | // The use occurs if a range-based for loop's body never executes. |
| 928 | // That may be impossible, and there's no syntactic fix for this, |
| 929 | // so treat it as a 'may be uninitialized' case. |
| 930 | continue; |
| 931 | } |
| 932 | DiagKind = 1; |
| 933 | Str = "for"; |
| 934 | Range = cast<CXXForRangeStmt>(Term)->getRangeInit()->getSourceRange(); |
| 935 | break; |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 936 | |
| 937 | // "condition is true / loop is exited". |
| 938 | case Stmt::DoStmtClass: |
| 939 | DiagKind = 2; |
| 940 | Str = "do"; |
| 941 | Range = cast<DoStmt>(Term)->getCond()->getSourceRange(); |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 942 | RemoveDiagKind = 1; |
| 943 | Fixit1 = FixItHint::CreateReplacement(Range, FixitStr); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 944 | break; |
| 945 | |
| 946 | // "switch case is taken". |
| 947 | case Stmt::CaseStmtClass: |
| 948 | DiagKind = 3; |
| 949 | Str = "case"; |
| 950 | Range = cast<CaseStmt>(Term)->getLHS()->getSourceRange(); |
| 951 | break; |
| 952 | case Stmt::DefaultStmtClass: |
| 953 | DiagKind = 3; |
| 954 | Str = "default"; |
| 955 | Range = cast<DefaultStmt>(Term)->getDefaultLoc(); |
| 956 | break; |
| 957 | } |
| 958 | |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 959 | S.Diag(Range.getBegin(), diag::warn_sometimes_uninit_var) |
| 960 | << VD->getDeclName() << IsCapturedByBlock << DiagKind |
| 961 | << Str << I->Output << Range; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 962 | S.Diag(User->getBeginLoc(), diag::note_uninit_var_use) |
| 963 | << IsCapturedByBlock << User->getSourceRange(); |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 964 | if (RemoveDiagKind != -1) |
| 965 | S.Diag(Fixit1.RemoveRange.getBegin(), diag::note_uninit_fixit_remove_cond) |
| 966 | << RemoveDiagKind << Str << I->Output << Fixit1 << Fixit2; |
| 967 | |
| 968 | Diagnosed = true; |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 969 | } |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 970 | |
| 971 | if (!Diagnosed) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 972 | S.Diag(Use.getUser()->getBeginLoc(), diag::warn_maybe_uninit_var) |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 973 | << VD->getDeclName() << IsCapturedByBlock |
| 974 | << Use.getUser()->getSourceRange(); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Chandler Carruth | dd8f0d0 | 2011-04-05 18:27:05 +0000 | [diff] [blame] | 977 | /// DiagnoseUninitializedUse -- Helper function for diagnosing uses of an |
| 978 | /// uninitialized variable. This manages the different forms of diagnostic |
| 979 | /// emitted for particular types of uses. Returns true if the use was diagnosed |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 980 | /// as a warning. If a particular use is one we omit warnings for, returns |
Chandler Carruth | dd8f0d0 | 2011-04-05 18:27:05 +0000 | [diff] [blame] | 981 | /// false. |
| 982 | static bool DiagnoseUninitializedUse(Sema &S, const VarDecl *VD, |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 983 | const UninitUse &Use, |
Ted Kremenek | 596fa16 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 984 | bool alwaysReportSelfInit = false) { |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 985 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Use.getUser())) { |
Richard Trieu | 43a2fc7 | 2012-05-09 21:08:22 +0000 | [diff] [blame] | 986 | // Inspect the initializer of the variable declaration which is |
| 987 | // being referenced prior to its initialization. We emit |
| 988 | // specialized diagnostics for self-initialization, and we |
| 989 | // specifically avoid warning about self references which take the |
| 990 | // form of: |
| 991 | // |
| 992 | // int x = x; |
| 993 | // |
| 994 | // This is used to indicate to GCC that 'x' is intentionally left |
| 995 | // uninitialized. Proven code paths which access 'x' in |
| 996 | // an uninitialized state after this will still warn. |
| 997 | if (const Expr *Initializer = VD->getInit()) { |
| 998 | if (!alwaysReportSelfInit && DRE == Initializer->IgnoreParenImpCasts()) |
| 999 | return false; |
Chandler Carruth | 895904da | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 1000 | |
Richard Trieu | 43a2fc7 | 2012-05-09 21:08:22 +0000 | [diff] [blame] | 1001 | ContainsReference CR(S.Context, DRE); |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 1002 | CR.Visit(Initializer); |
Richard Trieu | 43a2fc7 | 2012-05-09 21:08:22 +0000 | [diff] [blame] | 1003 | if (CR.doesContainReference()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1004 | S.Diag(DRE->getBeginLoc(), diag::warn_uninit_self_reference_in_init) |
| 1005 | << VD->getDeclName() << VD->getLocation() << DRE->getSourceRange(); |
Richard Trieu | 43a2fc7 | 2012-05-09 21:08:22 +0000 | [diff] [blame] | 1006 | return true; |
Chandler Carruth | 895904da | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 1007 | } |
Chandler Carruth | 895904da | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 1008 | } |
Richard Trieu | 43a2fc7 | 2012-05-09 21:08:22 +0000 | [diff] [blame] | 1009 | |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 1010 | DiagUninitUse(S, VD, Use, false); |
Chandler Carruth | 895904da | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 1011 | } else { |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 1012 | const BlockExpr *BE = cast<BlockExpr>(Use.getUser()); |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 1013 | if (VD->getType()->isBlockPointerType() && !VD->hasAttr<BlocksAttr>()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1014 | S.Diag(BE->getBeginLoc(), |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 1015 | diag::warn_uninit_byref_blockvar_captured_by_block) |
Akira Hatanaka | 53796d9 | 2019-04-23 23:52:02 +0000 | [diff] [blame] | 1016 | << VD->getDeclName() |
| 1017 | << VD->getType().getQualifiers().hasObjCLifetime(); |
Richard Smith | 1bb8edb8 | 2012-05-26 06:20:46 +0000 | [diff] [blame] | 1018 | else |
| 1019 | DiagUninitUse(S, VD, Use, true); |
Chandler Carruth | 895904da | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | // Report where the variable was declared when the use wasn't within |
David Blaikie | e5f9a9e | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 1023 | // the initializer of that declaration & we didn't already suggest |
| 1024 | // an initialization fixit. |
Richard Trieu | 43a2fc7 | 2012-05-09 21:08:22 +0000 | [diff] [blame] | 1025 | if (!SuggestInitializationFixit(S, VD)) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1026 | S.Diag(VD->getBeginLoc(), diag::note_var_declared_here) |
| 1027 | << VD->getDeclName(); |
Chandler Carruth | 895904da | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 1028 | |
Chandler Carruth | dd8f0d0 | 2011-04-05 18:27:05 +0000 | [diff] [blame] | 1029 | return true; |
Chandler Carruth | 7a03720 | 2011-04-05 18:18:08 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1032 | namespace { |
| 1033 | class FallthroughMapper : public RecursiveASTVisitor<FallthroughMapper> { |
| 1034 | public: |
| 1035 | FallthroughMapper(Sema &S) |
| 1036 | : FoundSwitchStatements(false), |
| 1037 | S(S) { |
| 1038 | } |
| 1039 | |
| 1040 | bool foundSwitchStatements() const { return FoundSwitchStatements; } |
| 1041 | |
| 1042 | void markFallthroughVisited(const AttributedStmt *Stmt) { |
| 1043 | bool Found = FallthroughStmts.erase(Stmt); |
| 1044 | assert(Found); |
Kaelyn Uhrain | 29a8eeb | 2012-05-03 19:46:38 +0000 | [diff] [blame] | 1045 | (void)Found; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | typedef llvm::SmallPtrSet<const AttributedStmt*, 8> AttrStmts; |
| 1049 | |
| 1050 | const AttrStmts &getFallthroughStmts() const { |
| 1051 | return FallthroughStmts; |
| 1052 | } |
| 1053 | |
Alexander Kornienko | afed1dd | 2013-01-30 03:49:44 +0000 | [diff] [blame] | 1054 | void fillReachableBlocks(CFG *Cfg) { |
| 1055 | assert(ReachableBlocks.empty() && "ReachableBlocks already filled"); |
| 1056 | std::deque<const CFGBlock *> BlockQueue; |
| 1057 | |
| 1058 | ReachableBlocks.insert(&Cfg->getEntry()); |
| 1059 | BlockQueue.push_back(&Cfg->getEntry()); |
Alexander Kornienko | c121b9b | 2013-02-07 02:17:19 +0000 | [diff] [blame] | 1060 | // Mark all case blocks reachable to avoid problems with switching on |
| 1061 | // constants, covered enums, etc. |
| 1062 | // These blocks can contain fall-through annotations, and we don't want to |
| 1063 | // issue a warn_fallthrough_attr_unreachable for them. |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1064 | for (const auto *B : *Cfg) { |
Alexander Kornienko | c121b9b | 2013-02-07 02:17:19 +0000 | [diff] [blame] | 1065 | const Stmt *L = B->getLabel(); |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 1066 | if (L && isa<SwitchCase>(L) && ReachableBlocks.insert(B).second) |
Alexander Kornienko | c121b9b | 2013-02-07 02:17:19 +0000 | [diff] [blame] | 1067 | BlockQueue.push_back(B); |
| 1068 | } |
| 1069 | |
Alexander Kornienko | afed1dd | 2013-01-30 03:49:44 +0000 | [diff] [blame] | 1070 | while (!BlockQueue.empty()) { |
| 1071 | const CFGBlock *P = BlockQueue.front(); |
| 1072 | BlockQueue.pop_front(); |
| 1073 | for (CFGBlock::const_succ_iterator I = P->succ_begin(), |
| 1074 | E = P->succ_end(); |
| 1075 | I != E; ++I) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 1076 | if (*I && ReachableBlocks.insert(*I).second) |
Alexander Kornienko | afed1dd | 2013-01-30 03:49:44 +0000 | [diff] [blame] | 1077 | BlockQueue.push_back(*I); |
| 1078 | } |
| 1079 | } |
| 1080 | } |
| 1081 | |
Richard Smith | 7532d37 | 2017-03-22 01:49:19 +0000 | [diff] [blame] | 1082 | bool checkFallThroughIntoBlock(const CFGBlock &B, int &AnnotatedCnt, |
| 1083 | bool IsTemplateInstantiation) { |
Alexander Kornienko | afed1dd | 2013-01-30 03:49:44 +0000 | [diff] [blame] | 1084 | assert(!ReachableBlocks.empty() && "ReachableBlocks empty"); |
| 1085 | |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1086 | int UnannotatedCnt = 0; |
| 1087 | AnnotatedCnt = 0; |
| 1088 | |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1089 | std::deque<const CFGBlock*> BlockQueue(B.pred_begin(), B.pred_end()); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1090 | while (!BlockQueue.empty()) { |
| 1091 | const CFGBlock *P = BlockQueue.front(); |
| 1092 | BlockQueue.pop_front(); |
Nick Lewycky | cdf1108 | 2014-02-27 02:43:25 +0000 | [diff] [blame] | 1093 | if (!P) continue; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1094 | |
Artem Dergachev | 4e53032 | 2019-05-24 01:34:22 +0000 | [diff] [blame] | 1095 | const Stmt *Term = P->getTerminatorStmt(); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1096 | if (Term && isa<SwitchStmt>(Term)) |
| 1097 | continue; // Switch statement, good. |
| 1098 | |
| 1099 | const SwitchCase *SW = dyn_cast_or_null<SwitchCase>(P->getLabel()); |
| 1100 | if (SW && SW->getSubStmt() == B.getLabel() && P->begin() == P->end()) |
| 1101 | continue; // Previous case label has no statements, good. |
| 1102 | |
Alexander Kornienko | 09f15f3 | 2013-01-25 20:44:56 +0000 | [diff] [blame] | 1103 | const LabelStmt *L = dyn_cast_or_null<LabelStmt>(P->getLabel()); |
| 1104 | if (L && L->getSubStmt() == B.getLabel() && P->begin() == P->end()) |
| 1105 | continue; // Case label is preceded with a normal label, good. |
| 1106 | |
Alexander Kornienko | afed1dd | 2013-01-30 03:49:44 +0000 | [diff] [blame] | 1107 | if (!ReachableBlocks.count(P)) { |
Alexander Kornienko | c121b9b | 2013-02-07 02:17:19 +0000 | [diff] [blame] | 1108 | for (CFGBlock::const_reverse_iterator ElemIt = P->rbegin(), |
| 1109 | ElemEnd = P->rend(); |
| 1110 | ElemIt != ElemEnd; ++ElemIt) { |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 1111 | if (Optional<CFGStmt> CS = ElemIt->getAs<CFGStmt>()) { |
| 1112 | if (const AttributedStmt *AS = asFallThroughAttr(CS->getStmt())) { |
Richard Smith | 7532d37 | 2017-03-22 01:49:19 +0000 | [diff] [blame] | 1113 | // Don't issue a warning for an unreachable fallthrough |
| 1114 | // attribute in template instantiations as it may not be |
| 1115 | // unreachable in all instantiations of the template. |
| 1116 | if (!IsTemplateInstantiation) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1117 | S.Diag(AS->getBeginLoc(), |
Richard Smith | 7532d37 | 2017-03-22 01:49:19 +0000 | [diff] [blame] | 1118 | diag::warn_fallthrough_attr_unreachable); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1119 | markFallthroughVisited(AS); |
| 1120 | ++AnnotatedCnt; |
Alexander Kornienko | c121b9b | 2013-02-07 02:17:19 +0000 | [diff] [blame] | 1121 | break; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1122 | } |
| 1123 | // Don't care about other unreachable statements. |
| 1124 | } |
| 1125 | } |
| 1126 | // If there are no unreachable statements, this may be a special |
| 1127 | // case in CFG: |
| 1128 | // case X: { |
| 1129 | // A a; // A has a destructor. |
| 1130 | // break; |
| 1131 | // } |
| 1132 | // // <<<< This place is represented by a 'hanging' CFG block. |
| 1133 | // case Y: |
| 1134 | continue; |
| 1135 | } |
| 1136 | |
| 1137 | const Stmt *LastStmt = getLastStmt(*P); |
| 1138 | if (const AttributedStmt *AS = asFallThroughAttr(LastStmt)) { |
| 1139 | markFallthroughVisited(AS); |
| 1140 | ++AnnotatedCnt; |
| 1141 | continue; // Fallthrough annotation, good. |
| 1142 | } |
| 1143 | |
| 1144 | if (!LastStmt) { // This block contains no executable statements. |
| 1145 | // Traverse its predecessors. |
| 1146 | std::copy(P->pred_begin(), P->pred_end(), |
| 1147 | std::back_inserter(BlockQueue)); |
| 1148 | continue; |
| 1149 | } |
| 1150 | |
| 1151 | ++UnannotatedCnt; |
| 1152 | } |
| 1153 | return !!UnannotatedCnt; |
| 1154 | } |
| 1155 | |
| 1156 | // RecursiveASTVisitor setup. |
| 1157 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 1158 | |
| 1159 | bool VisitAttributedStmt(AttributedStmt *S) { |
| 1160 | if (asFallThroughAttr(S)) |
| 1161 | FallthroughStmts.insert(S); |
| 1162 | return true; |
| 1163 | } |
| 1164 | |
| 1165 | bool VisitSwitchStmt(SwitchStmt *S) { |
| 1166 | FoundSwitchStatements = true; |
| 1167 | return true; |
| 1168 | } |
| 1169 | |
Alexander Kornienko | a9c809f | 2013-04-02 15:20:32 +0000 | [diff] [blame] | 1170 | // We don't want to traverse local type declarations. We analyze their |
| 1171 | // methods separately. |
| 1172 | bool TraverseDecl(Decl *D) { return true; } |
| 1173 | |
Alexander Kornienko | bf91164 | 2014-06-24 15:28:21 +0000 | [diff] [blame] | 1174 | // We analyze lambda bodies separately. Skip them here. |
Sam McCall | e60151c | 2019-01-14 10:31:42 +0000 | [diff] [blame] | 1175 | bool TraverseLambdaExpr(LambdaExpr *LE) { |
| 1176 | // Traverse the captures, but not the body. |
Mark de Wever | 3ec6128 | 2019-12-17 21:54:32 +0100 | [diff] [blame] | 1177 | for (const auto C : zip(LE->captures(), LE->capture_inits())) |
Sam McCall | e60151c | 2019-01-14 10:31:42 +0000 | [diff] [blame] | 1178 | TraverseLambdaCapture(LE, &std::get<0>(C), std::get<1>(C)); |
| 1179 | return true; |
| 1180 | } |
Alexander Kornienko | bf91164 | 2014-06-24 15:28:21 +0000 | [diff] [blame] | 1181 | |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1182 | private: |
| 1183 | |
| 1184 | static const AttributedStmt *asFallThroughAttr(const Stmt *S) { |
| 1185 | if (const AttributedStmt *AS = dyn_cast_or_null<AttributedStmt>(S)) { |
| 1186 | if (hasSpecificAttr<FallThroughAttr>(AS->getAttrs())) |
| 1187 | return AS; |
| 1188 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1189 | return nullptr; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | static const Stmt *getLastStmt(const CFGBlock &B) { |
Artem Dergachev | 4e53032 | 2019-05-24 01:34:22 +0000 | [diff] [blame] | 1193 | if (const Stmt *Term = B.getTerminatorStmt()) |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1194 | return Term; |
| 1195 | for (CFGBlock::const_reverse_iterator ElemIt = B.rbegin(), |
| 1196 | ElemEnd = B.rend(); |
| 1197 | ElemIt != ElemEnd; ++ElemIt) { |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 1198 | if (Optional<CFGStmt> CS = ElemIt->getAs<CFGStmt>()) |
| 1199 | return CS->getStmt(); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1200 | } |
| 1201 | // Workaround to detect a statement thrown out by CFGBuilder: |
| 1202 | // case X: {} case Y: |
| 1203 | // case X: ; case Y: |
| 1204 | if (const SwitchCase *SW = dyn_cast_or_null<SwitchCase>(B.getLabel())) |
| 1205 | if (!isa<SwitchCase>(SW->getSubStmt())) |
| 1206 | return SW->getSubStmt(); |
| 1207 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1208 | return nullptr; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | bool FoundSwitchStatements; |
| 1212 | AttrStmts FallthroughStmts; |
| 1213 | Sema &S; |
Alexander Kornienko | afed1dd | 2013-01-30 03:49:44 +0000 | [diff] [blame] | 1214 | llvm::SmallPtrSet<const CFGBlock *, 16> ReachableBlocks; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1215 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1216 | } // anonymous namespace |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1217 | |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 1218 | static StringRef getFallthroughAttrSpelling(Preprocessor &PP, |
| 1219 | SourceLocation Loc) { |
| 1220 | TokenValue FallthroughTokens[] = { |
| 1221 | tok::l_square, tok::l_square, |
| 1222 | PP.getIdentifierInfo("fallthrough"), |
| 1223 | tok::r_square, tok::r_square |
| 1224 | }; |
| 1225 | |
| 1226 | TokenValue ClangFallthroughTokens[] = { |
| 1227 | tok::l_square, tok::l_square, PP.getIdentifierInfo("clang"), |
| 1228 | tok::coloncolon, PP.getIdentifierInfo("fallthrough"), |
| 1229 | tok::r_square, tok::r_square |
| 1230 | }; |
| 1231 | |
Nathan Huckleberry | 1e0affb | 2019-08-20 17:16:49 +0000 | [diff] [blame] | 1232 | bool PreferClangAttr = !PP.getLangOpts().CPlusPlus17 && !PP.getLangOpts().C2x; |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 1233 | |
| 1234 | StringRef MacroName; |
| 1235 | if (PreferClangAttr) |
| 1236 | MacroName = PP.getLastMacroWithSpelling(Loc, ClangFallthroughTokens); |
| 1237 | if (MacroName.empty()) |
| 1238 | MacroName = PP.getLastMacroWithSpelling(Loc, FallthroughTokens); |
| 1239 | if (MacroName.empty() && !PreferClangAttr) |
| 1240 | MacroName = PP.getLastMacroWithSpelling(Loc, ClangFallthroughTokens); |
Nathan Huckleberry | 1e0affb | 2019-08-20 17:16:49 +0000 | [diff] [blame] | 1241 | if (MacroName.empty()) { |
| 1242 | if (!PreferClangAttr) |
| 1243 | MacroName = "[[fallthrough]]"; |
| 1244 | else if (PP.getLangOpts().CPlusPlus) |
| 1245 | MacroName = "[[clang::fallthrough]]"; |
| 1246 | else |
| 1247 | MacroName = "__attribute__((fallthrough))"; |
| 1248 | } |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 1249 | return MacroName; |
| 1250 | } |
| 1251 | |
Alexander Kornienko | 06caf7d | 2012-06-02 01:01:07 +0000 | [diff] [blame] | 1252 | static void DiagnoseSwitchLabelsFallthrough(Sema &S, AnalysisDeclContext &AC, |
Alexis Hunt | 2178f14 | 2012-06-15 21:22:05 +0000 | [diff] [blame] | 1253 | bool PerFunction) { |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1254 | FallthroughMapper FM(S); |
| 1255 | FM.TraverseStmt(AC.getBody()); |
| 1256 | |
| 1257 | if (!FM.foundSwitchStatements()) |
| 1258 | return; |
| 1259 | |
Alexis Hunt | 2178f14 | 2012-06-15 21:22:05 +0000 | [diff] [blame] | 1260 | if (PerFunction && FM.getFallthroughStmts().empty()) |
Alexander Kornienko | 06caf7d | 2012-06-02 01:01:07 +0000 | [diff] [blame] | 1261 | return; |
| 1262 | |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1263 | CFG *Cfg = AC.getCFG(); |
| 1264 | |
| 1265 | if (!Cfg) |
| 1266 | return; |
| 1267 | |
Alexander Kornienko | afed1dd | 2013-01-30 03:49:44 +0000 | [diff] [blame] | 1268 | FM.fillReachableBlocks(Cfg); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1269 | |
Pete Cooper | 57d3f14 | 2015-07-30 17:22:52 +0000 | [diff] [blame] | 1270 | for (const CFGBlock *B : llvm::reverse(*Cfg)) { |
Alexander Kornienko | 5548879 | 2013-01-25 15:49:34 +0000 | [diff] [blame] | 1271 | const Stmt *Label = B->getLabel(); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1272 | |
| 1273 | if (!Label || !isa<SwitchCase>(Label)) |
| 1274 | continue; |
| 1275 | |
Alexander Kornienko | afed1dd | 2013-01-30 03:49:44 +0000 | [diff] [blame] | 1276 | int AnnotatedCnt; |
| 1277 | |
Richard Smith | 7532d37 | 2017-03-22 01:49:19 +0000 | [diff] [blame] | 1278 | bool IsTemplateInstantiation = false; |
| 1279 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(AC.getDecl())) |
| 1280 | IsTemplateInstantiation = Function->isTemplateInstantiation(); |
| 1281 | if (!FM.checkFallThroughIntoBlock(*B, AnnotatedCnt, |
| 1282 | IsTemplateInstantiation)) |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1283 | continue; |
| 1284 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1285 | S.Diag(Label->getBeginLoc(), |
| 1286 | PerFunction ? diag::warn_unannotated_fallthrough_per_function |
| 1287 | : diag::warn_unannotated_fallthrough); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1288 | |
| 1289 | if (!AnnotatedCnt) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1290 | SourceLocation L = Label->getBeginLoc(); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1291 | if (L.isMacroID()) |
| 1292 | continue; |
Nathan Huckleberry | 1e0affb | 2019-08-20 17:16:49 +0000 | [diff] [blame] | 1293 | |
| 1294 | const Stmt *Term = B->getTerminatorStmt(); |
| 1295 | // Skip empty cases. |
| 1296 | while (B->empty() && !Term && B->succ_size() == 1) { |
| 1297 | B = *B->succ_begin(); |
| 1298 | Term = B->getTerminatorStmt(); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1299 | } |
Nathan Huckleberry | 1e0affb | 2019-08-20 17:16:49 +0000 | [diff] [blame] | 1300 | if (!(B->empty() && Term && isa<BreakStmt>(Term))) { |
| 1301 | Preprocessor &PP = S.getPreprocessor(); |
| 1302 | StringRef AnnotationSpelling = getFallthroughAttrSpelling(PP, L); |
| 1303 | SmallString<64> TextToInsert(AnnotationSpelling); |
| 1304 | TextToInsert += "; "; |
| 1305 | S.Diag(L, diag::note_insert_fallthrough_fixit) |
| 1306 | << AnnotationSpelling |
| 1307 | << FixItHint::CreateInsertion(L, TextToInsert); |
| 1308 | } |
| 1309 | S.Diag(L, diag::note_insert_break_fixit) |
| 1310 | << FixItHint::CreateInsertion(L, "break; "); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1311 | } |
| 1312 | } |
| 1313 | |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1314 | for (const auto *F : FM.getFallthroughStmts()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1315 | S.Diag(F->getBeginLoc(), diag::err_fallthrough_attr_invalid_placement); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Jordan Rose | 25c0ea8 | 2012-10-29 17:46:47 +0000 | [diff] [blame] | 1318 | static bool isInLoop(const ASTContext &Ctx, const ParentMap &PM, |
| 1319 | const Stmt *S) { |
Jordan Rose | 76831c6 | 2012-10-11 16:10:19 +0000 | [diff] [blame] | 1320 | assert(S); |
| 1321 | |
| 1322 | do { |
| 1323 | switch (S->getStmtClass()) { |
Jordan Rose | 76831c6 | 2012-10-11 16:10:19 +0000 | [diff] [blame] | 1324 | case Stmt::ForStmtClass: |
| 1325 | case Stmt::WhileStmtClass: |
| 1326 | case Stmt::CXXForRangeStmtClass: |
| 1327 | case Stmt::ObjCForCollectionStmtClass: |
| 1328 | return true; |
Jordan Rose | 25c0ea8 | 2012-10-29 17:46:47 +0000 | [diff] [blame] | 1329 | case Stmt::DoStmtClass: { |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 1330 | Expr::EvalResult Result; |
| 1331 | if (!cast<DoStmt>(S)->getCond()->EvaluateAsInt(Result, Ctx)) |
Jordan Rose | 25c0ea8 | 2012-10-29 17:46:47 +0000 | [diff] [blame] | 1332 | return true; |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 1333 | return Result.Val.getInt().getBoolValue(); |
Jordan Rose | 25c0ea8 | 2012-10-29 17:46:47 +0000 | [diff] [blame] | 1334 | } |
Jordan Rose | 76831c6 | 2012-10-11 16:10:19 +0000 | [diff] [blame] | 1335 | default: |
| 1336 | break; |
| 1337 | } |
| 1338 | } while ((S = PM.getParent(S))); |
| 1339 | |
| 1340 | return false; |
| 1341 | } |
| 1342 | |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1343 | static void diagnoseRepeatedUseOfWeak(Sema &S, |
| 1344 | const sema::FunctionScopeInfo *CurFn, |
Jordan Rose | 76831c6 | 2012-10-11 16:10:19 +0000 | [diff] [blame] | 1345 | const Decl *D, |
| 1346 | const ParentMap &PM) { |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1347 | typedef sema::FunctionScopeInfo::WeakObjectProfileTy WeakObjectProfileTy; |
| 1348 | typedef sema::FunctionScopeInfo::WeakObjectUseMap WeakObjectUseMap; |
| 1349 | typedef sema::FunctionScopeInfo::WeakUseVector WeakUseVector; |
Benjamin Kramer | bbdd764 | 2014-03-01 14:48:57 +0000 | [diff] [blame] | 1350 | typedef std::pair<const Stmt *, WeakObjectUseMap::const_iterator> |
| 1351 | StmtUsesPair; |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1352 | |
Jordan Rose | 25c0ea8 | 2012-10-29 17:46:47 +0000 | [diff] [blame] | 1353 | ASTContext &Ctx = S.getASTContext(); |
| 1354 | |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1355 | const WeakObjectUseMap &WeakMap = CurFn->getWeakObjectUses(); |
| 1356 | |
| 1357 | // Extract all weak objects that are referenced more than once. |
| 1358 | SmallVector<StmtUsesPair, 8> UsesByStmt; |
| 1359 | for (WeakObjectUseMap::const_iterator I = WeakMap.begin(), E = WeakMap.end(); |
| 1360 | I != E; ++I) { |
| 1361 | const WeakUseVector &Uses = I->second; |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1362 | |
| 1363 | // Find the first read of the weak object. |
| 1364 | WeakUseVector::const_iterator UI = Uses.begin(), UE = Uses.end(); |
| 1365 | for ( ; UI != UE; ++UI) { |
| 1366 | if (UI->isUnsafe()) |
| 1367 | break; |
| 1368 | } |
| 1369 | |
| 1370 | // If there were only writes to this object, don't warn. |
| 1371 | if (UI == UE) |
| 1372 | continue; |
| 1373 | |
Jordan Rose | 76831c6 | 2012-10-11 16:10:19 +0000 | [diff] [blame] | 1374 | // If there was only one read, followed by any number of writes, and the |
Jordan Rose | 25c0ea8 | 2012-10-29 17:46:47 +0000 | [diff] [blame] | 1375 | // read is not within a loop, don't warn. Additionally, don't warn in a |
| 1376 | // loop if the base object is a local variable -- local variables are often |
| 1377 | // changed in loops. |
Jordan Rose | 76831c6 | 2012-10-11 16:10:19 +0000 | [diff] [blame] | 1378 | if (UI == Uses.begin()) { |
| 1379 | WeakUseVector::const_iterator UI2 = UI; |
| 1380 | for (++UI2; UI2 != UE; ++UI2) |
| 1381 | if (UI2->isUnsafe()) |
| 1382 | break; |
| 1383 | |
Jordan Rose | 25c0ea8 | 2012-10-29 17:46:47 +0000 | [diff] [blame] | 1384 | if (UI2 == UE) { |
| 1385 | if (!isInLoop(Ctx, PM, UI->getUseExpr())) |
Jordan Rose | 76831c6 | 2012-10-11 16:10:19 +0000 | [diff] [blame] | 1386 | continue; |
Jordan Rose | 25c0ea8 | 2012-10-29 17:46:47 +0000 | [diff] [blame] | 1387 | |
| 1388 | const WeakObjectProfileTy &Profile = I->first; |
| 1389 | if (!Profile.isExactProfile()) |
| 1390 | continue; |
| 1391 | |
| 1392 | const NamedDecl *Base = Profile.getBase(); |
| 1393 | if (!Base) |
| 1394 | Base = Profile.getProperty(); |
| 1395 | assert(Base && "A profile always has a base or property."); |
| 1396 | |
| 1397 | if (const VarDecl *BaseVar = dyn_cast<VarDecl>(Base)) |
| 1398 | if (BaseVar->hasLocalStorage() && !isa<ParmVarDecl>(Base)) |
| 1399 | continue; |
| 1400 | } |
Jordan Rose | 76831c6 | 2012-10-11 16:10:19 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1403 | UsesByStmt.push_back(StmtUsesPair(UI->getUseExpr(), I)); |
| 1404 | } |
| 1405 | |
| 1406 | if (UsesByStmt.empty()) |
| 1407 | return; |
| 1408 | |
| 1409 | // Sort by first use so that we emit the warnings in a deterministic order. |
Benjamin Kramer | bbdd764 | 2014-03-01 14:48:57 +0000 | [diff] [blame] | 1410 | SourceManager &SM = S.getSourceManager(); |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 1411 | llvm::sort(UsesByStmt, |
Mandeep Singh Grang | c205d8c | 2018-03-27 16:50:00 +0000 | [diff] [blame] | 1412 | [&SM](const StmtUsesPair &LHS, const StmtUsesPair &RHS) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1413 | return SM.isBeforeInTranslationUnit(LHS.first->getBeginLoc(), |
| 1414 | RHS.first->getBeginLoc()); |
| 1415 | }); |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1416 | |
| 1417 | // Classify the current code body for better warning text. |
| 1418 | // This enum should stay in sync with the cases in |
| 1419 | // warn_arc_repeated_use_of_weak and warn_arc_possible_repeated_use_of_weak. |
| 1420 | // FIXME: Should we use a common classification enum and the same set of |
| 1421 | // possibilities all throughout Sema? |
| 1422 | enum { |
| 1423 | Function, |
| 1424 | Method, |
| 1425 | Block, |
| 1426 | Lambda |
| 1427 | } FunctionKind; |
| 1428 | |
| 1429 | if (isa<sema::BlockScopeInfo>(CurFn)) |
| 1430 | FunctionKind = Block; |
| 1431 | else if (isa<sema::LambdaScopeInfo>(CurFn)) |
| 1432 | FunctionKind = Lambda; |
| 1433 | else if (isa<ObjCMethodDecl>(D)) |
| 1434 | FunctionKind = Method; |
| 1435 | else |
| 1436 | FunctionKind = Function; |
| 1437 | |
| 1438 | // Iterate through the sorted problems and emit warnings for each. |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1439 | for (const auto &P : UsesByStmt) { |
| 1440 | const Stmt *FirstRead = P.first; |
| 1441 | const WeakObjectProfileTy &Key = P.second->first; |
| 1442 | const WeakUseVector &Uses = P.second->second; |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1443 | |
Jordan Rose | 657b5f4 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 1444 | // For complicated expressions like 'a.b.c' and 'x.b.c', WeakObjectProfileTy |
| 1445 | // may not contain enough information to determine that these are different |
| 1446 | // properties. We can only be 100% sure of a repeated use in certain cases, |
| 1447 | // and we adjust the diagnostic kind accordingly so that the less certain |
| 1448 | // case can be turned off if it is too noisy. |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1449 | unsigned DiagKind; |
| 1450 | if (Key.isExactProfile()) |
| 1451 | DiagKind = diag::warn_arc_repeated_use_of_weak; |
| 1452 | else |
| 1453 | DiagKind = diag::warn_arc_possible_repeated_use_of_weak; |
| 1454 | |
Jordan Rose | 657b5f4 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 1455 | // Classify the weak object being accessed for better warning text. |
| 1456 | // This enum should stay in sync with the cases in |
| 1457 | // warn_arc_repeated_use_of_weak and warn_arc_possible_repeated_use_of_weak. |
| 1458 | enum { |
| 1459 | Variable, |
| 1460 | Property, |
| 1461 | ImplicitProperty, |
| 1462 | Ivar |
| 1463 | } ObjectKind; |
| 1464 | |
Bob Wilson | 34cc8eb | 2016-05-25 05:42:00 +0000 | [diff] [blame] | 1465 | const NamedDecl *KeyProp = Key.getProperty(); |
| 1466 | if (isa<VarDecl>(KeyProp)) |
Jordan Rose | 657b5f4 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 1467 | ObjectKind = Variable; |
Bob Wilson | 34cc8eb | 2016-05-25 05:42:00 +0000 | [diff] [blame] | 1468 | else if (isa<ObjCPropertyDecl>(KeyProp)) |
Jordan Rose | 657b5f4 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 1469 | ObjectKind = Property; |
Bob Wilson | 34cc8eb | 2016-05-25 05:42:00 +0000 | [diff] [blame] | 1470 | else if (isa<ObjCMethodDecl>(KeyProp)) |
Jordan Rose | 657b5f4 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 1471 | ObjectKind = ImplicitProperty; |
Bob Wilson | 34cc8eb | 2016-05-25 05:42:00 +0000 | [diff] [blame] | 1472 | else if (isa<ObjCIvarDecl>(KeyProp)) |
Jordan Rose | 657b5f4 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 1473 | ObjectKind = Ivar; |
| 1474 | else |
| 1475 | llvm_unreachable("Unexpected weak object kind!"); |
| 1476 | |
Bob Wilson | f4f54e3 | 2016-05-25 05:41:57 +0000 | [diff] [blame] | 1477 | // Do not warn about IBOutlet weak property receivers being set to null |
| 1478 | // since they are typically only used from the main thread. |
Bob Wilson | 34cc8eb | 2016-05-25 05:42:00 +0000 | [diff] [blame] | 1479 | if (const ObjCPropertyDecl *Prop = dyn_cast<ObjCPropertyDecl>(KeyProp)) |
Bob Wilson | f4f54e3 | 2016-05-25 05:41:57 +0000 | [diff] [blame] | 1480 | if (Prop->hasAttr<IBOutletAttr>()) |
| 1481 | continue; |
| 1482 | |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1483 | // Show the first time the object was read. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1484 | S.Diag(FirstRead->getBeginLoc(), DiagKind) |
| 1485 | << int(ObjectKind) << KeyProp << int(FunctionKind) |
| 1486 | << FirstRead->getSourceRange(); |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1487 | |
| 1488 | // Print all the other accesses as notes. |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1489 | for (const auto &Use : Uses) { |
| 1490 | if (Use.getUseExpr() == FirstRead) |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1491 | continue; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1492 | S.Diag(Use.getUseExpr()->getBeginLoc(), |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1493 | diag::note_arc_weak_also_accessed_here) |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1494 | << Use.getUseExpr()->getSourceRange(); |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1495 | } |
| 1496 | } |
| 1497 | } |
| 1498 | |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 1499 | namespace { |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 1500 | class UninitValsDiagReporter : public UninitVariablesHandler { |
| 1501 | Sema &S; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1502 | typedef SmallVector<UninitUse, 2> UsesVec; |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 1503 | typedef llvm::PointerIntPair<UsesVec *, 1, bool> MappedType; |
Enea Zaffanella | 2f40be7 | 2013-02-15 20:09:55 +0000 | [diff] [blame] | 1504 | // Prefer using MapVector to DenseMap, so that iteration order will be |
| 1505 | // the same as insertion order. This is needed to obtain a deterministic |
| 1506 | // order of diagnostics when calling flushDiagnostics(). |
| 1507 | typedef llvm::MapVector<const VarDecl *, MappedType> UsesMap; |
George Burgess IV | 0fc4e8b | 2015-12-10 19:25:21 +0000 | [diff] [blame] | 1508 | UsesMap uses; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1509 | |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 1510 | public: |
George Burgess IV | 0fc4e8b | 2015-12-10 19:25:21 +0000 | [diff] [blame] | 1511 | UninitValsDiagReporter(Sema &S) : S(S) {} |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1512 | ~UninitValsDiagReporter() override { flushDiagnostics(); } |
Ted Kremenek | 596fa16 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 1513 | |
Enea Zaffanella | 2f40be7 | 2013-02-15 20:09:55 +0000 | [diff] [blame] | 1514 | MappedType &getUses(const VarDecl *vd) { |
George Burgess IV | 0fc4e8b | 2015-12-10 19:25:21 +0000 | [diff] [blame] | 1515 | MappedType &V = uses[vd]; |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 1516 | if (!V.getPointer()) |
| 1517 | V.setPointer(new UsesVec()); |
Ted Kremenek | 596fa16 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 1518 | return V; |
| 1519 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1520 | |
| 1521 | void handleUseOfUninitVariable(const VarDecl *vd, |
| 1522 | const UninitUse &use) override { |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 1523 | getUses(vd).getPointer()->push_back(use); |
Ted Kremenek | 596fa16 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 1524 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1525 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1526 | void handleSelfInit(const VarDecl *vd) override { |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 1527 | getUses(vd).setInt(true); |
Ted Kremenek | 39fa056 | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 1528 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1529 | |
Ted Kremenek | 39fa056 | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 1530 | void flushDiagnostics() { |
George Burgess IV | 0fc4e8b | 2015-12-10 19:25:21 +0000 | [diff] [blame] | 1531 | for (const auto &P : uses) { |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1532 | const VarDecl *vd = P.first; |
| 1533 | const MappedType &V = P.second; |
Ted Kremenek | b3dbe28 | 2011-02-02 23:35:53 +0000 | [diff] [blame] | 1534 | |
Benjamin Kramer | eb8c446 | 2013-06-29 17:52:13 +0000 | [diff] [blame] | 1535 | UsesVec *vec = V.getPointer(); |
| 1536 | bool hasSelfInit = V.getInt(); |
Ted Kremenek | 596fa16 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 1537 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1538 | // Specially handle the case where we have uses of an uninitialized |
Ted Kremenek | 596fa16 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 1539 | // variable, but the root cause is an idiomatic self-init. We want |
| 1540 | // to report the diagnostic at the self-init since that is the root cause. |
Matt Beaumont-Gay | 4b489fa | 2011-10-19 18:53:03 +0000 | [diff] [blame] | 1541 | if (!vec->empty() && hasSelfInit && hasAlwaysUninitializedUse(vec)) |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 1542 | DiagnoseUninitializedUse(S, vd, |
| 1543 | UninitUse(vd->getInit()->IgnoreParenCasts(), |
| 1544 | /* isAlwaysUninit */ true), |
Matt Beaumont-Gay | 4b489fa | 2011-10-19 18:53:03 +0000 | [diff] [blame] | 1545 | /* alwaysReportSelfInit */ true); |
Ted Kremenek | 596fa16 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 1546 | else { |
| 1547 | // Sort the uses by their SourceLocations. While not strictly |
| 1548 | // guaranteed to produce them in line/column order, this will provide |
| 1549 | // a stable ordering. |
Mandeep Singh Grang | c205d8c | 2018-03-27 16:50:00 +0000 | [diff] [blame] | 1550 | llvm::sort(vec->begin(), vec->end(), |
| 1551 | [](const UninitUse &a, const UninitUse &b) { |
Benjamin Kramer | bbdd764 | 2014-03-01 14:48:57 +0000 | [diff] [blame] | 1552 | // Prefer a more confident report over a less confident one. |
| 1553 | if (a.getKind() != b.getKind()) |
| 1554 | return a.getKind() > b.getKind(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1555 | return a.getUser()->getBeginLoc() < b.getUser()->getBeginLoc(); |
Benjamin Kramer | bbdd764 | 2014-03-01 14:48:57 +0000 | [diff] [blame] | 1556 | }); |
| 1557 | |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1558 | for (const auto &U : *vec) { |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 1559 | // If we have self-init, downgrade all uses to 'may be uninitialized'. |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1560 | UninitUse Use = hasSelfInit ? UninitUse(U.getUser(), false) : U; |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 1561 | |
| 1562 | if (DiagnoseUninitializedUse(S, vd, Use)) |
Ted Kremenek | 596fa16 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 1563 | // Skip further diagnostics for this variable. We try to warn only |
| 1564 | // on the first point at which a variable is used uninitialized. |
| 1565 | break; |
| 1566 | } |
Chandler Carruth | 7a03720 | 2011-04-05 18:18:08 +0000 | [diff] [blame] | 1567 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1568 | |
Ted Kremenek | 596fa16 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 1569 | // Release the uses vector. |
Ted Kremenek | 39fa056 | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 1570 | delete vec; |
| 1571 | } |
George Burgess IV | 0fc4e8b | 2015-12-10 19:25:21 +0000 | [diff] [blame] | 1572 | |
| 1573 | uses.clear(); |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 1574 | } |
Matt Beaumont-Gay | 4b489fa | 2011-10-19 18:53:03 +0000 | [diff] [blame] | 1575 | |
| 1576 | private: |
| 1577 | static bool hasAlwaysUninitializedUse(const UsesVec* vec) { |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1578 | return std::any_of(vec->begin(), vec->end(), [](const UninitUse &U) { |
| 1579 | return U.getKind() == UninitUse::Always || |
| 1580 | U.getKind() == UninitUse::AfterCall || |
| 1581 | U.getKind() == UninitUse::AfterDecl; |
| 1582 | }); |
Matt Beaumont-Gay | 4b489fa | 2011-10-19 18:53:03 +0000 | [diff] [blame] | 1583 | } |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 1584 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1585 | } // anonymous namespace |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 1586 | |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1587 | namespace clang { |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1588 | namespace { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1589 | typedef SmallVector<PartialDiagnosticAt, 1> OptionalNotes; |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1590 | typedef std::pair<PartialDiagnosticAt, OptionalNotes> DelayedDiag; |
Benjamin Kramer | 40b099b | 2012-03-26 14:05:40 +0000 | [diff] [blame] | 1591 | typedef std::list<DelayedDiag> DiagList; |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1592 | |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1593 | struct SortDiagBySourceLocation { |
Benjamin Kramer | 40b099b | 2012-03-26 14:05:40 +0000 | [diff] [blame] | 1594 | SourceManager &SM; |
| 1595 | SortDiagBySourceLocation(SourceManager &SM) : SM(SM) {} |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1596 | |
| 1597 | bool operator()(const DelayedDiag &left, const DelayedDiag &right) { |
| 1598 | // Although this call will be slow, this is only called when outputting |
| 1599 | // multiple warnings. |
Benjamin Kramer | 40b099b | 2012-03-26 14:05:40 +0000 | [diff] [blame] | 1600 | return SM.isBeforeInTranslationUnit(left.first.first, right.first.first); |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1601 | } |
| 1602 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1603 | } // anonymous namespace |
| 1604 | } // namespace clang |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1605 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1606 | //===----------------------------------------------------------------------===// |
| 1607 | // -Wthread-safety |
| 1608 | //===----------------------------------------------------------------------===// |
| 1609 | namespace clang { |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 1610 | namespace threadSafety { |
Benjamin Kramer | 539803c | 2015-03-19 14:23:45 +0000 | [diff] [blame] | 1611 | namespace { |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 1612 | class ThreadSafetyReporter : public clang::threadSafety::ThreadSafetyHandler { |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1613 | Sema &S; |
| 1614 | DiagList Warnings; |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1615 | SourceLocation FunLocation, FunEndLocation; |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1616 | |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1617 | const FunctionDecl *CurrentFunction; |
| 1618 | bool Verbose; |
| 1619 | |
Aaron Ballman | 71291bc | 2014-08-15 12:38:17 +0000 | [diff] [blame] | 1620 | OptionalNotes getNotes() const { |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1621 | if (Verbose && CurrentFunction) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1622 | PartialDiagnosticAt FNote(CurrentFunction->getBody()->getBeginLoc(), |
Aaron Ballman | 71291bc | 2014-08-15 12:38:17 +0000 | [diff] [blame] | 1623 | S.PDiag(diag::note_thread_warning_in_fun) |
Richard Trieu | b402580 | 2018-03-28 04:16:13 +0000 | [diff] [blame] | 1624 | << CurrentFunction); |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1625 | return OptionalNotes(1, FNote); |
| 1626 | } |
Aaron Ballman | 71291bc | 2014-08-15 12:38:17 +0000 | [diff] [blame] | 1627 | return OptionalNotes(); |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
Aaron Ballman | 71291bc | 2014-08-15 12:38:17 +0000 | [diff] [blame] | 1630 | OptionalNotes getNotes(const PartialDiagnosticAt &Note) const { |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1631 | OptionalNotes ONS(1, Note); |
| 1632 | if (Verbose && CurrentFunction) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1633 | PartialDiagnosticAt FNote(CurrentFunction->getBody()->getBeginLoc(), |
Aaron Ballman | 71291bc | 2014-08-15 12:38:17 +0000 | [diff] [blame] | 1634 | S.PDiag(diag::note_thread_warning_in_fun) |
Richard Trieu | b402580 | 2018-03-28 04:16:13 +0000 | [diff] [blame] | 1635 | << CurrentFunction); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1636 | ONS.push_back(std::move(FNote)); |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1637 | } |
| 1638 | return ONS; |
| 1639 | } |
| 1640 | |
DeLesley Hutchins | c60dc2c | 2014-09-18 23:02:26 +0000 | [diff] [blame] | 1641 | OptionalNotes getNotes(const PartialDiagnosticAt &Note1, |
| 1642 | const PartialDiagnosticAt &Note2) const { |
| 1643 | OptionalNotes ONS; |
| 1644 | ONS.push_back(Note1); |
| 1645 | ONS.push_back(Note2); |
| 1646 | if (Verbose && CurrentFunction) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1647 | PartialDiagnosticAt FNote(CurrentFunction->getBody()->getBeginLoc(), |
DeLesley Hutchins | c60dc2c | 2014-09-18 23:02:26 +0000 | [diff] [blame] | 1648 | S.PDiag(diag::note_thread_warning_in_fun) |
Richard Trieu | b402580 | 2018-03-28 04:16:13 +0000 | [diff] [blame] | 1649 | << CurrentFunction); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1650 | ONS.push_back(std::move(FNote)); |
DeLesley Hutchins | c60dc2c | 2014-09-18 23:02:26 +0000 | [diff] [blame] | 1651 | } |
| 1652 | return ONS; |
| 1653 | } |
| 1654 | |
Aaron Puchert | ad4d52a | 2019-03-18 23:26:54 +0000 | [diff] [blame] | 1655 | OptionalNotes makeLockedHereNote(SourceLocation LocLocked, StringRef Kind) { |
| 1656 | return LocLocked.isValid() |
| 1657 | ? getNotes(PartialDiagnosticAt( |
| 1658 | LocLocked, S.PDiag(diag::note_locked_here) << Kind)) |
| 1659 | : getNotes(); |
| 1660 | } |
| 1661 | |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1662 | public: |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1663 | ThreadSafetyReporter(Sema &S, SourceLocation FL, SourceLocation FEL) |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1664 | : S(S), FunLocation(FL), FunEndLocation(FEL), |
| 1665 | CurrentFunction(nullptr), Verbose(false) {} |
| 1666 | |
| 1667 | void setVerbose(bool b) { Verbose = b; } |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1668 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1669 | /// Emit all buffered diagnostics in order of sourcelocation. |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1670 | /// We need to output diagnostics produced while iterating through |
| 1671 | /// the lockset in deterministic order, so this function orders diagnostics |
| 1672 | /// and outputs them. |
| 1673 | void emitDiagnostics() { |
Benjamin Kramer | 40b099b | 2012-03-26 14:05:40 +0000 | [diff] [blame] | 1674 | Warnings.sort(SortDiagBySourceLocation(S.getSourceManager())); |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1675 | for (const auto &Diag : Warnings) { |
| 1676 | S.Diag(Diag.first.first, Diag.first.second); |
| 1677 | for (const auto &Note : Diag.second) |
| 1678 | S.Diag(Note.first, Note.second); |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1679 | } |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1682 | void handleInvalidLockExp(StringRef Kind, SourceLocation Loc) override { |
| 1683 | PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_cannot_resolve_lock) |
| 1684 | << Loc); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1685 | Warnings.emplace_back(std::move(Warning), getNotes()); |
Caitlin Sadowski | ff2f3f8 | 2011-09-09 16:21:55 +0000 | [diff] [blame] | 1686 | } |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1687 | |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1688 | void handleUnmatchedUnlock(StringRef Kind, Name LockName, |
| 1689 | SourceLocation Loc) override { |
Aaron Puchert | ffa1d6a | 2019-01-29 22:11:42 +0000 | [diff] [blame] | 1690 | if (Loc.isInvalid()) |
| 1691 | Loc = FunLocation; |
| 1692 | PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_unlock_but_no_lock) |
| 1693 | << Kind << LockName); |
| 1694 | Warnings.emplace_back(std::move(Warning), getNotes()); |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1695 | } |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1696 | |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1697 | void handleIncorrectUnlockKind(StringRef Kind, Name LockName, |
| 1698 | LockKind Expected, LockKind Received, |
Aaron Puchert | ad4d52a | 2019-03-18 23:26:54 +0000 | [diff] [blame] | 1699 | SourceLocation LocLocked, |
Aaron Puchert | dc087de | 2019-03-19 00:14:46 +0000 | [diff] [blame] | 1700 | SourceLocation LocUnlock) override { |
| 1701 | if (LocUnlock.isInvalid()) |
| 1702 | LocUnlock = FunLocation; |
| 1703 | PartialDiagnosticAt Warning( |
| 1704 | LocUnlock, S.PDiag(diag::warn_unlock_kind_mismatch) |
| 1705 | << Kind << LockName << Received << Expected); |
Aaron Puchert | ad4d52a | 2019-03-18 23:26:54 +0000 | [diff] [blame] | 1706 | Warnings.emplace_back(std::move(Warning), |
| 1707 | makeLockedHereNote(LocLocked, Kind)); |
Aaron Ballman | df115d9 | 2014-03-21 14:48:48 +0000 | [diff] [blame] | 1708 | } |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1709 | |
Aaron Puchert | ffa1d6a | 2019-01-29 22:11:42 +0000 | [diff] [blame] | 1710 | void handleDoubleLock(StringRef Kind, Name LockName, SourceLocation LocLocked, |
Aaron Puchert | dc087de | 2019-03-19 00:14:46 +0000 | [diff] [blame] | 1711 | SourceLocation LocDoubleLock) override { |
| 1712 | if (LocDoubleLock.isInvalid()) |
| 1713 | LocDoubleLock = FunLocation; |
| 1714 | PartialDiagnosticAt Warning(LocDoubleLock, S.PDiag(diag::warn_double_lock) |
| 1715 | << Kind << LockName); |
Aaron Puchert | ad4d52a | 2019-03-18 23:26:54 +0000 | [diff] [blame] | 1716 | Warnings.emplace_back(std::move(Warning), |
| 1717 | makeLockedHereNote(LocLocked, Kind)); |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1720 | void handleMutexHeldEndOfScope(StringRef Kind, Name LockName, |
| 1721 | SourceLocation LocLocked, |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1722 | SourceLocation LocEndOfScope, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1723 | LockErrorKind LEK) override { |
Caitlin Sadowski | af9b7c5 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 1724 | unsigned DiagID = 0; |
| 1725 | switch (LEK) { |
| 1726 | case LEK_LockedSomePredecessors: |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1727 | DiagID = diag::warn_lock_some_predecessors; |
Caitlin Sadowski | af9b7c5 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 1728 | break; |
| 1729 | case LEK_LockedSomeLoopIterations: |
| 1730 | DiagID = diag::warn_expecting_lock_held_on_loop; |
| 1731 | break; |
| 1732 | case LEK_LockedAtEndOfFunction: |
| 1733 | DiagID = diag::warn_no_unlock; |
| 1734 | break; |
DeLesley Hutchins | 6e6dbb7 | 2012-07-02 22:16:54 +0000 | [diff] [blame] | 1735 | case LEK_NotLockedAtEndOfFunction: |
| 1736 | DiagID = diag::warn_expecting_locked; |
| 1737 | break; |
Caitlin Sadowski | af9b7c5 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 1738 | } |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1739 | if (LocEndOfScope.isInvalid()) |
| 1740 | LocEndOfScope = FunEndLocation; |
| 1741 | |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1742 | PartialDiagnosticAt Warning(LocEndOfScope, S.PDiag(DiagID) << Kind |
| 1743 | << LockName); |
Aaron Puchert | ad4d52a | 2019-03-18 23:26:54 +0000 | [diff] [blame] | 1744 | Warnings.emplace_back(std::move(Warning), |
| 1745 | makeLockedHereNote(LocLocked, Kind)); |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1748 | void handleExclusiveAndShared(StringRef Kind, Name LockName, |
| 1749 | SourceLocation Loc1, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1750 | SourceLocation Loc2) override { |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1751 | PartialDiagnosticAt Warning(Loc1, |
| 1752 | S.PDiag(diag::warn_lock_exclusive_and_shared) |
| 1753 | << Kind << LockName); |
| 1754 | PartialDiagnosticAt Note(Loc2, S.PDiag(diag::note_lock_exclusive_and_shared) |
| 1755 | << Kind << LockName); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1756 | Warnings.emplace_back(std::move(Warning), getNotes(Note)); |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1759 | void handleNoMutexHeld(StringRef Kind, const NamedDecl *D, |
| 1760 | ProtectedOperationKind POK, AccessKind AK, |
| 1761 | SourceLocation Loc) override { |
| 1762 | assert((POK == POK_VarAccess || POK == POK_VarDereference) && |
| 1763 | "Only works for variables"); |
Caitlin Sadowski | e50d8c3 | 2011-09-14 20:09:09 +0000 | [diff] [blame] | 1764 | unsigned DiagID = POK == POK_VarAccess? |
| 1765 | diag::warn_variable_requires_any_lock: |
| 1766 | diag::warn_var_deref_requires_any_lock; |
Richard Smith | 9228667 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1767 | PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID) |
Richard Trieu | b402580 | 2018-03-28 04:16:13 +0000 | [diff] [blame] | 1768 | << D << getLockKindFromAccessKind(AK)); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1769 | Warnings.emplace_back(std::move(Warning), getNotes()); |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1772 | void handleMutexNotHeld(StringRef Kind, const NamedDecl *D, |
| 1773 | ProtectedOperationKind POK, Name LockName, |
| 1774 | LockKind LK, SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1775 | Name *PossibleMatch) override { |
Caitlin Sadowski | 427f42e | 2011-09-13 18:01:58 +0000 | [diff] [blame] | 1776 | unsigned DiagID = 0; |
DeLesley Hutchins | 5ff1644 | 2012-09-10 19:58:23 +0000 | [diff] [blame] | 1777 | if (PossibleMatch) { |
| 1778 | switch (POK) { |
| 1779 | case POK_VarAccess: |
| 1780 | DiagID = diag::warn_variable_requires_lock_precise; |
| 1781 | break; |
| 1782 | case POK_VarDereference: |
| 1783 | DiagID = diag::warn_var_deref_requires_lock_precise; |
| 1784 | break; |
| 1785 | case POK_FunctionCall: |
| 1786 | DiagID = diag::warn_fun_requires_lock_precise; |
| 1787 | break; |
DeLesley Hutchins | c60dc2c | 2014-09-18 23:02:26 +0000 | [diff] [blame] | 1788 | case POK_PassByRef: |
| 1789 | DiagID = diag::warn_guarded_pass_by_reference; |
| 1790 | break; |
| 1791 | case POK_PtPassByRef: |
| 1792 | DiagID = diag::warn_pt_guarded_pass_by_reference; |
| 1793 | break; |
DeLesley Hutchins | 5ff1644 | 2012-09-10 19:58:23 +0000 | [diff] [blame] | 1794 | } |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1795 | PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID) << Kind |
Richard Trieu | b402580 | 2018-03-28 04:16:13 +0000 | [diff] [blame] | 1796 | << D |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1797 | << LockName << LK); |
DeLesley Hutchins | 5ff1644 | 2012-09-10 19:58:23 +0000 | [diff] [blame] | 1798 | PartialDiagnosticAt Note(Loc, S.PDiag(diag::note_found_mutex_near_match) |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1799 | << *PossibleMatch); |
DeLesley Hutchins | c60dc2c | 2014-09-18 23:02:26 +0000 | [diff] [blame] | 1800 | if (Verbose && POK == POK_VarAccess) { |
| 1801 | PartialDiagnosticAt VNote(D->getLocation(), |
| 1802 | S.PDiag(diag::note_guarded_by_declared_here) |
| 1803 | << D->getNameAsString()); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1804 | Warnings.emplace_back(std::move(Warning), getNotes(Note, VNote)); |
DeLesley Hutchins | c60dc2c | 2014-09-18 23:02:26 +0000 | [diff] [blame] | 1805 | } else |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1806 | Warnings.emplace_back(std::move(Warning), getNotes(Note)); |
DeLesley Hutchins | 5ff1644 | 2012-09-10 19:58:23 +0000 | [diff] [blame] | 1807 | } else { |
| 1808 | switch (POK) { |
| 1809 | case POK_VarAccess: |
| 1810 | DiagID = diag::warn_variable_requires_lock; |
| 1811 | break; |
| 1812 | case POK_VarDereference: |
| 1813 | DiagID = diag::warn_var_deref_requires_lock; |
| 1814 | break; |
| 1815 | case POK_FunctionCall: |
| 1816 | DiagID = diag::warn_fun_requires_lock; |
| 1817 | break; |
DeLesley Hutchins | c60dc2c | 2014-09-18 23:02:26 +0000 | [diff] [blame] | 1818 | case POK_PassByRef: |
| 1819 | DiagID = diag::warn_guarded_pass_by_reference; |
| 1820 | break; |
| 1821 | case POK_PtPassByRef: |
| 1822 | DiagID = diag::warn_pt_guarded_pass_by_reference; |
| 1823 | break; |
DeLesley Hutchins | 5ff1644 | 2012-09-10 19:58:23 +0000 | [diff] [blame] | 1824 | } |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1825 | PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID) << Kind |
Richard Trieu | b402580 | 2018-03-28 04:16:13 +0000 | [diff] [blame] | 1826 | << D |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1827 | << LockName << LK); |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1828 | if (Verbose && POK == POK_VarAccess) { |
| 1829 | PartialDiagnosticAt Note(D->getLocation(), |
Richard Trieu | b402580 | 2018-03-28 04:16:13 +0000 | [diff] [blame] | 1830 | S.PDiag(diag::note_guarded_by_declared_here)); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1831 | Warnings.emplace_back(std::move(Warning), getNotes(Note)); |
Aaron Ballman | 71291bc | 2014-08-15 12:38:17 +0000 | [diff] [blame] | 1832 | } else |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1833 | Warnings.emplace_back(std::move(Warning), getNotes()); |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1834 | } |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1837 | void handleNegativeNotHeld(StringRef Kind, Name LockName, Name Neg, |
| 1838 | SourceLocation Loc) override { |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 1839 | PartialDiagnosticAt Warning(Loc, |
| 1840 | S.PDiag(diag::warn_acquire_requires_negative_cap) |
| 1841 | << Kind << LockName << Neg); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1842 | Warnings.emplace_back(std::move(Warning), getNotes()); |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 1843 | } |
| 1844 | |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1845 | void handleFunExcludesLock(StringRef Kind, Name FunName, Name LockName, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1846 | SourceLocation Loc) override { |
Aaron Ballman | e044904 | 2014-04-01 21:43:23 +0000 | [diff] [blame] | 1847 | PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_fun_excludes_mutex) |
| 1848 | << Kind << FunName << LockName); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1849 | Warnings.emplace_back(std::move(Warning), getNotes()); |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1852 | void handleLockAcquiredBefore(StringRef Kind, Name L1Name, Name L2Name, |
| 1853 | SourceLocation Loc) override { |
DeLesley Hutchins | ab1dc2d | 2015-02-03 22:11:04 +0000 | [diff] [blame] | 1854 | PartialDiagnosticAt Warning(Loc, |
| 1855 | S.PDiag(diag::warn_acquired_before) << Kind << L1Name << L2Name); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1856 | Warnings.emplace_back(std::move(Warning), getNotes()); |
DeLesley Hutchins | ab1dc2d | 2015-02-03 22:11:04 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1859 | void handleBeforeAfterCycle(Name L1Name, SourceLocation Loc) override { |
DeLesley Hutchins | ab1dc2d | 2015-02-03 22:11:04 +0000 | [diff] [blame] | 1860 | PartialDiagnosticAt Warning(Loc, |
| 1861 | S.PDiag(diag::warn_acquired_before_after_cycle) << L1Name); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1862 | Warnings.emplace_back(std::move(Warning), getNotes()); |
DeLesley Hutchins | ab1dc2d | 2015-02-03 22:11:04 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 1865 | void enterFunction(const FunctionDecl* FD) override { |
| 1866 | CurrentFunction = FD; |
| 1867 | } |
| 1868 | |
| 1869 | void leaveFunction(const FunctionDecl* FD) override { |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1870 | CurrentFunction = nullptr; |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1871 | } |
| 1872 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1873 | } // anonymous namespace |
Benjamin Kramer | 539803c | 2015-03-19 14:23:45 +0000 | [diff] [blame] | 1874 | } // namespace threadSafety |
| 1875 | } // namespace clang |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1876 | |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 1877 | //===----------------------------------------------------------------------===// |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1878 | // -Wconsumed |
| 1879 | //===----------------------------------------------------------------------===// |
| 1880 | |
| 1881 | namespace clang { |
| 1882 | namespace consumed { |
| 1883 | namespace { |
| 1884 | class ConsumedWarningsHandler : public ConsumedWarningsHandlerBase { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1885 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1886 | Sema &S; |
| 1887 | DiagList Warnings; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1888 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1889 | public: |
DeLesley Hutchins | ab1dc2d | 2015-02-03 22:11:04 +0000 | [diff] [blame] | 1890 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1891 | ConsumedWarningsHandler(Sema &S) : S(S) {} |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1892 | |
| 1893 | void emitDiagnostics() override { |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1894 | Warnings.sort(SortDiagBySourceLocation(S.getSourceManager())); |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 1895 | for (const auto &Diag : Warnings) { |
| 1896 | S.Diag(Diag.first.first, Diag.first.second); |
| 1897 | for (const auto &Note : Diag.second) |
| 1898 | S.Diag(Note.first, Note.second); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1899 | } |
| 1900 | } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1901 | |
| 1902 | void warnLoopStateMismatch(SourceLocation Loc, |
| 1903 | StringRef VariableName) override { |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1904 | PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_loop_state_mismatch) << |
| 1905 | VariableName); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1906 | |
| 1907 | Warnings.emplace_back(std::move(Warning), OptionalNotes()); |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1908 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1909 | |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1910 | void warnParamReturnTypestateMismatch(SourceLocation Loc, |
| 1911 | StringRef VariableName, |
| 1912 | StringRef ExpectedState, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1913 | StringRef ObservedState) override { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1914 | |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1915 | PartialDiagnosticAt Warning(Loc, S.PDiag( |
| 1916 | diag::warn_param_return_typestate_mismatch) << VariableName << |
| 1917 | ExpectedState << ObservedState); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1918 | |
| 1919 | Warnings.emplace_back(std::move(Warning), OptionalNotes()); |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1920 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1921 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1922 | void warnParamTypestateMismatch(SourceLocation Loc, StringRef ExpectedState, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1923 | StringRef ObservedState) override { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1924 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1925 | PartialDiagnosticAt Warning(Loc, S.PDiag( |
| 1926 | diag::warn_param_typestate_mismatch) << ExpectedState << ObservedState); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1927 | |
| 1928 | Warnings.emplace_back(std::move(Warning), OptionalNotes()); |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1929 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1930 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1931 | void warnReturnTypestateForUnconsumableType(SourceLocation Loc, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1932 | StringRef TypeName) override { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1933 | PartialDiagnosticAt Warning(Loc, S.PDiag( |
| 1934 | diag::warn_return_typestate_for_unconsumable_type) << TypeName); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1935 | |
| 1936 | Warnings.emplace_back(std::move(Warning), OptionalNotes()); |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1937 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1938 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1939 | void warnReturnTypestateMismatch(SourceLocation Loc, StringRef ExpectedState, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1940 | StringRef ObservedState) override { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1941 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1942 | PartialDiagnosticAt Warning(Loc, S.PDiag( |
| 1943 | diag::warn_return_typestate_mismatch) << ExpectedState << ObservedState); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1944 | |
| 1945 | Warnings.emplace_back(std::move(Warning), OptionalNotes()); |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1946 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1947 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1948 | void warnUseOfTempInInvalidState(StringRef MethodName, StringRef State, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1949 | SourceLocation Loc) override { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1950 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1951 | PartialDiagnosticAt Warning(Loc, S.PDiag( |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1952 | diag::warn_use_of_temp_in_invalid_state) << MethodName << State); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1953 | |
| 1954 | Warnings.emplace_back(std::move(Warning), OptionalNotes()); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1955 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1956 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1957 | void warnUseInInvalidState(StringRef MethodName, StringRef VariableName, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1958 | StringRef State, SourceLocation Loc) override { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1959 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1960 | PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_use_in_invalid_state) << |
| 1961 | MethodName << VariableName << State); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1962 | |
| 1963 | Warnings.emplace_back(std::move(Warning), OptionalNotes()); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1964 | } |
| 1965 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1966 | } // anonymous namespace |
| 1967 | } // namespace consumed |
| 1968 | } // namespace clang |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1969 | |
| 1970 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1971 | // AnalysisBasedWarnings - Worker object used by Sema to execute analysis-based |
| 1972 | // warnings on a function, method, or block. |
| 1973 | //===----------------------------------------------------------------------===// |
| 1974 | |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 1975 | clang::sema::AnalysisBasedWarnings::Policy::Policy() { |
| 1976 | enableCheckFallThrough = 1; |
| 1977 | enableCheckUnreachable = 0; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1978 | enableThreadSafetyAnalysis = 0; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1979 | enableConsumedAnalysis = 0; |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
Ted Kremenek | ad8753c | 2014-03-15 05:47:06 +0000 | [diff] [blame] | 1982 | static unsigned isEnabled(DiagnosticsEngine &D, unsigned diag) { |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 1983 | return (unsigned)!D.isIgnored(diag, SourceLocation()); |
Ted Kremenek | ad8753c | 2014-03-15 05:47:06 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
Chandler Carruth | b4836ea | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 1986 | clang::sema::AnalysisBasedWarnings::AnalysisBasedWarnings(Sema &s) |
| 1987 | : S(s), |
| 1988 | NumFunctionsAnalyzed(0), |
Benjamin Kramer | 581f48f | 2011-07-08 20:38:53 +0000 | [diff] [blame] | 1989 | NumFunctionsWithBadCFGs(0), |
Chandler Carruth | b4836ea | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 1990 | NumCFGBlocks(0), |
Benjamin Kramer | 581f48f | 2011-07-08 20:38:53 +0000 | [diff] [blame] | 1991 | MaxCFGBlocksPerFunction(0), |
| 1992 | NumUninitAnalysisFunctions(0), |
| 1993 | NumUninitAnalysisVariables(0), |
| 1994 | MaxUninitAnalysisVariablesPerFunction(0), |
| 1995 | NumUninitAnalysisBlockVisits(0), |
| 1996 | MaxUninitAnalysisBlockVisitsPerFunction(0) { |
Ted Kremenek | ad8753c | 2014-03-15 05:47:06 +0000 | [diff] [blame] | 1997 | |
| 1998 | using namespace diag; |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1999 | DiagnosticsEngine &D = S.getDiagnostics(); |
Ted Kremenek | ad8753c | 2014-03-15 05:47:06 +0000 | [diff] [blame] | 2000 | |
| 2001 | DefaultPolicy.enableCheckUnreachable = |
| 2002 | isEnabled(D, warn_unreachable) || |
| 2003 | isEnabled(D, warn_unreachable_break) || |
Ted Kremenek | 1421037 | 2014-03-21 06:02:36 +0000 | [diff] [blame] | 2004 | isEnabled(D, warn_unreachable_return) || |
| 2005 | isEnabled(D, warn_unreachable_loop_increment); |
Ted Kremenek | ad8753c | 2014-03-15 05:47:06 +0000 | [diff] [blame] | 2006 | |
| 2007 | DefaultPolicy.enableThreadSafetyAnalysis = |
| 2008 | isEnabled(D, warn_double_lock); |
| 2009 | |
| 2010 | DefaultPolicy.enableConsumedAnalysis = |
| 2011 | isEnabled(D, warn_use_in_invalid_state); |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 2014 | static void flushDiagnostics(Sema &S, const sema::FunctionScopeInfo *fscope) { |
| 2015 | for (const auto &D : fscope->PossiblyUnreachableDiags) |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 2016 | S.Diag(D.Loc, D.PD); |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 2019 | void clang::sema:: |
| 2020 | AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P, |
Ted Kremenek | cc7f1f8 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 2021 | sema::FunctionScopeInfo *fscope, |
Richard Smith | 2fdd95c | 2019-05-31 00:45:09 +0000 | [diff] [blame] | 2022 | const Decl *D, QualType BlockType) { |
Ted Kremenek | b45ebee | 2010-03-20 21:11:09 +0000 | [diff] [blame] | 2023 | |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 2024 | // We avoid doing analysis-based warnings when there are errors for |
| 2025 | // two reasons: |
| 2026 | // (1) The CFGs often can't be constructed (if the body is invalid), so |
| 2027 | // don't bother trying. |
| 2028 | // (2) The code already has problems; running the analysis just takes more |
| 2029 | // time. |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 2030 | DiagnosticsEngine &Diags = S.getDiagnostics(); |
Ted Kremenek | b802192 | 2010-04-30 21:49:25 +0000 | [diff] [blame] | 2031 | |
Olivier Goffart | 270ced2 | 2017-11-23 08:15:22 +0000 | [diff] [blame] | 2032 | // Do not do any analysis if we are going to just ignore them. |
| 2033 | if (Diags.getIgnoreAllWarnings() || |
| 2034 | (Diags.getSuppressSystemWarnings() && |
| 2035 | S.SourceMgr.isInSystemHeader(D->getLocation()))) |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 2036 | return; |
| 2037 | |
John McCall | 1d570a7 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 2038 | // For code in dependent contexts, we'll do this at instantiation time. |
David Blaikie | 0f2ae78 | 2012-01-24 04:51:48 +0000 | [diff] [blame] | 2039 | if (cast<DeclContext>(D)->isDependentContext()) |
| 2040 | return; |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 2041 | |
Argyrios Kyrtzidis | 70ec1c7 | 2016-07-13 20:35:26 +0000 | [diff] [blame] | 2042 | if (Diags.hasUncompilableErrorOccurred()) { |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 2043 | // Flush out any possibly unreachable diagnostics. |
| 2044 | flushDiagnostics(S, fscope); |
| 2045 | return; |
| 2046 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2047 | |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 2048 | const Stmt *Body = D->getBody(); |
| 2049 | assert(Body); |
| 2050 | |
Ted Kremenek | b3a38a9 | 2013-10-14 19:11:25 +0000 | [diff] [blame] | 2051 | // Construct the analysis context with the specified CFG build options. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2052 | AnalysisDeclContext AC(/* AnalysisDeclContextManager */ nullptr, D); |
Ted Kremenek | 189ecec | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 2053 | |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 2054 | // Don't generate EH edges for CallExprs as we'd like to avoid the n^2 |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 2055 | // explosion for destructors that can result and the compile time hit. |
Ted Kremenek | 189ecec | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 2056 | AC.getCFGBuildOptions().PruneTriviallyFalseEdges = true; |
| 2057 | AC.getCFGBuildOptions().AddEHEdges = false; |
| 2058 | AC.getCFGBuildOptions().AddInitializers = true; |
| 2059 | AC.getCFGBuildOptions().AddImplicitDtors = true; |
Jordan Rose | 91f7840 | 2012-09-05 23:11:06 +0000 | [diff] [blame] | 2060 | AC.getCFGBuildOptions().AddTemporaryDtors = true; |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 2061 | AC.getCFGBuildOptions().AddCXXNewAllocator = false; |
Enrico Pertoso | faed801 | 2015-06-03 10:12:40 +0000 | [diff] [blame] | 2062 | AC.getCFGBuildOptions().AddCXXDefaultInitExprInCtors = true; |
Jordan Rose | 91f7840 | 2012-09-05 23:11:06 +0000 | [diff] [blame] | 2063 | |
Ted Kremenek | 9e100ea | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 2064 | // Force that certain expressions appear as CFGElements in the CFG. This |
| 2065 | // is used to speed up various analyses. |
| 2066 | // FIXME: This isn't the right factoring. This is here for initial |
| 2067 | // prototyping, but we need a way for analyses to say what expressions they |
| 2068 | // expect to always be CFGElements and then fill in the BuildOptions |
| 2069 | // appropriately. This is essentially a layering violation. |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 2070 | if (P.enableCheckUnreachable || P.enableThreadSafetyAnalysis || |
| 2071 | P.enableConsumedAnalysis) { |
DeLesley Hutchins | f7faa6a | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 2072 | // Unreachable code analysis and thread safety require a linearized CFG. |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 2073 | AC.getCFGBuildOptions().setAllAlwaysAdd(); |
| 2074 | } |
| 2075 | else { |
| 2076 | AC.getCFGBuildOptions() |
| 2077 | .setAlwaysAdd(Stmt::BinaryOperatorClass) |
Richard Smith | b21dd02 | 2012-07-17 01:27:33 +0000 | [diff] [blame] | 2078 | .setAlwaysAdd(Stmt::CompoundAssignOperatorClass) |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 2079 | .setAlwaysAdd(Stmt::BlockExprClass) |
| 2080 | .setAlwaysAdd(Stmt::CStyleCastExprClass) |
| 2081 | .setAlwaysAdd(Stmt::DeclRefExprClass) |
| 2082 | .setAlwaysAdd(Stmt::ImplicitCastExprClass) |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 2083 | .setAlwaysAdd(Stmt::UnaryOperatorClass) |
| 2084 | .setAlwaysAdd(Stmt::AttributedStmtClass); |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 2085 | } |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 2086 | |
Richard Trieu | 8b0d14a | 2019-10-19 00:57:23 +0000 | [diff] [blame] | 2087 | // Install the logical handler. |
George Burgess IV | b65955e | 2018-08-05 01:37:07 +0000 | [diff] [blame] | 2088 | llvm::Optional<LogicalErrorHandler> LEH; |
Richard Trieu | 8b0d14a | 2019-10-19 00:57:23 +0000 | [diff] [blame] | 2089 | if (LogicalErrorHandler::hasActiveDiagnostics(Diags, D->getBeginLoc())) { |
George Burgess IV | b65955e | 2018-08-05 01:37:07 +0000 | [diff] [blame] | 2090 | LEH.emplace(S); |
| 2091 | AC.getCFGBuildOptions().Observer = &*LEH; |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 2092 | } |
Ted Kremenek | b3a38a9 | 2013-10-14 19:11:25 +0000 | [diff] [blame] | 2093 | |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 2094 | // Emit delayed diagnostics. |
David Blaikie | 0f2ae78 | 2012-01-24 04:51:48 +0000 | [diff] [blame] | 2095 | if (!fscope->PossiblyUnreachableDiags.empty()) { |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 2096 | bool analyzed = false; |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 2097 | |
| 2098 | // Register the expressions with the CFGBuilder. |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 2099 | for (const auto &D : fscope->PossiblyUnreachableDiags) { |
Richard Smith | 7d02ca4 | 2019-05-06 04:14:01 +0000 | [diff] [blame] | 2100 | for (const Stmt *S : D.Stmts) |
| 2101 | AC.registerForcedBlockExpression(S); |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | if (AC.getCFG()) { |
| 2105 | analyzed = true; |
Aaron Ballman | e519522 | 2014-05-15 20:50:47 +0000 | [diff] [blame] | 2106 | for (const auto &D : fscope->PossiblyUnreachableDiags) { |
Richard Smith | 7d02ca4 | 2019-05-06 04:14:01 +0000 | [diff] [blame] | 2107 | bool AllReachable = true; |
| 2108 | for (const Stmt *S : D.Stmts) { |
| 2109 | const CFGBlock *block = AC.getBlockForRegisteredExpression(S); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 2110 | CFGReverseBlockReachabilityAnalysis *cra = |
| 2111 | AC.getCFGReachablityAnalysis(); |
| 2112 | // FIXME: We should be able to assert that block is non-null, but |
| 2113 | // the CFG analysis can skip potentially-evaluated expressions in |
| 2114 | // edge cases; see test/Sema/vla-2.c. |
| 2115 | if (block && cra) { |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 2116 | // Can this block be reached from the entrance? |
Richard Smith | 7d02ca4 | 2019-05-06 04:14:01 +0000 | [diff] [blame] | 2117 | if (!cra->isReachable(&AC.getCFG()->getEntry(), block)) { |
| 2118 | AllReachable = false; |
| 2119 | break; |
| 2120 | } |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 2121 | } |
Richard Smith | 7d02ca4 | 2019-05-06 04:14:01 +0000 | [diff] [blame] | 2122 | // If we cannot map to a basic block, assume the statement is |
| 2123 | // reachable. |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 2124 | } |
Richard Smith | 7d02ca4 | 2019-05-06 04:14:01 +0000 | [diff] [blame] | 2125 | |
| 2126 | if (AllReachable) |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 2127 | S.Diag(D.Loc, D.PD); |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 2128 | } |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 2129 | } |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 2130 | |
| 2131 | if (!analyzed) |
| 2132 | flushDiagnostics(S, fscope); |
| 2133 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2134 | |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 2135 | // Warning: check missing 'return' |
David Blaikie | 0f2ae78 | 2012-01-24 04:51:48 +0000 | [diff] [blame] | 2136 | if (P.enableCheckFallThrough) { |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 2137 | const CheckFallThroughDiagnostics &CD = |
Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 2138 | (isa<BlockDecl>(D) |
| 2139 | ? CheckFallThroughDiagnostics::MakeForBlock() |
| 2140 | : (isa<CXXMethodDecl>(D) && |
| 2141 | cast<CXXMethodDecl>(D)->getOverloadedOperator() == OO_Call && |
| 2142 | cast<CXXMethodDecl>(D)->getParent()->isLambda()) |
| 2143 | ? CheckFallThroughDiagnostics::MakeForLambda() |
Eric Fiselier | da8f9b5 | 2017-05-25 02:16:53 +0000 | [diff] [blame] | 2144 | : (fscope->isCoroutine() |
Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 2145 | ? CheckFallThroughDiagnostics::MakeForCoroutine(D) |
| 2146 | : CheckFallThroughDiagnostics::MakeForFunction(D))); |
Richard Smith | 2fdd95c | 2019-05-31 00:45:09 +0000 | [diff] [blame] | 2147 | CheckFallThroughForBody(S, D, Body, BlockType, CD, AC, fscope); |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | // Warning: check for unreachable code |
Ted Kremenek | 7f77003 | 2011-11-30 21:22:09 +0000 | [diff] [blame] | 2151 | if (P.enableCheckUnreachable) { |
| 2152 | // Only check for unreachable code on non-template instantiations. |
| 2153 | // Different template instantiations can effectively change the control-flow |
| 2154 | // and it is very difficult to prove that a snippet of code in a template |
| 2155 | // is unreachable for all instantiations. |
Ted Kremenek | 85825ae | 2011-12-01 00:59:17 +0000 | [diff] [blame] | 2156 | bool isTemplateInstantiation = false; |
| 2157 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 2158 | isTemplateInstantiation = Function->isTemplateInstantiation(); |
| 2159 | if (!isTemplateInstantiation) |
Ted Kremenek | 7f77003 | 2011-11-30 21:22:09 +0000 | [diff] [blame] | 2160 | CheckUnreachable(S, AC); |
| 2161 | } |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 2162 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 2163 | // Check for thread safety violations |
David Blaikie | 0f2ae78 | 2012-01-24 04:51:48 +0000 | [diff] [blame] | 2164 | if (P.enableThreadSafetyAnalysis) { |
DeLesley Hutchins | c209051 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 2165 | SourceLocation FL = AC.getDecl()->getLocation(); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2166 | SourceLocation FEL = AC.getDecl()->getEndLoc(); |
DeLesley Hutchins | ea1f833 | 2014-07-28 15:57:27 +0000 | [diff] [blame] | 2167 | threadSafety::ThreadSafetyReporter Reporter(S, FL, FEL); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2168 | if (!Diags.isIgnored(diag::warn_thread_safety_beta, D->getBeginLoc())) |
DeLesley Hutchins | 8edae13 | 2012-12-05 00:06:15 +0000 | [diff] [blame] | 2169 | Reporter.setIssueBetaWarnings(true); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2170 | if (!Diags.isIgnored(diag::warn_thread_safety_verbose, D->getBeginLoc())) |
DeLesley Hutchins | eb0ea5f | 2014-08-14 21:40:15 +0000 | [diff] [blame] | 2171 | Reporter.setVerbose(true); |
DeLesley Hutchins | 8edae13 | 2012-12-05 00:06:15 +0000 | [diff] [blame] | 2172 | |
DeLesley Hutchins | ab1dc2d | 2015-02-03 22:11:04 +0000 | [diff] [blame] | 2173 | threadSafety::runThreadSafetyAnalysis(AC, Reporter, |
| 2174 | &S.ThreadSafetyDeclCache); |
Caitlin Sadowski | 0b3501c | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 2175 | Reporter.emitDiagnostics(); |
| 2176 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 2177 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 2178 | // Check for violations of consumed properties. |
| 2179 | if (P.enableConsumedAnalysis) { |
| 2180 | consumed::ConsumedWarningsHandler WarningHandler(S); |
Reid Kleckner | e846dea | 2013-08-12 23:49:39 +0000 | [diff] [blame] | 2181 | consumed::ConsumedAnalyzer Analyzer(WarningHandler); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 2182 | Analyzer.run(AC); |
| 2183 | } |
| 2184 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2185 | if (!Diags.isIgnored(diag::warn_uninit_var, D->getBeginLoc()) || |
| 2186 | !Diags.isIgnored(diag::warn_sometimes_uninit_var, D->getBeginLoc()) || |
| 2187 | !Diags.isIgnored(diag::warn_maybe_uninit_var, D->getBeginLoc())) { |
Ted Kremenek | 2551fbe | 2011-03-17 05:29:57 +0000 | [diff] [blame] | 2188 | if (CFG *cfg = AC.getCFG()) { |
Ted Kremenek | b63931e | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 2189 | UninitValsDiagReporter reporter(S); |
Fariborz Jahanian | 8809a9d | 2011-07-16 18:31:33 +0000 | [diff] [blame] | 2190 | UninitVariablesAnalysisStats stats; |
Benjamin Kramer | e492cb4 | 2011-07-16 20:13:06 +0000 | [diff] [blame] | 2191 | std::memset(&stats, 0, sizeof(UninitVariablesAnalysisStats)); |
Ted Kremenek | bcf848f | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 2192 | runUninitializedVariablesAnalysis(*cast<DeclContext>(D), *cfg, AC, |
Chandler Carruth | b4836ea | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 2193 | reporter, stats); |
| 2194 | |
| 2195 | if (S.CollectStats && stats.NumVariablesAnalyzed > 0) { |
| 2196 | ++NumUninitAnalysisFunctions; |
| 2197 | NumUninitAnalysisVariables += stats.NumVariablesAnalyzed; |
| 2198 | NumUninitAnalysisBlockVisits += stats.NumBlockVisits; |
| 2199 | MaxUninitAnalysisVariablesPerFunction = |
| 2200 | std::max(MaxUninitAnalysisVariablesPerFunction, |
| 2201 | stats.NumVariablesAnalyzed); |
| 2202 | MaxUninitAnalysisBlockVisitsPerFunction = |
| 2203 | std::max(MaxUninitAnalysisBlockVisitsPerFunction, |
| 2204 | stats.NumBlockVisits); |
| 2205 | } |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 2206 | } |
| 2207 | } |
Chandler Carruth | b4836ea | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 2208 | |
Alexander Kornienko | 06caf7d | 2012-06-02 01:01:07 +0000 | [diff] [blame] | 2209 | bool FallThroughDiagFull = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2210 | !Diags.isIgnored(diag::warn_unannotated_fallthrough, D->getBeginLoc()); |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 2211 | bool FallThroughDiagPerFunction = !Diags.isIgnored( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2212 | diag::warn_unannotated_fallthrough_per_function, D->getBeginLoc()); |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 2213 | if (FallThroughDiagFull || FallThroughDiagPerFunction || |
| 2214 | fscope->HasFallthroughStmt) { |
Alexander Kornienko | 06caf7d | 2012-06-02 01:01:07 +0000 | [diff] [blame] | 2215 | DiagnoseSwitchLabelsFallthrough(S, AC, !FallThroughDiagFull); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 2218 | if (S.getLangOpts().ObjCWeak && |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2219 | !Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, D->getBeginLoc())) |
Jordan Rose | 76831c6 | 2012-10-11 16:10:19 +0000 | [diff] [blame] | 2220 | diagnoseRepeatedUseOfWeak(S, fscope, D, AC.getParentMap()); |
Jordan Rose | d393458 | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 2221 | |
Richard Trieu | 2f024f4 | 2013-12-21 02:33:43 +0000 | [diff] [blame] | 2222 | |
| 2223 | // Check for infinite self-recursion in functions |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 2224 | if (!Diags.isIgnored(diag::warn_infinite_recursive_function, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2225 | D->getBeginLoc())) { |
Richard Trieu | 2f024f4 | 2013-12-21 02:33:43 +0000 | [diff] [blame] | 2226 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 2227 | checkRecursiveFunction(S, FD, Body, AC); |
| 2228 | } |
| 2229 | } |
| 2230 | |
Erich Keane | 89fe9c2 | 2017-06-23 20:22:19 +0000 | [diff] [blame] | 2231 | // Check for throw out of non-throwing function. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2232 | if (!Diags.isIgnored(diag::warn_throw_in_noexcept_func, D->getBeginLoc())) |
Erich Keane | 89fe9c2 | 2017-06-23 20:22:19 +0000 | [diff] [blame] | 2233 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 2234 | if (S.getLangOpts().CPlusPlus && isNoexcept(FD)) |
| 2235 | checkThrowInNonThrowingFunc(S, FD, AC); |
| 2236 | |
Richard Trieu | e9fa266 | 2014-04-15 00:57:50 +0000 | [diff] [blame] | 2237 | // If none of the previous checks caused a CFG build, trigger one here |
Richard Trieu | 8b0d14a | 2019-10-19 00:57:23 +0000 | [diff] [blame] | 2238 | // for the logical error handler. |
| 2239 | if (LogicalErrorHandler::hasActiveDiagnostics(Diags, D->getBeginLoc())) { |
Richard Trieu | e9fa266 | 2014-04-15 00:57:50 +0000 | [diff] [blame] | 2240 | AC.getCFG(); |
| 2241 | } |
| 2242 | |
Chandler Carruth | b4836ea | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 2243 | // Collect statistics about the CFG if it was built. |
| 2244 | if (S.CollectStats && AC.isCFGBuilt()) { |
| 2245 | ++NumFunctionsAnalyzed; |
| 2246 | if (CFG *cfg = AC.getCFG()) { |
| 2247 | // If we successfully built a CFG for this context, record some more |
| 2248 | // detail information about it. |
Chandler Carruth | 50020d9 | 2011-07-06 22:21:45 +0000 | [diff] [blame] | 2249 | NumCFGBlocks += cfg->getNumBlockIDs(); |
Chandler Carruth | b4836ea | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 2250 | MaxCFGBlocksPerFunction = std::max(MaxCFGBlocksPerFunction, |
Chandler Carruth | 50020d9 | 2011-07-06 22:21:45 +0000 | [diff] [blame] | 2251 | cfg->getNumBlockIDs()); |
Chandler Carruth | b4836ea | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 2252 | } else { |
| 2253 | ++NumFunctionsWithBadCFGs; |
| 2254 | } |
| 2255 | } |
| 2256 | } |
| 2257 | |
| 2258 | void clang::sema::AnalysisBasedWarnings::PrintStats() const { |
| 2259 | llvm::errs() << "\n*** Analysis Based Warnings Stats:\n"; |
| 2260 | |
| 2261 | unsigned NumCFGsBuilt = NumFunctionsAnalyzed - NumFunctionsWithBadCFGs; |
| 2262 | unsigned AvgCFGBlocksPerFunction = |
| 2263 | !NumCFGsBuilt ? 0 : NumCFGBlocks/NumCFGsBuilt; |
| 2264 | llvm::errs() << NumFunctionsAnalyzed << " functions analyzed (" |
| 2265 | << NumFunctionsWithBadCFGs << " w/o CFGs).\n" |
| 2266 | << " " << NumCFGBlocks << " CFG blocks built.\n" |
| 2267 | << " " << AvgCFGBlocksPerFunction |
| 2268 | << " average CFG blocks per function.\n" |
| 2269 | << " " << MaxCFGBlocksPerFunction |
| 2270 | << " max CFG blocks per function.\n"; |
| 2271 | |
| 2272 | unsigned AvgUninitVariablesPerFunction = !NumUninitAnalysisFunctions ? 0 |
| 2273 | : NumUninitAnalysisVariables/NumUninitAnalysisFunctions; |
| 2274 | unsigned AvgUninitBlockVisitsPerFunction = !NumUninitAnalysisFunctions ? 0 |
| 2275 | : NumUninitAnalysisBlockVisits/NumUninitAnalysisFunctions; |
| 2276 | llvm::errs() << NumUninitAnalysisFunctions |
| 2277 | << " functions analyzed for uninitialiazed variables\n" |
| 2278 | << " " << NumUninitAnalysisVariables << " variables analyzed.\n" |
| 2279 | << " " << AvgUninitVariablesPerFunction |
| 2280 | << " average variables per function.\n" |
| 2281 | << " " << MaxUninitAnalysisVariablesPerFunction |
| 2282 | << " max variables per function.\n" |
| 2283 | << " " << NumUninitAnalysisBlockVisits << " block visits.\n" |
| 2284 | << " " << AvgUninitBlockVisitsPerFunction |
| 2285 | << " average block visits per function.\n" |
| 2286 | << " " << MaxUninitAnalysisBlockVisitsPerFunction |
| 2287 | << " max block visits per function.\n"; |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 2288 | } |