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