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