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