1 //=- AnalysisBasedWarnings.cpp - Sema warnings based on libAnalysis -*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "clang/Sema/AnalysisBasedWarnings.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/AST/EvaluatedExprVisitor.h"
20 #include "clang/AST/ExprCXX.h"
21 #include "clang/AST/ExprObjC.h"
22 #include "clang/AST/ParentMap.h"
23 #include "clang/AST/RecursiveASTVisitor.h"
24 #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/Consumed.h"
29 #include "clang/Analysis/Analyses/ReachableCode.h"
30 #include "clang/Analysis/Analyses/ThreadSafety.h"
31 #include "clang/Analysis/Analyses/UninitializedValues.h"
32 #include "clang/Analysis/AnalysisContext.h"
33 #include "clang/Analysis/CFG.h"
34 #include "clang/Analysis/CFGStmtMap.h"
35 #include "clang/Basic/SourceLocation.h"
36 #include "clang/Basic/SourceManager.h"
37 #include "clang/Lex/Preprocessor.h"
38 #include "clang/Sema/ScopeInfo.h"
39 #include "clang/Sema/SemaInternal.h"
40 #include "llvm/ADT/ArrayRef.h"
41 #include "llvm/ADT/BitVector.h"
42 #include "llvm/ADT/FoldingSet.h"
43 #include "llvm/ADT/ImmutableMap.h"
44 #include "llvm/ADT/MapVector.h"
45 #include "llvm/ADT/PostOrderIterator.h"
46 #include "llvm/ADT/SmallString.h"
47 #include "llvm/ADT/SmallVector.h"
48 #include "llvm/ADT/StringRef.h"
49 #include "llvm/Support/Casting.h"
55 using namespace clang;
57 //===----------------------------------------------------------------------===//
58 // Unreachable code analysis.
59 //===----------------------------------------------------------------------===//
62 class UnreachableCodeHandler : public reachable_code::Callback {
65 UnreachableCodeHandler(Sema &s) : S(s) {}
67 void HandleUnreachable(reachable_code::UnreachableKind UK,
69 SourceRange SilenceableCondVal,
71 SourceRange R2) override {
72 unsigned diag = diag::warn_unreachable;
74 case reachable_code::UK_Break:
75 diag = diag::warn_unreachable_break;
77 case reachable_code::UK_Return:
78 diag = diag::warn_unreachable_return;
80 case reachable_code::UK_Loop_Increment:
81 diag = diag::warn_unreachable_loop_increment;
83 case reachable_code::UK_Other:
87 S.Diag(L, diag) << R1 << R2;
89 SourceLocation Open = SilenceableCondVal.getBegin();
91 SourceLocation Close = SilenceableCondVal.getEnd();
92 Close = S.getLocForEndOfToken(Close);
93 if (Close.isValid()) {
94 S.Diag(Open, diag::note_unreachable_silence)
95 << FixItHint::CreateInsertion(Open, "/* DISABLES CODE */ (")
96 << FixItHint::CreateInsertion(Close, ")");
101 } // anonymous namespace
103 /// CheckUnreachable - Check for unreachable code.
104 static void CheckUnreachable(Sema &S, AnalysisDeclContext &AC) {
105 // As a heuristic prune all diagnostics not in the main file. Currently
106 // the majority of warnings in headers are false positives. These
107 // are largely caused by configuration state, e.g. preprocessor
108 // defined code, etc.
110 // Note that this is also a performance optimization. Analyzing
111 // headers many times can be expensive.
112 if (!S.getSourceManager().isInMainFile(AC.getDecl()->getLocStart()))
115 UnreachableCodeHandler UC(S);
116 reachable_code::FindUnreachableCode(AC, S.getPreprocessor(), UC);
120 /// \brief Warn on logical operator errors in CFGBuilder
121 class LogicalErrorHandler : public CFGCallback {
125 LogicalErrorHandler(Sema &S) : CFGCallback(), S(S) {}
127 static bool HasMacroID(const Expr *E) {
128 if (E->getExprLoc().isMacroID())
131 // Recurse to children.
132 for (const Stmt *SubStmt : E->children())
133 if (const Expr *SubExpr = dyn_cast_or_null<Expr>(SubStmt))
134 if (HasMacroID(SubExpr))
140 void compareAlwaysTrue(const BinaryOperator *B, bool isAlwaysTrue) override {
144 SourceRange DiagRange = B->getSourceRange();
145 S.Diag(B->getExprLoc(), diag::warn_tautological_overlap_comparison)
146 << DiagRange << isAlwaysTrue;
149 void compareBitwiseEquality(const BinaryOperator *B,
150 bool isAlwaysTrue) override {
154 SourceRange DiagRange = B->getSourceRange();
155 S.Diag(B->getExprLoc(), diag::warn_comparison_bitwise_always)
156 << DiagRange << isAlwaysTrue;
159 } // anonymous namespace
161 //===----------------------------------------------------------------------===//
162 // Check for infinite self-recursion in functions
163 //===----------------------------------------------------------------------===//
165 // Returns true if the function is called anywhere within the CFGBlock.
166 // For member functions, the additional condition of being call from the
167 // this pointer is required.
168 static bool hasRecursiveCallInPath(const FunctionDecl *FD, CFGBlock &Block) {
169 // Process all the Stmt's in this block to find any calls to FD.
170 for (const auto &B : Block) {
171 if (B.getKind() != CFGElement::Statement)
174 const CallExpr *CE = dyn_cast<CallExpr>(B.getAs<CFGStmt>()->getStmt());
175 if (!CE || !CE->getCalleeDecl() ||
176 CE->getCalleeDecl()->getCanonicalDecl() != FD)
179 // Skip function calls which are qualified with a templated class.
180 if (const DeclRefExpr *DRE =
181 dyn_cast<DeclRefExpr>(CE->getCallee()->IgnoreParenImpCasts())) {
182 if (NestedNameSpecifier *NNS = DRE->getQualifier()) {
183 if (NNS->getKind() == NestedNameSpecifier::TypeSpec &&
184 isa<TemplateSpecializationType>(NNS->getAsType())) {
190 const CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(CE);
191 if (!MCE || isa<CXXThisExpr>(MCE->getImplicitObjectArgument()) ||
192 !MCE->getMethodDecl()->isVirtual())
198 // All blocks are in one of three states. States are ordered so that blocks
199 // can only move to higher states.
200 enum RecursiveState {
203 FoundPathWithNoRecursiveCall
206 // Returns true if there exists a path to the exit block and every path
207 // to the exit block passes through a call to FD.
208 static bool checkForRecursiveFunctionCall(const FunctionDecl *FD, CFG *cfg) {
210 const unsigned ExitID = cfg->getExit().getBlockID();
212 // Mark all nodes as FoundNoPath, then set the status of the entry block.
213 SmallVector<RecursiveState, 16> States(cfg->getNumBlockIDs(), FoundNoPath);
214 States[cfg->getEntry().getBlockID()] = FoundPathWithNoRecursiveCall;
216 // Make the processing stack and seed it with the entry block.
217 SmallVector<CFGBlock *, 16> Stack;
218 Stack.push_back(&cfg->getEntry());
220 while (!Stack.empty()) {
221 CFGBlock *CurBlock = Stack.back();
224 unsigned ID = CurBlock->getBlockID();
225 RecursiveState CurState = States[ID];
227 if (CurState == FoundPathWithNoRecursiveCall) {
228 // Found a path to the exit node without a recursive call.
232 // Only change state if the block has a recursive call.
233 if (hasRecursiveCallInPath(FD, *CurBlock))
234 CurState = FoundPath;
237 // Loop over successor blocks and add them to the Stack if their state
239 for (auto I = CurBlock->succ_begin(), E = CurBlock->succ_end(); I != E; ++I)
241 unsigned next_ID = (*I)->getBlockID();
242 if (States[next_ID] < CurState) {
243 States[next_ID] = CurState;
249 // Return true if the exit node is reachable, and only reachable through
251 return States[ExitID] == FoundPath;
254 static void checkRecursiveFunction(Sema &S, const FunctionDecl *FD,
255 const Stmt *Body, AnalysisDeclContext &AC) {
256 FD = FD->getCanonicalDecl();
258 // Only run on non-templated functions and non-templated members of
259 // templated classes.
260 if (FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate &&
261 FD->getTemplatedKind() != FunctionDecl::TK_MemberSpecialization)
264 CFG *cfg = AC.getCFG();
267 // If the exit block is unreachable, skip processing the function.
268 if (cfg->getExit().pred_empty())
271 // Emit diagnostic if a recursive function call is detected for all paths.
272 if (checkForRecursiveFunctionCall(FD, cfg))
273 S.Diag(Body->getLocStart(), diag::warn_infinite_recursive_function);
276 //===----------------------------------------------------------------------===//
277 // Check for missing return value.
278 //===----------------------------------------------------------------------===//
280 enum ControlFlowKind {
285 NeverFallThroughOrReturn
288 /// CheckFallThrough - Check that we don't fall off the end of a
289 /// Statement that should return a value.
291 /// \returns AlwaysFallThrough iff we always fall off the end of the statement,
292 /// MaybeFallThrough iff we might or might not fall off the end,
293 /// NeverFallThroughOrReturn iff we never fall off the end of the statement or
294 /// return. We assume NeverFallThrough iff we never fall off the end of the
295 /// statement but we may return. We assume that functions not marked noreturn
297 static ControlFlowKind CheckFallThrough(AnalysisDeclContext &AC) {
298 CFG *cfg = AC.getCFG();
299 if (!cfg) return UnknownFallThrough;
301 // The CFG leaves in dead things, and we don't want the dead code paths to
302 // confuse us, so we mark all live things first.
303 llvm::BitVector live(cfg->getNumBlockIDs());
304 unsigned count = reachable_code::ScanReachableFromBlock(&cfg->getEntry(),
307 bool AddEHEdges = AC.getAddEHEdges();
308 if (!AddEHEdges && count != cfg->getNumBlockIDs())
309 // When there are things remaining dead, and we didn't add EH edges
310 // from CallExprs to the catch clauses, we have to go back and
311 // mark them as live.
312 for (const auto *B : *cfg) {
313 if (!live[B->getBlockID()]) {
314 if (B->pred_begin() == B->pred_end()) {
315 if (B->getTerminator() && isa<CXXTryStmt>(B->getTerminator()))
316 // When not adding EH edges from calls, catch clauses
317 // can otherwise seem dead. Avoid noting them as dead.
318 count += reachable_code::ScanReachableFromBlock(B, live);
324 // Now we know what is live, we check the live precessors of the exit block
325 // and look for fall through paths, being careful to ignore normal returns,
326 // and exceptional paths.
327 bool HasLiveReturn = false;
328 bool HasFakeEdge = false;
329 bool HasPlainEdge = false;
330 bool HasAbnormalEdge = false;
332 // Ignore default cases that aren't likely to be reachable because all
333 // enums in a switch(X) have explicit case statements.
334 CFGBlock::FilterOptions FO;
335 FO.IgnoreDefaultsWithCoveredEnums = 1;
337 for (CFGBlock::filtered_pred_iterator
338 I = cfg->getExit().filtered_pred_start_end(FO); I.hasMore(); ++I) {
339 const CFGBlock& B = **I;
340 if (!live[B.getBlockID()])
343 // Skip blocks which contain an element marked as no-return. They don't
344 // represent actually viable edges into the exit block, so mark them as
346 if (B.hasNoReturnElement()) {
347 HasAbnormalEdge = true;
351 // Destructors can appear after the 'return' in the CFG. This is
352 // normal. We need to look pass the destructors for the return
353 // statement (if it exists).
354 CFGBlock::const_reverse_iterator ri = B.rbegin(), re = B.rend();
356 for ( ; ri != re ; ++ri)
357 if (ri->getAs<CFGStmt>())
360 // No more CFGElements in the block?
362 if (B.getTerminator() && isa<CXXTryStmt>(B.getTerminator())) {
363 HasAbnormalEdge = true;
366 // A labeled empty statement, or the entry block...
371 CFGStmt CS = ri->castAs<CFGStmt>();
372 const Stmt *S = CS.getStmt();
373 if (isa<ReturnStmt>(S)) {
374 HasLiveReturn = true;
377 if (isa<ObjCAtThrowStmt>(S)) {
381 if (isa<CXXThrowExpr>(S)) {
385 if (isa<MSAsmStmt>(S)) {
386 // TODO: Verify this is correct.
388 HasLiveReturn = true;
391 if (isa<CXXTryStmt>(S)) {
392 HasAbnormalEdge = true;
395 if (std::find(B.succ_begin(), B.succ_end(), &cfg->getExit())
397 HasAbnormalEdge = true;
405 return NeverFallThrough;
406 return NeverFallThroughOrReturn;
408 if (HasAbnormalEdge || HasFakeEdge || HasLiveReturn)
409 return MaybeFallThrough;
410 // This says AlwaysFallThrough for calls to functions that are not marked
411 // noreturn, that don't return. If people would like this warning to be more
412 // accurate, such functions should be marked as noreturn.
413 return AlwaysFallThrough;
418 struct CheckFallThroughDiagnostics {
419 unsigned diag_MaybeFallThrough_HasNoReturn;
420 unsigned diag_MaybeFallThrough_ReturnsNonVoid;
421 unsigned diag_AlwaysFallThrough_HasNoReturn;
422 unsigned diag_AlwaysFallThrough_ReturnsNonVoid;
423 unsigned diag_NeverFallThroughOrReturn;
424 enum { Function, Block, Lambda } funMode;
425 SourceLocation FuncLoc;
427 static CheckFallThroughDiagnostics MakeForFunction(const Decl *Func) {
428 CheckFallThroughDiagnostics D;
429 D.FuncLoc = Func->getLocation();
430 D.diag_MaybeFallThrough_HasNoReturn =
431 diag::warn_falloff_noreturn_function;
432 D.diag_MaybeFallThrough_ReturnsNonVoid =
433 diag::warn_maybe_falloff_nonvoid_function;
434 D.diag_AlwaysFallThrough_HasNoReturn =
435 diag::warn_falloff_noreturn_function;
436 D.diag_AlwaysFallThrough_ReturnsNonVoid =
437 diag::warn_falloff_nonvoid_function;
439 // Don't suggest that virtual functions be marked "noreturn", since they
440 // might be overridden by non-noreturn functions.
441 bool isVirtualMethod = false;
442 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Func))
443 isVirtualMethod = Method->isVirtual();
445 // Don't suggest that template instantiations be marked "noreturn"
446 bool isTemplateInstantiation = false;
447 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(Func))
448 isTemplateInstantiation = Function->isTemplateInstantiation();
450 if (!isVirtualMethod && !isTemplateInstantiation)
451 D.diag_NeverFallThroughOrReturn =
452 diag::warn_suggest_noreturn_function;
454 D.diag_NeverFallThroughOrReturn = 0;
456 D.funMode = Function;
460 static CheckFallThroughDiagnostics MakeForBlock() {
461 CheckFallThroughDiagnostics D;
462 D.diag_MaybeFallThrough_HasNoReturn =
463 diag::err_noreturn_block_has_return_expr;
464 D.diag_MaybeFallThrough_ReturnsNonVoid =
465 diag::err_maybe_falloff_nonvoid_block;
466 D.diag_AlwaysFallThrough_HasNoReturn =
467 diag::err_noreturn_block_has_return_expr;
468 D.diag_AlwaysFallThrough_ReturnsNonVoid =
469 diag::err_falloff_nonvoid_block;
470 D.diag_NeverFallThroughOrReturn = 0;
475 static CheckFallThroughDiagnostics MakeForLambda() {
476 CheckFallThroughDiagnostics D;
477 D.diag_MaybeFallThrough_HasNoReturn =
478 diag::err_noreturn_lambda_has_return_expr;
479 D.diag_MaybeFallThrough_ReturnsNonVoid =
480 diag::warn_maybe_falloff_nonvoid_lambda;
481 D.diag_AlwaysFallThrough_HasNoReturn =
482 diag::err_noreturn_lambda_has_return_expr;
483 D.diag_AlwaysFallThrough_ReturnsNonVoid =
484 diag::warn_falloff_nonvoid_lambda;
485 D.diag_NeverFallThroughOrReturn = 0;
490 bool checkDiagnostics(DiagnosticsEngine &D, bool ReturnsVoid,
491 bool HasNoReturn) const {
492 if (funMode == Function) {
493 return (ReturnsVoid ||
494 D.isIgnored(diag::warn_maybe_falloff_nonvoid_function,
497 D.isIgnored(diag::warn_noreturn_function_has_return_expr,
500 D.isIgnored(diag::warn_suggest_noreturn_block, FuncLoc));
503 // For blocks / lambdas.
504 return ReturnsVoid && !HasNoReturn;
508 } // anonymous namespace
510 /// CheckFallThroughForFunctionDef - Check that we don't fall off the end of a
511 /// function that should return a value. Check that we don't fall off the end
512 /// of a noreturn function. We assume that functions and blocks not marked
513 /// noreturn will return.
514 static void CheckFallThroughForBody(Sema &S, const Decl *D, const Stmt *Body,
515 const BlockExpr *blkExpr,
516 const CheckFallThroughDiagnostics& CD,
517 AnalysisDeclContext &AC) {
519 bool ReturnsVoid = false;
520 bool HasNoReturn = false;
522 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
523 ReturnsVoid = FD->getReturnType()->isVoidType();
524 HasNoReturn = FD->isNoReturn();
526 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
527 ReturnsVoid = MD->getReturnType()->isVoidType();
528 HasNoReturn = MD->hasAttr<NoReturnAttr>();
530 else if (isa<BlockDecl>(D)) {
531 QualType BlockTy = blkExpr->getType();
532 if (const FunctionType *FT =
533 BlockTy->getPointeeType()->getAs<FunctionType>()) {
534 if (FT->getReturnType()->isVoidType())
536 if (FT->getNoReturnAttr())
541 DiagnosticsEngine &Diags = S.getDiagnostics();
543 // Short circuit for compilation speed.
544 if (CD.checkDiagnostics(Diags, ReturnsVoid, HasNoReturn))
547 SourceLocation LBrace = Body->getLocStart(), RBrace = Body->getLocEnd();
548 // Either in a function body compound statement, or a function-try-block.
549 switch (CheckFallThrough(AC)) {
550 case UnknownFallThrough:
553 case MaybeFallThrough:
555 S.Diag(RBrace, CD.diag_MaybeFallThrough_HasNoReturn);
556 else if (!ReturnsVoid)
557 S.Diag(RBrace, CD.diag_MaybeFallThrough_ReturnsNonVoid);
559 case AlwaysFallThrough:
561 S.Diag(RBrace, CD.diag_AlwaysFallThrough_HasNoReturn);
562 else if (!ReturnsVoid)
563 S.Diag(RBrace, CD.diag_AlwaysFallThrough_ReturnsNonVoid);
565 case NeverFallThroughOrReturn:
566 if (ReturnsVoid && !HasNoReturn && CD.diag_NeverFallThroughOrReturn) {
567 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
568 S.Diag(LBrace, CD.diag_NeverFallThroughOrReturn) << 0 << FD;
569 } else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
570 S.Diag(LBrace, CD.diag_NeverFallThroughOrReturn) << 1 << MD;
572 S.Diag(LBrace, CD.diag_NeverFallThroughOrReturn);
576 case NeverFallThrough:
581 //===----------------------------------------------------------------------===//
583 //===----------------------------------------------------------------------===//
586 /// ContainsReference - A visitor class to search for references to
587 /// a particular declaration (the needle) within any evaluated component of an
588 /// expression (recursively).
589 class ContainsReference : public ConstEvaluatedExprVisitor<ContainsReference> {
591 const DeclRefExpr *Needle;
594 typedef ConstEvaluatedExprVisitor<ContainsReference> Inherited;
596 ContainsReference(ASTContext &Context, const DeclRefExpr *Needle)
597 : Inherited(Context), FoundReference(false), Needle(Needle) {}
599 void VisitExpr(const Expr *E) {
600 // Stop evaluating if we already have a reference.
604 Inherited::VisitExpr(E);
607 void VisitDeclRefExpr(const DeclRefExpr *E) {
609 FoundReference = true;
611 Inherited::VisitDeclRefExpr(E);
614 bool doesContainReference() const { return FoundReference; }
616 } // anonymous namespace
618 static bool SuggestInitializationFixit(Sema &S, const VarDecl *VD) {
619 QualType VariableTy = VD->getType().getCanonicalType();
620 if (VariableTy->isBlockPointerType() &&
621 !VD->hasAttr<BlocksAttr>()) {
622 S.Diag(VD->getLocation(), diag::note_block_var_fixit_add_initialization)
624 << FixItHint::CreateInsertion(VD->getLocation(), "__block ");
628 // Don't issue a fixit if there is already an initializer.
632 // Don't suggest a fixit inside macros.
633 if (VD->getLocEnd().isMacroID())
636 SourceLocation Loc = S.getLocForEndOfToken(VD->getLocEnd());
638 // Suggest possible initialization (if any).
639 std::string Init = S.getFixItZeroInitializerForType(VariableTy, Loc);
643 S.Diag(Loc, diag::note_var_fixit_add_initialization) << VD->getDeclName()
644 << FixItHint::CreateInsertion(Loc, Init);
648 /// Create a fixit to remove an if-like statement, on the assumption that its
649 /// condition is CondVal.
650 static void CreateIfFixit(Sema &S, const Stmt *If, const Stmt *Then,
651 const Stmt *Else, bool CondVal,
652 FixItHint &Fixit1, FixItHint &Fixit2) {
654 // If condition is always true, remove all but the 'then'.
655 Fixit1 = FixItHint::CreateRemoval(
656 CharSourceRange::getCharRange(If->getLocStart(),
657 Then->getLocStart()));
659 SourceLocation ElseKwLoc = S.getLocForEndOfToken(Then->getLocEnd());
660 Fixit2 = FixItHint::CreateRemoval(
661 SourceRange(ElseKwLoc, Else->getLocEnd()));
664 // If condition is always false, remove all but the 'else'.
666 Fixit1 = FixItHint::CreateRemoval(
667 CharSourceRange::getCharRange(If->getLocStart(),
668 Else->getLocStart()));
670 Fixit1 = FixItHint::CreateRemoval(If->getSourceRange());
674 /// DiagUninitUse -- Helper function to produce a diagnostic for an
675 /// uninitialized use of a variable.
676 static void DiagUninitUse(Sema &S, const VarDecl *VD, const UninitUse &Use,
677 bool IsCapturedByBlock) {
678 bool Diagnosed = false;
680 switch (Use.getKind()) {
681 case UninitUse::Always:
682 S.Diag(Use.getUser()->getLocStart(), diag::warn_uninit_var)
683 << VD->getDeclName() << IsCapturedByBlock
684 << Use.getUser()->getSourceRange();
687 case UninitUse::AfterDecl:
688 case UninitUse::AfterCall:
689 S.Diag(VD->getLocation(), diag::warn_sometimes_uninit_var)
690 << VD->getDeclName() << IsCapturedByBlock
691 << (Use.getKind() == UninitUse::AfterDecl ? 4 : 5)
692 << const_cast<DeclContext*>(VD->getLexicalDeclContext())
693 << VD->getSourceRange();
694 S.Diag(Use.getUser()->getLocStart(), diag::note_uninit_var_use)
695 << IsCapturedByBlock << Use.getUser()->getSourceRange();
698 case UninitUse::Maybe:
699 case UninitUse::Sometimes:
700 // Carry on to report sometimes-uninitialized branches, if possible,
701 // or a 'may be used uninitialized' diagnostic otherwise.
705 // Diagnose each branch which leads to a sometimes-uninitialized use.
706 for (UninitUse::branch_iterator I = Use.branch_begin(), E = Use.branch_end();
708 assert(Use.getKind() == UninitUse::Sometimes);
710 const Expr *User = Use.getUser();
711 const Stmt *Term = I->Terminator;
713 // Information used when building the diagnostic.
718 // FixIts to suppress the diagnostic by removing the dead condition.
719 // For all binary terminators, branch 0 is taken if the condition is true,
720 // and branch 1 is taken if the condition is false.
721 int RemoveDiagKind = -1;
722 const char *FixitStr =
723 S.getLangOpts().CPlusPlus ? (I->Output ? "true" : "false")
724 : (I->Output ? "1" : "0");
725 FixItHint Fixit1, Fixit2;
727 switch (Term ? Term->getStmtClass() : Stmt::DeclStmtClass) {
729 // Don't know how to report this. Just fall back to 'may be used
730 // uninitialized'. FIXME: Can this happen?
733 // "condition is true / condition is false".
734 case Stmt::IfStmtClass: {
735 const IfStmt *IS = cast<IfStmt>(Term);
738 Range = IS->getCond()->getSourceRange();
740 CreateIfFixit(S, IS, IS->getThen(), IS->getElse(),
741 I->Output, Fixit1, Fixit2);
744 case Stmt::ConditionalOperatorClass: {
745 const ConditionalOperator *CO = cast<ConditionalOperator>(Term);
748 Range = CO->getCond()->getSourceRange();
750 CreateIfFixit(S, CO, CO->getTrueExpr(), CO->getFalseExpr(),
751 I->Output, Fixit1, Fixit2);
754 case Stmt::BinaryOperatorClass: {
755 const BinaryOperator *BO = cast<BinaryOperator>(Term);
756 if (!BO->isLogicalOp())
759 Str = BO->getOpcodeStr();
760 Range = BO->getLHS()->getSourceRange();
762 if ((BO->getOpcode() == BO_LAnd && I->Output) ||
763 (BO->getOpcode() == BO_LOr && !I->Output))
764 // true && y -> y, false || y -> y.
765 Fixit1 = FixItHint::CreateRemoval(SourceRange(BO->getLocStart(),
766 BO->getOperatorLoc()));
768 // false && y -> false, true || y -> true.
769 Fixit1 = FixItHint::CreateReplacement(BO->getSourceRange(), FixitStr);
773 // "loop is entered / loop is exited".
774 case Stmt::WhileStmtClass:
777 Range = cast<WhileStmt>(Term)->getCond()->getSourceRange();
779 Fixit1 = FixItHint::CreateReplacement(Range, FixitStr);
781 case Stmt::ForStmtClass:
784 Range = cast<ForStmt>(Term)->getCond()->getSourceRange();
787 Fixit1 = FixItHint::CreateRemoval(Range);
789 Fixit1 = FixItHint::CreateReplacement(Range, FixitStr);
791 case Stmt::CXXForRangeStmtClass:
792 if (I->Output == 1) {
793 // The use occurs if a range-based for loop's body never executes.
794 // That may be impossible, and there's no syntactic fix for this,
795 // so treat it as a 'may be uninitialized' case.
800 Range = cast<CXXForRangeStmt>(Term)->getRangeInit()->getSourceRange();
803 // "condition is true / loop is exited".
804 case Stmt::DoStmtClass:
807 Range = cast<DoStmt>(Term)->getCond()->getSourceRange();
809 Fixit1 = FixItHint::CreateReplacement(Range, FixitStr);
812 // "switch case is taken".
813 case Stmt::CaseStmtClass:
816 Range = cast<CaseStmt>(Term)->getLHS()->getSourceRange();
818 case Stmt::DefaultStmtClass:
821 Range = cast<DefaultStmt>(Term)->getDefaultLoc();
825 S.Diag(Range.getBegin(), diag::warn_sometimes_uninit_var)
826 << VD->getDeclName() << IsCapturedByBlock << DiagKind
827 << Str << I->Output << Range;
828 S.Diag(User->getLocStart(), diag::note_uninit_var_use)
829 << IsCapturedByBlock << User->getSourceRange();
830 if (RemoveDiagKind != -1)
831 S.Diag(Fixit1.RemoveRange.getBegin(), diag::note_uninit_fixit_remove_cond)
832 << RemoveDiagKind << Str << I->Output << Fixit1 << Fixit2;
838 S.Diag(Use.getUser()->getLocStart(), diag::warn_maybe_uninit_var)
839 << VD->getDeclName() << IsCapturedByBlock
840 << Use.getUser()->getSourceRange();
843 /// DiagnoseUninitializedUse -- Helper function for diagnosing uses of an
844 /// uninitialized variable. This manages the different forms of diagnostic
845 /// emitted for particular types of uses. Returns true if the use was diagnosed
846 /// as a warning. If a particular use is one we omit warnings for, returns
848 static bool DiagnoseUninitializedUse(Sema &S, const VarDecl *VD,
849 const UninitUse &Use,
850 bool alwaysReportSelfInit = false) {
851 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Use.getUser())) {
852 // Inspect the initializer of the variable declaration which is
853 // being referenced prior to its initialization. We emit
854 // specialized diagnostics for self-initialization, and we
855 // specifically avoid warning about self references which take the
860 // This is used to indicate to GCC that 'x' is intentionally left
861 // uninitialized. Proven code paths which access 'x' in
862 // an uninitialized state after this will still warn.
863 if (const Expr *Initializer = VD->getInit()) {
864 if (!alwaysReportSelfInit && DRE == Initializer->IgnoreParenImpCasts())
867 ContainsReference CR(S.Context, DRE);
868 CR.Visit(Initializer);
869 if (CR.doesContainReference()) {
870 S.Diag(DRE->getLocStart(),
871 diag::warn_uninit_self_reference_in_init)
872 << VD->getDeclName() << VD->getLocation() << DRE->getSourceRange();
877 DiagUninitUse(S, VD, Use, false);
879 const BlockExpr *BE = cast<BlockExpr>(Use.getUser());
880 if (VD->getType()->isBlockPointerType() && !VD->hasAttr<BlocksAttr>())
881 S.Diag(BE->getLocStart(),
882 diag::warn_uninit_byref_blockvar_captured_by_block)
883 << VD->getDeclName();
885 DiagUninitUse(S, VD, Use, true);
888 // Report where the variable was declared when the use wasn't within
889 // the initializer of that declaration & we didn't already suggest
890 // an initialization fixit.
891 if (!SuggestInitializationFixit(S, VD))
892 S.Diag(VD->getLocStart(), diag::note_var_declared_here)
893 << VD->getDeclName();
899 class FallthroughMapper : public RecursiveASTVisitor<FallthroughMapper> {
901 FallthroughMapper(Sema &S)
902 : FoundSwitchStatements(false),
906 bool foundSwitchStatements() const { return FoundSwitchStatements; }
908 void markFallthroughVisited(const AttributedStmt *Stmt) {
909 bool Found = FallthroughStmts.erase(Stmt);
914 typedef llvm::SmallPtrSet<const AttributedStmt*, 8> AttrStmts;
916 const AttrStmts &getFallthroughStmts() const {
917 return FallthroughStmts;
920 void fillReachableBlocks(CFG *Cfg) {
921 assert(ReachableBlocks.empty() && "ReachableBlocks already filled");
922 std::deque<const CFGBlock *> BlockQueue;
924 ReachableBlocks.insert(&Cfg->getEntry());
925 BlockQueue.push_back(&Cfg->getEntry());
926 // Mark all case blocks reachable to avoid problems with switching on
927 // constants, covered enums, etc.
928 // These blocks can contain fall-through annotations, and we don't want to
929 // issue a warn_fallthrough_attr_unreachable for them.
930 for (const auto *B : *Cfg) {
931 const Stmt *L = B->getLabel();
932 if (L && isa<SwitchCase>(L) && ReachableBlocks.insert(B).second)
933 BlockQueue.push_back(B);
936 while (!BlockQueue.empty()) {
937 const CFGBlock *P = BlockQueue.front();
938 BlockQueue.pop_front();
939 for (CFGBlock::const_succ_iterator I = P->succ_begin(),
942 if (*I && ReachableBlocks.insert(*I).second)
943 BlockQueue.push_back(*I);
948 bool checkFallThroughIntoBlock(const CFGBlock &B, int &AnnotatedCnt) {
949 assert(!ReachableBlocks.empty() && "ReachableBlocks empty");
951 int UnannotatedCnt = 0;
954 std::deque<const CFGBlock*> BlockQueue(B.pred_begin(), B.pred_end());
955 while (!BlockQueue.empty()) {
956 const CFGBlock *P = BlockQueue.front();
957 BlockQueue.pop_front();
960 const Stmt *Term = P->getTerminator();
961 if (Term && isa<SwitchStmt>(Term))
962 continue; // Switch statement, good.
964 const SwitchCase *SW = dyn_cast_or_null<SwitchCase>(P->getLabel());
965 if (SW && SW->getSubStmt() == B.getLabel() && P->begin() == P->end())
966 continue; // Previous case label has no statements, good.
968 const LabelStmt *L = dyn_cast_or_null<LabelStmt>(P->getLabel());
969 if (L && L->getSubStmt() == B.getLabel() && P->begin() == P->end())
970 continue; // Case label is preceded with a normal label, good.
972 if (!ReachableBlocks.count(P)) {
973 for (CFGBlock::const_reverse_iterator ElemIt = P->rbegin(),
975 ElemIt != ElemEnd; ++ElemIt) {
976 if (Optional<CFGStmt> CS = ElemIt->getAs<CFGStmt>()) {
977 if (const AttributedStmt *AS = asFallThroughAttr(CS->getStmt())) {
978 S.Diag(AS->getLocStart(),
979 diag::warn_fallthrough_attr_unreachable);
980 markFallthroughVisited(AS);
984 // Don't care about other unreachable statements.
987 // If there are no unreachable statements, this may be a special
990 // A a; // A has a destructor.
993 // // <<<< This place is represented by a 'hanging' CFG block.
998 const Stmt *LastStmt = getLastStmt(*P);
999 if (const AttributedStmt *AS = asFallThroughAttr(LastStmt)) {
1000 markFallthroughVisited(AS);
1002 continue; // Fallthrough annotation, good.
1005 if (!LastStmt) { // This block contains no executable statements.
1006 // Traverse its predecessors.
1007 std::copy(P->pred_begin(), P->pred_end(),
1008 std::back_inserter(BlockQueue));
1014 return !!UnannotatedCnt;
1017 // RecursiveASTVisitor setup.
1018 bool shouldWalkTypesOfTypeLocs() const { return false; }
1020 bool VisitAttributedStmt(AttributedStmt *S) {
1021 if (asFallThroughAttr(S))
1022 FallthroughStmts.insert(S);
1026 bool VisitSwitchStmt(SwitchStmt *S) {
1027 FoundSwitchStatements = true;
1031 // We don't want to traverse local type declarations. We analyze their
1032 // methods separately.
1033 bool TraverseDecl(Decl *D) { return true; }
1035 // We analyze lambda bodies separately. Skip them here.
1036 bool TraverseLambdaBody(LambdaExpr *LE) { return true; }
1040 static const AttributedStmt *asFallThroughAttr(const Stmt *S) {
1041 if (const AttributedStmt *AS = dyn_cast_or_null<AttributedStmt>(S)) {
1042 if (hasSpecificAttr<FallThroughAttr>(AS->getAttrs()))
1048 static const Stmt *getLastStmt(const CFGBlock &B) {
1049 if (const Stmt *Term = B.getTerminator())
1051 for (CFGBlock::const_reverse_iterator ElemIt = B.rbegin(),
1053 ElemIt != ElemEnd; ++ElemIt) {
1054 if (Optional<CFGStmt> CS = ElemIt->getAs<CFGStmt>())
1055 return CS->getStmt();
1057 // Workaround to detect a statement thrown out by CFGBuilder:
1058 // case X: {} case Y:
1059 // case X: ; case Y:
1060 if (const SwitchCase *SW = dyn_cast_or_null<SwitchCase>(B.getLabel()))
1061 if (!isa<SwitchCase>(SW->getSubStmt()))
1062 return SW->getSubStmt();
1067 bool FoundSwitchStatements;
1068 AttrStmts FallthroughStmts;
1070 llvm::SmallPtrSet<const CFGBlock *, 16> ReachableBlocks;
1072 } // anonymous namespace
1074 static StringRef getFallthroughAttrSpelling(Preprocessor &PP,
1075 SourceLocation Loc) {
1076 TokenValue FallthroughTokens[] = {
1077 tok::l_square, tok::l_square,
1078 PP.getIdentifierInfo("fallthrough"),
1079 tok::r_square, tok::r_square
1082 TokenValue ClangFallthroughTokens[] = {
1083 tok::l_square, tok::l_square, PP.getIdentifierInfo("clang"),
1084 tok::coloncolon, PP.getIdentifierInfo("fallthrough"),
1085 tok::r_square, tok::r_square
1088 bool PreferClangAttr = !PP.getLangOpts().CPlusPlus1z;
1090 StringRef MacroName;
1091 if (PreferClangAttr)
1092 MacroName = PP.getLastMacroWithSpelling(Loc, ClangFallthroughTokens);
1093 if (MacroName.empty())
1094 MacroName = PP.getLastMacroWithSpelling(Loc, FallthroughTokens);
1095 if (MacroName.empty() && !PreferClangAttr)
1096 MacroName = PP.getLastMacroWithSpelling(Loc, ClangFallthroughTokens);
1097 if (MacroName.empty())
1098 MacroName = PreferClangAttr ? "[[clang::fallthrough]]" : "[[fallthrough]]";
1102 static void DiagnoseSwitchLabelsFallthrough(Sema &S, AnalysisDeclContext &AC,
1104 // Only perform this analysis when using C++11. There is no good workflow
1105 // for this warning when not using C++11. There is no good way to silence
1106 // the warning (no attribute is available) unless we are using C++11's support
1107 // for generalized attributes. Once could use pragmas to silence the warning,
1108 // but as a general solution that is gross and not in the spirit of this
1111 // NOTE: This an intermediate solution. There are on-going discussions on
1112 // how to properly support this warning outside of C++11 with an annotation.
1113 if (!AC.getASTContext().getLangOpts().CPlusPlus11)
1116 FallthroughMapper FM(S);
1117 FM.TraverseStmt(AC.getBody());
1119 if (!FM.foundSwitchStatements())
1122 if (PerFunction && FM.getFallthroughStmts().empty())
1125 CFG *Cfg = AC.getCFG();
1130 FM.fillReachableBlocks(Cfg);
1132 for (const CFGBlock *B : llvm::reverse(*Cfg)) {
1133 const Stmt *Label = B->getLabel();
1135 if (!Label || !isa<SwitchCase>(Label))
1140 if (!FM.checkFallThroughIntoBlock(*B, AnnotatedCnt))
1143 S.Diag(Label->getLocStart(),
1144 PerFunction ? diag::warn_unannotated_fallthrough_per_function
1145 : diag::warn_unannotated_fallthrough);
1147 if (!AnnotatedCnt) {
1148 SourceLocation L = Label->getLocStart();
1151 if (S.getLangOpts().CPlusPlus11) {
1152 const Stmt *Term = B->getTerminator();
1153 // Skip empty cases.
1154 while (B->empty() && !Term && B->succ_size() == 1) {
1155 B = *B->succ_begin();
1156 Term = B->getTerminator();
1158 if (!(B->empty() && Term && isa<BreakStmt>(Term))) {
1159 Preprocessor &PP = S.getPreprocessor();
1160 StringRef AnnotationSpelling = getFallthroughAttrSpelling(PP, L);
1161 SmallString<64> TextToInsert(AnnotationSpelling);
1162 TextToInsert += "; ";
1163 S.Diag(L, diag::note_insert_fallthrough_fixit) <<
1164 AnnotationSpelling <<
1165 FixItHint::CreateInsertion(L, TextToInsert);
1168 S.Diag(L, diag::note_insert_break_fixit) <<
1169 FixItHint::CreateInsertion(L, "break; ");
1173 for (const auto *F : FM.getFallthroughStmts())
1174 S.Diag(F->getLocStart(), diag::err_fallthrough_attr_invalid_placement);
1177 static bool isInLoop(const ASTContext &Ctx, const ParentMap &PM,
1182 switch (S->getStmtClass()) {
1183 case Stmt::ForStmtClass:
1184 case Stmt::WhileStmtClass:
1185 case Stmt::CXXForRangeStmtClass:
1186 case Stmt::ObjCForCollectionStmtClass:
1188 case Stmt::DoStmtClass: {
1189 const Expr *Cond = cast<DoStmt>(S)->getCond();
1191 if (!Cond->EvaluateAsInt(Val, Ctx))
1193 return Val.getBoolValue();
1198 } while ((S = PM.getParent(S)));
1203 static void diagnoseRepeatedUseOfWeak(Sema &S,
1204 const sema::FunctionScopeInfo *CurFn,
1206 const ParentMap &PM) {
1207 typedef sema::FunctionScopeInfo::WeakObjectProfileTy WeakObjectProfileTy;
1208 typedef sema::FunctionScopeInfo::WeakObjectUseMap WeakObjectUseMap;
1209 typedef sema::FunctionScopeInfo::WeakUseVector WeakUseVector;
1210 typedef std::pair<const Stmt *, WeakObjectUseMap::const_iterator>
1213 ASTContext &Ctx = S.getASTContext();
1215 const WeakObjectUseMap &WeakMap = CurFn->getWeakObjectUses();
1217 // Extract all weak objects that are referenced more than once.
1218 SmallVector<StmtUsesPair, 8> UsesByStmt;
1219 for (WeakObjectUseMap::const_iterator I = WeakMap.begin(), E = WeakMap.end();
1221 const WeakUseVector &Uses = I->second;
1223 // Find the first read of the weak object.
1224 WeakUseVector::const_iterator UI = Uses.begin(), UE = Uses.end();
1225 for ( ; UI != UE; ++UI) {
1230 // If there were only writes to this object, don't warn.
1234 // If there was only one read, followed by any number of writes, and the
1235 // read is not within a loop, don't warn. Additionally, don't warn in a
1236 // loop if the base object is a local variable -- local variables are often
1237 // changed in loops.
1238 if (UI == Uses.begin()) {
1239 WeakUseVector::const_iterator UI2 = UI;
1240 for (++UI2; UI2 != UE; ++UI2)
1241 if (UI2->isUnsafe())
1245 if (!isInLoop(Ctx, PM, UI->getUseExpr()))
1248 const WeakObjectProfileTy &Profile = I->first;
1249 if (!Profile.isExactProfile())
1252 const NamedDecl *Base = Profile.getBase();
1254 Base = Profile.getProperty();
1255 assert(Base && "A profile always has a base or property.");
1257 if (const VarDecl *BaseVar = dyn_cast<VarDecl>(Base))
1258 if (BaseVar->hasLocalStorage() && !isa<ParmVarDecl>(Base))
1263 UsesByStmt.push_back(StmtUsesPair(UI->getUseExpr(), I));
1266 if (UsesByStmt.empty())
1269 // Sort by first use so that we emit the warnings in a deterministic order.
1270 SourceManager &SM = S.getSourceManager();
1271 std::sort(UsesByStmt.begin(), UsesByStmt.end(),
1272 [&SM](const StmtUsesPair &LHS, const StmtUsesPair &RHS) {
1273 return SM.isBeforeInTranslationUnit(LHS.first->getLocStart(),
1274 RHS.first->getLocStart());
1277 // Classify the current code body for better warning text.
1278 // This enum should stay in sync with the cases in
1279 // warn_arc_repeated_use_of_weak and warn_arc_possible_repeated_use_of_weak.
1280 // FIXME: Should we use a common classification enum and the same set of
1281 // possibilities all throughout Sema?
1289 if (isa<sema::BlockScopeInfo>(CurFn))
1290 FunctionKind = Block;
1291 else if (isa<sema::LambdaScopeInfo>(CurFn))
1292 FunctionKind = Lambda;
1293 else if (isa<ObjCMethodDecl>(D))
1294 FunctionKind = Method;
1296 FunctionKind = Function;
1298 // Iterate through the sorted problems and emit warnings for each.
1299 for (const auto &P : UsesByStmt) {
1300 const Stmt *FirstRead = P.first;
1301 const WeakObjectProfileTy &Key = P.second->first;
1302 const WeakUseVector &Uses = P.second->second;
1304 // For complicated expressions like 'a.b.c' and 'x.b.c', WeakObjectProfileTy
1305 // may not contain enough information to determine that these are different
1306 // properties. We can only be 100% sure of a repeated use in certain cases,
1307 // and we adjust the diagnostic kind accordingly so that the less certain
1308 // case can be turned off if it is too noisy.
1310 if (Key.isExactProfile())
1311 DiagKind = diag::warn_arc_repeated_use_of_weak;
1313 DiagKind = diag::warn_arc_possible_repeated_use_of_weak;
1315 // Classify the weak object being accessed for better warning text.
1316 // This enum should stay in sync with the cases in
1317 // warn_arc_repeated_use_of_weak and warn_arc_possible_repeated_use_of_weak.
1325 const NamedDecl *KeyProp = Key.getProperty();
1326 if (isa<VarDecl>(KeyProp))
1327 ObjectKind = Variable;
1328 else if (isa<ObjCPropertyDecl>(KeyProp))
1329 ObjectKind = Property;
1330 else if (isa<ObjCMethodDecl>(KeyProp))
1331 ObjectKind = ImplicitProperty;
1332 else if (isa<ObjCIvarDecl>(KeyProp))
1335 llvm_unreachable("Unexpected weak object kind!");
1337 // Do not warn about IBOutlet weak property receivers being set to null
1338 // since they are typically only used from the main thread.
1339 if (const ObjCPropertyDecl *Prop = dyn_cast<ObjCPropertyDecl>(KeyProp))
1340 if (Prop->hasAttr<IBOutletAttr>())
1343 // Show the first time the object was read.
1344 S.Diag(FirstRead->getLocStart(), DiagKind)
1345 << int(ObjectKind) << KeyProp << int(FunctionKind)
1346 << FirstRead->getSourceRange();
1348 // Print all the other accesses as notes.
1349 for (const auto &Use : Uses) {
1350 if (Use.getUseExpr() == FirstRead)
1352 S.Diag(Use.getUseExpr()->getLocStart(),
1353 diag::note_arc_weak_also_accessed_here)
1354 << Use.getUseExpr()->getSourceRange();
1360 class UninitValsDiagReporter : public UninitVariablesHandler {
1362 typedef SmallVector<UninitUse, 2> UsesVec;
1363 typedef llvm::PointerIntPair<UsesVec *, 1, bool> MappedType;
1364 // Prefer using MapVector to DenseMap, so that iteration order will be
1365 // the same as insertion order. This is needed to obtain a deterministic
1366 // order of diagnostics when calling flushDiagnostics().
1367 typedef llvm::MapVector<const VarDecl *, MappedType> UsesMap;
1371 UninitValsDiagReporter(Sema &S) : S(S) {}
1372 ~UninitValsDiagReporter() override { flushDiagnostics(); }
1374 MappedType &getUses(const VarDecl *vd) {
1375 MappedType &V = uses[vd];
1376 if (!V.getPointer())
1377 V.setPointer(new UsesVec());
1381 void handleUseOfUninitVariable(const VarDecl *vd,
1382 const UninitUse &use) override {
1383 getUses(vd).getPointer()->push_back(use);
1386 void handleSelfInit(const VarDecl *vd) override {
1387 getUses(vd).setInt(true);
1390 void flushDiagnostics() {
1391 for (const auto &P : uses) {
1392 const VarDecl *vd = P.first;
1393 const MappedType &V = P.second;
1395 UsesVec *vec = V.getPointer();
1396 bool hasSelfInit = V.getInt();
1398 // Specially handle the case where we have uses of an uninitialized
1399 // variable, but the root cause is an idiomatic self-init. We want
1400 // to report the diagnostic at the self-init since that is the root cause.
1401 if (!vec->empty() && hasSelfInit && hasAlwaysUninitializedUse(vec))
1402 DiagnoseUninitializedUse(S, vd,
1403 UninitUse(vd->getInit()->IgnoreParenCasts(),
1404 /* isAlwaysUninit */ true),
1405 /* alwaysReportSelfInit */ true);
1407 // Sort the uses by their SourceLocations. While not strictly
1408 // guaranteed to produce them in line/column order, this will provide
1409 // a stable ordering.
1410 std::sort(vec->begin(), vec->end(),
1411 [](const UninitUse &a, const UninitUse &b) {
1412 // Prefer a more confident report over a less confident one.
1413 if (a.getKind() != b.getKind())
1414 return a.getKind() > b.getKind();
1415 return a.getUser()->getLocStart() < b.getUser()->getLocStart();
1418 for (const auto &U : *vec) {
1419 // If we have self-init, downgrade all uses to 'may be uninitialized'.
1420 UninitUse Use = hasSelfInit ? UninitUse(U.getUser(), false) : U;
1422 if (DiagnoseUninitializedUse(S, vd, Use))
1423 // Skip further diagnostics for this variable. We try to warn only
1424 // on the first point at which a variable is used uninitialized.
1429 // Release the uses vector.
1437 static bool hasAlwaysUninitializedUse(const UsesVec* vec) {
1438 return std::any_of(vec->begin(), vec->end(), [](const UninitUse &U) {
1439 return U.getKind() == UninitUse::Always ||
1440 U.getKind() == UninitUse::AfterCall ||
1441 U.getKind() == UninitUse::AfterDecl;
1445 } // anonymous namespace
1449 typedef SmallVector<PartialDiagnosticAt, 1> OptionalNotes;
1450 typedef std::pair<PartialDiagnosticAt, OptionalNotes> DelayedDiag;
1451 typedef std::list<DelayedDiag> DiagList;
1453 struct SortDiagBySourceLocation {
1455 SortDiagBySourceLocation(SourceManager &SM) : SM(SM) {}
1457 bool operator()(const DelayedDiag &left, const DelayedDiag &right) {
1458 // Although this call will be slow, this is only called when outputting
1459 // multiple warnings.
1460 return SM.isBeforeInTranslationUnit(left.first.first, right.first.first);
1463 } // anonymous namespace
1464 } // namespace clang
1466 //===----------------------------------------------------------------------===//
1468 //===----------------------------------------------------------------------===//
1470 namespace threadSafety {
1472 class ThreadSafetyReporter : public clang::threadSafety::ThreadSafetyHandler {
1475 SourceLocation FunLocation, FunEndLocation;
1477 const FunctionDecl *CurrentFunction;
1480 OptionalNotes getNotes() const {
1481 if (Verbose && CurrentFunction) {
1482 PartialDiagnosticAt FNote(CurrentFunction->getBody()->getLocStart(),
1483 S.PDiag(diag::note_thread_warning_in_fun)
1484 << CurrentFunction->getNameAsString());
1485 return OptionalNotes(1, FNote);
1487 return OptionalNotes();
1490 OptionalNotes getNotes(const PartialDiagnosticAt &Note) const {
1491 OptionalNotes ONS(1, Note);
1492 if (Verbose && CurrentFunction) {
1493 PartialDiagnosticAt FNote(CurrentFunction->getBody()->getLocStart(),
1494 S.PDiag(diag::note_thread_warning_in_fun)
1495 << CurrentFunction->getNameAsString());
1496 ONS.push_back(std::move(FNote));
1501 OptionalNotes getNotes(const PartialDiagnosticAt &Note1,
1502 const PartialDiagnosticAt &Note2) const {
1504 ONS.push_back(Note1);
1505 ONS.push_back(Note2);
1506 if (Verbose && CurrentFunction) {
1507 PartialDiagnosticAt FNote(CurrentFunction->getBody()->getLocStart(),
1508 S.PDiag(diag::note_thread_warning_in_fun)
1509 << CurrentFunction->getNameAsString());
1510 ONS.push_back(std::move(FNote));
1516 void warnLockMismatch(unsigned DiagID, StringRef Kind, Name LockName,
1517 SourceLocation Loc) {
1518 // Gracefully handle rare cases when the analysis can't get a more
1519 // precise source location.
1522 PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID) << Kind << LockName);
1523 Warnings.emplace_back(std::move(Warning), getNotes());
1527 ThreadSafetyReporter(Sema &S, SourceLocation FL, SourceLocation FEL)
1528 : S(S), FunLocation(FL), FunEndLocation(FEL),
1529 CurrentFunction(nullptr), Verbose(false) {}
1531 void setVerbose(bool b) { Verbose = b; }
1533 /// \brief Emit all buffered diagnostics in order of sourcelocation.
1534 /// We need to output diagnostics produced while iterating through
1535 /// the lockset in deterministic order, so this function orders diagnostics
1536 /// and outputs them.
1537 void emitDiagnostics() {
1538 Warnings.sort(SortDiagBySourceLocation(S.getSourceManager()));
1539 for (const auto &Diag : Warnings) {
1540 S.Diag(Diag.first.first, Diag.first.second);
1541 for (const auto &Note : Diag.second)
1542 S.Diag(Note.first, Note.second);
1546 void handleInvalidLockExp(StringRef Kind, SourceLocation Loc) override {
1547 PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_cannot_resolve_lock)
1549 Warnings.emplace_back(std::move(Warning), getNotes());
1552 void handleUnmatchedUnlock(StringRef Kind, Name LockName,
1553 SourceLocation Loc) override {
1554 warnLockMismatch(diag::warn_unlock_but_no_lock, Kind, LockName, Loc);
1557 void handleIncorrectUnlockKind(StringRef Kind, Name LockName,
1558 LockKind Expected, LockKind Received,
1559 SourceLocation Loc) override {
1560 if (Loc.isInvalid())
1562 PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_unlock_kind_mismatch)
1563 << Kind << LockName << Received
1565 Warnings.emplace_back(std::move(Warning), getNotes());
1568 void handleDoubleLock(StringRef Kind, Name LockName, SourceLocation Loc) override {
1569 warnLockMismatch(diag::warn_double_lock, Kind, LockName, Loc);
1572 void handleMutexHeldEndOfScope(StringRef Kind, Name LockName,
1573 SourceLocation LocLocked,
1574 SourceLocation LocEndOfScope,
1575 LockErrorKind LEK) override {
1576 unsigned DiagID = 0;
1578 case LEK_LockedSomePredecessors:
1579 DiagID = diag::warn_lock_some_predecessors;
1581 case LEK_LockedSomeLoopIterations:
1582 DiagID = diag::warn_expecting_lock_held_on_loop;
1584 case LEK_LockedAtEndOfFunction:
1585 DiagID = diag::warn_no_unlock;
1587 case LEK_NotLockedAtEndOfFunction:
1588 DiagID = diag::warn_expecting_locked;
1591 if (LocEndOfScope.isInvalid())
1592 LocEndOfScope = FunEndLocation;
1594 PartialDiagnosticAt Warning(LocEndOfScope, S.PDiag(DiagID) << Kind
1596 if (LocLocked.isValid()) {
1597 PartialDiagnosticAt Note(LocLocked, S.PDiag(diag::note_locked_here)
1599 Warnings.emplace_back(std::move(Warning), getNotes(Note));
1602 Warnings.emplace_back(std::move(Warning), getNotes());
1605 void handleExclusiveAndShared(StringRef Kind, Name LockName,
1606 SourceLocation Loc1,
1607 SourceLocation Loc2) override {
1608 PartialDiagnosticAt Warning(Loc1,
1609 S.PDiag(diag::warn_lock_exclusive_and_shared)
1610 << Kind << LockName);
1611 PartialDiagnosticAt Note(Loc2, S.PDiag(diag::note_lock_exclusive_and_shared)
1612 << Kind << LockName);
1613 Warnings.emplace_back(std::move(Warning), getNotes(Note));
1616 void handleNoMutexHeld(StringRef Kind, const NamedDecl *D,
1617 ProtectedOperationKind POK, AccessKind AK,
1618 SourceLocation Loc) override {
1619 assert((POK == POK_VarAccess || POK == POK_VarDereference) &&
1620 "Only works for variables");
1621 unsigned DiagID = POK == POK_VarAccess?
1622 diag::warn_variable_requires_any_lock:
1623 diag::warn_var_deref_requires_any_lock;
1624 PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID)
1625 << D->getNameAsString() << getLockKindFromAccessKind(AK));
1626 Warnings.emplace_back(std::move(Warning), getNotes());
1629 void handleMutexNotHeld(StringRef Kind, const NamedDecl *D,
1630 ProtectedOperationKind POK, Name LockName,
1631 LockKind LK, SourceLocation Loc,
1632 Name *PossibleMatch) override {
1633 unsigned DiagID = 0;
1634 if (PossibleMatch) {
1637 DiagID = diag::warn_variable_requires_lock_precise;
1639 case POK_VarDereference:
1640 DiagID = diag::warn_var_deref_requires_lock_precise;
1642 case POK_FunctionCall:
1643 DiagID = diag::warn_fun_requires_lock_precise;
1646 DiagID = diag::warn_guarded_pass_by_reference;
1648 case POK_PtPassByRef:
1649 DiagID = diag::warn_pt_guarded_pass_by_reference;
1652 PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID) << Kind
1653 << D->getNameAsString()
1655 PartialDiagnosticAt Note(Loc, S.PDiag(diag::note_found_mutex_near_match)
1657 if (Verbose && POK == POK_VarAccess) {
1658 PartialDiagnosticAt VNote(D->getLocation(),
1659 S.PDiag(diag::note_guarded_by_declared_here)
1660 << D->getNameAsString());
1661 Warnings.emplace_back(std::move(Warning), getNotes(Note, VNote));
1663 Warnings.emplace_back(std::move(Warning), getNotes(Note));
1667 DiagID = diag::warn_variable_requires_lock;
1669 case POK_VarDereference:
1670 DiagID = diag::warn_var_deref_requires_lock;
1672 case POK_FunctionCall:
1673 DiagID = diag::warn_fun_requires_lock;
1676 DiagID = diag::warn_guarded_pass_by_reference;
1678 case POK_PtPassByRef:
1679 DiagID = diag::warn_pt_guarded_pass_by_reference;
1682 PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID) << Kind
1683 << D->getNameAsString()
1685 if (Verbose && POK == POK_VarAccess) {
1686 PartialDiagnosticAt Note(D->getLocation(),
1687 S.PDiag(diag::note_guarded_by_declared_here)
1688 << D->getNameAsString());
1689 Warnings.emplace_back(std::move(Warning), getNotes(Note));
1691 Warnings.emplace_back(std::move(Warning), getNotes());
1695 void handleNegativeNotHeld(StringRef Kind, Name LockName, Name Neg,
1696 SourceLocation Loc) override {
1697 PartialDiagnosticAt Warning(Loc,
1698 S.PDiag(diag::warn_acquire_requires_negative_cap)
1699 << Kind << LockName << Neg);
1700 Warnings.emplace_back(std::move(Warning), getNotes());
1703 void handleFunExcludesLock(StringRef Kind, Name FunName, Name LockName,
1704 SourceLocation Loc) override {
1705 PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_fun_excludes_mutex)
1706 << Kind << FunName << LockName);
1707 Warnings.emplace_back(std::move(Warning), getNotes());
1710 void handleLockAcquiredBefore(StringRef Kind, Name L1Name, Name L2Name,
1711 SourceLocation Loc) override {
1712 PartialDiagnosticAt Warning(Loc,
1713 S.PDiag(diag::warn_acquired_before) << Kind << L1Name << L2Name);
1714 Warnings.emplace_back(std::move(Warning), getNotes());
1717 void handleBeforeAfterCycle(Name L1Name, SourceLocation Loc) override {
1718 PartialDiagnosticAt Warning(Loc,
1719 S.PDiag(diag::warn_acquired_before_after_cycle) << L1Name);
1720 Warnings.emplace_back(std::move(Warning), getNotes());
1723 void enterFunction(const FunctionDecl* FD) override {
1724 CurrentFunction = FD;
1727 void leaveFunction(const FunctionDecl* FD) override {
1728 CurrentFunction = nullptr;
1731 } // anonymous namespace
1732 } // namespace threadSafety
1733 } // namespace clang
1735 //===----------------------------------------------------------------------===//
1737 //===----------------------------------------------------------------------===//
1740 namespace consumed {
1742 class ConsumedWarningsHandler : public ConsumedWarningsHandlerBase {
1749 ConsumedWarningsHandler(Sema &S) : S(S) {}
1751 void emitDiagnostics() override {
1752 Warnings.sort(SortDiagBySourceLocation(S.getSourceManager()));
1753 for (const auto &Diag : Warnings) {
1754 S.Diag(Diag.first.first, Diag.first.second);
1755 for (const auto &Note : Diag.second)
1756 S.Diag(Note.first, Note.second);
1760 void warnLoopStateMismatch(SourceLocation Loc,
1761 StringRef VariableName) override {
1762 PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_loop_state_mismatch) <<
1765 Warnings.emplace_back(std::move(Warning), OptionalNotes());
1768 void warnParamReturnTypestateMismatch(SourceLocation Loc,
1769 StringRef VariableName,
1770 StringRef ExpectedState,
1771 StringRef ObservedState) override {
1773 PartialDiagnosticAt Warning(Loc, S.PDiag(
1774 diag::warn_param_return_typestate_mismatch) << VariableName <<
1775 ExpectedState << ObservedState);
1777 Warnings.emplace_back(std::move(Warning), OptionalNotes());
1780 void warnParamTypestateMismatch(SourceLocation Loc, StringRef ExpectedState,
1781 StringRef ObservedState) override {
1783 PartialDiagnosticAt Warning(Loc, S.PDiag(
1784 diag::warn_param_typestate_mismatch) << ExpectedState << ObservedState);
1786 Warnings.emplace_back(std::move(Warning), OptionalNotes());
1789 void warnReturnTypestateForUnconsumableType(SourceLocation Loc,
1790 StringRef TypeName) override {
1791 PartialDiagnosticAt Warning(Loc, S.PDiag(
1792 diag::warn_return_typestate_for_unconsumable_type) << TypeName);
1794 Warnings.emplace_back(std::move(Warning), OptionalNotes());
1797 void warnReturnTypestateMismatch(SourceLocation Loc, StringRef ExpectedState,
1798 StringRef ObservedState) override {
1800 PartialDiagnosticAt Warning(Loc, S.PDiag(
1801 diag::warn_return_typestate_mismatch) << ExpectedState << ObservedState);
1803 Warnings.emplace_back(std::move(Warning), OptionalNotes());
1806 void warnUseOfTempInInvalidState(StringRef MethodName, StringRef State,
1807 SourceLocation Loc) override {
1809 PartialDiagnosticAt Warning(Loc, S.PDiag(
1810 diag::warn_use_of_temp_in_invalid_state) << MethodName << State);
1812 Warnings.emplace_back(std::move(Warning), OptionalNotes());
1815 void warnUseInInvalidState(StringRef MethodName, StringRef VariableName,
1816 StringRef State, SourceLocation Loc) override {
1818 PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_use_in_invalid_state) <<
1819 MethodName << VariableName << State);
1821 Warnings.emplace_back(std::move(Warning), OptionalNotes());
1824 } // anonymous namespace
1825 } // namespace consumed
1826 } // namespace clang
1828 //===----------------------------------------------------------------------===//
1829 // AnalysisBasedWarnings - Worker object used by Sema to execute analysis-based
1830 // warnings on a function, method, or block.
1831 //===----------------------------------------------------------------------===//
1833 clang::sema::AnalysisBasedWarnings::Policy::Policy() {
1834 enableCheckFallThrough = 1;
1835 enableCheckUnreachable = 0;
1836 enableThreadSafetyAnalysis = 0;
1837 enableConsumedAnalysis = 0;
1840 static unsigned isEnabled(DiagnosticsEngine &D, unsigned diag) {
1841 return (unsigned)!D.isIgnored(diag, SourceLocation());
1844 clang::sema::AnalysisBasedWarnings::AnalysisBasedWarnings(Sema &s)
1846 NumFunctionsAnalyzed(0),
1847 NumFunctionsWithBadCFGs(0),
1849 MaxCFGBlocksPerFunction(0),
1850 NumUninitAnalysisFunctions(0),
1851 NumUninitAnalysisVariables(0),
1852 MaxUninitAnalysisVariablesPerFunction(0),
1853 NumUninitAnalysisBlockVisits(0),
1854 MaxUninitAnalysisBlockVisitsPerFunction(0) {
1856 using namespace diag;
1857 DiagnosticsEngine &D = S.getDiagnostics();
1859 DefaultPolicy.enableCheckUnreachable =
1860 isEnabled(D, warn_unreachable) ||
1861 isEnabled(D, warn_unreachable_break) ||
1862 isEnabled(D, warn_unreachable_return) ||
1863 isEnabled(D, warn_unreachable_loop_increment);
1865 DefaultPolicy.enableThreadSafetyAnalysis =
1866 isEnabled(D, warn_double_lock);
1868 DefaultPolicy.enableConsumedAnalysis =
1869 isEnabled(D, warn_use_in_invalid_state);
1872 static void flushDiagnostics(Sema &S, const sema::FunctionScopeInfo *fscope) {
1873 for (const auto &D : fscope->PossiblyUnreachableDiags)
1874 S.Diag(D.Loc, D.PD);
1878 AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P,
1879 sema::FunctionScopeInfo *fscope,
1880 const Decl *D, const BlockExpr *blkExpr) {
1882 // We avoid doing analysis-based warnings when there are errors for
1884 // (1) The CFGs often can't be constructed (if the body is invalid), so
1885 // don't bother trying.
1886 // (2) The code already has problems; running the analysis just takes more
1888 DiagnosticsEngine &Diags = S.getDiagnostics();
1890 // Do not do any analysis for declarations in system headers if we are
1891 // going to just ignore them.
1892 if (Diags.getSuppressSystemWarnings() &&
1893 S.SourceMgr.isInSystemHeader(D->getLocation()))
1896 // For code in dependent contexts, we'll do this at instantiation time.
1897 if (cast<DeclContext>(D)->isDependentContext())
1900 if (Diags.hasUncompilableErrorOccurred()) {
1901 // Flush out any possibly unreachable diagnostics.
1902 flushDiagnostics(S, fscope);
1906 const Stmt *Body = D->getBody();
1909 // Construct the analysis context with the specified CFG build options.
1910 AnalysisDeclContext AC(/* AnalysisDeclContextManager */ nullptr, D);
1912 // Don't generate EH edges for CallExprs as we'd like to avoid the n^2
1913 // explosion for destructors that can result and the compile time hit.
1914 AC.getCFGBuildOptions().PruneTriviallyFalseEdges = true;
1915 AC.getCFGBuildOptions().AddEHEdges = false;
1916 AC.getCFGBuildOptions().AddInitializers = true;
1917 AC.getCFGBuildOptions().AddImplicitDtors = true;
1918 AC.getCFGBuildOptions().AddTemporaryDtors = true;
1919 AC.getCFGBuildOptions().AddCXXNewAllocator = false;
1920 AC.getCFGBuildOptions().AddCXXDefaultInitExprInCtors = true;
1922 // Force that certain expressions appear as CFGElements in the CFG. This
1923 // is used to speed up various analyses.
1924 // FIXME: This isn't the right factoring. This is here for initial
1925 // prototyping, but we need a way for analyses to say what expressions they
1926 // expect to always be CFGElements and then fill in the BuildOptions
1927 // appropriately. This is essentially a layering violation.
1928 if (P.enableCheckUnreachable || P.enableThreadSafetyAnalysis ||
1929 P.enableConsumedAnalysis) {
1930 // Unreachable code analysis and thread safety require a linearized CFG.
1931 AC.getCFGBuildOptions().setAllAlwaysAdd();
1934 AC.getCFGBuildOptions()
1935 .setAlwaysAdd(Stmt::BinaryOperatorClass)
1936 .setAlwaysAdd(Stmt::CompoundAssignOperatorClass)
1937 .setAlwaysAdd(Stmt::BlockExprClass)
1938 .setAlwaysAdd(Stmt::CStyleCastExprClass)
1939 .setAlwaysAdd(Stmt::DeclRefExprClass)
1940 .setAlwaysAdd(Stmt::ImplicitCastExprClass)
1941 .setAlwaysAdd(Stmt::UnaryOperatorClass)
1942 .setAlwaysAdd(Stmt::AttributedStmtClass);
1945 // Install the logical handler for -Wtautological-overlap-compare
1946 std::unique_ptr<LogicalErrorHandler> LEH;
1947 if (!Diags.isIgnored(diag::warn_tautological_overlap_comparison,
1948 D->getLocStart())) {
1949 LEH.reset(new LogicalErrorHandler(S));
1950 AC.getCFGBuildOptions().Observer = LEH.get();
1953 // Emit delayed diagnostics.
1954 if (!fscope->PossiblyUnreachableDiags.empty()) {
1955 bool analyzed = false;
1957 // Register the expressions with the CFGBuilder.
1958 for (const auto &D : fscope->PossiblyUnreachableDiags) {
1960 AC.registerForcedBlockExpression(D.stmt);
1965 for (const auto &D : fscope->PossiblyUnreachableDiags) {
1966 bool processed = false;
1968 const CFGBlock *block = AC.getBlockForRegisteredExpression(D.stmt);
1969 CFGReverseBlockReachabilityAnalysis *cra =
1970 AC.getCFGReachablityAnalysis();
1971 // FIXME: We should be able to assert that block is non-null, but
1972 // the CFG analysis can skip potentially-evaluated expressions in
1973 // edge cases; see test/Sema/vla-2.c.
1975 // Can this block be reached from the entrance?
1976 if (cra->isReachable(&AC.getCFG()->getEntry(), block))
1977 S.Diag(D.Loc, D.PD);
1982 // Emit the warning anyway if we cannot map to a basic block.
1983 S.Diag(D.Loc, D.PD);
1989 flushDiagnostics(S, fscope);
1992 // Warning: check missing 'return'
1993 if (P.enableCheckFallThrough) {
1994 const CheckFallThroughDiagnostics &CD =
1995 (isa<BlockDecl>(D) ? CheckFallThroughDiagnostics::MakeForBlock()
1996 : (isa<CXXMethodDecl>(D) &&
1997 cast<CXXMethodDecl>(D)->getOverloadedOperator() == OO_Call &&
1998 cast<CXXMethodDecl>(D)->getParent()->isLambda())
1999 ? CheckFallThroughDiagnostics::MakeForLambda()
2000 : CheckFallThroughDiagnostics::MakeForFunction(D));
2001 CheckFallThroughForBody(S, D, Body, blkExpr, CD, AC);
2004 // Warning: check for unreachable code
2005 if (P.enableCheckUnreachable) {
2006 // Only check for unreachable code on non-template instantiations.
2007 // Different template instantiations can effectively change the control-flow
2008 // and it is very difficult to prove that a snippet of code in a template
2009 // is unreachable for all instantiations.
2010 bool isTemplateInstantiation = false;
2011 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
2012 isTemplateInstantiation = Function->isTemplateInstantiation();
2013 if (!isTemplateInstantiation)
2014 CheckUnreachable(S, AC);
2017 // Check for thread safety violations
2018 if (P.enableThreadSafetyAnalysis) {
2019 SourceLocation FL = AC.getDecl()->getLocation();
2020 SourceLocation FEL = AC.getDecl()->getLocEnd();
2021 threadSafety::ThreadSafetyReporter Reporter(S, FL, FEL);
2022 if (!Diags.isIgnored(diag::warn_thread_safety_beta, D->getLocStart()))
2023 Reporter.setIssueBetaWarnings(true);
2024 if (!Diags.isIgnored(diag::warn_thread_safety_verbose, D->getLocStart()))
2025 Reporter.setVerbose(true);
2027 threadSafety::runThreadSafetyAnalysis(AC, Reporter,
2028 &S.ThreadSafetyDeclCache);
2029 Reporter.emitDiagnostics();
2032 // Check for violations of consumed properties.
2033 if (P.enableConsumedAnalysis) {
2034 consumed::ConsumedWarningsHandler WarningHandler(S);
2035 consumed::ConsumedAnalyzer Analyzer(WarningHandler);
2039 if (!Diags.isIgnored(diag::warn_uninit_var, D->getLocStart()) ||
2040 !Diags.isIgnored(diag::warn_sometimes_uninit_var, D->getLocStart()) ||
2041 !Diags.isIgnored(diag::warn_maybe_uninit_var, D->getLocStart())) {
2042 if (CFG *cfg = AC.getCFG()) {
2043 UninitValsDiagReporter reporter(S);
2044 UninitVariablesAnalysisStats stats;
2045 std::memset(&stats, 0, sizeof(UninitVariablesAnalysisStats));
2046 runUninitializedVariablesAnalysis(*cast<DeclContext>(D), *cfg, AC,
2049 if (S.CollectStats && stats.NumVariablesAnalyzed > 0) {
2050 ++NumUninitAnalysisFunctions;
2051 NumUninitAnalysisVariables += stats.NumVariablesAnalyzed;
2052 NumUninitAnalysisBlockVisits += stats.NumBlockVisits;
2053 MaxUninitAnalysisVariablesPerFunction =
2054 std::max(MaxUninitAnalysisVariablesPerFunction,
2055 stats.NumVariablesAnalyzed);
2056 MaxUninitAnalysisBlockVisitsPerFunction =
2057 std::max(MaxUninitAnalysisBlockVisitsPerFunction,
2058 stats.NumBlockVisits);
2063 bool FallThroughDiagFull =
2064 !Diags.isIgnored(diag::warn_unannotated_fallthrough, D->getLocStart());
2065 bool FallThroughDiagPerFunction = !Diags.isIgnored(
2066 diag::warn_unannotated_fallthrough_per_function, D->getLocStart());
2067 if (FallThroughDiagFull || FallThroughDiagPerFunction ||
2068 fscope->HasFallthroughStmt) {
2069 DiagnoseSwitchLabelsFallthrough(S, AC, !FallThroughDiagFull);
2072 if (S.getLangOpts().ObjCWeak &&
2073 !Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, D->getLocStart()))
2074 diagnoseRepeatedUseOfWeak(S, fscope, D, AC.getParentMap());
2077 // Check for infinite self-recursion in functions
2078 if (!Diags.isIgnored(diag::warn_infinite_recursive_function,
2079 D->getLocStart())) {
2080 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2081 checkRecursiveFunction(S, FD, Body, AC);
2085 // If none of the previous checks caused a CFG build, trigger one here
2086 // for -Wtautological-overlap-compare
2087 if (!Diags.isIgnored(diag::warn_tautological_overlap_comparison,
2088 D->getLocStart())) {
2092 // Collect statistics about the CFG if it was built.
2093 if (S.CollectStats && AC.isCFGBuilt()) {
2094 ++NumFunctionsAnalyzed;
2095 if (CFG *cfg = AC.getCFG()) {
2096 // If we successfully built a CFG for this context, record some more
2097 // detail information about it.
2098 NumCFGBlocks += cfg->getNumBlockIDs();
2099 MaxCFGBlocksPerFunction = std::max(MaxCFGBlocksPerFunction,
2100 cfg->getNumBlockIDs());
2102 ++NumFunctionsWithBadCFGs;
2107 void clang::sema::AnalysisBasedWarnings::PrintStats() const {
2108 llvm::errs() << "\n*** Analysis Based Warnings Stats:\n";
2110 unsigned NumCFGsBuilt = NumFunctionsAnalyzed - NumFunctionsWithBadCFGs;
2111 unsigned AvgCFGBlocksPerFunction =
2112 !NumCFGsBuilt ? 0 : NumCFGBlocks/NumCFGsBuilt;
2113 llvm::errs() << NumFunctionsAnalyzed << " functions analyzed ("
2114 << NumFunctionsWithBadCFGs << " w/o CFGs).\n"
2115 << " " << NumCFGBlocks << " CFG blocks built.\n"
2116 << " " << AvgCFGBlocksPerFunction
2117 << " average CFG blocks per function.\n"
2118 << " " << MaxCFGBlocksPerFunction
2119 << " max CFG blocks per function.\n";
2121 unsigned AvgUninitVariablesPerFunction = !NumUninitAnalysisFunctions ? 0
2122 : NumUninitAnalysisVariables/NumUninitAnalysisFunctions;
2123 unsigned AvgUninitBlockVisitsPerFunction = !NumUninitAnalysisFunctions ? 0
2124 : NumUninitAnalysisBlockVisits/NumUninitAnalysisFunctions;
2125 llvm::errs() << NumUninitAnalysisFunctions
2126 << " functions analyzed for uninitialiazed variables\n"
2127 << " " << NumUninitAnalysisVariables << " variables analyzed.\n"
2128 << " " << AvgUninitVariablesPerFunction
2129 << " average variables per function.\n"
2130 << " " << MaxUninitAnalysisVariablesPerFunction
2131 << " max variables per function.\n"
2132 << " " << NumUninitAnalysisBlockVisits << " block visits.\n"
2133 << " " << AvgUninitBlockVisitsPerFunction
2134 << " average block visits per function.\n"
2135 << " " << MaxUninitAnalysisBlockVisitsPerFunction
2136 << " max block visits per function.\n";