#include "clang/Sema/AnalysisBasedWarnings.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/EvaluatedExprVisitor.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/ExprObjC.h"
#include "clang/AST/ParentMap.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/StmtCXX.h"
#include "clang/AST/StmtObjC.h"
#include "clang/AST/StmtVisitor.h"
#include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h"
#include "clang/Analysis/Analyses/CalledOnceCheck.h"
#include "clang/Analysis/Analyses/Consumed.h"
#include "clang/Analysis/Analyses/ReachableCode.h"
#include "clang/Analysis/Analyses/ThreadSafety.h"
#include "clang/Analysis/Analyses/UninitializedValues.h"
#include "clang/Analysis/AnalysisDeclContext.h"
#include "clang/Analysis/CFG.h"
#include "clang/Analysis/CFGStmtMap.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Sema/ScopeInfo.h"
#include "clang/Sema/SemaInternal.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/BitVector.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Casting.h"
#include <algorithm>
#include <deque>
#include <iterator>
using namespace clang;
namespace {
class UnreachableCodeHandler : public reachable_code::Callback {
Sema &S;
SourceRange PreviousSilenceableCondVal;
public:
UnreachableCodeHandler(Sema &s) : S(s) {}
void HandleUnreachable(reachable_code::UnreachableKind UK,
SourceLocation L,
SourceRange SilenceableCondVal,
SourceRange R1,
SourceRange R2) override {
if (PreviousSilenceableCondVal.isValid() &&
SilenceableCondVal.isValid() &&
PreviousSilenceableCondVal == SilenceableCondVal)
return;
PreviousSilenceableCondVal = SilenceableCondVal;
unsigned diag = diag::warn_unreachable;
switch (UK) {
case reachable_code::UK_Break:
diag = diag::warn_unreachable_break;
break;
case reachable_code::UK_Return:
diag = diag::warn_unreachable_return;
break;
case reachable_code::UK_Loop_Increment:
diag = diag::warn_unreachable_loop_increment;
break;
case reachable_code::UK_Other:
break;
}
S.Diag(L, diag) << R1 << R2;
SourceLocation Open = SilenceableCondVal.getBegin();
if (Open.isValid()) {
SourceLocation Close = SilenceableCondVal.getEnd();
Close = S.getLocForEndOfToken(Close);
if (Close.isValid()) {
S.Diag(Open, diag::note_unreachable_silence)
<< FixItHint::CreateInsertion(Open, "/* DISABLES CODE */ (")
<< FixItHint::CreateInsertion(Close, ")");
}
}
}
};
}
static void CheckUnreachable(Sema &S, AnalysisDeclContext &AC) {
if (!S.getSourceManager().isInMainFile(AC.getDecl()->getBeginLoc()))
return;
UnreachableCodeHandler UC(S);
reachable_code::FindUnreachableCode(AC, S.getPreprocessor(), UC);
}
namespace {
class LogicalErrorHandler : public CFGCallback {
Sema &S;
public:
LogicalErrorHandler(Sema &S) : S(S) {}
static bool HasMacroID(const Expr *E) {
if (E->getExprLoc().isMacroID())
return true;
for (const Stmt *SubStmt : E->children())
if (const Expr *SubExpr = dyn_cast_or_null<Expr>(SubStmt))
if (HasMacroID(SubExpr))
return true;
return false;
}
void compareAlwaysTrue(const BinaryOperator *B, bool isAlwaysTrue) override {
if (HasMacroID(B))
return;
SourceRange DiagRange = B->getSourceRange();
S.Diag(B->getExprLoc(), diag::warn_tautological_overlap_comparison)
<< DiagRange << isAlwaysTrue;
}
void compareBitwiseEquality(const BinaryOperator *B,
bool isAlwaysTrue) override {
if (HasMacroID(B))
return;
SourceRange DiagRange = B->getSourceRange();
S.Diag(B->getExprLoc(), diag::warn_comparison_bitwise_always)
<< DiagRange << isAlwaysTrue;
}
void compareBitwiseOr(const BinaryOperator *B) override {
if (HasMacroID(B))
return;
SourceRange DiagRange = B->getSourceRange();
S.Diag(B->getExprLoc(), diag::warn_comparison_bitwise_or) << DiagRange;
}
static bool hasActiveDiagnostics(DiagnosticsEngine &Diags,
SourceLocation Loc) {
return !Diags.isIgnored(diag::warn_tautological_overlap_comparison, Loc) ||
!Diags.isIgnored(diag::warn_comparison_bitwise_or, Loc);
}
};
}
static bool hasRecursiveCallInPath(const FunctionDecl *FD, CFGBlock &Block) {
for (const auto &B : Block) {
if (B.getKind() != CFGElement::Statement)
continue;
const CallExpr *CE = dyn_cast<CallExpr>(B.getAs<CFGStmt>()->getStmt());
if (!CE || !CE->getCalleeDecl() ||
CE->getCalleeDecl()->getCanonicalDecl() != FD)
continue;
if (const DeclRefExpr *DRE =
dyn_cast<DeclRefExpr>(CE->getCallee()->IgnoreParenImpCasts())) {
if (NestedNameSpecifier *NNS = DRE->getQualifier()) {
if (NNS->getKind() == NestedNameSpecifier::TypeSpec &&
isa<TemplateSpecializationType>(NNS->getAsType())) {
continue;
}
}
}
const CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(CE);
if (!MCE || isa<CXXThisExpr>(MCE->getImplicitObjectArgument()) ||
!MCE->getMethodDecl()->isVirtual())
return true;
}
return false;
}
static bool checkForRecursiveFunctionCall(const FunctionDecl *FD, CFG *cfg) {
llvm::SmallPtrSet<CFGBlock *, 16> Visited;
llvm::SmallVector<CFGBlock *, 16> WorkList;
bool foundRecursion = false;
const unsigned ExitID = cfg->getExit().getBlockID();
WorkList.push_back(&cfg->getEntry());
while (!WorkList.empty()) {
CFGBlock *Block = WorkList.pop_back_val();
for (auto I = Block->succ_begin(), E = Block->succ_end(); I != E; ++I) {
if (CFGBlock *SuccBlock = *I) {
if (!Visited.insert(SuccBlock).second)
continue;
if (ExitID == SuccBlock->getBlockID())
return false;
if (hasRecursiveCallInPath(FD, *SuccBlock)) {
foundRecursion = true;
continue;
}
WorkList.push_back(SuccBlock);
}
}
}
return foundRecursion;
}
static void checkRecursiveFunction(Sema &S, const FunctionDecl *FD,
const Stmt *Body, AnalysisDeclContext &AC) {
FD = FD->getCanonicalDecl();
if (FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate &&
FD->getTemplatedKind() != FunctionDecl::TK_MemberSpecialization)
return;
CFG *cfg = AC.getCFG();
if (!cfg) return;
if (cfg->getExit().pred_empty())
return;
if (checkForRecursiveFunctionCall(FD, cfg))
S.Diag(Body->getBeginLoc(), diag::warn_infinite_recursive_function);
}
static bool throwEscapes(Sema &S, const CXXThrowExpr *E, CFGBlock &ThrowBlock,
CFG *Body) {
SmallVector<CFGBlock *, 16> Stack;
llvm::BitVector Queued(Body->getNumBlockIDs());
Stack.push_back(&ThrowBlock);
Queued[ThrowBlock.getBlockID()] = true;
while (!Stack.empty()) {
CFGBlock &UnwindBlock = *Stack.back();
Stack.pop_back();
for (auto &Succ : UnwindBlock.succs()) {
if (!Succ.isReachable() || Queued[Succ->getBlockID()])
continue;
if (Succ->getBlockID() == Body->getExit().getBlockID())
return true;
if (auto *Catch =
dyn_cast_or_null<CXXCatchStmt>(Succ->getLabel())) {
QualType Caught = Catch->getCaughtType();
if (Caught.isNull() || !E->getSubExpr() || S.handlerCanCatch(Caught, E->getSubExpr()->getType()))
break;
} else {
Stack.push_back(Succ);
Queued[Succ->getBlockID()] = true;
}
}
}
return false;
}
static void visitReachableThrows(
CFG *BodyCFG,
llvm::function_ref<void(const CXXThrowExpr *, CFGBlock &)> Visit) {
llvm::BitVector Reachable(BodyCFG->getNumBlockIDs());
clang::reachable_code::ScanReachableFromBlock(&BodyCFG->getEntry(), Reachable);
for (CFGBlock *B : *BodyCFG) {
if (!Reachable[B->getBlockID()])
continue;
for (CFGElement &E : *B) {
Optional<CFGStmt> S = E.getAs<CFGStmt>();
if (!S)
continue;
if (auto *Throw = dyn_cast<CXXThrowExpr>(S->getStmt()))
Visit(Throw, *B);
}
}
}
static void EmitDiagForCXXThrowInNonThrowingFunc(Sema &S, SourceLocation OpLoc,
const FunctionDecl *FD) {
if (!S.getSourceManager().isInSystemHeader(OpLoc) &&
FD->getTypeSourceInfo()) {
S.Diag(OpLoc, diag::warn_throw_in_noexcept_func) << FD;
if (S.getLangOpts().CPlusPlus11 &&
(isa<CXXDestructorDecl>(FD) ||
FD->getDeclName().getCXXOverloadedOperator() == OO_Delete ||
FD->getDeclName().getCXXOverloadedOperator() == OO_Array_Delete)) {
if (const auto *Ty = FD->getTypeSourceInfo()->getType()->
getAs<FunctionProtoType>())
S.Diag(FD->getLocation(), diag::note_throw_in_dtor)
<< !isa<CXXDestructorDecl>(FD) << !Ty->hasExceptionSpec()
<< FD->getExceptionSpecSourceRange();
} else
S.Diag(FD->getLocation(), diag::note_throw_in_function)
<< FD->getExceptionSpecSourceRange();
}
}
static void checkThrowInNonThrowingFunc(Sema &S, const FunctionDecl *FD,
AnalysisDeclContext &AC) {
CFG *BodyCFG = AC.getCFG();
if (!BodyCFG)
return;
if (BodyCFG->getExit().pred_empty())
return;
visitReachableThrows(BodyCFG, [&](const CXXThrowExpr *Throw, CFGBlock &Block) {
if (throwEscapes(S, Throw, Block, BodyCFG))
EmitDiagForCXXThrowInNonThrowingFunc(S, Throw->getThrowLoc(), FD);
});
}
static bool isNoexcept(const FunctionDecl *FD) {
const auto *FPT = FD->getType()->castAs<FunctionProtoType>();
if (FPT->isNothrow() || FD->hasAttr<NoThrowAttr>())
return true;
return false;
}
enum ControlFlowKind {
UnknownFallThrough,
NeverFallThrough,
MaybeFallThrough,
AlwaysFallThrough,
NeverFallThroughOrReturn
};
static ControlFlowKind CheckFallThrough(AnalysisDeclContext &AC) {
CFG *cfg = AC.getCFG();
if (!cfg) return UnknownFallThrough;
llvm::BitVector live(cfg->getNumBlockIDs());
unsigned count = reachable_code::ScanReachableFromBlock(&cfg->getEntry(),
live);
bool AddEHEdges = AC.getAddEHEdges();
if (!AddEHEdges && count != cfg->getNumBlockIDs())
for (const auto *B : *cfg) {
if (!live[B->getBlockID()]) {
if (B->pred_begin() == B->pred_end()) {
const Stmt *Term = B->getTerminatorStmt();
if (Term && isa<CXXTryStmt>(Term))
count += reachable_code::ScanReachableFromBlock(B, live);
continue;
}
}
}
bool HasLiveReturn = false;
bool HasFakeEdge = false;
bool HasPlainEdge = false;
bool HasAbnormalEdge = false;
CFGBlock::FilterOptions FO;
FO.IgnoreDefaultsWithCoveredEnums = 1;
for (CFGBlock::filtered_pred_iterator I =
cfg->getExit().filtered_pred_start_end(FO);
I.hasMore(); ++I) {
const CFGBlock &B = **I;
if (!live[B.getBlockID()])
continue;
if (B.hasNoReturnElement()) {
HasAbnormalEdge = true;
continue;
}
CFGBlock::const_reverse_iterator ri = B.rbegin(), re = B.rend();
for ( ; ri != re ; ++ri)
if (ri->getAs<CFGStmt>())
break;
if (ri == re) {
const Stmt *Term = B.getTerminatorStmt();
if (Term && (isa<CXXTryStmt>(Term) || isa<ObjCAtTryStmt>(Term))) {
HasAbnormalEdge = true;
continue;
}
HasPlainEdge = true;
continue;
}
CFGStmt CS = ri->castAs<CFGStmt>();
const Stmt *S = CS.getStmt();
if (isa<ReturnStmt>(S) || isa<CoreturnStmt>(S)) {
HasLiveReturn = true;
continue;
}
if (isa<ObjCAtThrowStmt>(S)) {
HasFakeEdge = true;
continue;
}
if (isa<CXXThrowExpr>(S)) {
HasFakeEdge = true;
continue;
}
if (isa<MSAsmStmt>(S)) {
HasFakeEdge = true;
HasLiveReturn = true;
continue;
}
if (isa<CXXTryStmt>(S)) {
HasAbnormalEdge = true;
continue;
}
if (!llvm::is_contained(B.succs(), &cfg->getExit())) {
HasAbnormalEdge = true;
continue;
}
HasPlainEdge = true;
}
if (!HasPlainEdge) {
if (HasLiveReturn)
return NeverFallThrough;
return NeverFallThroughOrReturn;
}
if (HasAbnormalEdge || HasFakeEdge || HasLiveReturn)
return MaybeFallThrough;
return AlwaysFallThrough;
}
namespace {
struct CheckFallThroughDiagnostics {
unsigned diag_MaybeFallThrough_HasNoReturn;
unsigned diag_MaybeFallThrough_ReturnsNonVoid;
unsigned diag_AlwaysFallThrough_HasNoReturn;
unsigned diag_AlwaysFallThrough_ReturnsNonVoid;
unsigned diag_NeverFallThroughOrReturn;
enum { Function, Block, Lambda, Coroutine } funMode;
SourceLocation FuncLoc;
static CheckFallThroughDiagnostics MakeForFunction(const Decl *Func) {
CheckFallThroughDiagnostics D;
D.FuncLoc = Func->getLocation();
D.diag_MaybeFallThrough_HasNoReturn =
diag::warn_falloff_noreturn_function;
D.diag_MaybeFallThrough_ReturnsNonVoid =
diag::warn_maybe_falloff_nonvoid_function;
D.diag_AlwaysFallThrough_HasNoReturn =
diag::warn_falloff_noreturn_function;
D.diag_AlwaysFallThrough_ReturnsNonVoid =
diag::warn_falloff_nonvoid_function;
bool isVirtualMethod = false;
if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Func))
isVirtualMethod = Method->isVirtual();
bool isTemplateInstantiation = false;
if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(Func))
isTemplateInstantiation = Function->isTemplateInstantiation();
if (!isVirtualMethod && !isTemplateInstantiation)
D.diag_NeverFallThroughOrReturn =
diag::warn_suggest_noreturn_function;
else
D.diag_NeverFallThroughOrReturn = 0;
D.funMode = Function;
return D;
}
static CheckFallThroughDiagnostics MakeForCoroutine(const Decl *Func) {
CheckFallThroughDiagnostics D;
D.FuncLoc = Func->getLocation();
D.diag_MaybeFallThrough_HasNoReturn = 0;
D.diag_MaybeFallThrough_ReturnsNonVoid =
diag::warn_maybe_falloff_nonvoid_coroutine;
D.diag_AlwaysFallThrough_HasNoReturn = 0;
D.diag_AlwaysFallThrough_ReturnsNonVoid =
diag::warn_falloff_nonvoid_coroutine;
D.funMode = Coroutine;
return D;
}
static CheckFallThroughDiagnostics MakeForBlock() {
CheckFallThroughDiagnostics D;
D.diag_MaybeFallThrough_HasNoReturn =
diag::err_noreturn_block_has_return_expr;
D.diag_MaybeFallThrough_ReturnsNonVoid =
diag::err_maybe_falloff_nonvoid_block;
D.diag_AlwaysFallThrough_HasNoReturn =
diag::err_noreturn_block_has_return_expr;
D.diag_AlwaysFallThrough_ReturnsNonVoid =
diag::err_falloff_nonvoid_block;
D.diag_NeverFallThroughOrReturn = 0;
D.funMode = Block;
return D;
}
static CheckFallThroughDiagnostics MakeForLambda() {
CheckFallThroughDiagnostics D;
D.diag_MaybeFallThrough_HasNoReturn =
diag::err_noreturn_lambda_has_return_expr;
D.diag_MaybeFallThrough_ReturnsNonVoid =
diag::warn_maybe_falloff_nonvoid_lambda;
D.diag_AlwaysFallThrough_HasNoReturn =
diag::err_noreturn_lambda_has_return_expr;
D.diag_AlwaysFallThrough_ReturnsNonVoid =
diag::warn_falloff_nonvoid_lambda;
D.diag_NeverFallThroughOrReturn = 0;
D.funMode = Lambda;
return D;
}
bool checkDiagnostics(DiagnosticsEngine &D, bool ReturnsVoid,
bool HasNoReturn) const {
if (funMode == Function) {
return (ReturnsVoid ||
D.isIgnored(diag::warn_maybe_falloff_nonvoid_function,
FuncLoc)) &&
(!HasNoReturn ||
D.isIgnored(diag::warn_noreturn_function_has_return_expr,
FuncLoc)) &&
(!ReturnsVoid ||
D.isIgnored(diag::warn_suggest_noreturn_block, FuncLoc));
}
if (funMode == Coroutine) {
return (ReturnsVoid ||
D.isIgnored(diag::warn_maybe_falloff_nonvoid_function, FuncLoc) ||
D.isIgnored(diag::warn_maybe_falloff_nonvoid_coroutine,
FuncLoc)) &&
(!HasNoReturn);
}
return ReturnsVoid && !HasNoReturn;
}
};
}
static void CheckFallThroughForBody(Sema &S, const Decl *D, const Stmt *Body,
QualType BlockType,
const CheckFallThroughDiagnostics &CD,
AnalysisDeclContext &AC,
sema::FunctionScopeInfo *FSI) {
bool ReturnsVoid = false;
bool HasNoReturn = false;
bool IsCoroutine = FSI->isCoroutine();
if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
if (const auto *CBody = dyn_cast<CoroutineBodyStmt>(Body))
ReturnsVoid = CBody->getFallthroughHandler() != nullptr;
else
ReturnsVoid = FD->getReturnType()->isVoidType();
HasNoReturn = FD->isNoReturn();
}
else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
ReturnsVoid = MD->getReturnType()->isVoidType();
HasNoReturn = MD->hasAttr<NoReturnAttr>();
}
else if (isa<BlockDecl>(D)) {
if (const FunctionType *FT =
BlockType->getPointeeType()->getAs<FunctionType>()) {
if (FT->getReturnType()->isVoidType())
ReturnsVoid = true;
if (FT->getNoReturnAttr())
HasNoReturn = true;
}
}
DiagnosticsEngine &Diags = S.getDiagnostics();
if (CD.checkDiagnostics(Diags, ReturnsVoid, HasNoReturn))
return;
SourceLocation LBrace = Body->getBeginLoc(), RBrace = Body->getEndLoc();
auto EmitDiag = [&](SourceLocation Loc, unsigned DiagID) {
if (IsCoroutine)
S.Diag(Loc, DiagID) << FSI->CoroutinePromise->getType();
else
S.Diag(Loc, DiagID);
};
if (D->getAsFunction() && D->getAsFunction()->isCPUDispatchMultiVersion())
return;
switch (CheckFallThrough(AC)) {
case UnknownFallThrough:
break;
case MaybeFallThrough:
if (HasNoReturn)
EmitDiag(RBrace, CD.diag_MaybeFallThrough_HasNoReturn);
else if (!ReturnsVoid)
EmitDiag(RBrace, CD.diag_MaybeFallThrough_ReturnsNonVoid);
break;
case AlwaysFallThrough:
if (HasNoReturn)
EmitDiag(RBrace, CD.diag_AlwaysFallThrough_HasNoReturn);
else if (!ReturnsVoid)
EmitDiag(RBrace, CD.diag_AlwaysFallThrough_ReturnsNonVoid);
break;
case NeverFallThroughOrReturn:
if (ReturnsVoid && !HasNoReturn && CD.diag_NeverFallThroughOrReturn) {
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
S.Diag(LBrace, CD.diag_NeverFallThroughOrReturn) << 0 << FD;
} else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
S.Diag(LBrace, CD.diag_NeverFallThroughOrReturn) << 1 << MD;
} else {
S.Diag(LBrace, CD.diag_NeverFallThroughOrReturn);
}
}
break;
case NeverFallThrough:
break;
}
}
namespace {
class ContainsReference : public ConstEvaluatedExprVisitor<ContainsReference> {
bool FoundReference;
const DeclRefExpr *Needle;
public:
typedef ConstEvaluatedExprVisitor<ContainsReference> Inherited;
ContainsReference(ASTContext &Context, const DeclRefExpr *Needle)
: Inherited(Context), FoundReference(false), Needle(Needle) {}
void VisitExpr(const Expr *E) {
if (FoundReference)
return;
Inherited::VisitExpr(E);
}
void VisitDeclRefExpr(const DeclRefExpr *E) {
if (E == Needle)
FoundReference = true;
else
Inherited::VisitDeclRefExpr(E);
}
bool doesContainReference() const { return FoundReference; }
};
}
static bool SuggestInitializationFixit(Sema &S, const VarDecl *VD) {
QualType VariableTy = VD->getType().getCanonicalType();
if (VariableTy->isBlockPointerType() &&
!VD->hasAttr<BlocksAttr>()) {
S.Diag(VD->getLocation(), diag::note_block_var_fixit_add_initialization)
<< VD->getDeclName()
<< FixItHint::CreateInsertion(VD->getLocation(), "__block ");
return true;
}
if (VD->getInit())
return false;
if (VD->getEndLoc().isMacroID())
return false;
SourceLocation Loc = S.getLocForEndOfToken(VD->getEndLoc());
std::string Init = S.getFixItZeroInitializerForType(VariableTy, Loc);
if (Init.empty())
return false;
S.Diag(Loc, diag::note_var_fixit_add_initialization) << VD->getDeclName()
<< FixItHint::CreateInsertion(Loc, Init);
return true;
}
static void CreateIfFixit(Sema &S, const Stmt *If, const Stmt *Then,
const Stmt *Else, bool CondVal,
FixItHint &Fixit1, FixItHint &Fixit2) {
if (CondVal) {
Fixit1 = FixItHint::CreateRemoval(
CharSourceRange::getCharRange(If->getBeginLoc(), Then->getBeginLoc()));
if (Else) {
SourceLocation ElseKwLoc = S.getLocForEndOfToken(Then->getEndLoc());
Fixit2 =
FixItHint::CreateRemoval(SourceRange(ElseKwLoc, Else->getEndLoc()));
}
} else {
if (Else)
Fixit1 = FixItHint::CreateRemoval(CharSourceRange::getCharRange(
If->getBeginLoc(), Else->getBeginLoc()));
else
Fixit1 = FixItHint::CreateRemoval(If->getSourceRange());
}
}
static void DiagUninitUse(Sema &S, const VarDecl *VD, const UninitUse &Use,
bool IsCapturedByBlock) {
bool Diagnosed = false;
switch (Use.getKind()) {
case UninitUse::Always:
S.Diag(Use.getUser()->getBeginLoc(), diag::warn_uninit_var)
<< VD->getDeclName() << IsCapturedByBlock
<< Use.getUser()->getSourceRange();
return;
case UninitUse::AfterDecl:
case UninitUse::AfterCall:
S.Diag(VD->getLocation(), diag::warn_sometimes_uninit_var)
<< VD->getDeclName() << IsCapturedByBlock
<< (Use.getKind() == UninitUse::AfterDecl ? 4 : 5)
<< const_cast<DeclContext*>(VD->getLexicalDeclContext())
<< VD->getSourceRange();
S.Diag(Use.getUser()->getBeginLoc(), diag::note_uninit_var_use)
<< IsCapturedByBlock << Use.getUser()->getSourceRange();
return;
case UninitUse::Maybe:
case UninitUse::Sometimes:
break;
}
for (UninitUse::branch_iterator I = Use.branch_begin(), E = Use.branch_end();
I != E; ++I) {
assert(Use.getKind() == UninitUse::Sometimes);
const Expr *User = Use.getUser();
const Stmt *Term = I->Terminator;
unsigned DiagKind;
StringRef Str;
SourceRange Range;
int RemoveDiagKind = -1;
const char *FixitStr =
S.getLangOpts().CPlusPlus ? (I->Output ? "true" : "false")
: (I->Output ? "1" : "0");
FixItHint Fixit1, Fixit2;
switch (Term ? Term->getStmtClass() : Stmt::DeclStmtClass) {
default:
continue;
case Stmt::IfStmtClass: {
const IfStmt *IS = cast<IfStmt>(Term);
DiagKind = 0;
Str = "if";
Range = IS->getCond()->getSourceRange();
RemoveDiagKind = 0;
CreateIfFixit(S, IS, IS->getThen(), IS->getElse(),
I->Output, Fixit1, Fixit2);
break;
}
case Stmt::ConditionalOperatorClass: {
const ConditionalOperator *CO = cast<ConditionalOperator>(Term);
DiagKind = 0;
Str = "?:";
Range = CO->getCond()->getSourceRange();
RemoveDiagKind = 0;
CreateIfFixit(S, CO, CO->getTrueExpr(), CO->getFalseExpr(),
I->Output, Fixit1, Fixit2);
break;
}
case Stmt::BinaryOperatorClass: {
const BinaryOperator *BO = cast<BinaryOperator>(Term);
if (!BO->isLogicalOp())
continue;
DiagKind = 0;
Str = BO->getOpcodeStr();
Range = BO->getLHS()->getSourceRange();
RemoveDiagKind = 0;
if ((BO->getOpcode() == BO_LAnd && I->Output) ||
(BO->getOpcode() == BO_LOr && !I->Output))
Fixit1 = FixItHint::CreateRemoval(
SourceRange(BO->getBeginLoc(), BO->getOperatorLoc()));
else
Fixit1 = FixItHint::CreateReplacement(BO->getSourceRange(), FixitStr);
break;
}
case Stmt::WhileStmtClass:
DiagKind = 1;
Str = "while";
Range = cast<WhileStmt>(Term)->getCond()->getSourceRange();
RemoveDiagKind = 1;
Fixit1 = FixItHint::CreateReplacement(Range, FixitStr);
break;
case Stmt::ForStmtClass:
DiagKind = 1;
Str = "for";
Range = cast<ForStmt>(Term)->getCond()->getSourceRange();
RemoveDiagKind = 1;
if (I->Output)
Fixit1 = FixItHint::CreateRemoval(Range);
else
Fixit1 = FixItHint::CreateReplacement(Range, FixitStr);
break;
case Stmt::CXXForRangeStmtClass:
if (I->Output == 1) {
continue;
}
DiagKind = 1;
Str = "for";
Range = cast<CXXForRangeStmt>(Term)->getRangeInit()->getSourceRange();
break;
case Stmt::DoStmtClass:
DiagKind = 2;
Str = "do";
Range = cast<DoStmt>(Term)->getCond()->getSourceRange();
RemoveDiagKind = 1;
Fixit1 = FixItHint::CreateReplacement(Range, FixitStr);
break;
case Stmt::CaseStmtClass:
DiagKind = 3;
Str = "case";
Range = cast<CaseStmt>(Term)->getLHS()->getSourceRange();
break;
case Stmt::DefaultStmtClass:
DiagKind = 3;
Str = "default";
Range = cast<DefaultStmt>(Term)->getDefaultLoc();
break;
}
S.Diag(Range.getBegin(), diag::warn_sometimes_uninit_var)
<< VD->getDeclName() << IsCapturedByBlock << DiagKind
<< Str << I->Output << Range;
S.Diag(User->getBeginLoc(), diag::note_uninit_var_use)
<< IsCapturedByBlock << User->getSourceRange();
if (RemoveDiagKind != -1)
S.Diag(Fixit1.RemoveRange.getBegin(), diag::note_uninit_fixit_remove_cond)
<< RemoveDiagKind << Str << I->Output << Fixit1 << Fixit2;
Diagnosed = true;
}
if (!Diagnosed)
S.Diag(Use.getUser()->getBeginLoc(), diag::warn_maybe_uninit_var)
<< VD->getDeclName() << IsCapturedByBlock
<< Use.getUser()->getSourceRange();
}
static bool DiagnoseUninitializedConstRefUse(Sema &S, const VarDecl *VD,
const UninitUse &Use) {
S.Diag(Use.getUser()->getBeginLoc(), diag::warn_uninit_const_reference)
<< VD->getDeclName() << Use.getUser()->getSourceRange();
return true;
}
static bool DiagnoseUninitializedUse(Sema &S, const VarDecl *VD,
const UninitUse &Use,
bool alwaysReportSelfInit = false) {
if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Use.getUser())) {
if (const Expr *Initializer = VD->getInit()) {
if (!alwaysReportSelfInit && DRE == Initializer->IgnoreParenImpCasts())
return false;
ContainsReference CR(S.Context, DRE);
CR.Visit(Initializer);
if (CR.doesContainReference()) {
S.Diag(DRE->getBeginLoc(), diag::warn_uninit_self_reference_in_init)
<< VD->getDeclName() << VD->getLocation() << DRE->getSourceRange();
return true;
}
}
DiagUninitUse(S, VD, Use, false);
} else {
const BlockExpr *BE = cast<BlockExpr>(Use.getUser());
if (VD->getType()->isBlockPointerType() && !VD->hasAttr<BlocksAttr>())
S.Diag(BE->getBeginLoc(),
diag::warn_uninit_byref_blockvar_captured_by_block)
<< VD->getDeclName()
<< VD->getType().getQualifiers().hasObjCLifetime();
else
DiagUninitUse(S, VD, Use, true);
}
if (!SuggestInitializationFixit(S, VD))
S.Diag(VD->getBeginLoc(), diag::note_var_declared_here)
<< VD->getDeclName();
return true;
}
namespace {
class FallthroughMapper : public RecursiveASTVisitor<FallthroughMapper> {
public:
FallthroughMapper(Sema &S)
: FoundSwitchStatements(false),
S(S) {
}
bool foundSwitchStatements() const { return FoundSwitchStatements; }
void markFallthroughVisited(const AttributedStmt *Stmt) {
bool Found = FallthroughStmts.erase(Stmt);
assert(Found);
(void)Found;
}
typedef llvm::SmallPtrSet<const AttributedStmt*, 8> AttrStmts;
const AttrStmts &getFallthroughStmts() const {
return FallthroughStmts;
}
void fillReachableBlocks(CFG *Cfg) {
assert(ReachableBlocks.empty() && "ReachableBlocks already filled");
std::deque<const CFGBlock *> BlockQueue;
ReachableBlocks.insert(&Cfg->getEntry());
BlockQueue.push_back(&Cfg->getEntry());
for (const auto *B : *Cfg) {
const Stmt *L = B->getLabel();
if (L && isa<SwitchCase>(L) && ReachableBlocks.insert(B).second)
BlockQueue.push_back(B);
}
while (!BlockQueue.empty()) {
const CFGBlock *P = BlockQueue.front();
BlockQueue.pop_front();
for (const CFGBlock *B : P->succs()) {
if (B && ReachableBlocks.insert(B).second)
BlockQueue.push_back(B);
}
}
}
bool checkFallThroughIntoBlock(const CFGBlock &B, int &AnnotatedCnt,
bool IsTemplateInstantiation) {
assert(!ReachableBlocks.empty() && "ReachableBlocks empty");
int UnannotatedCnt = 0;
AnnotatedCnt = 0;
std::deque<const CFGBlock*> BlockQueue(B.pred_begin(), B.pred_end());
while (!BlockQueue.empty()) {
const CFGBlock *P = BlockQueue.front();
BlockQueue.pop_front();
if (!P) continue;
const Stmt *Term = P->getTerminatorStmt();
if (Term && isa<SwitchStmt>(Term))
continue;
const SwitchCase *SW = dyn_cast_or_null<SwitchCase>(P->getLabel());
if (SW && SW->getSubStmt() == B.getLabel() && P->begin() == P->end())
continue;
const LabelStmt *L = dyn_cast_or_null<LabelStmt>(P->getLabel());
if (L && L->getSubStmt() == B.getLabel() && P->begin() == P->end())
continue;
if (!ReachableBlocks.count(P)) {
for (const CFGElement &Elem : llvm::reverse(*P)) {
if (Optional<CFGStmt> CS = Elem.getAs<CFGStmt>()) {
if (const AttributedStmt *AS = asFallThroughAttr(CS->getStmt())) {
if (!IsTemplateInstantiation)
S.Diag(AS->getBeginLoc(),
diag::warn_unreachable_fallthrough_attr);
markFallthroughVisited(AS);
++AnnotatedCnt;
break;
}
}
}
continue;
}
const Stmt *LastStmt = getLastStmt(*P);
if (const AttributedStmt *AS = asFallThroughAttr(LastStmt)) {
markFallthroughVisited(AS);
++AnnotatedCnt;
continue; }
if (!LastStmt) { std::copy(P->pred_begin(), P->pred_end(),
std::back_inserter(BlockQueue));
continue;
}
++UnannotatedCnt;
}
return !!UnannotatedCnt;
}
bool shouldWalkTypesOfTypeLocs() const { return false; }
bool VisitAttributedStmt(AttributedStmt *S) {
if (asFallThroughAttr(S))
FallthroughStmts.insert(S);
return true;
}
bool VisitSwitchStmt(SwitchStmt *S) {
FoundSwitchStatements = true;
return true;
}
bool TraverseDecl(Decl *D) { return true; }
bool TraverseLambdaExpr(LambdaExpr *LE) {
for (const auto C : zip(LE->captures(), LE->capture_inits()))
TraverseLambdaCapture(LE, &std::get<0>(C), std::get<1>(C));
return true;
}
private:
static const AttributedStmt *asFallThroughAttr(const Stmt *S) {
if (const AttributedStmt *AS = dyn_cast_or_null<AttributedStmt>(S)) {
if (hasSpecificAttr<FallThroughAttr>(AS->getAttrs()))
return AS;
}
return nullptr;
}
static const Stmt *getLastStmt(const CFGBlock &B) {
if (const Stmt *Term = B.getTerminatorStmt())
return Term;
for (const CFGElement &Elem : llvm::reverse(B))
if (Optional<CFGStmt> CS = Elem.getAs<CFGStmt>())
return CS->getStmt();
if (const SwitchCase *SW = dyn_cast_or_null<SwitchCase>(B.getLabel()))
if (!isa<SwitchCase>(SW->getSubStmt()))
return SW->getSubStmt();
return nullptr;
}
bool FoundSwitchStatements;
AttrStmts FallthroughStmts;
Sema &S;
llvm::SmallPtrSet<const CFGBlock *, 16> ReachableBlocks;
};
}
static StringRef getFallthroughAttrSpelling(Preprocessor &PP,
SourceLocation Loc) {
TokenValue FallthroughTokens[] = {
tok::l_square, tok::l_square,
PP.getIdentifierInfo("fallthrough"),
tok::r_square, tok::r_square
};
TokenValue ClangFallthroughTokens[] = {
tok::l_square, tok::l_square, PP.getIdentifierInfo("clang"),
tok::coloncolon, PP.getIdentifierInfo("fallthrough"),
tok::r_square, tok::r_square
};
bool PreferClangAttr = !PP.getLangOpts().CPlusPlus17 && !PP.getLangOpts().C2x;
StringRef MacroName;
if (PreferClangAttr)
MacroName = PP.getLastMacroWithSpelling(Loc, ClangFallthroughTokens);
if (MacroName.empty())
MacroName = PP.getLastMacroWithSpelling(Loc, FallthroughTokens);
if (MacroName.empty() && !PreferClangAttr)
MacroName = PP.getLastMacroWithSpelling(Loc, ClangFallthroughTokens);
if (MacroName.empty()) {
if (!PreferClangAttr)
MacroName = "[[fallthrough]]";
else if (PP.getLangOpts().CPlusPlus)
MacroName = "[[clang::fallthrough]]";
else
MacroName = "__attribute__((fallthrough))";
}
return MacroName;
}
static void DiagnoseSwitchLabelsFallthrough(Sema &S, AnalysisDeclContext &AC,
bool PerFunction) {
FallthroughMapper FM(S);
FM.TraverseStmt(AC.getBody());
if (!FM.foundSwitchStatements())
return;
if (PerFunction && FM.getFallthroughStmts().empty())
return;
CFG *Cfg = AC.getCFG();
if (!Cfg)
return;
FM.fillReachableBlocks(Cfg);
for (const CFGBlock *B : llvm::reverse(*Cfg)) {
const Stmt *Label = B->getLabel();
if (!isa_and_nonnull<SwitchCase>(Label))
continue;
int AnnotatedCnt;
bool IsTemplateInstantiation = false;
if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(AC.getDecl()))
IsTemplateInstantiation = Function->isTemplateInstantiation();
if (!FM.checkFallThroughIntoBlock(*B, AnnotatedCnt,
IsTemplateInstantiation))
continue;
S.Diag(Label->getBeginLoc(),
PerFunction ? diag::warn_unannotated_fallthrough_per_function
: diag::warn_unannotated_fallthrough);
if (!AnnotatedCnt) {
SourceLocation L = Label->getBeginLoc();
if (L.isMacroID())
continue;
const Stmt *Term = B->getTerminatorStmt();
while (B->empty() && !Term && B->succ_size() == 1) {
B = *B->succ_begin();
Term = B->getTerminatorStmt();
}
if (!(B->empty() && Term && isa<BreakStmt>(Term))) {
Preprocessor &PP = S.getPreprocessor();
StringRef AnnotationSpelling = getFallthroughAttrSpelling(PP, L);
SmallString<64> TextToInsert(AnnotationSpelling);
TextToInsert += "; ";
S.Diag(L, diag::note_insert_fallthrough_fixit)
<< AnnotationSpelling
<< FixItHint::CreateInsertion(L, TextToInsert);
}
S.Diag(L, diag::note_insert_break_fixit)
<< FixItHint::CreateInsertion(L, "break; ");
}
}
for (const auto *F : FM.getFallthroughStmts())
S.Diag(F->getBeginLoc(), diag::err_fallthrough_attr_invalid_placement);
}
static bool isInLoop(const ASTContext &Ctx, const ParentMap &PM,
const Stmt *S) {
assert(S);
do {
switch (S->getStmtClass()) {
case Stmt::ForStmtClass:
case Stmt::WhileStmtClass:
case Stmt::CXXForRangeStmtClass:
case Stmt::ObjCForCollectionStmtClass:
return true;
case Stmt::DoStmtClass: {
Expr::EvalResult Result;
if (!cast<DoStmt>(S)->getCond()->EvaluateAsInt(Result, Ctx))
return true;
return Result.Val.getInt().getBoolValue();
}
default:
break;
}
} while ((S = PM.getParent(S)));
return false;
}
static void diagnoseRepeatedUseOfWeak(Sema &S,
const sema::FunctionScopeInfo *CurFn,
const Decl *D,
const ParentMap &PM) {
typedef sema::FunctionScopeInfo::WeakObjectProfileTy WeakObjectProfileTy;
typedef sema::FunctionScopeInfo::WeakObjectUseMap WeakObjectUseMap;
typedef sema::FunctionScopeInfo::WeakUseVector WeakUseVector;
typedef std::pair<const Stmt *, WeakObjectUseMap::const_iterator>
StmtUsesPair;
ASTContext &Ctx = S.getASTContext();
const WeakObjectUseMap &WeakMap = CurFn->getWeakObjectUses();
SmallVector<StmtUsesPair, 8> UsesByStmt;
for (WeakObjectUseMap::const_iterator I = WeakMap.begin(), E = WeakMap.end();
I != E; ++I) {
const WeakUseVector &Uses = I->second;
WeakUseVector::const_iterator UI = Uses.begin(), UE = Uses.end();
for ( ; UI != UE; ++UI) {
if (UI->isUnsafe())
break;
}
if (UI == UE)
continue;
if (UI == Uses.begin()) {
WeakUseVector::const_iterator UI2 = UI;
for (++UI2; UI2 != UE; ++UI2)
if (UI2->isUnsafe())
break;
if (UI2 == UE) {
if (!isInLoop(Ctx, PM, UI->getUseExpr()))
continue;
const WeakObjectProfileTy &Profile = I->first;
if (!Profile.isExactProfile())
continue;
const NamedDecl *Base = Profile.getBase();
if (!Base)
Base = Profile.getProperty();
assert(Base && "A profile always has a base or property.");
if (const VarDecl *BaseVar = dyn_cast<VarDecl>(Base))
if (BaseVar->hasLocalStorage() && !isa<ParmVarDecl>(Base))
continue;
}
}
UsesByStmt.push_back(StmtUsesPair(UI->getUseExpr(), I));
}
if (UsesByStmt.empty())
return;
SourceManager &SM = S.getSourceManager();
llvm::sort(UsesByStmt,
[&SM](const StmtUsesPair &LHS, const StmtUsesPair &RHS) {
return SM.isBeforeInTranslationUnit(LHS.first->getBeginLoc(),
RHS.first->getBeginLoc());
});
enum {
Function,
Method,
Block,
Lambda
} FunctionKind;
if (isa<sema::BlockScopeInfo>(CurFn))
FunctionKind = Block;
else if (isa<sema::LambdaScopeInfo>(CurFn))
FunctionKind = Lambda;
else if (isa<ObjCMethodDecl>(D))
FunctionKind = Method;
else
FunctionKind = Function;
for (const auto &P : UsesByStmt) {
const Stmt *FirstRead = P.first;
const WeakObjectProfileTy &Key = P.second->first;
const WeakUseVector &Uses = P.second->second;
unsigned DiagKind;
if (Key.isExactProfile())
DiagKind = diag::warn_arc_repeated_use_of_weak;
else
DiagKind = diag::warn_arc_possible_repeated_use_of_weak;
enum {
Variable,
Property,
ImplicitProperty,
Ivar
} ObjectKind;
const NamedDecl *KeyProp = Key.getProperty();
if (isa<VarDecl>(KeyProp))
ObjectKind = Variable;
else if (isa<ObjCPropertyDecl>(KeyProp))
ObjectKind = Property;
else if (isa<ObjCMethodDecl>(KeyProp))
ObjectKind = ImplicitProperty;
else if (isa<ObjCIvarDecl>(KeyProp))
ObjectKind = Ivar;
else
llvm_unreachable("Unexpected weak object kind!");
if (const ObjCPropertyDecl *Prop = dyn_cast<ObjCPropertyDecl>(KeyProp))
if (Prop->hasAttr<IBOutletAttr>())
continue;
S.Diag(FirstRead->getBeginLoc(), DiagKind)
<< int(ObjectKind) << KeyProp << int(FunctionKind)
<< FirstRead->getSourceRange();
for (const auto &Use : Uses) {
if (Use.getUseExpr() == FirstRead)
continue;
S.Diag(Use.getUseExpr()->getBeginLoc(),
diag::note_arc_weak_also_accessed_here)
<< Use.getUseExpr()->getSourceRange();
}
}
}
namespace clang {
namespace {
typedef SmallVector<PartialDiagnosticAt, 1> OptionalNotes;
typedef std::pair<PartialDiagnosticAt, OptionalNotes> DelayedDiag;
typedef std::list<DelayedDiag> DiagList;
struct SortDiagBySourceLocation {
SourceManager &SM;
SortDiagBySourceLocation(SourceManager &SM) : SM(SM) {}
bool operator()(const DelayedDiag &left, const DelayedDiag &right) {
return SM.isBeforeInTranslationUnit(left.first.first, right.first.first);
}
};
} }
namespace {
class UninitValsDiagReporter : public UninitVariablesHandler {
Sema &S;
typedef SmallVector<UninitUse, 2> UsesVec;
typedef llvm::PointerIntPair<UsesVec *, 1, bool> MappedType;
typedef llvm::MapVector<const VarDecl *, MappedType> UsesMap;
UsesMap uses;
UsesMap constRefUses;
public:
UninitValsDiagReporter(Sema &S) : S(S) {}
~UninitValsDiagReporter() override { flushDiagnostics(); }
MappedType &getUses(UsesMap &um, const VarDecl *vd) {
MappedType &V = um[vd];
if (!V.getPointer())
V.setPointer(new UsesVec());
return V;
}
void handleUseOfUninitVariable(const VarDecl *vd,
const UninitUse &use) override {
getUses(uses, vd).getPointer()->push_back(use);
}
void handleConstRefUseOfUninitVariable(const VarDecl *vd,
const UninitUse &use) override {
getUses(constRefUses, vd).getPointer()->push_back(use);
}
void handleSelfInit(const VarDecl *vd) override {
getUses(uses, vd).setInt(true);
getUses(constRefUses, vd).setInt(true);
}
void flushDiagnostics() {
for (const auto &P : uses) {
const VarDecl *vd = P.first;
const MappedType &V = P.second;
UsesVec *vec = V.getPointer();
bool hasSelfInit = V.getInt();
if (!vec->empty() && hasSelfInit && hasAlwaysUninitializedUse(vec))
DiagnoseUninitializedUse(S, vd,
UninitUse(vd->getInit()->IgnoreParenCasts(),
true),
true);
else {
llvm::sort(*vec, [](const UninitUse &a, const UninitUse &b) {
if (a.getKind() != b.getKind())
return a.getKind() > b.getKind();
return a.getUser()->getBeginLoc() < b.getUser()->getBeginLoc();
});
for (const auto &U : *vec) {
UninitUse Use = hasSelfInit ? UninitUse(U.getUser(), false) : U;
if (DiagnoseUninitializedUse(S, vd, Use))
break;
}
}
delete vec;
}
uses.clear();
for (const auto &P : constRefUses) {
const VarDecl *vd = P.first;
const MappedType &V = P.second;
UsesVec *vec = V.getPointer();
bool hasSelfInit = V.getInt();
if (!vec->empty() && hasSelfInit && hasAlwaysUninitializedUse(vec))
DiagnoseUninitializedUse(S, vd,
UninitUse(vd->getInit()->IgnoreParenCasts(),
true),
true);
else {
for (const auto &U : *vec) {
if (DiagnoseUninitializedConstRefUse(S, vd, U))
break;
}
}
delete vec;
}
constRefUses.clear();
}
private:
static bool hasAlwaysUninitializedUse(const UsesVec* vec) {
return llvm::any_of(*vec, [](const UninitUse &U) {
return U.getKind() == UninitUse::Always ||
U.getKind() == UninitUse::AfterCall ||
U.getKind() == UninitUse::AfterDecl;
});
}
};
class CalledOnceInterProceduralData {
public:
void addDelayedWarning(const BlockDecl *Block,
PartialDiagnosticAt &&Warning) {
DelayedBlockWarnings[Block].emplace_back(std::move(Warning));
}
void flushWarnings(const BlockDecl *Block, Sema &S) {
for (const PartialDiagnosticAt &Delayed : DelayedBlockWarnings[Block])
S.Diag(Delayed.first, Delayed.second);
discardWarnings(Block);
}
void discardWarnings(const BlockDecl *Block) {
DelayedBlockWarnings.erase(Block);
}
private:
using DelayedDiagnostics = SmallVector<PartialDiagnosticAt, 2>;
llvm::DenseMap<const BlockDecl *, DelayedDiagnostics> DelayedBlockWarnings;
};
class CalledOnceCheckReporter : public CalledOnceCheckHandler {
public:
CalledOnceCheckReporter(Sema &S, CalledOnceInterProceduralData &Data)
: S(S), Data(Data) {}
void handleDoubleCall(const ParmVarDecl *Parameter, const Expr *Call,
const Expr *PrevCall, bool IsCompletionHandler,
bool Poised) override {
auto DiagToReport = IsCompletionHandler
? diag::warn_completion_handler_called_twice
: diag::warn_called_once_gets_called_twice;
S.Diag(Call->getBeginLoc(), DiagToReport) << Parameter;
S.Diag(PrevCall->getBeginLoc(), diag::note_called_once_gets_called_twice)
<< Poised;
}
void handleNeverCalled(const ParmVarDecl *Parameter,
bool IsCompletionHandler) override {
auto DiagToReport = IsCompletionHandler
? diag::warn_completion_handler_never_called
: diag::warn_called_once_never_called;
S.Diag(Parameter->getBeginLoc(), DiagToReport)
<< Parameter << false;
}
void handleNeverCalled(const ParmVarDecl *Parameter, const Decl *Function,
const Stmt *Where, NeverCalledReason Reason,
bool IsCalledDirectly,
bool IsCompletionHandler) override {
auto DiagToReport = IsCompletionHandler
? diag::warn_completion_handler_never_called_when
: diag::warn_called_once_never_called_when;
PartialDiagnosticAt Warning(Where->getBeginLoc(), S.PDiag(DiagToReport)
<< Parameter
<< IsCalledDirectly
<< (unsigned)Reason);
if (const auto *Block = dyn_cast<BlockDecl>(Function)) {
Data.addDelayedWarning(Block, std::move(Warning));
} else {
S.Diag(Warning.first, Warning.second);
}
}
void handleCapturedNeverCalled(const ParmVarDecl *Parameter,
const Decl *Where,
bool IsCompletionHandler) override {
auto DiagToReport = IsCompletionHandler
? diag::warn_completion_handler_never_called
: diag::warn_called_once_never_called;
S.Diag(Where->getBeginLoc(), DiagToReport)
<< Parameter << true;
}
void
handleBlockThatIsGuaranteedToBeCalledOnce(const BlockDecl *Block) override {
Data.flushWarnings(Block, S);
}
void handleBlockWithNoGuarantees(const BlockDecl *Block) override {
Data.discardWarnings(Block);
}
private:
Sema &S;
CalledOnceInterProceduralData &Data;
};
constexpr unsigned CalledOnceWarnings[] = {
diag::warn_called_once_never_called,
diag::warn_called_once_never_called_when,
diag::warn_called_once_gets_called_twice};
constexpr unsigned CompletionHandlerWarnings[]{
diag::warn_completion_handler_never_called,
diag::warn_completion_handler_never_called_when,
diag::warn_completion_handler_called_twice};
bool shouldAnalyzeCalledOnceImpl(llvm::ArrayRef<unsigned> DiagIDs,
const DiagnosticsEngine &Diags,
SourceLocation At) {
return llvm::any_of(DiagIDs, [&Diags, At](unsigned DiagID) {
return !Diags.isIgnored(DiagID, At);
});
}
bool shouldAnalyzeCalledOnceConventions(const DiagnosticsEngine &Diags,
SourceLocation At) {
return shouldAnalyzeCalledOnceImpl(CompletionHandlerWarnings, Diags, At);
}
bool shouldAnalyzeCalledOnceParameters(const DiagnosticsEngine &Diags,
SourceLocation At) {
return shouldAnalyzeCalledOnceImpl(CalledOnceWarnings, Diags, At) ||
shouldAnalyzeCalledOnceConventions(Diags, At);
}
}
namespace clang {
namespace threadSafety {
namespace {
class ThreadSafetyReporter : public clang::threadSafety::ThreadSafetyHandler {
Sema &S;
DiagList Warnings;
SourceLocation FunLocation, FunEndLocation;
const FunctionDecl *CurrentFunction;
bool Verbose;
OptionalNotes getNotes() const {
if (Verbose && CurrentFunction) {
PartialDiagnosticAt FNote(CurrentFunction->getBody()->getBeginLoc(),
S.PDiag(diag::note_thread_warning_in_fun)
<< CurrentFunction);
return OptionalNotes(1, FNote);
}
return OptionalNotes();
}
OptionalNotes getNotes(const PartialDiagnosticAt &Note) const {
OptionalNotes ONS(1, Note);
if (Verbose && CurrentFunction) {
PartialDiagnosticAt FNote(CurrentFunction->getBody()->getBeginLoc(),
S.PDiag(diag::note_thread_warning_in_fun)
<< CurrentFunction);
ONS.push_back(std::move(FNote));
}
return ONS;
}
OptionalNotes getNotes(const PartialDiagnosticAt &Note1,
const PartialDiagnosticAt &Note2) const {
OptionalNotes ONS;
ONS.push_back(Note1);
ONS.push_back(Note2);
if (Verbose && CurrentFunction) {
PartialDiagnosticAt FNote(CurrentFunction->getBody()->getBeginLoc(),
S.PDiag(diag::note_thread_warning_in_fun)
<< CurrentFunction);
ONS.push_back(std::move(FNote));
}
return ONS;
}
OptionalNotes makeLockedHereNote(SourceLocation LocLocked, StringRef Kind) {
return LocLocked.isValid()
? getNotes(PartialDiagnosticAt(
LocLocked, S.PDiag(diag::note_locked_here) << Kind))
: getNotes();
}
OptionalNotes makeUnlockedHereNote(SourceLocation LocUnlocked,
StringRef Kind) {
return LocUnlocked.isValid()
? getNotes(PartialDiagnosticAt(
LocUnlocked, S.PDiag(diag::note_unlocked_here) << Kind))
: getNotes();
}
public:
ThreadSafetyReporter(Sema &S, SourceLocation FL, SourceLocation FEL)
: S(S), FunLocation(FL), FunEndLocation(FEL),
CurrentFunction(nullptr), Verbose(false) {}
void setVerbose(bool b) { Verbose = b; }
void emitDiagnostics() {
Warnings.sort(SortDiagBySourceLocation(S.getSourceManager()));
for (const auto &Diag : Warnings) {
S.Diag(Diag.first.first, Diag.first.second);
for (const auto &Note : Diag.second)
S.Diag(Note.first, Note.second);
}
}
void handleInvalidLockExp(SourceLocation Loc) override {
PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_cannot_resolve_lock)
<< Loc);
Warnings.emplace_back(std::move(Warning), getNotes());
}
void handleUnmatchedUnlock(StringRef Kind, Name LockName, SourceLocation Loc,
SourceLocation LocPreviousUnlock) override {
if (Loc.isInvalid())
Loc = FunLocation;
PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_unlock_but_no_lock)
<< Kind << LockName);
Warnings.emplace_back(std::move(Warning),
makeUnlockedHereNote(LocPreviousUnlock, Kind));
}
void handleIncorrectUnlockKind(StringRef Kind, Name LockName,
LockKind Expected, LockKind Received,
SourceLocation LocLocked,
SourceLocation LocUnlock) override {
if (LocUnlock.isInvalid())
LocUnlock = FunLocation;
PartialDiagnosticAt Warning(
LocUnlock, S.PDiag(diag::warn_unlock_kind_mismatch)
<< Kind << LockName << Received << Expected);
Warnings.emplace_back(std::move(Warning),
makeLockedHereNote(LocLocked, Kind));
}
void handleDoubleLock(StringRef Kind, Name LockName, SourceLocation LocLocked,
SourceLocation LocDoubleLock) override {
if (LocDoubleLock.isInvalid())
LocDoubleLock = FunLocation;
PartialDiagnosticAt Warning(LocDoubleLock, S.PDiag(diag::warn_double_lock)
<< Kind << LockName);
Warnings.emplace_back(std::move(Warning),
makeLockedHereNote(LocLocked, Kind));
}
void handleMutexHeldEndOfScope(StringRef Kind, Name LockName,
SourceLocation LocLocked,
SourceLocation LocEndOfScope,
LockErrorKind LEK) override {
unsigned DiagID = 0;
switch (LEK) {
case LEK_LockedSomePredecessors:
DiagID = diag::warn_lock_some_predecessors;
break;
case LEK_LockedSomeLoopIterations:
DiagID = diag::warn_expecting_lock_held_on_loop;
break;
case LEK_LockedAtEndOfFunction:
DiagID = diag::warn_no_unlock;
break;
case LEK_NotLockedAtEndOfFunction:
DiagID = diag::warn_expecting_locked;
break;
}
if (LocEndOfScope.isInvalid())
LocEndOfScope = FunEndLocation;
PartialDiagnosticAt Warning(LocEndOfScope, S.PDiag(DiagID) << Kind
<< LockName);
Warnings.emplace_back(std::move(Warning),
makeLockedHereNote(LocLocked, Kind));
}
void handleExclusiveAndShared(StringRef Kind, Name LockName,
SourceLocation Loc1,
SourceLocation Loc2) override {
PartialDiagnosticAt Warning(Loc1,
S.PDiag(diag::warn_lock_exclusive_and_shared)
<< Kind << LockName);
PartialDiagnosticAt Note(Loc2, S.PDiag(diag::note_lock_exclusive_and_shared)
<< Kind << LockName);
Warnings.emplace_back(std::move(Warning), getNotes(Note));
}
void handleNoMutexHeld(const NamedDecl *D, ProtectedOperationKind POK,
AccessKind AK, SourceLocation Loc) override {
assert((POK == POK_VarAccess || POK == POK_VarDereference) &&
"Only works for variables");
unsigned DiagID = POK == POK_VarAccess?
diag::warn_variable_requires_any_lock:
diag::warn_var_deref_requires_any_lock;
PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID)
<< D << getLockKindFromAccessKind(AK));
Warnings.emplace_back(std::move(Warning), getNotes());
}
void handleMutexNotHeld(StringRef Kind, const NamedDecl *D,
ProtectedOperationKind POK, Name LockName,
LockKind LK, SourceLocation Loc,
Name *PossibleMatch) override {
unsigned DiagID = 0;
if (PossibleMatch) {
switch (POK) {
case POK_VarAccess:
DiagID = diag::warn_variable_requires_lock_precise;
break;
case POK_VarDereference:
DiagID = diag::warn_var_deref_requires_lock_precise;
break;
case POK_FunctionCall:
DiagID = diag::warn_fun_requires_lock_precise;
break;
case POK_PassByRef:
DiagID = diag::warn_guarded_pass_by_reference;
break;
case POK_PtPassByRef:
DiagID = diag::warn_pt_guarded_pass_by_reference;
break;
}
PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID) << Kind
<< D
<< LockName << LK);
PartialDiagnosticAt Note(Loc, S.PDiag(diag::note_found_mutex_near_match)
<< *PossibleMatch);
if (Verbose && POK == POK_VarAccess) {
PartialDiagnosticAt VNote(D->getLocation(),
S.PDiag(diag::note_guarded_by_declared_here)
<< D->getDeclName());
Warnings.emplace_back(std::move(Warning), getNotes(Note, VNote));
} else
Warnings.emplace_back(std::move(Warning), getNotes(Note));
} else {
switch (POK) {
case POK_VarAccess:
DiagID = diag::warn_variable_requires_lock;
break;
case POK_VarDereference:
DiagID = diag::warn_var_deref_requires_lock;
break;
case POK_FunctionCall:
DiagID = diag::warn_fun_requires_lock;
break;
case POK_PassByRef:
DiagID = diag::warn_guarded_pass_by_reference;
break;
case POK_PtPassByRef:
DiagID = diag::warn_pt_guarded_pass_by_reference;
break;
}
PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID) << Kind
<< D
<< LockName << LK);
if (Verbose && POK == POK_VarAccess) {
PartialDiagnosticAt Note(D->getLocation(),
S.PDiag(diag::note_guarded_by_declared_here));
Warnings.emplace_back(std::move(Warning), getNotes(Note));
} else
Warnings.emplace_back(std::move(Warning), getNotes());
}
}
void handleNegativeNotHeld(StringRef Kind, Name LockName, Name Neg,
SourceLocation Loc) override {
PartialDiagnosticAt Warning(Loc,
S.PDiag(diag::warn_acquire_requires_negative_cap)
<< Kind << LockName << Neg);
Warnings.emplace_back(std::move(Warning), getNotes());
}
void handleNegativeNotHeld(const NamedDecl *D, Name LockName,
SourceLocation Loc) override {
PartialDiagnosticAt Warning(
Loc, S.PDiag(diag::warn_fun_requires_negative_cap) << D << LockName);
Warnings.emplace_back(std::move(Warning), getNotes());
}
void handleFunExcludesLock(StringRef Kind, Name FunName, Name LockName,
SourceLocation Loc) override {
PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_fun_excludes_mutex)
<< Kind << FunName << LockName);
Warnings.emplace_back(std::move(Warning), getNotes());
}
void handleLockAcquiredBefore(StringRef Kind, Name L1Name, Name L2Name,
SourceLocation Loc) override {
PartialDiagnosticAt Warning(Loc,
S.PDiag(diag::warn_acquired_before) << Kind << L1Name << L2Name);
Warnings.emplace_back(std::move(Warning), getNotes());
}
void handleBeforeAfterCycle(Name L1Name, SourceLocation Loc) override {
PartialDiagnosticAt Warning(Loc,
S.PDiag(diag::warn_acquired_before_after_cycle) << L1Name);
Warnings.emplace_back(std::move(Warning), getNotes());
}
void enterFunction(const FunctionDecl* FD) override {
CurrentFunction = FD;
}
void leaveFunction(const FunctionDecl* FD) override {
CurrentFunction = nullptr;
}
};
} } }
namespace clang {
namespace consumed {
namespace {
class ConsumedWarningsHandler : public ConsumedWarningsHandlerBase {
Sema &S;
DiagList Warnings;
public:
ConsumedWarningsHandler(Sema &S) : S(S) {}
void emitDiagnostics() override {
Warnings.sort(SortDiagBySourceLocation(S.getSourceManager()));
for (const auto &Diag : Warnings) {
S.Diag(Diag.first.first, Diag.first.second);
for (const auto &Note : Diag.second)
S.Diag(Note.first, Note.second);
}
}
void warnLoopStateMismatch(SourceLocation Loc,
StringRef VariableName) override {
PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_loop_state_mismatch) <<
VariableName);
Warnings.emplace_back(std::move(Warning), OptionalNotes());
}
void warnParamReturnTypestateMismatch(SourceLocation Loc,
StringRef VariableName,
StringRef ExpectedState,
StringRef ObservedState) override {
PartialDiagnosticAt Warning(Loc, S.PDiag(
diag::warn_param_return_typestate_mismatch) << VariableName <<
ExpectedState << ObservedState);
Warnings.emplace_back(std::move(Warning), OptionalNotes());
}
void warnParamTypestateMismatch(SourceLocation Loc, StringRef ExpectedState,
StringRef ObservedState) override {
PartialDiagnosticAt Warning(Loc, S.PDiag(
diag::warn_param_typestate_mismatch) << ExpectedState << ObservedState);
Warnings.emplace_back(std::move(Warning), OptionalNotes());
}
void warnReturnTypestateForUnconsumableType(SourceLocation Loc,
StringRef TypeName) override {
PartialDiagnosticAt Warning(Loc, S.PDiag(
diag::warn_return_typestate_for_unconsumable_type) << TypeName);
Warnings.emplace_back(std::move(Warning), OptionalNotes());
}
void warnReturnTypestateMismatch(SourceLocation Loc, StringRef ExpectedState,
StringRef ObservedState) override {
PartialDiagnosticAt Warning(Loc, S.PDiag(
diag::warn_return_typestate_mismatch) << ExpectedState << ObservedState);
Warnings.emplace_back(std::move(Warning), OptionalNotes());
}
void warnUseOfTempInInvalidState(StringRef MethodName, StringRef State,
SourceLocation Loc) override {
PartialDiagnosticAt Warning(Loc, S.PDiag(
diag::warn_use_of_temp_in_invalid_state) << MethodName << State);
Warnings.emplace_back(std::move(Warning), OptionalNotes());
}
void warnUseInInvalidState(StringRef MethodName, StringRef VariableName,
StringRef State, SourceLocation Loc) override {
PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_use_in_invalid_state) <<
MethodName << VariableName << State);
Warnings.emplace_back(std::move(Warning), OptionalNotes());
}
};
} } }
sema::AnalysisBasedWarnings::Policy::Policy() {
enableCheckFallThrough = 1;
enableCheckUnreachable = 0;
enableThreadSafetyAnalysis = 0;
enableConsumedAnalysis = 0;
}
class sema::AnalysisBasedWarnings::InterProceduralData {
public:
CalledOnceInterProceduralData CalledOnceData;
};
static unsigned isEnabled(DiagnosticsEngine &D, unsigned diag) {
return (unsigned)!D.isIgnored(diag, SourceLocation());
}
sema::AnalysisBasedWarnings::AnalysisBasedWarnings(Sema &s)
: S(s), IPData(std::make_unique<InterProceduralData>()),
NumFunctionsAnalyzed(0), NumFunctionsWithBadCFGs(0), NumCFGBlocks(0),
MaxCFGBlocksPerFunction(0), NumUninitAnalysisFunctions(0),
NumUninitAnalysisVariables(0), MaxUninitAnalysisVariablesPerFunction(0),
NumUninitAnalysisBlockVisits(0),
MaxUninitAnalysisBlockVisitsPerFunction(0) {
using namespace diag;
DiagnosticsEngine &D = S.getDiagnostics();
DefaultPolicy.enableCheckUnreachable =
isEnabled(D, warn_unreachable) || isEnabled(D, warn_unreachable_break) ||
isEnabled(D, warn_unreachable_return) ||
isEnabled(D, warn_unreachable_loop_increment);
DefaultPolicy.enableThreadSafetyAnalysis = isEnabled(D, warn_double_lock);
DefaultPolicy.enableConsumedAnalysis =
isEnabled(D, warn_use_in_invalid_state);
}
sema::AnalysisBasedWarnings::~AnalysisBasedWarnings() = default;
static void flushDiagnostics(Sema &S, const sema::FunctionScopeInfo *fscope) {
for (const auto &D : fscope->PossiblyUnreachableDiags)
S.Diag(D.Loc, D.PD);
}
void clang::sema::AnalysisBasedWarnings::IssueWarnings(
sema::AnalysisBasedWarnings::Policy P, sema::FunctionScopeInfo *fscope,
const Decl *D, QualType BlockType) {
DiagnosticsEngine &Diags = S.getDiagnostics();
if (Diags.getIgnoreAllWarnings() ||
(Diags.getSuppressSystemWarnings() &&
S.SourceMgr.isInSystemHeader(D->getLocation())))
return;
if (cast<DeclContext>(D)->isDependentContext())
return;
if (S.hasUncompilableErrorOccurred()) {
flushDiagnostics(S, fscope);
return;
}
const Stmt *Body = D->getBody();
assert(Body);
AnalysisDeclContext AC( nullptr, D);
AC.getCFGBuildOptions().PruneTriviallyFalseEdges = true;
AC.getCFGBuildOptions().AddEHEdges = false;
AC.getCFGBuildOptions().AddInitializers = true;
AC.getCFGBuildOptions().AddImplicitDtors = true;
AC.getCFGBuildOptions().AddTemporaryDtors = true;
AC.getCFGBuildOptions().AddCXXNewAllocator = false;
AC.getCFGBuildOptions().AddCXXDefaultInitExprInCtors = true;
if (P.enableCheckUnreachable || P.enableThreadSafetyAnalysis ||
P.enableConsumedAnalysis) {
AC.getCFGBuildOptions().setAllAlwaysAdd();
}
else {
AC.getCFGBuildOptions()
.setAlwaysAdd(Stmt::BinaryOperatorClass)
.setAlwaysAdd(Stmt::CompoundAssignOperatorClass)
.setAlwaysAdd(Stmt::BlockExprClass)
.setAlwaysAdd(Stmt::CStyleCastExprClass)
.setAlwaysAdd(Stmt::DeclRefExprClass)
.setAlwaysAdd(Stmt::ImplicitCastExprClass)
.setAlwaysAdd(Stmt::UnaryOperatorClass);
}
llvm::Optional<LogicalErrorHandler> LEH;
if (LogicalErrorHandler::hasActiveDiagnostics(Diags, D->getBeginLoc())) {
LEH.emplace(S);
AC.getCFGBuildOptions().Observer = &*LEH;
}
if (!fscope->PossiblyUnreachableDiags.empty()) {
bool analyzed = false;
for (const auto &D : fscope->PossiblyUnreachableDiags) {
for (const Stmt *S : D.Stmts)
AC.registerForcedBlockExpression(S);
}
if (AC.getCFG()) {
analyzed = true;
for (const auto &D : fscope->PossiblyUnreachableDiags) {
bool AllReachable = true;
for (const Stmt *S : D.Stmts) {
const CFGBlock *block = AC.getBlockForRegisteredExpression(S);
CFGReverseBlockReachabilityAnalysis *cra =
AC.getCFGReachablityAnalysis();
if (block && cra) {
if (!cra->isReachable(&AC.getCFG()->getEntry(), block)) {
AllReachable = false;
break;
}
}
}
if (AllReachable)
S.Diag(D.Loc, D.PD);
}
}
if (!analyzed)
flushDiagnostics(S, fscope);
}
if (P.enableCheckFallThrough) {
const CheckFallThroughDiagnostics &CD =
(isa<BlockDecl>(D)
? CheckFallThroughDiagnostics::MakeForBlock()
: (isa<CXXMethodDecl>(D) &&
cast<CXXMethodDecl>(D)->getOverloadedOperator() == OO_Call &&
cast<CXXMethodDecl>(D)->getParent()->isLambda())
? CheckFallThroughDiagnostics::MakeForLambda()
: (fscope->isCoroutine()
? CheckFallThroughDiagnostics::MakeForCoroutine(D)
: CheckFallThroughDiagnostics::MakeForFunction(D)));
CheckFallThroughForBody(S, D, Body, BlockType, CD, AC, fscope);
}
if (P.enableCheckUnreachable) {
bool isTemplateInstantiation = false;
if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
isTemplateInstantiation = Function->isTemplateInstantiation();
if (!isTemplateInstantiation)
CheckUnreachable(S, AC);
}
if (P.enableThreadSafetyAnalysis) {
SourceLocation FL = AC.getDecl()->getLocation();
SourceLocation FEL = AC.getDecl()->getEndLoc();
threadSafety::ThreadSafetyReporter Reporter(S, FL, FEL);
if (!Diags.isIgnored(diag::warn_thread_safety_beta, D->getBeginLoc()))
Reporter.setIssueBetaWarnings(true);
if (!Diags.isIgnored(diag::warn_thread_safety_verbose, D->getBeginLoc()))
Reporter.setVerbose(true);
threadSafety::runThreadSafetyAnalysis(AC, Reporter,
&S.ThreadSafetyDeclCache);
Reporter.emitDiagnostics();
}
if (P.enableConsumedAnalysis) {
consumed::ConsumedWarningsHandler WarningHandler(S);
consumed::ConsumedAnalyzer Analyzer(WarningHandler);
Analyzer.run(AC);
}
if (!Diags.isIgnored(diag::warn_uninit_var, D->getBeginLoc()) ||
!Diags.isIgnored(diag::warn_sometimes_uninit_var, D->getBeginLoc()) ||
!Diags.isIgnored(diag::warn_maybe_uninit_var, D->getBeginLoc()) ||
!Diags.isIgnored(diag::warn_uninit_const_reference, D->getBeginLoc())) {
if (CFG *cfg = AC.getCFG()) {
UninitValsDiagReporter reporter(S);
UninitVariablesAnalysisStats stats;
std::memset(&stats, 0, sizeof(UninitVariablesAnalysisStats));
runUninitializedVariablesAnalysis(*cast<DeclContext>(D), *cfg, AC,
reporter, stats);
if (S.CollectStats && stats.NumVariablesAnalyzed > 0) {
++NumUninitAnalysisFunctions;
NumUninitAnalysisVariables += stats.NumVariablesAnalyzed;
NumUninitAnalysisBlockVisits += stats.NumBlockVisits;
MaxUninitAnalysisVariablesPerFunction =
std::max(MaxUninitAnalysisVariablesPerFunction,
stats.NumVariablesAnalyzed);
MaxUninitAnalysisBlockVisitsPerFunction =
std::max(MaxUninitAnalysisBlockVisitsPerFunction,
stats.NumBlockVisits);
}
}
}
if (S.getLangOpts().ObjC && !S.getLangOpts().CPlusPlus &&
shouldAnalyzeCalledOnceParameters(Diags, D->getBeginLoc())) {
if (AC.getCFG()) {
CalledOnceCheckReporter Reporter(S, IPData->CalledOnceData);
checkCalledOnceParameters(
AC, Reporter,
shouldAnalyzeCalledOnceConventions(Diags, D->getBeginLoc()));
}
}
bool FallThroughDiagFull =
!Diags.isIgnored(diag::warn_unannotated_fallthrough, D->getBeginLoc());
bool FallThroughDiagPerFunction = !Diags.isIgnored(
diag::warn_unannotated_fallthrough_per_function, D->getBeginLoc());
if (FallThroughDiagFull || FallThroughDiagPerFunction ||
fscope->HasFallthroughStmt) {
DiagnoseSwitchLabelsFallthrough(S, AC, !FallThroughDiagFull);
}
if (S.getLangOpts().ObjCWeak &&
!Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, D->getBeginLoc()))
diagnoseRepeatedUseOfWeak(S, fscope, D, AC.getParentMap());
if (!Diags.isIgnored(diag::warn_infinite_recursive_function,
D->getBeginLoc())) {
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
checkRecursiveFunction(S, FD, Body, AC);
}
}
if (!Diags.isIgnored(diag::warn_throw_in_noexcept_func, D->getBeginLoc()))
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
if (S.getLangOpts().CPlusPlus && isNoexcept(FD))
checkThrowInNonThrowingFunc(S, FD, AC);
if (LogicalErrorHandler::hasActiveDiagnostics(Diags, D->getBeginLoc())) {
AC.getCFG();
}
if (S.CollectStats && AC.isCFGBuilt()) {
++NumFunctionsAnalyzed;
if (CFG *cfg = AC.getCFG()) {
NumCFGBlocks += cfg->getNumBlockIDs();
MaxCFGBlocksPerFunction = std::max(MaxCFGBlocksPerFunction,
cfg->getNumBlockIDs());
} else {
++NumFunctionsWithBadCFGs;
}
}
}
void clang::sema::AnalysisBasedWarnings::PrintStats() const {
llvm::errs() << "\n*** Analysis Based Warnings Stats:\n";
unsigned NumCFGsBuilt = NumFunctionsAnalyzed - NumFunctionsWithBadCFGs;
unsigned AvgCFGBlocksPerFunction =
!NumCFGsBuilt ? 0 : NumCFGBlocks/NumCFGsBuilt;
llvm::errs() << NumFunctionsAnalyzed << " functions analyzed ("
<< NumFunctionsWithBadCFGs << " w/o CFGs).\n"
<< " " << NumCFGBlocks << " CFG blocks built.\n"
<< " " << AvgCFGBlocksPerFunction
<< " average CFG blocks per function.\n"
<< " " << MaxCFGBlocksPerFunction
<< " max CFG blocks per function.\n";
unsigned AvgUninitVariablesPerFunction = !NumUninitAnalysisFunctions ? 0
: NumUninitAnalysisVariables/NumUninitAnalysisFunctions;
unsigned AvgUninitBlockVisitsPerFunction = !NumUninitAnalysisFunctions ? 0
: NumUninitAnalysisBlockVisits/NumUninitAnalysisFunctions;
llvm::errs() << NumUninitAnalysisFunctions
<< " functions analyzed for uninitialiazed variables\n"
<< " " << NumUninitAnalysisVariables << " variables analyzed.\n"
<< " " << AvgUninitVariablesPerFunction
<< " average variables per function.\n"
<< " " << MaxUninitAnalysisVariablesPerFunction
<< " max variables per function.\n"
<< " " << NumUninitAnalysisBlockVisits << " block visits.\n"
<< " " << AvgUninitBlockVisitsPerFunction
<< " average block visits per function.\n"
<< " " << MaxUninitAnalysisBlockVisitsPerFunction
<< " max block visits per function.\n";
}