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; |
| 124 | if (B.size() == 0) { |
| 125 | if (B.getTerminator() && isa<CXXTryStmt>(B.getTerminator())) { |
| 126 | HasAbnormalEdge = true; |
| 127 | continue; |
| 128 | } |
| 129 | |
| 130 | // A labeled empty statement, or the entry block... |
| 131 | HasPlainEdge = true; |
| 132 | continue; |
| 133 | } |
Zhongxing Xu | b36cd3e | 2010-09-16 01:25:47 +0000 | [diff] [blame] | 134 | CFGElement CE = B[B.size()-1]; |
Ted Kremenek | f39e6a3 | 2011-01-25 22:50:47 +0000 | [diff] [blame] | 135 | |
| 136 | if (!isa<CFGStmt>(CE)) { |
Anders Carlsson | 0dc5f9a | 2011-01-16 22:12:43 +0000 | [diff] [blame] | 137 | HasPlainEdge = true; |
| 138 | continue; |
| 139 | } |
Ted Kremenek | f39e6a3 | 2011-01-25 22:50:47 +0000 | [diff] [blame] | 140 | |
Zhongxing Xu | b36cd3e | 2010-09-16 01:25:47 +0000 | [diff] [blame] | 141 | CFGStmt CS = CE.getAs<CFGStmt>(); |
| 142 | if (!CS.isValid()) |
| 143 | continue; |
| 144 | Stmt *S = CS.getStmt(); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 145 | if (isa<ReturnStmt>(S)) { |
| 146 | HasLiveReturn = true; |
| 147 | continue; |
| 148 | } |
| 149 | if (isa<ObjCAtThrowStmt>(S)) { |
| 150 | HasFakeEdge = true; |
| 151 | continue; |
| 152 | } |
| 153 | if (isa<CXXThrowExpr>(S)) { |
| 154 | HasFakeEdge = true; |
| 155 | continue; |
| 156 | } |
| 157 | if (const AsmStmt *AS = dyn_cast<AsmStmt>(S)) { |
| 158 | if (AS->isMSAsm()) { |
| 159 | HasFakeEdge = true; |
| 160 | HasLiveReturn = true; |
| 161 | continue; |
| 162 | } |
| 163 | } |
| 164 | if (isa<CXXTryStmt>(S)) { |
| 165 | HasAbnormalEdge = true; |
| 166 | continue; |
| 167 | } |
| 168 | |
| 169 | bool NoReturnEdge = false; |
| 170 | if (CallExpr *C = dyn_cast<CallExpr>(S)) { |
John McCall | 259d48e | 2010-04-30 07:10:06 +0000 | [diff] [blame] | 171 | if (std::find(B.succ_begin(), B.succ_end(), &cfg->getExit()) |
| 172 | == B.succ_end()) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 173 | HasAbnormalEdge = true; |
| 174 | continue; |
| 175 | } |
| 176 | Expr *CEE = C->getCallee()->IgnoreParenCasts(); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 177 | if (getFunctionExtInfo(CEE->getType()).getNoReturn()) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 178 | NoReturnEdge = true; |
| 179 | HasFakeEdge = true; |
| 180 | } else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE)) { |
| 181 | ValueDecl *VD = DRE->getDecl(); |
| 182 | if (VD->hasAttr<NoReturnAttr>()) { |
| 183 | NoReturnEdge = true; |
| 184 | HasFakeEdge = true; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | // FIXME: Add noreturn message sends. |
| 189 | if (NoReturnEdge == false) |
| 190 | HasPlainEdge = true; |
| 191 | } |
| 192 | if (!HasPlainEdge) { |
| 193 | if (HasLiveReturn) |
| 194 | return NeverFallThrough; |
| 195 | return NeverFallThroughOrReturn; |
| 196 | } |
| 197 | if (HasAbnormalEdge || HasFakeEdge || HasLiveReturn) |
| 198 | return MaybeFallThrough; |
| 199 | // This says AlwaysFallThrough for calls to functions that are not marked |
| 200 | // noreturn, that don't return. If people would like this warning to be more |
| 201 | // accurate, such functions should be marked as noreturn. |
| 202 | return AlwaysFallThrough; |
| 203 | } |
| 204 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 205 | namespace { |
| 206 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 207 | struct CheckFallThroughDiagnostics { |
| 208 | unsigned diag_MaybeFallThrough_HasNoReturn; |
| 209 | unsigned diag_MaybeFallThrough_ReturnsNonVoid; |
| 210 | unsigned diag_AlwaysFallThrough_HasNoReturn; |
| 211 | unsigned diag_AlwaysFallThrough_ReturnsNonVoid; |
| 212 | unsigned diag_NeverFallThroughOrReturn; |
| 213 | bool funMode; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 214 | SourceLocation FuncLoc; |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 215 | |
Douglas Gregor | ca7eaee | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 216 | static CheckFallThroughDiagnostics MakeForFunction(const Decl *Func) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 217 | CheckFallThroughDiagnostics D; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 218 | D.FuncLoc = Func->getLocation(); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 219 | D.diag_MaybeFallThrough_HasNoReturn = |
| 220 | diag::warn_falloff_noreturn_function; |
| 221 | D.diag_MaybeFallThrough_ReturnsNonVoid = |
| 222 | diag::warn_maybe_falloff_nonvoid_function; |
| 223 | D.diag_AlwaysFallThrough_HasNoReturn = |
| 224 | diag::warn_falloff_noreturn_function; |
| 225 | D.diag_AlwaysFallThrough_ReturnsNonVoid = |
| 226 | diag::warn_falloff_nonvoid_function; |
Douglas Gregor | ca7eaee | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 227 | |
| 228 | // Don't suggest that virtual functions be marked "noreturn", since they |
| 229 | // might be overridden by non-noreturn functions. |
| 230 | bool isVirtualMethod = false; |
| 231 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Func)) |
| 232 | isVirtualMethod = Method->isVirtual(); |
| 233 | |
| 234 | if (!isVirtualMethod) |
| 235 | D.diag_NeverFallThroughOrReturn = |
| 236 | diag::warn_suggest_noreturn_function; |
| 237 | else |
| 238 | D.diag_NeverFallThroughOrReturn = 0; |
| 239 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 240 | D.funMode = true; |
| 241 | return D; |
| 242 | } |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 243 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 244 | static CheckFallThroughDiagnostics MakeForBlock() { |
| 245 | CheckFallThroughDiagnostics D; |
| 246 | D.diag_MaybeFallThrough_HasNoReturn = |
| 247 | diag::err_noreturn_block_has_return_expr; |
| 248 | D.diag_MaybeFallThrough_ReturnsNonVoid = |
| 249 | diag::err_maybe_falloff_nonvoid_block; |
| 250 | D.diag_AlwaysFallThrough_HasNoReturn = |
| 251 | diag::err_noreturn_block_has_return_expr; |
| 252 | D.diag_AlwaysFallThrough_ReturnsNonVoid = |
| 253 | diag::err_falloff_nonvoid_block; |
| 254 | D.diag_NeverFallThroughOrReturn = |
| 255 | diag::warn_suggest_noreturn_block; |
| 256 | D.funMode = false; |
| 257 | return D; |
| 258 | } |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 259 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 260 | bool checkDiagnostics(Diagnostic &D, bool ReturnsVoid, |
| 261 | bool HasNoReturn) const { |
| 262 | if (funMode) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 263 | return (ReturnsVoid || |
| 264 | D.getDiagnosticLevel(diag::warn_maybe_falloff_nonvoid_function, |
| 265 | FuncLoc) == Diagnostic::Ignored) |
| 266 | && (!HasNoReturn || |
| 267 | D.getDiagnosticLevel(diag::warn_noreturn_function_has_return_expr, |
| 268 | FuncLoc) == Diagnostic::Ignored) |
| 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 | } |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 273 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 274 | // For blocks. |
| 275 | return ReturnsVoid && !HasNoReturn |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 276 | && (!ReturnsVoid || |
| 277 | D.getDiagnosticLevel(diag::warn_suggest_noreturn_block, FuncLoc) |
| 278 | == Diagnostic::Ignored); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 279 | } |
| 280 | }; |
| 281 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 284 | /// CheckFallThroughForFunctionDef - Check that we don't fall off the end of a |
| 285 | /// function that should return a value. Check that we don't fall off the end |
| 286 | /// of a noreturn function. We assume that functions and blocks not marked |
| 287 | /// noreturn will return. |
| 288 | static void CheckFallThroughForBody(Sema &S, const Decl *D, const Stmt *Body, |
| 289 | QualType BlockTy, |
| 290 | const CheckFallThroughDiagnostics& CD, |
| 291 | AnalysisContext &AC) { |
| 292 | |
| 293 | bool ReturnsVoid = false; |
| 294 | bool HasNoReturn = false; |
| 295 | |
| 296 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 297 | ReturnsVoid = FD->getResultType()->isVoidType(); |
| 298 | HasNoReturn = FD->hasAttr<NoReturnAttr>() || |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 299 | FD->getType()->getAs<FunctionType>()->getNoReturnAttr(); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 300 | } |
| 301 | else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 302 | ReturnsVoid = MD->getResultType()->isVoidType(); |
| 303 | HasNoReturn = MD->hasAttr<NoReturnAttr>(); |
| 304 | } |
| 305 | else if (isa<BlockDecl>(D)) { |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 306 | if (const FunctionType *FT = |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 307 | BlockTy->getPointeeType()->getAs<FunctionType>()) { |
| 308 | if (FT->getResultType()->isVoidType()) |
| 309 | ReturnsVoid = true; |
| 310 | if (FT->getNoReturnAttr()) |
| 311 | HasNoReturn = true; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | Diagnostic &Diags = S.getDiagnostics(); |
| 316 | |
| 317 | // Short circuit for compilation speed. |
| 318 | if (CD.checkDiagnostics(Diags, ReturnsVoid, HasNoReturn)) |
| 319 | return; |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 320 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 321 | // FIXME: Function try block |
| 322 | if (const CompoundStmt *Compound = dyn_cast<CompoundStmt>(Body)) { |
| 323 | switch (CheckFallThrough(AC)) { |
John McCall | 16565aa | 2010-05-16 09:34:11 +0000 | [diff] [blame] | 324 | case UnknownFallThrough: |
| 325 | break; |
| 326 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 327 | case MaybeFallThrough: |
| 328 | if (HasNoReturn) |
| 329 | S.Diag(Compound->getRBracLoc(), |
| 330 | CD.diag_MaybeFallThrough_HasNoReturn); |
| 331 | else if (!ReturnsVoid) |
| 332 | S.Diag(Compound->getRBracLoc(), |
| 333 | CD.diag_MaybeFallThrough_ReturnsNonVoid); |
| 334 | break; |
| 335 | case AlwaysFallThrough: |
| 336 | if (HasNoReturn) |
| 337 | S.Diag(Compound->getRBracLoc(), |
| 338 | CD.diag_AlwaysFallThrough_HasNoReturn); |
| 339 | else if (!ReturnsVoid) |
| 340 | S.Diag(Compound->getRBracLoc(), |
| 341 | CD.diag_AlwaysFallThrough_ReturnsNonVoid); |
| 342 | break; |
| 343 | case NeverFallThroughOrReturn: |
Douglas Gregor | ca7eaee | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 344 | if (ReturnsVoid && !HasNoReturn && CD.diag_NeverFallThroughOrReturn) |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 345 | S.Diag(Compound->getLBracLoc(), |
| 346 | CD.diag_NeverFallThroughOrReturn); |
| 347 | break; |
| 348 | case NeverFallThrough: |
| 349 | break; |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 355 | // -Wuninitialized |
| 356 | //===----------------------------------------------------------------------===// |
| 357 | |
| 358 | namespace { |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 359 | struct SLocSort { |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 360 | bool operator()(const Expr *a, const Expr *b) { |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 361 | SourceLocation aLoc = a->getLocStart(); |
| 362 | SourceLocation bLoc = b->getLocStart(); |
| 363 | return aLoc.getRawEncoding() < bLoc.getRawEncoding(); |
| 364 | } |
| 365 | }; |
| 366 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 367 | class UninitValsDiagReporter : public UninitVariablesHandler { |
| 368 | Sema &S; |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 369 | typedef llvm::SmallVector<const Expr *, 2> UsesVec; |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 370 | typedef llvm::DenseMap<const VarDecl *, UsesVec*> UsesMap; |
| 371 | UsesMap *uses; |
| 372 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 373 | public: |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 374 | UninitValsDiagReporter(Sema &S) : S(S), uses(0) {} |
| 375 | ~UninitValsDiagReporter() { |
| 376 | flushDiagnostics(); |
| 377 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 378 | |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 379 | void handleUseOfUninitVariable(const Expr *ex, const VarDecl *vd) { |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 380 | if (!uses) |
| 381 | uses = new UsesMap(); |
| 382 | |
| 383 | UsesVec *&vec = (*uses)[vd]; |
| 384 | if (!vec) |
| 385 | vec = new UsesVec(); |
| 386 | |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 387 | vec->push_back(ex); |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | void flushDiagnostics() { |
| 391 | if (!uses) |
| 392 | return; |
| 393 | |
| 394 | for (UsesMap::iterator i = uses->begin(), e = uses->end(); i != e; ++i) { |
| 395 | const VarDecl *vd = i->first; |
| 396 | UsesVec *vec = i->second; |
| 397 | |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 398 | S.Diag(vd->getLocStart(), diag::warn_uninit_var) |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 399 | << vd->getDeclName() << vd->getSourceRange(); |
Ted Kremenek | fbb178a | 2011-01-21 19:41:46 +0000 | [diff] [blame] | 400 | |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 401 | // Sort the uses by their SourceLocations. While not strictly |
| 402 | // guaranteed to produce them in line/column order, this will provide |
| 403 | // a stable ordering. |
| 404 | std::sort(vec->begin(), vec->end(), SLocSort()); |
| 405 | |
| 406 | for (UsesVec::iterator vi = vec->begin(), ve = vec->end(); vi != ve; ++vi) |
| 407 | { |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 408 | if (const DeclRefExpr *dr = dyn_cast<DeclRefExpr>(*vi)) { |
| 409 | S.Diag(dr->getLocStart(), diag::note_uninit_var) |
| 410 | << vd->getDeclName() << dr->getSourceRange(); |
| 411 | } |
| 412 | else { |
| 413 | const BlockExpr *be = cast<BlockExpr>(*vi); |
| 414 | S.Diag(be->getLocStart(), diag::note_uninit_var_captured_by_block) |
| 415 | << vd->getDeclName(); |
| 416 | } |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 417 | } |
Ted Kremenek | fbb178a | 2011-01-21 19:41:46 +0000 | [diff] [blame] | 418 | |
| 419 | // Suggest possible initialization (if any). |
| 420 | const char *initialization = 0; |
Ted Kremenek | dcfb360 | 2011-01-21 22:49:49 +0000 | [diff] [blame] | 421 | QualType vdTy = vd->getType().getCanonicalType(); |
Ted Kremenek | fbb178a | 2011-01-21 19:41:46 +0000 | [diff] [blame] | 422 | |
| 423 | if (vdTy->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | dcfb360 | 2011-01-21 22:49:49 +0000 | [diff] [blame] | 424 | // Check if 'nil' is defined. |
| 425 | if (S.PP.getMacroInfo(&S.getASTContext().Idents.get("nil"))) |
| 426 | initialization = " = nil"; |
| 427 | else |
| 428 | initialization = " = 0"; |
Ted Kremenek | fbb178a | 2011-01-21 19:41:46 +0000 | [diff] [blame] | 429 | } |
Ted Kremenek | dcfb360 | 2011-01-21 22:49:49 +0000 | [diff] [blame] | 430 | else if (vdTy->isRealFloatingType()) { |
| 431 | initialization = " = 0.0"; |
| 432 | } |
| 433 | else if (vdTy->isScalarType()) { |
Ted Kremenek | fbb178a | 2011-01-21 19:41:46 +0000 | [diff] [blame] | 434 | initialization = " = 0"; |
| 435 | } |
| 436 | |
| 437 | if (initialization) { |
| 438 | SourceLocation loc = S.PP.getLocForEndOfToken(vd->getLocEnd()); |
| 439 | S.Diag(loc, diag::note_var_fixit_add_initialization) |
| 440 | << FixItHint::CreateInsertion(loc, initialization); |
| 441 | } |
| 442 | |
Ted Kremenek | 94b1b4d | 2011-01-21 19:41:41 +0000 | [diff] [blame] | 443 | delete vec; |
| 444 | } |
| 445 | delete uses; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 446 | } |
| 447 | }; |
| 448 | } |
| 449 | |
| 450 | //===----------------------------------------------------------------------===// |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 451 | // AnalysisBasedWarnings - Worker object used by Sema to execute analysis-based |
| 452 | // warnings on a function, method, or block. |
| 453 | //===----------------------------------------------------------------------===// |
| 454 | |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 455 | clang::sema::AnalysisBasedWarnings::Policy::Policy() { |
| 456 | enableCheckFallThrough = 1; |
| 457 | enableCheckUnreachable = 0; |
| 458 | } |
| 459 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 460 | clang::sema::AnalysisBasedWarnings::AnalysisBasedWarnings(Sema &s) : S(s) { |
| 461 | Diagnostic &D = S.getDiagnostics(); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 462 | DefaultPolicy.enableCheckUnreachable = (unsigned) |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 463 | (D.getDiagnosticLevel(diag::warn_unreachable, SourceLocation()) != |
| 464 | Diagnostic::Ignored); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 467 | void clang::sema:: |
| 468 | AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P, |
Ted Kremenek | b7e5f14 | 2010-04-08 18:51:44 +0000 | [diff] [blame] | 469 | const Decl *D, QualType BlockTy) { |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 470 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 471 | assert(BlockTy.isNull() || isa<BlockDecl>(D)); |
Ted Kremenek | d068aab | 2010-03-20 21:11:09 +0000 | [diff] [blame] | 472 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 473 | // We avoid doing analysis-based warnings when there are errors for |
| 474 | // two reasons: |
| 475 | // (1) The CFGs often can't be constructed (if the body is invalid), so |
| 476 | // don't bother trying. |
| 477 | // (2) The code already has problems; running the analysis just takes more |
| 478 | // time. |
Ted Kremenek | 99e8192 | 2010-04-30 21:49:25 +0000 | [diff] [blame] | 479 | Diagnostic &Diags = S.getDiagnostics(); |
| 480 | |
| 481 | if (Diags.hasErrorOccurred() || Diags.hasFatalErrorOccurred()) |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 482 | return; |
| 483 | |
| 484 | // Do not do any analysis for declarations in system headers if we are |
| 485 | // going to just ignore them. |
Ted Kremenek | 99e8192 | 2010-04-30 21:49:25 +0000 | [diff] [blame] | 486 | if (Diags.getSuppressSystemWarnings() && |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 487 | S.SourceMgr.isInSystemHeader(D->getLocation())) |
| 488 | return; |
| 489 | |
John McCall | e0054f6 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 490 | // For code in dependent contexts, we'll do this at instantiation time. |
| 491 | if (cast<DeclContext>(D)->isDependentContext()) |
| 492 | return; |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 493 | |
| 494 | const Stmt *Body = D->getBody(); |
| 495 | assert(Body); |
| 496 | |
| 497 | // Don't generate EH edges for CallExprs as we'd like to avoid the n^2 |
| 498 | // explosion for destrutors that can result and the compile time hit. |
Chandler Carruth | eeef924 | 2011-01-08 06:54:40 +0000 | [diff] [blame] | 499 | AnalysisContext AC(D, 0, /*useUnoptimizedCFG=*/false, /*addehedges=*/false, |
| 500 | /*addImplicitDtors=*/true, /*addInitializers=*/true); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 501 | |
| 502 | // Warning: check missing 'return' |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 503 | if (P.enableCheckFallThrough) { |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 504 | const CheckFallThroughDiagnostics &CD = |
| 505 | (isa<BlockDecl>(D) ? CheckFallThroughDiagnostics::MakeForBlock() |
Douglas Gregor | ca7eaee | 2010-04-16 23:28:44 +0000 | [diff] [blame] | 506 | : CheckFallThroughDiagnostics::MakeForFunction(D)); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 507 | CheckFallThroughForBody(S, D, Body, BlockTy, CD, AC); |
| 508 | } |
| 509 | |
| 510 | // Warning: check for unreachable code |
Ted Kremenek | b7e5f14 | 2010-04-08 18:51:44 +0000 | [diff] [blame] | 511 | if (P.enableCheckUnreachable) |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 512 | CheckUnreachable(S, AC); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 513 | |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 514 | if (Diags.getDiagnosticLevel(diag::warn_uninit_var, D->getLocStart()) |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 515 | != Diagnostic::Ignored) { |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 516 | if (CFG *cfg = AC.getCFG()) { |
| 517 | UninitValsDiagReporter reporter(S); |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 518 | runUninitializedVariablesAnalysis(*cast<DeclContext>(D), *cfg, AC, |
| 519 | reporter); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 522 | } |
John McCall | e0054f6 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 523 | |
| 524 | void clang::sema:: |
| 525 | AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P, |
| 526 | const BlockExpr *E) { |
| 527 | return IssueWarnings(P, E->getBlockDecl(), E->getType()); |
| 528 | } |
| 529 | |
| 530 | void clang::sema:: |
| 531 | AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P, |
| 532 | const ObjCMethodDecl *D) { |
| 533 | return IssueWarnings(P, D, QualType()); |
| 534 | } |
| 535 | |
| 536 | void clang::sema:: |
| 537 | AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P, |
| 538 | const FunctionDecl *D) { |
| 539 | return IssueWarnings(P, D, QualType()); |
| 540 | } |