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