clang 20.0.0git
|
#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< Stmt > | clang::ast_matchers::isInUnspecifiedPointerContext (internal::Matcher< Stmt > InnerMatcher) |
static internal::Matcher< Stmt > | clang::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< SourceLocation > | getEndCharLoc (const NodeTy *Node, const SourceManager &SM, const LangOptions &LangOpts) |
template<typename NodeTy > | |
static std::optional< SourceLocation > | getPastLoc (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) |
#define DEBUG_NOTE_DECL_FAIL | ( | D, | |
Msg | |||
) |
Definition at line 2294 of file UnsafeBufferUsage.cpp.
#define FIXABLE_GADGET | ( | name | ) |
#define FIXABLE_GADGET | ( | x | ) | x ## Gadget::matcher().bind(#x), |
#define GADGET | ( | x | ) | x, |
Definition at line 469 of file UnsafeBufferUsage.cpp.
#define GADGET | ( | x | ) |
Definition at line 469 of file UnsafeBufferUsage.cpp.
#define NEXT ++numFound |
#define WARNING_CONTAINER_GADGET | ( | x | ) |
#define WARNING_GADGET | ( | name | ) |
#define WARNING_GADGET | ( | x | ) |
|
inlinestatic |
Definition at line 2056 of file UnsafeBufferUsage.cpp.
References clang::FixItHint::CreateInsertion(), clang::ASTContext::getLangOpts(), getPastLoc(), clang::ASTContext::getSourceManager(), and SM.
|
static |
Definition at line 2867 of file UnsafeBufferUsage.cpp.
References createOverloadsForFixedParams(), clang::VariableGroupsManager::getGroupOfParms(), and clang::Member.
Referenced by getFixIts().
|
static |
Definition at line 2425 of file UnsafeBufferUsage.cpp.
References clang::FixItHint::CreateInsertion(), clang::DeclaratorDecl::getBeginLoc(), clang::Stmt::getBeginLoc(), clang::FunctionDecl::getBody(), getEndOfLine(), getFunNameText(), clang::NamedDecl::getIdentifier(), clang::ASTContext::getLangOpts(), clang::NamedDecl::getName(), clang::IdentifierInfo::getName(), clang::FunctionDecl::getNumParams(), clang::FunctionDecl::getParamDecl(), getPastLoc(), getPointeeTypeText(), getRangeText(), clang::ASTContext::getSourceManager(), getSourceRangeToTokenEnd(), getSpanTypeText(), clang::UnsafeBufferUsageHandler::getUnsafeBufferUsageAttributeTextAt(), getUserFillPlaceHolder(), hasConflictingOverload(), clang::Decl::isImplicit(), Loc, clang::FunctionDecl::param_begin(), clang::Redeclarable< decl_type >::redecls(), and SM.
Referenced by createFunctionOverloadsForParms().
|
static |
Definition at line 2306 of file UnsafeBufferUsage.cpp.
References clang::ASTContext::getLangOpts(), getPointeeTypeText(), clang::ASTContext::getSourceManager(), clang::ValueDecl::getType(), and clang::Type::isPointerType().
Referenced by fixLocalVarDeclWithSpan(), and fixParamWithSpan().
|
static |
Definition at line 2838 of file UnsafeBufferUsage.cpp.
References clang::VariableGroupsManager::getGroupOfVar(), and clang::Member.
Referenced by getFixIts().
|
static |
Scan the function and return a list of gadgets found with provided kits.
Definition at line 1395 of file UnsafeBufferUsage.cpp.
References clang::ast_matchers::MatchFinder::addMatcher(), clang::ast_matchers::allOf, clang::ast_matchers::anyOf, clang::ast_matchers::bindingDecl, D, clang::ast_matchers::declRefExpr, clang::ast_matchers::declStmt, clang::ast_matchers::eachOf, FIXABLE_GADGET, clang::Decl::getASTContext(), clang::Decl::getBody(), clang::ast_matchers::hasArrayType(), clang::ast_matchers::hasPointerType(), clang::ast_matchers::MatchFinder::match(), NEXT, clang::ast_matchers::MatchFinder::MatchCallback::run(), clang::ast_matchers::stmt, clang::ast_matchers::unless, clang::ast_matchers::varDecl, WARNING_CONTAINER_GADGET, and WARNING_GADGET.
Referenced by clang::checkUnsafeBufferUsage().
|
static |
Definition at line 2342 of file UnsafeBufferUsage.cpp.
References clang::FixItHint::CreateReplacement(), createSpanTypeForVarDecl(), D, DEBUG_NOTE_DECL_FAIL, FixVarInitializerWithSpan(), clang::Decl::getBeginLoc(), clang::SourceLocation::getLocWithOffset(), clang::ASTContext::getSourceManager(), getVarDeclIdentifierLoc(), hasUnsupportedSpecifiers(), clang::Init, and clang::SourceLocation::isValid().
Referenced by fixVariableWithSpan().
|
static |
Definition at line 2592 of file UnsafeBufferUsage.cpp.
References clang::FixItHint::CreateReplacement(), createSpanTypeForVarDecl(), DEBUG_NOTE_DECL_FAIL, clang::Qualifiers::getAsString(), clang::NamedDecl::getIdentifier(), clang::ASTContext::getLangOpts(), clang::IdentifierInfo::getName(), getPointeeTypeText(), clang::QualType::getQualifiers(), clang::ASTContext::getSourceManager(), clang::ParmVarDecl::getSourceRange(), getSpanTypeText(), clang::ValueDecl::getType(), clang::ParmVarDecl::hasDefaultArg(), clang::QualType::hasQualifiers(), and hasUnsupportedSpecifiers().
Referenced by fixVariable().
|
static |
Definition at line 2094 of file UnsafeBufferUsage.cpp.
References clang::FixItHint::CreateReplacement(), clang::Decl::getASTContext(), clang::DeclRefExpr::getDecl(), getExprText(), clang::Expr::getIntegerConstantExpr(), clang::ASTContext::getLangOpts(), clang::ASTContext::getSourceManager(), clang::DynTypedNode::getSourceRange(), Node, and SM.
|
static |
Definition at line 2661 of file UnsafeBufferUsage.cpp.
References clang::FixItHint::CreateReplacement(), D, DEBUG_NOTE_DECL_FAIL, clang::Lexer::findNextToken(), clang::ASTContext::getAsConstantArrayType(), clang::Decl::getBeginLoc(), clang::QualType::getCanonicalType(), clang::SourceRange::getEnd(), clang::ASTContext::getLangOpts(), clang::SourceLocation::getLocWithOffset(), getRangeText(), clang::ASTContext::getSourceManager(), clang::Decl::getSourceRange(), getVarDeclIdentifierLoc(), getVarDeclIdentifierText(), and clang::QualType::isNull().
Referenced by fixVariableWithArray().
|
static |
Definition at line 2757 of file UnsafeBufferUsage.cpp.
References D, DEBUG_NOTE_DECL_FAIL, fixParamWithSpan(), fixVariableWithArray(), fixVariableWithSpan(), clang::ASTContext::getAsConstantArrayType(), clang::ValueDecl::getType(), clang::VarDecl::isLocalVarDecl(), and clang::Type::isPointerType().
Referenced by getFixIts().
|
static |
Definition at line 2735 of file UnsafeBufferUsage.cpp.
References fixVarDeclWithArray(), and clang::DeclStmt::isSingleDecl().
Referenced by fixVariable().
|
static |
Definition at line 2637 of file UnsafeBufferUsage.cpp.
References DEBUG_NOTE_DECL_FAIL, fixLocalVarDeclWithSpan(), getUserFillPlaceHolder(), and clang::DeclStmt::isSingleDecl().
Referenced by fixVariable().
|
static |
Definition at line 2216 of file UnsafeBufferUsage.cpp.
References clang::FixItHint::CreateInsertion(), clang::FixItHint::CreateRemoval(), clang::ASTContext::getAsConstantArrayType(), getEndCharLoc(), getExprText(), clang::ASTContext::getLangOpts(), getPastLoc(), clang::ASTContext::getSourceManager(), clang::Init, and SM.
Referenced by fixLocalVarDeclWithSpan().
|
static |
Definition at line 1744 of file UnsafeBufferUsage.cpp.
References clang::SourceLocation::isValid(), Loc, clang::Lexer::MeasureTokenLength(), Node, and SM.
Referenced by FixVarInitializerWithSpan(), UPCPreIncrementGadget::getFixits(), and UUCAddAssignGadget::getFixits().
|
static |
Definition at line 1728 of file UnsafeBufferUsage.cpp.
Referenced by createOverloadsForFixedParams().
|
static |
Definition at line 1768 of file UnsafeBufferUsage.cpp.
References E, clang::Stmt::getBeginLoc(), clang::CharSourceRange::getCharRange(), getPastLoc(), clang::Lexer::getSourceText(), and SM.
Referenced by fixUPCAddressofArraySubscriptWithSpan(), and FixVarInitializerWithSpan().
|
static |
Definition at line 2891 of file UnsafeBufferUsage.cpp.
References clang::UnsafeBufferUsageHandler::addDebugNoteForVar(), clang::internal::anyConflict(), FixableGadgetSets::byVar, createFunctionOverloadsForParms(), D, eraseVarsForUnfixableGroupMates(), fixVariable(), clang::VariableGroupsManager::getGroupOfVar(), clang::ASTContext::getSourceManager(), Iter, and overlapWithMacro().
Referenced by clang::checkUnsafeBufferUsage().
|
static |
Definition at line 1932 of file UnsafeBufferUsage.cpp.
References clang::DeclarationNameInfo::getBeginLoc(), clang::NestedNameSpecifierLoc::getBeginLoc(), clang::DeclarationNameInfo::getEndLoc(), clang::Lexer::getLocForEndOfToken(), clang::FunctionDecl::getNameInfo(), clang::DeclaratorDecl::getQualifier(), clang::DeclaratorDecl::getQualifierLoc(), getRangeText(), and SM.
Referenced by createOverloadsForFixedParams().
|
static |
Definition at line 2991 of file UnsafeBufferUsage.cpp.
References clang::QualType::getCanonicalType(), and clang::ValueDecl::getType().
Referenced by clang::checkUnsafeBufferUsage().
|
static |
Definition at line 1757 of file UnsafeBufferUsage.cpp.
References clang::Lexer::getLocForEndOfToken(), clang::SourceLocation::isValid(), Loc, Node, and SM.
Referenced by createDataFixit(), createOverloadsForFixedParams(), FixVarInitializerWithSpan(), getExprText(), and DerefSimplePtrArithFixableGadget::getFixits().
|
static |
Definition at line 1859 of file UnsafeBufferUsage.cpp.
References clang::TypeLoc::castAs(), clang::TypeLoc::getBeginLoc(), clang::TypeLoc::getEndLoc(), clang::Lexer::getLocForEndOfToken(), clang::TypeLoc::getNextTypeLoc(), clang::Type::getPointeeType(), clang::QualType::getQualifiers(), getRangeText(), clang::TypeLoc::getSourceRange(), clang::ValueDecl::getType(), clang::TypeSourceInfo::getTypeLoc(), clang::TypeLoc::getTypeLocClass(), clang::DeclaratorDecl::getTypeSourceInfo(), clang::TypeLoc::getUnqualifiedLoc(), getVarDeclIdentifierLoc(), clang::QualType::hasQualifiers(), clang::Type::isFunctionPointerType(), clang::TypeLoc::isNull(), clang::Type::isPointerType(), clang::SourceLocation::isValid(), clang::SourceRange::isValid(), and SM.
Referenced by createOverloadsForFixedParams(), createSpanTypeForVarDecl(), and fixParamWithSpan().
|
static |
Definition at line 1782 of file UnsafeBufferUsage.cpp.
References clang::CharSourceRange::getCharRange(), clang::Lexer::getSourceText(), clang::Invalid, SM, and Text.
Referenced by createOverloadsForFixedParams(), fixVarDeclWithArray(), getFunNameText(), getPointeeTypeText(), and getVarDeclIdentifierText().
|
static |
Definition at line 1841 of file UnsafeBufferUsage.cpp.
References Begin, D, clang::Decl::getBeginLoc(), clang::Decl::getEndLoc(), clang::Lexer::getLocForEndOfToken(), and SM.
Referenced by createOverloadsForFixedParams().
|
static |
Definition at line 1953 of file UnsafeBufferUsage.cpp.
Referenced by createOverloadsForFixedParams(), and fixParamWithSpan().
std::string getUserFillPlaceHolder | ( | StringRef | HintTextToUser = "placeholder" | ) |
Definition at line 1734 of file UnsafeBufferUsage.cpp.
References s.
Referenced by createOverloadsForFixedParams(), and fixVariableWithSpan().
|
static |
Definition at line 1796 of file UnsafeBufferUsage.cpp.
References clang::Decl::getLocation().
Referenced by fixLocalVarDeclWithSpan(), fixVarDeclWithArray(), getPointeeTypeText(), and getVarDeclIdentifierText().
|
static |
Definition at line 1804 of file UnsafeBufferUsage.cpp.
References clang::Lexer::getLocForEndOfToken(), getRangeText(), getVarDeclIdentifierLoc(), clang::Lexer::isAtEndOfMacroExpansion(), clang::SourceLocation::isMacroID(), and SM.
Referenced by fixVarDeclWithArray().
|
static |
Definition at line 1547 of file UnsafeBufferUsage.cpp.
References FixableGadgetSets::byVar, and clang::DeclRefExpr::getDecl().
Referenced by clang::checkUnsafeBufferUsage().
|
static |
Definition at line 1515 of file UnsafeBufferUsage.cpp.
References WarningGadgetSets::byVar, clang::DeclRefExpr::getDecl(), and WarningGadgetSets::noVar.
Referenced by clang::checkUnsafeBufferUsage().
|
static |
Definition at line 2389 of file UnsafeBufferUsage.cpp.
References clang::Decl::getDeclContext(), clang::NamedDecl::getDeclName(), clang::DeclContextLookupResult::isSingleResult(), and clang::DeclContext::lookup().
Referenced by createOverloadsForFixedParams().
|
static |
Definition at line 1822 of file UnsafeBufferUsage.cpp.
References clang::Decl::attrs(), clang::VarDecl::hasConstantInitialization(), clang::VarDecl::hasLocalStorage(), clang::VarDecl::isConstexpr(), and clang::VarDecl::isInlineSpecified().
Referenced by fixLocalVarDeclWithSpan(), and fixParamWithSpan().
|
static |
Definition at line 1668 of file UnsafeBufferUsage.cpp.
References clang::Expr::getIntegerConstantExpr(), clang::Expr::getType(), and clang::Type::isUnsignedIntegerType().
Referenced by UUCAddAssignGadget::getFixits().
Definition at line 2830 of file UnsafeBufferUsage.cpp.
References D, and clang::Decl::getDeclContext().
Referenced by clang::checkUnsafeBufferUsage().
|
static |
Definition at line 2817 of file UnsafeBufferUsage.cpp.
References clang::SourceRange::getBegin(), clang::SourceRange::getEnd(), clang::SourceLocation::isMacroID(), Range, and clang::FixItHint::RemoveRange.
Referenced by getFixIts().