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