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