blob: 7893a459c192fce2de317993b5daa2e119204385 [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"
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +000028#include "clang/Analysis/Analyses/Consumed.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000029#include "clang/Analysis/Analyses/ReachableCode.h"
30#include "clang/Analysis/Analyses/ThreadSafety.h"
31#include "clang/Analysis/Analyses/UninitializedValues.h"
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +000032#include "clang/Analysis/AnalysisContext.h"
33#include "clang/Analysis/CFG.h"
Ted Kremenek351ba912011-02-23 01:52:04 +000034#include "clang/Analysis/CFGStmtMap.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000035#include "clang/Basic/SourceLocation.h"
36#include "clang/Basic/SourceManager.h"
37#include "clang/Lex/Lexer.h"
38#include "clang/Lex/Preprocessor.h"
39#include "clang/Sema/ScopeInfo.h"
40#include "clang/Sema/SemaInternal.h"
Alexander Kornienko66da0ab2012-09-28 22:24:03 +000041#include "llvm/ADT/ArrayRef.h"
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +000042#include "llvm/ADT/BitVector.h"
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +000043#include "llvm/ADT/FoldingSet.h"
44#include "llvm/ADT/ImmutableMap.h"
Enea Zaffanella3285c782013-02-15 20:09:55 +000045#include "llvm/ADT/MapVector.h"
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +000046#include "llvm/ADT/PostOrderIterator.h"
Dmitri Gribenko19523542012-09-29 11:40:46 +000047#include "llvm/ADT/SmallString.h"
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +000048#include "llvm/ADT/SmallVector.h"
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +000049#include "llvm/ADT/StringRef.h"
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +000050#include "llvm/Support/Casting.h"
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +000051#include <algorithm>
Chandler Carruth55fc8732012-12-04 09:13:33 +000052#include <deque>
Richard Smithe0d3b4c2012-05-03 18:27:39 +000053#include <iterator>
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +000054#include <vector>
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +000055
56using namespace clang;
57
58//===----------------------------------------------------------------------===//
59// Unreachable code analysis.
60//===----------------------------------------------------------------------===//
61
62namespace {
63 class UnreachableCodeHandler : public reachable_code::Callback {
64 Sema &S;
65 public:
66 UnreachableCodeHandler(Sema &s) : S(s) {}
67
68 void HandleUnreachable(SourceLocation L, SourceRange R1, SourceRange R2) {
69 S.Diag(L, diag::warn_unreachable) << R1 << R2;
70 }
71 };
72}
73
74/// CheckUnreachable - Check for unreachable code.
Ted Kremenek1d26f482011-10-24 01:32:45 +000075static void CheckUnreachable(Sema &S, AnalysisDeclContext &AC) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +000076 UnreachableCodeHandler UC(S);
77 reachable_code::FindUnreachableCode(AC, UC);
78}
79
80//===----------------------------------------------------------------------===//
81// Check for missing return value.
82//===----------------------------------------------------------------------===//
83
John McCall16565aa2010-05-16 09:34:11 +000084enum ControlFlowKind {
85 UnknownFallThrough,
86 NeverFallThrough,
87 MaybeFallThrough,
88 AlwaysFallThrough,
89 NeverFallThroughOrReturn
90};
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +000091
92/// CheckFallThrough - Check that we don't fall off the end of a
93/// Statement that should return a value.
94///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +000095/// \returns AlwaysFallThrough iff we always fall off the end of the statement,
96/// MaybeFallThrough iff we might or might not fall off the end,
97/// NeverFallThroughOrReturn iff we never fall off the end of the statement or
98/// return. We assume NeverFallThrough iff we never fall off the end of the
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +000099/// statement but we may return. We assume that functions not marked noreturn
100/// will return.
Ted Kremenek1d26f482011-10-24 01:32:45 +0000101static ControlFlowKind CheckFallThrough(AnalysisDeclContext &AC) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000102 CFG *cfg = AC.getCFG();
John McCall16565aa2010-05-16 09:34:11 +0000103 if (cfg == 0) return UnknownFallThrough;
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000104
105 // The CFG leaves in dead things, and we don't want the dead code paths to
106 // confuse us, so we mark all live things first.
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000107 llvm::BitVector live(cfg->getNumBlockIDs());
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +0000108 unsigned count = reachable_code::ScanReachableFromBlock(&cfg->getEntry(),
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000109 live);
110
111 bool AddEHEdges = AC.getAddEHEdges();
112 if (!AddEHEdges && count != cfg->getNumBlockIDs())
113 // When there are things remaining dead, and we didn't add EH edges
114 // from CallExprs to the catch clauses, we have to go back and
115 // mark them as live.
116 for (CFG::iterator I = cfg->begin(), E = cfg->end(); I != E; ++I) {
117 CFGBlock &b = **I;
118 if (!live[b.getBlockID()]) {
119 if (b.pred_begin() == b.pred_end()) {
120 if (b.getTerminator() && isa<CXXTryStmt>(b.getTerminator()))
121 // When not adding EH edges from calls, catch clauses
122 // can otherwise seem dead. Avoid noting them as dead.
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +0000123 count += reachable_code::ScanReachableFromBlock(&b, live);
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000124 continue;
125 }
126 }
127 }
128
129 // Now we know what is live, we check the live precessors of the exit block
130 // and look for fall through paths, being careful to ignore normal returns,
131 // and exceptional paths.
132 bool HasLiveReturn = false;
133 bool HasFakeEdge = false;
134 bool HasPlainEdge = false;
135 bool HasAbnormalEdge = false;
Ted Kremenek90b828a2010-09-09 00:06:07 +0000136
137 // Ignore default cases that aren't likely to be reachable because all
138 // enums in a switch(X) have explicit case statements.
139 CFGBlock::FilterOptions FO;
140 FO.IgnoreDefaultsWithCoveredEnums = 1;
141
142 for (CFGBlock::filtered_pred_iterator
143 I = cfg->getExit().filtered_pred_start_end(FO); I.hasMore(); ++I) {
144 const CFGBlock& B = **I;
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000145 if (!live[B.getBlockID()])
146 continue;
Ted Kremenek5811f592011-01-26 04:49:52 +0000147
Chandler Carruthe05ee6d2011-09-13 09:53:58 +0000148 // Skip blocks which contain an element marked as no-return. They don't
149 // represent actually viable edges into the exit block, so mark them as
150 // abnormal.
151 if (B.hasNoReturnElement()) {
152 HasAbnormalEdge = true;
153 continue;
154 }
155
Ted Kremenek5811f592011-01-26 04:49:52 +0000156 // Destructors can appear after the 'return' in the CFG. This is
157 // normal. We need to look pass the destructors for the return
158 // statement (if it exists).
159 CFGBlock::const_reverse_iterator ri = B.rbegin(), re = B.rend();
Ted Kremenekc9f8f5a2011-03-02 20:32:29 +0000160
Chandler Carruthe05ee6d2011-09-13 09:53:58 +0000161 for ( ; ri != re ; ++ri)
David Blaikiefdf6a272013-02-21 20:58:29 +0000162 if (ri->getAs<CFGStmt>())
Ted Kremenek5811f592011-01-26 04:49:52 +0000163 break;
Chandler Carruthe05ee6d2011-09-13 09:53:58 +0000164
Ted Kremenek5811f592011-01-26 04:49:52 +0000165 // No more CFGElements in the block?
166 if (ri == re) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000167 if (B.getTerminator() && isa<CXXTryStmt>(B.getTerminator())) {
168 HasAbnormalEdge = true;
169 continue;
170 }
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000171 // A labeled empty statement, or the entry block...
172 HasPlainEdge = true;
173 continue;
174 }
Ted Kremenekf39e6a32011-01-25 22:50:47 +0000175
David Blaikiefdf6a272013-02-21 20:58:29 +0000176 CFGStmt CS = ri->castAs<CFGStmt>();
Ted Kremenekf1d10d92011-08-23 23:05:04 +0000177 const Stmt *S = CS.getStmt();
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000178 if (isa<ReturnStmt>(S)) {
179 HasLiveReturn = true;
180 continue;
181 }
182 if (isa<ObjCAtThrowStmt>(S)) {
183 HasFakeEdge = true;
184 continue;
185 }
186 if (isa<CXXThrowExpr>(S)) {
187 HasFakeEdge = true;
188 continue;
189 }
Chad Rosier8cd64b42012-06-11 20:47:18 +0000190 if (isa<MSAsmStmt>(S)) {
191 // TODO: Verify this is correct.
192 HasFakeEdge = true;
193 HasLiveReturn = true;
194 continue;
195 }
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000196 if (isa<CXXTryStmt>(S)) {
197 HasAbnormalEdge = true;
198 continue;
199 }
Chandler Carruthe05ee6d2011-09-13 09:53:58 +0000200 if (std::find(B.succ_begin(), B.succ_end(), &cfg->getExit())
201 == B.succ_end()) {
202 HasAbnormalEdge = true;
203 continue;
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000204 }
Chandler Carruthe05ee6d2011-09-13 09:53:58 +0000205
206 HasPlainEdge = true;
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000207 }
208 if (!HasPlainEdge) {
209 if (HasLiveReturn)
210 return NeverFallThrough;
211 return NeverFallThroughOrReturn;
212 }
213 if (HasAbnormalEdge || HasFakeEdge || HasLiveReturn)
214 return MaybeFallThrough;
215 // This says AlwaysFallThrough for calls to functions that are not marked
216 // noreturn, that don't return. If people would like this warning to be more
217 // accurate, such functions should be marked as noreturn.
218 return AlwaysFallThrough;
219}
220
Dan Gohman3c46e8d2010-07-26 21:25:24 +0000221namespace {
222
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000223struct CheckFallThroughDiagnostics {
224 unsigned diag_MaybeFallThrough_HasNoReturn;
225 unsigned diag_MaybeFallThrough_ReturnsNonVoid;
226 unsigned diag_AlwaysFallThrough_HasNoReturn;
227 unsigned diag_AlwaysFallThrough_ReturnsNonVoid;
228 unsigned diag_NeverFallThroughOrReturn;
Douglas Gregor793cd1c2012-02-15 16:20:15 +0000229 enum { Function, Block, Lambda } funMode;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000230 SourceLocation FuncLoc;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000231
Douglas Gregorca7eaee2010-04-16 23:28:44 +0000232 static CheckFallThroughDiagnostics MakeForFunction(const Decl *Func) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000233 CheckFallThroughDiagnostics D;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000234 D.FuncLoc = Func->getLocation();
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000235 D.diag_MaybeFallThrough_HasNoReturn =
236 diag::warn_falloff_noreturn_function;
237 D.diag_MaybeFallThrough_ReturnsNonVoid =
238 diag::warn_maybe_falloff_nonvoid_function;
239 D.diag_AlwaysFallThrough_HasNoReturn =
240 diag::warn_falloff_noreturn_function;
241 D.diag_AlwaysFallThrough_ReturnsNonVoid =
242 diag::warn_falloff_nonvoid_function;
Douglas Gregorca7eaee2010-04-16 23:28:44 +0000243
244 // Don't suggest that virtual functions be marked "noreturn", since they
245 // might be overridden by non-noreturn functions.
246 bool isVirtualMethod = false;
247 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Func))
248 isVirtualMethod = Method->isVirtual();
249
Douglas Gregorfcdd2cb2011-10-10 18:15:57 +0000250 // Don't suggest that template instantiations be marked "noreturn"
251 bool isTemplateInstantiation = false;
Ted Kremenek75df4ee2011-12-01 00:59:17 +0000252 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(Func))
253 isTemplateInstantiation = Function->isTemplateInstantiation();
Douglas Gregorfcdd2cb2011-10-10 18:15:57 +0000254
255 if (!isVirtualMethod && !isTemplateInstantiation)
Douglas Gregorca7eaee2010-04-16 23:28:44 +0000256 D.diag_NeverFallThroughOrReturn =
257 diag::warn_suggest_noreturn_function;
258 else
259 D.diag_NeverFallThroughOrReturn = 0;
260
Douglas Gregor793cd1c2012-02-15 16:20:15 +0000261 D.funMode = Function;
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000262 return D;
263 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000264
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000265 static CheckFallThroughDiagnostics MakeForBlock() {
266 CheckFallThroughDiagnostics D;
267 D.diag_MaybeFallThrough_HasNoReturn =
268 diag::err_noreturn_block_has_return_expr;
269 D.diag_MaybeFallThrough_ReturnsNonVoid =
270 diag::err_maybe_falloff_nonvoid_block;
271 D.diag_AlwaysFallThrough_HasNoReturn =
272 diag::err_noreturn_block_has_return_expr;
273 D.diag_AlwaysFallThrough_ReturnsNonVoid =
274 diag::err_falloff_nonvoid_block;
275 D.diag_NeverFallThroughOrReturn =
276 diag::warn_suggest_noreturn_block;
Douglas Gregor793cd1c2012-02-15 16:20:15 +0000277 D.funMode = Block;
278 return D;
279 }
280
281 static CheckFallThroughDiagnostics MakeForLambda() {
282 CheckFallThroughDiagnostics D;
283 D.diag_MaybeFallThrough_HasNoReturn =
284 diag::err_noreturn_lambda_has_return_expr;
285 D.diag_MaybeFallThrough_ReturnsNonVoid =
286 diag::warn_maybe_falloff_nonvoid_lambda;
287 D.diag_AlwaysFallThrough_HasNoReturn =
288 diag::err_noreturn_lambda_has_return_expr;
289 D.diag_AlwaysFallThrough_ReturnsNonVoid =
290 diag::warn_falloff_nonvoid_lambda;
291 D.diag_NeverFallThroughOrReturn = 0;
292 D.funMode = Lambda;
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000293 return D;
294 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000295
David Blaikied6471f72011-09-25 23:23:43 +0000296 bool checkDiagnostics(DiagnosticsEngine &D, bool ReturnsVoid,
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000297 bool HasNoReturn) const {
Douglas Gregor793cd1c2012-02-15 16:20:15 +0000298 if (funMode == Function) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000299 return (ReturnsVoid ||
300 D.getDiagnosticLevel(diag::warn_maybe_falloff_nonvoid_function,
David Blaikied6471f72011-09-25 23:23:43 +0000301 FuncLoc) == DiagnosticsEngine::Ignored)
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000302 && (!HasNoReturn ||
303 D.getDiagnosticLevel(diag::warn_noreturn_function_has_return_expr,
David Blaikied6471f72011-09-25 23:23:43 +0000304 FuncLoc) == DiagnosticsEngine::Ignored)
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000305 && (!ReturnsVoid ||
306 D.getDiagnosticLevel(diag::warn_suggest_noreturn_block, FuncLoc)
David Blaikied6471f72011-09-25 23:23:43 +0000307 == DiagnosticsEngine::Ignored);
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000308 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000309
Douglas Gregor793cd1c2012-02-15 16:20:15 +0000310 // For blocks / lambdas.
311 return ReturnsVoid && !HasNoReturn
312 && ((funMode == Lambda) ||
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000313 D.getDiagnosticLevel(diag::warn_suggest_noreturn_block, FuncLoc)
David Blaikied6471f72011-09-25 23:23:43 +0000314 == DiagnosticsEngine::Ignored);
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000315 }
316};
317
Dan Gohman3c46e8d2010-07-26 21:25:24 +0000318}
319
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000320/// CheckFallThroughForFunctionDef - Check that we don't fall off the end of a
321/// function that should return a value. Check that we don't fall off the end
322/// of a noreturn function. We assume that functions and blocks not marked
323/// noreturn will return.
324static void CheckFallThroughForBody(Sema &S, const Decl *D, const Stmt *Body,
Ted Kremenek3ed6fc02011-02-23 01:51:48 +0000325 const BlockExpr *blkExpr,
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000326 const CheckFallThroughDiagnostics& CD,
Ted Kremenek1d26f482011-10-24 01:32:45 +0000327 AnalysisDeclContext &AC) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000328
329 bool ReturnsVoid = false;
330 bool HasNoReturn = false;
331
332 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
333 ReturnsVoid = FD->getResultType()->isVoidType();
Richard Smithcd8ab512013-01-17 01:30:42 +0000334 HasNoReturn = FD->isNoReturn();
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000335 }
336 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
337 ReturnsVoid = MD->getResultType()->isVoidType();
338 HasNoReturn = MD->hasAttr<NoReturnAttr>();
339 }
340 else if (isa<BlockDecl>(D)) {
Ted Kremenek3ed6fc02011-02-23 01:51:48 +0000341 QualType BlockTy = blkExpr->getType();
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000342 if (const FunctionType *FT =
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000343 BlockTy->getPointeeType()->getAs<FunctionType>()) {
344 if (FT->getResultType()->isVoidType())
345 ReturnsVoid = true;
346 if (FT->getNoReturnAttr())
347 HasNoReturn = true;
348 }
349 }
350
David Blaikied6471f72011-09-25 23:23:43 +0000351 DiagnosticsEngine &Diags = S.getDiagnostics();
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000352
353 // Short circuit for compilation speed.
354 if (CD.checkDiagnostics(Diags, ReturnsVoid, HasNoReturn))
355 return;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000356
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000357 // FIXME: Function try block
358 if (const CompoundStmt *Compound = dyn_cast<CompoundStmt>(Body)) {
359 switch (CheckFallThrough(AC)) {
John McCall16565aa2010-05-16 09:34:11 +0000360 case UnknownFallThrough:
361 break;
362
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000363 case MaybeFallThrough:
364 if (HasNoReturn)
365 S.Diag(Compound->getRBracLoc(),
366 CD.diag_MaybeFallThrough_HasNoReturn);
367 else if (!ReturnsVoid)
368 S.Diag(Compound->getRBracLoc(),
369 CD.diag_MaybeFallThrough_ReturnsNonVoid);
370 break;
371 case AlwaysFallThrough:
372 if (HasNoReturn)
373 S.Diag(Compound->getRBracLoc(),
374 CD.diag_AlwaysFallThrough_HasNoReturn);
375 else if (!ReturnsVoid)
376 S.Diag(Compound->getRBracLoc(),
377 CD.diag_AlwaysFallThrough_ReturnsNonVoid);
378 break;
379 case NeverFallThroughOrReturn:
Chandler Carruthb0656ec2011-08-31 09:01:53 +0000380 if (ReturnsVoid && !HasNoReturn && CD.diag_NeverFallThroughOrReturn) {
381 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
382 S.Diag(Compound->getLBracLoc(), CD.diag_NeverFallThroughOrReturn)
Douglas Gregorb3321092011-09-10 00:56:20 +0000383 << 0 << FD;
384 } else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
385 S.Diag(Compound->getLBracLoc(), CD.diag_NeverFallThroughOrReturn)
386 << 1 << MD;
Chandler Carruthb0656ec2011-08-31 09:01:53 +0000387 } else {
388 S.Diag(Compound->getLBracLoc(), CD.diag_NeverFallThroughOrReturn);
389 }
390 }
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +0000391 break;
392 case NeverFallThrough:
393 break;
394 }
395 }
396}
397
398//===----------------------------------------------------------------------===//
Ted Kremenek610068c2011-01-15 02:58:47 +0000399// -Wuninitialized
400//===----------------------------------------------------------------------===//
401
Ted Kremenek6f417152011-04-04 20:56:00 +0000402namespace {
Chandler Carruth9f649462011-04-05 06:48:00 +0000403/// ContainsReference - A visitor class to search for references to
404/// a particular declaration (the needle) within any evaluated component of an
405/// expression (recursively).
Ted Kremenek6f417152011-04-04 20:56:00 +0000406class ContainsReference : public EvaluatedExprVisitor<ContainsReference> {
Chandler Carruth9f649462011-04-05 06:48:00 +0000407 bool FoundReference;
408 const DeclRefExpr *Needle;
409
Ted Kremenek6f417152011-04-04 20:56:00 +0000410public:
Chandler Carruth9f649462011-04-05 06:48:00 +0000411 ContainsReference(ASTContext &Context, const DeclRefExpr *Needle)
412 : EvaluatedExprVisitor<ContainsReference>(Context),
413 FoundReference(false), Needle(Needle) {}
414
415 void VisitExpr(Expr *E) {
Ted Kremenek6f417152011-04-04 20:56:00 +0000416 // Stop evaluating if we already have a reference.
Chandler Carruth9f649462011-04-05 06:48:00 +0000417 if (FoundReference)
Ted Kremenek6f417152011-04-04 20:56:00 +0000418 return;
Chandler Carruth9f649462011-04-05 06:48:00 +0000419
420 EvaluatedExprVisitor<ContainsReference>::VisitExpr(E);
Ted Kremenek6f417152011-04-04 20:56:00 +0000421 }
Chandler Carruth9f649462011-04-05 06:48:00 +0000422
423 void VisitDeclRefExpr(DeclRefExpr *E) {
424 if (E == Needle)
425 FoundReference = true;
426 else
427 EvaluatedExprVisitor<ContainsReference>::VisitDeclRefExpr(E);
Ted Kremenek6f417152011-04-04 20:56:00 +0000428 }
Chandler Carruth9f649462011-04-05 06:48:00 +0000429
430 bool doesContainReference() const { return FoundReference; }
Ted Kremenek6f417152011-04-04 20:56:00 +0000431};
432}
433
David Blaikie4f4f3492011-09-10 05:35:08 +0000434static bool SuggestInitializationFixit(Sema &S, const VarDecl *VD) {
Fariborz Jahaniana34194f2012-03-08 00:22:50 +0000435 QualType VariableTy = VD->getType().getCanonicalType();
436 if (VariableTy->isBlockPointerType() &&
437 !VD->hasAttr<BlocksAttr>()) {
438 S.Diag(VD->getLocation(), diag::note_block_var_fixit_add_initialization) << VD->getDeclName()
439 << FixItHint::CreateInsertion(VD->getLocation(), "__block ");
440 return true;
441 }
442
David Blaikie4f4f3492011-09-10 05:35:08 +0000443 // Don't issue a fixit if there is already an initializer.
444 if (VD->getInit())
445 return false;
Fariborz Jahaniana34194f2012-03-08 00:22:50 +0000446
David Blaikie4f4f3492011-09-10 05:35:08 +0000447 // Suggest possible initialization (if any).
David Blaikie2c0abf42012-04-30 18:27:22 +0000448 std::string Init = S.getFixItZeroInitializerForType(VariableTy);
449 if (Init.empty())
David Blaikie4f4f3492011-09-10 05:35:08 +0000450 return false;
Richard Trieu7b0a3e32012-05-03 01:09:59 +0000451
452 // Don't suggest a fixit inside macros.
453 if (VD->getLocEnd().isMacroID())
454 return false;
455
Richard Smith7984de32012-01-12 23:53:29 +0000456 SourceLocation Loc = S.PP.getLocForEndOfToken(VD->getLocEnd());
Fariborz Jahaniana34194f2012-03-08 00:22:50 +0000457
Richard Smith7984de32012-01-12 23:53:29 +0000458 S.Diag(Loc, diag::note_var_fixit_add_initialization) << VD->getDeclName()
459 << FixItHint::CreateInsertion(Loc, Init);
460 return true;
David Blaikie4f4f3492011-09-10 05:35:08 +0000461}
462
Richard Smithbdb97ff2012-05-26 06:20:46 +0000463/// Create a fixit to remove an if-like statement, on the assumption that its
464/// condition is CondVal.
465static void CreateIfFixit(Sema &S, const Stmt *If, const Stmt *Then,
466 const Stmt *Else, bool CondVal,
467 FixItHint &Fixit1, FixItHint &Fixit2) {
468 if (CondVal) {
469 // If condition is always true, remove all but the 'then'.
470 Fixit1 = FixItHint::CreateRemoval(
471 CharSourceRange::getCharRange(If->getLocStart(),
472 Then->getLocStart()));
473 if (Else) {
474 SourceLocation ElseKwLoc = Lexer::getLocForEndOfToken(
475 Then->getLocEnd(), 0, S.getSourceManager(), S.getLangOpts());
476 Fixit2 = FixItHint::CreateRemoval(
477 SourceRange(ElseKwLoc, Else->getLocEnd()));
478 }
479 } else {
480 // If condition is always false, remove all but the 'else'.
481 if (Else)
482 Fixit1 = FixItHint::CreateRemoval(
483 CharSourceRange::getCharRange(If->getLocStart(),
484 Else->getLocStart()));
485 else
486 Fixit1 = FixItHint::CreateRemoval(If->getSourceRange());
487 }
488}
489
490/// DiagUninitUse -- Helper function to produce a diagnostic for an
491/// uninitialized use of a variable.
492static void DiagUninitUse(Sema &S, const VarDecl *VD, const UninitUse &Use,
493 bool IsCapturedByBlock) {
494 bool Diagnosed = false;
495
Richard Smith8a1fdfc2013-09-12 18:49:10 +0000496 switch (Use.getKind()) {
497 case UninitUse::Always:
498 S.Diag(Use.getUser()->getLocStart(), diag::warn_uninit_var)
499 << VD->getDeclName() << IsCapturedByBlock
500 << Use.getUser()->getSourceRange();
501 return;
502
503 case UninitUse::AfterDecl:
504 case UninitUse::AfterCall:
505 S.Diag(VD->getLocation(), diag::warn_sometimes_uninit_var)
506 << VD->getDeclName() << IsCapturedByBlock
507 << (Use.getKind() == UninitUse::AfterDecl ? 4 : 5)
508 << const_cast<DeclContext*>(VD->getLexicalDeclContext())
509 << VD->getSourceRange();
510 S.Diag(Use.getUser()->getLocStart(), diag::note_uninit_var_use)
511 << IsCapturedByBlock << Use.getUser()->getSourceRange();
512 return;
513
514 case UninitUse::Maybe:
515 case UninitUse::Sometimes:
516 // Carry on to report sometimes-uninitialized branches, if possible,
517 // or a 'may be used uninitialized' diagnostic otherwise.
518 break;
519 }
520
Richard Smithbdb97ff2012-05-26 06:20:46 +0000521 // Diagnose each branch which leads to a sometimes-uninitialized use.
Richard Smith2815e1a2012-05-25 02:17:09 +0000522 for (UninitUse::branch_iterator I = Use.branch_begin(), E = Use.branch_end();
523 I != E; ++I) {
Richard Smithbdb97ff2012-05-26 06:20:46 +0000524 assert(Use.getKind() == UninitUse::Sometimes);
525
526 const Expr *User = Use.getUser();
Richard Smith2815e1a2012-05-25 02:17:09 +0000527 const Stmt *Term = I->Terminator;
Richard Smithbdb97ff2012-05-26 06:20:46 +0000528
529 // Information used when building the diagnostic.
Richard Smith2815e1a2012-05-25 02:17:09 +0000530 unsigned DiagKind;
David Blaikie0bea8632012-10-08 01:11:04 +0000531 StringRef Str;
Richard Smithbdb97ff2012-05-26 06:20:46 +0000532 SourceRange Range;
533
Stefanus Du Toitfc093362013-03-01 21:41:22 +0000534 // FixIts to suppress the diagnostic by removing the dead condition.
Richard Smithbdb97ff2012-05-26 06:20:46 +0000535 // For all binary terminators, branch 0 is taken if the condition is true,
536 // and branch 1 is taken if the condition is false.
537 int RemoveDiagKind = -1;
538 const char *FixitStr =
539 S.getLangOpts().CPlusPlus ? (I->Output ? "true" : "false")
540 : (I->Output ? "1" : "0");
541 FixItHint Fixit1, Fixit2;
542
Richard Smith8a1fdfc2013-09-12 18:49:10 +0000543 switch (Term ? Term->getStmtClass() : Stmt::DeclStmtClass) {
Richard Smith2815e1a2012-05-25 02:17:09 +0000544 default:
Richard Smithbdb97ff2012-05-26 06:20:46 +0000545 // Don't know how to report this. Just fall back to 'may be used
Richard Smith8a1fdfc2013-09-12 18:49:10 +0000546 // uninitialized'. FIXME: Can this happen?
Richard Smith2815e1a2012-05-25 02:17:09 +0000547 continue;
548
549 // "condition is true / condition is false".
Richard Smithbdb97ff2012-05-26 06:20:46 +0000550 case Stmt::IfStmtClass: {
551 const IfStmt *IS = cast<IfStmt>(Term);
Richard Smith2815e1a2012-05-25 02:17:09 +0000552 DiagKind = 0;
553 Str = "if";
Richard Smithbdb97ff2012-05-26 06:20:46 +0000554 Range = IS->getCond()->getSourceRange();
555 RemoveDiagKind = 0;
556 CreateIfFixit(S, IS, IS->getThen(), IS->getElse(),
557 I->Output, Fixit1, Fixit2);
Richard Smith2815e1a2012-05-25 02:17:09 +0000558 break;
Richard Smithbdb97ff2012-05-26 06:20:46 +0000559 }
560 case Stmt::ConditionalOperatorClass: {
561 const ConditionalOperator *CO = cast<ConditionalOperator>(Term);
Richard Smith2815e1a2012-05-25 02:17:09 +0000562 DiagKind = 0;
563 Str = "?:";
Richard Smithbdb97ff2012-05-26 06:20:46 +0000564 Range = CO->getCond()->getSourceRange();
565 RemoveDiagKind = 0;
566 CreateIfFixit(S, CO, CO->getTrueExpr(), CO->getFalseExpr(),
567 I->Output, Fixit1, Fixit2);
Richard Smith2815e1a2012-05-25 02:17:09 +0000568 break;
Richard Smithbdb97ff2012-05-26 06:20:46 +0000569 }
Richard Smith2815e1a2012-05-25 02:17:09 +0000570 case Stmt::BinaryOperatorClass: {
571 const BinaryOperator *BO = cast<BinaryOperator>(Term);
572 if (!BO->isLogicalOp())
573 continue;
574 DiagKind = 0;
575 Str = BO->getOpcodeStr();
576 Range = BO->getLHS()->getSourceRange();
Richard Smithbdb97ff2012-05-26 06:20:46 +0000577 RemoveDiagKind = 0;
578 if ((BO->getOpcode() == BO_LAnd && I->Output) ||
579 (BO->getOpcode() == BO_LOr && !I->Output))
580 // true && y -> y, false || y -> y.
581 Fixit1 = FixItHint::CreateRemoval(SourceRange(BO->getLocStart(),
582 BO->getOperatorLoc()));
583 else
584 // false && y -> false, true || y -> true.
585 Fixit1 = FixItHint::CreateReplacement(BO->getSourceRange(), FixitStr);
Richard Smith2815e1a2012-05-25 02:17:09 +0000586 break;
587 }
588
589 // "loop is entered / loop is exited".
590 case Stmt::WhileStmtClass:
591 DiagKind = 1;
592 Str = "while";
593 Range = cast<WhileStmt>(Term)->getCond()->getSourceRange();
Richard Smithbdb97ff2012-05-26 06:20:46 +0000594 RemoveDiagKind = 1;
595 Fixit1 = FixItHint::CreateReplacement(Range, FixitStr);
Richard Smith2815e1a2012-05-25 02:17:09 +0000596 break;
597 case Stmt::ForStmtClass:
598 DiagKind = 1;
599 Str = "for";
600 Range = cast<ForStmt>(Term)->getCond()->getSourceRange();
Richard Smithbdb97ff2012-05-26 06:20:46 +0000601 RemoveDiagKind = 1;
602 if (I->Output)
603 Fixit1 = FixItHint::CreateRemoval(Range);
604 else
605 Fixit1 = FixItHint::CreateReplacement(Range, FixitStr);
Richard Smith2815e1a2012-05-25 02:17:09 +0000606 break;
Richard Smith8a1fdfc2013-09-12 18:49:10 +0000607 case Stmt::CXXForRangeStmtClass:
608 if (I->Output == 1) {
609 // The use occurs if a range-based for loop's body never executes.
610 // That may be impossible, and there's no syntactic fix for this,
611 // so treat it as a 'may be uninitialized' case.
612 continue;
613 }
614 DiagKind = 1;
615 Str = "for";
616 Range = cast<CXXForRangeStmt>(Term)->getRangeInit()->getSourceRange();
617 break;
Richard Smith2815e1a2012-05-25 02:17:09 +0000618
619 // "condition is true / loop is exited".
620 case Stmt::DoStmtClass:
621 DiagKind = 2;
622 Str = "do";
623 Range = cast<DoStmt>(Term)->getCond()->getSourceRange();
Richard Smithbdb97ff2012-05-26 06:20:46 +0000624 RemoveDiagKind = 1;
625 Fixit1 = FixItHint::CreateReplacement(Range, FixitStr);
Richard Smith2815e1a2012-05-25 02:17:09 +0000626 break;
627
628 // "switch case is taken".
629 case Stmt::CaseStmtClass:
630 DiagKind = 3;
631 Str = "case";
632 Range = cast<CaseStmt>(Term)->getLHS()->getSourceRange();
633 break;
634 case Stmt::DefaultStmtClass:
635 DiagKind = 3;
636 Str = "default";
637 Range = cast<DefaultStmt>(Term)->getDefaultLoc();
638 break;
639 }
640
Richard Smithbdb97ff2012-05-26 06:20:46 +0000641 S.Diag(Range.getBegin(), diag::warn_sometimes_uninit_var)
642 << VD->getDeclName() << IsCapturedByBlock << DiagKind
643 << Str << I->Output << Range;
644 S.Diag(User->getLocStart(), diag::note_uninit_var_use)
645 << IsCapturedByBlock << User->getSourceRange();
646 if (RemoveDiagKind != -1)
647 S.Diag(Fixit1.RemoveRange.getBegin(), diag::note_uninit_fixit_remove_cond)
648 << RemoveDiagKind << Str << I->Output << Fixit1 << Fixit2;
649
650 Diagnosed = true;
Richard Smith2815e1a2012-05-25 02:17:09 +0000651 }
Richard Smithbdb97ff2012-05-26 06:20:46 +0000652
653 if (!Diagnosed)
Richard Smith8a1fdfc2013-09-12 18:49:10 +0000654 S.Diag(Use.getUser()->getLocStart(), diag::warn_maybe_uninit_var)
Richard Smithbdb97ff2012-05-26 06:20:46 +0000655 << VD->getDeclName() << IsCapturedByBlock
656 << Use.getUser()->getSourceRange();
Richard Smith2815e1a2012-05-25 02:17:09 +0000657}
658
Chandler Carruth262d50e2011-04-05 18:27:05 +0000659/// DiagnoseUninitializedUse -- Helper function for diagnosing uses of an
660/// uninitialized variable. This manages the different forms of diagnostic
661/// emitted for particular types of uses. Returns true if the use was diagnosed
Richard Smith2815e1a2012-05-25 02:17:09 +0000662/// as a warning. If a particular use is one we omit warnings for, returns
Chandler Carruth262d50e2011-04-05 18:27:05 +0000663/// false.
664static bool DiagnoseUninitializedUse(Sema &S, const VarDecl *VD,
Richard Smith2815e1a2012-05-25 02:17:09 +0000665 const UninitUse &Use,
Ted Kremenek9e761722011-10-13 18:50:06 +0000666 bool alwaysReportSelfInit = false) {
Chandler Carruth4c4983b2011-04-05 18:18:05 +0000667
Richard Smith2815e1a2012-05-25 02:17:09 +0000668 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Use.getUser())) {
Richard Trieuf6278e52012-05-09 21:08:22 +0000669 // Inspect the initializer of the variable declaration which is
670 // being referenced prior to its initialization. We emit
671 // specialized diagnostics for self-initialization, and we
672 // specifically avoid warning about self references which take the
673 // form of:
674 //
675 // int x = x;
676 //
677 // This is used to indicate to GCC that 'x' is intentionally left
678 // uninitialized. Proven code paths which access 'x' in
679 // an uninitialized state after this will still warn.
680 if (const Expr *Initializer = VD->getInit()) {
681 if (!alwaysReportSelfInit && DRE == Initializer->IgnoreParenImpCasts())
682 return false;
Chandler Carruth4c4983b2011-04-05 18:18:05 +0000683
Richard Trieuf6278e52012-05-09 21:08:22 +0000684 ContainsReference CR(S.Context, DRE);
685 CR.Visit(const_cast<Expr*>(Initializer));
686 if (CR.doesContainReference()) {
Chandler Carruth4c4983b2011-04-05 18:18:05 +0000687 S.Diag(DRE->getLocStart(),
688 diag::warn_uninit_self_reference_in_init)
Richard Trieuf6278e52012-05-09 21:08:22 +0000689 << VD->getDeclName() << VD->getLocation() << DRE->getSourceRange();
690 return true;
Chandler Carruth4c4983b2011-04-05 18:18:05 +0000691 }
Chandler Carruth4c4983b2011-04-05 18:18:05 +0000692 }
Richard Trieuf6278e52012-05-09 21:08:22 +0000693
Richard Smithbdb97ff2012-05-26 06:20:46 +0000694 DiagUninitUse(S, VD, Use, false);
Chandler Carruth4c4983b2011-04-05 18:18:05 +0000695 } else {
Richard Smith2815e1a2012-05-25 02:17:09 +0000696 const BlockExpr *BE = cast<BlockExpr>(Use.getUser());
Richard Smithbdb97ff2012-05-26 06:20:46 +0000697 if (VD->getType()->isBlockPointerType() && !VD->hasAttr<BlocksAttr>())
698 S.Diag(BE->getLocStart(),
699 diag::warn_uninit_byref_blockvar_captured_by_block)
Fariborz Jahaniana34194f2012-03-08 00:22:50 +0000700 << VD->getDeclName();
Richard Smithbdb97ff2012-05-26 06:20:46 +0000701 else
702 DiagUninitUse(S, VD, Use, true);
Chandler Carruth4c4983b2011-04-05 18:18:05 +0000703 }
704
705 // Report where the variable was declared when the use wasn't within
David Blaikie4f4f3492011-09-10 05:35:08 +0000706 // the initializer of that declaration & we didn't already suggest
707 // an initialization fixit.
Richard Trieuf6278e52012-05-09 21:08:22 +0000708 if (!SuggestInitializationFixit(S, VD))
Chandler Carruth4c4983b2011-04-05 18:18:05 +0000709 S.Diag(VD->getLocStart(), diag::note_uninit_var_def)
710 << VD->getDeclName();
711
Chandler Carruth262d50e2011-04-05 18:27:05 +0000712 return true;
Chandler Carruth64fb9592011-04-05 18:18:08 +0000713}
714
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000715namespace {
716 class FallthroughMapper : public RecursiveASTVisitor<FallthroughMapper> {
717 public:
718 FallthroughMapper(Sema &S)
719 : FoundSwitchStatements(false),
720 S(S) {
721 }
722
723 bool foundSwitchStatements() const { return FoundSwitchStatements; }
724
725 void markFallthroughVisited(const AttributedStmt *Stmt) {
726 bool Found = FallthroughStmts.erase(Stmt);
727 assert(Found);
Kaelyn Uhrain3bb29942012-05-03 19:46:38 +0000728 (void)Found;
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000729 }
730
731 typedef llvm::SmallPtrSet<const AttributedStmt*, 8> AttrStmts;
732
733 const AttrStmts &getFallthroughStmts() const {
734 return FallthroughStmts;
735 }
736
Alexander Kornienko4874a812013-01-30 03:49:44 +0000737 void fillReachableBlocks(CFG *Cfg) {
738 assert(ReachableBlocks.empty() && "ReachableBlocks already filled");
739 std::deque<const CFGBlock *> BlockQueue;
740
741 ReachableBlocks.insert(&Cfg->getEntry());
742 BlockQueue.push_back(&Cfg->getEntry());
Alexander Kornienko878d0ad2013-02-07 02:17:19 +0000743 // Mark all case blocks reachable to avoid problems with switching on
744 // constants, covered enums, etc.
745 // These blocks can contain fall-through annotations, and we don't want to
746 // issue a warn_fallthrough_attr_unreachable for them.
747 for (CFG::iterator I = Cfg->begin(), E = Cfg->end(); I != E; ++I) {
748 const CFGBlock *B = *I;
749 const Stmt *L = B->getLabel();
750 if (L && isa<SwitchCase>(L) && ReachableBlocks.insert(B))
751 BlockQueue.push_back(B);
752 }
753
Alexander Kornienko4874a812013-01-30 03:49:44 +0000754 while (!BlockQueue.empty()) {
755 const CFGBlock *P = BlockQueue.front();
756 BlockQueue.pop_front();
757 for (CFGBlock::const_succ_iterator I = P->succ_begin(),
758 E = P->succ_end();
759 I != E; ++I) {
Alexander Kornienko0162b832013-02-01 15:39:20 +0000760 if (*I && ReachableBlocks.insert(*I))
Alexander Kornienko4874a812013-01-30 03:49:44 +0000761 BlockQueue.push_back(*I);
762 }
763 }
764 }
765
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000766 bool checkFallThroughIntoBlock(const CFGBlock &B, int &AnnotatedCnt) {
Alexander Kornienko4874a812013-01-30 03:49:44 +0000767 assert(!ReachableBlocks.empty() && "ReachableBlocks empty");
768
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000769 int UnannotatedCnt = 0;
770 AnnotatedCnt = 0;
771
772 std::deque<const CFGBlock*> BlockQueue;
773
774 std::copy(B.pred_begin(), B.pred_end(), std::back_inserter(BlockQueue));
775
776 while (!BlockQueue.empty()) {
777 const CFGBlock *P = BlockQueue.front();
778 BlockQueue.pop_front();
779
780 const Stmt *Term = P->getTerminator();
781 if (Term && isa<SwitchStmt>(Term))
782 continue; // Switch statement, good.
783
784 const SwitchCase *SW = dyn_cast_or_null<SwitchCase>(P->getLabel());
785 if (SW && SW->getSubStmt() == B.getLabel() && P->begin() == P->end())
786 continue; // Previous case label has no statements, good.
787
Alexander Kornienkoc6dcea92013-01-25 20:44:56 +0000788 const LabelStmt *L = dyn_cast_or_null<LabelStmt>(P->getLabel());
789 if (L && L->getSubStmt() == B.getLabel() && P->begin() == P->end())
790 continue; // Case label is preceded with a normal label, good.
791
Alexander Kornienko4874a812013-01-30 03:49:44 +0000792 if (!ReachableBlocks.count(P)) {
Alexander Kornienko878d0ad2013-02-07 02:17:19 +0000793 for (CFGBlock::const_reverse_iterator ElemIt = P->rbegin(),
794 ElemEnd = P->rend();
795 ElemIt != ElemEnd; ++ElemIt) {
David Blaikieb0780542013-02-23 00:29:34 +0000796 if (Optional<CFGStmt> CS = ElemIt->getAs<CFGStmt>()) {
797 if (const AttributedStmt *AS = asFallThroughAttr(CS->getStmt())) {
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000798 S.Diag(AS->getLocStart(),
799 diag::warn_fallthrough_attr_unreachable);
800 markFallthroughVisited(AS);
801 ++AnnotatedCnt;
Alexander Kornienko878d0ad2013-02-07 02:17:19 +0000802 break;
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000803 }
804 // Don't care about other unreachable statements.
805 }
806 }
807 // If there are no unreachable statements, this may be a special
808 // case in CFG:
809 // case X: {
810 // A a; // A has a destructor.
811 // break;
812 // }
813 // // <<<< This place is represented by a 'hanging' CFG block.
814 // case Y:
815 continue;
816 }
817
818 const Stmt *LastStmt = getLastStmt(*P);
819 if (const AttributedStmt *AS = asFallThroughAttr(LastStmt)) {
820 markFallthroughVisited(AS);
821 ++AnnotatedCnt;
822 continue; // Fallthrough annotation, good.
823 }
824
825 if (!LastStmt) { // This block contains no executable statements.
826 // Traverse its predecessors.
827 std::copy(P->pred_begin(), P->pred_end(),
828 std::back_inserter(BlockQueue));
829 continue;
830 }
831
832 ++UnannotatedCnt;
833 }
834 return !!UnannotatedCnt;
835 }
836
837 // RecursiveASTVisitor setup.
838 bool shouldWalkTypesOfTypeLocs() const { return false; }
839
840 bool VisitAttributedStmt(AttributedStmt *S) {
841 if (asFallThroughAttr(S))
842 FallthroughStmts.insert(S);
843 return true;
844 }
845
846 bool VisitSwitchStmt(SwitchStmt *S) {
847 FoundSwitchStatements = true;
848 return true;
849 }
850
Alexander Kornienkob0707c92013-04-02 15:20:32 +0000851 // We don't want to traverse local type declarations. We analyze their
852 // methods separately.
853 bool TraverseDecl(Decl *D) { return true; }
854
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000855 private:
856
857 static const AttributedStmt *asFallThroughAttr(const Stmt *S) {
858 if (const AttributedStmt *AS = dyn_cast_or_null<AttributedStmt>(S)) {
859 if (hasSpecificAttr<FallThroughAttr>(AS->getAttrs()))
860 return AS;
861 }
862 return 0;
863 }
864
865 static const Stmt *getLastStmt(const CFGBlock &B) {
866 if (const Stmt *Term = B.getTerminator())
867 return Term;
868 for (CFGBlock::const_reverse_iterator ElemIt = B.rbegin(),
869 ElemEnd = B.rend();
870 ElemIt != ElemEnd; ++ElemIt) {
David Blaikieb0780542013-02-23 00:29:34 +0000871 if (Optional<CFGStmt> CS = ElemIt->getAs<CFGStmt>())
872 return CS->getStmt();
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000873 }
874 // Workaround to detect a statement thrown out by CFGBuilder:
875 // case X: {} case Y:
876 // case X: ; case Y:
877 if (const SwitchCase *SW = dyn_cast_or_null<SwitchCase>(B.getLabel()))
878 if (!isa<SwitchCase>(SW->getSubStmt()))
879 return SW->getSubStmt();
880
881 return 0;
882 }
883
884 bool FoundSwitchStatements;
885 AttrStmts FallthroughStmts;
886 Sema &S;
Alexander Kornienko4874a812013-01-30 03:49:44 +0000887 llvm::SmallPtrSet<const CFGBlock *, 16> ReachableBlocks;
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000888 };
889}
890
Alexander Kornienko19736342012-06-02 01:01:07 +0000891static void DiagnoseSwitchLabelsFallthrough(Sema &S, AnalysisDeclContext &AC,
Sean Huntc2f51cf2012-06-15 21:22:05 +0000892 bool PerFunction) {
Ted Kremenek30783532012-11-12 21:20:48 +0000893 // Only perform this analysis when using C++11. There is no good workflow
894 // for this warning when not using C++11. There is no good way to silence
895 // the warning (no attribute is available) unless we are using C++11's support
896 // for generalized attributes. Once could use pragmas to silence the warning,
897 // but as a general solution that is gross and not in the spirit of this
898 // warning.
899 //
900 // NOTE: This an intermediate solution. There are on-going discussions on
901 // how to properly support this warning outside of C++11 with an annotation.
Richard Smith80ad52f2013-01-02 11:42:31 +0000902 if (!AC.getASTContext().getLangOpts().CPlusPlus11)
Ted Kremenek30783532012-11-12 21:20:48 +0000903 return;
904
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000905 FallthroughMapper FM(S);
906 FM.TraverseStmt(AC.getBody());
907
908 if (!FM.foundSwitchStatements())
909 return;
910
Sean Huntc2f51cf2012-06-15 21:22:05 +0000911 if (PerFunction && FM.getFallthroughStmts().empty())
Alexander Kornienko19736342012-06-02 01:01:07 +0000912 return;
913
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000914 CFG *Cfg = AC.getCFG();
915
916 if (!Cfg)
917 return;
918
Alexander Kornienko4874a812013-01-30 03:49:44 +0000919 FM.fillReachableBlocks(Cfg);
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000920
921 for (CFG::reverse_iterator I = Cfg->rbegin(), E = Cfg->rend(); I != E; ++I) {
Alexander Kornienkoe992ed12013-01-25 15:49:34 +0000922 const CFGBlock *B = *I;
923 const Stmt *Label = B->getLabel();
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000924
925 if (!Label || !isa<SwitchCase>(Label))
926 continue;
927
Alexander Kornienko4874a812013-01-30 03:49:44 +0000928 int AnnotatedCnt;
929
Alexander Kornienkoe992ed12013-01-25 15:49:34 +0000930 if (!FM.checkFallThroughIntoBlock(*B, AnnotatedCnt))
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000931 continue;
932
Alexander Kornienko19736342012-06-02 01:01:07 +0000933 S.Diag(Label->getLocStart(),
Sean Huntc2f51cf2012-06-15 21:22:05 +0000934 PerFunction ? diag::warn_unannotated_fallthrough_per_function
935 : diag::warn_unannotated_fallthrough);
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000936
937 if (!AnnotatedCnt) {
938 SourceLocation L = Label->getLocStart();
939 if (L.isMacroID())
940 continue;
Richard Smith80ad52f2013-01-02 11:42:31 +0000941 if (S.getLangOpts().CPlusPlus11) {
Alexander Kornienkoe992ed12013-01-25 15:49:34 +0000942 const Stmt *Term = B->getTerminator();
943 // Skip empty cases.
944 while (B->empty() && !Term && B->succ_size() == 1) {
945 B = *B->succ_begin();
946 Term = B->getTerminator();
947 }
948 if (!(B->empty() && Term && isa<BreakStmt>(Term))) {
Alexander Kornienko66da0ab2012-09-28 22:24:03 +0000949 Preprocessor &PP = S.getPreprocessor();
950 TokenValue Tokens[] = {
951 tok::l_square, tok::l_square, PP.getIdentifierInfo("clang"),
952 tok::coloncolon, PP.getIdentifierInfo("fallthrough"),
953 tok::r_square, tok::r_square
954 };
Dmitri Gribenko19523542012-09-29 11:40:46 +0000955 StringRef AnnotationSpelling = "[[clang::fallthrough]]";
956 StringRef MacroName = PP.getLastMacroWithSpelling(L, Tokens);
957 if (!MacroName.empty())
958 AnnotationSpelling = MacroName;
959 SmallString<64> TextToInsert(AnnotationSpelling);
960 TextToInsert += "; ";
Alexander Kornienkoa189d892012-05-26 00:49:15 +0000961 S.Diag(L, diag::note_insert_fallthrough_fixit) <<
Alexander Kornienko66da0ab2012-09-28 22:24:03 +0000962 AnnotationSpelling <<
Dmitri Gribenko19523542012-09-29 11:40:46 +0000963 FixItHint::CreateInsertion(L, TextToInsert);
Alexander Kornienkoa189d892012-05-26 00:49:15 +0000964 }
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000965 }
966 S.Diag(L, diag::note_insert_break_fixit) <<
967 FixItHint::CreateInsertion(L, "break; ");
968 }
969 }
970
971 const FallthroughMapper::AttrStmts &Fallthroughs = FM.getFallthroughStmts();
972 for (FallthroughMapper::AttrStmts::const_iterator I = Fallthroughs.begin(),
973 E = Fallthroughs.end();
974 I != E; ++I) {
975 S.Diag((*I)->getLocStart(), diag::warn_fallthrough_attr_invalid_placement);
976 }
977
978}
979
Ted Kremenek610068c2011-01-15 02:58:47 +0000980namespace {
Jordan Rose20441c52012-09-28 22:29:02 +0000981typedef std::pair<const Stmt *,
982 sema::FunctionScopeInfo::WeakObjectUseMap::const_iterator>
983 StmtUsesPair;
Jordan Rose58b6bdc2012-09-28 22:21:30 +0000984
Jordan Rose20441c52012-09-28 22:29:02 +0000985class StmtUseSorter {
Jordan Rose58b6bdc2012-09-28 22:21:30 +0000986 const SourceManager &SM;
987
988public:
Jordan Rose20441c52012-09-28 22:29:02 +0000989 explicit StmtUseSorter(const SourceManager &SM) : SM(SM) { }
Jordan Rose58b6bdc2012-09-28 22:21:30 +0000990
991 bool operator()(const StmtUsesPair &LHS, const StmtUsesPair &RHS) {
992 return SM.isBeforeInTranslationUnit(LHS.first->getLocStart(),
993 RHS.first->getLocStart());
994 }
995};
Jordan Rose20441c52012-09-28 22:29:02 +0000996}
Jordan Rose58b6bdc2012-09-28 22:21:30 +0000997
Jordan Rosec0e44452012-10-29 17:46:47 +0000998static bool isInLoop(const ASTContext &Ctx, const ParentMap &PM,
999 const Stmt *S) {
Jordan Roseb5cd1222012-10-11 16:10:19 +00001000 assert(S);
1001
1002 do {
1003 switch (S->getStmtClass()) {
Jordan Roseb5cd1222012-10-11 16:10:19 +00001004 case Stmt::ForStmtClass:
1005 case Stmt::WhileStmtClass:
1006 case Stmt::CXXForRangeStmtClass:
1007 case Stmt::ObjCForCollectionStmtClass:
1008 return true;
Jordan Rosec0e44452012-10-29 17:46:47 +00001009 case Stmt::DoStmtClass: {
1010 const Expr *Cond = cast<DoStmt>(S)->getCond();
1011 llvm::APSInt Val;
1012 if (!Cond->EvaluateAsInt(Val, Ctx))
1013 return true;
1014 return Val.getBoolValue();
1015 }
Jordan Roseb5cd1222012-10-11 16:10:19 +00001016 default:
1017 break;
1018 }
1019 } while ((S = PM.getParent(S)));
1020
1021 return false;
1022}
1023
Jordan Rose58b6bdc2012-09-28 22:21:30 +00001024
1025static void diagnoseRepeatedUseOfWeak(Sema &S,
1026 const sema::FunctionScopeInfo *CurFn,
Jordan Roseb5cd1222012-10-11 16:10:19 +00001027 const Decl *D,
1028 const ParentMap &PM) {
Jordan Rose58b6bdc2012-09-28 22:21:30 +00001029 typedef sema::FunctionScopeInfo::WeakObjectProfileTy WeakObjectProfileTy;
1030 typedef sema::FunctionScopeInfo::WeakObjectUseMap WeakObjectUseMap;
1031 typedef sema::FunctionScopeInfo::WeakUseVector WeakUseVector;
1032
Jordan Rosec0e44452012-10-29 17:46:47 +00001033 ASTContext &Ctx = S.getASTContext();
1034
Jordan Rose58b6bdc2012-09-28 22:21:30 +00001035 const WeakObjectUseMap &WeakMap = CurFn->getWeakObjectUses();
1036
1037 // Extract all weak objects that are referenced more than once.
1038 SmallVector<StmtUsesPair, 8> UsesByStmt;
1039 for (WeakObjectUseMap::const_iterator I = WeakMap.begin(), E = WeakMap.end();
1040 I != E; ++I) {
1041 const WeakUseVector &Uses = I->second;
Jordan Rose58b6bdc2012-09-28 22:21:30 +00001042
1043 // Find the first read of the weak object.
1044 WeakUseVector::const_iterator UI = Uses.begin(), UE = Uses.end();
1045 for ( ; UI != UE; ++UI) {
1046 if (UI->isUnsafe())
1047 break;
1048 }
1049
1050 // If there were only writes to this object, don't warn.
1051 if (UI == UE)
1052 continue;
1053
Jordan Roseb5cd1222012-10-11 16:10:19 +00001054 // If there was only one read, followed by any number of writes, and the
Jordan Rosec0e44452012-10-29 17:46:47 +00001055 // read is not within a loop, don't warn. Additionally, don't warn in a
1056 // loop if the base object is a local variable -- local variables are often
1057 // changed in loops.
Jordan Roseb5cd1222012-10-11 16:10:19 +00001058 if (UI == Uses.begin()) {
1059 WeakUseVector::const_iterator UI2 = UI;
1060 for (++UI2; UI2 != UE; ++UI2)
1061 if (UI2->isUnsafe())
1062 break;
1063
Jordan Rosec0e44452012-10-29 17:46:47 +00001064 if (UI2 == UE) {
1065 if (!isInLoop(Ctx, PM, UI->getUseExpr()))
Jordan Roseb5cd1222012-10-11 16:10:19 +00001066 continue;
Jordan Rosec0e44452012-10-29 17:46:47 +00001067
1068 const WeakObjectProfileTy &Profile = I->first;
1069 if (!Profile.isExactProfile())
1070 continue;
1071
1072 const NamedDecl *Base = Profile.getBase();
1073 if (!Base)
1074 Base = Profile.getProperty();
1075 assert(Base && "A profile always has a base or property.");
1076
1077 if (const VarDecl *BaseVar = dyn_cast<VarDecl>(Base))
1078 if (BaseVar->hasLocalStorage() && !isa<ParmVarDecl>(Base))
1079 continue;
1080 }
Jordan Roseb5cd1222012-10-11 16:10:19 +00001081 }
1082
Jordan Rose58b6bdc2012-09-28 22:21:30 +00001083 UsesByStmt.push_back(StmtUsesPair(UI->getUseExpr(), I));
1084 }
1085
1086 if (UsesByStmt.empty())
1087 return;
1088
1089 // Sort by first use so that we emit the warnings in a deterministic order.
1090 std::sort(UsesByStmt.begin(), UsesByStmt.end(),
Jordan Rose20441c52012-09-28 22:29:02 +00001091 StmtUseSorter(S.getSourceManager()));
Jordan Rose58b6bdc2012-09-28 22:21:30 +00001092
1093 // Classify the current code body for better warning text.
1094 // This enum should stay in sync with the cases in
1095 // warn_arc_repeated_use_of_weak and warn_arc_possible_repeated_use_of_weak.
1096 // FIXME: Should we use a common classification enum and the same set of
1097 // possibilities all throughout Sema?
1098 enum {
1099 Function,
1100 Method,
1101 Block,
1102 Lambda
1103 } FunctionKind;
1104
1105 if (isa<sema::BlockScopeInfo>(CurFn))
1106 FunctionKind = Block;
1107 else if (isa<sema::LambdaScopeInfo>(CurFn))
1108 FunctionKind = Lambda;
1109 else if (isa<ObjCMethodDecl>(D))
1110 FunctionKind = Method;
1111 else
1112 FunctionKind = Function;
1113
1114 // Iterate through the sorted problems and emit warnings for each.
1115 for (SmallVectorImpl<StmtUsesPair>::const_iterator I = UsesByStmt.begin(),
1116 E = UsesByStmt.end();
1117 I != E; ++I) {
1118 const Stmt *FirstRead = I->first;
1119 const WeakObjectProfileTy &Key = I->second->first;
1120 const WeakUseVector &Uses = I->second->second;
1121
Jordan Rose7a270482012-09-28 22:21:35 +00001122 // For complicated expressions like 'a.b.c' and 'x.b.c', WeakObjectProfileTy
1123 // may not contain enough information to determine that these are different
1124 // properties. We can only be 100% sure of a repeated use in certain cases,
1125 // and we adjust the diagnostic kind accordingly so that the less certain
1126 // case can be turned off if it is too noisy.
Jordan Rose58b6bdc2012-09-28 22:21:30 +00001127 unsigned DiagKind;
1128 if (Key.isExactProfile())
1129 DiagKind = diag::warn_arc_repeated_use_of_weak;
1130 else
1131 DiagKind = diag::warn_arc_possible_repeated_use_of_weak;
1132
Jordan Rose7a270482012-09-28 22:21:35 +00001133 // Classify the weak object being accessed for better warning text.
1134 // This enum should stay in sync with the cases in
1135 // warn_arc_repeated_use_of_weak and warn_arc_possible_repeated_use_of_weak.
1136 enum {
1137 Variable,
1138 Property,
1139 ImplicitProperty,
1140 Ivar
1141 } ObjectKind;
1142
1143 const NamedDecl *D = Key.getProperty();
1144 if (isa<VarDecl>(D))
1145 ObjectKind = Variable;
1146 else if (isa<ObjCPropertyDecl>(D))
1147 ObjectKind = Property;
1148 else if (isa<ObjCMethodDecl>(D))
1149 ObjectKind = ImplicitProperty;
1150 else if (isa<ObjCIvarDecl>(D))
1151 ObjectKind = Ivar;
1152 else
1153 llvm_unreachable("Unexpected weak object kind!");
1154
Jordan Rose58b6bdc2012-09-28 22:21:30 +00001155 // Show the first time the object was read.
1156 S.Diag(FirstRead->getLocStart(), DiagKind)
Joerg Sonnenberger73484542013-06-26 21:31:47 +00001157 << int(ObjectKind) << D << int(FunctionKind)
Jordan Rose58b6bdc2012-09-28 22:21:30 +00001158 << FirstRead->getSourceRange();
1159
1160 // Print all the other accesses as notes.
1161 for (WeakUseVector::const_iterator UI = Uses.begin(), UE = Uses.end();
1162 UI != UE; ++UI) {
1163 if (UI->getUseExpr() == FirstRead)
1164 continue;
1165 S.Diag(UI->getUseExpr()->getLocStart(),
1166 diag::note_arc_weak_also_accessed_here)
1167 << UI->getUseExpr()->getSourceRange();
1168 }
1169 }
1170}
1171
1172
1173namespace {
Ted Kremenek94b1b4d2011-01-21 19:41:41 +00001174struct SLocSort {
Ted Kremenekf7bafc72011-03-15 04:57:38 +00001175 bool operator()(const UninitUse &a, const UninitUse &b) {
Richard Smith2815e1a2012-05-25 02:17:09 +00001176 // Prefer a more confident report over a less confident one.
1177 if (a.getKind() != b.getKind())
1178 return a.getKind() > b.getKind();
1179 SourceLocation aLoc = a.getUser()->getLocStart();
1180 SourceLocation bLoc = b.getUser()->getLocStart();
Ted Kremenek94b1b4d2011-01-21 19:41:41 +00001181 return aLoc.getRawEncoding() < bLoc.getRawEncoding();
1182 }
1183};
1184
Ted Kremenek610068c2011-01-15 02:58:47 +00001185class UninitValsDiagReporter : public UninitVariablesHandler {
1186 Sema &S;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001187 typedef SmallVector<UninitUse, 2> UsesVec;
Benjamin Kramere1039792013-06-29 17:52:13 +00001188 typedef llvm::PointerIntPair<UsesVec *, 1, bool> MappedType;
Enea Zaffanella3285c782013-02-15 20:09:55 +00001189 // Prefer using MapVector to DenseMap, so that iteration order will be
1190 // the same as insertion order. This is needed to obtain a deterministic
1191 // order of diagnostics when calling flushDiagnostics().
1192 typedef llvm::MapVector<const VarDecl *, MappedType> UsesMap;
Ted Kremenek94b1b4d2011-01-21 19:41:41 +00001193 UsesMap *uses;
1194
Ted Kremenek610068c2011-01-15 02:58:47 +00001195public:
Ted Kremenek94b1b4d2011-01-21 19:41:41 +00001196 UninitValsDiagReporter(Sema &S) : S(S), uses(0) {}
1197 ~UninitValsDiagReporter() {
1198 flushDiagnostics();
1199 }
Ted Kremenek9e761722011-10-13 18:50:06 +00001200
Enea Zaffanella3285c782013-02-15 20:09:55 +00001201 MappedType &getUses(const VarDecl *vd) {
Ted Kremenek94b1b4d2011-01-21 19:41:41 +00001202 if (!uses)
1203 uses = new UsesMap();
Ted Kremenek9e761722011-10-13 18:50:06 +00001204
Enea Zaffanella3285c782013-02-15 20:09:55 +00001205 MappedType &V = (*uses)[vd];
Benjamin Kramere1039792013-06-29 17:52:13 +00001206 if (!V.getPointer())
1207 V.setPointer(new UsesVec());
Ted Kremenek94b1b4d2011-01-21 19:41:41 +00001208
Ted Kremenek9e761722011-10-13 18:50:06 +00001209 return V;
1210 }
1211
Richard Smith2815e1a2012-05-25 02:17:09 +00001212 void handleUseOfUninitVariable(const VarDecl *vd, const UninitUse &use) {
Benjamin Kramere1039792013-06-29 17:52:13 +00001213 getUses(vd).getPointer()->push_back(use);
Ted Kremenek9e761722011-10-13 18:50:06 +00001214 }
1215
1216 void handleSelfInit(const VarDecl *vd) {
Benjamin Kramere1039792013-06-29 17:52:13 +00001217 getUses(vd).setInt(true);
Ted Kremenek94b1b4d2011-01-21 19:41:41 +00001218 }
1219
1220 void flushDiagnostics() {
1221 if (!uses)
1222 return;
Enea Zaffanella3285c782013-02-15 20:09:55 +00001223
Ted Kremenek94b1b4d2011-01-21 19:41:41 +00001224 for (UsesMap::iterator i = uses->begin(), e = uses->end(); i != e; ++i) {
1225 const VarDecl *vd = i->first;
Enea Zaffanella3285c782013-02-15 20:09:55 +00001226 const MappedType &V = i->second;
Ted Kremenek609e3172011-02-02 23:35:53 +00001227
Benjamin Kramere1039792013-06-29 17:52:13 +00001228 UsesVec *vec = V.getPointer();
1229 bool hasSelfInit = V.getInt();
Ted Kremenek9e761722011-10-13 18:50:06 +00001230
1231 // Specially handle the case where we have uses of an uninitialized
1232 // variable, but the root cause is an idiomatic self-init. We want
1233 // to report the diagnostic at the self-init since that is the root cause.
Matt Beaumont-Gay0d381812011-10-19 18:53:03 +00001234 if (!vec->empty() && hasSelfInit && hasAlwaysUninitializedUse(vec))
Richard Smith2815e1a2012-05-25 02:17:09 +00001235 DiagnoseUninitializedUse(S, vd,
1236 UninitUse(vd->getInit()->IgnoreParenCasts(),
1237 /* isAlwaysUninit */ true),
Matt Beaumont-Gay0d381812011-10-19 18:53:03 +00001238 /* alwaysReportSelfInit */ true);
Ted Kremenek9e761722011-10-13 18:50:06 +00001239 else {
1240 // Sort the uses by their SourceLocations. While not strictly
1241 // guaranteed to produce them in line/column order, this will provide
1242 // a stable ordering.
1243 std::sort(vec->begin(), vec->end(), SLocSort());
1244
1245 for (UsesVec::iterator vi = vec->begin(), ve = vec->end(); vi != ve;
1246 ++vi) {
Richard Smith2815e1a2012-05-25 02:17:09 +00001247 // If we have self-init, downgrade all uses to 'may be uninitialized'.
1248 UninitUse Use = hasSelfInit ? UninitUse(vi->getUser(), false) : *vi;
1249
1250 if (DiagnoseUninitializedUse(S, vd, Use))
Ted Kremenek9e761722011-10-13 18:50:06 +00001251 // Skip further diagnostics for this variable. We try to warn only
1252 // on the first point at which a variable is used uninitialized.
1253 break;
1254 }
Chandler Carruth64fb9592011-04-05 18:18:08 +00001255 }
Ted Kremenek9e761722011-10-13 18:50:06 +00001256
1257 // Release the uses vector.
Ted Kremenek94b1b4d2011-01-21 19:41:41 +00001258 delete vec;
1259 }
1260 delete uses;
Ted Kremenek610068c2011-01-15 02:58:47 +00001261 }
Matt Beaumont-Gay0d381812011-10-19 18:53:03 +00001262
1263private:
1264 static bool hasAlwaysUninitializedUse(const UsesVec* vec) {
1265 for (UsesVec::const_iterator i = vec->begin(), e = vec->end(); i != e; ++i) {
Richard Smith8a1fdfc2013-09-12 18:49:10 +00001266 if (i->getKind() == UninitUse::Always ||
1267 i->getKind() == UninitUse::AfterCall ||
1268 i->getKind() == UninitUse::AfterDecl) {
Matt Beaumont-Gay0d381812011-10-19 18:53:03 +00001269 return true;
1270 }
1271 }
1272 return false;
1273}
Ted Kremenek610068c2011-01-15 02:58:47 +00001274};
1275}
1276
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001277namespace clang {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001278namespace {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001279typedef SmallVector<PartialDiagnosticAt, 1> OptionalNotes;
Richard Smith2e515622012-02-03 04:45:26 +00001280typedef std::pair<PartialDiagnosticAt, OptionalNotes> DelayedDiag;
Benjamin Kramerecafd302012-03-26 14:05:40 +00001281typedef std::list<DelayedDiag> DiagList;
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001282
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001283struct SortDiagBySourceLocation {
Benjamin Kramerecafd302012-03-26 14:05:40 +00001284 SourceManager &SM;
1285 SortDiagBySourceLocation(SourceManager &SM) : SM(SM) {}
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001286
1287 bool operator()(const DelayedDiag &left, const DelayedDiag &right) {
1288 // Although this call will be slow, this is only called when outputting
1289 // multiple warnings.
Benjamin Kramerecafd302012-03-26 14:05:40 +00001290 return SM.isBeforeInTranslationUnit(left.first.first, right.first.first);
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001291 }
1292};
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001293}}
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001294
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001295//===----------------------------------------------------------------------===//
1296// -Wthread-safety
1297//===----------------------------------------------------------------------===//
1298namespace clang {
1299namespace thread_safety {
David Blaikie99ba9e32011-12-20 02:48:34 +00001300namespace {
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001301class ThreadSafetyReporter : public clang::thread_safety::ThreadSafetyHandler {
1302 Sema &S;
1303 DiagList Warnings;
Richard Smith2e515622012-02-03 04:45:26 +00001304 SourceLocation FunLocation, FunEndLocation;
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001305
1306 // Helper functions
1307 void warnLockMismatch(unsigned DiagID, Name LockName, SourceLocation Loc) {
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +00001308 // Gracefully handle rare cases when the analysis can't get a more
1309 // precise source location.
1310 if (!Loc.isValid())
1311 Loc = FunLocation;
Richard Smith2e515622012-02-03 04:45:26 +00001312 PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID) << LockName);
1313 Warnings.push_back(DelayedDiag(Warning, OptionalNotes()));
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001314 }
1315
1316 public:
Richard Smith2e515622012-02-03 04:45:26 +00001317 ThreadSafetyReporter(Sema &S, SourceLocation FL, SourceLocation FEL)
1318 : S(S), FunLocation(FL), FunEndLocation(FEL) {}
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001319
1320 /// \brief Emit all buffered diagnostics in order of sourcelocation.
1321 /// We need to output diagnostics produced while iterating through
1322 /// the lockset in deterministic order, so this function orders diagnostics
1323 /// and outputs them.
1324 void emitDiagnostics() {
Benjamin Kramerecafd302012-03-26 14:05:40 +00001325 Warnings.sort(SortDiagBySourceLocation(S.getSourceManager()));
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001326 for (DiagList::iterator I = Warnings.begin(), E = Warnings.end();
Richard Smith2e515622012-02-03 04:45:26 +00001327 I != E; ++I) {
1328 S.Diag(I->first.first, I->first.second);
1329 const OptionalNotes &Notes = I->second;
1330 for (unsigned NoteI = 0, NoteN = Notes.size(); NoteI != NoteN; ++NoteI)
1331 S.Diag(Notes[NoteI].first, Notes[NoteI].second);
1332 }
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001333 }
1334
Caitlin Sadowski99107eb2011-09-09 16:21:55 +00001335 void handleInvalidLockExp(SourceLocation Loc) {
Richard Smith2e515622012-02-03 04:45:26 +00001336 PartialDiagnosticAt Warning(Loc,
1337 S.PDiag(diag::warn_cannot_resolve_lock) << Loc);
1338 Warnings.push_back(DelayedDiag(Warning, OptionalNotes()));
Caitlin Sadowski99107eb2011-09-09 16:21:55 +00001339 }
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001340 void handleUnmatchedUnlock(Name LockName, SourceLocation Loc) {
1341 warnLockMismatch(diag::warn_unlock_but_no_lock, LockName, Loc);
1342 }
1343
1344 void handleDoubleLock(Name LockName, SourceLocation Loc) {
1345 warnLockMismatch(diag::warn_double_lock, LockName, Loc);
1346 }
1347
Richard Smith2e515622012-02-03 04:45:26 +00001348 void handleMutexHeldEndOfScope(Name LockName, SourceLocation LocLocked,
1349 SourceLocation LocEndOfScope,
Caitlin Sadowski4e4bc752011-09-15 17:25:19 +00001350 LockErrorKind LEK){
1351 unsigned DiagID = 0;
1352 switch (LEK) {
1353 case LEK_LockedSomePredecessors:
Richard Smith2e515622012-02-03 04:45:26 +00001354 DiagID = diag::warn_lock_some_predecessors;
Caitlin Sadowski4e4bc752011-09-15 17:25:19 +00001355 break;
1356 case LEK_LockedSomeLoopIterations:
1357 DiagID = diag::warn_expecting_lock_held_on_loop;
1358 break;
1359 case LEK_LockedAtEndOfFunction:
1360 DiagID = diag::warn_no_unlock;
1361 break;
DeLesley Hutchins879a4332012-07-02 22:16:54 +00001362 case LEK_NotLockedAtEndOfFunction:
1363 DiagID = diag::warn_expecting_locked;
1364 break;
Caitlin Sadowski4e4bc752011-09-15 17:25:19 +00001365 }
Richard Smith2e515622012-02-03 04:45:26 +00001366 if (LocEndOfScope.isInvalid())
1367 LocEndOfScope = FunEndLocation;
1368
1369 PartialDiagnosticAt Warning(LocEndOfScope, S.PDiag(DiagID) << LockName);
DeLesley Hutchins56968842013-04-08 20:11:11 +00001370 if (LocLocked.isValid()) {
1371 PartialDiagnosticAt Note(LocLocked, S.PDiag(diag::note_locked_here));
1372 Warnings.push_back(DelayedDiag(Warning, OptionalNotes(1, Note)));
1373 return;
1374 }
1375 Warnings.push_back(DelayedDiag(Warning, OptionalNotes()));
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001376 }
1377
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001378
1379 void handleExclusiveAndShared(Name LockName, SourceLocation Loc1,
1380 SourceLocation Loc2) {
Richard Smith2e515622012-02-03 04:45:26 +00001381 PartialDiagnosticAt Warning(
1382 Loc1, S.PDiag(diag::warn_lock_exclusive_and_shared) << LockName);
1383 PartialDiagnosticAt Note(
1384 Loc2, S.PDiag(diag::note_lock_exclusive_and_shared) << LockName);
1385 Warnings.push_back(DelayedDiag(Warning, OptionalNotes(1, Note)));
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001386 }
1387
1388 void handleNoMutexHeld(const NamedDecl *D, ProtectedOperationKind POK,
1389 AccessKind AK, SourceLocation Loc) {
Caitlin Sadowskidf8327c2011-09-14 20:09:09 +00001390 assert((POK == POK_VarAccess || POK == POK_VarDereference)
1391 && "Only works for variables");
1392 unsigned DiagID = POK == POK_VarAccess?
1393 diag::warn_variable_requires_any_lock:
1394 diag::warn_var_deref_requires_any_lock;
Richard Smith2e515622012-02-03 04:45:26 +00001395 PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID)
DeLesley Hutchins5b280f22012-09-19 19:18:29 +00001396 << D->getNameAsString() << getLockKindFromAccessKind(AK));
Richard Smith2e515622012-02-03 04:45:26 +00001397 Warnings.push_back(DelayedDiag(Warning, OptionalNotes()));
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001398 }
1399
1400 void handleMutexNotHeld(const NamedDecl *D, ProtectedOperationKind POK,
DeLesley Hutchins3f0ec522012-09-10 19:58:23 +00001401 Name LockName, LockKind LK, SourceLocation Loc,
1402 Name *PossibleMatch) {
Caitlin Sadowskie87158d2011-09-13 18:01:58 +00001403 unsigned DiagID = 0;
DeLesley Hutchins3f0ec522012-09-10 19:58:23 +00001404 if (PossibleMatch) {
1405 switch (POK) {
1406 case POK_VarAccess:
1407 DiagID = diag::warn_variable_requires_lock_precise;
1408 break;
1409 case POK_VarDereference:
1410 DiagID = diag::warn_var_deref_requires_lock_precise;
1411 break;
1412 case POK_FunctionCall:
1413 DiagID = diag::warn_fun_requires_lock_precise;
1414 break;
1415 }
1416 PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID)
DeLesley Hutchins5b280f22012-09-19 19:18:29 +00001417 << D->getNameAsString() << LockName << LK);
DeLesley Hutchins3f0ec522012-09-10 19:58:23 +00001418 PartialDiagnosticAt Note(Loc, S.PDiag(diag::note_found_mutex_near_match)
1419 << *PossibleMatch);
1420 Warnings.push_back(DelayedDiag(Warning, OptionalNotes(1, Note)));
1421 } else {
1422 switch (POK) {
1423 case POK_VarAccess:
1424 DiagID = diag::warn_variable_requires_lock;
1425 break;
1426 case POK_VarDereference:
1427 DiagID = diag::warn_var_deref_requires_lock;
1428 break;
1429 case POK_FunctionCall:
1430 DiagID = diag::warn_fun_requires_lock;
1431 break;
1432 }
1433 PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID)
DeLesley Hutchins5b280f22012-09-19 19:18:29 +00001434 << D->getNameAsString() << LockName << LK);
DeLesley Hutchins3f0ec522012-09-10 19:58:23 +00001435 Warnings.push_back(DelayedDiag(Warning, OptionalNotes()));
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001436 }
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001437 }
1438
1439 void handleFunExcludesLock(Name FunName, Name LockName, SourceLocation Loc) {
Richard Smith2e515622012-02-03 04:45:26 +00001440 PartialDiagnosticAt Warning(Loc,
1441 S.PDiag(diag::warn_fun_excludes_mutex) << FunName << LockName);
1442 Warnings.push_back(DelayedDiag(Warning, OptionalNotes()));
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001443 }
1444};
1445}
1446}
David Blaikie99ba9e32011-12-20 02:48:34 +00001447}
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001448
Ted Kremenek610068c2011-01-15 02:58:47 +00001449//===----------------------------------------------------------------------===//
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001450// -Wconsumed
1451//===----------------------------------------------------------------------===//
1452
1453namespace clang {
1454namespace consumed {
1455namespace {
1456class ConsumedWarningsHandler : public ConsumedWarningsHandlerBase {
1457
1458 Sema &S;
1459 DiagList Warnings;
1460
1461public:
1462
1463 ConsumedWarningsHandler(Sema &S) : S(S) {}
1464
1465 void emitDiagnostics() {
1466 Warnings.sort(SortDiagBySourceLocation(S.getSourceManager()));
1467
1468 for (DiagList::iterator I = Warnings.begin(), E = Warnings.end();
1469 I != E; ++I) {
1470
1471 const OptionalNotes &Notes = I->second;
1472 S.Diag(I->first.first, I->first.second);
1473
1474 for (unsigned NoteI = 0, NoteN = Notes.size(); NoteI != NoteN; ++NoteI) {
1475 S.Diag(Notes[NoteI].first, Notes[NoteI].second);
1476 }
1477 }
1478 }
1479
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001480 void warnReturnTypestateForUnconsumableType(SourceLocation Loc,
1481 StringRef TypeName) {
1482 PartialDiagnosticAt Warning(Loc, S.PDiag(
1483 diag::warn_return_typestate_for_unconsumable_type) << TypeName);
1484
1485 Warnings.push_back(DelayedDiag(Warning, OptionalNotes()));
1486 }
1487
1488 void warnReturnTypestateMismatch(SourceLocation Loc, StringRef ExpectedState,
1489 StringRef ObservedState) {
1490
1491 PartialDiagnosticAt Warning(Loc, S.PDiag(
1492 diag::warn_return_typestate_mismatch) << ExpectedState << ObservedState);
1493
1494 Warnings.push_back(DelayedDiag(Warning, OptionalNotes()));
1495 }
1496
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001497 void warnUnnecessaryTest(StringRef VariableName, StringRef VariableState,
1498 SourceLocation Loc) {
1499
1500 PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_unnecessary_test) <<
1501 VariableName << VariableState);
1502
1503 Warnings.push_back(DelayedDiag(Warning, OptionalNotes()));
1504 }
1505
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001506 void warnUseOfTempWhileConsumed(StringRef MethodName, SourceLocation Loc) {
1507
1508 PartialDiagnosticAt Warning(Loc, S.PDiag(
1509 diag::warn_use_of_temp_while_consumed) << MethodName);
1510
1511 Warnings.push_back(DelayedDiag(Warning, OptionalNotes()));
1512 }
1513
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001514 void warnUseOfTempInUnknownState(StringRef MethodName, SourceLocation Loc) {
1515
1516 PartialDiagnosticAt Warning(Loc, S.PDiag(
1517 diag::warn_use_of_temp_in_unknown_state) << MethodName);
1518
1519 Warnings.push_back(DelayedDiag(Warning, OptionalNotes()));
1520 }
1521
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001522 void warnUseWhileConsumed(StringRef MethodName, StringRef VariableName,
1523 SourceLocation Loc) {
1524
1525 PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_use_while_consumed) <<
1526 MethodName << VariableName);
1527
1528 Warnings.push_back(DelayedDiag(Warning, OptionalNotes()));
1529 }
1530
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001531 void warnUseInUnknownState(StringRef MethodName, StringRef VariableName,
1532 SourceLocation Loc) {
1533
1534 PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_use_in_unknown_state) <<
1535 MethodName << VariableName);
1536
1537 Warnings.push_back(DelayedDiag(Warning, OptionalNotes()));
1538 }
1539};
1540}}}
1541
1542//===----------------------------------------------------------------------===//
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001543// AnalysisBasedWarnings - Worker object used by Sema to execute analysis-based
1544// warnings on a function, method, or block.
1545//===----------------------------------------------------------------------===//
1546
Ted Kremenekd064fdc2010-03-23 00:13:23 +00001547clang::sema::AnalysisBasedWarnings::Policy::Policy() {
1548 enableCheckFallThrough = 1;
1549 enableCheckUnreachable = 0;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +00001550 enableThreadSafetyAnalysis = 0;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001551 enableConsumedAnalysis = 0;
Ted Kremenekd064fdc2010-03-23 00:13:23 +00001552}
1553
Chandler Carruth5d989942011-07-06 16:21:37 +00001554clang::sema::AnalysisBasedWarnings::AnalysisBasedWarnings(Sema &s)
1555 : S(s),
1556 NumFunctionsAnalyzed(0),
Benjamin Kramer54cf3412011-07-08 20:38:53 +00001557 NumFunctionsWithBadCFGs(0),
Chandler Carruth5d989942011-07-06 16:21:37 +00001558 NumCFGBlocks(0),
Benjamin Kramer54cf3412011-07-08 20:38:53 +00001559 MaxCFGBlocksPerFunction(0),
1560 NumUninitAnalysisFunctions(0),
1561 NumUninitAnalysisVariables(0),
1562 MaxUninitAnalysisVariablesPerFunction(0),
1563 NumUninitAnalysisBlockVisits(0),
1564 MaxUninitAnalysisBlockVisitsPerFunction(0) {
David Blaikied6471f72011-09-25 23:23:43 +00001565 DiagnosticsEngine &D = S.getDiagnostics();
Ted Kremenekd064fdc2010-03-23 00:13:23 +00001566 DefaultPolicy.enableCheckUnreachable = (unsigned)
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001567 (D.getDiagnosticLevel(diag::warn_unreachable, SourceLocation()) !=
David Blaikied6471f72011-09-25 23:23:43 +00001568 DiagnosticsEngine::Ignored);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +00001569 DefaultPolicy.enableThreadSafetyAnalysis = (unsigned)
1570 (D.getDiagnosticLevel(diag::warn_double_lock, SourceLocation()) !=
David Blaikied6471f72011-09-25 23:23:43 +00001571 DiagnosticsEngine::Ignored);
DeLesley Hutchins5fdd2072013-08-22 20:44:47 +00001572 DefaultPolicy.enableConsumedAnalysis = (unsigned)
1573 (D.getDiagnosticLevel(diag::warn_use_while_consumed, SourceLocation()) !=
1574 DiagnosticsEngine::Ignored);
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001575}
1576
Ted Kremenek351ba912011-02-23 01:52:04 +00001577static void flushDiagnostics(Sema &S, sema::FunctionScopeInfo *fscope) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001578 for (SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator
Ted Kremenek351ba912011-02-23 01:52:04 +00001579 i = fscope->PossiblyUnreachableDiags.begin(),
1580 e = fscope->PossiblyUnreachableDiags.end();
1581 i != e; ++i) {
1582 const sema::PossiblyUnreachableDiag &D = *i;
1583 S.Diag(D.Loc, D.PD);
1584 }
1585}
1586
Ted Kremenekd064fdc2010-03-23 00:13:23 +00001587void clang::sema::
1588AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P,
Ted Kremenek283a3582011-02-23 01:51:53 +00001589 sema::FunctionScopeInfo *fscope,
Ted Kremenek3ed6fc02011-02-23 01:51:48 +00001590 const Decl *D, const BlockExpr *blkExpr) {
Ted Kremenekd068aab2010-03-20 21:11:09 +00001591
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001592 // We avoid doing analysis-based warnings when there are errors for
1593 // two reasons:
1594 // (1) The CFGs often can't be constructed (if the body is invalid), so
1595 // don't bother trying.
1596 // (2) The code already has problems; running the analysis just takes more
1597 // time.
David Blaikied6471f72011-09-25 23:23:43 +00001598 DiagnosticsEngine &Diags = S.getDiagnostics();
Ted Kremenek99e81922010-04-30 21:49:25 +00001599
Ted Kremenekd064fdc2010-03-23 00:13:23 +00001600 // Do not do any analysis for declarations in system headers if we are
1601 // going to just ignore them.
Ted Kremenek99e81922010-04-30 21:49:25 +00001602 if (Diags.getSuppressSystemWarnings() &&
Ted Kremenekd064fdc2010-03-23 00:13:23 +00001603 S.SourceMgr.isInSystemHeader(D->getLocation()))
1604 return;
1605
John McCalle0054f62010-08-25 05:56:39 +00001606 // For code in dependent contexts, we'll do this at instantiation time.
David Blaikie23661d32012-01-24 04:51:48 +00001607 if (cast<DeclContext>(D)->isDependentContext())
1608 return;
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001609
DeLesley Hutchins12f37e42012-12-07 22:53:48 +00001610 if (Diags.hasUncompilableErrorOccurred() || Diags.hasFatalErrorOccurred()) {
Ted Kremenek351ba912011-02-23 01:52:04 +00001611 // Flush out any possibly unreachable diagnostics.
1612 flushDiagnostics(S, fscope);
1613 return;
1614 }
1615
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001616 const Stmt *Body = D->getBody();
1617 assert(Body);
1618
Jordy Rosed2001872012-04-28 01:58:08 +00001619 AnalysisDeclContext AC(/* AnalysisDeclContextManager */ 0, D);
Ted Kremenekbc5cb8a2011-07-21 05:22:47 +00001620
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001621 // Don't generate EH edges for CallExprs as we'd like to avoid the n^2
Benjamin Kramere5753592013-09-09 14:48:42 +00001622 // explosion for destructors that can result and the compile time hit.
Ted Kremenekbc5cb8a2011-07-21 05:22:47 +00001623 AC.getCFGBuildOptions().PruneTriviallyFalseEdges = true;
1624 AC.getCFGBuildOptions().AddEHEdges = false;
1625 AC.getCFGBuildOptions().AddInitializers = true;
1626 AC.getCFGBuildOptions().AddImplicitDtors = true;
Jordan Rosefaadf482012-09-05 23:11:06 +00001627 AC.getCFGBuildOptions().AddTemporaryDtors = true;
1628
Ted Kremenek0c8e5a02011-07-19 14:18:48 +00001629 // Force that certain expressions appear as CFGElements in the CFG. This
1630 // is used to speed up various analyses.
1631 // FIXME: This isn't the right factoring. This is here for initial
1632 // prototyping, but we need a way for analyses to say what expressions they
1633 // expect to always be CFGElements and then fill in the BuildOptions
1634 // appropriately. This is essentially a layering violation.
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001635 if (P.enableCheckUnreachable || P.enableThreadSafetyAnalysis ||
1636 P.enableConsumedAnalysis) {
DeLesley Hutchins1fa3c062011-12-08 20:23:06 +00001637 // Unreachable code analysis and thread safety require a linearized CFG.
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +00001638 AC.getCFGBuildOptions().setAllAlwaysAdd();
1639 }
1640 else {
1641 AC.getCFGBuildOptions()
1642 .setAlwaysAdd(Stmt::BinaryOperatorClass)
Richard Smith6cfa78f2012-07-17 01:27:33 +00001643 .setAlwaysAdd(Stmt::CompoundAssignOperatorClass)
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +00001644 .setAlwaysAdd(Stmt::BlockExprClass)
1645 .setAlwaysAdd(Stmt::CStyleCastExprClass)
1646 .setAlwaysAdd(Stmt::DeclRefExprClass)
1647 .setAlwaysAdd(Stmt::ImplicitCastExprClass)
Richard Smithe0d3b4c2012-05-03 18:27:39 +00001648 .setAlwaysAdd(Stmt::UnaryOperatorClass)
1649 .setAlwaysAdd(Stmt::AttributedStmtClass);
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +00001650 }
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001651
Ted Kremenekbc5cb8a2011-07-21 05:22:47 +00001652 // Construct the analysis context with the specified CFG build options.
1653
Ted Kremenek351ba912011-02-23 01:52:04 +00001654 // Emit delayed diagnostics.
David Blaikie23661d32012-01-24 04:51:48 +00001655 if (!fscope->PossiblyUnreachableDiags.empty()) {
Ted Kremenek351ba912011-02-23 01:52:04 +00001656 bool analyzed = false;
Ted Kremenek0d28d362011-03-10 03:50:34 +00001657
1658 // Register the expressions with the CFGBuilder.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001659 for (SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator
Ted Kremenek0d28d362011-03-10 03:50:34 +00001660 i = fscope->PossiblyUnreachableDiags.begin(),
1661 e = fscope->PossiblyUnreachableDiags.end();
1662 i != e; ++i) {
1663 if (const Stmt *stmt = i->stmt)
1664 AC.registerForcedBlockExpression(stmt);
1665 }
1666
1667 if (AC.getCFG()) {
1668 analyzed = true;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001669 for (SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator
Ted Kremenek0d28d362011-03-10 03:50:34 +00001670 i = fscope->PossiblyUnreachableDiags.begin(),
1671 e = fscope->PossiblyUnreachableDiags.end();
1672 i != e; ++i)
1673 {
1674 const sema::PossiblyUnreachableDiag &D = *i;
1675 bool processed = false;
1676 if (const Stmt *stmt = i->stmt) {
1677 const CFGBlock *block = AC.getBlockForRegisteredExpression(stmt);
Eli Friedman71b8fb52012-01-21 01:01:51 +00001678 CFGReverseBlockReachabilityAnalysis *cra =
1679 AC.getCFGReachablityAnalysis();
1680 // FIXME: We should be able to assert that block is non-null, but
1681 // the CFG analysis can skip potentially-evaluated expressions in
1682 // edge cases; see test/Sema/vla-2.c.
1683 if (block && cra) {
Ted Kremenek351ba912011-02-23 01:52:04 +00001684 // Can this block be reached from the entrance?
Ted Kremenek0d28d362011-03-10 03:50:34 +00001685 if (cra->isReachable(&AC.getCFG()->getEntry(), block))
Ted Kremenek351ba912011-02-23 01:52:04 +00001686 S.Diag(D.Loc, D.PD);
Ted Kremenek0d28d362011-03-10 03:50:34 +00001687 processed = true;
Ted Kremenek351ba912011-02-23 01:52:04 +00001688 }
1689 }
Ted Kremenek0d28d362011-03-10 03:50:34 +00001690 if (!processed) {
1691 // Emit the warning anyway if we cannot map to a basic block.
1692 S.Diag(D.Loc, D.PD);
1693 }
Ted Kremenek351ba912011-02-23 01:52:04 +00001694 }
Ted Kremenek0d28d362011-03-10 03:50:34 +00001695 }
Ted Kremenek351ba912011-02-23 01:52:04 +00001696
1697 if (!analyzed)
1698 flushDiagnostics(S, fscope);
1699 }
1700
1701
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001702 // Warning: check missing 'return'
David Blaikie23661d32012-01-24 04:51:48 +00001703 if (P.enableCheckFallThrough) {
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001704 const CheckFallThroughDiagnostics &CD =
1705 (isa<BlockDecl>(D) ? CheckFallThroughDiagnostics::MakeForBlock()
Douglas Gregor793cd1c2012-02-15 16:20:15 +00001706 : (isa<CXXMethodDecl>(D) &&
1707 cast<CXXMethodDecl>(D)->getOverloadedOperator() == OO_Call &&
1708 cast<CXXMethodDecl>(D)->getParent()->isLambda())
1709 ? CheckFallThroughDiagnostics::MakeForLambda()
1710 : CheckFallThroughDiagnostics::MakeForFunction(D));
Ted Kremenek3ed6fc02011-02-23 01:51:48 +00001711 CheckFallThroughForBody(S, D, Body, blkExpr, CD, AC);
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001712 }
1713
1714 // Warning: check for unreachable code
Ted Kremenek5dfee062011-11-30 21:22:09 +00001715 if (P.enableCheckUnreachable) {
1716 // Only check for unreachable code on non-template instantiations.
1717 // Different template instantiations can effectively change the control-flow
1718 // and it is very difficult to prove that a snippet of code in a template
1719 // is unreachable for all instantiations.
Ted Kremenek75df4ee2011-12-01 00:59:17 +00001720 bool isTemplateInstantiation = false;
1721 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
1722 isTemplateInstantiation = Function->isTemplateInstantiation();
1723 if (!isTemplateInstantiation)
Ted Kremenek5dfee062011-11-30 21:22:09 +00001724 CheckUnreachable(S, AC);
1725 }
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001726
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +00001727 // Check for thread safety violations
David Blaikie23661d32012-01-24 04:51:48 +00001728 if (P.enableThreadSafetyAnalysis) {
DeLesley Hutchinsf1ac6372011-10-21 18:10:14 +00001729 SourceLocation FL = AC.getDecl()->getLocation();
Richard Smith2e515622012-02-03 04:45:26 +00001730 SourceLocation FEL = AC.getDecl()->getLocEnd();
1731 thread_safety::ThreadSafetyReporter Reporter(S, FL, FEL);
DeLesley Hutchinsfb4afc22012-12-05 00:06:15 +00001732 if (Diags.getDiagnosticLevel(diag::warn_thread_safety_beta,D->getLocStart())
1733 != DiagnosticsEngine::Ignored)
1734 Reporter.setIssueBetaWarnings(true);
1735
Caitlin Sadowski75f23ae2011-09-09 16:04:02 +00001736 thread_safety::runThreadSafetyAnalysis(AC, Reporter);
1737 Reporter.emitDiagnostics();
1738 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +00001739
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001740 // Check for violations of consumed properties.
1741 if (P.enableConsumedAnalysis) {
1742 consumed::ConsumedWarningsHandler WarningHandler(S);
Reid Kleckner2d84f6b2013-08-12 23:49:39 +00001743 consumed::ConsumedAnalyzer Analyzer(WarningHandler);
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001744 Analyzer.run(AC);
1745 }
1746
Ted Kremeneka8c17a52011-01-25 19:13:48 +00001747 if (Diags.getDiagnosticLevel(diag::warn_uninit_var, D->getLocStart())
David Blaikied6471f72011-09-25 23:23:43 +00001748 != DiagnosticsEngine::Ignored ||
Richard Smith2815e1a2012-05-25 02:17:09 +00001749 Diags.getDiagnosticLevel(diag::warn_sometimes_uninit_var,D->getLocStart())
1750 != DiagnosticsEngine::Ignored ||
Ted Kremenek76709bf2011-03-15 05:22:28 +00001751 Diags.getDiagnosticLevel(diag::warn_maybe_uninit_var, D->getLocStart())
David Blaikied6471f72011-09-25 23:23:43 +00001752 != DiagnosticsEngine::Ignored) {
Ted Kremenekc5e43c12011-03-17 05:29:57 +00001753 if (CFG *cfg = AC.getCFG()) {
Ted Kremenekc21fed32011-01-18 21:18:58 +00001754 UninitValsDiagReporter reporter(S);
Fariborz Jahanian57080fb2011-07-16 18:31:33 +00001755 UninitVariablesAnalysisStats stats;
Benjamin Kramer12efd572011-07-16 20:13:06 +00001756 std::memset(&stats, 0, sizeof(UninitVariablesAnalysisStats));
Ted Kremeneka8c17a52011-01-25 19:13:48 +00001757 runUninitializedVariablesAnalysis(*cast<DeclContext>(D), *cfg, AC,
Chandler Carruth5d989942011-07-06 16:21:37 +00001758 reporter, stats);
1759
1760 if (S.CollectStats && stats.NumVariablesAnalyzed > 0) {
1761 ++NumUninitAnalysisFunctions;
1762 NumUninitAnalysisVariables += stats.NumVariablesAnalyzed;
1763 NumUninitAnalysisBlockVisits += stats.NumBlockVisits;
1764 MaxUninitAnalysisVariablesPerFunction =
1765 std::max(MaxUninitAnalysisVariablesPerFunction,
1766 stats.NumVariablesAnalyzed);
1767 MaxUninitAnalysisBlockVisitsPerFunction =
1768 std::max(MaxUninitAnalysisBlockVisitsPerFunction,
1769 stats.NumBlockVisits);
1770 }
Ted Kremenek610068c2011-01-15 02:58:47 +00001771 }
1772 }
Chandler Carruth5d989942011-07-06 16:21:37 +00001773
Alexander Kornienko19736342012-06-02 01:01:07 +00001774 bool FallThroughDiagFull =
1775 Diags.getDiagnosticLevel(diag::warn_unannotated_fallthrough,
1776 D->getLocStart()) != DiagnosticsEngine::Ignored;
Sean Huntc2f51cf2012-06-15 21:22:05 +00001777 bool FallThroughDiagPerFunction =
1778 Diags.getDiagnosticLevel(diag::warn_unannotated_fallthrough_per_function,
Alexander Kornienko19736342012-06-02 01:01:07 +00001779 D->getLocStart()) != DiagnosticsEngine::Ignored;
Sean Huntc2f51cf2012-06-15 21:22:05 +00001780 if (FallThroughDiagFull || FallThroughDiagPerFunction) {
Alexander Kornienko19736342012-06-02 01:01:07 +00001781 DiagnoseSwitchLabelsFallthrough(S, AC, !FallThroughDiagFull);
Richard Smithe0d3b4c2012-05-03 18:27:39 +00001782 }
1783
Jordan Rose58b6bdc2012-09-28 22:21:30 +00001784 if (S.getLangOpts().ObjCARCWeak &&
1785 Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak,
1786 D->getLocStart()) != DiagnosticsEngine::Ignored)
Jordan Roseb5cd1222012-10-11 16:10:19 +00001787 diagnoseRepeatedUseOfWeak(S, fscope, D, AC.getParentMap());
Jordan Rose58b6bdc2012-09-28 22:21:30 +00001788
Chandler Carruth5d989942011-07-06 16:21:37 +00001789 // Collect statistics about the CFG if it was built.
1790 if (S.CollectStats && AC.isCFGBuilt()) {
1791 ++NumFunctionsAnalyzed;
1792 if (CFG *cfg = AC.getCFG()) {
1793 // If we successfully built a CFG for this context, record some more
1794 // detail information about it.
Chandler Carruth3ea4c492011-07-06 22:21:45 +00001795 NumCFGBlocks += cfg->getNumBlockIDs();
Chandler Carruth5d989942011-07-06 16:21:37 +00001796 MaxCFGBlocksPerFunction = std::max(MaxCFGBlocksPerFunction,
Chandler Carruth3ea4c492011-07-06 22:21:45 +00001797 cfg->getNumBlockIDs());
Chandler Carruth5d989942011-07-06 16:21:37 +00001798 } else {
1799 ++NumFunctionsWithBadCFGs;
1800 }
1801 }
1802}
1803
1804void clang::sema::AnalysisBasedWarnings::PrintStats() const {
1805 llvm::errs() << "\n*** Analysis Based Warnings Stats:\n";
1806
1807 unsigned NumCFGsBuilt = NumFunctionsAnalyzed - NumFunctionsWithBadCFGs;
1808 unsigned AvgCFGBlocksPerFunction =
1809 !NumCFGsBuilt ? 0 : NumCFGBlocks/NumCFGsBuilt;
1810 llvm::errs() << NumFunctionsAnalyzed << " functions analyzed ("
1811 << NumFunctionsWithBadCFGs << " w/o CFGs).\n"
1812 << " " << NumCFGBlocks << " CFG blocks built.\n"
1813 << " " << AvgCFGBlocksPerFunction
1814 << " average CFG blocks per function.\n"
1815 << " " << MaxCFGBlocksPerFunction
1816 << " max CFG blocks per function.\n";
1817
1818 unsigned AvgUninitVariablesPerFunction = !NumUninitAnalysisFunctions ? 0
1819 : NumUninitAnalysisVariables/NumUninitAnalysisFunctions;
1820 unsigned AvgUninitBlockVisitsPerFunction = !NumUninitAnalysisFunctions ? 0
1821 : NumUninitAnalysisBlockVisits/NumUninitAnalysisFunctions;
1822 llvm::errs() << NumUninitAnalysisFunctions
1823 << " functions analyzed for uninitialiazed variables\n"
1824 << " " << NumUninitAnalysisVariables << " variables analyzed.\n"
1825 << " " << AvgUninitVariablesPerFunction
1826 << " average variables per function.\n"
1827 << " " << MaxUninitAnalysisVariablesPerFunction
1828 << " max variables per function.\n"
1829 << " " << NumUninitAnalysisBlockVisits << " block visits.\n"
1830 << " " << AvgUninitBlockVisitsPerFunction
1831 << " average block visits per function.\n"
1832 << " " << MaxUninitAnalysisBlockVisitsPerFunction
1833 << " max block visits per function.\n";
Ted Kremenekdbdbaaf2010-03-20 21:06:02 +00001834}