blob: 91f95a762ac2d29a7ed9999ab0ecaa4f6b66f0c6 [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];
Anders Carlsson0dc5f9a2011-01-16 22:12:43 +0000135 if (CFGInitializer CI = CE.getAs<CFGInitializer>()) {
136 // A base or member initializer.
137 HasPlainEdge = true;
138 continue;
139 }
Anders Carlsson22c41202011-01-17 19:06:31 +0000140 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 Xub36cd3e2010-09-16 01:25:47 +0000150 CFGStmt CS = CE.getAs<CFGStmt>();
151 if (!CS.isValid())
152 continue;
153 Stmt *S = CS.getStmt();
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000154 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 McCall259d48e2010-04-30 07:10:06 +0000180 if (std::find(B.succ_begin(), B.succ_end(), &cfg->getExit())
181 == B.succ_end()) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000182 HasAbnormalEdge = true;
183 continue;
184 }
185 Expr *CEE = C->getCallee()->IgnoreParenCasts();
Rafael Espindola264ba482010-03-30 20:24:48 +0000186 if (getFunctionExtInfo(CEE->getType()).getNoReturn()) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000187 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 Gohman3c46e8d2010-07-26 21:25:24 +0000214namespace {
215
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000216struct 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 Kyrtzidis08274082010-12-15 18:44:22 +0000223 SourceLocation FuncLoc;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000224
Douglas Gregorca7eaee2010-04-16 23:28:44 +0000225 static CheckFallThroughDiagnostics MakeForFunction(const Decl *Func) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000226 CheckFallThroughDiagnostics D;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000227 D.FuncLoc = Func->getLocation();
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000228 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 Gregorca7eaee2010-04-16 23:28:44 +0000236
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 Kremenekdbdbaaf2010-03-20 21:06:02 +0000249 D.funMode = true;
250 return D;
251 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000252
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000253 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 Kremenekd064fdc2010-03-23 00:13:23 +0000268
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000269 bool checkDiagnostics(Diagnostic &D, bool ReturnsVoid,
270 bool HasNoReturn) const {
271 if (funMode) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000272 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 Kremenekdbdbaaf2010-03-20 21:06:02 +0000281 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000282
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000283 // For blocks.
284 return ReturnsVoid && !HasNoReturn
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000285 && (!ReturnsVoid ||
286 D.getDiagnosticLevel(diag::warn_suggest_noreturn_block, FuncLoc)
287 == Diagnostic::Ignored);
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000288 }
289};
290
Dan Gohman3c46e8d2010-07-26 21:25:24 +0000291}
292
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000293/// 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.
297static 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 Espindola264ba482010-03-30 20:24:48 +0000308 FD->getType()->getAs<FunctionType>()->getNoReturnAttr();
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000309 }
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 Kremenekd064fdc2010-03-23 00:13:23 +0000315 if (const FunctionType *FT =
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000316 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 Kremenekd064fdc2010-03-23 00:13:23 +0000329
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000330 // FIXME: Function try block
331 if (const CompoundStmt *Compound = dyn_cast<CompoundStmt>(Body)) {
332 switch (CheckFallThrough(AC)) {
John McCall16565aa2010-05-16 09:34:11 +0000333 case UnknownFallThrough:
334 break;
335
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000336 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 Gregorca7eaee2010-04-16 23:28:44 +0000353 if (ReturnsVoid && !HasNoReturn && CD.diag_NeverFallThroughOrReturn)
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000354 S.Diag(Compound->getLBracLoc(),
355 CD.diag_NeverFallThroughOrReturn);
356 break;
357 case NeverFallThrough:
358 break;
359 }
360 }
361}
362
363//===----------------------------------------------------------------------===//
Ted Kremenek610068c2011-01-15 02:58:47 +0000364// -Wuninitialized
365//===----------------------------------------------------------------------===//
366
367namespace {
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000368struct 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 Kremenek610068c2011-01-15 02:58:47 +0000376class UninitValsDiagReporter : public UninitVariablesHandler {
377 Sema &S;
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000378 typedef llvm::SmallVector<const DeclRefExpr *, 2> UsesVec;
379 typedef llvm::DenseMap<const VarDecl *, UsesVec*> UsesMap;
380 UsesMap *uses;
381
Ted Kremenek610068c2011-01-15 02:58:47 +0000382public:
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000383 UninitValsDiagReporter(Sema &S) : S(S), uses(0) {}
384 ~UninitValsDiagReporter() {
385 flushDiagnostics();
386 }
Ted Kremenek610068c2011-01-15 02:58:47 +0000387
388 void handleUseOfUninitVariable(const DeclRefExpr *dr, const VarDecl *vd) {
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000389 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 Kremenekfbb178a2011-01-21 19:41:46 +0000409
Ted Kremenek94b1b4d2011-01-21 19:41:41 +0000410 // 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 Kremenekfbb178a2011-01-21 19:41:46 +0000421
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 Kremenek94b1b4d2011-01-21 19:41:41 +0000439 delete vec;
440 }
441 delete uses;
Ted Kremenek610068c2011-01-15 02:58:47 +0000442 }
443};
444}
445
446//===----------------------------------------------------------------------===//
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000447// AnalysisBasedWarnings - Worker object used by Sema to execute analysis-based
448// warnings on a function, method, or block.
449//===----------------------------------------------------------------------===//
450
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000451clang::sema::AnalysisBasedWarnings::Policy::Policy() {
452 enableCheckFallThrough = 1;
453 enableCheckUnreachable = 0;
454}
455
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000456clang::sema::AnalysisBasedWarnings::AnalysisBasedWarnings(Sema &s) : S(s) {
457 Diagnostic &D = S.getDiagnostics();
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000458 DefaultPolicy.enableCheckUnreachable = (unsigned)
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000459 (D.getDiagnosticLevel(diag::warn_unreachable, SourceLocation()) !=
460 Diagnostic::Ignored);
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000461}
462
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000463void clang::sema::
464AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P,
Ted Kremenekb7e5f142010-04-08 18:51:44 +0000465 const Decl *D, QualType BlockTy) {
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000466
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000467 assert(BlockTy.isNull() || isa<BlockDecl>(D));
Ted Kremenekd068aab2010-03-20 21:11:09 +0000468
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000469 // 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 Kremenek99e81922010-04-30 21:49:25 +0000475 Diagnostic &Diags = S.getDiagnostics();
476
477 if (Diags.hasErrorOccurred() || Diags.hasFatalErrorOccurred())
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000478 return;
479
480 // Do not do any analysis for declarations in system headers if we are
481 // going to just ignore them.
Ted Kremenek99e81922010-04-30 21:49:25 +0000482 if (Diags.getSuppressSystemWarnings() &&
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000483 S.SourceMgr.isInSystemHeader(D->getLocation()))
484 return;
485
John McCalle0054f62010-08-25 05:56:39 +0000486 // For code in dependent contexts, we'll do this at instantiation time.
487 if (cast<DeclContext>(D)->isDependentContext())
488 return;
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000489
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 Carrutheeef9242011-01-08 06:54:40 +0000495 AnalysisContext AC(D, 0, /*useUnoptimizedCFG=*/false, /*addehedges=*/false,
496 /*addImplicitDtors=*/true, /*addInitializers=*/true);
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000497
498 // Warning: check missing 'return'
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000499 if (P.enableCheckFallThrough) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000500 const CheckFallThroughDiagnostics &CD =
501 (isa<BlockDecl>(D) ? CheckFallThroughDiagnostics::MakeForBlock()
Douglas Gregorca7eaee2010-04-16 23:28:44 +0000502 : CheckFallThroughDiagnostics::MakeForFunction(D));
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000503 CheckFallThroughForBody(S, D, Body, BlockTy, CD, AC);
504 }
505
506 // Warning: check for unreachable code
Ted Kremenekb7e5f142010-04-08 18:51:44 +0000507 if (P.enableCheckUnreachable)
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000508 CheckUnreachable(S, AC);
Ted Kremenek610068c2011-01-15 02:58:47 +0000509
510 if (Diags.getDiagnosticLevel(diag::warn_var_is_uninit, D->getLocStart())
511 != Diagnostic::Ignored) {
Ted Kremenekc21fed32011-01-18 21:18:58 +0000512 if (CFG *cfg = AC.getCFG()) {
513 UninitValsDiagReporter reporter(S);
514 runUninitializedVariablesAnalysis(*cast<DeclContext>(D), *cfg, reporter);
Ted Kremenek610068c2011-01-15 02:58:47 +0000515 }
516 }
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000517}
John McCalle0054f62010-08-25 05:56:39 +0000518
519void clang::sema::
520AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P,
521 const BlockExpr *E) {
522 return IssueWarnings(P, E->getBlockDecl(), E->getType());
523}
524
525void clang::sema::
526AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P,
527 const ObjCMethodDecl *D) {
528 return IssueWarnings(P, D, QualType());
529}
530
531void clang::sema::
532AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P,
533 const FunctionDecl *D) {
534 return IssueWarnings(P, D, QualType());
535}