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 | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 17 | #include "clang/Sema/SemaInternal.h" |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 18 | #include "clang/Sema/ScopeInfo.h" |
Ted Kremenek | d068aab | 2010-03-20 21:11:09 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceManager.h" |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceLocation.h" |
Ted Kremenek | fbb178a | 2011-01-21 19:41:46 +0000 | [diff] [blame] | 21 | #include "clang/Lex/Preprocessor.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclCXX.h" |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprObjC.h" |
| 25 | #include "clang/AST/ExprCXX.h" |
| 26 | #include "clang/AST/StmtObjC.h" |
| 27 | #include "clang/AST/StmtCXX.h" |
Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 28 | #include "clang/AST/EvaluatedExprVisitor.h" |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 29 | #include "clang/AST/StmtVisitor.h" |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame^] | 30 | #include "clang/AST/RecursiveASTVisitor.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" |
| 33 | #include "clang/Analysis/Analyses/ReachableCode.h" |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 34 | #include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h" |
Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 35 | #include "clang/Analysis/Analyses/ThreadSafety.h" |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 36 | #include "clang/Analysis/CFGStmtMap.h" |
Ted Kremenek | 6f34213 | 2011-03-15 03:17:07 +0000 | [diff] [blame] | 37 | #include "clang/Analysis/Analyses/UninitializedValues.h" |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/BitVector.h" |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/FoldingSet.h" |
| 40 | #include "llvm/ADT/ImmutableMap.h" |
| 41 | #include "llvm/ADT/PostOrderIterator.h" |
| 42 | #include "llvm/ADT/SmallVector.h" |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 43 | #include "llvm/ADT/StringRef.h" |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 44 | #include "llvm/Support/Casting.h" |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 45 | #include <algorithm> |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame^] | 46 | #include <iterator> |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 47 | #include <vector> |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame^] | 48 | #include <deque> |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 49 | |
| 50 | using namespace clang; |
| 51 | |
| 52 | //===----------------------------------------------------------------------===// |
| 53 | // Unreachable code analysis. |
| 54 | //===----------------------------------------------------------------------===// |
| 55 | |
| 56 | namespace { |
| 57 | class UnreachableCodeHandler : public reachable_code::Callback { |
| 58 | Sema &S; |
| 59 | public: |
| 60 | UnreachableCodeHandler(Sema &s) : S(s) {} |
| 61 | |
| 62 | void HandleUnreachable(SourceLocation L, SourceRange R1, SourceRange R2) { |
| 63 | S.Diag(L, diag::warn_unreachable) << R1 << R2; |
| 64 | } |
| 65 | }; |
| 66 | } |
| 67 | |
| 68 | /// CheckUnreachable - Check for unreachable code. |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 69 | static void CheckUnreachable(Sema &S, AnalysisDeclContext &AC) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 70 | UnreachableCodeHandler UC(S); |
| 71 | reachable_code::FindUnreachableCode(AC, UC); |
| 72 | } |
| 73 | |
| 74 | //===----------------------------------------------------------------------===// |
| 75 | // Check for missing return value. |
| 76 | //===----------------------------------------------------------------------===// |
| 77 | |
John McCall | 16565aa | 2010-05-16 09:34:11 +0000 | [diff] [blame] | 78 | enum ControlFlowKind { |
| 79 | UnknownFallThrough, |
| 80 | NeverFallThrough, |
| 81 | MaybeFallThrough, |
| 82 | AlwaysFallThrough, |
| 83 | NeverFallThroughOrReturn |
| 84 | }; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 85 | |
| 86 | /// CheckFallThrough - Check that we don't fall off the end of a |
| 87 | /// Statement that should return a value. |
| 88 | /// |
| 89 | /// \returns AlwaysFallThrough iff we always fall off the end of the statement, |
| 90 | /// MaybeFallThrough iff we might or might not fall off the end, |
| 91 | /// NeverFallThroughOrReturn iff we never fall off the end of the statement or |
| 92 | /// return. We assume NeverFallThrough iff we never fall off the end of the |
| 93 | /// statement but we may return. We assume that functions not marked noreturn |
| 94 | /// will return. |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 95 | static ControlFlowKind CheckFallThrough(AnalysisDeclContext &AC) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 96 | CFG *cfg = AC.getCFG(); |
John McCall | 16565aa | 2010-05-16 09:34:11 +0000 | [diff] [blame] | 97 | if (cfg == 0) return UnknownFallThrough; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 98 | |
| 99 | // The CFG leaves in dead things, and we don't want the dead code paths to |
| 100 | // confuse us, so we mark all live things first. |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 101 | llvm::BitVector live(cfg->getNumBlockIDs()); |
Ted Kremenek | 0f3b4ca | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 102 | unsigned count = reachable_code::ScanReachableFromBlock(&cfg->getEntry(), |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 103 | live); |
| 104 | |
| 105 | bool AddEHEdges = AC.getAddEHEdges(); |
| 106 | if (!AddEHEdges && count != cfg->getNumBlockIDs()) |
| 107 | // When there are things remaining dead, and we didn't add EH edges |
| 108 | // from CallExprs to the catch clauses, we have to go back and |
| 109 | // mark them as live. |
| 110 | for (CFG::iterator I = cfg->begin(), E = cfg->end(); I != E; ++I) { |
| 111 | CFGBlock &b = **I; |
| 112 | if (!live[b.getBlockID()]) { |
| 113 | if (b.pred_begin() == b.pred_end()) { |
| 114 | if (b.getTerminator() && isa<CXXTryStmt>(b.getTerminator())) |
| 115 | // When not adding EH edges from calls, catch clauses |
| 116 | // can otherwise seem dead. Avoid noting them as dead. |
Ted Kremenek | 0f3b4ca | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 117 | count += reachable_code::ScanReachableFromBlock(&b, live); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 118 | continue; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // Now we know what is live, we check the live precessors of the exit block |
| 124 | // and look for fall through paths, being careful to ignore normal returns, |
| 125 | // and exceptional paths. |
| 126 | bool HasLiveReturn = false; |
| 127 | bool HasFakeEdge = false; |
| 128 | bool HasPlainEdge = false; |
| 129 | bool HasAbnormalEdge = false; |
Ted Kremenek | 90b828a | 2010-09-09 00:06:07 +0000 | [diff] [blame] | 130 | |
| 131 | // Ignore default cases that aren't likely to be reachable because all |
| 132 | // enums in a switch(X) have explicit case statements. |
| 133 | CFGBlock::FilterOptions FO; |
| 134 | FO.IgnoreDefaultsWithCoveredEnums = 1; |
| 135 | |
| 136 | for (CFGBlock::filtered_pred_iterator |
| 137 | I = cfg->getExit().filtered_pred_start_end(FO); I.hasMore(); ++I) { |
| 138 | const CFGBlock& B = **I; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 139 | if (!live[B.getBlockID()]) |
| 140 | continue; |
Ted Kremenek | 5811f59 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 141 | |
Chandler Carruth | e05ee6d | 2011-09-13 09:53:58 +0000 | [diff] [blame] | 142 | // Skip blocks which contain an element marked as no-return. They don't |
| 143 | // represent actually viable edges into the exit block, so mark them as |
| 144 | // abnormal. |
| 145 | if (B.hasNoReturnElement()) { |
| 146 | HasAbnormalEdge = true; |
| 147 | continue; |
| 148 | } |
| 149 | |
Ted Kremenek | 5811f59 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 150 | // Destructors can appear after the 'return' in the CFG. This is |
| 151 | // normal. We need to look pass the destructors for the return |
| 152 | // statement (if it exists). |
| 153 | CFGBlock::const_reverse_iterator ri = B.rbegin(), re = B.rend(); |
Ted Kremenek | c9f8f5a | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 154 | |
Chandler Carruth | e05ee6d | 2011-09-13 09:53:58 +0000 | [diff] [blame] | 155 | for ( ; ri != re ; ++ri) |
| 156 | if (isa<CFGStmt>(*ri)) |
Ted Kremenek | 5811f59 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 157 | break; |
Chandler Carruth | e05ee6d | 2011-09-13 09:53:58 +0000 | [diff] [blame] | 158 | |
Ted Kremenek | 5811f59 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 159 | // No more CFGElements in the block? |
| 160 | if (ri == re) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 161 | if (B.getTerminator() && isa<CXXTryStmt>(B.getTerminator())) { |
| 162 | HasAbnormalEdge = true; |
| 163 | continue; |
| 164 | } |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 165 | // A labeled empty statement, or the entry block... |
| 166 | HasPlainEdge = true; |
| 167 | continue; |
| 168 | } |
Ted Kremenek | f39e6a3 | 2011-01-25 22:50:47 +0000 | [diff] [blame] | 169 | |
Ted Kremenek | 5811f59 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 170 | CFGStmt CS = cast<CFGStmt>(*ri); |
Ted Kremenek | f1d10d9 | 2011-08-23 23:05:04 +0000 | [diff] [blame] | 171 | const Stmt *S = CS.getStmt(); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 172 | if (isa<ReturnStmt>(S)) { |
| 173 | HasLiveReturn = true; |
| 174 | continue; |
| 175 | } |
| 176 | if (isa<ObjCAtThrowStmt>(S)) { |
| 177 | HasFakeEdge = true; |
| 178 | continue; |
| 179 | } |
| 180 | if (isa<CXXThrowExpr>(S)) { |
| 181 | HasFakeEdge = true; |
| 182 | continue; |
| 183 | } |
| 184 | if (const AsmStmt *AS = dyn_cast<AsmStmt>(S)) { |
| 185 | if (AS->isMSAsm()) { |
| 186 | HasFakeEdge = true; |
| 187 | HasLiveReturn = true; |
| 188 | continue; |
| 189 | } |
| 190 | } |
| 191 | if (isa<CXXTryStmt>(S)) { |
| 192 | HasAbnormalEdge = true; |
| 193 | continue; |
| 194 | } |
Chandler Carruth | e05ee6d | 2011-09-13 09:53:58 +0000 | [diff] [blame] | 195 | if (std::find(B.succ_begin(), B.succ_end(), &cfg->getExit()) |
| 196 | == B.succ_end()) { |
| 197 | HasAbnormalEdge = true; |
| 198 | continue; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 199 | } |
Chandler Carruth | e05ee6d | 2011-09-13 09:53:58 +0000 | [diff] [blame] | 200 | |
| 201 | HasPlainEdge = true; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 202 | } |
| 203 | if (!HasPlainEdge) { |
| 204 | if (HasLiveReturn) |
| 205 | return NeverFallThrough; |
| 206 | return NeverFallThroughOrReturn; |
| 207 | } |
| 208 | if (HasAbnormalEdge || HasFakeEdge || HasLiveReturn) |
| 209 | return MaybeFallThrough; |
| 210 | // This says AlwaysFallThrough for calls to functions that are not marked |
| 211 | // noreturn, that don't return. If people would like this warning to be more |
| 212 | // accurate, such functions should be marked as noreturn. |
| 213 | return AlwaysFallThrough; |
| 214 | } |
| 215 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 216 | namespace { |
| 217 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 218 | struct CheckFallThroughDiagnostics { |
| 219 | unsigned diag_MaybeFallThrough_HasNoReturn; |
| 220 | unsigned diag_MaybeFallThrough_ReturnsNonVoid; |
| 221 | unsigned diag_AlwaysFallThrough_HasNoReturn; |
| 222 | unsigned diag_AlwaysFallThrough_ReturnsNonVoid; |
| 223 | unsigned diag_NeverFallThroughOrReturn; |
Douglas Gregor | 793cd1c | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 224 | enum { Function, Block, Lambda } funMode; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 225 | SourceLocation FuncLoc; |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 226 | |
Douglas Gregor | ca7eaee | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 227 | static CheckFallThroughDiagnostics MakeForFunction(const Decl *Func) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 228 | CheckFallThroughDiagnostics D; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 229 | D.FuncLoc = Func->getLocation(); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 230 | D.diag_MaybeFallThrough_HasNoReturn = |
| 231 | diag::warn_falloff_noreturn_function; |
| 232 | D.diag_MaybeFallThrough_ReturnsNonVoid = |
| 233 | diag::warn_maybe_falloff_nonvoid_function; |
| 234 | D.diag_AlwaysFallThrough_HasNoReturn = |
| 235 | diag::warn_falloff_noreturn_function; |
| 236 | D.diag_AlwaysFallThrough_ReturnsNonVoid = |
| 237 | diag::warn_falloff_nonvoid_function; |
Douglas Gregor | ca7eaee | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 238 | |
| 239 | // Don't suggest that virtual functions be marked "noreturn", since they |
| 240 | // might be overridden by non-noreturn functions. |
| 241 | bool isVirtualMethod = false; |
| 242 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Func)) |
| 243 | isVirtualMethod = Method->isVirtual(); |
| 244 | |
Douglas Gregor | fcdd2cb | 2011-10-10 18:15:57 +0000 | [diff] [blame] | 245 | // Don't suggest that template instantiations be marked "noreturn" |
| 246 | bool isTemplateInstantiation = false; |
Ted Kremenek | 75df4ee | 2011-12-01 00:59:17 +0000 | [diff] [blame] | 247 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(Func)) |
| 248 | isTemplateInstantiation = Function->isTemplateInstantiation(); |
Douglas Gregor | fcdd2cb | 2011-10-10 18:15:57 +0000 | [diff] [blame] | 249 | |
| 250 | if (!isVirtualMethod && !isTemplateInstantiation) |
Douglas Gregor | ca7eaee | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 251 | D.diag_NeverFallThroughOrReturn = |
| 252 | diag::warn_suggest_noreturn_function; |
| 253 | else |
| 254 | D.diag_NeverFallThroughOrReturn = 0; |
| 255 | |
Douglas Gregor | 793cd1c | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 256 | D.funMode = Function; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 257 | return D; |
| 258 | } |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 259 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 260 | static CheckFallThroughDiagnostics MakeForBlock() { |
| 261 | CheckFallThroughDiagnostics D; |
| 262 | D.diag_MaybeFallThrough_HasNoReturn = |
| 263 | diag::err_noreturn_block_has_return_expr; |
| 264 | D.diag_MaybeFallThrough_ReturnsNonVoid = |
| 265 | diag::err_maybe_falloff_nonvoid_block; |
| 266 | D.diag_AlwaysFallThrough_HasNoReturn = |
| 267 | diag::err_noreturn_block_has_return_expr; |
| 268 | D.diag_AlwaysFallThrough_ReturnsNonVoid = |
| 269 | diag::err_falloff_nonvoid_block; |
| 270 | D.diag_NeverFallThroughOrReturn = |
| 271 | diag::warn_suggest_noreturn_block; |
Douglas Gregor | 793cd1c | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 272 | D.funMode = Block; |
| 273 | return D; |
| 274 | } |
| 275 | |
| 276 | static CheckFallThroughDiagnostics MakeForLambda() { |
| 277 | CheckFallThroughDiagnostics D; |
| 278 | D.diag_MaybeFallThrough_HasNoReturn = |
| 279 | diag::err_noreturn_lambda_has_return_expr; |
| 280 | D.diag_MaybeFallThrough_ReturnsNonVoid = |
| 281 | diag::warn_maybe_falloff_nonvoid_lambda; |
| 282 | D.diag_AlwaysFallThrough_HasNoReturn = |
| 283 | diag::err_noreturn_lambda_has_return_expr; |
| 284 | D.diag_AlwaysFallThrough_ReturnsNonVoid = |
| 285 | diag::warn_falloff_nonvoid_lambda; |
| 286 | D.diag_NeverFallThroughOrReturn = 0; |
| 287 | D.funMode = Lambda; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 288 | return D; |
| 289 | } |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 290 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 291 | bool checkDiagnostics(DiagnosticsEngine &D, bool ReturnsVoid, |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 292 | bool HasNoReturn) const { |
Douglas Gregor | 793cd1c | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 293 | if (funMode == Function) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 294 | return (ReturnsVoid || |
| 295 | D.getDiagnosticLevel(diag::warn_maybe_falloff_nonvoid_function, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 296 | FuncLoc) == DiagnosticsEngine::Ignored) |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 297 | && (!HasNoReturn || |
| 298 | D.getDiagnosticLevel(diag::warn_noreturn_function_has_return_expr, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 299 | FuncLoc) == DiagnosticsEngine::Ignored) |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 300 | && (!ReturnsVoid || |
| 301 | D.getDiagnosticLevel(diag::warn_suggest_noreturn_block, FuncLoc) |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 302 | == DiagnosticsEngine::Ignored); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 303 | } |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 304 | |
Douglas Gregor | 793cd1c | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 305 | // For blocks / lambdas. |
| 306 | return ReturnsVoid && !HasNoReturn |
| 307 | && ((funMode == Lambda) || |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 308 | D.getDiagnosticLevel(diag::warn_suggest_noreturn_block, FuncLoc) |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 309 | == DiagnosticsEngine::Ignored); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 310 | } |
| 311 | }; |
| 312 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 315 | /// CheckFallThroughForFunctionDef - Check that we don't fall off the end of a |
| 316 | /// function that should return a value. Check that we don't fall off the end |
| 317 | /// of a noreturn function. We assume that functions and blocks not marked |
| 318 | /// noreturn will return. |
| 319 | static void CheckFallThroughForBody(Sema &S, const Decl *D, const Stmt *Body, |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 320 | const BlockExpr *blkExpr, |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 321 | const CheckFallThroughDiagnostics& CD, |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 322 | AnalysisDeclContext &AC) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 323 | |
| 324 | bool ReturnsVoid = false; |
| 325 | bool HasNoReturn = false; |
| 326 | |
| 327 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 328 | ReturnsVoid = FD->getResultType()->isVoidType(); |
| 329 | HasNoReturn = FD->hasAttr<NoReturnAttr>() || |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 330 | FD->getType()->getAs<FunctionType>()->getNoReturnAttr(); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 331 | } |
| 332 | else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 333 | ReturnsVoid = MD->getResultType()->isVoidType(); |
| 334 | HasNoReturn = MD->hasAttr<NoReturnAttr>(); |
| 335 | } |
| 336 | else if (isa<BlockDecl>(D)) { |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 337 | QualType BlockTy = blkExpr->getType(); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 338 | if (const FunctionType *FT = |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 339 | BlockTy->getPointeeType()->getAs<FunctionType>()) { |
| 340 | if (FT->getResultType()->isVoidType()) |
| 341 | ReturnsVoid = true; |
| 342 | if (FT->getNoReturnAttr()) |
| 343 | HasNoReturn = true; |
| 344 | } |
| 345 | } |
| 346 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 347 | DiagnosticsEngine &Diags = S.getDiagnostics(); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 348 | |
| 349 | // Short circuit for compilation speed. |
| 350 | if (CD.checkDiagnostics(Diags, ReturnsVoid, HasNoReturn)) |
| 351 | return; |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 352 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 353 | // FIXME: Function try block |
| 354 | if (const CompoundStmt *Compound = dyn_cast<CompoundStmt>(Body)) { |
| 355 | switch (CheckFallThrough(AC)) { |
John McCall | 16565aa | 2010-05-16 09:34:11 +0000 | [diff] [blame] | 356 | case UnknownFallThrough: |
| 357 | break; |
| 358 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 359 | case MaybeFallThrough: |
| 360 | if (HasNoReturn) |
| 361 | S.Diag(Compound->getRBracLoc(), |
| 362 | CD.diag_MaybeFallThrough_HasNoReturn); |
| 363 | else if (!ReturnsVoid) |
| 364 | S.Diag(Compound->getRBracLoc(), |
| 365 | CD.diag_MaybeFallThrough_ReturnsNonVoid); |
| 366 | break; |
| 367 | case AlwaysFallThrough: |
| 368 | if (HasNoReturn) |
| 369 | S.Diag(Compound->getRBracLoc(), |
| 370 | CD.diag_AlwaysFallThrough_HasNoReturn); |
| 371 | else if (!ReturnsVoid) |
| 372 | S.Diag(Compound->getRBracLoc(), |
| 373 | CD.diag_AlwaysFallThrough_ReturnsNonVoid); |
| 374 | break; |
| 375 | case NeverFallThroughOrReturn: |
Chandler Carruth | b0656ec | 2011-08-31 09:01:53 +0000 | [diff] [blame] | 376 | if (ReturnsVoid && !HasNoReturn && CD.diag_NeverFallThroughOrReturn) { |
| 377 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 378 | S.Diag(Compound->getLBracLoc(), CD.diag_NeverFallThroughOrReturn) |
Douglas Gregor | b332109 | 2011-09-10 00:56:20 +0000 | [diff] [blame] | 379 | << 0 << FD; |
| 380 | } else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 381 | S.Diag(Compound->getLBracLoc(), CD.diag_NeverFallThroughOrReturn) |
| 382 | << 1 << MD; |
Chandler Carruth | b0656ec | 2011-08-31 09:01:53 +0000 | [diff] [blame] | 383 | } else { |
| 384 | S.Diag(Compound->getLBracLoc(), CD.diag_NeverFallThroughOrReturn); |
| 385 | } |
| 386 | } |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 387 | break; |
| 388 | case NeverFallThrough: |
| 389 | break; |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 395 | // -Wuninitialized |
| 396 | //===----------------------------------------------------------------------===// |
| 397 | |
Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 398 | namespace { |
Chandler Carruth | 9f64946 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 399 | /// ContainsReference - A visitor class to search for references to |
| 400 | /// a particular declaration (the needle) within any evaluated component of an |
| 401 | /// expression (recursively). |
Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 402 | class ContainsReference : public EvaluatedExprVisitor<ContainsReference> { |
Chandler Carruth | 9f64946 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 403 | bool FoundReference; |
| 404 | const DeclRefExpr *Needle; |
| 405 | |
Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 406 | public: |
Chandler Carruth | 9f64946 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 407 | ContainsReference(ASTContext &Context, const DeclRefExpr *Needle) |
| 408 | : EvaluatedExprVisitor<ContainsReference>(Context), |
| 409 | FoundReference(false), Needle(Needle) {} |
| 410 | |
| 411 | void VisitExpr(Expr *E) { |
Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 412 | // Stop evaluating if we already have a reference. |
Chandler Carruth | 9f64946 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 413 | if (FoundReference) |
Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 414 | return; |
Chandler Carruth | 9f64946 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 415 | |
| 416 | EvaluatedExprVisitor<ContainsReference>::VisitExpr(E); |
Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 417 | } |
Chandler Carruth | 9f64946 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 418 | |
| 419 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 420 | if (E == Needle) |
| 421 | FoundReference = true; |
| 422 | else |
| 423 | EvaluatedExprVisitor<ContainsReference>::VisitDeclRefExpr(E); |
Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 424 | } |
Chandler Carruth | 9f64946 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 425 | |
| 426 | bool doesContainReference() const { return FoundReference; } |
Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 427 | }; |
| 428 | } |
| 429 | |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 430 | static bool SuggestInitializationFixit(Sema &S, const VarDecl *VD) { |
Fariborz Jahanian | a34194f | 2012-03-08 00:22:50 +0000 | [diff] [blame] | 431 | QualType VariableTy = VD->getType().getCanonicalType(); |
| 432 | if (VariableTy->isBlockPointerType() && |
| 433 | !VD->hasAttr<BlocksAttr>()) { |
| 434 | S.Diag(VD->getLocation(), diag::note_block_var_fixit_add_initialization) << VD->getDeclName() |
| 435 | << FixItHint::CreateInsertion(VD->getLocation(), "__block "); |
| 436 | return true; |
| 437 | } |
| 438 | |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 439 | // Don't issue a fixit if there is already an initializer. |
| 440 | if (VD->getInit()) |
| 441 | return false; |
Fariborz Jahanian | a34194f | 2012-03-08 00:22:50 +0000 | [diff] [blame] | 442 | |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 443 | // Suggest possible initialization (if any). |
David Blaikie | 2c0abf4 | 2012-04-30 18:27:22 +0000 | [diff] [blame] | 444 | std::string Init = S.getFixItZeroInitializerForType(VariableTy); |
| 445 | if (Init.empty()) |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 446 | return false; |
Richard Trieu | 7b0a3e3 | 2012-05-03 01:09:59 +0000 | [diff] [blame] | 447 | |
| 448 | // Don't suggest a fixit inside macros. |
| 449 | if (VD->getLocEnd().isMacroID()) |
| 450 | return false; |
| 451 | |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 452 | SourceLocation Loc = S.PP.getLocForEndOfToken(VD->getLocEnd()); |
Fariborz Jahanian | a34194f | 2012-03-08 00:22:50 +0000 | [diff] [blame] | 453 | |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 454 | S.Diag(Loc, diag::note_var_fixit_add_initialization) << VD->getDeclName() |
| 455 | << FixItHint::CreateInsertion(Loc, Init); |
| 456 | return true; |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Chandler Carruth | 262d50e | 2011-04-05 18:27:05 +0000 | [diff] [blame] | 459 | /// DiagnoseUninitializedUse -- Helper function for diagnosing uses of an |
| 460 | /// uninitialized variable. This manages the different forms of diagnostic |
| 461 | /// emitted for particular types of uses. Returns true if the use was diagnosed |
| 462 | /// as a warning. If a pariticular use is one we omit warnings for, returns |
| 463 | /// false. |
| 464 | static bool DiagnoseUninitializedUse(Sema &S, const VarDecl *VD, |
Ted Kremenek | 9e76172 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 465 | const Expr *E, bool isAlwaysUninit, |
| 466 | bool alwaysReportSelfInit = false) { |
Chandler Carruth | 4c4983b | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 467 | bool isSelfInit = false; |
| 468 | |
| 469 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 470 | if (isAlwaysUninit) { |
| 471 | // Inspect the initializer of the variable declaration which is |
| 472 | // being referenced prior to its initialization. We emit |
| 473 | // specialized diagnostics for self-initialization, and we |
| 474 | // specifically avoid warning about self references which take the |
| 475 | // form of: |
| 476 | // |
| 477 | // int x = x; |
| 478 | // |
| 479 | // This is used to indicate to GCC that 'x' is intentionally left |
| 480 | // uninitialized. Proven code paths which access 'x' in |
| 481 | // an uninitialized state after this will still warn. |
| 482 | // |
| 483 | // TODO: Should we suppress maybe-uninitialized warnings for |
| 484 | // variables initialized in this way? |
| 485 | if (const Expr *Initializer = VD->getInit()) { |
Ted Kremenek | 9e76172 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 486 | if (!alwaysReportSelfInit && DRE == Initializer->IgnoreParenImpCasts()) |
Chandler Carruth | 262d50e | 2011-04-05 18:27:05 +0000 | [diff] [blame] | 487 | return false; |
Chandler Carruth | 4c4983b | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 488 | |
| 489 | ContainsReference CR(S.Context, DRE); |
| 490 | CR.Visit(const_cast<Expr*>(Initializer)); |
| 491 | isSelfInit = CR.doesContainReference(); |
| 492 | } |
| 493 | if (isSelfInit) { |
| 494 | S.Diag(DRE->getLocStart(), |
| 495 | diag::warn_uninit_self_reference_in_init) |
| 496 | << VD->getDeclName() << VD->getLocation() << DRE->getSourceRange(); |
| 497 | } else { |
| 498 | S.Diag(DRE->getLocStart(), diag::warn_uninit_var) |
| 499 | << VD->getDeclName() << DRE->getSourceRange(); |
| 500 | } |
| 501 | } else { |
| 502 | S.Diag(DRE->getLocStart(), diag::warn_maybe_uninit_var) |
| 503 | << VD->getDeclName() << DRE->getSourceRange(); |
| 504 | } |
| 505 | } else { |
| 506 | const BlockExpr *BE = cast<BlockExpr>(E); |
Fariborz Jahanian | a34194f | 2012-03-08 00:22:50 +0000 | [diff] [blame] | 507 | if (VD->getType()->isBlockPointerType() && |
| 508 | !VD->hasAttr<BlocksAttr>()) |
| 509 | S.Diag(BE->getLocStart(), diag::warn_uninit_byref_blockvar_captured_by_block) |
| 510 | << VD->getDeclName(); |
| 511 | else |
| 512 | S.Diag(BE->getLocStart(), |
| 513 | isAlwaysUninit ? diag::warn_uninit_var_captured_by_block |
| 514 | : diag::warn_maybe_uninit_var_captured_by_block) |
| 515 | << VD->getDeclName(); |
Chandler Carruth | 4c4983b | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | // Report where the variable was declared when the use wasn't within |
David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 519 | // the initializer of that declaration & we didn't already suggest |
| 520 | // an initialization fixit. |
| 521 | if (!isSelfInit && !SuggestInitializationFixit(S, VD)) |
Chandler Carruth | 4c4983b | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 522 | S.Diag(VD->getLocStart(), diag::note_uninit_var_def) |
| 523 | << VD->getDeclName(); |
| 524 | |
Chandler Carruth | 262d50e | 2011-04-05 18:27:05 +0000 | [diff] [blame] | 525 | return true; |
Chandler Carruth | 64fb959 | 2011-04-05 18:18:08 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame^] | 528 | namespace { |
| 529 | class FallthroughMapper : public RecursiveASTVisitor<FallthroughMapper> { |
| 530 | public: |
| 531 | FallthroughMapper(Sema &S) |
| 532 | : FoundSwitchStatements(false), |
| 533 | S(S) { |
| 534 | } |
| 535 | |
| 536 | bool foundSwitchStatements() const { return FoundSwitchStatements; } |
| 537 | |
| 538 | void markFallthroughVisited(const AttributedStmt *Stmt) { |
| 539 | bool Found = FallthroughStmts.erase(Stmt); |
| 540 | assert(Found); |
| 541 | } |
| 542 | |
| 543 | typedef llvm::SmallPtrSet<const AttributedStmt*, 8> AttrStmts; |
| 544 | |
| 545 | const AttrStmts &getFallthroughStmts() const { |
| 546 | return FallthroughStmts; |
| 547 | } |
| 548 | |
| 549 | bool checkFallThroughIntoBlock(const CFGBlock &B, int &AnnotatedCnt) { |
| 550 | int UnannotatedCnt = 0; |
| 551 | AnnotatedCnt = 0; |
| 552 | |
| 553 | std::deque<const CFGBlock*> BlockQueue; |
| 554 | |
| 555 | std::copy(B.pred_begin(), B.pred_end(), std::back_inserter(BlockQueue)); |
| 556 | |
| 557 | while (!BlockQueue.empty()) { |
| 558 | const CFGBlock *P = BlockQueue.front(); |
| 559 | BlockQueue.pop_front(); |
| 560 | |
| 561 | const Stmt *Term = P->getTerminator(); |
| 562 | if (Term && isa<SwitchStmt>(Term)) |
| 563 | continue; // Switch statement, good. |
| 564 | |
| 565 | const SwitchCase *SW = dyn_cast_or_null<SwitchCase>(P->getLabel()); |
| 566 | if (SW && SW->getSubStmt() == B.getLabel() && P->begin() == P->end()) |
| 567 | continue; // Previous case label has no statements, good. |
| 568 | |
| 569 | if (P->pred_begin() == P->pred_end()) { // The block is unreachable. |
| 570 | // This only catches trivially unreachable blocks. |
| 571 | for (CFGBlock::const_iterator ElIt = P->begin(), ElEnd = P->end(); |
| 572 | ElIt != ElEnd; ++ElIt) { |
| 573 | if (const CFGStmt *CS = ElIt->getAs<CFGStmt>()){ |
| 574 | if (const AttributedStmt *AS = asFallThroughAttr(CS->getStmt())) { |
| 575 | S.Diag(AS->getLocStart(), |
| 576 | diag::warn_fallthrough_attr_unreachable); |
| 577 | markFallthroughVisited(AS); |
| 578 | ++AnnotatedCnt; |
| 579 | } |
| 580 | // Don't care about other unreachable statements. |
| 581 | } |
| 582 | } |
| 583 | // If there are no unreachable statements, this may be a special |
| 584 | // case in CFG: |
| 585 | // case X: { |
| 586 | // A a; // A has a destructor. |
| 587 | // break; |
| 588 | // } |
| 589 | // // <<<< This place is represented by a 'hanging' CFG block. |
| 590 | // case Y: |
| 591 | continue; |
| 592 | } |
| 593 | |
| 594 | const Stmt *LastStmt = getLastStmt(*P); |
| 595 | if (const AttributedStmt *AS = asFallThroughAttr(LastStmt)) { |
| 596 | markFallthroughVisited(AS); |
| 597 | ++AnnotatedCnt; |
| 598 | continue; // Fallthrough annotation, good. |
| 599 | } |
| 600 | |
| 601 | if (!LastStmt) { // This block contains no executable statements. |
| 602 | // Traverse its predecessors. |
| 603 | std::copy(P->pred_begin(), P->pred_end(), |
| 604 | std::back_inserter(BlockQueue)); |
| 605 | continue; |
| 606 | } |
| 607 | |
| 608 | ++UnannotatedCnt; |
| 609 | } |
| 610 | return !!UnannotatedCnt; |
| 611 | } |
| 612 | |
| 613 | // RecursiveASTVisitor setup. |
| 614 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 615 | |
| 616 | bool VisitAttributedStmt(AttributedStmt *S) { |
| 617 | if (asFallThroughAttr(S)) |
| 618 | FallthroughStmts.insert(S); |
| 619 | return true; |
| 620 | } |
| 621 | |
| 622 | bool VisitSwitchStmt(SwitchStmt *S) { |
| 623 | FoundSwitchStatements = true; |
| 624 | return true; |
| 625 | } |
| 626 | |
| 627 | private: |
| 628 | |
| 629 | static const AttributedStmt *asFallThroughAttr(const Stmt *S) { |
| 630 | if (const AttributedStmt *AS = dyn_cast_or_null<AttributedStmt>(S)) { |
| 631 | if (hasSpecificAttr<FallThroughAttr>(AS->getAttrs())) |
| 632 | return AS; |
| 633 | } |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | static const Stmt *getLastStmt(const CFGBlock &B) { |
| 638 | if (const Stmt *Term = B.getTerminator()) |
| 639 | return Term; |
| 640 | for (CFGBlock::const_reverse_iterator ElemIt = B.rbegin(), |
| 641 | ElemEnd = B.rend(); |
| 642 | ElemIt != ElemEnd; ++ElemIt) { |
| 643 | if (const CFGStmt *CS = ElemIt->getAs<CFGStmt>()) |
| 644 | return CS->getStmt(); |
| 645 | } |
| 646 | // Workaround to detect a statement thrown out by CFGBuilder: |
| 647 | // case X: {} case Y: |
| 648 | // case X: ; case Y: |
| 649 | if (const SwitchCase *SW = dyn_cast_or_null<SwitchCase>(B.getLabel())) |
| 650 | if (!isa<SwitchCase>(SW->getSubStmt())) |
| 651 | return SW->getSubStmt(); |
| 652 | |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | bool FoundSwitchStatements; |
| 657 | AttrStmts FallthroughStmts; |
| 658 | Sema &S; |
| 659 | }; |
| 660 | } |
| 661 | |
| 662 | static void DiagnoseSwitchLabelsFallthrough(Sema &S, AnalysisDeclContext &AC) { |
| 663 | FallthroughMapper FM(S); |
| 664 | FM.TraverseStmt(AC.getBody()); |
| 665 | |
| 666 | if (!FM.foundSwitchStatements()) |
| 667 | return; |
| 668 | |
| 669 | CFG *Cfg = AC.getCFG(); |
| 670 | |
| 671 | if (!Cfg) |
| 672 | return; |
| 673 | |
| 674 | int AnnotatedCnt; |
| 675 | |
| 676 | for (CFG::reverse_iterator I = Cfg->rbegin(), E = Cfg->rend(); I != E; ++I) { |
| 677 | const CFGBlock &B = **I; |
| 678 | const Stmt *Label = B.getLabel(); |
| 679 | |
| 680 | if (!Label || !isa<SwitchCase>(Label)) |
| 681 | continue; |
| 682 | |
| 683 | if (!FM.checkFallThroughIntoBlock(B, AnnotatedCnt)) |
| 684 | continue; |
| 685 | |
| 686 | S.Diag(Label->getLocStart(), diag::warn_unannotated_fallthrough); |
| 687 | |
| 688 | if (!AnnotatedCnt) { |
| 689 | SourceLocation L = Label->getLocStart(); |
| 690 | if (L.isMacroID()) |
| 691 | continue; |
| 692 | if (S.getLangOpts().CPlusPlus0x) { |
| 693 | S.Diag(L, diag::note_insert_fallthrough_fixit) << |
| 694 | FixItHint::CreateInsertion(L, "[[clang::fallthrough]]; "); |
| 695 | } |
| 696 | S.Diag(L, diag::note_insert_break_fixit) << |
| 697 | FixItHint::CreateInsertion(L, "break; "); |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | const FallthroughMapper::AttrStmts &Fallthroughs = FM.getFallthroughStmts(); |
| 702 | for (FallthroughMapper::AttrStmts::const_iterator I = Fallthroughs.begin(), |
| 703 | E = Fallthroughs.end(); |
| 704 | I != E; ++I) { |
| 705 | S.Diag((*I)->getLocStart(), diag::warn_fallthrough_attr_invalid_placement); |
| 706 | } |
| 707 | |
| 708 | } |
| 709 | |
Ted Kremenek | f7bafc7 | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 710 | typedef std::pair<const Expr*, bool> UninitUse; |
| 711 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 712 | namespace { |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 713 | struct SLocSort { |
Ted Kremenek | f7bafc7 | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 714 | bool operator()(const UninitUse &a, const UninitUse &b) { |
| 715 | SourceLocation aLoc = a.first->getLocStart(); |
| 716 | SourceLocation bLoc = b.first->getLocStart(); |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 717 | return aLoc.getRawEncoding() < bLoc.getRawEncoding(); |
| 718 | } |
| 719 | }; |
| 720 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 721 | class UninitValsDiagReporter : public UninitVariablesHandler { |
| 722 | Sema &S; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 723 | typedef SmallVector<UninitUse, 2> UsesVec; |
Ted Kremenek | 9e76172 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 724 | typedef llvm::DenseMap<const VarDecl *, std::pair<UsesVec*, bool> > UsesMap; |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 725 | UsesMap *uses; |
| 726 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 727 | public: |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 728 | UninitValsDiagReporter(Sema &S) : S(S), uses(0) {} |
| 729 | ~UninitValsDiagReporter() { |
| 730 | flushDiagnostics(); |
| 731 | } |
Ted Kremenek | 9e76172 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 732 | |
| 733 | std::pair<UsesVec*, bool> &getUses(const VarDecl *vd) { |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 734 | if (!uses) |
| 735 | uses = new UsesMap(); |
Ted Kremenek | 9e76172 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 736 | |
| 737 | UsesMap::mapped_type &V = (*uses)[vd]; |
| 738 | UsesVec *&vec = V.first; |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 739 | if (!vec) |
| 740 | vec = new UsesVec(); |
| 741 | |
Ted Kremenek | 9e76172 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 742 | return V; |
| 743 | } |
| 744 | |
| 745 | void handleUseOfUninitVariable(const Expr *ex, const VarDecl *vd, |
| 746 | bool isAlwaysUninit) { |
| 747 | getUses(vd).first->push_back(std::make_pair(ex, isAlwaysUninit)); |
| 748 | } |
| 749 | |
| 750 | void handleSelfInit(const VarDecl *vd) { |
| 751 | getUses(vd).second = true; |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | void flushDiagnostics() { |
| 755 | if (!uses) |
| 756 | return; |
Ted Kremenek | 609e317 | 2011-02-02 23:35:53 +0000 | [diff] [blame] | 757 | |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 758 | for (UsesMap::iterator i = uses->begin(), e = uses->end(); i != e; ++i) { |
| 759 | const VarDecl *vd = i->first; |
Ted Kremenek | 9e76172 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 760 | const UsesMap::mapped_type &V = i->second; |
Ted Kremenek | 609e317 | 2011-02-02 23:35:53 +0000 | [diff] [blame] | 761 | |
Ted Kremenek | 9e76172 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 762 | UsesVec *vec = V.first; |
| 763 | bool hasSelfInit = V.second; |
| 764 | |
| 765 | // Specially handle the case where we have uses of an uninitialized |
| 766 | // variable, but the root cause is an idiomatic self-init. We want |
| 767 | // 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] | 768 | if (!vec->empty() && hasSelfInit && hasAlwaysUninitializedUse(vec)) |
Ted Kremenek | 9e76172 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 769 | DiagnoseUninitializedUse(S, vd, vd->getInit()->IgnoreParenCasts(), |
Matt Beaumont-Gay | 0d38181 | 2011-10-19 18:53:03 +0000 | [diff] [blame] | 770 | /* isAlwaysUninit */ true, |
| 771 | /* alwaysReportSelfInit */ true); |
Ted Kremenek | 9e76172 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 772 | else { |
| 773 | // Sort the uses by their SourceLocations. While not strictly |
| 774 | // guaranteed to produce them in line/column order, this will provide |
| 775 | // a stable ordering. |
| 776 | std::sort(vec->begin(), vec->end(), SLocSort()); |
| 777 | |
| 778 | for (UsesVec::iterator vi = vec->begin(), ve = vec->end(); vi != ve; |
| 779 | ++vi) { |
| 780 | if (DiagnoseUninitializedUse(S, vd, vi->first, |
| 781 | /*isAlwaysUninit=*/vi->second)) |
| 782 | // Skip further diagnostics for this variable. We try to warn only |
| 783 | // on the first point at which a variable is used uninitialized. |
| 784 | break; |
| 785 | } |
Chandler Carruth | 64fb959 | 2011-04-05 18:18:08 +0000 | [diff] [blame] | 786 | } |
Ted Kremenek | 9e76172 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 787 | |
| 788 | // Release the uses vector. |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 789 | delete vec; |
| 790 | } |
| 791 | delete uses; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 792 | } |
Matt Beaumont-Gay | 0d38181 | 2011-10-19 18:53:03 +0000 | [diff] [blame] | 793 | |
| 794 | private: |
| 795 | static bool hasAlwaysUninitializedUse(const UsesVec* vec) { |
| 796 | for (UsesVec::const_iterator i = vec->begin(), e = vec->end(); i != e; ++i) { |
| 797 | if (i->second) { |
| 798 | return true; |
| 799 | } |
| 800 | } |
| 801 | return false; |
| 802 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 803 | }; |
| 804 | } |
| 805 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 806 | |
| 807 | //===----------------------------------------------------------------------===// |
| 808 | // -Wthread-safety |
| 809 | //===----------------------------------------------------------------------===// |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 810 | namespace clang { |
| 811 | namespace thread_safety { |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 812 | typedef llvm::SmallVector<PartialDiagnosticAt, 1> OptionalNotes; |
| 813 | typedef std::pair<PartialDiagnosticAt, OptionalNotes> DelayedDiag; |
Benjamin Kramer | ecafd30 | 2012-03-26 14:05:40 +0000 | [diff] [blame] | 814 | typedef std::list<DelayedDiag> DiagList; |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 815 | |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 816 | struct SortDiagBySourceLocation { |
Benjamin Kramer | ecafd30 | 2012-03-26 14:05:40 +0000 | [diff] [blame] | 817 | SourceManager &SM; |
| 818 | SortDiagBySourceLocation(SourceManager &SM) : SM(SM) {} |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 819 | |
| 820 | bool operator()(const DelayedDiag &left, const DelayedDiag &right) { |
| 821 | // Although this call will be slow, this is only called when outputting |
| 822 | // multiple warnings. |
Benjamin Kramer | ecafd30 | 2012-03-26 14:05:40 +0000 | [diff] [blame] | 823 | return SM.isBeforeInTranslationUnit(left.first.first, right.first.first); |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 824 | } |
| 825 | }; |
| 826 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 827 | namespace { |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 828 | class ThreadSafetyReporter : public clang::thread_safety::ThreadSafetyHandler { |
| 829 | Sema &S; |
| 830 | DiagList Warnings; |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 831 | SourceLocation FunLocation, FunEndLocation; |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 832 | |
| 833 | // Helper functions |
| 834 | void warnLockMismatch(unsigned DiagID, Name LockName, SourceLocation Loc) { |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 835 | // Gracefully handle rare cases when the analysis can't get a more |
| 836 | // precise source location. |
| 837 | if (!Loc.isValid()) |
| 838 | Loc = FunLocation; |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 839 | PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID) << LockName); |
| 840 | Warnings.push_back(DelayedDiag(Warning, OptionalNotes())); |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | public: |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 844 | ThreadSafetyReporter(Sema &S, SourceLocation FL, SourceLocation FEL) |
| 845 | : S(S), FunLocation(FL), FunEndLocation(FEL) {} |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 846 | |
| 847 | /// \brief Emit all buffered diagnostics in order of sourcelocation. |
| 848 | /// We need to output diagnostics produced while iterating through |
| 849 | /// the lockset in deterministic order, so this function orders diagnostics |
| 850 | /// and outputs them. |
| 851 | void emitDiagnostics() { |
Benjamin Kramer | ecafd30 | 2012-03-26 14:05:40 +0000 | [diff] [blame] | 852 | Warnings.sort(SortDiagBySourceLocation(S.getSourceManager())); |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 853 | for (DiagList::iterator I = Warnings.begin(), E = Warnings.end(); |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 854 | I != E; ++I) { |
| 855 | S.Diag(I->first.first, I->first.second); |
| 856 | const OptionalNotes &Notes = I->second; |
| 857 | for (unsigned NoteI = 0, NoteN = Notes.size(); NoteI != NoteN; ++NoteI) |
| 858 | S.Diag(Notes[NoteI].first, Notes[NoteI].second); |
| 859 | } |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Caitlin Sadowski | 99107eb | 2011-09-09 16:21:55 +0000 | [diff] [blame] | 862 | void handleInvalidLockExp(SourceLocation Loc) { |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 863 | PartialDiagnosticAt Warning(Loc, |
| 864 | S.PDiag(diag::warn_cannot_resolve_lock) << Loc); |
| 865 | Warnings.push_back(DelayedDiag(Warning, OptionalNotes())); |
Caitlin Sadowski | 99107eb | 2011-09-09 16:21:55 +0000 | [diff] [blame] | 866 | } |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 867 | void handleUnmatchedUnlock(Name LockName, SourceLocation Loc) { |
| 868 | warnLockMismatch(diag::warn_unlock_but_no_lock, LockName, Loc); |
| 869 | } |
| 870 | |
| 871 | void handleDoubleLock(Name LockName, SourceLocation Loc) { |
| 872 | warnLockMismatch(diag::warn_double_lock, LockName, Loc); |
| 873 | } |
| 874 | |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 875 | void handleMutexHeldEndOfScope(Name LockName, SourceLocation LocLocked, |
| 876 | SourceLocation LocEndOfScope, |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 877 | LockErrorKind LEK){ |
| 878 | unsigned DiagID = 0; |
| 879 | switch (LEK) { |
| 880 | case LEK_LockedSomePredecessors: |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 881 | DiagID = diag::warn_lock_some_predecessors; |
Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame] | 882 | break; |
| 883 | case LEK_LockedSomeLoopIterations: |
| 884 | DiagID = diag::warn_expecting_lock_held_on_loop; |
| 885 | break; |
| 886 | case LEK_LockedAtEndOfFunction: |
| 887 | DiagID = diag::warn_no_unlock; |
| 888 | break; |
| 889 | } |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 890 | if (LocEndOfScope.isInvalid()) |
| 891 | LocEndOfScope = FunEndLocation; |
| 892 | |
| 893 | PartialDiagnosticAt Warning(LocEndOfScope, S.PDiag(DiagID) << LockName); |
| 894 | PartialDiagnosticAt Note(LocLocked, S.PDiag(diag::note_locked_here)); |
| 895 | Warnings.push_back(DelayedDiag(Warning, OptionalNotes(1, Note))); |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 898 | |
| 899 | void handleExclusiveAndShared(Name LockName, SourceLocation Loc1, |
| 900 | SourceLocation Loc2) { |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 901 | PartialDiagnosticAt Warning( |
| 902 | Loc1, S.PDiag(diag::warn_lock_exclusive_and_shared) << LockName); |
| 903 | PartialDiagnosticAt Note( |
| 904 | Loc2, S.PDiag(diag::note_lock_exclusive_and_shared) << LockName); |
| 905 | Warnings.push_back(DelayedDiag(Warning, OptionalNotes(1, Note))); |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | void handleNoMutexHeld(const NamedDecl *D, ProtectedOperationKind POK, |
| 909 | AccessKind AK, SourceLocation Loc) { |
Caitlin Sadowski | df8327c | 2011-09-14 20:09:09 +0000 | [diff] [blame] | 910 | assert((POK == POK_VarAccess || POK == POK_VarDereference) |
| 911 | && "Only works for variables"); |
| 912 | unsigned DiagID = POK == POK_VarAccess? |
| 913 | diag::warn_variable_requires_any_lock: |
| 914 | diag::warn_var_deref_requires_any_lock; |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 915 | PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID) |
| 916 | << D->getName() << getLockKindFromAccessKind(AK)); |
| 917 | Warnings.push_back(DelayedDiag(Warning, OptionalNotes())); |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | void handleMutexNotHeld(const NamedDecl *D, ProtectedOperationKind POK, |
| 921 | Name LockName, LockKind LK, SourceLocation Loc) { |
Caitlin Sadowski | e87158d | 2011-09-13 18:01:58 +0000 | [diff] [blame] | 922 | unsigned DiagID = 0; |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 923 | switch (POK) { |
| 924 | case POK_VarAccess: |
| 925 | DiagID = diag::warn_variable_requires_lock; |
| 926 | break; |
| 927 | case POK_VarDereference: |
| 928 | DiagID = diag::warn_var_deref_requires_lock; |
| 929 | break; |
| 930 | case POK_FunctionCall: |
| 931 | DiagID = diag::warn_fun_requires_lock; |
| 932 | break; |
| 933 | } |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 934 | PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID) |
| 935 | << D->getName() << LockName << LK); |
| 936 | Warnings.push_back(DelayedDiag(Warning, OptionalNotes())); |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | void handleFunExcludesLock(Name FunName, Name LockName, SourceLocation Loc) { |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 940 | PartialDiagnosticAt Warning(Loc, |
| 941 | S.PDiag(diag::warn_fun_excludes_mutex) << FunName << LockName); |
| 942 | Warnings.push_back(DelayedDiag(Warning, OptionalNotes())); |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 943 | } |
| 944 | }; |
| 945 | } |
| 946 | } |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 947 | } |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 948 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 949 | //===----------------------------------------------------------------------===// |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 950 | // AnalysisBasedWarnings - Worker object used by Sema to execute analysis-based |
| 951 | // warnings on a function, method, or block. |
| 952 | //===----------------------------------------------------------------------===// |
| 953 | |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 954 | clang::sema::AnalysisBasedWarnings::Policy::Policy() { |
| 955 | enableCheckFallThrough = 1; |
| 956 | enableCheckUnreachable = 0; |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 957 | enableThreadSafetyAnalysis = 0; |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 960 | clang::sema::AnalysisBasedWarnings::AnalysisBasedWarnings(Sema &s) |
| 961 | : S(s), |
| 962 | NumFunctionsAnalyzed(0), |
Benjamin Kramer | 54cf341 | 2011-07-08 20:38:53 +0000 | [diff] [blame] | 963 | NumFunctionsWithBadCFGs(0), |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 964 | NumCFGBlocks(0), |
Benjamin Kramer | 54cf341 | 2011-07-08 20:38:53 +0000 | [diff] [blame] | 965 | MaxCFGBlocksPerFunction(0), |
| 966 | NumUninitAnalysisFunctions(0), |
| 967 | NumUninitAnalysisVariables(0), |
| 968 | MaxUninitAnalysisVariablesPerFunction(0), |
| 969 | NumUninitAnalysisBlockVisits(0), |
| 970 | MaxUninitAnalysisBlockVisitsPerFunction(0) { |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 971 | DiagnosticsEngine &D = S.getDiagnostics(); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 972 | DefaultPolicy.enableCheckUnreachable = (unsigned) |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 973 | (D.getDiagnosticLevel(diag::warn_unreachable, SourceLocation()) != |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 974 | DiagnosticsEngine::Ignored); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 975 | DefaultPolicy.enableThreadSafetyAnalysis = (unsigned) |
| 976 | (D.getDiagnosticLevel(diag::warn_double_lock, SourceLocation()) != |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 977 | DiagnosticsEngine::Ignored); |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 978 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 981 | static void flushDiagnostics(Sema &S, sema::FunctionScopeInfo *fscope) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 982 | for (SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 983 | i = fscope->PossiblyUnreachableDiags.begin(), |
| 984 | e = fscope->PossiblyUnreachableDiags.end(); |
| 985 | i != e; ++i) { |
| 986 | const sema::PossiblyUnreachableDiag &D = *i; |
| 987 | S.Diag(D.Loc, D.PD); |
| 988 | } |
| 989 | } |
| 990 | |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 991 | void clang::sema:: |
| 992 | AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P, |
Ted Kremenek | 283a358 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 993 | sema::FunctionScopeInfo *fscope, |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 994 | const Decl *D, const BlockExpr *blkExpr) { |
Ted Kremenek | d068aab | 2010-03-20 21:11:09 +0000 | [diff] [blame] | 995 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 996 | // We avoid doing analysis-based warnings when there are errors for |
| 997 | // two reasons: |
| 998 | // (1) The CFGs often can't be constructed (if the body is invalid), so |
| 999 | // don't bother trying. |
| 1000 | // (2) The code already has problems; running the analysis just takes more |
| 1001 | // time. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1002 | DiagnosticsEngine &Diags = S.getDiagnostics(); |
Ted Kremenek | 99e8192 | 2010-04-30 21:49:25 +0000 | [diff] [blame] | 1003 | |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 1004 | // Do not do any analysis for declarations in system headers if we are |
| 1005 | // going to just ignore them. |
Ted Kremenek | 99e8192 | 2010-04-30 21:49:25 +0000 | [diff] [blame] | 1006 | if (Diags.getSuppressSystemWarnings() && |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 1007 | S.SourceMgr.isInSystemHeader(D->getLocation())) |
| 1008 | return; |
| 1009 | |
John McCall | e0054f6 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 1010 | // For code in dependent contexts, we'll do this at instantiation time. |
David Blaikie | 23661d3 | 2012-01-24 04:51:48 +0000 | [diff] [blame] | 1011 | if (cast<DeclContext>(D)->isDependentContext()) |
| 1012 | return; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1013 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1014 | if (Diags.hasErrorOccurred() || Diags.hasFatalErrorOccurred()) { |
| 1015 | // Flush out any possibly unreachable diagnostics. |
| 1016 | flushDiagnostics(S, fscope); |
| 1017 | return; |
| 1018 | } |
| 1019 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1020 | const Stmt *Body = D->getBody(); |
| 1021 | assert(Body); |
| 1022 | |
Jordy Rose | d200187 | 2012-04-28 01:58:08 +0000 | [diff] [blame] | 1023 | AnalysisDeclContext AC(/* AnalysisDeclContextManager */ 0, D); |
Ted Kremenek | bc5cb8a | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 1024 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1025 | // Don't generate EH edges for CallExprs as we'd like to avoid the n^2 |
| 1026 | // explosion for destrutors that can result and the compile time hit. |
Ted Kremenek | bc5cb8a | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 1027 | AC.getCFGBuildOptions().PruneTriviallyFalseEdges = true; |
| 1028 | AC.getCFGBuildOptions().AddEHEdges = false; |
| 1029 | AC.getCFGBuildOptions().AddInitializers = true; |
| 1030 | AC.getCFGBuildOptions().AddImplicitDtors = true; |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 1031 | |
| 1032 | // Force that certain expressions appear as CFGElements in the CFG. This |
| 1033 | // is used to speed up various analyses. |
| 1034 | // FIXME: This isn't the right factoring. This is here for initial |
| 1035 | // prototyping, but we need a way for analyses to say what expressions they |
| 1036 | // expect to always be CFGElements and then fill in the BuildOptions |
| 1037 | // appropriately. This is essentially a layering violation. |
DeLesley Hutchins | 1fa3c06 | 2011-12-08 20:23:06 +0000 | [diff] [blame] | 1038 | if (P.enableCheckUnreachable || P.enableThreadSafetyAnalysis) { |
| 1039 | // Unreachable code analysis and thread safety require a linearized CFG. |
Ted Kremenek | 0f3b4ca | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 1040 | AC.getCFGBuildOptions().setAllAlwaysAdd(); |
| 1041 | } |
| 1042 | else { |
| 1043 | AC.getCFGBuildOptions() |
| 1044 | .setAlwaysAdd(Stmt::BinaryOperatorClass) |
| 1045 | .setAlwaysAdd(Stmt::BlockExprClass) |
| 1046 | .setAlwaysAdd(Stmt::CStyleCastExprClass) |
| 1047 | .setAlwaysAdd(Stmt::DeclRefExprClass) |
| 1048 | .setAlwaysAdd(Stmt::ImplicitCastExprClass) |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame^] | 1049 | .setAlwaysAdd(Stmt::UnaryOperatorClass) |
| 1050 | .setAlwaysAdd(Stmt::AttributedStmtClass); |
Ted Kremenek | 0f3b4ca | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 1051 | } |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1052 | |
Ted Kremenek | bc5cb8a | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 1053 | // Construct the analysis context with the specified CFG build options. |
| 1054 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1055 | // Emit delayed diagnostics. |
David Blaikie | 23661d3 | 2012-01-24 04:51:48 +0000 | [diff] [blame] | 1056 | if (!fscope->PossiblyUnreachableDiags.empty()) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1057 | bool analyzed = false; |
Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1058 | |
| 1059 | // Register the expressions with the CFGBuilder. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1060 | for (SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator |
Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1061 | i = fscope->PossiblyUnreachableDiags.begin(), |
| 1062 | e = fscope->PossiblyUnreachableDiags.end(); |
| 1063 | i != e; ++i) { |
| 1064 | if (const Stmt *stmt = i->stmt) |
| 1065 | AC.registerForcedBlockExpression(stmt); |
| 1066 | } |
| 1067 | |
| 1068 | if (AC.getCFG()) { |
| 1069 | analyzed = true; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1070 | for (SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator |
Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1071 | i = fscope->PossiblyUnreachableDiags.begin(), |
| 1072 | e = fscope->PossiblyUnreachableDiags.end(); |
| 1073 | i != e; ++i) |
| 1074 | { |
| 1075 | const sema::PossiblyUnreachableDiag &D = *i; |
| 1076 | bool processed = false; |
| 1077 | if (const Stmt *stmt = i->stmt) { |
| 1078 | const CFGBlock *block = AC.getBlockForRegisteredExpression(stmt); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 1079 | CFGReverseBlockReachabilityAnalysis *cra = |
| 1080 | AC.getCFGReachablityAnalysis(); |
| 1081 | // FIXME: We should be able to assert that block is non-null, but |
| 1082 | // the CFG analysis can skip potentially-evaluated expressions in |
| 1083 | // edge cases; see test/Sema/vla-2.c. |
| 1084 | if (block && cra) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1085 | // Can this block be reached from the entrance? |
Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1086 | if (cra->isReachable(&AC.getCFG()->getEntry(), block)) |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1087 | S.Diag(D.Loc, D.PD); |
Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1088 | processed = true; |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1089 | } |
| 1090 | } |
Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1091 | if (!processed) { |
| 1092 | // Emit the warning anyway if we cannot map to a basic block. |
| 1093 | S.Diag(D.Loc, D.PD); |
| 1094 | } |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1095 | } |
Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1096 | } |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 1097 | |
| 1098 | if (!analyzed) |
| 1099 | flushDiagnostics(S, fscope); |
| 1100 | } |
| 1101 | |
| 1102 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1103 | // Warning: check missing 'return' |
David Blaikie | 23661d3 | 2012-01-24 04:51:48 +0000 | [diff] [blame] | 1104 | if (P.enableCheckFallThrough) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1105 | const CheckFallThroughDiagnostics &CD = |
| 1106 | (isa<BlockDecl>(D) ? CheckFallThroughDiagnostics::MakeForBlock() |
Douglas Gregor | 793cd1c | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 1107 | : (isa<CXXMethodDecl>(D) && |
| 1108 | cast<CXXMethodDecl>(D)->getOverloadedOperator() == OO_Call && |
| 1109 | cast<CXXMethodDecl>(D)->getParent()->isLambda()) |
| 1110 | ? CheckFallThroughDiagnostics::MakeForLambda() |
| 1111 | : CheckFallThroughDiagnostics::MakeForFunction(D)); |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 1112 | CheckFallThroughForBody(S, D, Body, blkExpr, CD, AC); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | // Warning: check for unreachable code |
Ted Kremenek | 5dfee06 | 2011-11-30 21:22:09 +0000 | [diff] [blame] | 1116 | if (P.enableCheckUnreachable) { |
| 1117 | // Only check for unreachable code on non-template instantiations. |
| 1118 | // Different template instantiations can effectively change the control-flow |
| 1119 | // and it is very difficult to prove that a snippet of code in a template |
| 1120 | // is unreachable for all instantiations. |
Ted Kremenek | 75df4ee | 2011-12-01 00:59:17 +0000 | [diff] [blame] | 1121 | bool isTemplateInstantiation = false; |
| 1122 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 1123 | isTemplateInstantiation = Function->isTemplateInstantiation(); |
| 1124 | if (!isTemplateInstantiation) |
Ted Kremenek | 5dfee06 | 2011-11-30 21:22:09 +0000 | [diff] [blame] | 1125 | CheckUnreachable(S, AC); |
| 1126 | } |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1127 | |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1128 | // Check for thread safety violations |
David Blaikie | 23661d3 | 2012-01-24 04:51:48 +0000 | [diff] [blame] | 1129 | if (P.enableThreadSafetyAnalysis) { |
DeLesley Hutchins | f1ac637 | 2011-10-21 18:10:14 +0000 | [diff] [blame] | 1130 | SourceLocation FL = AC.getDecl()->getLocation(); |
Richard Smith | 2e51562 | 2012-02-03 04:45:26 +0000 | [diff] [blame] | 1131 | SourceLocation FEL = AC.getDecl()->getLocEnd(); |
| 1132 | thread_safety::ThreadSafetyReporter Reporter(S, FL, FEL); |
Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 1133 | thread_safety::runThreadSafetyAnalysis(AC, Reporter); |
| 1134 | Reporter.emitDiagnostics(); |
| 1135 | } |
Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1136 | |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 1137 | if (Diags.getDiagnosticLevel(diag::warn_uninit_var, D->getLocStart()) |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1138 | != DiagnosticsEngine::Ignored || |
Ted Kremenek | 76709bf | 2011-03-15 05:22:28 +0000 | [diff] [blame] | 1139 | Diags.getDiagnosticLevel(diag::warn_maybe_uninit_var, D->getLocStart()) |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1140 | != DiagnosticsEngine::Ignored) { |
Ted Kremenek | c5e43c1 | 2011-03-17 05:29:57 +0000 | [diff] [blame] | 1141 | if (CFG *cfg = AC.getCFG()) { |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 1142 | UninitValsDiagReporter reporter(S); |
Fariborz Jahanian | 57080fb | 2011-07-16 18:31:33 +0000 | [diff] [blame] | 1143 | UninitVariablesAnalysisStats stats; |
Benjamin Kramer | 12efd57 | 2011-07-16 20:13:06 +0000 | [diff] [blame] | 1144 | std::memset(&stats, 0, sizeof(UninitVariablesAnalysisStats)); |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 1145 | runUninitializedVariablesAnalysis(*cast<DeclContext>(D), *cfg, AC, |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 1146 | reporter, stats); |
| 1147 | |
| 1148 | if (S.CollectStats && stats.NumVariablesAnalyzed > 0) { |
| 1149 | ++NumUninitAnalysisFunctions; |
| 1150 | NumUninitAnalysisVariables += stats.NumVariablesAnalyzed; |
| 1151 | NumUninitAnalysisBlockVisits += stats.NumBlockVisits; |
| 1152 | MaxUninitAnalysisVariablesPerFunction = |
| 1153 | std::max(MaxUninitAnalysisVariablesPerFunction, |
| 1154 | stats.NumVariablesAnalyzed); |
| 1155 | MaxUninitAnalysisBlockVisitsPerFunction = |
| 1156 | std::max(MaxUninitAnalysisBlockVisitsPerFunction, |
| 1157 | stats.NumBlockVisits); |
| 1158 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 1159 | } |
| 1160 | } |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 1161 | |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame^] | 1162 | if (Diags.getDiagnosticLevel(diag::warn_unannotated_fallthrough, |
| 1163 | D->getLocStart()) != DiagnosticsEngine::Ignored) { |
| 1164 | DiagnoseSwitchLabelsFallthrough(S, AC); |
| 1165 | } |
| 1166 | |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 1167 | // Collect statistics about the CFG if it was built. |
| 1168 | if (S.CollectStats && AC.isCFGBuilt()) { |
| 1169 | ++NumFunctionsAnalyzed; |
| 1170 | if (CFG *cfg = AC.getCFG()) { |
| 1171 | // If we successfully built a CFG for this context, record some more |
| 1172 | // detail information about it. |
Chandler Carruth | 3ea4c49 | 2011-07-06 22:21:45 +0000 | [diff] [blame] | 1173 | NumCFGBlocks += cfg->getNumBlockIDs(); |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 1174 | MaxCFGBlocksPerFunction = std::max(MaxCFGBlocksPerFunction, |
Chandler Carruth | 3ea4c49 | 2011-07-06 22:21:45 +0000 | [diff] [blame] | 1175 | cfg->getNumBlockIDs()); |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 1176 | } else { |
| 1177 | ++NumFunctionsWithBadCFGs; |
| 1178 | } |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | void clang::sema::AnalysisBasedWarnings::PrintStats() const { |
| 1183 | llvm::errs() << "\n*** Analysis Based Warnings Stats:\n"; |
| 1184 | |
| 1185 | unsigned NumCFGsBuilt = NumFunctionsAnalyzed - NumFunctionsWithBadCFGs; |
| 1186 | unsigned AvgCFGBlocksPerFunction = |
| 1187 | !NumCFGsBuilt ? 0 : NumCFGBlocks/NumCFGsBuilt; |
| 1188 | llvm::errs() << NumFunctionsAnalyzed << " functions analyzed (" |
| 1189 | << NumFunctionsWithBadCFGs << " w/o CFGs).\n" |
| 1190 | << " " << NumCFGBlocks << " CFG blocks built.\n" |
| 1191 | << " " << AvgCFGBlocksPerFunction |
| 1192 | << " average CFG blocks per function.\n" |
| 1193 | << " " << MaxCFGBlocksPerFunction |
| 1194 | << " max CFG blocks per function.\n"; |
| 1195 | |
| 1196 | unsigned AvgUninitVariablesPerFunction = !NumUninitAnalysisFunctions ? 0 |
| 1197 | : NumUninitAnalysisVariables/NumUninitAnalysisFunctions; |
| 1198 | unsigned AvgUninitBlockVisitsPerFunction = !NumUninitAnalysisFunctions ? 0 |
| 1199 | : NumUninitAnalysisBlockVisits/NumUninitAnalysisFunctions; |
| 1200 | llvm::errs() << NumUninitAnalysisFunctions |
| 1201 | << " functions analyzed for uninitialiazed variables\n" |
| 1202 | << " " << NumUninitAnalysisVariables << " variables analyzed.\n" |
| 1203 | << " " << AvgUninitVariablesPerFunction |
| 1204 | << " average variables per function.\n" |
| 1205 | << " " << MaxUninitAnalysisVariablesPerFunction |
| 1206 | << " max variables per function.\n" |
| 1207 | << " " << NumUninitAnalysisBlockVisits << " block visits.\n" |
| 1208 | << " " << AvgUninitBlockVisitsPerFunction |
| 1209 | << " average block visits per function.\n" |
| 1210 | << " " << MaxUninitAnalysisBlockVisitsPerFunction |
| 1211 | << " max block visits per function.\n"; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 1212 | } |