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 | d068aab | 2010-03-20 21:11:09 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclCXX.h" |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprObjC.h" |
| 22 | #include "clang/AST/ExprCXX.h" |
| 23 | #include "clang/AST/StmtObjC.h" |
| 24 | #include "clang/AST/StmtCXX.h" |
| 25 | #include "clang/Analysis/AnalysisContext.h" |
| 26 | #include "clang/Analysis/CFG.h" |
| 27 | #include "clang/Analysis/Analyses/ReachableCode.h" |
| 28 | #include "llvm/ADT/BitVector.h" |
| 29 | #include "llvm/Support/Casting.h" |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace clang; |
| 32 | |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | // Unreachable code analysis. |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | |
| 37 | namespace { |
| 38 | class UnreachableCodeHandler : public reachable_code::Callback { |
| 39 | Sema &S; |
| 40 | public: |
| 41 | UnreachableCodeHandler(Sema &s) : S(s) {} |
| 42 | |
| 43 | void HandleUnreachable(SourceLocation L, SourceRange R1, SourceRange R2) { |
| 44 | S.Diag(L, diag::warn_unreachable) << R1 << R2; |
| 45 | } |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | /// CheckUnreachable - Check for unreachable code. |
| 50 | static void CheckUnreachable(Sema &S, AnalysisContext &AC) { |
| 51 | UnreachableCodeHandler UC(S); |
| 52 | reachable_code::FindUnreachableCode(AC, UC); |
| 53 | } |
| 54 | |
| 55 | //===----------------------------------------------------------------------===// |
| 56 | // Check for missing return value. |
| 57 | //===----------------------------------------------------------------------===// |
| 58 | |
John McCall | 16565aa | 2010-05-16 09:34:11 +0000 | [diff] [blame] | 59 | enum ControlFlowKind { |
| 60 | UnknownFallThrough, |
| 61 | NeverFallThrough, |
| 62 | MaybeFallThrough, |
| 63 | AlwaysFallThrough, |
| 64 | NeverFallThroughOrReturn |
| 65 | }; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 66 | |
| 67 | /// CheckFallThrough - Check that we don't fall off the end of a |
| 68 | /// Statement that should return a value. |
| 69 | /// |
| 70 | /// \returns AlwaysFallThrough iff we always fall off the end of the statement, |
| 71 | /// MaybeFallThrough iff we might or might not fall off the end, |
| 72 | /// NeverFallThroughOrReturn iff we never fall off the end of the statement or |
| 73 | /// return. We assume NeverFallThrough iff we never fall off the end of the |
| 74 | /// statement but we may return. We assume that functions not marked noreturn |
| 75 | /// will return. |
| 76 | static ControlFlowKind CheckFallThrough(AnalysisContext &AC) { |
| 77 | CFG *cfg = AC.getCFG(); |
John McCall | 16565aa | 2010-05-16 09:34:11 +0000 | [diff] [blame] | 78 | if (cfg == 0) return UnknownFallThrough; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 79 | |
| 80 | // The CFG leaves in dead things, and we don't want the dead code paths to |
| 81 | // confuse us, so we mark all live things first. |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 82 | llvm::BitVector live(cfg->getNumBlockIDs()); |
| 83 | unsigned count = reachable_code::ScanReachableFromBlock(cfg->getEntry(), |
| 84 | live); |
| 85 | |
| 86 | bool AddEHEdges = AC.getAddEHEdges(); |
| 87 | if (!AddEHEdges && count != cfg->getNumBlockIDs()) |
| 88 | // When there are things remaining dead, and we didn't add EH edges |
| 89 | // from CallExprs to the catch clauses, we have to go back and |
| 90 | // mark them as live. |
| 91 | for (CFG::iterator I = cfg->begin(), E = cfg->end(); I != E; ++I) { |
| 92 | CFGBlock &b = **I; |
| 93 | if (!live[b.getBlockID()]) { |
| 94 | if (b.pred_begin() == b.pred_end()) { |
| 95 | if (b.getTerminator() && isa<CXXTryStmt>(b.getTerminator())) |
| 96 | // When not adding EH edges from calls, catch clauses |
| 97 | // can otherwise seem dead. Avoid noting them as dead. |
| 98 | count += reachable_code::ScanReachableFromBlock(b, live); |
| 99 | continue; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // Now we know what is live, we check the live precessors of the exit block |
| 105 | // and look for fall through paths, being careful to ignore normal returns, |
| 106 | // and exceptional paths. |
| 107 | bool HasLiveReturn = false; |
| 108 | bool HasFakeEdge = false; |
| 109 | bool HasPlainEdge = false; |
| 110 | bool HasAbnormalEdge = false; |
Ted Kremenek | 90b828a | 2010-09-09 00:06:07 +0000 | [diff] [blame] | 111 | |
| 112 | // Ignore default cases that aren't likely to be reachable because all |
| 113 | // enums in a switch(X) have explicit case statements. |
| 114 | CFGBlock::FilterOptions FO; |
| 115 | FO.IgnoreDefaultsWithCoveredEnums = 1; |
| 116 | |
| 117 | for (CFGBlock::filtered_pred_iterator |
| 118 | I = cfg->getExit().filtered_pred_start_end(FO); I.hasMore(); ++I) { |
| 119 | const CFGBlock& B = **I; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 120 | if (!live[B.getBlockID()]) |
| 121 | continue; |
| 122 | if (B.size() == 0) { |
| 123 | if (B.getTerminator() && isa<CXXTryStmt>(B.getTerminator())) { |
| 124 | HasAbnormalEdge = true; |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | // A labeled empty statement, or the entry block... |
| 129 | HasPlainEdge = true; |
| 130 | continue; |
| 131 | } |
Zhongxing Xu | b36cd3e | 2010-09-16 01:25:47 +0000 | [diff] [blame] | 132 | CFGElement CE = B[B.size()-1]; |
| 133 | CFGStmt CS = CE.getAs<CFGStmt>(); |
| 134 | if (!CS.isValid()) |
| 135 | continue; |
| 136 | Stmt *S = CS.getStmt(); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 137 | if (isa<ReturnStmt>(S)) { |
| 138 | HasLiveReturn = true; |
| 139 | continue; |
| 140 | } |
| 141 | if (isa<ObjCAtThrowStmt>(S)) { |
| 142 | HasFakeEdge = true; |
| 143 | continue; |
| 144 | } |
| 145 | if (isa<CXXThrowExpr>(S)) { |
| 146 | HasFakeEdge = true; |
| 147 | continue; |
| 148 | } |
| 149 | if (const AsmStmt *AS = dyn_cast<AsmStmt>(S)) { |
| 150 | if (AS->isMSAsm()) { |
| 151 | HasFakeEdge = true; |
| 152 | HasLiveReturn = true; |
| 153 | continue; |
| 154 | } |
| 155 | } |
| 156 | if (isa<CXXTryStmt>(S)) { |
| 157 | HasAbnormalEdge = true; |
| 158 | continue; |
| 159 | } |
| 160 | |
| 161 | bool NoReturnEdge = false; |
| 162 | if (CallExpr *C = dyn_cast<CallExpr>(S)) { |
John McCall | 259d48e | 2010-04-30 07:10:06 +0000 | [diff] [blame] | 163 | if (std::find(B.succ_begin(), B.succ_end(), &cfg->getExit()) |
| 164 | == B.succ_end()) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 165 | HasAbnormalEdge = true; |
| 166 | continue; |
| 167 | } |
| 168 | Expr *CEE = C->getCallee()->IgnoreParenCasts(); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 169 | if (getFunctionExtInfo(CEE->getType()).getNoReturn()) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 170 | NoReturnEdge = true; |
| 171 | HasFakeEdge = true; |
| 172 | } else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE)) { |
| 173 | ValueDecl *VD = DRE->getDecl(); |
| 174 | if (VD->hasAttr<NoReturnAttr>()) { |
| 175 | NoReturnEdge = true; |
| 176 | HasFakeEdge = true; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | // FIXME: Add noreturn message sends. |
| 181 | if (NoReturnEdge == false) |
| 182 | HasPlainEdge = true; |
| 183 | } |
| 184 | if (!HasPlainEdge) { |
| 185 | if (HasLiveReturn) |
| 186 | return NeverFallThrough; |
| 187 | return NeverFallThroughOrReturn; |
| 188 | } |
| 189 | if (HasAbnormalEdge || HasFakeEdge || HasLiveReturn) |
| 190 | return MaybeFallThrough; |
| 191 | // This says AlwaysFallThrough for calls to functions that are not marked |
| 192 | // noreturn, that don't return. If people would like this warning to be more |
| 193 | // accurate, such functions should be marked as noreturn. |
| 194 | return AlwaysFallThrough; |
| 195 | } |
| 196 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 197 | namespace { |
| 198 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 199 | struct CheckFallThroughDiagnostics { |
| 200 | unsigned diag_MaybeFallThrough_HasNoReturn; |
| 201 | unsigned diag_MaybeFallThrough_ReturnsNonVoid; |
| 202 | unsigned diag_AlwaysFallThrough_HasNoReturn; |
| 203 | unsigned diag_AlwaysFallThrough_ReturnsNonVoid; |
| 204 | unsigned diag_NeverFallThroughOrReturn; |
| 205 | bool funMode; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 206 | SourceLocation FuncLoc; |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 207 | |
Douglas Gregor | ca7eaee | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 208 | static CheckFallThroughDiagnostics MakeForFunction(const Decl *Func) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 209 | CheckFallThroughDiagnostics D; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 210 | D.FuncLoc = Func->getLocation(); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 211 | D.diag_MaybeFallThrough_HasNoReturn = |
| 212 | diag::warn_falloff_noreturn_function; |
| 213 | D.diag_MaybeFallThrough_ReturnsNonVoid = |
| 214 | diag::warn_maybe_falloff_nonvoid_function; |
| 215 | D.diag_AlwaysFallThrough_HasNoReturn = |
| 216 | diag::warn_falloff_noreturn_function; |
| 217 | D.diag_AlwaysFallThrough_ReturnsNonVoid = |
| 218 | diag::warn_falloff_nonvoid_function; |
Douglas Gregor | ca7eaee | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 219 | |
| 220 | // Don't suggest that virtual functions be marked "noreturn", since they |
| 221 | // might be overridden by non-noreturn functions. |
| 222 | bool isVirtualMethod = false; |
| 223 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Func)) |
| 224 | isVirtualMethod = Method->isVirtual(); |
| 225 | |
| 226 | if (!isVirtualMethod) |
| 227 | D.diag_NeverFallThroughOrReturn = |
| 228 | diag::warn_suggest_noreturn_function; |
| 229 | else |
| 230 | D.diag_NeverFallThroughOrReturn = 0; |
| 231 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 232 | D.funMode = true; |
| 233 | return D; |
| 234 | } |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 235 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 236 | static CheckFallThroughDiagnostics MakeForBlock() { |
| 237 | CheckFallThroughDiagnostics D; |
| 238 | D.diag_MaybeFallThrough_HasNoReturn = |
| 239 | diag::err_noreturn_block_has_return_expr; |
| 240 | D.diag_MaybeFallThrough_ReturnsNonVoid = |
| 241 | diag::err_maybe_falloff_nonvoid_block; |
| 242 | D.diag_AlwaysFallThrough_HasNoReturn = |
| 243 | diag::err_noreturn_block_has_return_expr; |
| 244 | D.diag_AlwaysFallThrough_ReturnsNonVoid = |
| 245 | diag::err_falloff_nonvoid_block; |
| 246 | D.diag_NeverFallThroughOrReturn = |
| 247 | diag::warn_suggest_noreturn_block; |
| 248 | D.funMode = false; |
| 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 | bool checkDiagnostics(Diagnostic &D, bool ReturnsVoid, |
| 253 | bool HasNoReturn) const { |
| 254 | if (funMode) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 255 | return (ReturnsVoid || |
| 256 | D.getDiagnosticLevel(diag::warn_maybe_falloff_nonvoid_function, |
| 257 | FuncLoc) == Diagnostic::Ignored) |
| 258 | && (!HasNoReturn || |
| 259 | D.getDiagnosticLevel(diag::warn_noreturn_function_has_return_expr, |
| 260 | FuncLoc) == Diagnostic::Ignored) |
| 261 | && (!ReturnsVoid || |
| 262 | D.getDiagnosticLevel(diag::warn_suggest_noreturn_block, FuncLoc) |
| 263 | == Diagnostic::Ignored); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 264 | } |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 265 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 266 | // For blocks. |
| 267 | return ReturnsVoid && !HasNoReturn |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 268 | && (!ReturnsVoid || |
| 269 | D.getDiagnosticLevel(diag::warn_suggest_noreturn_block, FuncLoc) |
| 270 | == Diagnostic::Ignored); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 271 | } |
| 272 | }; |
| 273 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 276 | /// CheckFallThroughForFunctionDef - Check that we don't fall off the end of a |
| 277 | /// function that should return a value. Check that we don't fall off the end |
| 278 | /// of a noreturn function. We assume that functions and blocks not marked |
| 279 | /// noreturn will return. |
| 280 | static void CheckFallThroughForBody(Sema &S, const Decl *D, const Stmt *Body, |
| 281 | QualType BlockTy, |
| 282 | const CheckFallThroughDiagnostics& CD, |
| 283 | AnalysisContext &AC) { |
| 284 | |
| 285 | bool ReturnsVoid = false; |
| 286 | bool HasNoReturn = false; |
| 287 | |
| 288 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 289 | ReturnsVoid = FD->getResultType()->isVoidType(); |
| 290 | HasNoReturn = FD->hasAttr<NoReturnAttr>() || |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 291 | FD->getType()->getAs<FunctionType>()->getNoReturnAttr(); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 292 | } |
| 293 | else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 294 | ReturnsVoid = MD->getResultType()->isVoidType(); |
| 295 | HasNoReturn = MD->hasAttr<NoReturnAttr>(); |
| 296 | } |
| 297 | else if (isa<BlockDecl>(D)) { |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 298 | if (const FunctionType *FT = |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 299 | BlockTy->getPointeeType()->getAs<FunctionType>()) { |
| 300 | if (FT->getResultType()->isVoidType()) |
| 301 | ReturnsVoid = true; |
| 302 | if (FT->getNoReturnAttr()) |
| 303 | HasNoReturn = true; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | Diagnostic &Diags = S.getDiagnostics(); |
| 308 | |
| 309 | // Short circuit for compilation speed. |
| 310 | if (CD.checkDiagnostics(Diags, ReturnsVoid, HasNoReturn)) |
| 311 | return; |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 312 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 313 | // FIXME: Function try block |
| 314 | if (const CompoundStmt *Compound = dyn_cast<CompoundStmt>(Body)) { |
| 315 | switch (CheckFallThrough(AC)) { |
John McCall | 16565aa | 2010-05-16 09:34:11 +0000 | [diff] [blame] | 316 | case UnknownFallThrough: |
| 317 | break; |
| 318 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 319 | case MaybeFallThrough: |
| 320 | if (HasNoReturn) |
| 321 | S.Diag(Compound->getRBracLoc(), |
| 322 | CD.diag_MaybeFallThrough_HasNoReturn); |
| 323 | else if (!ReturnsVoid) |
| 324 | S.Diag(Compound->getRBracLoc(), |
| 325 | CD.diag_MaybeFallThrough_ReturnsNonVoid); |
| 326 | break; |
| 327 | case AlwaysFallThrough: |
| 328 | if (HasNoReturn) |
| 329 | S.Diag(Compound->getRBracLoc(), |
| 330 | CD.diag_AlwaysFallThrough_HasNoReturn); |
| 331 | else if (!ReturnsVoid) |
| 332 | S.Diag(Compound->getRBracLoc(), |
| 333 | CD.diag_AlwaysFallThrough_ReturnsNonVoid); |
| 334 | break; |
| 335 | case NeverFallThroughOrReturn: |
Douglas Gregor | ca7eaee | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 336 | if (ReturnsVoid && !HasNoReturn && CD.diag_NeverFallThroughOrReturn) |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 337 | S.Diag(Compound->getLBracLoc(), |
| 338 | CD.diag_NeverFallThroughOrReturn); |
| 339 | break; |
| 340 | case NeverFallThrough: |
| 341 | break; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | //===----------------------------------------------------------------------===// |
| 347 | // AnalysisBasedWarnings - Worker object used by Sema to execute analysis-based |
| 348 | // warnings on a function, method, or block. |
| 349 | //===----------------------------------------------------------------------===// |
| 350 | |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 351 | clang::sema::AnalysisBasedWarnings::Policy::Policy() { |
| 352 | enableCheckFallThrough = 1; |
| 353 | enableCheckUnreachable = 0; |
| 354 | } |
| 355 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 356 | clang::sema::AnalysisBasedWarnings::AnalysisBasedWarnings(Sema &s) : S(s) { |
| 357 | Diagnostic &D = S.getDiagnostics(); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 358 | DefaultPolicy.enableCheckUnreachable = (unsigned) |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 359 | (D.getDiagnosticLevel(diag::warn_unreachable, SourceLocation()) != |
| 360 | Diagnostic::Ignored); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 363 | void clang::sema:: |
| 364 | AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P, |
Ted Kremenek | b7e5f14 | 2010-04-08 18:51:44 +0000 | [diff] [blame] | 365 | const Decl *D, QualType BlockTy) { |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 366 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 367 | assert(BlockTy.isNull() || isa<BlockDecl>(D)); |
Ted Kremenek | d068aab | 2010-03-20 21:11:09 +0000 | [diff] [blame] | 368 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 369 | // We avoid doing analysis-based warnings when there are errors for |
| 370 | // two reasons: |
| 371 | // (1) The CFGs often can't be constructed (if the body is invalid), so |
| 372 | // don't bother trying. |
| 373 | // (2) The code already has problems; running the analysis just takes more |
| 374 | // time. |
Ted Kremenek | 99e8192 | 2010-04-30 21:49:25 +0000 | [diff] [blame] | 375 | Diagnostic &Diags = S.getDiagnostics(); |
| 376 | |
| 377 | if (Diags.hasErrorOccurred() || Diags.hasFatalErrorOccurred()) |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 378 | return; |
| 379 | |
| 380 | // Do not do any analysis for declarations in system headers if we are |
| 381 | // going to just ignore them. |
Ted Kremenek | 99e8192 | 2010-04-30 21:49:25 +0000 | [diff] [blame] | 382 | if (Diags.getSuppressSystemWarnings() && |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 383 | S.SourceMgr.isInSystemHeader(D->getLocation())) |
| 384 | return; |
| 385 | |
John McCall | e0054f6 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 386 | // For code in dependent contexts, we'll do this at instantiation time. |
| 387 | if (cast<DeclContext>(D)->isDependentContext()) |
| 388 | return; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 389 | |
| 390 | const Stmt *Body = D->getBody(); |
| 391 | assert(Body); |
| 392 | |
| 393 | // Don't generate EH edges for CallExprs as we'd like to avoid the n^2 |
| 394 | // explosion for destrutors that can result and the compile time hit. |
Chandler Carruth | eeef924 | 2011-01-08 06:54:40 +0000 | [diff] [blame] | 395 | AnalysisContext AC(D, 0, /*useUnoptimizedCFG=*/false, /*addehedges=*/false, |
| 396 | /*addImplicitDtors=*/true, /*addInitializers=*/true); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 397 | |
| 398 | // Warning: check missing 'return' |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 399 | if (P.enableCheckFallThrough) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 400 | const CheckFallThroughDiagnostics &CD = |
| 401 | (isa<BlockDecl>(D) ? CheckFallThroughDiagnostics::MakeForBlock() |
Douglas Gregor | ca7eaee | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 402 | : CheckFallThroughDiagnostics::MakeForFunction(D)); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 403 | CheckFallThroughForBody(S, D, Body, BlockTy, CD, AC); |
| 404 | } |
| 405 | |
| 406 | // Warning: check for unreachable code |
Ted Kremenek | b7e5f14 | 2010-04-08 18:51:44 +0000 | [diff] [blame] | 407 | if (P.enableCheckUnreachable) |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 408 | CheckUnreachable(S, AC); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 409 | } |
John McCall | e0054f6 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 410 | |
| 411 | void clang::sema:: |
| 412 | AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P, |
| 413 | const BlockExpr *E) { |
| 414 | return IssueWarnings(P, E->getBlockDecl(), E->getType()); |
| 415 | } |
| 416 | |
| 417 | void clang::sema:: |
| 418 | AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P, |
| 419 | const ObjCMethodDecl *D) { |
| 420 | return IssueWarnings(P, D, QualType()); |
| 421 | } |
| 422 | |
| 423 | void clang::sema:: |
| 424 | AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P, |
| 425 | const FunctionDecl *D) { |
| 426 | return IssueWarnings(P, D, QualType()); |
| 427 | } |