clang 22.0.0git
AnalysisBasedWarnings.cpp File Reference
#include "clang/Sema/AnalysisBasedWarnings.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/DynamicRecursiveASTVisitor.h"
#include "clang/AST/EvaluatedExprVisitor.h"
#include "clang/AST/Expr.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/ExprObjC.h"
#include "clang/AST/OperationKinds.h"
#include "clang/AST/ParentMap.h"
#include "clang/AST/StmtCXX.h"
#include "clang/AST/StmtObjC.h"
#include "clang/AST/Type.h"
#include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h"
#include "clang/Analysis/Analyses/CalledOnceCheck.h"
#include "clang/Analysis/Analyses/Consumed.h"
#include "clang/Analysis/Analyses/LifetimeSafety.h"
#include "clang/Analysis/Analyses/ReachableCode.h"
#include "clang/Analysis/Analyses/ThreadSafety.h"
#include "clang/Analysis/Analyses/UninitializedValues.h"
#include "clang/Analysis/Analyses/UnsafeBufferUsage.h"
#include "clang/Analysis/AnalysisDeclContext.h"
#include "clang/Analysis/CFG.h"
#include "clang/Analysis/CFGStmtMap.h"
#include "clang/Analysis/FlowSensitive/DataflowWorklist.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/DiagnosticSema.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/DenseMap.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Debug.h"
#include <algorithm>
#include <deque>
#include <iterator>
#include <optional>

Go to the source code of this file.

Classes

class  clang::sema::AnalysisBasedWarnings::InterProceduralData
 InterProceduralData aims to be a storage of whatever data should be passed between analyses of different functions. More...
class  CallableVisitor

Namespaces

namespace  clang
 The JSON file list parser is used to communicate input to InstallAPI.
namespace  clang::threadSafety
namespace  clang::consumed
namespace  clang::lifetimes

Enumerations

enum  ControlFlowKind {
  UnknownFallThrough , NeverFallThrough , MaybeFallThrough , AlwaysFallThrough ,
  NeverFallThroughOrReturn
}

Functions

static void CheckUnreachable (Sema &S, AnalysisDeclContext &AC)
 CheckUnreachable - Check for unreachable code.
static bool hasRecursiveCallInPath (const FunctionDecl *FD, CFGBlock &Block)
static bool checkForRecursiveFunctionCall (const FunctionDecl *FD, CFG *cfg)
static void checkRecursiveFunction (Sema &S, const FunctionDecl *FD, const Stmt *Body, AnalysisDeclContext &AC)
static bool throwEscapes (Sema &S, const CXXThrowExpr *E, CFGBlock &ThrowBlock, CFG *Body)
 Determine whether an exception thrown by E, unwinding from ThrowBlock, can reach ExitBlock.
static void visitReachableThrows (CFG *BodyCFG, llvm::function_ref< void(const CXXThrowExpr *, CFGBlock &)> Visit)
static void EmitDiagForCXXThrowInNonThrowingFunc (Sema &S, SourceLocation OpLoc, const FunctionDecl *FD)
static void checkThrowInNonThrowingFunc (Sema &S, const FunctionDecl *FD, AnalysisDeclContext &AC)
static bool isNoexcept (const FunctionDecl *FD)
static bool isReferenceToNoReturn (const Expr *E)
 Checks if the given expression is a reference to a function with 'noreturn' attribute.
static bool isInitializedWithNoReturn (const VarDecl *VD)
 Checks if the given variable, which is assumed to be a function pointer, is initialized with a function having 'noreturn' attribute.
static bool areAllValuesNoReturn (const VarDecl *VD, const CFGBlock &VarBlk, AnalysisDeclContext &AC)
static ControlFlowKind CheckFallThrough (AnalysisDeclContext &AC)
 CheckFallThrough - Check that we don't fall off the end of a Statement that should return a value.
static void CheckFallThroughForBody (Sema &S, const Decl *D, const Stmt *Body, QualType BlockType, const CheckFallThroughDiagnostics &CD, AnalysisDeclContext &AC)
 CheckFallThroughForBody - Check that we don't fall off the end of a function that should return a value.
static bool SuggestInitializationFixit (Sema &S, const VarDecl *VD)
static void CreateIfFixit (Sema &S, const Stmt *If, const Stmt *Then, const Stmt *Else, bool CondVal, FixItHint &Fixit1, FixItHint &Fixit2)
 Create a fixit to remove an if-like statement, on the assumption that its condition is CondVal.
static void DiagUninitUse (Sema &S, const VarDecl *VD, const UninitUse &Use, bool IsCapturedByBlock)
 DiagUninitUse – Helper function to produce a diagnostic for an uninitialized use of a variable.
static bool DiagnoseUninitializedConstRefUse (Sema &S, const VarDecl *VD, const UninitUse &Use)
 Diagnose uninitialized const reference usages.
static bool DiagnoseUninitializedConstPtrUse (Sema &S, const VarDecl *VD, const UninitUse &Use)
 Diagnose uninitialized const pointer usages.
static bool DiagnoseUninitializedUse (Sema &S, const VarDecl *VD, const UninitUse &Use, bool alwaysReportSelfInit=false)
 DiagnoseUninitializedUse – Helper function for diagnosing uses of an uninitialized variable.
static StringRef getFallthroughAttrSpelling (Preprocessor &PP, SourceLocation Loc)
static void DiagnoseSwitchLabelsFallthrough (Sema &S, AnalysisDeclContext &AC, bool PerFunction)
static bool isInLoop (const ASTContext &Ctx, const ParentMap &PM, const Stmt *S)
static void diagnoseRepeatedUseOfWeak (Sema &S, const sema::FunctionScopeInfo *CurFn, const Decl *D, const ParentMap &PM)
template<typename... Ts>
static bool areAnyEnabled (DiagnosticsEngine &D, SourceLocation Loc, Ts... Diags)
static void flushDiagnostics (Sema &S, const sema::FunctionScopeInfo *fscope)

Enumeration Type Documentation

◆ ControlFlowKind

Enumerator
UnknownFallThrough 
NeverFallThrough 
MaybeFallThrough 
AlwaysFallThrough 
NeverFallThroughOrReturn 

Definition at line 547 of file AnalysisBasedWarnings.cpp.

Function Documentation

◆ areAllValuesNoReturn()

◆ areAnyEnabled()

template<typename... Ts>
bool areAnyEnabled ( DiagnosticsEngine & D,
SourceLocation Loc,
Ts... Diags )
static

◆ CheckFallThrough()

ControlFlowKind CheckFallThrough ( AnalysisDeclContext & AC)
static

CheckFallThrough - Check that we don't fall off the end of a Statement that should return a value.

Returns
AlwaysFallThrough iff we always fall off the end of the statement, MaybeFallThrough iff we might or might not fall off the end, NeverFallThroughOrReturn iff we never fall off the end of the statement or return. We assume NeverFallThrough iff we never fall off the end of the statement but we may return. We assume that functions not marked noreturn will return.

Definition at line 564 of file AnalysisBasedWarnings.cpp.

References AlwaysFallThrough, areAllValuesNoReturn(), clang::Call, clang::CFGElement::castAs(), clang::CFGBlock::filtered_pred_start_end(), clang::AnalysisDeclContext::getAddEHEdges(), clang::CFGElement::getAs(), clang::CFGBlock::getBlockID(), clang::AnalysisDeclContext::getCFG(), clang::CFG::getEntry(), clang::CFG::getExit(), clang::CFG::getNumBlockIDs(), clang::CFGStmt::getStmt(), clang::CFGBlock::getTerminatorStmt(), clang::CFGBlock::FilteredCFGBlockIterator< IMPL, IsPred >::hasMore(), clang::CFGBlock::hasNoReturnElement(), clang::CFGBlock::FilterOptions::IgnoreDefaultsWithCoveredEnums, clang::isa(), MaybeFallThrough, NeverFallThrough, NeverFallThroughOrReturn, clang::CFGBlock::rbegin(), clang::CFGBlock::rend(), clang::reachable_code::ScanReachableFromBlock(), clang::CFGBlock::succs(), and UnknownFallThrough.

Referenced by CheckFallThroughForBody().

◆ CheckFallThroughForBody()

void CheckFallThroughForBody ( Sema & S,
const Decl * D,
const Stmt * Body,
QualType BlockType,
const CheckFallThroughDiagnostics & CD,
AnalysisDeclContext & AC )
static

CheckFallThroughForBody - Check that we don't fall off the end of a function that should return a value.

Check that we don't fall off the end of a noreturn function. We assume that functions and blocks not marked noreturn will return.

Definition at line 785 of file AnalysisBasedWarnings.cpp.

References AlwaysFallThrough, CheckFallThrough(), clang::SemaBase::Diag(), clang::Type::getAs(), clang::Decl::getAsFunction(), clang::Stmt::getBeginLoc(), clang::Sema::getDiagnostics(), clang::CallExpr::getDirectCallee(), clang::Stmt::getEndLoc(), clang::Type::getPointeeType(), clang::isa(), clang::FunctionDecl::isCPUDispatchMultiVersion(), MaybeFallThrough, NeverFallThrough, NeverFallThroughOrReturn, and UnknownFallThrough.

Referenced by clang::sema::AnalysisBasedWarnings::IssueWarnings().

◆ checkForRecursiveFunctionCall()

bool checkForRecursiveFunctionCall ( const FunctionDecl * FD,
CFG * cfg )
static

◆ checkRecursiveFunction()

◆ checkThrowInNonThrowingFunc()

◆ CheckUnreachable()

◆ CreateIfFixit()

void CreateIfFixit ( Sema & S,
const Stmt * If,
const Stmt * Then,
const Stmt * Else,
bool CondVal,
FixItHint & Fixit1,
FixItHint & Fixit2 )
static

Create a fixit to remove an if-like statement, on the assumption that its condition is CondVal.

Definition at line 948 of file AnalysisBasedWarnings.cpp.

References clang::FixItHint::CreateRemoval(), clang::Stmt::getBeginLoc(), clang::CharSourceRange::getCharRange(), clang::Stmt::getEndLoc(), clang::Sema::getLocForEndOfToken(), and clang::If.

Referenced by DiagUninitUse().

◆ diagnoseRepeatedUseOfWeak()

◆ DiagnoseSwitchLabelsFallthrough()

◆ DiagnoseUninitializedConstPtrUse()

bool DiagnoseUninitializedConstPtrUse ( Sema & S,
const VarDecl * VD,
const UninitUse & Use )
static

◆ DiagnoseUninitializedConstRefUse()

bool DiagnoseUninitializedConstRefUse ( Sema & S,
const VarDecl * VD,
const UninitUse & Use )
static

◆ DiagnoseUninitializedUse()

bool DiagnoseUninitializedUse ( Sema & S,
const VarDecl * VD,
const UninitUse & Use,
bool alwaysReportSelfInit = false )
static

DiagnoseUninitializedUse – Helper function for diagnosing uses of an uninitialized variable.

This manages the different forms of diagnostic emitted for particular types of uses. Returns true if the use was diagnosed as a warning. If a particular use is one we omit warnings for, returns false.

Definition at line 1160 of file AnalysisBasedWarnings.cpp.

References clang::cast(), clang::Sema::Context, clang::SemaBase::Diag(), DiagUninitUse(), clang::BlockExpr::getBeginLoc(), clang::DeclaratorDecl::getBeginLoc(), clang::NamedDecl::getDeclName(), clang::Sema::getDiagnostics(), clang::VarDecl::getInit(), clang::Decl::getLocation(), clang::QualType::getQualifiers(), clang::ValueDecl::getType(), clang::UninitUse::getUser(), clang::Decl::hasAttr(), clang::Qualifiers::hasObjCLifetime(), clang::Initializer, clang::Type::isBlockPointerType(), clang::DiagnosticsEngine::isLastDiagnosticIgnored(), and SuggestInitializationFixit().

◆ DiagUninitUse()

◆ EmitDiagForCXXThrowInNonThrowingFunc()

◆ flushDiagnostics()

◆ getFallthroughAttrSpelling()

◆ hasRecursiveCallInPath()

◆ isInitializedWithNoReturn()

bool isInitializedWithNoReturn ( const VarDecl * VD)
static

Checks if the given variable, which is assumed to be a function pointer, is initialized with a function having 'noreturn' attribute.

Definition at line 414 of file AnalysisBasedWarnings.cpp.

References clang::VarDecl::getInit(), clang::Init, isReferenceToNoReturn(), and clang::ListInit.

Referenced by areAllValuesNoReturn().

◆ isInLoop()

◆ isNoexcept()

◆ isReferenceToNoReturn()

bool isReferenceToNoReturn ( const Expr * E)
static

Checks if the given expression is a reference to a function with 'noreturn' attribute.

Definition at line 405 of file AnalysisBasedWarnings.cpp.

References clang::Expr::IgnoreParenCasts().

Referenced by isInitializedWithNoReturn().

◆ SuggestInitializationFixit()

◆ throwEscapes()

bool throwEscapes ( Sema & S,
const CXXThrowExpr * E,
CFGBlock & ThrowBlock,
CFG * Body )
static

◆ visitReachableThrows()

void visitReachableThrows ( CFG * BodyCFG,
llvm::function_ref< void(const CXXThrowExpr *, CFGBlock &)> Visit )
static