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