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