blob: 2f02e158cbd7c1d2ded50dbc9e46b12d8d7e5bd1 [file] [log] [blame]
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001//=- 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 Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/AnalysisBasedWarnings.h"
John McCall2d887082010-08-25 22:03:47 +000017#include "clang/Sema/SemaInternal.h"
Ted Kremenekd068aab2010-03-20 21:11:09 +000018#include "clang/Basic/SourceManager.h"
Ted Kremenekfbb178a2011-01-21 19:41:46 +000019#include "clang/Lex/Preprocessor.h"
John McCall7cd088e2010-08-24 07:21:54 +000020#include "clang/AST/DeclObjC.h"
John McCall384aff82010-08-25 07:42:41 +000021#include "clang/AST/DeclCXX.h"
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +000022#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 Kremenek610068c2011-01-15 02:58:47 +000029#include "clang/Analysis/Analyses/UninitializedValuesV2.h"
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +000030#include "llvm/ADT/BitVector.h"
31#include "llvm/Support/Casting.h"
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +000032
33using namespace clang;
34
35//===----------------------------------------------------------------------===//
36// Unreachable code analysis.
37//===----------------------------------------------------------------------===//
38
39namespace {
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.
52static 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 McCall16565aa2010-05-16 09:34:11 +000061enum ControlFlowKind {
62 UnknownFallThrough,
63 NeverFallThrough,
64 MaybeFallThrough,
65 AlwaysFallThrough,
66 NeverFallThroughOrReturn
67};
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +000068
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.
78static ControlFlowKind CheckFallThrough(AnalysisContext &AC) {
79 CFG *cfg = AC.getCFG();
John McCall16565aa2010-05-16 09:34:11 +000080 if (cfg == 0) return UnknownFallThrough;
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +000081
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 Kremenekdbdbaaf2010-03-20 21:06:02 +000084 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 Kremenek90b828a2010-09-09 00:06:07 +0000113
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 Kremenekdbdbaaf2010-03-20 21:06:02 +0000122 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 Xub36cd3e2010-09-16 01:25:47 +0000134 CFGElement CE = B[B.size()-1];
Ted Kremenekf39e6a32011-01-25 22:50:47 +0000135
136 if (!isa<CFGStmt>(CE)) {
Anders Carlsson0dc5f9a2011-01-16 22:12:43 +0000137 HasPlainEdge = true;
138 continue;
139 }
Ted Kremenekf39e6a32011-01-25 22:50:47 +0000140
Zhongxing Xub36cd3e2010-09-16 01:25:47 +0000141 CFGStmt CS = CE.getAs<CFGStmt>();
142 if (!CS.isValid())
143 continue;
144 Stmt *S = CS.getStmt();
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000145 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 McCall259d48e2010-04-30 07:10:06 +0000171 if (std::find(B.succ_begin(), B.succ_end(), &cfg->getExit())
172 == B.succ_end()) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000173 HasAbnormalEdge = true;
174 continue;
175 }
176 Expr *CEE = C->getCallee()->IgnoreParenCasts();
Rafael Espindola264ba482010-03-30 20:24:48 +0000177 if (getFunctionExtInfo(CEE->getType()).getNoReturn()) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000178 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 Gohman3c46e8d2010-07-26 21:25:24 +0000205namespace {
206
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000207struct 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 Kyrtzidis08274082010-12-15 18:44:22 +0000214 SourceLocation FuncLoc;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000215
Douglas Gregorca7eaee2010-04-16 23:28:44 +0000216 static CheckFallThroughDiagnostics MakeForFunction(const Decl *Func) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000217 CheckFallThroughDiagnostics D;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000218 D.FuncLoc = Func->getLocation();
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000219 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 Gregorca7eaee2010-04-16 23:28:44 +0000227
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 Kremenekdbdbaaf2010-03-20 21:06:02 +0000240 D.funMode = true;
241 return D;
242 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000243
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000244 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 Kremenekd064fdc2010-03-23 00:13:23 +0000259
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000260 bool checkDiagnostics(Diagnostic &D, bool ReturnsVoid,
261 bool HasNoReturn) const {
262 if (funMode) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000263 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 Kremenekdbdbaaf2010-03-20 21:06:02 +0000272 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000273
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000274 // For blocks.
275 return ReturnsVoid && !HasNoReturn
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000276 && (!ReturnsVoid ||
277 D.getDiagnosticLevel(diag::warn_suggest_noreturn_block, FuncLoc)
278 == Diagnostic::Ignored);
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000279 }
280};
281
Dan Gohman3c46e8d2010-07-26 21:25:24 +0000282}
283
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000284/// 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.
288static 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 Espindola264ba482010-03-30 20:24:48 +0000299 FD->getType()->getAs<FunctionType>()->getNoReturnAttr();
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000300 }
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 Kremenekd064fdc2010-03-23 00:13:23 +0000306 if (const FunctionType *FT =
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000307 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 Kremenekd064fdc2010-03-23 00:13:23 +0000320
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000321 // FIXME: Function try block
322 if (const CompoundStmt *Compound = dyn_cast<CompoundStmt>(Body)) {
323 switch (CheckFallThrough(AC)) {
John McCall16565aa2010-05-16 09:34:11 +0000324 case UnknownFallThrough:
325 break;
326
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000327 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 Gregorca7eaee2010-04-16 23:28:44 +0000344 if (ReturnsVoid && !HasNoReturn && CD.diag_NeverFallThroughOrReturn)
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000345 S.Diag(Compound->getLBracLoc(),
346 CD.diag_NeverFallThroughOrReturn);
347 break;
348 case NeverFallThrough:
349 break;
350 }
351 }
352}
353
354//===----------------------------------------------------------------------===//
Ted Kremenek610068c2011-01-15 02:58:47 +0000355// -Wuninitialized
356//===----------------------------------------------------------------------===//
357
358namespace {
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000359struct SLocSort {
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000360 bool operator()(const Expr *a, const Expr *b) {
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000361 SourceLocation aLoc = a->getLocStart();
362 SourceLocation bLoc = b->getLocStart();
363 return aLoc.getRawEncoding() < bLoc.getRawEncoding();
364 }
365};
366
Ted Kremenek610068c2011-01-15 02:58:47 +0000367class UninitValsDiagReporter : public UninitVariablesHandler {
368 Sema &S;
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000369 typedef llvm::SmallVector<const Expr *, 2> UsesVec;
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000370 typedef llvm::DenseMap<const VarDecl *, UsesVec*> UsesMap;
371 UsesMap *uses;
372
Ted Kremenek610068c2011-01-15 02:58:47 +0000373public:
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000374 UninitValsDiagReporter(Sema &S) : S(S), uses(0) {}
375 ~UninitValsDiagReporter() {
376 flushDiagnostics();
377 }
Ted Kremenek610068c2011-01-15 02:58:47 +0000378
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000379 void handleUseOfUninitVariable(const Expr *ex, const VarDecl *vd) {
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000380 if (!uses)
381 uses = new UsesMap();
382
383 UsesVec *&vec = (*uses)[vd];
384 if (!vec)
385 vec = new UsesVec();
386
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000387 vec->push_back(ex);
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000388 }
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 Kremeneka8c17a52011-01-25 19:13:48 +0000398 S.Diag(vd->getLocStart(), diag::warn_uninit_var)
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000399 << vd->getDeclName() << vd->getSourceRange();
Ted Kremenekfbb178a2011-01-21 19:41:46 +0000400
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000401 // 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 Kremeneka8c17a52011-01-25 19:13:48 +0000408 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 Kremenek94b1b4d2011-01-21 19:41:41 +0000417 }
Ted Kremenekfbb178a2011-01-21 19:41:46 +0000418
419 // Suggest possible initialization (if any).
420 const char *initialization = 0;
Ted Kremenekdcfb3602011-01-21 22:49:49 +0000421 QualType vdTy = vd->getType().getCanonicalType();
Ted Kremenekfbb178a2011-01-21 19:41:46 +0000422
423 if (vdTy->getAs<ObjCObjectPointerType>()) {
Ted Kremenekdcfb3602011-01-21 22:49:49 +0000424 // Check if 'nil' is defined.
425 if (S.PP.getMacroInfo(&S.getASTContext().Idents.get("nil")))
426 initialization = " = nil";
427 else
428 initialization = " = 0";
Ted Kremenekfbb178a2011-01-21 19:41:46 +0000429 }
Ted Kremenekdcfb3602011-01-21 22:49:49 +0000430 else if (vdTy->isRealFloatingType()) {
431 initialization = " = 0.0";
432 }
433 else if (vdTy->isScalarType()) {
Ted Kremenekfbb178a2011-01-21 19:41:46 +0000434 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 Kremenek94b1b4d2011-01-21 19:41:41 +0000443 delete vec;
444 }
445 delete uses;
Ted Kremenek610068c2011-01-15 02:58:47 +0000446 }
447};
448}
449
450//===----------------------------------------------------------------------===//
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000451// AnalysisBasedWarnings - Worker object used by Sema to execute analysis-based
452// warnings on a function, method, or block.
453//===----------------------------------------------------------------------===//
454
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000455clang::sema::AnalysisBasedWarnings::Policy::Policy() {
456 enableCheckFallThrough = 1;
457 enableCheckUnreachable = 0;
458}
459
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000460clang::sema::AnalysisBasedWarnings::AnalysisBasedWarnings(Sema &s) : S(s) {
461 Diagnostic &D = S.getDiagnostics();
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000462 DefaultPolicy.enableCheckUnreachable = (unsigned)
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000463 (D.getDiagnosticLevel(diag::warn_unreachable, SourceLocation()) !=
464 Diagnostic::Ignored);
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000465}
466
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000467void clang::sema::
468AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P,
Ted Kremenekb7e5f142010-04-08 18:51:44 +0000469 const Decl *D, QualType BlockTy) {
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000470
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000471 assert(BlockTy.isNull() || isa<BlockDecl>(D));
Ted Kremenekd068aab2010-03-20 21:11:09 +0000472
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000473 // 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 Kremenek99e81922010-04-30 21:49:25 +0000479 Diagnostic &Diags = S.getDiagnostics();
480
481 if (Diags.hasErrorOccurred() || Diags.hasFatalErrorOccurred())
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000482 return;
483
484 // Do not do any analysis for declarations in system headers if we are
485 // going to just ignore them.
Ted Kremenek99e81922010-04-30 21:49:25 +0000486 if (Diags.getSuppressSystemWarnings() &&
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000487 S.SourceMgr.isInSystemHeader(D->getLocation()))
488 return;
489
John McCalle0054f62010-08-25 05:56:39 +0000490 // For code in dependent contexts, we'll do this at instantiation time.
491 if (cast<DeclContext>(D)->isDependentContext())
492 return;
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000493
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 Carrutheeef9242011-01-08 06:54:40 +0000499 AnalysisContext AC(D, 0, /*useUnoptimizedCFG=*/false, /*addehedges=*/false,
500 /*addImplicitDtors=*/true, /*addInitializers=*/true);
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000501
502 // Warning: check missing 'return'
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000503 if (P.enableCheckFallThrough) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000504 const CheckFallThroughDiagnostics &CD =
505 (isa<BlockDecl>(D) ? CheckFallThroughDiagnostics::MakeForBlock()
Douglas Gregorca7eaee2010-04-16 23:28:44 +0000506 : CheckFallThroughDiagnostics::MakeForFunction(D));
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000507 CheckFallThroughForBody(S, D, Body, BlockTy, CD, AC);
508 }
509
510 // Warning: check for unreachable code
Ted Kremenekb7e5f142010-04-08 18:51:44 +0000511 if (P.enableCheckUnreachable)
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000512 CheckUnreachable(S, AC);
Ted Kremenek610068c2011-01-15 02:58:47 +0000513
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000514 if (Diags.getDiagnosticLevel(diag::warn_uninit_var, D->getLocStart())
Ted Kremenek610068c2011-01-15 02:58:47 +0000515 != Diagnostic::Ignored) {
Ted Kremenekc21fed32011-01-18 21:18:58 +0000516 if (CFG *cfg = AC.getCFG()) {
517 UninitValsDiagReporter reporter(S);
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000518 runUninitializedVariablesAnalysis(*cast<DeclContext>(D), *cfg, AC,
519 reporter);
Ted Kremenek610068c2011-01-15 02:58:47 +0000520 }
521 }
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000522}
John McCalle0054f62010-08-25 05:56:39 +0000523
524void clang::sema::
525AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P,
526 const BlockExpr *E) {
527 return IssueWarnings(P, E->getBlockDecl(), E->getType());
528}
529
530void clang::sema::
531AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P,
532 const ObjCMethodDecl *D) {
533 return IssueWarnings(P, D, QualType());
534}
535
536void clang::sema::
537AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P,
538 const FunctionDecl *D) {
539 return IssueWarnings(P, D, QualType());
540}