clang-tools 20.0.0git
Classes | Namespaces | Macros | Typedefs | Enumerations | Functions | Variables
EasilySwappableParametersCheck.cpp File Reference
#include "EasilySwappableParametersCheck.h"
#include "../utils/OptionsUtils.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Lex/Lexer.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/Support/Debug.h"
#include <optional>

Go to the source code of this file.

Classes

struct  clang::tidy::bugprone::model::ConversionSequence
 The results of the steps of an Implicit Conversion Sequence is saved in an instance of this record. More...
 
struct  clang::tidy::bugprone::model::ConversionSequence::UserDefinedConvertingConstructor
 
struct  clang::tidy::bugprone::model::ConversionSequence::UserDefinedConversionOperator
 
struct  clang::tidy::bugprone::model::MixData
 Contains the metadata for the mixability result between two types, independently of which parameters they were calculated from. More...
 
struct  clang::tidy::bugprone::model::Mix
 A named tuple that contains the information for a mix between two concrete parameters. More...
 
struct  clang::tidy::bugprone::model::MixableParameterRange
 
class  clang::tidy::bugprone::filter::relatedness_heuristic::AppearsInSameExpr
 Implements the heuristic that marks two parameters related if there is a usage for both in the same strict expression subtree. More...
 
class  clang::tidy::bugprone::filter::relatedness_heuristic::PassedToSameFunction
 Implements the heuristic that marks two parameters related if there are two separate calls to the same function (overload) and the parameters are passed to the same index in both calls, i.e f(a, b) and f(a, c) passes b and c to the same index (2) of f(), marking them related. More...
 
class  clang::tidy::bugprone::filter::relatedness_heuristic::AccessedSameMemberOf
 Implements the heuristic that marks two parameters related if the same member is accessed (referred to) inside the current function's body. More...
 
class  clang::tidy::bugprone::filter::relatedness_heuristic::Returned
 Implements the heuristic that marks two parameters related if different ReturnStmts return them from the function. More...
 
class  clang::tidy::bugprone::filter::SimilarlyUsedParameterPairSuppressor
 Helper class that is used to detect if two parameters of the same function are used in a similar fashion, to suppress the result. More...
 

Namespaces

namespace  clang
 ===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
 
namespace  clang::tidy
 
namespace  clang::tidy::bugprone
 
namespace  clang::tidy::bugprone::filter
 
namespace  clang::tidy::bugprone::model
 
namespace  clang::tidy::bugprone::filter::relatedness_heuristic
 This namespace contains the implementations for the suppression of diagnostics from similarly-used ("related") parameters.
 

Macros

#define DEBUG_TYPE   "EasilySwappableParametersCheck"
 

Typedefs

using clang::tidy::bugprone::TheCheck = EasilySwappableParametersCheck
 
template<typename T , std::size_t N = SmallDataStructureSize>
using clang::tidy::bugprone::filter::relatedness_heuristic::ParamToSmallSetMap = llvm::DenseMap< const ParmVarDecl *, llvm::SmallSet< T, N > >
 

Enumerations

enum class  clang::tidy::bugprone::model::MixFlags : unsigned char {
  clang::tidy::bugprone::model::Invalid = 0 , clang::tidy::bugprone::model::WorkaroundDisableCanonicalEquivalence = 1 , clang::tidy::bugprone::model::None = 2 , clang::tidy::bugprone::model::Trivial = 4 ,
  clang::tidy::bugprone::model::Canonical = 8 , clang::tidy::bugprone::model::TypeAlias = 16 , clang::tidy::bugprone::model::ReferenceBind = 32 , clang::tidy::bugprone::model::Qualifiers = 64 ,
  clang::tidy::bugprone::model::ImplicitConversion = 128
}
 The language features involved in allowing the mix between two parameters. More...
 
enum class  clang::tidy::bugprone::model::ImplicitConversionModellingMode : unsigned char { clang::tidy::bugprone::model::None , clang::tidy::bugprone::model::All , clang::tidy::bugprone::model::OneWaySingleStandardOnly }
 Helper enum for the recursive calls in the modelling that toggle what kinds of implicit conversions are to be modelled. More...
 

Functions

static bool clang::tidy::bugprone::filter::isIgnoredParameter (const TheCheck &Check, const ParmVarDecl *Node)
 Returns whether the parameter's name or the parameter's type's name is configured by the user to be ignored from analysis and diagnostic.
 
static bool clang::tidy::bugprone::filter::isSimilarlyUsedParameter (const SimilarlyUsedParameterPairSuppressor &Suppressor, const ParmVarDecl *Param1, const ParmVarDecl *Param2)
 
static bool clang::tidy::bugprone::filter::prefixSuffixCoverUnderThreshold (std::size_t Threshold, StringRef Str1, StringRef Str2)
 Returns whether the two strings are prefixes or suffixes of each other with at most Threshold characters differing on the non-common end.
 
 clang::tidy::bugprone::model::LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE ()
 
static bool clang::tidy::bugprone::model::hasFlag (MixFlags Data, MixFlags SearchedFlag)
 Returns whether the SearchedFlag is turned on in the Data.
 
static std::string clang::tidy::bugprone::model::formatMixFlags (MixFlags F)
 Formats the MixFlags enum into a useful, user-readable representation.
 
static MixData clang::tidy::bugprone::model::isLRefEquallyBindingToType (const TheCheck &Check, const LValueReferenceType *LRef, QualType Ty, const ASTContext &Ctx, bool IsRefRHS, ImplicitConversionModellingMode ImplicitMode)
 Calculates if the reference binds an expression of the given type.
 
static MixData clang::tidy::bugprone::model::approximateImplicitConversion (const TheCheck &Check, QualType LType, QualType RType, const ASTContext &Ctx, ImplicitConversionModellingMode ImplicitMode)
 Returns whether an expression of LType can be used in an RType context, as per the implicit conversion rules.
 
static bool clang::tidy::bugprone::model::isUselessSugar (const Type *T)
 
static NonCVRQualifiersResult clang::tidy::bugprone::model::getNonCVRQualifiers (const ASTContext &Ctx, QualType LType, QualType RType)
 Returns if the two types are qualified in a way that ever after equating or removing local CVR qualification, even if the unqualified types would mix, the qualified ones don't, because there are some other local qualifiers that aren't equal.
 
static MixData clang::tidy::bugprone::model::calculateMixability (const TheCheck &Check, QualType LType, QualType RType, const ASTContext &Ctx, ImplicitConversionModellingMode ImplicitMode)
 Approximate the way how LType and RType might refer to "essentially the same" type, in a sense that at a particular call site, an expression of type LType and RType might be successfully passed to a variable (in our specific case, a parameter) of type RType and LType, respectively.
 
static bool clang::tidy::bugprone::model::isDerivedToBase (const CXXRecordDecl *Derived, const CXXRecordDecl *Base)
 
static std::optional< QualType > clang::tidy::bugprone::model::approximateStandardConversionSequence (const TheCheck &Check, QualType From, QualType To, const ASTContext &Ctx)
 
static std::optional< ConversionSequence > clang::tidy::bugprone::model::tryConversionOperators (const TheCheck &Check, const CXXRecordDecl *RD, QualType ToType)
 
static std::optional< ConversionSequence > clang::tidy::bugprone::model::tryConvertingConstructors (const TheCheck &Check, QualType FromType, const CXXRecordDecl *RD)
 
static MixableParameterRange clang::tidy::bugprone::model::modelMixingRange (const TheCheck &Check, const FunctionDecl *FD, std::size_t StartIndex, const filter::SimilarlyUsedParameterPairSuppressor &UsageBasedSuppressor)
 
 clang::tidy::bugprone::AST_MATCHER_FUNCTION (ast_matchers::internal::Matcher< Stmt >, paramRefExpr)
 Matches DeclRefExprs and their ignorable wrappers to ParmVarDecls.
 
template<typename MapTy , typename ElemTy >
bool clang::tidy::bugprone::filter::relatedness_heuristic::lazyMapOfSetsIntersectionExists (const MapTy &Map, const ElemTy &E1, const ElemTy &E2)
 Returns whether the sets mapped to the two elements in the map have at least one element in common.
 
static void clang::tidy::bugprone::filter::padStringAtEnd (SmallVectorImpl< char > &Str, std::size_t ToLen)
 
static void clang::tidy::bugprone::filter::padStringAtBegin (SmallVectorImpl< char > &Str, std::size_t ToLen)
 
static bool clang::tidy::bugprone::filter::isCommonPrefixWithoutSomeCharacters (std::size_t N, StringRef S1, StringRef S2)
 
static bool clang::tidy::bugprone::filter::isCommonSuffixWithoutSomeCharacters (std::size_t N, StringRef S1, StringRef S2)
 
 clang::tidy::bugprone::AST_MATCHER_P (FunctionDecl, parameterCountGE, unsigned, N)
 Matches functions that have at least the specified amount of parameters.
 
 clang::tidy::bugprone::AST_MATCHER (FunctionDecl, isOverloadedUnaryOrBinaryOperator)
 Matches any overloaded unary and binary operators.
 
static unsigned clang::tidy::bugprone::clampMinimumLength (const unsigned Value)
 Returns the DefaultMinimumLength if the Value of requested minimum length is less than 2.
 
static SmallString< 64 > clang::tidy::bugprone::getName (const NamedDecl *ND)
 Returns the diagnostic-friendly name of the node, or empty string.
 
static SmallString< 64 > clang::tidy::bugprone::getNameOrUnnamed (const NamedDecl *ND)
 Returns the diagnostic-friendly name of the node, or a constant value.
 
static bool clang::tidy::bugprone::needsToPrintTypeInDiagnostic (const model::Mix &M)
 Returns whether a particular Mix between two parameters should have the types involved diagnosed to the user.
 
static bool clang::tidy::bugprone::needsToElaborateImplicitConversion (const model::Mix &M)
 Returns whether a particular Mix between the two parameters should have implicit conversions elaborated.
 

Variables

static constexpr std::size_t DefaultMinimumLength = 2
 The default value for the MinimumLength check option.
 
static constexpr llvm::StringLiteral DefaultIgnoredParameterNames
 The default value for ignored parameter names.
 
static constexpr llvm::StringLiteral DefaultIgnoredParameterTypeSuffixes
 The default value for ignored parameter type suffixes.
 
static constexpr bool DefaultQualifiersMix = false
 The default value for the QualifiersMix check option.
 
static constexpr bool DefaultModelImplicitConversions = true
 The default value for the ModelImplicitConversions check option.
 
static constexpr bool DefaultSuppressParametersUsedTogether = true
 The default value for suppressing diagnostics about parameters that are used together.
 
static constexpr std::size_t DefaultNamePrefixSuffixSilenceDissimilarityTreshold = 1
 The default value for the NamePrefixSuffixSilenceDissimilarityTreshold check option.
 
static constexpr std::size_t clang::tidy::bugprone::filter::relatedness_heuristic::SmallDataStructureSize = 4
 

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "EasilySwappableParametersCheck"

Definition at line 17 of file EasilySwappableParametersCheck.cpp.

Variable Documentation

◆ CommonQualifiers

Qualifiers CommonQualifiers

The set of equal qualifiers between the two types.

Definition at line 593 of file EasilySwappableParametersCheck.cpp.

◆ CommonType

QualType CommonType

◆ ConversionFun

const CXXMethodDecl* ConversionFun

Definition at line 1096 of file EasilySwappableParametersCheck.cpp.

◆ DefaultIgnoredParameterNames

constexpr llvm::StringLiteral DefaultIgnoredParameterNames
staticconstexpr
Initial value:
= "\"\";"
"iterator;"
"Iterator;"
"begin;"
"Begin;"
"end;"
"End;"
"first;"
"First;"
"last;"
"Last;"
"lhs;"
"LHS;"
"rhs;"
"RHS"

The default value for ignored parameter names.

Definition at line 27 of file EasilySwappableParametersCheck.cpp.

◆ DefaultIgnoredParameterTypeSuffixes

constexpr llvm::StringLiteral DefaultIgnoredParameterTypeSuffixes
staticconstexpr
Initial value:
=
"bool;"
"Bool;"
"_Bool;"
"it;"
"It;"
"iterator;"
"Iterator;"
"inputit;"
"InputIt;"
"forwardit;"
"ForwardIt;"
"bidirit;"
"BidirIt;"
"constiterator;"
"const_iterator;"
"Const_Iterator;"
"Constiterator;"
"ConstIterator;"
"RandomIt;"
"randomit;"
"random_iterator;"
"ReverseIt;"
"reverse_iterator;"
"reverse_const_iterator;"
"ConstReverseIterator;"
"Const_Reverse_Iterator;"
"const_reverse_iterator;"
"Constreverseiterator;"
"constreverseiterator"

The default value for ignored parameter type suffixes.

Definition at line 44 of file EasilySwappableParametersCheck.cpp.

◆ DefaultMinimumLength

constexpr std::size_t DefaultMinimumLength = 2
staticconstexpr

The default value for the MinimumLength check option.

Definition at line 24 of file EasilySwappableParametersCheck.cpp.

Referenced by clang::tidy::bugprone::clampMinimumLength().

◆ DefaultModelImplicitConversions

constexpr bool DefaultModelImplicitConversions = true
staticconstexpr

The default value for the ModelImplicitConversions check option.

Definition at line 79 of file EasilySwappableParametersCheck.cpp.

◆ DefaultNamePrefixSuffixSilenceDissimilarityTreshold

constexpr std::size_t DefaultNamePrefixSuffixSilenceDissimilarityTreshold = 1
staticconstexpr

The default value for the NamePrefixSuffixSilenceDissimilarityTreshold check option.

Definition at line 88 of file EasilySwappableParametersCheck.cpp.

◆ DefaultQualifiersMix

constexpr bool DefaultQualifiersMix = false
staticconstexpr

The default value for the QualifiersMix check option.

Definition at line 76 of file EasilySwappableParametersCheck.cpp.

◆ DefaultSuppressParametersUsedTogether

constexpr bool DefaultSuppressParametersUsedTogether = true
staticconstexpr

The default value for suppressing diagnostics about parameters that are used together.

Definition at line 83 of file EasilySwappableParametersCheck.cpp.

◆ DiagnosticText

std::string DiagnosticText

Definition at line 1951 of file EasilySwappableParametersCheck.cpp.

◆ Flags

MixFlags Flags

◆ HasMixabilityBreakingQualifiers

bool HasMixabilityBreakingQualifiers

True if the types are qualified in a way that even after equating or removing local CVR qualification, even if the unqualified types themselves would mix, the qualified ones don't, because there are some other local qualifiers that are not equal.

Definition at line 590 of file EasilySwappableParametersCheck.cpp.

◆ LHSType

QualType LHSType

◆ RHSType

QualType RHSType

◆ Seq

ConversionSequence Seq

Definition at line 1098 of file EasilySwappableParametersCheck.cpp.

◆ Trivial

bool Trivial = true

The formatted sequence is trivial if it is "Ty1 -> Ty2", but Ty1 and Ty2 are the types that are shown in the code.

A trivial diagnostic does not need to be printed.

Definition at line 1956 of file EasilySwappableParametersCheck.cpp.