blob: 9a67d4991fd147e814af7fa9f9de85f220837c28 [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 McCall384aff82010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/AST/DeclObjC.h"
Ted Kremenek6f417152011-04-04 20:56:00 +000019#include "clang/AST/EvaluatedExprVisitor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/AST/ExprCXX.h"
21#include "clang/AST/ExprObjC.h"
Jordan Roseb5cd1222012-10-11 16:10:19 +000022#include "clang/AST/ParentMap.h"
Richard Smithe0d3b4c2012-05-03 18:27:39 +000023#include "clang/AST/RecursiveASTVisitor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000024#include "clang/AST/StmtCXX.h"
25#include "clang/AST/StmtObjC.h"
26#include "clang/AST/StmtVisitor.h"
27#include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h"
28#include "clang/Analysis/Analyses/ReachableCode.h"
29#include "clang/Analysis/Analyses/ThreadSafety.h"
30#include "clang/Analysis/Analyses/UninitializedValues.h"
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +000031#include "clang/Analysis/AnalysisContext.h"
32#include "clang/Analysis/CFG.h"
Ted Kremenek351ba912011-02-23 01:52:04 +000033#include "clang/Analysis/CFGStmtMap.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000034#include "clang/Basic/SourceLocation.h"
35#include "clang/Basic/SourceManager.h"
36#include "clang/Lex/Lexer.h"
37#include "clang/Lex/Preprocessor.h"
38#include "clang/Sema/ScopeInfo.h"
39#include "clang/Sema/SemaInternal.h"
Alexander Kornienko66da0ab2012-09-28 22:24:03 +000040#include "llvm/ADT/ArrayRef.h"
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +000041#include "llvm/ADT/BitVector.h"
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +000042#include "llvm/ADT/FoldingSet.h"
43#include "llvm/ADT/ImmutableMap.h"
Enea Zaffanella3285c782013-02-15 20:09:55 +000044#include "llvm/ADT/MapVector.h"
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +000045#include "llvm/ADT/PostOrderIterator.h"
Dmitri Gribenko19523542012-09-29 11:40:46 +000046#include "llvm/ADT/SmallString.h"
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +000047#include "llvm/ADT/SmallVector.h"
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +000048#include "llvm/ADT/StringRef.h"
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +000049#include "llvm/Support/Casting.h"
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +000050#include <algorithm>
Chandler Carruth55fc8732012-12-04 09:13:33 +000051#include <deque>
Richard Smithe0d3b4c2012-05-03 18:27:39 +000052#include <iterator>
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +000053#include <vector>
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +000054
55using namespace clang;
56
57//===----------------------------------------------------------------------===//
58// Unreachable code analysis.
59//===----------------------------------------------------------------------===//
60
61namespace {
62 class UnreachableCodeHandler : public reachable_code::Callback {
63 Sema &S;
64 public:
65 UnreachableCodeHandler(Sema &s) : S(s) {}
66
67 void HandleUnreachable(SourceLocation L, SourceRange R1, SourceRange R2) {
68 S.Diag(L, diag::warn_unreachable) << R1 << R2;
69 }
70 };
71}
72
73/// CheckUnreachable - Check for unreachable code.
Ted Kremenek1d26f482011-10-24 01:32:45 +000074static void CheckUnreachable(Sema &S, AnalysisDeclContext &AC) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +000075 UnreachableCodeHandler UC(S);
76 reachable_code::FindUnreachableCode(AC, UC);
77}
78
79//===----------------------------------------------------------------------===//
80// Check for missing return value.
81//===----------------------------------------------------------------------===//
82
John McCall16565aa2010-05-16 09:34:11 +000083enum ControlFlowKind {
84 UnknownFallThrough,
85 NeverFallThrough,
86 MaybeFallThrough,
87 AlwaysFallThrough,
88 NeverFallThroughOrReturn
89};
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +000090
91/// CheckFallThrough - Check that we don't fall off the end of a
92/// Statement that should return a value.
93///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +000094/// \returns AlwaysFallThrough iff we always fall off the end of the statement,
95/// MaybeFallThrough iff we might or might not fall off the end,
96/// NeverFallThroughOrReturn iff we never fall off the end of the statement or
97/// return. We assume NeverFallThrough iff we never fall off the end of the
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +000098/// statement but we may return. We assume that functions not marked noreturn
99/// will return.
Ted Kremenek1d26f482011-10-24 01:32:45 +0000100static ControlFlowKind CheckFallThrough(AnalysisDeclContext &AC) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000101 CFG *cfg = AC.getCFG();
John McCall16565aa2010-05-16 09:34:11 +0000102 if (cfg == 0) return UnknownFallThrough;
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000103
104 // The CFG leaves in dead things, and we don't want the dead code paths to
105 // confuse us, so we mark all live things first.
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000106 llvm::BitVector live(cfg->getNumBlockIDs());
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +0000107 unsigned count = reachable_code::ScanReachableFromBlock(&cfg->getEntry(),
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000108 live);
109
110 bool AddEHEdges = AC.getAddEHEdges();
111 if (!AddEHEdges && count != cfg->getNumBlockIDs())
112 // When there are things remaining dead, and we didn't add EH edges
113 // from CallExprs to the catch clauses, we have to go back and
114 // mark them as live.
115 for (CFG::iterator I = cfg->begin(), E = cfg->end(); I != E; ++I) {
116 CFGBlock &b = **I;
117 if (!live[b.getBlockID()]) {
118 if (b.pred_begin() == b.pred_end()) {
119 if (b.getTerminator() && isa<CXXTryStmt>(b.getTerminator()))
120 // When not adding EH edges from calls, catch clauses
121 // can otherwise seem dead. Avoid noting them as dead.
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +0000122 count += reachable_code::ScanReachableFromBlock(&b, live);
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000123 continue;
124 }
125 }
126 }
127
128 // Now we know what is live, we check the live precessors of the exit block
129 // and look for fall through paths, being careful to ignore normal returns,
130 // and exceptional paths.
131 bool HasLiveReturn = false;
132 bool HasFakeEdge = false;
133 bool HasPlainEdge = false;
134 bool HasAbnormalEdge = false;
Ted Kremenek90b828a2010-09-09 00:06:07 +0000135
136 // Ignore default cases that aren't likely to be reachable because all
137 // enums in a switch(X) have explicit case statements.
138 CFGBlock::FilterOptions FO;
139 FO.IgnoreDefaultsWithCoveredEnums = 1;
140
141 for (CFGBlock::filtered_pred_iterator
142 I = cfg->getExit().filtered_pred_start_end(FO); I.hasMore(); ++I) {
143 const CFGBlock& B = **I;
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000144 if (!live[B.getBlockID()])
145 continue;
Ted Kremenek5811f592011-01-26 04:49:52 +0000146
Chandler Carruthe05ee6d2011-09-13 09:53:58 +0000147 // Skip blocks which contain an element marked as no-return. They don't
148 // represent actually viable edges into the exit block, so mark them as
149 // abnormal.
150 if (B.hasNoReturnElement()) {
151 HasAbnormalEdge = true;
152 continue;
153 }
154
Ted Kremenek5811f592011-01-26 04:49:52 +0000155 // Destructors can appear after the 'return' in the CFG. This is
156 // normal. We need to look pass the destructors for the return
157 // statement (if it exists).
158 CFGBlock::const_reverse_iterator ri = B.rbegin(), re = B.rend();
Ted Kremenekc9f8f5a2011-03-02 20:32:29 +0000159
Chandler Carruthe05ee6d2011-09-13 09:53:58 +0000160 for ( ; ri != re ; ++ri)
David Blaikiefdf6a272013-02-21 20:58:29 +0000161 if (ri->getAs<CFGStmt>())
Ted Kremenek5811f592011-01-26 04:49:52 +0000162 break;
Chandler Carruthe05ee6d2011-09-13 09:53:58 +0000163
Ted Kremenek5811f592011-01-26 04:49:52 +0000164 // No more CFGElements in the block?
165 if (ri == re) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000166 if (B.getTerminator() && isa<CXXTryStmt>(B.getTerminator())) {
167 HasAbnormalEdge = true;
168 continue;
169 }
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000170 // A labeled empty statement, or the entry block...
171 HasPlainEdge = true;
172 continue;
173 }
Ted Kremenekf39e6a32011-01-25 22:50:47 +0000174
David Blaikiefdf6a272013-02-21 20:58:29 +0000175 CFGStmt CS = ri->castAs<CFGStmt>();
Ted Kremenekf1d10d92011-08-23 23:05:04 +0000176 const Stmt *S = CS.getStmt();
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000177 if (isa<ReturnStmt>(S)) {
178 HasLiveReturn = true;
179 continue;
180 }
181 if (isa<ObjCAtThrowStmt>(S)) {
182 HasFakeEdge = true;
183 continue;
184 }
185 if (isa<CXXThrowExpr>(S)) {
186 HasFakeEdge = true;
187 continue;
188 }
Chad Rosier8cd64b42012-06-11 20:47:18 +0000189 if (isa<MSAsmStmt>(S)) {
190 // TODO: Verify this is correct.
191 HasFakeEdge = true;
192 HasLiveReturn = true;
193 continue;
194 }
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000195 if (isa<CXXTryStmt>(S)) {
196 HasAbnormalEdge = true;
197 continue;
198 }
Chandler Carruthe05ee6d2011-09-13 09:53:58 +0000199 if (std::find(B.succ_begin(), B.succ_end(), &cfg->getExit())
200 == B.succ_end()) {
201 HasAbnormalEdge = true;
202 continue;
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000203 }
Chandler Carruthe05ee6d2011-09-13 09:53:58 +0000204
205 HasPlainEdge = true;
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000206 }
207 if (!HasPlainEdge) {
208 if (HasLiveReturn)
209 return NeverFallThrough;
210 return NeverFallThroughOrReturn;
211 }
212 if (HasAbnormalEdge || HasFakeEdge || HasLiveReturn)
213 return MaybeFallThrough;
214 // This says AlwaysFallThrough for calls to functions that are not marked
215 // noreturn, that don't return. If people would like this warning to be more
216 // accurate, such functions should be marked as noreturn.
217 return AlwaysFallThrough;
218}
219
Dan Gohman3c46e8d2010-07-26 21:25:24 +0000220namespace {
221
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000222struct CheckFallThroughDiagnostics {
223 unsigned diag_MaybeFallThrough_HasNoReturn;
224 unsigned diag_MaybeFallThrough_ReturnsNonVoid;
225 unsigned diag_AlwaysFallThrough_HasNoReturn;
226 unsigned diag_AlwaysFallThrough_ReturnsNonVoid;
227 unsigned diag_NeverFallThroughOrReturn;
Douglas Gregor793cd1c2012-02-15 16:20:15 +0000228 enum { Function, Block, Lambda } funMode;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000229 SourceLocation FuncLoc;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000230
Douglas Gregorca7eaee2010-04-16 23:28:44 +0000231 static CheckFallThroughDiagnostics MakeForFunction(const Decl *Func) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000232 CheckFallThroughDiagnostics D;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000233 D.FuncLoc = Func->getLocation();
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000234 D.diag_MaybeFallThrough_HasNoReturn =
235 diag::warn_falloff_noreturn_function;
236 D.diag_MaybeFallThrough_ReturnsNonVoid =
237 diag::warn_maybe_falloff_nonvoid_function;
238 D.diag_AlwaysFallThrough_HasNoReturn =
239 diag::warn_falloff_noreturn_function;
240 D.diag_AlwaysFallThrough_ReturnsNonVoid =
241 diag::warn_falloff_nonvoid_function;
Douglas Gregorca7eaee2010-04-16 23:28:44 +0000242
243 // Don't suggest that virtual functions be marked "noreturn", since they
244 // might be overridden by non-noreturn functions.
245 bool isVirtualMethod = false;
246 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Func))
247 isVirtualMethod = Method->isVirtual();
248
Douglas Gregorfcdd2cb2011-10-10 18:15:57 +0000249 // Don't suggest that template instantiations be marked "noreturn"
250 bool isTemplateInstantiation = false;
Ted Kremenek75df4ee2011-12-01 00:59:17 +0000251 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(Func))
252 isTemplateInstantiation = Function->isTemplateInstantiation();
Douglas Gregorfcdd2cb2011-10-10 18:15:57 +0000253
254 if (!isVirtualMethod && !isTemplateInstantiation)
Douglas Gregorca7eaee2010-04-16 23:28:44 +0000255 D.diag_NeverFallThroughOrReturn =
256 diag::warn_suggest_noreturn_function;
257 else
258 D.diag_NeverFallThroughOrReturn = 0;
259
Douglas Gregor793cd1c2012-02-15 16:20:15 +0000260 D.funMode = Function;
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000261 return D;
262 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000263
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000264 static CheckFallThroughDiagnostics MakeForBlock() {
265 CheckFallThroughDiagnostics D;
266 D.diag_MaybeFallThrough_HasNoReturn =
267 diag::err_noreturn_block_has_return_expr;
268 D.diag_MaybeFallThrough_ReturnsNonVoid =
269 diag::err_maybe_falloff_nonvoid_block;
270 D.diag_AlwaysFallThrough_HasNoReturn =
271 diag::err_noreturn_block_has_return_expr;
272 D.diag_AlwaysFallThrough_ReturnsNonVoid =
273 diag::err_falloff_nonvoid_block;
274 D.diag_NeverFallThroughOrReturn =
275 diag::warn_suggest_noreturn_block;
Douglas Gregor793cd1c2012-02-15 16:20:15 +0000276 D.funMode = Block;
277 return D;
278 }
279
280 static CheckFallThroughDiagnostics MakeForLambda() {
281 CheckFallThroughDiagnostics D;
282 D.diag_MaybeFallThrough_HasNoReturn =
283 diag::err_noreturn_lambda_has_return_expr;
284 D.diag_MaybeFallThrough_ReturnsNonVoid =
285 diag::warn_maybe_falloff_nonvoid_lambda;
286 D.diag_AlwaysFallThrough_HasNoReturn =
287 diag::err_noreturn_lambda_has_return_expr;
288 D.diag_AlwaysFallThrough_ReturnsNonVoid =
289 diag::warn_falloff_nonvoid_lambda;
290 D.diag_NeverFallThroughOrReturn = 0;
291 D.funMode = Lambda;
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000292 return D;
293 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000294
David Blaikied6471f72011-09-25 23:23:43 +0000295 bool checkDiagnostics(DiagnosticsEngine &D, bool ReturnsVoid,
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000296 bool HasNoReturn) const {
Douglas Gregor793cd1c2012-02-15 16:20:15 +0000297 if (funMode == Function) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000298 return (ReturnsVoid ||
299 D.getDiagnosticLevel(diag::warn_maybe_falloff_nonvoid_function,
David Blaikied6471f72011-09-25 23:23:43 +0000300 FuncLoc) == DiagnosticsEngine::Ignored)
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000301 && (!HasNoReturn ||
302 D.getDiagnosticLevel(diag::warn_noreturn_function_has_return_expr,
David Blaikied6471f72011-09-25 23:23:43 +0000303 FuncLoc) == DiagnosticsEngine::Ignored)
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000304 && (!ReturnsVoid ||
305 D.getDiagnosticLevel(diag::warn_suggest_noreturn_block, FuncLoc)
David Blaikied6471f72011-09-25 23:23:43 +0000306 == DiagnosticsEngine::Ignored);
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000307 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000308
Douglas Gregor793cd1c2012-02-15 16:20:15 +0000309 // For blocks / lambdas.
310 return ReturnsVoid && !HasNoReturn
311 && ((funMode == Lambda) ||
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000312 D.getDiagnosticLevel(diag::warn_suggest_noreturn_block, FuncLoc)
David Blaikied6471f72011-09-25 23:23:43 +0000313 == DiagnosticsEngine::Ignored);
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000314 }
315};
316
Dan Gohman3c46e8d2010-07-26 21:25:24 +0000317}
318
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000319/// CheckFallThroughForFunctionDef - Check that we don't fall off the end of a
320/// function that should return a value. Check that we don't fall off the end
321/// of a noreturn function. We assume that functions and blocks not marked
322/// noreturn will return.
323static void CheckFallThroughForBody(Sema &S, const Decl *D, const Stmt *Body,
Ted Kremenek3ed6fc02011-02-23 01:51:48 +0000324 const BlockExpr *blkExpr,
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000325 const CheckFallThroughDiagnostics& CD,
Ted Kremenek1d26f482011-10-24 01:32:45 +0000326 AnalysisDeclContext &AC) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000327
328 bool ReturnsVoid = false;
329 bool HasNoReturn = false;
330
331 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
332 ReturnsVoid = FD->getResultType()->isVoidType();
Richard Smithcd8ab512013-01-17 01:30:42 +0000333 HasNoReturn = FD->isNoReturn();
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000334 }
335 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
336 ReturnsVoid = MD->getResultType()->isVoidType();
337 HasNoReturn = MD->hasAttr<NoReturnAttr>();
338 }
339 else if (isa<BlockDecl>(D)) {
Ted Kremenek3ed6fc02011-02-23 01:51:48 +0000340 QualType BlockTy = blkExpr->getType();
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000341 if (const FunctionType *FT =
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000342 BlockTy->getPointeeType()->getAs<FunctionType>()) {
343 if (FT->getResultType()->isVoidType())
344 ReturnsVoid = true;
345 if (FT->getNoReturnAttr())
346 HasNoReturn = true;
347 }
348 }
349
David Blaikied6471f72011-09-25 23:23:43 +0000350 DiagnosticsEngine &Diags = S.getDiagnostics();
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000351
352 // Short circuit for compilation speed.
353 if (CD.checkDiagnostics(Diags, ReturnsVoid, HasNoReturn))
354 return;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000355
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000356 // FIXME: Function try block
357 if (const CompoundStmt *Compound = dyn_cast<CompoundStmt>(Body)) {
358 switch (CheckFallThrough(AC)) {
John McCall16565aa2010-05-16 09:34:11 +0000359 case UnknownFallThrough:
360 break;
361
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000362 case MaybeFallThrough:
363 if (HasNoReturn)
364 S.Diag(Compound->getRBracLoc(),
365 CD.diag_MaybeFallThrough_HasNoReturn);
366 else if (!ReturnsVoid)
367 S.Diag(Compound->getRBracLoc(),
368 CD.diag_MaybeFallThrough_ReturnsNonVoid);
369 break;
370 case AlwaysFallThrough:
371 if (HasNoReturn)
372 S.Diag(Compound->getRBracLoc(),
373 CD.diag_AlwaysFallThrough_HasNoReturn);
374 else if (!ReturnsVoid)
375 S.Diag(Compound->getRBracLoc(),
376 CD.diag_AlwaysFallThrough_ReturnsNonVoid);
377 break;
378 case NeverFallThroughOrReturn:
Chandler Carruthb0656ec2011-08-31 09:01:53 +0000379 if (ReturnsVoid && !HasNoReturn && CD.diag_NeverFallThroughOrReturn) {
380 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
381 S.Diag(Compound->getLBracLoc(), CD.diag_NeverFallThroughOrReturn)
Douglas Gregorb3321092011-09-10 00:56:20 +0000382 << 0 << FD;
383 } else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
384 S.Diag(Compound->getLBracLoc(), CD.diag_NeverFallThroughOrReturn)
385 << 1 << MD;
Chandler Carruthb0656ec2011-08-31 09:01:53 +0000386 } else {
387 S.Diag(Compound->getLBracLoc(), CD.diag_NeverFallThroughOrReturn);
388 }
389 }
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000390 break;
391 case NeverFallThrough:
392 break;
393 }
394 }
395}
396
397//===----------------------------------------------------------------------===//
Ted Kremenek610068c2011-01-15 02:58:47 +0000398// -Wuninitialized
399//===----------------------------------------------------------------------===//
400
Ted Kremenek6f417152011-04-04 20:56:00 +0000401namespace {
Chandler Carruth9f649462011-04-05 06:48:00 +0000402/// ContainsReference - A visitor class to search for references to
403/// a particular declaration (the needle) within any evaluated component of an
404/// expression (recursively).
Ted Kremenek6f417152011-04-04 20:56:00 +0000405class ContainsReference : public EvaluatedExprVisitor<ContainsReference> {
Chandler Carruth9f649462011-04-05 06:48:00 +0000406 bool FoundReference;
407 const DeclRefExpr *Needle;
408
Ted Kremenek6f417152011-04-04 20:56:00 +0000409public:
Chandler Carruth9f649462011-04-05 06:48:00 +0000410 ContainsReference(ASTContext &Context, const DeclRefExpr *Needle)
411 : EvaluatedExprVisitor<ContainsReference>(Context),
412 FoundReference(false), Needle(Needle) {}
413
414 void VisitExpr(Expr *E) {
Ted Kremenek6f417152011-04-04 20:56:00 +0000415 // Stop evaluating if we already have a reference.
Chandler Carruth9f649462011-04-05 06:48:00 +0000416 if (FoundReference)
Ted Kremenek6f417152011-04-04 20:56:00 +0000417 return;
Chandler Carruth9f649462011-04-05 06:48:00 +0000418
419 EvaluatedExprVisitor<ContainsReference>::VisitExpr(E);
Ted Kremenek6f417152011-04-04 20:56:00 +0000420 }
Chandler Carruth9f649462011-04-05 06:48:00 +0000421
422 void VisitDeclRefExpr(DeclRefExpr *E) {
423 if (E == Needle)
424 FoundReference = true;
425 else
426 EvaluatedExprVisitor<ContainsReference>::VisitDeclRefExpr(E);
Ted Kremenek6f417152011-04-04 20:56:00 +0000427 }
Chandler Carruth9f649462011-04-05 06:48:00 +0000428
429 bool doesContainReference() const { return FoundReference; }
Ted Kremenek6f417152011-04-04 20:56:00 +0000430};
431}
432
David Blaikie4f4f3492011-09-10 05:35:08 +0000433static bool SuggestInitializationFixit(Sema &S, const VarDecl *VD) {
Fariborz Jahaniana34194f2012-03-08 00:22:50 +0000434 QualType VariableTy = VD->getType().getCanonicalType();
435 if (VariableTy->isBlockPointerType() &&
436 !VD->hasAttr<BlocksAttr>()) {
437 S.Diag(VD->getLocation(), diag::note_block_var_fixit_add_initialization) << VD->getDeclName()
438 << FixItHint::CreateInsertion(VD->getLocation(), "__block ");
439 return true;
440 }
441
David Blaikie4f4f3492011-09-10 05:35:08 +0000442 // Don't issue a fixit if there is already an initializer.
443 if (VD->getInit())
444 return false;
Fariborz Jahaniana34194f2012-03-08 00:22:50 +0000445
David Blaikie4f4f3492011-09-10 05:35:08 +0000446 // Suggest possible initialization (if any).
David Blaikie2c0abf42012-04-30 18:27:22 +0000447 std::string Init = S.getFixItZeroInitializerForType(VariableTy);
448 if (Init.empty())
David Blaikie4f4f3492011-09-10 05:35:08 +0000449 return false;
Richard Trieu7b0a3e32012-05-03 01:09:59 +0000450
451 // Don't suggest a fixit inside macros.
452 if (VD->getLocEnd().isMacroID())
453 return false;
454
Richard Smith7984de32012-01-12 23:53:29 +0000455 SourceLocation Loc = S.PP.getLocForEndOfToken(VD->getLocEnd());
Fariborz Jahaniana34194f2012-03-08 00:22:50 +0000456
Richard Smith7984de32012-01-12 23:53:29 +0000457 S.Diag(Loc, diag::note_var_fixit_add_initialization) << VD->getDeclName()
458 << FixItHint::CreateInsertion(Loc, Init);
459 return true;
David Blaikie4f4f3492011-09-10 05:35:08 +0000460}
461
Richard Smithbdb97ff2012-05-26 06:20:46 +0000462/// Create a fixit to remove an if-like statement, on the assumption that its
463/// condition is CondVal.
464static void CreateIfFixit(Sema &S, const Stmt *If, const Stmt *Then,
465 const Stmt *Else, bool CondVal,
466 FixItHint &Fixit1, FixItHint &Fixit2) {
467 if (CondVal) {
468 // If condition is always true, remove all but the 'then'.
469 Fixit1 = FixItHint::CreateRemoval(
470 CharSourceRange::getCharRange(If->getLocStart(),
471 Then->getLocStart()));
472 if (Else) {
473 SourceLocation ElseKwLoc = Lexer::getLocForEndOfToken(
474 Then->getLocEnd(), 0, S.getSourceManager(), S.getLangOpts());
475 Fixit2 = FixItHint::CreateRemoval(
476 SourceRange(ElseKwLoc, Else->getLocEnd()));
477 }
478 } else {
479 // If condition is always false, remove all but the 'else'.
480 if (Else)
481 Fixit1 = FixItHint::CreateRemoval(
482 CharSourceRange::getCharRange(If->getLocStart(),
483 Else->getLocStart()));
484 else
485 Fixit1 = FixItHint::CreateRemoval(If->getSourceRange());
486 }
487}
488
489/// DiagUninitUse -- Helper function to produce a diagnostic for an
490/// uninitialized use of a variable.
491static void DiagUninitUse(Sema &S, const VarDecl *VD, const UninitUse &Use,
492 bool IsCapturedByBlock) {
493 bool Diagnosed = false;
494
495 // Diagnose each branch which leads to a sometimes-uninitialized use.
Richard Smith2815e1a2012-05-25 02:17:09 +0000496 for (UninitUse::branch_iterator I = Use.branch_begin(), E = Use.branch_end();
497 I != E; ++I) {
Richard Smithbdb97ff2012-05-26 06:20:46 +0000498 assert(Use.getKind() == UninitUse::Sometimes);
499
500 const Expr *User = Use.getUser();
Richard Smith2815e1a2012-05-25 02:17:09 +0000501 const Stmt *Term = I->Terminator;
Richard Smithbdb97ff2012-05-26 06:20:46 +0000502
503 // Information used when building the diagnostic.
Richard Smith2815e1a2012-05-25 02:17:09 +0000504 unsigned DiagKind;
David Blaikie0bea8632012-10-08 01:11:04 +0000505 StringRef Str;
Richard Smithbdb97ff2012-05-26 06:20:46 +0000506 SourceRange Range;
507
508 // FixIts to suppress the diagnosic by removing the dead condition.
509 // For all binary terminators, branch 0 is taken if the condition is true,
510 // and branch 1 is taken if the condition is false.
511 int RemoveDiagKind = -1;
512 const char *FixitStr =
513 S.getLangOpts().CPlusPlus ? (I->Output ? "true" : "false")
514 : (I->Output ? "1" : "0");
515 FixItHint Fixit1, Fixit2;
516
Richard Smith2815e1a2012-05-25 02:17:09 +0000517 switch (Term->getStmtClass()) {
518 default:
Richard Smithbdb97ff2012-05-26 06:20:46 +0000519 // Don't know how to report this. Just fall back to 'may be used
520 // uninitialized'. This happens for range-based for, which the user
521 // can't explicitly fix.
522 // FIXME: This also happens if the first use of a variable is always
523 // uninitialized, eg "for (int n; n < 10; ++n)". We should report that
524 // with the 'is uninitialized' diagnostic.
Richard Smith2815e1a2012-05-25 02:17:09 +0000525 continue;
526
527 // "condition is true / condition is false".
Richard Smithbdb97ff2012-05-26 06:20:46 +0000528 case Stmt::IfStmtClass: {
529 const IfStmt *IS = cast<IfStmt>(Term);
Richard Smith2815e1a2012-05-25 02:17:09 +0000530 DiagKind = 0;
531 Str = "if";
Richard Smithbdb97ff2012-05-26 06:20:46 +0000532 Range = IS->getCond()->getSourceRange();
533 RemoveDiagKind = 0;
534 CreateIfFixit(S, IS, IS->getThen(), IS->getElse(),
535 I->Output, Fixit1, Fixit2);
Richard Smith2815e1a2012-05-25 02:17:09 +0000536 break;
Richard Smithbdb97ff2012-05-26 06:20:46 +0000537 }
538 case Stmt::ConditionalOperatorClass: {
539 const ConditionalOperator *CO = cast<ConditionalOperator>(Term);
Richard Smith2815e1a2012-05-25 02:17:09 +0000540 DiagKind = 0;
541 Str = "?:";
Richard Smithbdb97ff2012-05-26 06:20:46 +0000542 Range = CO->getCond()->getSourceRange();
543 RemoveDiagKind = 0;
544 CreateIfFixit(S, CO, CO->getTrueExpr(), CO->getFalseExpr(),
545 I->Output, Fixit1, Fixit2);
Richard Smith2815e1a2012-05-25 02:17:09 +0000546 break;
Richard Smithbdb97ff2012-05-26 06:20:46 +0000547 }
Richard Smith2815e1a2012-05-25 02:17:09 +0000548 case Stmt::BinaryOperatorClass: {
549 const BinaryOperator *BO = cast<BinaryOperator>(Term);
550 if (!BO->isLogicalOp())
551 continue;
552 DiagKind = 0;
553 Str = BO->getOpcodeStr();
554 Range = BO->getLHS()->getSourceRange();
Richard Smithbdb97ff2012-05-26 06:20:46 +0000555 RemoveDiagKind = 0;
556 if ((BO->getOpcode() == BO_LAnd && I->Output) ||
557 (BO->getOpcode() == BO_LOr && !I->Output))
558 // true && y -> y, false || y -> y.
559 Fixit1 = FixItHint::CreateRemoval(SourceRange(BO->getLocStart(),
560 BO->getOperatorLoc()));
561 else
562 // false && y -> false, true || y -> true.
563 Fixit1 = FixItHint::CreateReplacement(BO->getSourceRange(), FixitStr);
Richard Smith2815e1a2012-05-25 02:17:09 +0000564 break;
565 }
566
567 // "loop is entered / loop is exited".
568 case Stmt::WhileStmtClass:
569 DiagKind = 1;
570 Str = "while";
571 Range = cast<WhileStmt>(Term)->getCond()->getSourceRange();
Richard Smithbdb97ff2012-05-26 06:20:46 +0000572 RemoveDiagKind = 1;
573 Fixit1 = FixItHint::CreateReplacement(Range, FixitStr);
Richard Smith2815e1a2012-05-25 02:17:09 +0000574 break;
575 case Stmt::ForStmtClass:
576 DiagKind = 1;
577 Str = "for";
578 Range = cast<ForStmt>(Term)->getCond()->getSourceRange();
Richard Smithbdb97ff2012-05-26 06:20:46 +0000579 RemoveDiagKind = 1;
580 if (I->Output)
581 Fixit1 = FixItHint::CreateRemoval(Range);
582 else
583 Fixit1 = FixItHint::CreateReplacement(Range, FixitStr);
Richard Smith2815e1a2012-05-25 02:17:09 +0000584 break;
585
586 // "condition is true / loop is exited".
587 case Stmt::DoStmtClass:
588 DiagKind = 2;
589 Str = "do";
590 Range = cast<DoStmt>(Term)->getCond()->getSourceRange();
Richard Smithbdb97ff2012-05-26 06:20:46 +0000591 RemoveDiagKind = 1;
592 Fixit1 = FixItHint::CreateReplacement(Range, FixitStr);
Richard Smith2815e1a2012-05-25 02:17:09 +0000593 break;
594
595 // "switch case is taken".
596 case Stmt::CaseStmtClass:
597 DiagKind = 3;
598 Str = "case";
599 Range = cast<CaseStmt>(Term)->getLHS()->getSourceRange();
600 break;
601 case Stmt::DefaultStmtClass:
602 DiagKind = 3;
603 Str = "default";
604 Range = cast<DefaultStmt>(Term)->getDefaultLoc();
605 break;
606 }
607
Richard Smithbdb97ff2012-05-26 06:20:46 +0000608 S.Diag(Range.getBegin(), diag::warn_sometimes_uninit_var)
609 << VD->getDeclName() << IsCapturedByBlock << DiagKind
610 << Str << I->Output << Range;
611 S.Diag(User->getLocStart(), diag::note_uninit_var_use)
612 << IsCapturedByBlock << User->getSourceRange();
613 if (RemoveDiagKind != -1)
614 S.Diag(Fixit1.RemoveRange.getBegin(), diag::note_uninit_fixit_remove_cond)
615 << RemoveDiagKind << Str << I->Output << Fixit1 << Fixit2;
616
617 Diagnosed = true;
Richard Smith2815e1a2012-05-25 02:17:09 +0000618 }
Richard Smithbdb97ff2012-05-26 06:20:46 +0000619
620 if (!Diagnosed)
621 S.Diag(Use.getUser()->getLocStart(),
622 Use.getKind() == UninitUse::Always ? diag::warn_uninit_var
623 : diag::warn_maybe_uninit_var)
624 << VD->getDeclName() << IsCapturedByBlock
625 << Use.getUser()->getSourceRange();
Richard Smith2815e1a2012-05-25 02:17:09 +0000626}
627
Chandler Carruth262d50e2011-04-05 18:27:05 +0000628/// DiagnoseUninitializedUse -- Helper function for diagnosing uses of an
629/// uninitialized variable. This manages the different forms of diagnostic
630/// emitted for particular types of uses. Returns true if the use was diagnosed
Richard Smith2815e1a2012-05-25 02:17:09 +0000631/// as a warning. If a particular use is one we omit warnings for, returns
Chandler Carruth262d50e2011-04-05 18:27:05 +0000632/// false.
633static bool DiagnoseUninitializedUse(Sema &S, const VarDecl *VD,
Richard Smith2815e1a2012-05-25 02:17:09 +0000634 const UninitUse &Use,
Ted Kremenek9e761722011-10-13 18:50:06 +0000635 bool alwaysReportSelfInit = false) {
Chandler Carruth4c4983b2011-04-05 18:18:05 +0000636
Richard Smith2815e1a2012-05-25 02:17:09 +0000637 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Use.getUser())) {
Richard Trieuf6278e52012-05-09 21:08:22 +0000638 // Inspect the initializer of the variable declaration which is
639 // being referenced prior to its initialization. We emit
640 // specialized diagnostics for self-initialization, and we
641 // specifically avoid warning about self references which take the
642 // form of:
643 //
644 // int x = x;
645 //
646 // This is used to indicate to GCC that 'x' is intentionally left
647 // uninitialized. Proven code paths which access 'x' in
648 // an uninitialized state after this will still warn.
649 if (const Expr *Initializer = VD->getInit()) {
650 if (!alwaysReportSelfInit && DRE == Initializer->IgnoreParenImpCasts())
651 return false;
Chandler Carruth4c4983b2011-04-05 18:18:05 +0000652
Richard Trieuf6278e52012-05-09 21:08:22 +0000653 ContainsReference CR(S.Context, DRE);
654 CR.Visit(const_cast<Expr*>(Initializer));
655 if (CR.doesContainReference()) {
Chandler Carruth4c4983b2011-04-05 18:18:05 +0000656 S.Diag(DRE->getLocStart(),
657 diag::warn_uninit_self_reference_in_init)
Richard Trieuf6278e52012-05-09 21:08:22 +0000658 << VD->getDeclName() << VD->getLocation() << DRE->getSourceRange();
659 return true;
Chandler Carruth4c4983b2011-04-05 18:18:05 +0000660 }
Chandler Carruth4c4983b2011-04-05 18:18:05 +0000661 }
Richard Trieuf6278e52012-05-09 21:08:22 +0000662
Richard Smithbdb97ff2012-05-26 06:20:46 +0000663 DiagUninitUse(S, VD, Use, false);
Chandler Carruth4c4983b2011-04-05 18:18:05 +0000664 } else {
Richard Smith2815e1a2012-05-25 02:17:09 +0000665 const BlockExpr *BE = cast<BlockExpr>(Use.getUser());
Richard Smithbdb97ff2012-05-26 06:20:46 +0000666 if (VD->getType()->isBlockPointerType() && !VD->hasAttr<BlocksAttr>())
667 S.Diag(BE->getLocStart(),
668 diag::warn_uninit_byref_blockvar_captured_by_block)
Fariborz Jahaniana34194f2012-03-08 00:22:50 +0000669 << VD->getDeclName();
Richard Smithbdb97ff2012-05-26 06:20:46 +0000670 else
671 DiagUninitUse(S, VD, Use, true);
Chandler Carruth4c4983b2011-04-05 18:18:05 +0000672 }
673
674 // Report where the variable was declared when the use wasn't within
David Blaikie4f4f3492011-09-10 05:35:08 +0000675 // the initializer of that declaration & we didn't already suggest
676 // an initialization fixit.
Richard Trieuf6278e52012-05-09 21:08:22 +0000677 if (!SuggestInitializationFixit(S, VD))
Chandler Carruth4c4983b2011-04-05 18:18:05 +0000678 S.Diag(VD->getLocStart(), diag::note_uninit_var_def)
679 << VD->getDeclName();
680
Chandler Carruth262d50e2011-04-05 18:27:05 +0000681 return true;
Chandler Carruth64fb9592011-04-05 18:18:08 +0000682}
683
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000684namespace {
685 class FallthroughMapper : public RecursiveASTVisitor<FallthroughMapper> {
686 public:
687 FallthroughMapper(Sema &S)
688 : FoundSwitchStatements(false),
689 S(S) {
690 }
691
692 bool foundSwitchStatements() const { return FoundSwitchStatements; }
693
694 void markFallthroughVisited(const AttributedStmt *Stmt) {
695 bool Found = FallthroughStmts.erase(Stmt);
696 assert(Found);
Kaelyn Uhrain3bb29942012-05-03 19:46:38 +0000697 (void)Found;
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000698 }
699
700 typedef llvm::SmallPtrSet<const AttributedStmt*, 8> AttrStmts;
701
702 const AttrStmts &getFallthroughStmts() const {
703 return FallthroughStmts;
704 }
705
Alexander Kornienko4874a812013-01-30 03:49:44 +0000706 void fillReachableBlocks(CFG *Cfg) {
707 assert(ReachableBlocks.empty() && "ReachableBlocks already filled");
708 std::deque<const CFGBlock *> BlockQueue;
709
710 ReachableBlocks.insert(&Cfg->getEntry());
711 BlockQueue.push_back(&Cfg->getEntry());
Alexander Kornienko878d0ad2013-02-07 02:17:19 +0000712 // Mark all case blocks reachable to avoid problems with switching on
713 // constants, covered enums, etc.
714 // These blocks can contain fall-through annotations, and we don't want to
715 // issue a warn_fallthrough_attr_unreachable for them.
716 for (CFG::iterator I = Cfg->begin(), E = Cfg->end(); I != E; ++I) {
717 const CFGBlock *B = *I;
718 const Stmt *L = B->getLabel();
719 if (L && isa<SwitchCase>(L) && ReachableBlocks.insert(B))
720 BlockQueue.push_back(B);
721 }
722
Alexander Kornienko4874a812013-01-30 03:49:44 +0000723 while (!BlockQueue.empty()) {
724 const CFGBlock *P = BlockQueue.front();
725 BlockQueue.pop_front();
726 for (CFGBlock::const_succ_iterator I = P->succ_begin(),
727 E = P->succ_end();
728 I != E; ++I) {
Alexander Kornienko0162b832013-02-01 15:39:20 +0000729 if (*I && ReachableBlocks.insert(*I))
Alexander Kornienko4874a812013-01-30 03:49:44 +0000730 BlockQueue.push_back(*I);
731 }
732 }
733 }
734
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000735 bool checkFallThroughIntoBlock(const CFGBlock &B, int &AnnotatedCnt) {
Alexander Kornienko4874a812013-01-30 03:49:44 +0000736 assert(!ReachableBlocks.empty() && "ReachableBlocks empty");
737
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000738 int UnannotatedCnt = 0;
739 AnnotatedCnt = 0;
740
741 std::deque<const CFGBlock*> BlockQueue;
742
743 std::copy(B.pred_begin(), B.pred_end(), std::back_inserter(BlockQueue));
744
745 while (!BlockQueue.empty()) {
746 const CFGBlock *P = BlockQueue.front();
747 BlockQueue.pop_front();
748
749 const Stmt *Term = P->getTerminator();
750 if (Term && isa<SwitchStmt>(Term))
751 continue; // Switch statement, good.
752
753 const SwitchCase *SW = dyn_cast_or_null<SwitchCase>(P->getLabel());
754 if (SW && SW->getSubStmt() == B.getLabel() && P->begin() == P->end())
755 continue; // Previous case label has no statements, good.
756
Alexander Kornienkoc6dcea92013-01-25 20:44:56 +0000757 const LabelStmt *L = dyn_cast_or_null<LabelStmt>(P->getLabel());
758 if (L && L->getSubStmt() == B.getLabel() && P->begin() == P->end())
759 continue; // Case label is preceded with a normal label, good.
760
Alexander Kornienko4874a812013-01-30 03:49:44 +0000761 if (!ReachableBlocks.count(P)) {
Alexander Kornienko878d0ad2013-02-07 02:17:19 +0000762 for (CFGBlock::const_reverse_iterator ElemIt = P->rbegin(),
763 ElemEnd = P->rend();
764 ElemIt != ElemEnd; ++ElemIt) {
David Blaikiefdf6a272013-02-21 20:58:29 +0000765 if (CFGStmt CS = ElemIt->getAs<CFGStmt>()) {
766 if (const AttributedStmt *AS = asFallThroughAttr(CS.getStmt())) {
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000767 S.Diag(AS->getLocStart(),
768 diag::warn_fallthrough_attr_unreachable);
769 markFallthroughVisited(AS);
770 ++AnnotatedCnt;
Alexander Kornienko878d0ad2013-02-07 02:17:19 +0000771 break;
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000772 }
773 // Don't care about other unreachable statements.
774 }
775 }
776 // If there are no unreachable statements, this may be a special
777 // case in CFG:
778 // case X: {
779 // A a; // A has a destructor.
780 // break;
781 // }
782 // // <<<< This place is represented by a 'hanging' CFG block.
783 // case Y:
784 continue;
785 }
786
787 const Stmt *LastStmt = getLastStmt(*P);
788 if (const AttributedStmt *AS = asFallThroughAttr(LastStmt)) {
789 markFallthroughVisited(AS);
790 ++AnnotatedCnt;
791 continue; // Fallthrough annotation, good.
792 }
793
794 if (!LastStmt) { // This block contains no executable statements.
795 // Traverse its predecessors.
796 std::copy(P->pred_begin(), P->pred_end(),
797 std::back_inserter(BlockQueue));
798 continue;
799 }
800
801 ++UnannotatedCnt;
802 }
803 return !!UnannotatedCnt;
804 }
805
806 // RecursiveASTVisitor setup.
807 bool shouldWalkTypesOfTypeLocs() const { return false; }
808
809 bool VisitAttributedStmt(AttributedStmt *S) {
810 if (asFallThroughAttr(S))
811 FallthroughStmts.insert(S);
812 return true;
813 }
814
815 bool VisitSwitchStmt(SwitchStmt *S) {
816 FoundSwitchStatements = true;
817 return true;
818 }
819
820 private:
821
822 static const AttributedStmt *asFallThroughAttr(const Stmt *S) {
823 if (const AttributedStmt *AS = dyn_cast_or_null<AttributedStmt>(S)) {
824 if (hasSpecificAttr<FallThroughAttr>(AS->getAttrs()))
825 return AS;
826 }
827 return 0;
828 }
829
830 static const Stmt *getLastStmt(const CFGBlock &B) {
831 if (const Stmt *Term = B.getTerminator())
832 return Term;
833 for (CFGBlock::const_reverse_iterator ElemIt = B.rbegin(),
834 ElemEnd = B.rend();
835 ElemIt != ElemEnd; ++ElemIt) {
David Blaikiefdf6a272013-02-21 20:58:29 +0000836 if (CFGStmt CS = ElemIt->getAs<CFGStmt>())
837 return CS.getStmt();
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000838 }
839 // Workaround to detect a statement thrown out by CFGBuilder:
840 // case X: {} case Y:
841 // case X: ; case Y:
842 if (const SwitchCase *SW = dyn_cast_or_null<SwitchCase>(B.getLabel()))
843 if (!isa<SwitchCase>(SW->getSubStmt()))
844 return SW->getSubStmt();
845
846 return 0;
847 }
848
849 bool FoundSwitchStatements;
850 AttrStmts FallthroughStmts;
851 Sema &S;
Alexander Kornienko4874a812013-01-30 03:49:44 +0000852 llvm::SmallPtrSet<const CFGBlock *, 16> ReachableBlocks;
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000853 };
854}
855
Alexander Kornienko19736342012-06-02 01:01:07 +0000856static void DiagnoseSwitchLabelsFallthrough(Sema &S, AnalysisDeclContext &AC,
Sean Huntc2f51cf2012-06-15 21:22:05 +0000857 bool PerFunction) {
Ted Kremenek30783532012-11-12 21:20:48 +0000858 // Only perform this analysis when using C++11. There is no good workflow
859 // for this warning when not using C++11. There is no good way to silence
860 // the warning (no attribute is available) unless we are using C++11's support
861 // for generalized attributes. Once could use pragmas to silence the warning,
862 // but as a general solution that is gross and not in the spirit of this
863 // warning.
864 //
865 // NOTE: This an intermediate solution. There are on-going discussions on
866 // how to properly support this warning outside of C++11 with an annotation.
Richard Smith80ad52f2013-01-02 11:42:31 +0000867 if (!AC.getASTContext().getLangOpts().CPlusPlus11)
Ted Kremenek30783532012-11-12 21:20:48 +0000868 return;
869
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000870 FallthroughMapper FM(S);
871 FM.TraverseStmt(AC.getBody());
872
873 if (!FM.foundSwitchStatements())
874 return;
875
Sean Huntc2f51cf2012-06-15 21:22:05 +0000876 if (PerFunction && FM.getFallthroughStmts().empty())
Alexander Kornienko19736342012-06-02 01:01:07 +0000877 return;
878
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000879 CFG *Cfg = AC.getCFG();
880
881 if (!Cfg)
882 return;
883
Alexander Kornienko4874a812013-01-30 03:49:44 +0000884 FM.fillReachableBlocks(Cfg);
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000885
886 for (CFG::reverse_iterator I = Cfg->rbegin(), E = Cfg->rend(); I != E; ++I) {
Alexander Kornienkoe992ed12013-01-25 15:49:34 +0000887 const CFGBlock *B = *I;
888 const Stmt *Label = B->getLabel();
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000889
890 if (!Label || !isa<SwitchCase>(Label))
891 continue;
892
Alexander Kornienko4874a812013-01-30 03:49:44 +0000893 int AnnotatedCnt;
894
Alexander Kornienkoe992ed12013-01-25 15:49:34 +0000895 if (!FM.checkFallThroughIntoBlock(*B, AnnotatedCnt))
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000896 continue;
897
Alexander Kornienko19736342012-06-02 01:01:07 +0000898 S.Diag(Label->getLocStart(),
Sean Huntc2f51cf2012-06-15 21:22:05 +0000899 PerFunction ? diag::warn_unannotated_fallthrough_per_function
900 : diag::warn_unannotated_fallthrough);
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000901
902 if (!AnnotatedCnt) {
903 SourceLocation L = Label->getLocStart();
904 if (L.isMacroID())
905 continue;
Richard Smith80ad52f2013-01-02 11:42:31 +0000906 if (S.getLangOpts().CPlusPlus11) {
Alexander Kornienkoe992ed12013-01-25 15:49:34 +0000907 const Stmt *Term = B->getTerminator();
908 // Skip empty cases.
909 while (B->empty() && !Term && B->succ_size() == 1) {
910 B = *B->succ_begin();
911 Term = B->getTerminator();
912 }
913 if (!(B->empty() && Term && isa<BreakStmt>(Term))) {
Alexander Kornienko66da0ab2012-09-28 22:24:03 +0000914 Preprocessor &PP = S.getPreprocessor();
915 TokenValue Tokens[] = {
916 tok::l_square, tok::l_square, PP.getIdentifierInfo("clang"),
917 tok::coloncolon, PP.getIdentifierInfo("fallthrough"),
918 tok::r_square, tok::r_square
919 };
Dmitri Gribenko19523542012-09-29 11:40:46 +0000920 StringRef AnnotationSpelling = "[[clang::fallthrough]]";
921 StringRef MacroName = PP.getLastMacroWithSpelling(L, Tokens);
922 if (!MacroName.empty())
923 AnnotationSpelling = MacroName;
924 SmallString<64> TextToInsert(AnnotationSpelling);
925 TextToInsert += "; ";
Alexander Kornienkoa189d892012-05-26 00:49:15 +0000926 S.Diag(L, diag::note_insert_fallthrough_fixit) <<
Alexander Kornienko66da0ab2012-09-28 22:24:03 +0000927 AnnotationSpelling <<
Dmitri Gribenko19523542012-09-29 11:40:46 +0000928 FixItHint::CreateInsertion(L, TextToInsert);
Alexander Kornienkoa189d892012-05-26 00:49:15 +0000929 }
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000930 }
931 S.Diag(L, diag::note_insert_break_fixit) <<
932 FixItHint::CreateInsertion(L, "break; ");
933 }
934 }
935
936 const FallthroughMapper::AttrStmts &Fallthroughs = FM.getFallthroughStmts();
937 for (FallthroughMapper::AttrStmts::const_iterator I = Fallthroughs.begin(),
938 E = Fallthroughs.end();
939 I != E; ++I) {
940 S.Diag((*I)->getLocStart(), diag::warn_fallthrough_attr_invalid_placement);
941 }
942
943}
944
Ted Kremenek610068c2011-01-15 02:58:47 +0000945namespace {
Jordan Rose20441c52012-09-28 22:29:02 +0000946typedef std::pair<const Stmt *,
947 sema::FunctionScopeInfo::WeakObjectUseMap::const_iterator>
948 StmtUsesPair;
Jordan Rose58b6bdc2012-09-28 22:21:30 +0000949
Jordan Rose20441c52012-09-28 22:29:02 +0000950class StmtUseSorter {
Jordan Rose58b6bdc2012-09-28 22:21:30 +0000951 const SourceManager &SM;
952
953public:
Jordan Rose20441c52012-09-28 22:29:02 +0000954 explicit StmtUseSorter(const SourceManager &SM) : SM(SM) { }
Jordan Rose58b6bdc2012-09-28 22:21:30 +0000955
956 bool operator()(const StmtUsesPair &LHS, const StmtUsesPair &RHS) {
957 return SM.isBeforeInTranslationUnit(LHS.first->getLocStart(),
958 RHS.first->getLocStart());
959 }
960};
Jordan Rose20441c52012-09-28 22:29:02 +0000961}
Jordan Rose58b6bdc2012-09-28 22:21:30 +0000962
Jordan Rosec0e44452012-10-29 17:46:47 +0000963static bool isInLoop(const ASTContext &Ctx, const ParentMap &PM,
964 const Stmt *S) {
Jordan Roseb5cd1222012-10-11 16:10:19 +0000965 assert(S);
966
967 do {
968 switch (S->getStmtClass()) {
Jordan Roseb5cd1222012-10-11 16:10:19 +0000969 case Stmt::ForStmtClass:
970 case Stmt::WhileStmtClass:
971 case Stmt::CXXForRangeStmtClass:
972 case Stmt::ObjCForCollectionStmtClass:
973 return true;
Jordan Rosec0e44452012-10-29 17:46:47 +0000974 case Stmt::DoStmtClass: {
975 const Expr *Cond = cast<DoStmt>(S)->getCond();
976 llvm::APSInt Val;
977 if (!Cond->EvaluateAsInt(Val, Ctx))
978 return true;
979 return Val.getBoolValue();
980 }
Jordan Roseb5cd1222012-10-11 16:10:19 +0000981 default:
982 break;
983 }
984 } while ((S = PM.getParent(S)));
985
986 return false;
987}
988
Jordan Rose58b6bdc2012-09-28 22:21:30 +0000989
990static void diagnoseRepeatedUseOfWeak(Sema &S,
991 const sema::FunctionScopeInfo *CurFn,
Jordan Roseb5cd1222012-10-11 16:10:19 +0000992 const Decl *D,
993 const ParentMap &PM) {
Jordan Rose58b6bdc2012-09-28 22:21:30 +0000994 typedef sema::FunctionScopeInfo::WeakObjectProfileTy WeakObjectProfileTy;
995 typedef sema::FunctionScopeInfo::WeakObjectUseMap WeakObjectUseMap;
996 typedef sema::FunctionScopeInfo::WeakUseVector WeakUseVector;
997
Jordan Rosec0e44452012-10-29 17:46:47 +0000998 ASTContext &Ctx = S.getASTContext();
999
Jordan Rose58b6bdc2012-09-28 22:21:30 +00001000 const WeakObjectUseMap &WeakMap = CurFn->getWeakObjectUses();
1001
1002 // Extract all weak objects that are referenced more than once.
1003 SmallVector<StmtUsesPair, 8> UsesByStmt;
1004 for (WeakObjectUseMap::const_iterator I = WeakMap.begin(), E = WeakMap.end();
1005 I != E; ++I) {
1006 const WeakUseVector &Uses = I->second;
Jordan Rose58b6bdc2012-09-28 22:21:30 +00001007
1008 // Find the first read of the weak object.
1009 WeakUseVector::const_iterator UI = Uses.begin(), UE = Uses.end();
1010 for ( ; UI != UE; ++UI) {
1011 if (UI->isUnsafe())
1012 break;
1013 }
1014
1015 // If there were only writes to this object, don't warn.
1016 if (UI == UE)
1017 continue;
1018
Jordan Roseb5cd1222012-10-11 16:10:19 +00001019 // If there was only one read, followed by any number of writes, and the
Jordan Rosec0e44452012-10-29 17:46:47 +00001020 // read is not within a loop, don't warn. Additionally, don't warn in a
1021 // loop if the base object is a local variable -- local variables are often
1022 // changed in loops.
Jordan Roseb5cd1222012-10-11 16:10:19 +00001023 if (UI == Uses.begin()) {
1024 WeakUseVector::const_iterator UI2 = UI;
1025 for (++UI2; UI2 != UE; ++UI2)
1026 if (UI2->isUnsafe())
1027 break;
1028
Jordan Rosec0e44452012-10-29 17:46:47 +00001029 if (UI2 == UE) {
1030 if (!isInLoop(Ctx, PM, UI->getUseExpr()))
Jordan Roseb5cd1222012-10-11 16:10:19 +00001031 continue;
Jordan Rosec0e44452012-10-29 17:46:47 +00001032
1033 const WeakObjectProfileTy &Profile = I->first;
1034 if (!Profile.isExactProfile())
1035 continue;
1036
1037 const NamedDecl *Base = Profile.getBase();
1038 if (!Base)
1039 Base = Profile.getProperty();
1040 assert(Base && "A profile always has a base or property.");
1041
1042 if (const VarDecl *BaseVar = dyn_cast<VarDecl>(Base))
1043 if (BaseVar->hasLocalStorage() && !isa<ParmVarDecl>(Base))
1044 continue;
1045 }
Jordan Roseb5cd1222012-10-11 16:10:19 +00001046 }
1047
Jordan Rose58b6bdc2012-09-28 22:21:30 +00001048 UsesByStmt.push_back(StmtUsesPair(UI->getUseExpr(), I));
1049 }
1050
1051 if (UsesByStmt.empty())
1052 return;
1053
1054 // Sort by first use so that we emit the warnings in a deterministic order.
1055 std::sort(UsesByStmt.begin(), UsesByStmt.end(),
Jordan Rose20441c52012-09-28 22:29:02 +00001056 StmtUseSorter(S.getSourceManager()));
Jordan Rose58b6bdc2012-09-28 22:21:30 +00001057
1058 // Classify the current code body for better warning text.
1059 // This enum should stay in sync with the cases in
1060 // warn_arc_repeated_use_of_weak and warn_arc_possible_repeated_use_of_weak.
1061 // FIXME: Should we use a common classification enum and the same set of
1062 // possibilities all throughout Sema?
1063 enum {
1064 Function,
1065 Method,
1066 Block,
1067 Lambda
1068 } FunctionKind;
1069
1070 if (isa<sema::BlockScopeInfo>(CurFn))
1071 FunctionKind = Block;
1072 else if (isa<sema::LambdaScopeInfo>(CurFn))
1073 FunctionKind = Lambda;
1074 else if (isa<ObjCMethodDecl>(D))
1075 FunctionKind = Method;
1076 else
1077 FunctionKind = Function;
1078
1079 // Iterate through the sorted problems and emit warnings for each.
1080 for (SmallVectorImpl<StmtUsesPair>::const_iterator I = UsesByStmt.begin(),
1081 E = UsesByStmt.end();
1082 I != E; ++I) {
1083 const Stmt *FirstRead = I->first;
1084 const WeakObjectProfileTy &Key = I->second->first;
1085 const WeakUseVector &Uses = I->second->second;
1086
Jordan Rose7a270482012-09-28 22:21:35 +00001087 // For complicated expressions like 'a.b.c' and 'x.b.c', WeakObjectProfileTy
1088 // may not contain enough information to determine that these are different
1089 // properties. We can only be 100% sure of a repeated use in certain cases,
1090 // and we adjust the diagnostic kind accordingly so that the less certain
1091 // case can be turned off if it is too noisy.
Jordan Rose58b6bdc2012-09-28 22:21:30 +00001092 unsigned DiagKind;
1093 if (Key.isExactProfile())
1094 DiagKind = diag::warn_arc_repeated_use_of_weak;
1095 else
1096 DiagKind = diag::warn_arc_possible_repeated_use_of_weak;
1097
Jordan Rose7a270482012-09-28 22:21:35 +00001098 // Classify the weak object being accessed for better warning text.
1099 // This enum should stay in sync with the cases in
1100 // warn_arc_repeated_use_of_weak and warn_arc_possible_repeated_use_of_weak.
1101 enum {
1102 Variable,
1103 Property,
1104 ImplicitProperty,
1105 Ivar
1106 } ObjectKind;
1107
1108 const NamedDecl *D = Key.getProperty();
1109 if (isa<VarDecl>(D))
1110 ObjectKind = Variable;
1111 else if (isa<ObjCPropertyDecl>(D))
1112 ObjectKind = Property;
1113 else if (isa<ObjCMethodDecl>(D))
1114 ObjectKind = ImplicitProperty;
1115 else if (isa<ObjCIvarDecl>(D))
1116 ObjectKind = Ivar;
1117 else
1118 llvm_unreachable("Unexpected weak object kind!");
1119
Jordan Rose58b6bdc2012-09-28 22:21:30 +00001120 // Show the first time the object was read.
1121 S.Diag(FirstRead->getLocStart(), DiagKind)
Jordan Rose7a270482012-09-28 22:21:35 +00001122 << ObjectKind << D << FunctionKind
Jordan Rose58b6bdc2012-09-28 22:21:30 +00001123 << FirstRead->getSourceRange();
1124
1125 // Print all the other accesses as notes.
1126 for (WeakUseVector::const_iterator UI = Uses.begin(), UE = Uses.end();
1127 UI != UE; ++UI) {
1128 if (UI->getUseExpr() == FirstRead)
1129 continue;
1130 S.Diag(UI->getUseExpr()->getLocStart(),
1131 diag::note_arc_weak_also_accessed_here)
1132 << UI->getUseExpr()->getSourceRange();
1133 }
1134 }
1135}
1136
1137
1138namespace {
Ted Kremenek94b1b4d2011-01-21 19:41:41 +00001139struct SLocSort {
Ted Kremenekf7bafc72011-03-15 04:57:38 +00001140 bool operator()(const UninitUse &a, const UninitUse &b) {
Richard Smith2815e1a2012-05-25 02:17:09 +00001141 // Prefer a more confident report over a less confident one.
1142 if (a.getKind() != b.getKind())
1143 return a.getKind() > b.getKind();
1144 SourceLocation aLoc = a.getUser()->getLocStart();
1145 SourceLocation bLoc = b.getUser()->getLocStart();
Ted Kremenek94b1b4d2011-01-21 19:41:41 +00001146 return aLoc.getRawEncoding() < bLoc.getRawEncoding();
1147 }
1148};
1149
Ted Kremenek610068c2011-01-15 02:58:47 +00001150class UninitValsDiagReporter : public UninitVariablesHandler {
1151 Sema &S;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001152 typedef SmallVector<UninitUse, 2> UsesVec;
Enea Zaffanella3285c782013-02-15 20:09:55 +00001153 typedef std::pair<UsesVec*, bool> MappedType;
1154 // Prefer using MapVector to DenseMap, so that iteration order will be
1155 // the same as insertion order. This is needed to obtain a deterministic
1156 // order of diagnostics when calling flushDiagnostics().
1157 typedef llvm::MapVector<const VarDecl *, MappedType> UsesMap;
Ted Kremenek94b1b4d2011-01-21 19:41:41 +00001158 UsesMap *uses;
1159
Ted Kremenek610068c2011-01-15 02:58:47 +00001160public:
Ted Kremenek94b1b4d2011-01-21 19:41:41 +00001161 UninitValsDiagReporter(Sema &S) : S(S), uses(0) {}
1162 ~UninitValsDiagReporter() {
1163 flushDiagnostics();
1164 }
Ted Kremenek9e761722011-10-13 18:50:06 +00001165
Enea Zaffanella3285c782013-02-15 20:09:55 +00001166 MappedType &getUses(const VarDecl *vd) {
Ted Kremenek94b1b4d2011-01-21 19:41:41 +00001167 if (!uses)
1168 uses = new UsesMap();
Ted Kremenek9e761722011-10-13 18:50:06 +00001169
Enea Zaffanella3285c782013-02-15 20:09:55 +00001170 MappedType &V = (*uses)[vd];
Ted Kremenek9e761722011-10-13 18:50:06 +00001171 UsesVec *&vec = V.first;
Ted Kremenek94b1b4d2011-01-21 19:41:41 +00001172 if (!vec)
1173 vec = new UsesVec();
1174
Ted Kremenek9e761722011-10-13 18:50:06 +00001175 return V;
1176 }
1177
Richard Smith2815e1a2012-05-25 02:17:09 +00001178 void handleUseOfUninitVariable(const VarDecl *vd, const UninitUse &use) {
1179 getUses(vd).first->push_back(use);
Ted Kremenek9e761722011-10-13 18:50:06 +00001180 }
1181
1182 void handleSelfInit(const VarDecl *vd) {
1183 getUses(vd).second = true;
Ted Kremenek94b1b4d2011-01-21 19:41:41 +00001184 }
1185
1186 void flushDiagnostics() {
1187 if (!uses)
1188 return;
Enea Zaffanella3285c782013-02-15 20:09:55 +00001189
Ted Kremenek94b1b4d2011-01-21 19:41:41 +00001190 for (UsesMap::iterator i = uses->begin(), e = uses->end(); i != e; ++i) {
1191 const VarDecl *vd = i->first;
Enea Zaffanella3285c782013-02-15 20:09:55 +00001192 const MappedType &V = i->second;
Ted Kremenek609e3172011-02-02 23:35:53 +00001193
Ted Kremenek9e761722011-10-13 18:50:06 +00001194 UsesVec *vec = V.first;
1195 bool hasSelfInit = V.second;
1196
1197 // Specially handle the case where we have uses of an uninitialized
1198 // variable, but the root cause is an idiomatic self-init. We want
1199 // to report the diagnostic at the self-init since that is the root cause.
Matt Beaumont-Gay0d381812011-10-19 18:53:03 +00001200 if (!vec->empty() && hasSelfInit && hasAlwaysUninitializedUse(vec))
Richard Smith2815e1a2012-05-25 02:17:09 +00001201 DiagnoseUninitializedUse(S, vd,
1202 UninitUse(vd->getInit()->IgnoreParenCasts(),
1203 /* isAlwaysUninit */ true),
Matt Beaumont-Gay0d381812011-10-19 18:53:03 +00001204 /* alwaysReportSelfInit */ true);
Ted Kremenek9e761722011-10-13 18:50:06 +00001205 else {
1206 // Sort the uses by their SourceLocations. While not strictly
1207 // guaranteed to produce them in line/column order, this will provide
1208 // a stable ordering.
1209 std::sort(vec->begin(), vec->end(), SLocSort());
1210
1211 for (UsesVec::iterator vi = vec->begin(), ve = vec->end(); vi != ve;
1212 ++vi) {
Richard Smith2815e1a2012-05-25 02:17:09 +00001213 // If we have self-init, downgrade all uses to 'may be uninitialized'.
1214 UninitUse Use = hasSelfInit ? UninitUse(vi->getUser(), false) : *vi;
1215
1216 if (DiagnoseUninitializedUse(S, vd, Use))
Ted Kremenek9e761722011-10-13 18:50:06 +00001217 // Skip further diagnostics for this variable. We try to warn only
1218 // on the first point at which a variable is used uninitialized.
1219 break;
1220 }
Chandler Carruth64fb9592011-04-05 18:18:08 +00001221 }
Ted Kremenek9e761722011-10-13 18:50:06 +00001222
1223 // Release the uses vector.
Ted Kremenek94b1b4d2011-01-21 19:41:41 +00001224 delete vec;
1225 }
1226 delete uses;
Ted Kremenek610068c2011-01-15 02:58:47 +00001227 }
Matt Beaumont-Gay0d381812011-10-19 18:53:03 +00001228
1229private:
1230 static bool hasAlwaysUninitializedUse(const UsesVec* vec) {
1231 for (UsesVec::const_iterator i = vec->begin(), e = vec->end(); i != e; ++i) {
Richard Smith2815e1a2012-05-25 02:17:09 +00001232 if (i->getKind() == UninitUse::Always) {
Matt Beaumont-Gay0d381812011-10-19 18:53:03 +00001233 return true;
1234 }
1235 }
1236 return false;
1237}
Ted Kremenek610068c2011-01-15 02:58:47 +00001238};
1239}
1240
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +00001241
1242//===----------------------------------------------------------------------===//
1243// -Wthread-safety
1244//===----------------------------------------------------------------------===//
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001245namespace clang {
1246namespace thread_safety {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001247typedef SmallVector<PartialDiagnosticAt, 1> OptionalNotes;
Richard Smith2e515622012-02-03 04:45:26 +00001248typedef std::pair<PartialDiagnosticAt, OptionalNotes> DelayedDiag;
Benjamin Kramerecafd302012-03-26 14:05:40 +00001249typedef std::list<DelayedDiag> DiagList;
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001250
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001251struct SortDiagBySourceLocation {
Benjamin Kramerecafd302012-03-26 14:05:40 +00001252 SourceManager &SM;
1253 SortDiagBySourceLocation(SourceManager &SM) : SM(SM) {}
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001254
1255 bool operator()(const DelayedDiag &left, const DelayedDiag &right) {
1256 // Although this call will be slow, this is only called when outputting
1257 // multiple warnings.
Benjamin Kramerecafd302012-03-26 14:05:40 +00001258 return SM.isBeforeInTranslationUnit(left.first.first, right.first.first);
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001259 }
1260};
1261
David Blaikie99ba9e32011-12-20 02:48:34 +00001262namespace {
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001263class ThreadSafetyReporter : public clang::thread_safety::ThreadSafetyHandler {
1264 Sema &S;
1265 DiagList Warnings;
Richard Smith2e515622012-02-03 04:45:26 +00001266 SourceLocation FunLocation, FunEndLocation;
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001267
1268 // Helper functions
1269 void warnLockMismatch(unsigned DiagID, Name LockName, SourceLocation Loc) {
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +00001270 // Gracefully handle rare cases when the analysis can't get a more
1271 // precise source location.
1272 if (!Loc.isValid())
1273 Loc = FunLocation;
Richard Smith2e515622012-02-03 04:45:26 +00001274 PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID) << LockName);
1275 Warnings.push_back(DelayedDiag(Warning, OptionalNotes()));
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001276 }
1277
1278 public:
Richard Smith2e515622012-02-03 04:45:26 +00001279 ThreadSafetyReporter(Sema &S, SourceLocation FL, SourceLocation FEL)
1280 : S(S), FunLocation(FL), FunEndLocation(FEL) {}
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001281
1282 /// \brief Emit all buffered diagnostics in order of sourcelocation.
1283 /// We need to output diagnostics produced while iterating through
1284 /// the lockset in deterministic order, so this function orders diagnostics
1285 /// and outputs them.
1286 void emitDiagnostics() {
Benjamin Kramerecafd302012-03-26 14:05:40 +00001287 Warnings.sort(SortDiagBySourceLocation(S.getSourceManager()));
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001288 for (DiagList::iterator I = Warnings.begin(), E = Warnings.end();
Richard Smith2e515622012-02-03 04:45:26 +00001289 I != E; ++I) {
1290 S.Diag(I->first.first, I->first.second);
1291 const OptionalNotes &Notes = I->second;
1292 for (unsigned NoteI = 0, NoteN = Notes.size(); NoteI != NoteN; ++NoteI)
1293 S.Diag(Notes[NoteI].first, Notes[NoteI].second);
1294 }
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001295 }
1296
Caitlin Sadowski99107eb2011-09-09 16:21:55 +00001297 void handleInvalidLockExp(SourceLocation Loc) {
Richard Smith2e515622012-02-03 04:45:26 +00001298 PartialDiagnosticAt Warning(Loc,
1299 S.PDiag(diag::warn_cannot_resolve_lock) << Loc);
1300 Warnings.push_back(DelayedDiag(Warning, OptionalNotes()));
Caitlin Sadowski99107eb2011-09-09 16:21:55 +00001301 }
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001302 void handleUnmatchedUnlock(Name LockName, SourceLocation Loc) {
1303 warnLockMismatch(diag::warn_unlock_but_no_lock, LockName, Loc);
1304 }
1305
1306 void handleDoubleLock(Name LockName, SourceLocation Loc) {
1307 warnLockMismatch(diag::warn_double_lock, LockName, Loc);
1308 }
1309
Richard Smith2e515622012-02-03 04:45:26 +00001310 void handleMutexHeldEndOfScope(Name LockName, SourceLocation LocLocked,
1311 SourceLocation LocEndOfScope,
Caitlin Sadowski4e4bc752011-09-15 17:25:19 +00001312 LockErrorKind LEK){
1313 unsigned DiagID = 0;
1314 switch (LEK) {
1315 case LEK_LockedSomePredecessors:
Richard Smith2e515622012-02-03 04:45:26 +00001316 DiagID = diag::warn_lock_some_predecessors;
Caitlin Sadowski4e4bc752011-09-15 17:25:19 +00001317 break;
1318 case LEK_LockedSomeLoopIterations:
1319 DiagID = diag::warn_expecting_lock_held_on_loop;
1320 break;
1321 case LEK_LockedAtEndOfFunction:
1322 DiagID = diag::warn_no_unlock;
1323 break;
DeLesley Hutchins879a4332012-07-02 22:16:54 +00001324 case LEK_NotLockedAtEndOfFunction:
1325 DiagID = diag::warn_expecting_locked;
1326 break;
Caitlin Sadowski4e4bc752011-09-15 17:25:19 +00001327 }
Richard Smith2e515622012-02-03 04:45:26 +00001328 if (LocEndOfScope.isInvalid())
1329 LocEndOfScope = FunEndLocation;
1330
1331 PartialDiagnosticAt Warning(LocEndOfScope, S.PDiag(DiagID) << LockName);
1332 PartialDiagnosticAt Note(LocLocked, S.PDiag(diag::note_locked_here));
1333 Warnings.push_back(DelayedDiag(Warning, OptionalNotes(1, Note)));
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001334 }
1335
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001336
1337 void handleExclusiveAndShared(Name LockName, SourceLocation Loc1,
1338 SourceLocation Loc2) {
Richard Smith2e515622012-02-03 04:45:26 +00001339 PartialDiagnosticAt Warning(
1340 Loc1, S.PDiag(diag::warn_lock_exclusive_and_shared) << LockName);
1341 PartialDiagnosticAt Note(
1342 Loc2, S.PDiag(diag::note_lock_exclusive_and_shared) << LockName);
1343 Warnings.push_back(DelayedDiag(Warning, OptionalNotes(1, Note)));
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001344 }
1345
1346 void handleNoMutexHeld(const NamedDecl *D, ProtectedOperationKind POK,
1347 AccessKind AK, SourceLocation Loc) {
Caitlin Sadowskidf8327c2011-09-14 20:09:09 +00001348 assert((POK == POK_VarAccess || POK == POK_VarDereference)
1349 && "Only works for variables");
1350 unsigned DiagID = POK == POK_VarAccess?
1351 diag::warn_variable_requires_any_lock:
1352 diag::warn_var_deref_requires_any_lock;
Richard Smith2e515622012-02-03 04:45:26 +00001353 PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID)
DeLesley Hutchins5b280f22012-09-19 19:18:29 +00001354 << D->getNameAsString() << getLockKindFromAccessKind(AK));
Richard Smith2e515622012-02-03 04:45:26 +00001355 Warnings.push_back(DelayedDiag(Warning, OptionalNotes()));
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001356 }
1357
1358 void handleMutexNotHeld(const NamedDecl *D, ProtectedOperationKind POK,
DeLesley Hutchins3f0ec522012-09-10 19:58:23 +00001359 Name LockName, LockKind LK, SourceLocation Loc,
1360 Name *PossibleMatch) {
Caitlin Sadowskie87158d2011-09-13 18:01:58 +00001361 unsigned DiagID = 0;
DeLesley Hutchins3f0ec522012-09-10 19:58:23 +00001362 if (PossibleMatch) {
1363 switch (POK) {
1364 case POK_VarAccess:
1365 DiagID = diag::warn_variable_requires_lock_precise;
1366 break;
1367 case POK_VarDereference:
1368 DiagID = diag::warn_var_deref_requires_lock_precise;
1369 break;
1370 case POK_FunctionCall:
1371 DiagID = diag::warn_fun_requires_lock_precise;
1372 break;
1373 }
1374 PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID)
DeLesley Hutchins5b280f22012-09-19 19:18:29 +00001375 << D->getNameAsString() << LockName << LK);
DeLesley Hutchins3f0ec522012-09-10 19:58:23 +00001376 PartialDiagnosticAt Note(Loc, S.PDiag(diag::note_found_mutex_near_match)
1377 << *PossibleMatch);
1378 Warnings.push_back(DelayedDiag(Warning, OptionalNotes(1, Note)));
1379 } else {
1380 switch (POK) {
1381 case POK_VarAccess:
1382 DiagID = diag::warn_variable_requires_lock;
1383 break;
1384 case POK_VarDereference:
1385 DiagID = diag::warn_var_deref_requires_lock;
1386 break;
1387 case POK_FunctionCall:
1388 DiagID = diag::warn_fun_requires_lock;
1389 break;
1390 }
1391 PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID)
DeLesley Hutchins5b280f22012-09-19 19:18:29 +00001392 << D->getNameAsString() << LockName << LK);
DeLesley Hutchins3f0ec522012-09-10 19:58:23 +00001393 Warnings.push_back(DelayedDiag(Warning, OptionalNotes()));
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001394 }
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001395 }
1396
1397 void handleFunExcludesLock(Name FunName, Name LockName, SourceLocation Loc) {
Richard Smith2e515622012-02-03 04:45:26 +00001398 PartialDiagnosticAt Warning(Loc,
1399 S.PDiag(diag::warn_fun_excludes_mutex) << FunName << LockName);
1400 Warnings.push_back(DelayedDiag(Warning, OptionalNotes()));
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001401 }
1402};
1403}
1404}
David Blaikie99ba9e32011-12-20 02:48:34 +00001405}
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001406
Ted Kremenek610068c2011-01-15 02:58:47 +00001407//===----------------------------------------------------------------------===//
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001408// AnalysisBasedWarnings - Worker object used by Sema to execute analysis-based
1409// warnings on a function, method, or block.
1410//===----------------------------------------------------------------------===//
1411
Ted Kremenekd064fdc2010-03-23 00:13:23 +00001412clang::sema::AnalysisBasedWarnings::Policy::Policy() {
1413 enableCheckFallThrough = 1;
1414 enableCheckUnreachable = 0;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +00001415 enableThreadSafetyAnalysis = 0;
Ted Kremenekd064fdc2010-03-23 00:13:23 +00001416}
1417
Chandler Carruth5d989942011-07-06 16:21:37 +00001418clang::sema::AnalysisBasedWarnings::AnalysisBasedWarnings(Sema &s)
1419 : S(s),
1420 NumFunctionsAnalyzed(0),
Benjamin Kramer54cf3412011-07-08 20:38:53 +00001421 NumFunctionsWithBadCFGs(0),
Chandler Carruth5d989942011-07-06 16:21:37 +00001422 NumCFGBlocks(0),
Benjamin Kramer54cf3412011-07-08 20:38:53 +00001423 MaxCFGBlocksPerFunction(0),
1424 NumUninitAnalysisFunctions(0),
1425 NumUninitAnalysisVariables(0),
1426 MaxUninitAnalysisVariablesPerFunction(0),
1427 NumUninitAnalysisBlockVisits(0),
1428 MaxUninitAnalysisBlockVisitsPerFunction(0) {
David Blaikied6471f72011-09-25 23:23:43 +00001429 DiagnosticsEngine &D = S.getDiagnostics();
Ted Kremenekd064fdc2010-03-23 00:13:23 +00001430 DefaultPolicy.enableCheckUnreachable = (unsigned)
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001431 (D.getDiagnosticLevel(diag::warn_unreachable, SourceLocation()) !=
David Blaikied6471f72011-09-25 23:23:43 +00001432 DiagnosticsEngine::Ignored);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +00001433 DefaultPolicy.enableThreadSafetyAnalysis = (unsigned)
1434 (D.getDiagnosticLevel(diag::warn_double_lock, SourceLocation()) !=
David Blaikied6471f72011-09-25 23:23:43 +00001435 DiagnosticsEngine::Ignored);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +00001436
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001437}
1438
Ted Kremenek351ba912011-02-23 01:52:04 +00001439static void flushDiagnostics(Sema &S, sema::FunctionScopeInfo *fscope) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001440 for (SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator
Ted Kremenek351ba912011-02-23 01:52:04 +00001441 i = fscope->PossiblyUnreachableDiags.begin(),
1442 e = fscope->PossiblyUnreachableDiags.end();
1443 i != e; ++i) {
1444 const sema::PossiblyUnreachableDiag &D = *i;
1445 S.Diag(D.Loc, D.PD);
1446 }
1447}
1448
Ted Kremenekd064fdc2010-03-23 00:13:23 +00001449void clang::sema::
1450AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P,
Ted Kremenek283a3582011-02-23 01:51:53 +00001451 sema::FunctionScopeInfo *fscope,
Ted Kremenek3ed6fc02011-02-23 01:51:48 +00001452 const Decl *D, const BlockExpr *blkExpr) {
Ted Kremenekd068aab2010-03-20 21:11:09 +00001453
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001454 // We avoid doing analysis-based warnings when there are errors for
1455 // two reasons:
1456 // (1) The CFGs often can't be constructed (if the body is invalid), so
1457 // don't bother trying.
1458 // (2) The code already has problems; running the analysis just takes more
1459 // time.
David Blaikied6471f72011-09-25 23:23:43 +00001460 DiagnosticsEngine &Diags = S.getDiagnostics();
Ted Kremenek99e81922010-04-30 21:49:25 +00001461
Ted Kremenekd064fdc2010-03-23 00:13:23 +00001462 // Do not do any analysis for declarations in system headers if we are
1463 // going to just ignore them.
Ted Kremenek99e81922010-04-30 21:49:25 +00001464 if (Diags.getSuppressSystemWarnings() &&
Ted Kremenekd064fdc2010-03-23 00:13:23 +00001465 S.SourceMgr.isInSystemHeader(D->getLocation()))
1466 return;
1467
John McCalle0054f62010-08-25 05:56:39 +00001468 // For code in dependent contexts, we'll do this at instantiation time.
David Blaikie23661d32012-01-24 04:51:48 +00001469 if (cast<DeclContext>(D)->isDependentContext())
1470 return;
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001471
DeLesley Hutchins12f37e42012-12-07 22:53:48 +00001472 if (Diags.hasUncompilableErrorOccurred() || Diags.hasFatalErrorOccurred()) {
Ted Kremenek351ba912011-02-23 01:52:04 +00001473 // Flush out any possibly unreachable diagnostics.
1474 flushDiagnostics(S, fscope);
1475 return;
1476 }
1477
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001478 const Stmt *Body = D->getBody();
1479 assert(Body);
1480
Jordy Rosed2001872012-04-28 01:58:08 +00001481 AnalysisDeclContext AC(/* AnalysisDeclContextManager */ 0, D);
Ted Kremenekbc5cb8a2011-07-21 05:22:47 +00001482
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001483 // Don't generate EH edges for CallExprs as we'd like to avoid the n^2
1484 // explosion for destrutors that can result and the compile time hit.
Ted Kremenekbc5cb8a2011-07-21 05:22:47 +00001485 AC.getCFGBuildOptions().PruneTriviallyFalseEdges = true;
1486 AC.getCFGBuildOptions().AddEHEdges = false;
1487 AC.getCFGBuildOptions().AddInitializers = true;
1488 AC.getCFGBuildOptions().AddImplicitDtors = true;
Jordan Rosefaadf482012-09-05 23:11:06 +00001489 AC.getCFGBuildOptions().AddTemporaryDtors = true;
1490
Ted Kremenek0c8e5a02011-07-19 14:18:48 +00001491 // Force that certain expressions appear as CFGElements in the CFG. This
1492 // is used to speed up various analyses.
1493 // FIXME: This isn't the right factoring. This is here for initial
1494 // prototyping, but we need a way for analyses to say what expressions they
1495 // expect to always be CFGElements and then fill in the BuildOptions
1496 // appropriately. This is essentially a layering violation.
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +00001497 if (P.enableCheckUnreachable || P.enableThreadSafetyAnalysis) {
1498 // Unreachable code analysis and thread safety require a linearized CFG.
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +00001499 AC.getCFGBuildOptions().setAllAlwaysAdd();
1500 }
1501 else {
1502 AC.getCFGBuildOptions()
1503 .setAlwaysAdd(Stmt::BinaryOperatorClass)
Richard Smith6cfa78f2012-07-17 01:27:33 +00001504 .setAlwaysAdd(Stmt::CompoundAssignOperatorClass)
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +00001505 .setAlwaysAdd(Stmt::BlockExprClass)
1506 .setAlwaysAdd(Stmt::CStyleCastExprClass)
1507 .setAlwaysAdd(Stmt::DeclRefExprClass)
1508 .setAlwaysAdd(Stmt::ImplicitCastExprClass)
Richard Smithe0d3b4c2012-05-03 18:27:39 +00001509 .setAlwaysAdd(Stmt::UnaryOperatorClass)
1510 .setAlwaysAdd(Stmt::AttributedStmtClass);
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +00001511 }
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001512
Ted Kremenekbc5cb8a2011-07-21 05:22:47 +00001513 // Construct the analysis context with the specified CFG build options.
1514
Ted Kremenek351ba912011-02-23 01:52:04 +00001515 // Emit delayed diagnostics.
David Blaikie23661d32012-01-24 04:51:48 +00001516 if (!fscope->PossiblyUnreachableDiags.empty()) {
Ted Kremenek351ba912011-02-23 01:52:04 +00001517 bool analyzed = false;
Ted Kremenek0d28d362011-03-10 03:50:34 +00001518
1519 // Register the expressions with the CFGBuilder.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001520 for (SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator
Ted Kremenek0d28d362011-03-10 03:50:34 +00001521 i = fscope->PossiblyUnreachableDiags.begin(),
1522 e = fscope->PossiblyUnreachableDiags.end();
1523 i != e; ++i) {
1524 if (const Stmt *stmt = i->stmt)
1525 AC.registerForcedBlockExpression(stmt);
1526 }
1527
1528 if (AC.getCFG()) {
1529 analyzed = true;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001530 for (SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator
Ted Kremenek0d28d362011-03-10 03:50:34 +00001531 i = fscope->PossiblyUnreachableDiags.begin(),
1532 e = fscope->PossiblyUnreachableDiags.end();
1533 i != e; ++i)
1534 {
1535 const sema::PossiblyUnreachableDiag &D = *i;
1536 bool processed = false;
1537 if (const Stmt *stmt = i->stmt) {
1538 const CFGBlock *block = AC.getBlockForRegisteredExpression(stmt);
Eli Friedman71b8fb52012-01-21 01:01:51 +00001539 CFGReverseBlockReachabilityAnalysis *cra =
1540 AC.getCFGReachablityAnalysis();
1541 // FIXME: We should be able to assert that block is non-null, but
1542 // the CFG analysis can skip potentially-evaluated expressions in
1543 // edge cases; see test/Sema/vla-2.c.
1544 if (block && cra) {
Ted Kremenek351ba912011-02-23 01:52:04 +00001545 // Can this block be reached from the entrance?
Ted Kremenek0d28d362011-03-10 03:50:34 +00001546 if (cra->isReachable(&AC.getCFG()->getEntry(), block))
Ted Kremenek351ba912011-02-23 01:52:04 +00001547 S.Diag(D.Loc, D.PD);
Ted Kremenek0d28d362011-03-10 03:50:34 +00001548 processed = true;
Ted Kremenek351ba912011-02-23 01:52:04 +00001549 }
1550 }
Ted Kremenek0d28d362011-03-10 03:50:34 +00001551 if (!processed) {
1552 // Emit the warning anyway if we cannot map to a basic block.
1553 S.Diag(D.Loc, D.PD);
1554 }
Ted Kremenek351ba912011-02-23 01:52:04 +00001555 }
Ted Kremenek0d28d362011-03-10 03:50:34 +00001556 }
Ted Kremenek351ba912011-02-23 01:52:04 +00001557
1558 if (!analyzed)
1559 flushDiagnostics(S, fscope);
1560 }
1561
1562
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001563 // Warning: check missing 'return'
David Blaikie23661d32012-01-24 04:51:48 +00001564 if (P.enableCheckFallThrough) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001565 const CheckFallThroughDiagnostics &CD =
1566 (isa<BlockDecl>(D) ? CheckFallThroughDiagnostics::MakeForBlock()
Douglas Gregor793cd1c2012-02-15 16:20:15 +00001567 : (isa<CXXMethodDecl>(D) &&
1568 cast<CXXMethodDecl>(D)->getOverloadedOperator() == OO_Call &&
1569 cast<CXXMethodDecl>(D)->getParent()->isLambda())
1570 ? CheckFallThroughDiagnostics::MakeForLambda()
1571 : CheckFallThroughDiagnostics::MakeForFunction(D));
Ted Kremenek3ed6fc02011-02-23 01:51:48 +00001572 CheckFallThroughForBody(S, D, Body, blkExpr, CD, AC);
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001573 }
1574
1575 // Warning: check for unreachable code
Ted Kremenek5dfee062011-11-30 21:22:09 +00001576 if (P.enableCheckUnreachable) {
1577 // Only check for unreachable code on non-template instantiations.
1578 // Different template instantiations can effectively change the control-flow
1579 // and it is very difficult to prove that a snippet of code in a template
1580 // is unreachable for all instantiations.
Ted Kremenek75df4ee2011-12-01 00:59:17 +00001581 bool isTemplateInstantiation = false;
1582 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
1583 isTemplateInstantiation = Function->isTemplateInstantiation();
1584 if (!isTemplateInstantiation)
Ted Kremenek5dfee062011-11-30 21:22:09 +00001585 CheckUnreachable(S, AC);
1586 }
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001587
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +00001588 // Check for thread safety violations
David Blaikie23661d32012-01-24 04:51:48 +00001589 if (P.enableThreadSafetyAnalysis) {
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +00001590 SourceLocation FL = AC.getDecl()->getLocation();
Richard Smith2e515622012-02-03 04:45:26 +00001591 SourceLocation FEL = AC.getDecl()->getLocEnd();
1592 thread_safety::ThreadSafetyReporter Reporter(S, FL, FEL);
DeLesley Hutchinsfb4afc22012-12-05 00:06:15 +00001593 if (Diags.getDiagnosticLevel(diag::warn_thread_safety_beta,D->getLocStart())
1594 != DiagnosticsEngine::Ignored)
1595 Reporter.setIssueBetaWarnings(true);
1596
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001597 thread_safety::runThreadSafetyAnalysis(AC, Reporter);
1598 Reporter.emitDiagnostics();
1599 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +00001600
Ted Kremeneka8c17a52011-01-25 19:13:48 +00001601 if (Diags.getDiagnosticLevel(diag::warn_uninit_var, D->getLocStart())
David Blaikied6471f72011-09-25 23:23:43 +00001602 != DiagnosticsEngine::Ignored ||
Richard Smith2815e1a2012-05-25 02:17:09 +00001603 Diags.getDiagnosticLevel(diag::warn_sometimes_uninit_var,D->getLocStart())
1604 != DiagnosticsEngine::Ignored ||
Ted Kremenek76709bf2011-03-15 05:22:28 +00001605 Diags.getDiagnosticLevel(diag::warn_maybe_uninit_var, D->getLocStart())
David Blaikied6471f72011-09-25 23:23:43 +00001606 != DiagnosticsEngine::Ignored) {
Ted Kremenekc5e43c12011-03-17 05:29:57 +00001607 if (CFG *cfg = AC.getCFG()) {
Ted Kremenekc21fed32011-01-18 21:18:58 +00001608 UninitValsDiagReporter reporter(S);
Fariborz Jahanian57080fb2011-07-16 18:31:33 +00001609 UninitVariablesAnalysisStats stats;
Benjamin Kramer12efd572011-07-16 20:13:06 +00001610 std::memset(&stats, 0, sizeof(UninitVariablesAnalysisStats));
Ted Kremeneka8c17a52011-01-25 19:13:48 +00001611 runUninitializedVariablesAnalysis(*cast<DeclContext>(D), *cfg, AC,
Chandler Carruth5d989942011-07-06 16:21:37 +00001612 reporter, stats);
1613
1614 if (S.CollectStats && stats.NumVariablesAnalyzed > 0) {
1615 ++NumUninitAnalysisFunctions;
1616 NumUninitAnalysisVariables += stats.NumVariablesAnalyzed;
1617 NumUninitAnalysisBlockVisits += stats.NumBlockVisits;
1618 MaxUninitAnalysisVariablesPerFunction =
1619 std::max(MaxUninitAnalysisVariablesPerFunction,
1620 stats.NumVariablesAnalyzed);
1621 MaxUninitAnalysisBlockVisitsPerFunction =
1622 std::max(MaxUninitAnalysisBlockVisitsPerFunction,
1623 stats.NumBlockVisits);
1624 }
Ted Kremenek610068c2011-01-15 02:58:47 +00001625 }
1626 }
Chandler Carruth5d989942011-07-06 16:21:37 +00001627
Alexander Kornienko19736342012-06-02 01:01:07 +00001628 bool FallThroughDiagFull =
1629 Diags.getDiagnosticLevel(diag::warn_unannotated_fallthrough,
1630 D->getLocStart()) != DiagnosticsEngine::Ignored;
Sean Huntc2f51cf2012-06-15 21:22:05 +00001631 bool FallThroughDiagPerFunction =
1632 Diags.getDiagnosticLevel(diag::warn_unannotated_fallthrough_per_function,
Alexander Kornienko19736342012-06-02 01:01:07 +00001633 D->getLocStart()) != DiagnosticsEngine::Ignored;
Sean Huntc2f51cf2012-06-15 21:22:05 +00001634 if (FallThroughDiagFull || FallThroughDiagPerFunction) {
Alexander Kornienko19736342012-06-02 01:01:07 +00001635 DiagnoseSwitchLabelsFallthrough(S, AC, !FallThroughDiagFull);
Richard Smithe0d3b4c2012-05-03 18:27:39 +00001636 }
1637
Jordan Rose58b6bdc2012-09-28 22:21:30 +00001638 if (S.getLangOpts().ObjCARCWeak &&
1639 Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak,
1640 D->getLocStart()) != DiagnosticsEngine::Ignored)
Jordan Roseb5cd1222012-10-11 16:10:19 +00001641 diagnoseRepeatedUseOfWeak(S, fscope, D, AC.getParentMap());
Jordan Rose58b6bdc2012-09-28 22:21:30 +00001642
Chandler Carruth5d989942011-07-06 16:21:37 +00001643 // Collect statistics about the CFG if it was built.
1644 if (S.CollectStats && AC.isCFGBuilt()) {
1645 ++NumFunctionsAnalyzed;
1646 if (CFG *cfg = AC.getCFG()) {
1647 // If we successfully built a CFG for this context, record some more
1648 // detail information about it.
Chandler Carruth3ea4c492011-07-06 22:21:45 +00001649 NumCFGBlocks += cfg->getNumBlockIDs();
Chandler Carruth5d989942011-07-06 16:21:37 +00001650 MaxCFGBlocksPerFunction = std::max(MaxCFGBlocksPerFunction,
Chandler Carruth3ea4c492011-07-06 22:21:45 +00001651 cfg->getNumBlockIDs());
Chandler Carruth5d989942011-07-06 16:21:37 +00001652 } else {
1653 ++NumFunctionsWithBadCFGs;
1654 }
1655 }
1656}
1657
1658void clang::sema::AnalysisBasedWarnings::PrintStats() const {
1659 llvm::errs() << "\n*** Analysis Based Warnings Stats:\n";
1660
1661 unsigned NumCFGsBuilt = NumFunctionsAnalyzed - NumFunctionsWithBadCFGs;
1662 unsigned AvgCFGBlocksPerFunction =
1663 !NumCFGsBuilt ? 0 : NumCFGBlocks/NumCFGsBuilt;
1664 llvm::errs() << NumFunctionsAnalyzed << " functions analyzed ("
1665 << NumFunctionsWithBadCFGs << " w/o CFGs).\n"
1666 << " " << NumCFGBlocks << " CFG blocks built.\n"
1667 << " " << AvgCFGBlocksPerFunction
1668 << " average CFG blocks per function.\n"
1669 << " " << MaxCFGBlocksPerFunction
1670 << " max CFG blocks per function.\n";
1671
1672 unsigned AvgUninitVariablesPerFunction = !NumUninitAnalysisFunctions ? 0
1673 : NumUninitAnalysisVariables/NumUninitAnalysisFunctions;
1674 unsigned AvgUninitBlockVisitsPerFunction = !NumUninitAnalysisFunctions ? 0
1675 : NumUninitAnalysisBlockVisits/NumUninitAnalysisFunctions;
1676 llvm::errs() << NumUninitAnalysisFunctions
1677 << " functions analyzed for uninitialiazed variables\n"
1678 << " " << NumUninitAnalysisVariables << " variables analyzed.\n"
1679 << " " << AvgUninitVariablesPerFunction
1680 << " average variables per function.\n"
1681 << " " << MaxUninitAnalysisVariablesPerFunction
1682 << " max variables per function.\n"
1683 << " " << NumUninitAnalysisBlockVisits << " block visits.\n"
1684 << " " << AvgUninitBlockVisitsPerFunction
1685 << " average block visits per function.\n"
1686 << " " << MaxUninitAnalysisBlockVisitsPerFunction
1687 << " max block visits per function.\n";
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001688}