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" |
Ted Kremenek | fbb178a | 2011-01-21 19:41:46 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclObjC.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclCXX.h" |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 23 | #include "clang/AST/ExprObjC.h" |
| 24 | #include "clang/AST/ExprCXX.h" |
| 25 | #include "clang/AST/StmtObjC.h" |
| 26 | #include "clang/AST/StmtCXX.h" |
| 27 | #include "clang/Analysis/AnalysisContext.h" |
| 28 | #include "clang/Analysis/CFG.h" |
| 29 | #include "clang/Analysis/Analyses/ReachableCode.h" |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 30 | #include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h" |
| 31 | #include "clang/Analysis/CFGStmtMap.h" |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 32 | #include "clang/Analysis/Analyses/UninitializedValuesV2.h" |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/BitVector.h" |
| 34 | #include "llvm/Support/Casting.h" |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 35 | |
| 36 | using namespace clang; |
| 37 | |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | // Unreachable code analysis. |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | |
| 42 | namespace { |
| 43 | class UnreachableCodeHandler : public reachable_code::Callback { |
| 44 | Sema &S; |
| 45 | public: |
| 46 | UnreachableCodeHandler(Sema &s) : S(s) {} |
| 47 | |
| 48 | void HandleUnreachable(SourceLocation L, SourceRange R1, SourceRange R2) { |
| 49 | S.Diag(L, diag::warn_unreachable) << R1 << R2; |
| 50 | } |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | /// CheckUnreachable - Check for unreachable code. |
| 55 | static void CheckUnreachable(Sema &S, AnalysisContext &AC) { |
| 56 | UnreachableCodeHandler UC(S); |
| 57 | reachable_code::FindUnreachableCode(AC, UC); |
| 58 | } |
| 59 | |
| 60 | //===----------------------------------------------------------------------===// |
| 61 | // Check for missing return value. |
| 62 | //===----------------------------------------------------------------------===// |
| 63 | |
John McCall | 16565aa | 2010-05-16 09:34:11 +0000 | [diff] [blame] | 64 | enum ControlFlowKind { |
| 65 | UnknownFallThrough, |
| 66 | NeverFallThrough, |
| 67 | MaybeFallThrough, |
| 68 | AlwaysFallThrough, |
| 69 | NeverFallThroughOrReturn |
| 70 | }; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 71 | |
| 72 | /// CheckFallThrough - Check that we don't fall off the end of a |
| 73 | /// Statement that should return a value. |
| 74 | /// |
| 75 | /// \returns AlwaysFallThrough iff we always fall off the end of the statement, |
| 76 | /// MaybeFallThrough iff we might or might not fall off the end, |
| 77 | /// NeverFallThroughOrReturn iff we never fall off the end of the statement or |
| 78 | /// return. We assume NeverFallThrough iff we never fall off the end of the |
| 79 | /// statement but we may return. We assume that functions not marked noreturn |
| 80 | /// will return. |
| 81 | static ControlFlowKind CheckFallThrough(AnalysisContext &AC) { |
| 82 | CFG *cfg = AC.getCFG(); |
John McCall | 16565aa | 2010-05-16 09:34:11 +0000 | [diff] [blame] | 83 | if (cfg == 0) return UnknownFallThrough; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 84 | |
| 85 | // The CFG leaves in dead things, and we don't want the dead code paths to |
| 86 | // confuse us, so we mark all live things first. |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 87 | llvm::BitVector live(cfg->getNumBlockIDs()); |
| 88 | unsigned count = reachable_code::ScanReachableFromBlock(cfg->getEntry(), |
| 89 | live); |
| 90 | |
| 91 | bool AddEHEdges = AC.getAddEHEdges(); |
| 92 | if (!AddEHEdges && count != cfg->getNumBlockIDs()) |
| 93 | // When there are things remaining dead, and we didn't add EH edges |
| 94 | // from CallExprs to the catch clauses, we have to go back and |
| 95 | // mark them as live. |
| 96 | for (CFG::iterator I = cfg->begin(), E = cfg->end(); I != E; ++I) { |
| 97 | CFGBlock &b = **I; |
| 98 | if (!live[b.getBlockID()]) { |
| 99 | if (b.pred_begin() == b.pred_end()) { |
| 100 | if (b.getTerminator() && isa<CXXTryStmt>(b.getTerminator())) |
| 101 | // When not adding EH edges from calls, catch clauses |
| 102 | // can otherwise seem dead. Avoid noting them as dead. |
| 103 | count += reachable_code::ScanReachableFromBlock(b, live); |
| 104 | continue; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Now we know what is live, we check the live precessors of the exit block |
| 110 | // and look for fall through paths, being careful to ignore normal returns, |
| 111 | // and exceptional paths. |
| 112 | bool HasLiveReturn = false; |
| 113 | bool HasFakeEdge = false; |
| 114 | bool HasPlainEdge = false; |
| 115 | bool HasAbnormalEdge = false; |
Ted Kremenek | 90b828a | 2010-09-09 00:06:07 +0000 | [diff] [blame] | 116 | |
| 117 | // Ignore default cases that aren't likely to be reachable because all |
| 118 | // enums in a switch(X) have explicit case statements. |
| 119 | CFGBlock::FilterOptions FO; |
| 120 | FO.IgnoreDefaultsWithCoveredEnums = 1; |
| 121 | |
| 122 | for (CFGBlock::filtered_pred_iterator |
| 123 | I = cfg->getExit().filtered_pred_start_end(FO); I.hasMore(); ++I) { |
| 124 | const CFGBlock& B = **I; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 125 | if (!live[B.getBlockID()]) |
| 126 | continue; |
Ted Kremenek | 5811f59 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 127 | |
| 128 | // Destructors can appear after the 'return' in the CFG. This is |
| 129 | // normal. We need to look pass the destructors for the return |
| 130 | // statement (if it exists). |
| 131 | CFGBlock::const_reverse_iterator ri = B.rbegin(), re = B.rend(); |
Ted Kremenek | c9f8f5a | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 132 | bool hasNoReturnDtor = false; |
| 133 | |
Ted Kremenek | 5811f59 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 134 | for ( ; ri != re ; ++ri) { |
| 135 | CFGElement CE = *ri; |
Ted Kremenek | c9f8f5a | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 136 | |
| 137 | // FIXME: The right solution is to just sever the edges in the |
| 138 | // CFG itself. |
| 139 | if (const CFGImplicitDtor *iDtor = ri->getAs<CFGImplicitDtor>()) |
Ted Kremenek | c5aff44 | 2011-03-03 01:21:32 +0000 | [diff] [blame^] | 140 | if (iDtor->isNoReturn(AC.getASTContext())) { |
Ted Kremenek | c9f8f5a | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 141 | hasNoReturnDtor = true; |
| 142 | HasFakeEdge = true; |
| 143 | break; |
| 144 | } |
| 145 | |
Ted Kremenek | 5811f59 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 146 | if (isa<CFGStmt>(CE)) |
| 147 | break; |
| 148 | } |
| 149 | |
Ted Kremenek | c9f8f5a | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 150 | if (hasNoReturnDtor) |
| 151 | continue; |
| 152 | |
Ted Kremenek | 5811f59 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 153 | // No more CFGElements in the block? |
| 154 | if (ri == re) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 155 | if (B.getTerminator() && isa<CXXTryStmt>(B.getTerminator())) { |
| 156 | HasAbnormalEdge = true; |
| 157 | continue; |
| 158 | } |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 159 | // A labeled empty statement, or the entry block... |
| 160 | HasPlainEdge = true; |
| 161 | continue; |
| 162 | } |
Ted Kremenek | f39e6a3 | 2011-01-25 22:50:47 +0000 | [diff] [blame] | 163 | |
Ted Kremenek | 5811f59 | 2011-01-26 04:49:52 +0000 | [diff] [blame] | 164 | CFGStmt CS = cast<CFGStmt>(*ri); |
Zhongxing Xu | b36cd3e | 2010-09-16 01:25:47 +0000 | [diff] [blame] | 165 | Stmt *S = CS.getStmt(); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 166 | if (isa<ReturnStmt>(S)) { |
| 167 | HasLiveReturn = true; |
| 168 | continue; |
| 169 | } |
| 170 | if (isa<ObjCAtThrowStmt>(S)) { |
| 171 | HasFakeEdge = true; |
| 172 | continue; |
| 173 | } |
| 174 | if (isa<CXXThrowExpr>(S)) { |
| 175 | HasFakeEdge = true; |
| 176 | continue; |
| 177 | } |
| 178 | if (const AsmStmt *AS = dyn_cast<AsmStmt>(S)) { |
| 179 | if (AS->isMSAsm()) { |
| 180 | HasFakeEdge = true; |
| 181 | HasLiveReturn = true; |
| 182 | continue; |
| 183 | } |
| 184 | } |
| 185 | if (isa<CXXTryStmt>(S)) { |
| 186 | HasAbnormalEdge = true; |
| 187 | continue; |
| 188 | } |
| 189 | |
| 190 | bool NoReturnEdge = false; |
| 191 | if (CallExpr *C = dyn_cast<CallExpr>(S)) { |
John McCall | 259d48e | 2010-04-30 07:10:06 +0000 | [diff] [blame] | 192 | if (std::find(B.succ_begin(), B.succ_end(), &cfg->getExit()) |
| 193 | == B.succ_end()) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 194 | HasAbnormalEdge = true; |
| 195 | continue; |
| 196 | } |
| 197 | Expr *CEE = C->getCallee()->IgnoreParenCasts(); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 198 | if (getFunctionExtInfo(CEE->getType()).getNoReturn()) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 199 | NoReturnEdge = true; |
| 200 | HasFakeEdge = true; |
| 201 | } else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE)) { |
| 202 | ValueDecl *VD = DRE->getDecl(); |
| 203 | if (VD->hasAttr<NoReturnAttr>()) { |
| 204 | NoReturnEdge = true; |
| 205 | HasFakeEdge = true; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | // FIXME: Add noreturn message sends. |
| 210 | if (NoReturnEdge == false) |
| 211 | HasPlainEdge = true; |
| 212 | } |
| 213 | if (!HasPlainEdge) { |
| 214 | if (HasLiveReturn) |
| 215 | return NeverFallThrough; |
| 216 | return NeverFallThroughOrReturn; |
| 217 | } |
| 218 | if (HasAbnormalEdge || HasFakeEdge || HasLiveReturn) |
| 219 | return MaybeFallThrough; |
| 220 | // This says AlwaysFallThrough for calls to functions that are not marked |
| 221 | // noreturn, that don't return. If people would like this warning to be more |
| 222 | // accurate, such functions should be marked as noreturn. |
| 223 | return AlwaysFallThrough; |
| 224 | } |
| 225 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 226 | namespace { |
| 227 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 228 | struct CheckFallThroughDiagnostics { |
| 229 | unsigned diag_MaybeFallThrough_HasNoReturn; |
| 230 | unsigned diag_MaybeFallThrough_ReturnsNonVoid; |
| 231 | unsigned diag_AlwaysFallThrough_HasNoReturn; |
| 232 | unsigned diag_AlwaysFallThrough_ReturnsNonVoid; |
| 233 | unsigned diag_NeverFallThroughOrReturn; |
| 234 | bool funMode; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 235 | SourceLocation FuncLoc; |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 236 | |
Douglas Gregor | ca7eaee | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 237 | static CheckFallThroughDiagnostics MakeForFunction(const Decl *Func) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 238 | CheckFallThroughDiagnostics D; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 239 | D.FuncLoc = Func->getLocation(); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 240 | D.diag_MaybeFallThrough_HasNoReturn = |
| 241 | diag::warn_falloff_noreturn_function; |
| 242 | D.diag_MaybeFallThrough_ReturnsNonVoid = |
| 243 | diag::warn_maybe_falloff_nonvoid_function; |
| 244 | D.diag_AlwaysFallThrough_HasNoReturn = |
| 245 | diag::warn_falloff_noreturn_function; |
| 246 | D.diag_AlwaysFallThrough_ReturnsNonVoid = |
| 247 | diag::warn_falloff_nonvoid_function; |
Douglas Gregor | ca7eaee | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 248 | |
| 249 | // Don't suggest that virtual functions be marked "noreturn", since they |
| 250 | // might be overridden by non-noreturn functions. |
| 251 | bool isVirtualMethod = false; |
| 252 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Func)) |
| 253 | isVirtualMethod = Method->isVirtual(); |
| 254 | |
| 255 | if (!isVirtualMethod) |
| 256 | D.diag_NeverFallThroughOrReturn = |
| 257 | diag::warn_suggest_noreturn_function; |
| 258 | else |
| 259 | D.diag_NeverFallThroughOrReturn = 0; |
| 260 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 261 | D.funMode = true; |
| 262 | return D; |
| 263 | } |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 264 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 265 | static CheckFallThroughDiagnostics MakeForBlock() { |
| 266 | CheckFallThroughDiagnostics D; |
| 267 | D.diag_MaybeFallThrough_HasNoReturn = |
| 268 | diag::err_noreturn_block_has_return_expr; |
| 269 | D.diag_MaybeFallThrough_ReturnsNonVoid = |
| 270 | diag::err_maybe_falloff_nonvoid_block; |
| 271 | D.diag_AlwaysFallThrough_HasNoReturn = |
| 272 | diag::err_noreturn_block_has_return_expr; |
| 273 | D.diag_AlwaysFallThrough_ReturnsNonVoid = |
| 274 | diag::err_falloff_nonvoid_block; |
| 275 | D.diag_NeverFallThroughOrReturn = |
| 276 | diag::warn_suggest_noreturn_block; |
| 277 | D.funMode = false; |
| 278 | return D; |
| 279 | } |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 280 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 281 | bool checkDiagnostics(Diagnostic &D, bool ReturnsVoid, |
| 282 | bool HasNoReturn) const { |
| 283 | if (funMode) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 284 | return (ReturnsVoid || |
| 285 | D.getDiagnosticLevel(diag::warn_maybe_falloff_nonvoid_function, |
| 286 | FuncLoc) == Diagnostic::Ignored) |
| 287 | && (!HasNoReturn || |
| 288 | D.getDiagnosticLevel(diag::warn_noreturn_function_has_return_expr, |
| 289 | FuncLoc) == Diagnostic::Ignored) |
| 290 | && (!ReturnsVoid || |
| 291 | D.getDiagnosticLevel(diag::warn_suggest_noreturn_block, FuncLoc) |
| 292 | == Diagnostic::Ignored); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 293 | } |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 294 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 295 | // For blocks. |
| 296 | return ReturnsVoid && !HasNoReturn |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 297 | && (!ReturnsVoid || |
| 298 | D.getDiagnosticLevel(diag::warn_suggest_noreturn_block, FuncLoc) |
| 299 | == Diagnostic::Ignored); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 300 | } |
| 301 | }; |
| 302 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 305 | /// CheckFallThroughForFunctionDef - Check that we don't fall off the end of a |
| 306 | /// function that should return a value. Check that we don't fall off the end |
| 307 | /// of a noreturn function. We assume that functions and blocks not marked |
| 308 | /// noreturn will return. |
| 309 | static void CheckFallThroughForBody(Sema &S, const Decl *D, const Stmt *Body, |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 310 | const BlockExpr *blkExpr, |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 311 | const CheckFallThroughDiagnostics& CD, |
| 312 | AnalysisContext &AC) { |
| 313 | |
| 314 | bool ReturnsVoid = false; |
| 315 | bool HasNoReturn = false; |
| 316 | |
| 317 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 318 | ReturnsVoid = FD->getResultType()->isVoidType(); |
| 319 | HasNoReturn = FD->hasAttr<NoReturnAttr>() || |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 320 | FD->getType()->getAs<FunctionType>()->getNoReturnAttr(); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 321 | } |
| 322 | else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 323 | ReturnsVoid = MD->getResultType()->isVoidType(); |
| 324 | HasNoReturn = MD->hasAttr<NoReturnAttr>(); |
| 325 | } |
| 326 | else if (isa<BlockDecl>(D)) { |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 327 | QualType BlockTy = blkExpr->getType(); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 328 | if (const FunctionType *FT = |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 329 | BlockTy->getPointeeType()->getAs<FunctionType>()) { |
| 330 | if (FT->getResultType()->isVoidType()) |
| 331 | ReturnsVoid = true; |
| 332 | if (FT->getNoReturnAttr()) |
| 333 | HasNoReturn = true; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | Diagnostic &Diags = S.getDiagnostics(); |
| 338 | |
| 339 | // Short circuit for compilation speed. |
| 340 | if (CD.checkDiagnostics(Diags, ReturnsVoid, HasNoReturn)) |
| 341 | return; |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 342 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 343 | // FIXME: Function try block |
| 344 | if (const CompoundStmt *Compound = dyn_cast<CompoundStmt>(Body)) { |
| 345 | switch (CheckFallThrough(AC)) { |
John McCall | 16565aa | 2010-05-16 09:34:11 +0000 | [diff] [blame] | 346 | case UnknownFallThrough: |
| 347 | break; |
| 348 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 349 | case MaybeFallThrough: |
| 350 | if (HasNoReturn) |
| 351 | S.Diag(Compound->getRBracLoc(), |
| 352 | CD.diag_MaybeFallThrough_HasNoReturn); |
| 353 | else if (!ReturnsVoid) |
| 354 | S.Diag(Compound->getRBracLoc(), |
| 355 | CD.diag_MaybeFallThrough_ReturnsNonVoid); |
| 356 | break; |
| 357 | case AlwaysFallThrough: |
| 358 | if (HasNoReturn) |
| 359 | S.Diag(Compound->getRBracLoc(), |
| 360 | CD.diag_AlwaysFallThrough_HasNoReturn); |
| 361 | else if (!ReturnsVoid) |
| 362 | S.Diag(Compound->getRBracLoc(), |
| 363 | CD.diag_AlwaysFallThrough_ReturnsNonVoid); |
| 364 | break; |
| 365 | case NeverFallThroughOrReturn: |
Douglas Gregor | ca7eaee | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 366 | if (ReturnsVoid && !HasNoReturn && CD.diag_NeverFallThroughOrReturn) |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 367 | S.Diag(Compound->getLBracLoc(), |
| 368 | CD.diag_NeverFallThroughOrReturn); |
| 369 | break; |
| 370 | case NeverFallThrough: |
| 371 | break; |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 377 | // -Wuninitialized |
| 378 | //===----------------------------------------------------------------------===// |
| 379 | |
| 380 | namespace { |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 381 | struct SLocSort { |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 382 | bool operator()(const Expr *a, const Expr *b) { |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 383 | SourceLocation aLoc = a->getLocStart(); |
| 384 | SourceLocation bLoc = b->getLocStart(); |
| 385 | return aLoc.getRawEncoding() < bLoc.getRawEncoding(); |
| 386 | } |
| 387 | }; |
| 388 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 389 | class UninitValsDiagReporter : public UninitVariablesHandler { |
| 390 | Sema &S; |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 391 | typedef llvm::SmallVector<const Expr *, 2> UsesVec; |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 392 | typedef llvm::DenseMap<const VarDecl *, UsesVec*> UsesMap; |
| 393 | UsesMap *uses; |
| 394 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 395 | public: |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 396 | UninitValsDiagReporter(Sema &S) : S(S), uses(0) {} |
| 397 | ~UninitValsDiagReporter() { |
| 398 | flushDiagnostics(); |
| 399 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 400 | |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 401 | void handleUseOfUninitVariable(const Expr *ex, const VarDecl *vd) { |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 402 | if (!uses) |
| 403 | uses = new UsesMap(); |
| 404 | |
| 405 | UsesVec *&vec = (*uses)[vd]; |
| 406 | if (!vec) |
| 407 | vec = new UsesVec(); |
| 408 | |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 409 | vec->push_back(ex); |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | void flushDiagnostics() { |
| 413 | if (!uses) |
| 414 | return; |
Ted Kremenek | 609e317 | 2011-02-02 23:35:53 +0000 | [diff] [blame] | 415 | |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 416 | for (UsesMap::iterator i = uses->begin(), e = uses->end(); i != e; ++i) { |
| 417 | const VarDecl *vd = i->first; |
| 418 | UsesVec *vec = i->second; |
Ted Kremenek | 609e317 | 2011-02-02 23:35:53 +0000 | [diff] [blame] | 419 | |
| 420 | bool fixitIssued = false; |
| 421 | |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 422 | // Sort the uses by their SourceLocations. While not strictly |
| 423 | // guaranteed to produce them in line/column order, this will provide |
| 424 | // a stable ordering. |
| 425 | std::sort(vec->begin(), vec->end(), SLocSort()); |
| 426 | |
| 427 | for (UsesVec::iterator vi = vec->begin(), ve = vec->end(); vi != ve; ++vi) |
| 428 | { |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 429 | if (const DeclRefExpr *dr = dyn_cast<DeclRefExpr>(*vi)) { |
Ted Kremenek | 609e317 | 2011-02-02 23:35:53 +0000 | [diff] [blame] | 430 | S.Diag(dr->getLocStart(), diag::warn_uninit_var) |
| 431 | << vd->getDeclName() << dr->getSourceRange(); |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 432 | } |
| 433 | else { |
| 434 | const BlockExpr *be = cast<BlockExpr>(*vi); |
Ted Kremenek | 609e317 | 2011-02-02 23:35:53 +0000 | [diff] [blame] | 435 | S.Diag(be->getLocStart(), diag::warn_uninit_var_captured_by_block) |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 436 | << vd->getDeclName(); |
| 437 | } |
Ted Kremenek | 609e317 | 2011-02-02 23:35:53 +0000 | [diff] [blame] | 438 | |
| 439 | // Report where the variable was declared. |
| 440 | S.Diag(vd->getLocStart(), diag::note_uninit_var_def) |
| 441 | << vd->getDeclName(); |
Ted Kremenek | fbb178a | 2011-01-21 19:41:46 +0000 | [diff] [blame] | 442 | |
Ted Kremenek | 609e317 | 2011-02-02 23:35:53 +0000 | [diff] [blame] | 443 | // Only report the fixit once. |
| 444 | if (fixitIssued) |
| 445 | continue; |
Ted Kremenek | fbb178a | 2011-01-21 19:41:46 +0000 | [diff] [blame] | 446 | |
Ted Kremenek | 609e317 | 2011-02-02 23:35:53 +0000 | [diff] [blame] | 447 | fixitIssued = true; |
| 448 | |
| 449 | // Suggest possible initialization (if any). |
| 450 | const char *initialization = 0; |
| 451 | QualType vdTy = vd->getType().getCanonicalType(); |
Ted Kremenek | 09f57b9 | 2011-02-05 01:18:18 +0000 | [diff] [blame] | 452 | |
Ted Kremenek | 609e317 | 2011-02-02 23:35:53 +0000 | [diff] [blame] | 453 | if (vdTy->getAs<ObjCObjectPointerType>()) { |
| 454 | // Check if 'nil' is defined. |
| 455 | if (S.PP.getMacroInfo(&S.getASTContext().Idents.get("nil"))) |
| 456 | initialization = " = nil"; |
| 457 | else |
| 458 | initialization = " = 0"; |
| 459 | } |
| 460 | else if (vdTy->isRealFloatingType()) |
| 461 | initialization = " = 0.0"; |
| 462 | else if (vdTy->isBooleanType() && S.Context.getLangOptions().CPlusPlus) |
| 463 | initialization = " = false"; |
Ted Kremenek | 09f57b9 | 2011-02-05 01:18:18 +0000 | [diff] [blame] | 464 | else if (vdTy->isEnumeralType()) |
| 465 | continue; |
Ted Kremenek | 609e317 | 2011-02-02 23:35:53 +0000 | [diff] [blame] | 466 | else if (vdTy->isScalarType()) |
Ted Kremenek | dcfb360 | 2011-01-21 22:49:49 +0000 | [diff] [blame] | 467 | initialization = " = 0"; |
Ted Kremenek | fbb178a | 2011-01-21 19:41:46 +0000 | [diff] [blame] | 468 | |
Ted Kremenek | 609e317 | 2011-02-02 23:35:53 +0000 | [diff] [blame] | 469 | if (initialization) { |
| 470 | SourceLocation loc = S.PP.getLocForEndOfToken(vd->getLocEnd()); |
| 471 | S.Diag(loc, diag::note_var_fixit_add_initialization) |
| 472 | << FixItHint::CreateInsertion(loc, initialization); |
| 473 | } |
Ted Kremenek | fbb178a | 2011-01-21 19:41:46 +0000 | [diff] [blame] | 474 | } |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 475 | delete vec; |
| 476 | } |
| 477 | delete uses; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 478 | } |
| 479 | }; |
| 480 | } |
| 481 | |
| 482 | //===----------------------------------------------------------------------===// |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 483 | // AnalysisBasedWarnings - Worker object used by Sema to execute analysis-based |
| 484 | // warnings on a function, method, or block. |
| 485 | //===----------------------------------------------------------------------===// |
| 486 | |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 487 | clang::sema::AnalysisBasedWarnings::Policy::Policy() { |
| 488 | enableCheckFallThrough = 1; |
| 489 | enableCheckUnreachable = 0; |
| 490 | } |
| 491 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 492 | clang::sema::AnalysisBasedWarnings::AnalysisBasedWarnings(Sema &s) : S(s) { |
| 493 | Diagnostic &D = S.getDiagnostics(); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 494 | DefaultPolicy.enableCheckUnreachable = (unsigned) |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 495 | (D.getDiagnosticLevel(diag::warn_unreachable, SourceLocation()) != |
| 496 | Diagnostic::Ignored); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 499 | static void flushDiagnostics(Sema &S, sema::FunctionScopeInfo *fscope) { |
| 500 | for (llvm::SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator |
| 501 | i = fscope->PossiblyUnreachableDiags.begin(), |
| 502 | e = fscope->PossiblyUnreachableDiags.end(); |
| 503 | i != e; ++i) { |
| 504 | const sema::PossiblyUnreachableDiag &D = *i; |
| 505 | S.Diag(D.Loc, D.PD); |
| 506 | } |
| 507 | } |
| 508 | |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 509 | void clang::sema:: |
| 510 | AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P, |
Ted Kremenek | 283a358 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 511 | sema::FunctionScopeInfo *fscope, |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 512 | const Decl *D, const BlockExpr *blkExpr) { |
Ted Kremenek | d068aab | 2010-03-20 21:11:09 +0000 | [diff] [blame] | 513 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 514 | // We avoid doing analysis-based warnings when there are errors for |
| 515 | // two reasons: |
| 516 | // (1) The CFGs often can't be constructed (if the body is invalid), so |
| 517 | // don't bother trying. |
| 518 | // (2) The code already has problems; running the analysis just takes more |
| 519 | // time. |
Ted Kremenek | 99e8192 | 2010-04-30 21:49:25 +0000 | [diff] [blame] | 520 | Diagnostic &Diags = S.getDiagnostics(); |
| 521 | |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 522 | // Do not do any analysis for declarations in system headers if we are |
| 523 | // going to just ignore them. |
Ted Kremenek | 99e8192 | 2010-04-30 21:49:25 +0000 | [diff] [blame] | 524 | if (Diags.getSuppressSystemWarnings() && |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 525 | S.SourceMgr.isInSystemHeader(D->getLocation())) |
| 526 | return; |
| 527 | |
John McCall | e0054f6 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 528 | // For code in dependent contexts, we'll do this at instantiation time. |
| 529 | if (cast<DeclContext>(D)->isDependentContext()) |
| 530 | return; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 531 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 532 | if (Diags.hasErrorOccurred() || Diags.hasFatalErrorOccurred()) { |
| 533 | // Flush out any possibly unreachable diagnostics. |
| 534 | flushDiagnostics(S, fscope); |
| 535 | return; |
| 536 | } |
| 537 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 538 | const Stmt *Body = D->getBody(); |
| 539 | assert(Body); |
| 540 | |
| 541 | // Don't generate EH edges for CallExprs as we'd like to avoid the n^2 |
| 542 | // explosion for destrutors that can result and the compile time hit. |
Chandler Carruth | eeef924 | 2011-01-08 06:54:40 +0000 | [diff] [blame] | 543 | AnalysisContext AC(D, 0, /*useUnoptimizedCFG=*/false, /*addehedges=*/false, |
| 544 | /*addImplicitDtors=*/true, /*addInitializers=*/true); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 545 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 546 | // Emit delayed diagnostics. |
| 547 | if (!fscope->PossiblyUnreachableDiags.empty()) { |
| 548 | bool analyzed = false; |
| 549 | if (CFGReachabilityAnalysis *cra = AC.getCFGReachablityAnalysis()) |
| 550 | if (CFGStmtMap *csm = AC.getCFGStmtMap()) { |
| 551 | analyzed = true; |
| 552 | for (llvm::SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator |
| 553 | i = fscope->PossiblyUnreachableDiags.begin(), |
| 554 | e = fscope->PossiblyUnreachableDiags.end(); |
| 555 | i != e; ++i) { |
| 556 | const sema::PossiblyUnreachableDiag &D = *i; |
| 557 | if (const CFGBlock *blk = csm->getBlock(D.stmt)) { |
| 558 | // Can this block be reached from the entrance? |
| 559 | if (cra->isReachable(&AC.getCFG()->getEntry(), blk)) |
| 560 | S.Diag(D.Loc, D.PD); |
| 561 | } |
| 562 | else { |
| 563 | // Emit the warning anyway if we cannot map to a basic block. |
| 564 | S.Diag(D.Loc, D.PD); |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | if (!analyzed) |
| 570 | flushDiagnostics(S, fscope); |
| 571 | } |
| 572 | |
| 573 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 574 | // Warning: check missing 'return' |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 575 | if (P.enableCheckFallThrough) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 576 | const CheckFallThroughDiagnostics &CD = |
| 577 | (isa<BlockDecl>(D) ? CheckFallThroughDiagnostics::MakeForBlock() |
Douglas Gregor | ca7eaee | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 578 | : CheckFallThroughDiagnostics::MakeForFunction(D)); |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 579 | CheckFallThroughForBody(S, D, Body, blkExpr, CD, AC); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | // Warning: check for unreachable code |
Ted Kremenek | b7e5f14 | 2010-04-08 18:51:44 +0000 | [diff] [blame] | 583 | if (P.enableCheckUnreachable) |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 584 | CheckUnreachable(S, AC); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 585 | |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 586 | if (Diags.getDiagnosticLevel(diag::warn_uninit_var, D->getLocStart()) |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 587 | != Diagnostic::Ignored) { |
Ted Kremenek | 63b5410 | 2011-02-01 17:43:21 +0000 | [diff] [blame] | 588 | ASTContext &ctx = D->getASTContext(); |
| 589 | llvm::OwningPtr<CFG> tmpCFG; |
| 590 | bool useAlternateCFG = false; |
| 591 | if (ctx.getLangOptions().CPlusPlus) { |
| 592 | // Temporary workaround: implicit dtors in the CFG can confuse |
| 593 | // the path-sensitivity in the uninitialized values analysis. |
| 594 | // For now create (if necessary) a separate CFG without implicit dtors. |
| 595 | // FIXME: We should not need to do this, as it results in multiple |
| 596 | // CFGs getting constructed. |
| 597 | CFG::BuildOptions B; |
| 598 | B.AddEHEdges = false; |
| 599 | B.AddImplicitDtors = false; |
| 600 | B.AddInitializers = true; |
| 601 | tmpCFG.reset(CFG::buildCFG(D, AC.getBody(), &ctx, B)); |
| 602 | useAlternateCFG = true; |
| 603 | } |
| 604 | CFG *cfg = useAlternateCFG ? tmpCFG.get() : AC.getCFG(); |
| 605 | if (cfg) { |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 606 | UninitValsDiagReporter reporter(S); |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 607 | runUninitializedVariablesAnalysis(*cast<DeclContext>(D), *cfg, AC, |
| 608 | reporter); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 609 | } |
| 610 | } |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 611 | } |