clang 19.0.0git
Classes | Namespaces | Macros | Functions
UnsafeBufferUsage.cpp File Reference
#include "clang/Analysis/Analyses/UnsafeBufferUsage.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/Expr.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/Stmt.h"
#include "clang/AST/StmtVisitor.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Basic/CharInfo.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Lex/Lexer.h"
#include "clang/Lex/Preprocessor.h"
#include "llvm/ADT/APSInt.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Casting.h"
#include <memory>
#include <optional>
#include <queue>
#include <sstream>
#include "clang/Analysis/Analyses/UnsafeBufferUsageGadgets.def"

Go to the source code of this file.

Classes

class  clang::ast_matchers::MatchDescendantVisitor
 
class  UPCPreIncrementGadget
 
class  UUCAddAssignGadget
 
class  DerefSimplePtrArithFixableGadget
 
struct  CompareNode< NodeTy >
 
struct  WarningGadgetSets
 
struct  FixableGadgetSets
 
class  VariableGroupsManagerImpl
 

Namespaces

namespace  clang
 The JSON file list parser is used to communicate input to InstallAPI.
 
namespace  clang::ast_matchers
 

Macros

#define GADGET(x)   x,
 
#define GADGET(x)
 
#define NEXT   ++numFound
 
#define FIXABLE_GADGET(name)
 
#define WARNING_GADGET(name)
 
#define WARNING_GADGET(x)
 
#define WARNING_CONTAINER_GADGET(x)
 
#define FIXABLE_GADGET(x)    x ## Gadget::matcher().bind(#x),
 
#define DEBUG_NOTE_DECL_FAIL(D, Msg)
 

Functions

static auto clang::ast_matchers::hasPointerType ()
 
static auto clang::ast_matchers::hasArrayType ()
 
 clang::ast_matchers::AST_MATCHER_P (Stmt, forEachDescendantEvaluatedStmt, internal::Matcher< Stmt >, innerMatcher)
 
 clang::ast_matchers::AST_MATCHER_P (Stmt, forEachDescendantStmt, internal::Matcher< Stmt >, innerMatcher)
 
 clang::ast_matchers::AST_MATCHER_P (Stmt, notInSafeBufferOptOut, const UnsafeBufferUsageHandler *, Handler)
 
 clang::ast_matchers::AST_MATCHER_P (Stmt, ignoreUnsafeBufferInContainer, const UnsafeBufferUsageHandler *, Handler)
 
 clang::ast_matchers::AST_MATCHER_P (CastExpr, castSubExpr, internal::Matcher< Expr >, innerMatcher)
 
 clang::ast_matchers::AST_MATCHER (UnaryOperator, isPreInc)
 
static auto clang::ast_matchers::isInUnspecifiedLvalueContext (internal::Matcher< Expr > innerMatcher)
 
static internal::Matcher< Stmtclang::ast_matchers::isInUnspecifiedPointerContext (internal::Matcher< Stmt > InnerMatcher)
 
static internal::Matcher< Stmtclang::ast_matchers::isInUnspecifiedUntypedContext (internal::Matcher< Stmt > InnerMatcher)
 
 clang::ast_matchers::AST_MATCHER (CXXConstructExpr, isSafeSpanTwoParamConstruct)
 
 clang::ast_matchers::AST_MATCHER (ArraySubscriptExpr, isSafeArraySubscript)
 
static std::tuple< FixableGadgetList, WarningGadgetList, DeclUseTracker > findGadgets (const Decl *D, const UnsafeBufferUsageHandler &Handler, bool EmitSuggestions)
 Scan the function and return a list of gadgets found with provided kits.
 
static WarningGadgetSets groupWarningGadgetsByVar (const WarningGadgetList &AllUnsafeOperations)
 
static FixableGadgetSets groupFixablesByVar (FixableGadgetList &&AllFixableOperations)
 
static std::optional< FixItList > createDataFixit (const ASTContext &Ctx, const DeclRefExpr *DRE)
 
static bool isNonNegativeIntegerExpr (const Expr *Expr, const VarDecl *VD, const ASTContext &Ctx)
 
static std::optional< FixItList > fixUPCAddressofArraySubscriptWithSpan (const UnaryOperator *Node)
 
static StringRef getEndOfLine ()
 
std::string getUserFillPlaceHolder (StringRef HintTextToUser="placeholder")
 
template<typename NodeTy >
static std::optional< SourceLocationgetEndCharLoc (const NodeTy *Node, const SourceManager &SM, const LangOptions &LangOpts)
 
template<typename NodeTy >
static std::optional< SourceLocationgetPastLoc (const NodeTy *Node, const SourceManager &SM, const LangOptions &LangOpts)
 
static std::optional< StringRef > getExprText (const Expr *E, const SourceManager &SM, const LangOptions &LangOpts)
 
static std::optional< StringRef > getRangeText (SourceRange SR, const SourceManager &SM, const LangOptions &LangOpts)
 
static SourceLocation getVarDeclIdentifierLoc (const VarDecl *VD)
 
static std::optional< StringRef > getVarDeclIdentifierText (const VarDecl *VD, const SourceManager &SM, const LangOptions &LangOpts)
 
static bool hasUnsupportedSpecifiers (const VarDecl *VD, const SourceManager &SM)
 
static SourceRange getSourceRangeToTokenEnd (const Decl *D, const SourceManager &SM, const LangOptions &LangOpts)
 
static std::optional< std::string > getPointeeTypeText (const VarDecl *VD, const SourceManager &SM, const LangOptions &LangOpts, std::optional< Qualifiers > *QualifiersToAppend)
 
static std::optional< StringRef > getFunNameText (const FunctionDecl *FD, const SourceManager &SM, const LangOptions &LangOpts)
 
static std::string getSpanTypeText (StringRef EltTyText, std::optional< Qualifiers > Quals=std::nullopt)
 
static std::optional< FixItList > FixVarInitializerWithSpan (const Expr *Init, ASTContext &Ctx, const StringRef UserFillPlaceHolder)
 
static std::optional< std::string > createSpanTypeForVarDecl (const VarDecl *VD, const ASTContext &Ctx)
 
static FixItList fixLocalVarDeclWithSpan (const VarDecl *D, ASTContext &Ctx, const StringRef UserFillPlaceHolder, UnsafeBufferUsageHandler &Handler)
 
static bool hasConflictingOverload (const FunctionDecl *FD)
 
static std::optional< FixItList > createOverloadsForFixedParams (const FixitStrategy &S, const FunctionDecl *FD, const ASTContext &Ctx, UnsafeBufferUsageHandler &Handler)
 
static FixItList fixParamWithSpan (const ParmVarDecl *PVD, const ASTContext &Ctx, UnsafeBufferUsageHandler &Handler)
 
static FixItList fixVariableWithSpan (const VarDecl *VD, const DeclUseTracker &Tracker, ASTContext &Ctx, UnsafeBufferUsageHandler &Handler)
 
static FixItList fixVarDeclWithArray (const VarDecl *D, const ASTContext &Ctx, UnsafeBufferUsageHandler &Handler)
 
static FixItList fixVariableWithArray (const VarDecl *VD, const DeclUseTracker &Tracker, const ASTContext &Ctx, UnsafeBufferUsageHandler &Handler)
 
static FixItList fixVariable (const VarDecl *VD, FixitStrategy::Kind K, const Decl *D, const DeclUseTracker &Tracker, ASTContext &Ctx, UnsafeBufferUsageHandler &Handler)
 
static bool overlapWithMacro (const FixItList &FixIts)
 
static bool isParameterOf (const VarDecl *VD, const Decl *D)
 
static void eraseVarsForUnfixableGroupMates (std::map< const VarDecl *, FixItList > &FixItsForVariable, const VariableGroupsManager &VarGrpMgr)
 
static FixItList createFunctionOverloadsForParms (std::map< const VarDecl *, FixItList > &FixItsForVariable, const VariableGroupsManager &VarGrpMgr, const FunctionDecl *FD, const FixitStrategy &S, ASTContext &Ctx, UnsafeBufferUsageHandler &Handler)
 
static std::map< const VarDecl *, FixItList > getFixIts (FixableGadgetSets &FixablesForAllVars, const FixitStrategy &S, ASTContext &Ctx, const Decl *D, const DeclUseTracker &Tracker, UnsafeBufferUsageHandler &Handler, const VariableGroupsManager &VarGrpMgr)
 
template<typename VarDeclIterTy >
static FixitStrategy getNaiveStrategy (llvm::iterator_range< VarDeclIterTy > UnsafeVars)
 

Macro Definition Documentation

◆ DEBUG_NOTE_DECL_FAIL

#define DEBUG_NOTE_DECL_FAIL (   D,
  Msg 
)
Value:
Handler.addDebugNoteForVar((D), (D)->getBeginLoc(), \
"failed to produce fixit for declaration '" + \
(D)->getNameAsString() + "'" + (Msg))

Definition at line 2289 of file UnsafeBufferUsage.cpp.

◆ FIXABLE_GADGET [1/2]

#define FIXABLE_GADGET (   name)
Value:
if (Result.Nodes.getNodeAs<Stmt>(#name)) { \
FixableGadgets.push_back(std::make_unique<name##Gadget>(Result)); \
NEXT; \
}
#define NEXT
Stmt - This represents one statement.
Definition: Stmt.h:84

◆ FIXABLE_GADGET [2/2]

#define FIXABLE_GADGET (   x)     x ## Gadget::matcher().bind(#x),

◆ GADGET [1/2]

#define GADGET (   x)    x,

Definition at line 469 of file UnsafeBufferUsage.cpp.

◆ GADGET [2/2]

#define GADGET (   x)
Value:
case Kind::x: \
return #x;

Definition at line 469 of file UnsafeBufferUsage.cpp.

◆ NEXT

#define NEXT   ++numFound

◆ WARNING_CONTAINER_GADGET

#define WARNING_CONTAINER_GADGET (   x)
Value:
allOf(x ## Gadget::matcher().bind(#x), \
notInSafeBufferOptOut(&Handler), \
unless(ignoreUnsafeBufferInContainer(&Handler))),
const internal::VariadicOperatorMatcherFunc< 1, 1 > unless
Matches if the provided matcher does not match.
const internal::VariadicOperatorMatcherFunc< 2, std::numeric_limits< unsigned >::max()> allOf
Matches if all given matchers match.

◆ WARNING_GADGET [1/2]

#define WARNING_GADGET (   name)
Value:
if (Result.Nodes.getNodeAs<Stmt>(#name)) { \
WarningGadgets.push_back(std::make_unique<name##Gadget>(Result)); \
NEXT; \
}

◆ WARNING_GADGET [2/2]

#define WARNING_GADGET (   x)
Value:
allOf(x ## Gadget::matcher().bind(#x), \
notInSafeBufferOptOut(&Handler)),

Function Documentation

◆ createDataFixit()

static std::optional< FixItList > createDataFixit ( const ASTContext Ctx,
const DeclRefExpr DRE 
)
inlinestatic
Returns
fixit that adds .data() call after \DRE.

Definition at line 2051 of file UnsafeBufferUsage.cpp.

References clang::FixItHint::CreateInsertion(), clang::ASTContext::getLangOpts(), getPastLoc(), clang::ASTContext::getSourceManager(), and SM.

◆ createFunctionOverloadsForParms()

static FixItList createFunctionOverloadsForParms ( std::map< const VarDecl *, FixItList > &  FixItsForVariable,
const VariableGroupsManager VarGrpMgr,
const FunctionDecl FD,
const FixitStrategy S,
ASTContext Ctx,
UnsafeBufferUsageHandler Handler 
)
static

◆ createOverloadsForFixedParams()

static std::optional< FixItList > createOverloadsForFixedParams ( const FixitStrategy S,
const FunctionDecl FD,
const ASTContext Ctx,
UnsafeBufferUsageHandler Handler 
)
static

◆ createSpanTypeForVarDecl()

static std::optional< std::string > createSpanTypeForVarDecl ( const VarDecl VD,
const ASTContext Ctx 
)
static

◆ eraseVarsForUnfixableGroupMates()

static void eraseVarsForUnfixableGroupMates ( std::map< const VarDecl *, FixItList > &  FixItsForVariable,
const VariableGroupsManager VarGrpMgr 
)
static

Definition at line 2834 of file UnsafeBufferUsage.cpp.

References clang::VariableGroupsManager::getGroupOfVar(), and clang::Member.

Referenced by getFixIts().

◆ findGadgets()

static std::tuple< FixableGadgetList, WarningGadgetList, DeclUseTracker > findGadgets ( const Decl D,
const UnsafeBufferUsageHandler Handler,
bool  EmitSuggestions 
)
static

◆ fixLocalVarDeclWithSpan()

static FixItList fixLocalVarDeclWithSpan ( const VarDecl D,
ASTContext Ctx,
const StringRef  UserFillPlaceHolder,
UnsafeBufferUsageHandler Handler 
)
static

◆ fixParamWithSpan()

static FixItList fixParamWithSpan ( const ParmVarDecl PVD,
const ASTContext Ctx,
UnsafeBufferUsageHandler Handler 
)
static

◆ fixUPCAddressofArraySubscriptWithSpan()

static std::optional< FixItList > fixUPCAddressofArraySubscriptWithSpan ( const UnaryOperator Node)
static

◆ fixVarDeclWithArray()

static FixItList fixVarDeclWithArray ( const VarDecl D,
const ASTContext Ctx,
UnsafeBufferUsageHandler Handler 
)
static

◆ fixVariable()

static FixItList fixVariable ( const VarDecl VD,
FixitStrategy::Kind  K,
const Decl D,
const DeclUseTracker &  Tracker,
ASTContext Ctx,
UnsafeBufferUsageHandler Handler 
)
static

◆ fixVariableWithArray()

static FixItList fixVariableWithArray ( const VarDecl VD,
const DeclUseTracker &  Tracker,
const ASTContext Ctx,
UnsafeBufferUsageHandler Handler 
)
static

Definition at line 2730 of file UnsafeBufferUsage.cpp.

References fixVarDeclWithArray(), and clang::DeclStmt::isSingleDecl().

Referenced by fixVariable().

◆ fixVariableWithSpan()

static FixItList fixVariableWithSpan ( const VarDecl VD,
const DeclUseTracker &  Tracker,
ASTContext Ctx,
UnsafeBufferUsageHandler Handler 
)
static

◆ FixVarInitializerWithSpan()

static std::optional< FixItList > FixVarInitializerWithSpan ( const Expr Init,
ASTContext Ctx,
const StringRef  UserFillPlaceHolder 
)
static

◆ getEndCharLoc()

template<typename NodeTy >
static std::optional< SourceLocation > getEndCharLoc ( const NodeTy *  Node,
const SourceManager SM,
const LangOptions LangOpts 
)
static

◆ getEndOfLine()

static StringRef getEndOfLine ( )
static

Definition at line 1723 of file UnsafeBufferUsage.cpp.

Referenced by createOverloadsForFixedParams().

◆ getExprText()

static std::optional< StringRef > getExprText ( const Expr E,
const SourceManager SM,
const LangOptions LangOpts 
)
static

◆ getFixIts()

static std::map< const VarDecl *, FixItList > getFixIts ( FixableGadgetSets FixablesForAllVars,
const FixitStrategy S,
ASTContext Ctx,
const Decl D,
const DeclUseTracker &  Tracker,
UnsafeBufferUsageHandler Handler,
const VariableGroupsManager VarGrpMgr 
)
static

◆ getFunNameText()

static std::optional< StringRef > getFunNameText ( const FunctionDecl FD,
const SourceManager SM,
const LangOptions LangOpts 
)
static

◆ getNaiveStrategy()

template<typename VarDeclIterTy >
static FixitStrategy getNaiveStrategy ( llvm::iterator_range< VarDeclIterTy >  UnsafeVars)
static

◆ getPastLoc()

template<typename NodeTy >
static std::optional< SourceLocation > getPastLoc ( const NodeTy *  Node,
const SourceManager SM,
const LangOptions LangOpts 
)
static

◆ getPointeeTypeText()

static std::optional< std::string > getPointeeTypeText ( const VarDecl VD,
const SourceManager SM,
const LangOptions LangOpts,
std::optional< Qualifiers > *  QualifiersToAppend 
)
static

◆ getRangeText()

static std::optional< StringRef > getRangeText ( SourceRange  SR,
const SourceManager SM,
const LangOptions LangOpts 
)
static

◆ getSourceRangeToTokenEnd()

static SourceRange getSourceRangeToTokenEnd ( const Decl D,
const SourceManager SM,
const LangOptions LangOpts 
)
static

◆ getSpanTypeText()

static std::string getSpanTypeText ( StringRef  EltTyText,
std::optional< Qualifiers Quals = std::nullopt 
)
static

Definition at line 1948 of file UnsafeBufferUsage.cpp.

Referenced by createOverloadsForFixedParams(), and fixParamWithSpan().

◆ getUserFillPlaceHolder()

std::string getUserFillPlaceHolder ( StringRef  HintTextToUser = "placeholder")

Definition at line 1729 of file UnsafeBufferUsage.cpp.

References s.

Referenced by createOverloadsForFixedParams(), and fixVariableWithSpan().

◆ getVarDeclIdentifierLoc()

static SourceLocation getVarDeclIdentifierLoc ( const VarDecl VD)
static

◆ getVarDeclIdentifierText()

static std::optional< StringRef > getVarDeclIdentifierText ( const VarDecl VD,
const SourceManager SM,
const LangOptions LangOpts 
)
static

◆ groupFixablesByVar()

static FixableGadgetSets groupFixablesByVar ( FixableGadgetList &&  AllFixableOperations)
static

◆ groupWarningGadgetsByVar()

static WarningGadgetSets groupWarningGadgetsByVar ( const WarningGadgetList &  AllUnsafeOperations)
static

◆ hasConflictingOverload()

static bool hasConflictingOverload ( const FunctionDecl FD)
static

◆ hasUnsupportedSpecifiers()

static bool hasUnsupportedSpecifiers ( const VarDecl VD,
const SourceManager SM 
)
static

◆ isNonNegativeIntegerExpr()

static bool isNonNegativeIntegerExpr ( const Expr Expr,
const VarDecl VD,
const ASTContext Ctx 
)
static

◆ isParameterOf()

static bool isParameterOf ( const VarDecl VD,
const Decl D 
)
static

Definition at line 2826 of file UnsafeBufferUsage.cpp.

References clang::Decl::getDeclContext().

Referenced by clang::checkUnsafeBufferUsage().

◆ overlapWithMacro()

static bool overlapWithMacro ( const FixItList &  FixIts)
static