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