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