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