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