blob: 99f19fca78476b6c92600a22e3752a21f2e5ed48 [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;
Ted Kremenek5811f592011-01-26 04:49:52 +0000124
125 // Destructors can appear after the 'return' in the CFG. This is
126 // normal. We need to look pass the destructors for the return
127 // statement (if it exists).
128 CFGBlock::const_reverse_iterator ri = B.rbegin(), re = B.rend();
129 for ( ; ri != re ; ++ri) {
130 CFGElement CE = *ri;
131 if (isa<CFGStmt>(CE))
132 break;
133 }
134
135 // No more CFGElements in the block?
136 if (ri == re) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000137 if (B.getTerminator() && isa<CXXTryStmt>(B.getTerminator())) {
138 HasAbnormalEdge = true;
139 continue;
140 }
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000141 // A labeled empty statement, or the entry block...
142 HasPlainEdge = true;
143 continue;
144 }
Ted Kremenekf39e6a32011-01-25 22:50:47 +0000145
Ted Kremenek5811f592011-01-26 04:49:52 +0000146 CFGStmt CS = cast<CFGStmt>(*ri);
Zhongxing Xub36cd3e2010-09-16 01:25:47 +0000147 Stmt *S = CS.getStmt();
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000148 if (isa<ReturnStmt>(S)) {
149 HasLiveReturn = true;
150 continue;
151 }
152 if (isa<ObjCAtThrowStmt>(S)) {
153 HasFakeEdge = true;
154 continue;
155 }
156 if (isa<CXXThrowExpr>(S)) {
157 HasFakeEdge = true;
158 continue;
159 }
160 if (const AsmStmt *AS = dyn_cast<AsmStmt>(S)) {
161 if (AS->isMSAsm()) {
162 HasFakeEdge = true;
163 HasLiveReturn = true;
164 continue;
165 }
166 }
167 if (isa<CXXTryStmt>(S)) {
168 HasAbnormalEdge = true;
169 continue;
170 }
171
172 bool NoReturnEdge = false;
173 if (CallExpr *C = dyn_cast<CallExpr>(S)) {
John McCall259d48e2010-04-30 07:10:06 +0000174 if (std::find(B.succ_begin(), B.succ_end(), &cfg->getExit())
175 == B.succ_end()) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000176 HasAbnormalEdge = true;
177 continue;
178 }
179 Expr *CEE = C->getCallee()->IgnoreParenCasts();
Rafael Espindola264ba482010-03-30 20:24:48 +0000180 if (getFunctionExtInfo(CEE->getType()).getNoReturn()) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000181 NoReturnEdge = true;
182 HasFakeEdge = true;
183 } else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE)) {
184 ValueDecl *VD = DRE->getDecl();
185 if (VD->hasAttr<NoReturnAttr>()) {
186 NoReturnEdge = true;
187 HasFakeEdge = true;
188 }
189 }
190 }
191 // FIXME: Add noreturn message sends.
192 if (NoReturnEdge == false)
193 HasPlainEdge = true;
194 }
195 if (!HasPlainEdge) {
196 if (HasLiveReturn)
197 return NeverFallThrough;
198 return NeverFallThroughOrReturn;
199 }
200 if (HasAbnormalEdge || HasFakeEdge || HasLiveReturn)
201 return MaybeFallThrough;
202 // This says AlwaysFallThrough for calls to functions that are not marked
203 // noreturn, that don't return. If people would like this warning to be more
204 // accurate, such functions should be marked as noreturn.
205 return AlwaysFallThrough;
206}
207
Dan Gohman3c46e8d2010-07-26 21:25:24 +0000208namespace {
209
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000210struct CheckFallThroughDiagnostics {
211 unsigned diag_MaybeFallThrough_HasNoReturn;
212 unsigned diag_MaybeFallThrough_ReturnsNonVoid;
213 unsigned diag_AlwaysFallThrough_HasNoReturn;
214 unsigned diag_AlwaysFallThrough_ReturnsNonVoid;
215 unsigned diag_NeverFallThroughOrReturn;
216 bool funMode;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000217 SourceLocation FuncLoc;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000218
Douglas Gregorca7eaee2010-04-16 23:28:44 +0000219 static CheckFallThroughDiagnostics MakeForFunction(const Decl *Func) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000220 CheckFallThroughDiagnostics D;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000221 D.FuncLoc = Func->getLocation();
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000222 D.diag_MaybeFallThrough_HasNoReturn =
223 diag::warn_falloff_noreturn_function;
224 D.diag_MaybeFallThrough_ReturnsNonVoid =
225 diag::warn_maybe_falloff_nonvoid_function;
226 D.diag_AlwaysFallThrough_HasNoReturn =
227 diag::warn_falloff_noreturn_function;
228 D.diag_AlwaysFallThrough_ReturnsNonVoid =
229 diag::warn_falloff_nonvoid_function;
Douglas Gregorca7eaee2010-04-16 23:28:44 +0000230
231 // Don't suggest that virtual functions be marked "noreturn", since they
232 // might be overridden by non-noreturn functions.
233 bool isVirtualMethod = false;
234 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Func))
235 isVirtualMethod = Method->isVirtual();
236
237 if (!isVirtualMethod)
238 D.diag_NeverFallThroughOrReturn =
239 diag::warn_suggest_noreturn_function;
240 else
241 D.diag_NeverFallThroughOrReturn = 0;
242
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000243 D.funMode = true;
244 return D;
245 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000246
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000247 static CheckFallThroughDiagnostics MakeForBlock() {
248 CheckFallThroughDiagnostics D;
249 D.diag_MaybeFallThrough_HasNoReturn =
250 diag::err_noreturn_block_has_return_expr;
251 D.diag_MaybeFallThrough_ReturnsNonVoid =
252 diag::err_maybe_falloff_nonvoid_block;
253 D.diag_AlwaysFallThrough_HasNoReturn =
254 diag::err_noreturn_block_has_return_expr;
255 D.diag_AlwaysFallThrough_ReturnsNonVoid =
256 diag::err_falloff_nonvoid_block;
257 D.diag_NeverFallThroughOrReturn =
258 diag::warn_suggest_noreturn_block;
259 D.funMode = false;
260 return D;
261 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000262
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000263 bool checkDiagnostics(Diagnostic &D, bool ReturnsVoid,
264 bool HasNoReturn) const {
265 if (funMode) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000266 return (ReturnsVoid ||
267 D.getDiagnosticLevel(diag::warn_maybe_falloff_nonvoid_function,
268 FuncLoc) == Diagnostic::Ignored)
269 && (!HasNoReturn ||
270 D.getDiagnosticLevel(diag::warn_noreturn_function_has_return_expr,
271 FuncLoc) == Diagnostic::Ignored)
272 && (!ReturnsVoid ||
273 D.getDiagnosticLevel(diag::warn_suggest_noreturn_block, FuncLoc)
274 == Diagnostic::Ignored);
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000275 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000276
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000277 // For blocks.
278 return ReturnsVoid && !HasNoReturn
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000279 && (!ReturnsVoid ||
280 D.getDiagnosticLevel(diag::warn_suggest_noreturn_block, FuncLoc)
281 == Diagnostic::Ignored);
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000282 }
283};
284
Dan Gohman3c46e8d2010-07-26 21:25:24 +0000285}
286
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000287/// CheckFallThroughForFunctionDef - Check that we don't fall off the end of a
288/// function that should return a value. Check that we don't fall off the end
289/// of a noreturn function. We assume that functions and blocks not marked
290/// noreturn will return.
291static void CheckFallThroughForBody(Sema &S, const Decl *D, const Stmt *Body,
292 QualType BlockTy,
293 const CheckFallThroughDiagnostics& CD,
294 AnalysisContext &AC) {
295
296 bool ReturnsVoid = false;
297 bool HasNoReturn = false;
298
299 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
300 ReturnsVoid = FD->getResultType()->isVoidType();
301 HasNoReturn = FD->hasAttr<NoReturnAttr>() ||
Rafael Espindola264ba482010-03-30 20:24:48 +0000302 FD->getType()->getAs<FunctionType>()->getNoReturnAttr();
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000303 }
304 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
305 ReturnsVoid = MD->getResultType()->isVoidType();
306 HasNoReturn = MD->hasAttr<NoReturnAttr>();
307 }
308 else if (isa<BlockDecl>(D)) {
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000309 if (const FunctionType *FT =
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000310 BlockTy->getPointeeType()->getAs<FunctionType>()) {
311 if (FT->getResultType()->isVoidType())
312 ReturnsVoid = true;
313 if (FT->getNoReturnAttr())
314 HasNoReturn = true;
315 }
316 }
317
318 Diagnostic &Diags = S.getDiagnostics();
319
320 // Short circuit for compilation speed.
321 if (CD.checkDiagnostics(Diags, ReturnsVoid, HasNoReturn))
322 return;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000323
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000324 // FIXME: Function try block
325 if (const CompoundStmt *Compound = dyn_cast<CompoundStmt>(Body)) {
326 switch (CheckFallThrough(AC)) {
John McCall16565aa2010-05-16 09:34:11 +0000327 case UnknownFallThrough:
328 break;
329
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000330 case MaybeFallThrough:
331 if (HasNoReturn)
332 S.Diag(Compound->getRBracLoc(),
333 CD.diag_MaybeFallThrough_HasNoReturn);
334 else if (!ReturnsVoid)
335 S.Diag(Compound->getRBracLoc(),
336 CD.diag_MaybeFallThrough_ReturnsNonVoid);
337 break;
338 case AlwaysFallThrough:
339 if (HasNoReturn)
340 S.Diag(Compound->getRBracLoc(),
341 CD.diag_AlwaysFallThrough_HasNoReturn);
342 else if (!ReturnsVoid)
343 S.Diag(Compound->getRBracLoc(),
344 CD.diag_AlwaysFallThrough_ReturnsNonVoid);
345 break;
346 case NeverFallThroughOrReturn:
Douglas Gregorca7eaee2010-04-16 23:28:44 +0000347 if (ReturnsVoid && !HasNoReturn && CD.diag_NeverFallThroughOrReturn)
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000348 S.Diag(Compound->getLBracLoc(),
349 CD.diag_NeverFallThroughOrReturn);
350 break;
351 case NeverFallThrough:
352 break;
353 }
354 }
355}
356
357//===----------------------------------------------------------------------===//
Ted Kremenek610068c2011-01-15 02:58:47 +0000358// -Wuninitialized
359//===----------------------------------------------------------------------===//
360
361namespace {
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000362struct SLocSort {
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000363 bool operator()(const Expr *a, const Expr *b) {
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000364 SourceLocation aLoc = a->getLocStart();
365 SourceLocation bLoc = b->getLocStart();
366 return aLoc.getRawEncoding() < bLoc.getRawEncoding();
367 }
368};
369
Ted Kremenek610068c2011-01-15 02:58:47 +0000370class UninitValsDiagReporter : public UninitVariablesHandler {
371 Sema &S;
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000372 typedef llvm::SmallVector<const Expr *, 2> UsesVec;
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000373 typedef llvm::DenseMap<const VarDecl *, UsesVec*> UsesMap;
374 UsesMap *uses;
375
Ted Kremenek610068c2011-01-15 02:58:47 +0000376public:
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000377 UninitValsDiagReporter(Sema &S) : S(S), uses(0) {}
378 ~UninitValsDiagReporter() {
379 flushDiagnostics();
380 }
Ted Kremenek610068c2011-01-15 02:58:47 +0000381
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000382 void handleUseOfUninitVariable(const Expr *ex, const VarDecl *vd) {
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000383 if (!uses)
384 uses = new UsesMap();
385
386 UsesVec *&vec = (*uses)[vd];
387 if (!vec)
388 vec = new UsesVec();
389
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000390 vec->push_back(ex);
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000391 }
392
393 void flushDiagnostics() {
394 if (!uses)
395 return;
396
397 for (UsesMap::iterator i = uses->begin(), e = uses->end(); i != e; ++i) {
398 const VarDecl *vd = i->first;
399 UsesVec *vec = i->second;
400
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000401 S.Diag(vd->getLocStart(), diag::warn_uninit_var)
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000402 << vd->getDeclName() << vd->getSourceRange();
Ted Kremenekfbb178a2011-01-21 19:41:46 +0000403
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000404 // Sort the uses by their SourceLocations. While not strictly
405 // guaranteed to produce them in line/column order, this will provide
406 // a stable ordering.
407 std::sort(vec->begin(), vec->end(), SLocSort());
408
409 for (UsesVec::iterator vi = vec->begin(), ve = vec->end(); vi != ve; ++vi)
410 {
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000411 if (const DeclRefExpr *dr = dyn_cast<DeclRefExpr>(*vi)) {
412 S.Diag(dr->getLocStart(), diag::note_uninit_var)
413 << vd->getDeclName() << dr->getSourceRange();
414 }
415 else {
416 const BlockExpr *be = cast<BlockExpr>(*vi);
417 S.Diag(be->getLocStart(), diag::note_uninit_var_captured_by_block)
418 << vd->getDeclName();
419 }
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000420 }
Ted Kremenekfbb178a2011-01-21 19:41:46 +0000421
422 // Suggest possible initialization (if any).
423 const char *initialization = 0;
Ted Kremenekdcfb3602011-01-21 22:49:49 +0000424 QualType vdTy = vd->getType().getCanonicalType();
Ted Kremenekfbb178a2011-01-21 19:41:46 +0000425
426 if (vdTy->getAs<ObjCObjectPointerType>()) {
Ted Kremenekdcfb3602011-01-21 22:49:49 +0000427 // Check if 'nil' is defined.
428 if (S.PP.getMacroInfo(&S.getASTContext().Idents.get("nil")))
429 initialization = " = nil";
430 else
431 initialization = " = 0";
Ted Kremenekfbb178a2011-01-21 19:41:46 +0000432 }
Ted Kremenekdcfb3602011-01-21 22:49:49 +0000433 else if (vdTy->isRealFloatingType()) {
434 initialization = " = 0.0";
435 }
436 else if (vdTy->isScalarType()) {
Ted Kremenekfbb178a2011-01-21 19:41:46 +0000437 initialization = " = 0";
438 }
439
440 if (initialization) {
441 SourceLocation loc = S.PP.getLocForEndOfToken(vd->getLocEnd());
442 S.Diag(loc, diag::note_var_fixit_add_initialization)
443 << FixItHint::CreateInsertion(loc, initialization);
444 }
445
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000446 delete vec;
447 }
448 delete uses;
Ted Kremenek610068c2011-01-15 02:58:47 +0000449 }
450};
451}
452
453//===----------------------------------------------------------------------===//
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000454// AnalysisBasedWarnings - Worker object used by Sema to execute analysis-based
455// warnings on a function, method, or block.
456//===----------------------------------------------------------------------===//
457
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000458clang::sema::AnalysisBasedWarnings::Policy::Policy() {
459 enableCheckFallThrough = 1;
460 enableCheckUnreachable = 0;
461}
462
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000463clang::sema::AnalysisBasedWarnings::AnalysisBasedWarnings(Sema &s) : S(s) {
464 Diagnostic &D = S.getDiagnostics();
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000465 DefaultPolicy.enableCheckUnreachable = (unsigned)
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000466 (D.getDiagnosticLevel(diag::warn_unreachable, SourceLocation()) !=
467 Diagnostic::Ignored);
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000468}
469
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000470void clang::sema::
471AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P,
Ted Kremenekb7e5f142010-04-08 18:51:44 +0000472 const Decl *D, QualType BlockTy) {
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000473
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000474 assert(BlockTy.isNull() || isa<BlockDecl>(D));
Ted Kremenekd068aab2010-03-20 21:11:09 +0000475
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000476 // We avoid doing analysis-based warnings when there are errors for
477 // two reasons:
478 // (1) The CFGs often can't be constructed (if the body is invalid), so
479 // don't bother trying.
480 // (2) The code already has problems; running the analysis just takes more
481 // time.
Ted Kremenek99e81922010-04-30 21:49:25 +0000482 Diagnostic &Diags = S.getDiagnostics();
483
484 if (Diags.hasErrorOccurred() || Diags.hasFatalErrorOccurred())
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000485 return;
486
487 // Do not do any analysis for declarations in system headers if we are
488 // going to just ignore them.
Ted Kremenek99e81922010-04-30 21:49:25 +0000489 if (Diags.getSuppressSystemWarnings() &&
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000490 S.SourceMgr.isInSystemHeader(D->getLocation()))
491 return;
492
John McCalle0054f62010-08-25 05:56:39 +0000493 // For code in dependent contexts, we'll do this at instantiation time.
494 if (cast<DeclContext>(D)->isDependentContext())
495 return;
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000496
497 const Stmt *Body = D->getBody();
498 assert(Body);
499
500 // Don't generate EH edges for CallExprs as we'd like to avoid the n^2
501 // explosion for destrutors that can result and the compile time hit.
Chandler Carrutheeef9242011-01-08 06:54:40 +0000502 AnalysisContext AC(D, 0, /*useUnoptimizedCFG=*/false, /*addehedges=*/false,
503 /*addImplicitDtors=*/true, /*addInitializers=*/true);
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000504
505 // Warning: check missing 'return'
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000506 if (P.enableCheckFallThrough) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000507 const CheckFallThroughDiagnostics &CD =
508 (isa<BlockDecl>(D) ? CheckFallThroughDiagnostics::MakeForBlock()
Douglas Gregorca7eaee2010-04-16 23:28:44 +0000509 : CheckFallThroughDiagnostics::MakeForFunction(D));
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000510 CheckFallThroughForBody(S, D, Body, BlockTy, CD, AC);
511 }
512
513 // Warning: check for unreachable code
Ted Kremenekb7e5f142010-04-08 18:51:44 +0000514 if (P.enableCheckUnreachable)
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000515 CheckUnreachable(S, AC);
Ted Kremenek610068c2011-01-15 02:58:47 +0000516
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000517 if (Diags.getDiagnosticLevel(diag::warn_uninit_var, D->getLocStart())
Ted Kremenek610068c2011-01-15 02:58:47 +0000518 != Diagnostic::Ignored) {
Ted Kremenekc21fed32011-01-18 21:18:58 +0000519 if (CFG *cfg = AC.getCFG()) {
520 UninitValsDiagReporter reporter(S);
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000521 runUninitializedVariablesAnalysis(*cast<DeclContext>(D), *cfg, AC,
522 reporter);
Ted Kremenek610068c2011-01-15 02:58:47 +0000523 }
524 }
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000525}
John McCalle0054f62010-08-25 05:56:39 +0000526
527void clang::sema::
528AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P,
529 const BlockExpr *E) {
530 return IssueWarnings(P, E->getBlockDecl(), E->getType());
531}
532
533void clang::sema::
534AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P,
535 const ObjCMethodDecl *D) {
536 return IssueWarnings(P, D, QualType());
537}
538
539void clang::sema::
540AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P,
541 const FunctionDecl *D) {
542 return IssueWarnings(P, D, QualType());
543}