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