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