| 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" | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 30 | #include "clang/Analysis/AnalysisContext.h" | 
|  | 31 | #include "clang/Analysis/CFG.h" | 
|  | 32 | #include "clang/Analysis/Analyses/ReachableCode.h" | 
| Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 33 | #include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h" | 
| Caitlin Sadowski | 402aa06 | 2011-09-09 16:11:56 +0000 | [diff] [blame] | 34 | #include "clang/Analysis/Analyses/ThreadSafety.h" | 
| Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 35 | #include "clang/Analysis/CFGStmtMap.h" | 
| Ted Kremenek | 6f34213 | 2011-03-15 03:17:07 +0000 | [diff] [blame] | 36 | #include "clang/Analysis/Analyses/UninitializedValues.h" | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/BitVector.h" | 
| Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/FoldingSet.h" | 
|  | 39 | #include "llvm/ADT/ImmutableMap.h" | 
|  | 40 | #include "llvm/ADT/PostOrderIterator.h" | 
|  | 41 | #include "llvm/ADT/SmallVector.h" | 
| Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/StringRef.h" | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Casting.h" | 
| Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 44 | #include <algorithm> | 
|  | 45 | #include <vector> | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 46 |  | 
|  | 47 | using namespace clang; | 
|  | 48 |  | 
|  | 49 | //===----------------------------------------------------------------------===// | 
|  | 50 | // Unreachable code analysis. | 
|  | 51 | //===----------------------------------------------------------------------===// | 
|  | 52 |  | 
|  | 53 | namespace { | 
|  | 54 | class UnreachableCodeHandler : public reachable_code::Callback { | 
|  | 55 | Sema &S; | 
|  | 56 | public: | 
|  | 57 | UnreachableCodeHandler(Sema &s) : S(s) {} | 
|  | 58 |  | 
|  | 59 | void HandleUnreachable(SourceLocation L, SourceRange R1, SourceRange R2) { | 
|  | 60 | S.Diag(L, diag::warn_unreachable) << R1 << R2; | 
|  | 61 | } | 
|  | 62 | }; | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | /// CheckUnreachable - Check for unreachable code. | 
|  | 66 | static void CheckUnreachable(Sema &S, AnalysisContext &AC) { | 
|  | 67 | UnreachableCodeHandler UC(S); | 
|  | 68 | reachable_code::FindUnreachableCode(AC, UC); | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | //===----------------------------------------------------------------------===// | 
|  | 72 | // Check for missing return value. | 
|  | 73 | //===----------------------------------------------------------------------===// | 
|  | 74 |  | 
| John McCall | 16565aa | 2010-05-16 09:34:11 +0000 | [diff] [blame] | 75 | enum ControlFlowKind { | 
|  | 76 | UnknownFallThrough, | 
|  | 77 | NeverFallThrough, | 
|  | 78 | MaybeFallThrough, | 
|  | 79 | AlwaysFallThrough, | 
|  | 80 | NeverFallThroughOrReturn | 
|  | 81 | }; | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 82 |  | 
|  | 83 | /// CheckFallThrough - Check that we don't fall off the end of a | 
|  | 84 | /// Statement that should return a value. | 
|  | 85 | /// | 
|  | 86 | /// \returns AlwaysFallThrough iff we always fall off the end of the statement, | 
|  | 87 | /// MaybeFallThrough iff we might or might not fall off the end, | 
|  | 88 | /// NeverFallThroughOrReturn iff we never fall off the end of the statement or | 
|  | 89 | /// return.  We assume NeverFallThrough iff we never fall off the end of the | 
|  | 90 | /// statement but we may return.  We assume that functions not marked noreturn | 
|  | 91 | /// will return. | 
|  | 92 | static ControlFlowKind CheckFallThrough(AnalysisContext &AC) { | 
|  | 93 | CFG *cfg = AC.getCFG(); | 
| John McCall | 16565aa | 2010-05-16 09:34:11 +0000 | [diff] [blame] | 94 | if (cfg == 0) return UnknownFallThrough; | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 95 |  | 
|  | 96 | // The CFG leaves in dead things, and we don't want the dead code paths to | 
|  | 97 | // confuse us, so we mark all live things first. | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 98 | llvm::BitVector live(cfg->getNumBlockIDs()); | 
| Ted Kremenek | 0f3b4ca | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 99 | unsigned count = reachable_code::ScanReachableFromBlock(&cfg->getEntry(), | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 100 | live); | 
|  | 101 |  | 
|  | 102 | bool AddEHEdges = AC.getAddEHEdges(); | 
|  | 103 | if (!AddEHEdges && count != cfg->getNumBlockIDs()) | 
|  | 104 | // When there are things remaining dead, and we didn't add EH edges | 
|  | 105 | // from CallExprs to the catch clauses, we have to go back and | 
|  | 106 | // mark them as live. | 
|  | 107 | for (CFG::iterator I = cfg->begin(), E = cfg->end(); I != E; ++I) { | 
|  | 108 | CFGBlock &b = **I; | 
|  | 109 | if (!live[b.getBlockID()]) { | 
|  | 110 | if (b.pred_begin() == b.pred_end()) { | 
|  | 111 | if (b.getTerminator() && isa<CXXTryStmt>(b.getTerminator())) | 
|  | 112 | // When not adding EH edges from calls, catch clauses | 
|  | 113 | // can otherwise seem dead.  Avoid noting them as dead. | 
| Ted Kremenek | 0f3b4ca | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 114 | count += reachable_code::ScanReachableFromBlock(&b, live); | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 115 | continue; | 
|  | 116 | } | 
|  | 117 | } | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | // Now we know what is live, we check the live precessors of the exit block | 
|  | 121 | // and look for fall through paths, being careful to ignore normal returns, | 
|  | 122 | // and exceptional paths. | 
|  | 123 | bool HasLiveReturn = false; | 
|  | 124 | bool HasFakeEdge = false; | 
|  | 125 | bool HasPlainEdge = false; | 
|  | 126 | bool HasAbnormalEdge = false; | 
| Ted Kremenek | 90b828a | 2010-09-09 00:06:07 +0000 | [diff] [blame] | 127 |  | 
|  | 128 | // Ignore default cases that aren't likely to be reachable because all | 
|  | 129 | // enums in a switch(X) have explicit case statements. | 
|  | 130 | CFGBlock::FilterOptions FO; | 
|  | 131 | FO.IgnoreDefaultsWithCoveredEnums = 1; | 
|  | 132 |  | 
|  | 133 | for (CFGBlock::filtered_pred_iterator | 
|  | 134 | I = cfg->getExit().filtered_pred_start_end(FO); I.hasMore(); ++I) { | 
|  | 135 | const CFGBlock& B = **I; | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 136 | if (!live[B.getBlockID()]) | 
|  | 137 | continue; | 
| Ted Kremenek | 5811f59 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 138 |  | 
| Chandler Carruth | e05ee6d | 2011-09-13 09:53:58 +0000 | [diff] [blame] | 139 | // Skip blocks which contain an element marked as no-return. They don't | 
|  | 140 | // represent actually viable edges into the exit block, so mark them as | 
|  | 141 | // abnormal. | 
|  | 142 | if (B.hasNoReturnElement()) { | 
|  | 143 | HasAbnormalEdge = true; | 
|  | 144 | continue; | 
|  | 145 | } | 
|  | 146 |  | 
| Ted Kremenek | 5811f59 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 147 | // Destructors can appear after the 'return' in the CFG.  This is | 
|  | 148 | // normal.  We need to look pass the destructors for the return | 
|  | 149 | // statement (if it exists). | 
|  | 150 | CFGBlock::const_reverse_iterator ri = B.rbegin(), re = B.rend(); | 
| Ted Kremenek | c9f8f5a | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 151 |  | 
| Chandler Carruth | e05ee6d | 2011-09-13 09:53:58 +0000 | [diff] [blame] | 152 | for ( ; ri != re ; ++ri) | 
|  | 153 | if (isa<CFGStmt>(*ri)) | 
| Ted Kremenek | 5811f59 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 154 | break; | 
| Chandler Carruth | e05ee6d | 2011-09-13 09:53:58 +0000 | [diff] [blame] | 155 |  | 
| Ted Kremenek | 5811f59 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 156 | // No more CFGElements in the block? | 
|  | 157 | if (ri == re) { | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 158 | if (B.getTerminator() && isa<CXXTryStmt>(B.getTerminator())) { | 
|  | 159 | HasAbnormalEdge = true; | 
|  | 160 | continue; | 
|  | 161 | } | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 162 | // A labeled empty statement, or the entry block... | 
|  | 163 | HasPlainEdge = true; | 
|  | 164 | continue; | 
|  | 165 | } | 
| Ted Kremenek | f39e6a3 | 2011-01-25 22:50:47 +0000 | [diff] [blame] | 166 |  | 
| Ted Kremenek | 5811f59 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 167 | CFGStmt CS = cast<CFGStmt>(*ri); | 
| Ted Kremenek | f1d10d9 | 2011-08-23 23:05:04 +0000 | [diff] [blame] | 168 | const Stmt *S = CS.getStmt(); | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 169 | if (isa<ReturnStmt>(S)) { | 
|  | 170 | HasLiveReturn = true; | 
|  | 171 | continue; | 
|  | 172 | } | 
|  | 173 | if (isa<ObjCAtThrowStmt>(S)) { | 
|  | 174 | HasFakeEdge = true; | 
|  | 175 | continue; | 
|  | 176 | } | 
|  | 177 | if (isa<CXXThrowExpr>(S)) { | 
|  | 178 | HasFakeEdge = true; | 
|  | 179 | continue; | 
|  | 180 | } | 
|  | 181 | if (const AsmStmt *AS = dyn_cast<AsmStmt>(S)) { | 
|  | 182 | if (AS->isMSAsm()) { | 
|  | 183 | HasFakeEdge = true; | 
|  | 184 | HasLiveReturn = true; | 
|  | 185 | continue; | 
|  | 186 | } | 
|  | 187 | } | 
|  | 188 | if (isa<CXXTryStmt>(S)) { | 
|  | 189 | HasAbnormalEdge = true; | 
|  | 190 | continue; | 
|  | 191 | } | 
| Chandler Carruth | e05ee6d | 2011-09-13 09:53:58 +0000 | [diff] [blame] | 192 | if (std::find(B.succ_begin(), B.succ_end(), &cfg->getExit()) | 
|  | 193 | == B.succ_end()) { | 
|  | 194 | HasAbnormalEdge = true; | 
|  | 195 | continue; | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 196 | } | 
| Chandler Carruth | e05ee6d | 2011-09-13 09:53:58 +0000 | [diff] [blame] | 197 |  | 
|  | 198 | HasPlainEdge = true; | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 199 | } | 
|  | 200 | if (!HasPlainEdge) { | 
|  | 201 | if (HasLiveReturn) | 
|  | 202 | return NeverFallThrough; | 
|  | 203 | return NeverFallThroughOrReturn; | 
|  | 204 | } | 
|  | 205 | if (HasAbnormalEdge || HasFakeEdge || HasLiveReturn) | 
|  | 206 | return MaybeFallThrough; | 
|  | 207 | // This says AlwaysFallThrough for calls to functions that are not marked | 
|  | 208 | // noreturn, that don't return.  If people would like this warning to be more | 
|  | 209 | // accurate, such functions should be marked as noreturn. | 
|  | 210 | return AlwaysFallThrough; | 
|  | 211 | } | 
|  | 212 |  | 
| Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 213 | namespace { | 
|  | 214 |  | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 215 | struct CheckFallThroughDiagnostics { | 
|  | 216 | unsigned diag_MaybeFallThrough_HasNoReturn; | 
|  | 217 | unsigned diag_MaybeFallThrough_ReturnsNonVoid; | 
|  | 218 | unsigned diag_AlwaysFallThrough_HasNoReturn; | 
|  | 219 | unsigned diag_AlwaysFallThrough_ReturnsNonVoid; | 
|  | 220 | unsigned diag_NeverFallThroughOrReturn; | 
|  | 221 | bool funMode; | 
| Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 222 | SourceLocation FuncLoc; | 
| Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 223 |  | 
| Douglas Gregor | ca7eaee | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 224 | static CheckFallThroughDiagnostics MakeForFunction(const Decl *Func) { | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 225 | CheckFallThroughDiagnostics D; | 
| Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 226 | D.FuncLoc = Func->getLocation(); | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 227 | D.diag_MaybeFallThrough_HasNoReturn = | 
|  | 228 | diag::warn_falloff_noreturn_function; | 
|  | 229 | D.diag_MaybeFallThrough_ReturnsNonVoid = | 
|  | 230 | diag::warn_maybe_falloff_nonvoid_function; | 
|  | 231 | D.diag_AlwaysFallThrough_HasNoReturn = | 
|  | 232 | diag::warn_falloff_noreturn_function; | 
|  | 233 | D.diag_AlwaysFallThrough_ReturnsNonVoid = | 
|  | 234 | diag::warn_falloff_nonvoid_function; | 
| Douglas Gregor | ca7eaee | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 235 |  | 
|  | 236 | // Don't suggest that virtual functions be marked "noreturn", since they | 
|  | 237 | // might be overridden by non-noreturn functions. | 
|  | 238 | bool isVirtualMethod = false; | 
|  | 239 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Func)) | 
|  | 240 | isVirtualMethod = Method->isVirtual(); | 
|  | 241 |  | 
|  | 242 | if (!isVirtualMethod) | 
|  | 243 | D.diag_NeverFallThroughOrReturn = | 
|  | 244 | diag::warn_suggest_noreturn_function; | 
|  | 245 | else | 
|  | 246 | D.diag_NeverFallThroughOrReturn = 0; | 
|  | 247 |  | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 248 | D.funMode = true; | 
|  | 249 | return D; | 
|  | 250 | } | 
| Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 251 |  | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 252 | static CheckFallThroughDiagnostics MakeForBlock() { | 
|  | 253 | CheckFallThroughDiagnostics D; | 
|  | 254 | D.diag_MaybeFallThrough_HasNoReturn = | 
|  | 255 | diag::err_noreturn_block_has_return_expr; | 
|  | 256 | D.diag_MaybeFallThrough_ReturnsNonVoid = | 
|  | 257 | diag::err_maybe_falloff_nonvoid_block; | 
|  | 258 | D.diag_AlwaysFallThrough_HasNoReturn = | 
|  | 259 | diag::err_noreturn_block_has_return_expr; | 
|  | 260 | D.diag_AlwaysFallThrough_ReturnsNonVoid = | 
|  | 261 | diag::err_falloff_nonvoid_block; | 
|  | 262 | D.diag_NeverFallThroughOrReturn = | 
|  | 263 | diag::warn_suggest_noreturn_block; | 
|  | 264 | D.funMode = false; | 
|  | 265 | return D; | 
|  | 266 | } | 
| Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 267 |  | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 268 | bool checkDiagnostics(Diagnostic &D, bool ReturnsVoid, | 
|  | 269 | bool HasNoReturn) const { | 
|  | 270 | if (funMode) { | 
| Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 271 | return (ReturnsVoid || | 
|  | 272 | D.getDiagnosticLevel(diag::warn_maybe_falloff_nonvoid_function, | 
|  | 273 | FuncLoc) == Diagnostic::Ignored) | 
|  | 274 | && (!HasNoReturn || | 
|  | 275 | D.getDiagnosticLevel(diag::warn_noreturn_function_has_return_expr, | 
|  | 276 | FuncLoc) == Diagnostic::Ignored) | 
|  | 277 | && (!ReturnsVoid || | 
|  | 278 | D.getDiagnosticLevel(diag::warn_suggest_noreturn_block, FuncLoc) | 
|  | 279 | == Diagnostic::Ignored); | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 280 | } | 
| Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 281 |  | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 282 | // For blocks. | 
|  | 283 | return  ReturnsVoid && !HasNoReturn | 
| Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 284 | && (!ReturnsVoid || | 
|  | 285 | D.getDiagnosticLevel(diag::warn_suggest_noreturn_block, FuncLoc) | 
|  | 286 | == Diagnostic::Ignored); | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 287 | } | 
|  | 288 | }; | 
|  | 289 |  | 
| Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 290 | } | 
|  | 291 |  | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 292 | /// CheckFallThroughForFunctionDef - Check that we don't fall off the end of a | 
|  | 293 | /// function that should return a value.  Check that we don't fall off the end | 
|  | 294 | /// of a noreturn function.  We assume that functions and blocks not marked | 
|  | 295 | /// noreturn will return. | 
|  | 296 | static void CheckFallThroughForBody(Sema &S, const Decl *D, const Stmt *Body, | 
| Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 297 | const BlockExpr *blkExpr, | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 298 | const CheckFallThroughDiagnostics& CD, | 
|  | 299 | AnalysisContext &AC) { | 
|  | 300 |  | 
|  | 301 | bool ReturnsVoid = false; | 
|  | 302 | bool HasNoReturn = false; | 
|  | 303 |  | 
|  | 304 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { | 
|  | 305 | ReturnsVoid = FD->getResultType()->isVoidType(); | 
|  | 306 | HasNoReturn = FD->hasAttr<NoReturnAttr>() || | 
| Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 307 | FD->getType()->getAs<FunctionType>()->getNoReturnAttr(); | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 308 | } | 
|  | 309 | else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { | 
|  | 310 | ReturnsVoid = MD->getResultType()->isVoidType(); | 
|  | 311 | HasNoReturn = MD->hasAttr<NoReturnAttr>(); | 
|  | 312 | } | 
|  | 313 | else if (isa<BlockDecl>(D)) { | 
| Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 314 | QualType BlockTy = blkExpr->getType(); | 
| Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 315 | if (const FunctionType *FT = | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 316 | BlockTy->getPointeeType()->getAs<FunctionType>()) { | 
|  | 317 | if (FT->getResultType()->isVoidType()) | 
|  | 318 | ReturnsVoid = true; | 
|  | 319 | if (FT->getNoReturnAttr()) | 
|  | 320 | HasNoReturn = true; | 
|  | 321 | } | 
|  | 322 | } | 
|  | 323 |  | 
|  | 324 | Diagnostic &Diags = S.getDiagnostics(); | 
|  | 325 |  | 
|  | 326 | // Short circuit for compilation speed. | 
|  | 327 | if (CD.checkDiagnostics(Diags, ReturnsVoid, HasNoReturn)) | 
|  | 328 | return; | 
| Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 329 |  | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 330 | // FIXME: Function try block | 
|  | 331 | if (const CompoundStmt *Compound = dyn_cast<CompoundStmt>(Body)) { | 
|  | 332 | switch (CheckFallThrough(AC)) { | 
| John McCall | 16565aa | 2010-05-16 09:34:11 +0000 | [diff] [blame] | 333 | case UnknownFallThrough: | 
|  | 334 | break; | 
|  | 335 |  | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 336 | case MaybeFallThrough: | 
|  | 337 | if (HasNoReturn) | 
|  | 338 | S.Diag(Compound->getRBracLoc(), | 
|  | 339 | CD.diag_MaybeFallThrough_HasNoReturn); | 
|  | 340 | else if (!ReturnsVoid) | 
|  | 341 | S.Diag(Compound->getRBracLoc(), | 
|  | 342 | CD.diag_MaybeFallThrough_ReturnsNonVoid); | 
|  | 343 | break; | 
|  | 344 | case AlwaysFallThrough: | 
|  | 345 | if (HasNoReturn) | 
|  | 346 | S.Diag(Compound->getRBracLoc(), | 
|  | 347 | CD.diag_AlwaysFallThrough_HasNoReturn); | 
|  | 348 | else if (!ReturnsVoid) | 
|  | 349 | S.Diag(Compound->getRBracLoc(), | 
|  | 350 | CD.diag_AlwaysFallThrough_ReturnsNonVoid); | 
|  | 351 | break; | 
|  | 352 | case NeverFallThroughOrReturn: | 
| Chandler Carruth | b0656ec | 2011-08-31 09:01:53 +0000 | [diff] [blame] | 353 | if (ReturnsVoid && !HasNoReturn && CD.diag_NeverFallThroughOrReturn) { | 
|  | 354 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { | 
|  | 355 | S.Diag(Compound->getLBracLoc(), CD.diag_NeverFallThroughOrReturn) | 
| Douglas Gregor | b332109 | 2011-09-10 00:56:20 +0000 | [diff] [blame] | 356 | << 0 << FD; | 
|  | 357 | } else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { | 
|  | 358 | S.Diag(Compound->getLBracLoc(), CD.diag_NeverFallThroughOrReturn) | 
|  | 359 | << 1 << MD; | 
| Chandler Carruth | b0656ec | 2011-08-31 09:01:53 +0000 | [diff] [blame] | 360 | } else { | 
|  | 361 | S.Diag(Compound->getLBracLoc(), CD.diag_NeverFallThroughOrReturn); | 
|  | 362 | } | 
|  | 363 | } | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 364 | break; | 
|  | 365 | case NeverFallThrough: | 
|  | 366 | break; | 
|  | 367 | } | 
|  | 368 | } | 
|  | 369 | } | 
|  | 370 |  | 
|  | 371 | //===----------------------------------------------------------------------===// | 
| Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 372 | // -Wuninitialized | 
|  | 373 | //===----------------------------------------------------------------------===// | 
|  | 374 |  | 
| Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 375 | namespace { | 
| Chandler Carruth | 9f64946 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 376 | /// ContainsReference - A visitor class to search for references to | 
|  | 377 | /// a particular declaration (the needle) within any evaluated component of an | 
|  | 378 | /// expression (recursively). | 
| Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 379 | class ContainsReference : public EvaluatedExprVisitor<ContainsReference> { | 
| Chandler Carruth | 9f64946 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 380 | bool FoundReference; | 
|  | 381 | const DeclRefExpr *Needle; | 
|  | 382 |  | 
| Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 383 | public: | 
| Chandler Carruth | 9f64946 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 384 | ContainsReference(ASTContext &Context, const DeclRefExpr *Needle) | 
|  | 385 | : EvaluatedExprVisitor<ContainsReference>(Context), | 
|  | 386 | FoundReference(false), Needle(Needle) {} | 
|  | 387 |  | 
|  | 388 | void VisitExpr(Expr *E) { | 
| Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 389 | // Stop evaluating if we already have a reference. | 
| Chandler Carruth | 9f64946 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 390 | if (FoundReference) | 
| Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 391 | return; | 
| Chandler Carruth | 9f64946 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 392 |  | 
|  | 393 | EvaluatedExprVisitor<ContainsReference>::VisitExpr(E); | 
| Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 394 | } | 
| Chandler Carruth | 9f64946 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 395 |  | 
|  | 396 | void VisitDeclRefExpr(DeclRefExpr *E) { | 
|  | 397 | if (E == Needle) | 
|  | 398 | FoundReference = true; | 
|  | 399 | else | 
|  | 400 | EvaluatedExprVisitor<ContainsReference>::VisitDeclRefExpr(E); | 
| Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 401 | } | 
| Chandler Carruth | 9f64946 | 2011-04-05 06:48:00 +0000 | [diff] [blame] | 402 |  | 
|  | 403 | bool doesContainReference() const { return FoundReference; } | 
| Ted Kremenek | 6f41715 | 2011-04-04 20:56:00 +0000 | [diff] [blame] | 404 | }; | 
|  | 405 | } | 
|  | 406 |  | 
| David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 407 | static bool SuggestInitializationFixit(Sema &S, const VarDecl *VD) { | 
|  | 408 | // Don't issue a fixit if there is already an initializer. | 
|  | 409 | if (VD->getInit()) | 
|  | 410 | return false; | 
|  | 411 |  | 
|  | 412 | // Suggest possible initialization (if any). | 
|  | 413 | const char *initialization = 0; | 
|  | 414 | QualType VariableTy = VD->getType().getCanonicalType(); | 
|  | 415 |  | 
|  | 416 | if (VariableTy->isObjCObjectPointerType() || | 
|  | 417 | VariableTy->isBlockPointerType()) { | 
|  | 418 | // Check if 'nil' is defined. | 
|  | 419 | if (S.PP.getMacroInfo(&S.getASTContext().Idents.get("nil"))) | 
|  | 420 | initialization = " = nil"; | 
|  | 421 | else | 
|  | 422 | initialization = " = 0"; | 
|  | 423 | } | 
|  | 424 | else if (VariableTy->isRealFloatingType()) | 
|  | 425 | initialization = " = 0.0"; | 
|  | 426 | else if (VariableTy->isBooleanType() && S.Context.getLangOptions().CPlusPlus) | 
|  | 427 | initialization = " = false"; | 
|  | 428 | else if (VariableTy->isEnumeralType()) | 
|  | 429 | return false; | 
|  | 430 | else if (VariableTy->isPointerType() || VariableTy->isMemberPointerType()) { | 
|  | 431 | if (S.Context.getLangOptions().CPlusPlus0x) | 
|  | 432 | initialization = " = nullptr"; | 
|  | 433 | // Check if 'NULL' is defined. | 
|  | 434 | else if (S.PP.getMacroInfo(&S.getASTContext().Idents.get("NULL"))) | 
|  | 435 | initialization = " = NULL"; | 
|  | 436 | else | 
|  | 437 | initialization = " = 0"; | 
|  | 438 | } | 
|  | 439 | else if (VariableTy->isScalarType()) | 
|  | 440 | initialization = " = 0"; | 
|  | 441 |  | 
|  | 442 | if (initialization) { | 
|  | 443 | SourceLocation loc = S.PP.getLocForEndOfToken(VD->getLocEnd()); | 
|  | 444 | S.Diag(loc, diag::note_var_fixit_add_initialization) << VD->getDeclName() | 
|  | 445 | << FixItHint::CreateInsertion(loc, initialization); | 
|  | 446 | return true; | 
|  | 447 | } | 
|  | 448 | return false; | 
|  | 449 | } | 
|  | 450 |  | 
| Chandler Carruth | 262d50e | 2011-04-05 18:27:05 +0000 | [diff] [blame] | 451 | /// DiagnoseUninitializedUse -- Helper function for diagnosing uses of an | 
|  | 452 | /// uninitialized variable. This manages the different forms of diagnostic | 
|  | 453 | /// emitted for particular types of uses. Returns true if the use was diagnosed | 
|  | 454 | /// as a warning. If a pariticular use is one we omit warnings for, returns | 
|  | 455 | /// false. | 
|  | 456 | static bool DiagnoseUninitializedUse(Sema &S, const VarDecl *VD, | 
| Chandler Carruth | 64fb959 | 2011-04-05 18:18:08 +0000 | [diff] [blame] | 457 | const Expr *E, bool isAlwaysUninit) { | 
| Chandler Carruth | 4c4983b | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 458 | bool isSelfInit = false; | 
|  | 459 |  | 
|  | 460 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { | 
|  | 461 | if (isAlwaysUninit) { | 
|  | 462 | // Inspect the initializer of the variable declaration which is | 
|  | 463 | // being referenced prior to its initialization. We emit | 
|  | 464 | // specialized diagnostics for self-initialization, and we | 
|  | 465 | // specifically avoid warning about self references which take the | 
|  | 466 | // form of: | 
|  | 467 | // | 
|  | 468 | //   int x = x; | 
|  | 469 | // | 
|  | 470 | // This is used to indicate to GCC that 'x' is intentionally left | 
|  | 471 | // uninitialized. Proven code paths which access 'x' in | 
|  | 472 | // an uninitialized state after this will still warn. | 
|  | 473 | // | 
|  | 474 | // TODO: Should we suppress maybe-uninitialized warnings for | 
|  | 475 | // variables initialized in this way? | 
|  | 476 | if (const Expr *Initializer = VD->getInit()) { | 
|  | 477 | if (DRE == Initializer->IgnoreParenImpCasts()) | 
| Chandler Carruth | 262d50e | 2011-04-05 18:27:05 +0000 | [diff] [blame] | 478 | return false; | 
| Chandler Carruth | 4c4983b | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 479 |  | 
|  | 480 | ContainsReference CR(S.Context, DRE); | 
|  | 481 | CR.Visit(const_cast<Expr*>(Initializer)); | 
|  | 482 | isSelfInit = CR.doesContainReference(); | 
|  | 483 | } | 
|  | 484 | if (isSelfInit) { | 
|  | 485 | S.Diag(DRE->getLocStart(), | 
|  | 486 | diag::warn_uninit_self_reference_in_init) | 
|  | 487 | << VD->getDeclName() << VD->getLocation() << DRE->getSourceRange(); | 
|  | 488 | } else { | 
|  | 489 | S.Diag(DRE->getLocStart(), diag::warn_uninit_var) | 
|  | 490 | << VD->getDeclName() << DRE->getSourceRange(); | 
|  | 491 | } | 
|  | 492 | } else { | 
|  | 493 | S.Diag(DRE->getLocStart(), diag::warn_maybe_uninit_var) | 
|  | 494 | << VD->getDeclName() << DRE->getSourceRange(); | 
|  | 495 | } | 
|  | 496 | } else { | 
|  | 497 | const BlockExpr *BE = cast<BlockExpr>(E); | 
|  | 498 | S.Diag(BE->getLocStart(), | 
|  | 499 | isAlwaysUninit ? diag::warn_uninit_var_captured_by_block | 
|  | 500 | : diag::warn_maybe_uninit_var_captured_by_block) | 
|  | 501 | << VD->getDeclName(); | 
|  | 502 | } | 
|  | 503 |  | 
|  | 504 | // Report where the variable was declared when the use wasn't within | 
| David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 505 | // the initializer of that declaration & we didn't already suggest | 
|  | 506 | // an initialization fixit. | 
|  | 507 | if (!isSelfInit && !SuggestInitializationFixit(S, VD)) | 
| Chandler Carruth | 4c4983b | 2011-04-05 18:18:05 +0000 | [diff] [blame] | 508 | S.Diag(VD->getLocStart(), diag::note_uninit_var_def) | 
|  | 509 | << VD->getDeclName(); | 
|  | 510 |  | 
| Chandler Carruth | 262d50e | 2011-04-05 18:27:05 +0000 | [diff] [blame] | 511 | return true; | 
| Chandler Carruth | 64fb959 | 2011-04-05 18:18:08 +0000 | [diff] [blame] | 512 | } | 
|  | 513 |  | 
| Ted Kremenek | f7bafc7 | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 514 | typedef std::pair<const Expr*, bool> UninitUse; | 
|  | 515 |  | 
| Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 516 | namespace { | 
| Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 517 | struct SLocSort { | 
| Ted Kremenek | f7bafc7 | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 518 | bool operator()(const UninitUse &a, const UninitUse &b) { | 
|  | 519 | SourceLocation aLoc = a.first->getLocStart(); | 
|  | 520 | SourceLocation bLoc = b.first->getLocStart(); | 
| Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 521 | return aLoc.getRawEncoding() < bLoc.getRawEncoding(); | 
|  | 522 | } | 
|  | 523 | }; | 
|  | 524 |  | 
| Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 525 | class UninitValsDiagReporter : public UninitVariablesHandler { | 
|  | 526 | Sema &S; | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 527 | typedef SmallVector<UninitUse, 2> UsesVec; | 
| Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 528 | typedef llvm::DenseMap<const VarDecl *, UsesVec*> UsesMap; | 
|  | 529 | UsesMap *uses; | 
|  | 530 |  | 
| Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 531 | public: | 
| Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 532 | UninitValsDiagReporter(Sema &S) : S(S), uses(0) {} | 
|  | 533 | ~UninitValsDiagReporter() { | 
|  | 534 | flushDiagnostics(); | 
|  | 535 | } | 
| Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 536 |  | 
| Ted Kremenek | f7bafc7 | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 537 | void handleUseOfUninitVariable(const Expr *ex, const VarDecl *vd, | 
|  | 538 | bool isAlwaysUninit) { | 
| Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 539 | if (!uses) | 
|  | 540 | uses = new UsesMap(); | 
|  | 541 |  | 
|  | 542 | UsesVec *&vec = (*uses)[vd]; | 
|  | 543 | if (!vec) | 
|  | 544 | vec = new UsesVec(); | 
|  | 545 |  | 
| Ted Kremenek | f7bafc7 | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 546 | vec->push_back(std::make_pair(ex, isAlwaysUninit)); | 
| Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 547 | } | 
|  | 548 |  | 
|  | 549 | void flushDiagnostics() { | 
|  | 550 | if (!uses) | 
|  | 551 | return; | 
| Ted Kremenek | 609e317 | 2011-02-02 23:35:53 +0000 | [diff] [blame] | 552 |  | 
| Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 553 | for (UsesMap::iterator i = uses->begin(), e = uses->end(); i != e; ++i) { | 
|  | 554 | const VarDecl *vd = i->first; | 
|  | 555 | UsesVec *vec = i->second; | 
| Ted Kremenek | 609e317 | 2011-02-02 23:35:53 +0000 | [diff] [blame] | 556 |  | 
| Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 557 | // Sort the uses by their SourceLocations.  While not strictly | 
|  | 558 | // guaranteed to produce them in line/column order, this will provide | 
|  | 559 | // a stable ordering. | 
|  | 560 | std::sort(vec->begin(), vec->end(), SLocSort()); | 
|  | 561 |  | 
| Chandler Carruth | 64fb959 | 2011-04-05 18:18:08 +0000 | [diff] [blame] | 562 | for (UsesVec::iterator vi = vec->begin(), ve = vec->end(); vi != ve; | 
|  | 563 | ++vi) { | 
| David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 564 | if (DiagnoseUninitializedUse(S, vd, vi->first, | 
| Chandler Carruth | 262d50e | 2011-04-05 18:27:05 +0000 | [diff] [blame] | 565 | /*isAlwaysUninit=*/vi->second)) | 
| David Blaikie | 4f4f349 | 2011-09-10 05:35:08 +0000 | [diff] [blame] | 566 | // Skip further diagnostics for this variable. We try to warn only on | 
|  | 567 | // the first point at which a variable is used uninitialized. | 
|  | 568 | break; | 
| Chandler Carruth | 64fb959 | 2011-04-05 18:18:08 +0000 | [diff] [blame] | 569 | } | 
| Ted Kremenek | d40066b | 2011-04-04 23:29:12 +0000 | [diff] [blame] | 570 |  | 
| Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 571 | delete vec; | 
|  | 572 | } | 
|  | 573 | delete uses; | 
| Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 574 | } | 
|  | 575 | }; | 
|  | 576 | } | 
|  | 577 |  | 
| Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 578 |  | 
|  | 579 | //===----------------------------------------------------------------------===// | 
|  | 580 | // -Wthread-safety | 
|  | 581 | //===----------------------------------------------------------------------===// | 
| Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 582 | namespace clang { | 
|  | 583 | namespace thread_safety { | 
|  | 584 | typedef std::pair<SourceLocation, PartialDiagnostic> DelayedDiag; | 
|  | 585 | typedef llvm::SmallVector<DelayedDiag, 4> DiagList; | 
|  | 586 |  | 
| Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 587 | struct SortDiagBySourceLocation { | 
|  | 588 | Sema &S; | 
|  | 589 | SortDiagBySourceLocation(Sema &S) : S(S) {} | 
|  | 590 |  | 
|  | 591 | bool operator()(const DelayedDiag &left, const DelayedDiag &right) { | 
|  | 592 | // Although this call will be slow, this is only called when outputting | 
|  | 593 | // multiple warnings. | 
|  | 594 | return S.getSourceManager().isBeforeInTranslationUnit(left.first, | 
|  | 595 | right.first); | 
|  | 596 | } | 
|  | 597 | }; | 
|  | 598 |  | 
| Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 599 | class ThreadSafetyReporter : public clang::thread_safety::ThreadSafetyHandler { | 
|  | 600 | Sema &S; | 
|  | 601 | DiagList Warnings; | 
|  | 602 |  | 
|  | 603 | // Helper functions | 
|  | 604 | void warnLockMismatch(unsigned DiagID, Name LockName, SourceLocation Loc) { | 
|  | 605 | PartialDiagnostic Warning = S.PDiag(DiagID) << LockName; | 
|  | 606 | Warnings.push_back(DelayedDiag(Loc, Warning)); | 
|  | 607 | } | 
|  | 608 |  | 
|  | 609 | public: | 
|  | 610 | ThreadSafetyReporter(Sema &S) : S(S) {} | 
|  | 611 |  | 
|  | 612 | /// \brief Emit all buffered diagnostics in order of sourcelocation. | 
|  | 613 | /// We need to output diagnostics produced while iterating through | 
|  | 614 | /// the lockset in deterministic order, so this function orders diagnostics | 
|  | 615 | /// and outputs them. | 
|  | 616 | void emitDiagnostics() { | 
|  | 617 | SortDiagBySourceLocation SortDiagBySL(S); | 
|  | 618 | sort(Warnings.begin(), Warnings.end(), SortDiagBySL); | 
|  | 619 | for (DiagList::iterator I = Warnings.begin(), E = Warnings.end(); | 
|  | 620 | I != E; ++I) | 
|  | 621 | S.Diag(I->first, I->second); | 
|  | 622 | } | 
|  | 623 |  | 
| Caitlin Sadowski | 99107eb | 2011-09-09 16:21:55 +0000 | [diff] [blame] | 624 | void handleInvalidLockExp(SourceLocation Loc) { | 
|  | 625 | PartialDiagnostic Warning = S.PDiag(diag::warn_cannot_resolve_lock) << Loc; | 
|  | 626 | Warnings.push_back(DelayedDiag(Loc, Warning)); | 
|  | 627 | } | 
| Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 628 | void handleUnmatchedUnlock(Name LockName, SourceLocation Loc) { | 
|  | 629 | warnLockMismatch(diag::warn_unlock_but_no_lock, LockName, Loc); | 
|  | 630 | } | 
|  | 631 |  | 
|  | 632 | void handleDoubleLock(Name LockName, SourceLocation Loc) { | 
|  | 633 | warnLockMismatch(diag::warn_double_lock, LockName, Loc); | 
|  | 634 | } | 
|  | 635 |  | 
| Caitlin Sadowski | 4e4bc75 | 2011-09-15 17:25:19 +0000 | [diff] [blame^] | 636 | void handleMutexHeldEndOfScope(Name LockName, SourceLocation Loc, | 
|  | 637 | LockErrorKind LEK){ | 
|  | 638 | unsigned DiagID = 0; | 
|  | 639 | switch (LEK) { | 
|  | 640 | case LEK_LockedSomePredecessors: | 
|  | 641 | DiagID = diag::warn_lock_at_end_of_scope; | 
|  | 642 | break; | 
|  | 643 | case LEK_LockedSomeLoopIterations: | 
|  | 644 | DiagID = diag::warn_expecting_lock_held_on_loop; | 
|  | 645 | break; | 
|  | 646 | case LEK_LockedAtEndOfFunction: | 
|  | 647 | DiagID = diag::warn_no_unlock; | 
|  | 648 | break; | 
|  | 649 | } | 
|  | 650 | warnLockMismatch(DiagID, LockName, Loc); | 
| Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 651 | } | 
|  | 652 |  | 
| Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 653 |  | 
|  | 654 | void handleExclusiveAndShared(Name LockName, SourceLocation Loc1, | 
|  | 655 | SourceLocation Loc2) { | 
|  | 656 | PartialDiagnostic Warning = | 
|  | 657 | S.PDiag(diag::warn_lock_exclusive_and_shared) << LockName; | 
|  | 658 | PartialDiagnostic Note = | 
|  | 659 | S.PDiag(diag::note_lock_exclusive_and_shared) << LockName; | 
|  | 660 | Warnings.push_back(DelayedDiag(Loc1, Warning)); | 
|  | 661 | Warnings.push_back(DelayedDiag(Loc2, Note)); | 
|  | 662 | } | 
|  | 663 |  | 
|  | 664 | void handleNoMutexHeld(const NamedDecl *D, ProtectedOperationKind POK, | 
|  | 665 | AccessKind AK, SourceLocation Loc) { | 
| Caitlin Sadowski | df8327c | 2011-09-14 20:09:09 +0000 | [diff] [blame] | 666 | assert((POK == POK_VarAccess || POK == POK_VarDereference) | 
|  | 667 | && "Only works for variables"); | 
|  | 668 | unsigned DiagID = POK == POK_VarAccess? | 
|  | 669 | diag::warn_variable_requires_any_lock: | 
|  | 670 | diag::warn_var_deref_requires_any_lock; | 
|  | 671 | PartialDiagnostic Warning = S.PDiag(DiagID) | 
|  | 672 | << D->getName() << getLockKindFromAccessKind(AK); | 
|  | 673 | Warnings.push_back(DelayedDiag(Loc, Warning)); | 
| Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 674 | } | 
|  | 675 |  | 
|  | 676 | void handleMutexNotHeld(const NamedDecl *D, ProtectedOperationKind POK, | 
|  | 677 | Name LockName, LockKind LK, SourceLocation Loc) { | 
| Caitlin Sadowski | e87158d | 2011-09-13 18:01:58 +0000 | [diff] [blame] | 678 | unsigned DiagID = 0; | 
| Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 679 | switch (POK) { | 
|  | 680 | case POK_VarAccess: | 
|  | 681 | DiagID = diag::warn_variable_requires_lock; | 
|  | 682 | break; | 
|  | 683 | case POK_VarDereference: | 
|  | 684 | DiagID = diag::warn_var_deref_requires_lock; | 
|  | 685 | break; | 
|  | 686 | case POK_FunctionCall: | 
|  | 687 | DiagID = diag::warn_fun_requires_lock; | 
|  | 688 | break; | 
|  | 689 | } | 
|  | 690 | PartialDiagnostic Warning = S.PDiag(DiagID) | 
| Caitlin Sadowski | df8327c | 2011-09-14 20:09:09 +0000 | [diff] [blame] | 691 | << D->getName() << LockName << LK; | 
| Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 692 | Warnings.push_back(DelayedDiag(Loc, Warning)); | 
|  | 693 | } | 
|  | 694 |  | 
|  | 695 | void handleFunExcludesLock(Name FunName, Name LockName, SourceLocation Loc) { | 
|  | 696 | PartialDiagnostic Warning = | 
|  | 697 | S.PDiag(diag::warn_fun_excludes_mutex) << FunName << LockName; | 
|  | 698 | Warnings.push_back(DelayedDiag(Loc, Warning)); | 
|  | 699 | } | 
|  | 700 | }; | 
|  | 701 | } | 
|  | 702 | } | 
|  | 703 |  | 
| Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 704 | //===----------------------------------------------------------------------===// | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 705 | // AnalysisBasedWarnings - Worker object used by Sema to execute analysis-based | 
|  | 706 | //  warnings on a function, method, or block. | 
|  | 707 | //===----------------------------------------------------------------------===// | 
|  | 708 |  | 
| Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 709 | clang::sema::AnalysisBasedWarnings::Policy::Policy() { | 
|  | 710 | enableCheckFallThrough = 1; | 
|  | 711 | enableCheckUnreachable = 0; | 
| Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 712 | enableThreadSafetyAnalysis = 0; | 
| Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 713 | } | 
|  | 714 |  | 
| Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 715 | clang::sema::AnalysisBasedWarnings::AnalysisBasedWarnings(Sema &s) | 
|  | 716 | : S(s), | 
|  | 717 | NumFunctionsAnalyzed(0), | 
| Benjamin Kramer | 54cf341 | 2011-07-08 20:38:53 +0000 | [diff] [blame] | 718 | NumFunctionsWithBadCFGs(0), | 
| Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 719 | NumCFGBlocks(0), | 
| Benjamin Kramer | 54cf341 | 2011-07-08 20:38:53 +0000 | [diff] [blame] | 720 | MaxCFGBlocksPerFunction(0), | 
|  | 721 | NumUninitAnalysisFunctions(0), | 
|  | 722 | NumUninitAnalysisVariables(0), | 
|  | 723 | MaxUninitAnalysisVariablesPerFunction(0), | 
|  | 724 | NumUninitAnalysisBlockVisits(0), | 
|  | 725 | MaxUninitAnalysisBlockVisitsPerFunction(0) { | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 726 | Diagnostic &D = S.getDiagnostics(); | 
| Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 727 | DefaultPolicy.enableCheckUnreachable = (unsigned) | 
| Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 728 | (D.getDiagnosticLevel(diag::warn_unreachable, SourceLocation()) != | 
|  | 729 | Diagnostic::Ignored); | 
| Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 730 | DefaultPolicy.enableThreadSafetyAnalysis = (unsigned) | 
|  | 731 | (D.getDiagnosticLevel(diag::warn_double_lock, SourceLocation()) != | 
|  | 732 | Diagnostic::Ignored); | 
|  | 733 |  | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 734 | } | 
|  | 735 |  | 
| Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 736 | static void flushDiagnostics(Sema &S, sema::FunctionScopeInfo *fscope) { | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 737 | for (SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator | 
| Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 738 | i = fscope->PossiblyUnreachableDiags.begin(), | 
|  | 739 | e = fscope->PossiblyUnreachableDiags.end(); | 
|  | 740 | i != e; ++i) { | 
|  | 741 | const sema::PossiblyUnreachableDiag &D = *i; | 
|  | 742 | S.Diag(D.Loc, D.PD); | 
|  | 743 | } | 
|  | 744 | } | 
|  | 745 |  | 
| Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 746 | void clang::sema:: | 
|  | 747 | AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P, | 
| Ted Kremenek | 283a358 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 748 | sema::FunctionScopeInfo *fscope, | 
| Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 749 | const Decl *D, const BlockExpr *blkExpr) { | 
| Ted Kremenek | d068aab | 2010-03-20 21:11:09 +0000 | [diff] [blame] | 750 |  | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 751 | // We avoid doing analysis-based warnings when there are errors for | 
|  | 752 | // two reasons: | 
|  | 753 | // (1) The CFGs often can't be constructed (if the body is invalid), so | 
|  | 754 | //     don't bother trying. | 
|  | 755 | // (2) The code already has problems; running the analysis just takes more | 
|  | 756 | //     time. | 
| Ted Kremenek | 99e8192 | 2010-04-30 21:49:25 +0000 | [diff] [blame] | 757 | Diagnostic &Diags = S.getDiagnostics(); | 
|  | 758 |  | 
| Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 759 | // Do not do any analysis for declarations in system headers if we are | 
|  | 760 | // going to just ignore them. | 
| Ted Kremenek | 99e8192 | 2010-04-30 21:49:25 +0000 | [diff] [blame] | 761 | if (Diags.getSuppressSystemWarnings() && | 
| Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 762 | S.SourceMgr.isInSystemHeader(D->getLocation())) | 
|  | 763 | return; | 
|  | 764 |  | 
| John McCall | e0054f6 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 765 | // For code in dependent contexts, we'll do this at instantiation time. | 
|  | 766 | if (cast<DeclContext>(D)->isDependentContext()) | 
|  | 767 | return; | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 768 |  | 
| Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 769 | if (Diags.hasErrorOccurred() || Diags.hasFatalErrorOccurred()) { | 
|  | 770 | // Flush out any possibly unreachable diagnostics. | 
|  | 771 | flushDiagnostics(S, fscope); | 
|  | 772 | return; | 
|  | 773 | } | 
|  | 774 |  | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 775 | const Stmt *Body = D->getBody(); | 
|  | 776 | assert(Body); | 
|  | 777 |  | 
| Ted Kremenek | bc5cb8a | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 778 | AnalysisContext AC(D, 0); | 
|  | 779 |  | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 780 | // Don't generate EH edges for CallExprs as we'd like to avoid the n^2 | 
|  | 781 | // explosion for destrutors that can result and the compile time hit. | 
| Ted Kremenek | bc5cb8a | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 782 | AC.getCFGBuildOptions().PruneTriviallyFalseEdges = true; | 
|  | 783 | AC.getCFGBuildOptions().AddEHEdges = false; | 
|  | 784 | AC.getCFGBuildOptions().AddInitializers = true; | 
|  | 785 | AC.getCFGBuildOptions().AddImplicitDtors = true; | 
| Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 786 |  | 
|  | 787 | // Force that certain expressions appear as CFGElements in the CFG.  This | 
|  | 788 | // is used to speed up various analyses. | 
|  | 789 | // FIXME: This isn't the right factoring.  This is here for initial | 
|  | 790 | // prototyping, but we need a way for analyses to say what expressions they | 
|  | 791 | // expect to always be CFGElements and then fill in the BuildOptions | 
|  | 792 | // appropriately.  This is essentially a layering violation. | 
| Ted Kremenek | 0f3b4ca | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 793 | if (P.enableCheckUnreachable) { | 
|  | 794 | // Unreachable code analysis requires a linearized CFG. | 
|  | 795 | AC.getCFGBuildOptions().setAllAlwaysAdd(); | 
|  | 796 | } | 
|  | 797 | else { | 
|  | 798 | AC.getCFGBuildOptions() | 
|  | 799 | .setAlwaysAdd(Stmt::BinaryOperatorClass) | 
|  | 800 | .setAlwaysAdd(Stmt::BlockExprClass) | 
|  | 801 | .setAlwaysAdd(Stmt::CStyleCastExprClass) | 
|  | 802 | .setAlwaysAdd(Stmt::DeclRefExprClass) | 
|  | 803 | .setAlwaysAdd(Stmt::ImplicitCastExprClass) | 
|  | 804 | .setAlwaysAdd(Stmt::UnaryOperatorClass); | 
|  | 805 | } | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 806 |  | 
| Ted Kremenek | bc5cb8a | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 807 | // Construct the analysis context with the specified CFG build options. | 
|  | 808 |  | 
| Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 809 | // Emit delayed diagnostics. | 
|  | 810 | if (!fscope->PossiblyUnreachableDiags.empty()) { | 
|  | 811 | bool analyzed = false; | 
| Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 812 |  | 
|  | 813 | // Register the expressions with the CFGBuilder. | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 814 | for (SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator | 
| Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 815 | i = fscope->PossiblyUnreachableDiags.begin(), | 
|  | 816 | e = fscope->PossiblyUnreachableDiags.end(); | 
|  | 817 | i != e; ++i) { | 
|  | 818 | if (const Stmt *stmt = i->stmt) | 
|  | 819 | AC.registerForcedBlockExpression(stmt); | 
|  | 820 | } | 
|  | 821 |  | 
|  | 822 | if (AC.getCFG()) { | 
|  | 823 | analyzed = true; | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 824 | for (SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator | 
| Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 825 | i = fscope->PossiblyUnreachableDiags.begin(), | 
|  | 826 | e = fscope->PossiblyUnreachableDiags.end(); | 
|  | 827 | i != e; ++i) | 
|  | 828 | { | 
|  | 829 | const sema::PossiblyUnreachableDiag &D = *i; | 
|  | 830 | bool processed = false; | 
|  | 831 | if (const Stmt *stmt = i->stmt) { | 
|  | 832 | const CFGBlock *block = AC.getBlockForRegisteredExpression(stmt); | 
|  | 833 | assert(block); | 
| Ted Kremenek | af13d5b | 2011-03-19 01:00:33 +0000 | [diff] [blame] | 834 | if (CFGReverseBlockReachabilityAnalysis *cra = AC.getCFGReachablityAnalysis()) { | 
| Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 835 | // Can this block be reached from the entrance? | 
| Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 836 | if (cra->isReachable(&AC.getCFG()->getEntry(), block)) | 
| Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 837 | S.Diag(D.Loc, D.PD); | 
| Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 838 | processed = true; | 
| Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 839 | } | 
|  | 840 | } | 
| Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 841 | if (!processed) { | 
|  | 842 | // Emit the warning anyway if we cannot map to a basic block. | 
|  | 843 | S.Diag(D.Loc, D.PD); | 
|  | 844 | } | 
| Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 845 | } | 
| Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 846 | } | 
| Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 847 |  | 
|  | 848 | if (!analyzed) | 
|  | 849 | flushDiagnostics(S, fscope); | 
|  | 850 | } | 
|  | 851 |  | 
|  | 852 |  | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 853 | // Warning: check missing 'return' | 
| Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 854 | if (P.enableCheckFallThrough) { | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 855 | const CheckFallThroughDiagnostics &CD = | 
|  | 856 | (isa<BlockDecl>(D) ? CheckFallThroughDiagnostics::MakeForBlock() | 
| Douglas Gregor | ca7eaee | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 857 | : CheckFallThroughDiagnostics::MakeForFunction(D)); | 
| Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 858 | CheckFallThroughForBody(S, D, Body, blkExpr, CD, AC); | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 859 | } | 
|  | 860 |  | 
|  | 861 | // Warning: check for unreachable code | 
| Ted Kremenek | b7e5f14 | 2010-04-08 18:51:44 +0000 | [diff] [blame] | 862 | if (P.enableCheckUnreachable) | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 863 | CheckUnreachable(S, AC); | 
| Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 864 |  | 
| Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 865 | // Check for thread safety violations | 
| Caitlin Sadowski | 75f23ae | 2011-09-09 16:04:02 +0000 | [diff] [blame] | 866 | if (P.enableThreadSafetyAnalysis) { | 
|  | 867 | thread_safety::ThreadSafetyReporter Reporter(S); | 
|  | 868 | thread_safety::runThreadSafetyAnalysis(AC, Reporter); | 
|  | 869 | Reporter.emitDiagnostics(); | 
|  | 870 | } | 
| Caitlin Sadowski | 3ac1fbc | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 871 |  | 
| Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 872 | if (Diags.getDiagnosticLevel(diag::warn_uninit_var, D->getLocStart()) | 
| Ted Kremenek | 76709bf | 2011-03-15 05:22:28 +0000 | [diff] [blame] | 873 | != Diagnostic::Ignored || | 
|  | 874 | Diags.getDiagnosticLevel(diag::warn_maybe_uninit_var, D->getLocStart()) | 
| Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 875 | != Diagnostic::Ignored) { | 
| Ted Kremenek | c5e43c1 | 2011-03-17 05:29:57 +0000 | [diff] [blame] | 876 | if (CFG *cfg = AC.getCFG()) { | 
| Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 877 | UninitValsDiagReporter reporter(S); | 
| Fariborz Jahanian | 57080fb | 2011-07-16 18:31:33 +0000 | [diff] [blame] | 878 | UninitVariablesAnalysisStats stats; | 
| Benjamin Kramer | 12efd57 | 2011-07-16 20:13:06 +0000 | [diff] [blame] | 879 | std::memset(&stats, 0, sizeof(UninitVariablesAnalysisStats)); | 
| Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 880 | runUninitializedVariablesAnalysis(*cast<DeclContext>(D), *cfg, AC, | 
| Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 881 | reporter, stats); | 
|  | 882 |  | 
|  | 883 | if (S.CollectStats && stats.NumVariablesAnalyzed > 0) { | 
|  | 884 | ++NumUninitAnalysisFunctions; | 
|  | 885 | NumUninitAnalysisVariables += stats.NumVariablesAnalyzed; | 
|  | 886 | NumUninitAnalysisBlockVisits += stats.NumBlockVisits; | 
|  | 887 | MaxUninitAnalysisVariablesPerFunction = | 
|  | 888 | std::max(MaxUninitAnalysisVariablesPerFunction, | 
|  | 889 | stats.NumVariablesAnalyzed); | 
|  | 890 | MaxUninitAnalysisBlockVisitsPerFunction = | 
|  | 891 | std::max(MaxUninitAnalysisBlockVisitsPerFunction, | 
|  | 892 | stats.NumBlockVisits); | 
|  | 893 | } | 
| Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 894 | } | 
|  | 895 | } | 
| Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 896 |  | 
|  | 897 | // Collect statistics about the CFG if it was built. | 
|  | 898 | if (S.CollectStats && AC.isCFGBuilt()) { | 
|  | 899 | ++NumFunctionsAnalyzed; | 
|  | 900 | if (CFG *cfg = AC.getCFG()) { | 
|  | 901 | // If we successfully built a CFG for this context, record some more | 
|  | 902 | // detail information about it. | 
| Chandler Carruth | 3ea4c49 | 2011-07-06 22:21:45 +0000 | [diff] [blame] | 903 | NumCFGBlocks += cfg->getNumBlockIDs(); | 
| Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 904 | MaxCFGBlocksPerFunction = std::max(MaxCFGBlocksPerFunction, | 
| Chandler Carruth | 3ea4c49 | 2011-07-06 22:21:45 +0000 | [diff] [blame] | 905 | cfg->getNumBlockIDs()); | 
| Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 906 | } else { | 
|  | 907 | ++NumFunctionsWithBadCFGs; | 
|  | 908 | } | 
|  | 909 | } | 
|  | 910 | } | 
|  | 911 |  | 
|  | 912 | void clang::sema::AnalysisBasedWarnings::PrintStats() const { | 
|  | 913 | llvm::errs() << "\n*** Analysis Based Warnings Stats:\n"; | 
|  | 914 |  | 
|  | 915 | unsigned NumCFGsBuilt = NumFunctionsAnalyzed - NumFunctionsWithBadCFGs; | 
|  | 916 | unsigned AvgCFGBlocksPerFunction = | 
|  | 917 | !NumCFGsBuilt ? 0 : NumCFGBlocks/NumCFGsBuilt; | 
|  | 918 | llvm::errs() << NumFunctionsAnalyzed << " functions analyzed (" | 
|  | 919 | << NumFunctionsWithBadCFGs << " w/o CFGs).\n" | 
|  | 920 | << "  " << NumCFGBlocks << " CFG blocks built.\n" | 
|  | 921 | << "  " << AvgCFGBlocksPerFunction | 
|  | 922 | << " average CFG blocks per function.\n" | 
|  | 923 | << "  " << MaxCFGBlocksPerFunction | 
|  | 924 | << " max CFG blocks per function.\n"; | 
|  | 925 |  | 
|  | 926 | unsigned AvgUninitVariablesPerFunction = !NumUninitAnalysisFunctions ? 0 | 
|  | 927 | : NumUninitAnalysisVariables/NumUninitAnalysisFunctions; | 
|  | 928 | unsigned AvgUninitBlockVisitsPerFunction = !NumUninitAnalysisFunctions ? 0 | 
|  | 929 | : NumUninitAnalysisBlockVisits/NumUninitAnalysisFunctions; | 
|  | 930 | llvm::errs() << NumUninitAnalysisFunctions | 
|  | 931 | << " functions analyzed for uninitialiazed variables\n" | 
|  | 932 | << "  " << NumUninitAnalysisVariables << " variables analyzed.\n" | 
|  | 933 | << "  " << AvgUninitVariablesPerFunction | 
|  | 934 | << " average variables per function.\n" | 
|  | 935 | << "  " << MaxUninitAnalysisVariablesPerFunction | 
|  | 936 | << " max variables per function.\n" | 
|  | 937 | << "  " << NumUninitAnalysisBlockVisits << " block visits.\n" | 
|  | 938 | << "  " << AvgUninitBlockVisitsPerFunction | 
|  | 939 | << " average block visits per function.\n" | 
|  | 940 | << "  " << MaxUninitAnalysisBlockVisitsPerFunction | 
|  | 941 | << " max block visits per function.\n"; | 
| Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 942 | } |