clang  19.0.0git
Public Types | Public Member Functions | Friends | List of all members
clang::ento::CallDescription Class Reference

A CallDescription is a pattern that can be used to match calls based on the qualified name and the argument/parameter counts. More...

#include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h"

Public Types

enum class  Mode {
  CLibrary , CLibraryMaybeHardened , SimpleFunc , CXXMethod ,
  Unspecified
}
 

Public Member Functions

 CallDescription (Mode MatchAs, ArrayRef< StringRef > QualifiedName, MaybeCount RequiredArgs=std::nullopt, MaybeCount RequiredParams=std::nullopt)
 Constructs a CallDescription object. More...
 
StringRef getFunctionName () const
 Get the name of the function that this object matches. More...
 
auto begin_qualified_name_parts () const
 Get the qualified name parts in reversed order. More...
 
auto end_qualified_name_parts () const
 
bool hasQualifiedNameParts () const
 It's false, if and only if we expect a single identifier, such as getenv. More...
 

Friends

class CallEvent
 

Matching CallDescriptions against a CallEvent

bool matchesAny (const CallEvent &Call, const CallDescription &CD1)
 Returns true whether the CallEvent matches on any of the CallDescriptions supplied. More...
 
template<typename... Ts>
bool matchesAny (const CallEvent &Call, const CallDescription &CD1, const Ts &...CDs)
 Returns true whether the CallEvent matches on any of the CallDescriptions supplied. More...
 
bool matches (const CallEvent &Call) const
 Returns true if the CallEvent is a call to a function that matches the CallDescription. More...
 

Matching CallDescriptions against a CallExpr

bool matchesAnyAsWritten (const CallExpr &CE, const CallDescription &CD1)
 Returns true whether the CallExpr matches on any of the CallDescriptions supplied. More...
 
template<typename... Ts>
bool matchesAnyAsWritten (const CallExpr &CE, const CallDescription &CD1, const Ts &...CDs)
 Returns true whether the CallExpr matches on any of the CallDescriptions supplied. More...
 
bool matchesAsWritten (const CallExpr &CE) const
 Returns true if the CallExpr is a call to a function that matches the CallDescription. More...
 

Detailed Description

A CallDescription is a pattern that can be used to match calls based on the qualified name and the argument/parameter counts.

Definition at line 32 of file CallDescription.h.

Member Enumeration Documentation

◆ Mode

Enumerator
CLibrary 

Match calls to functions from the C standard library.

This also recognizes builtin variants whose name is derived by adding "__builtin", "__inline" or similar prefixes or suffixes; but only matches functions than are externally visible and are declared either directly within a TU or in the namespace 'std'. For the exact heuristics, see CheckerContext::isCLibraryFunction().

CLibraryMaybeHardened 

An extended version of the CLibrary mode that also matches the hardened variants like __FOO_chk() and __builtin__FOO_chk() that take additional arguments compared to the "regular" function FOO().

This is not the default behavior of CLibrary because in this case the checker code must be prepared to handle the different parametrization. For the exact heuristics, see CheckerContext::isHardenedVariantOf().

SimpleFunc 

Matches "simple" functions that are not methods.

(Static methods are methods.)

CXXMethod 

Matches a C++ method (may be static, may be virtual, may be an overloaded operator, a constructor or a destructor).

Unspecified 

Match any CallEvent that is not an ObjCMethodCall.

This should not be used when the checker looks for a concrete function (and knows whether it is a method); but GenericTaintChecker uses this mode to match functions whose name was configured by the user.

Definition at line 34 of file CallDescription.h.

Constructor & Destructor Documentation

◆ CallDescription()

ento::CallDescription::CallDescription ( Mode  MatchAs,
ArrayRef< StringRef >  QualifiedName,
MaybeCount  RequiredArgs = std::nullopt,
MaybeCount  RequiredParams = std::nullopt 
)

Constructs a CallDescription object.

Parameters
MatchAsSpecifies the kind of the call that should be matched.
QualifiedNameThe list of the name qualifiers of the function that will be matched. The user is allowed to skip any of the qualifiers. For example, {"std", "basic_string", "c_str"} would match both std::basic_string<...>::c_str() and std::__1::basic_string<...>::c_str().
RequiredArgsThe expected number of arguments that are passed to the function. Omit this parameter (or pass std::nullopt) to match every occurrence without checking the argument count in the call.
RequiredParamsThe expected number of parameters in the function definition that is called. Omit this parameter to match every occurrence without checking the parameter count in the definition.

Definition at line 38 of file CallDescription.cpp.

Member Function Documentation

◆ begin_qualified_name_parts()

auto clang::ento::CallDescription::begin_qualified_name_parts ( ) const
inline

Get the qualified name parts in reversed order.

E.g. { "std", "vector", "data" } -> "vector", "std"

Definition at line 109 of file CallDescription.h.

◆ end_qualified_name_parts()

auto clang::ento::CallDescription::end_qualified_name_parts ( ) const
inline

Definition at line 112 of file CallDescription.h.

◆ getFunctionName()

StringRef clang::ento::CallDescription::getFunctionName ( ) const
inline

Get the name of the function that this object matches.

Definition at line 105 of file CallDescription.h.

◆ hasQualifiedNameParts()

bool clang::ento::CallDescription::hasQualifiedNameParts ( ) const
inline

It's false, if and only if we expect a single identifier, such as getenv.

It's true for std::swap, or my::detail::container::data.

Definition at line 116 of file CallDescription.h.

◆ matches()

bool ento::CallDescription::matches ( const CallEvent Call) const

Returns true if the CallEvent is a call to a function that matches the CallDescription.

Note
This function is not intended to be used to match Obj-C method calls.

Definition at line 51 of file CallDescription.cpp.

References clang::ento::CE_ObjCMessage.

◆ matchesAsWritten()

bool ento::CallDescription::matchesAsWritten ( const CallExpr CE) const

Returns true if the CallExpr is a call to a function that matches the CallDescription.

When available, always prefer matching with a CallEvent! This function exists only when that is not available, for example, when only syntactic check is done on a piece of code.

Also, StdLibraryFunctionsChecker::Signature is likely a better candicade for syntactic only matching if you are writing a new checker. This is handy if a CallDescriptionMap is already there.

The function is imprecise because CallEvent may know path sensitive information, such as the precise argument count (see comments for CallEvent::getNumArgs), the called function if it was called through a function pointer, and other information not available syntactically.

Definition at line 63 of file CallDescription.cpp.

References clang::CallExpr::getCalleeDecl(), and clang::CallExpr::getNumArgs().

Friends And Related Function Documentation

◆ CallEvent

friend class CallEvent
friend

Definition at line 72 of file CallDescription.h.

◆ matchesAny [1/2]

bool matchesAny ( const CallEvent Call,
const CallDescription CD1 
)
friend

Returns true whether the CallEvent matches on any of the CallDescriptions supplied.

Note
This function is not intended to be used to match Obj-C method calls.

Definition at line 133 of file CallDescription.h.

◆ matchesAny [2/2]

template<typename... Ts>
bool matchesAny ( const CallEvent Call,
const CallDescription CD1,
const Ts &...  CDs 
)
friend

Returns true whether the CallEvent matches on any of the CallDescriptions supplied.

Note
This function is not intended to be used to match Obj-C method calls.

Definition at line 139 of file CallDescription.h.

◆ matchesAnyAsWritten [1/2]

bool matchesAnyAsWritten ( const CallExpr CE,
const CallDescription CD1 
)
friend

Returns true whether the CallExpr matches on any of the CallDescriptions supplied.

Note
This function is not intended to be used to match Obj-C method calls.

Definition at line 170 of file CallDescription.h.

◆ matchesAnyAsWritten [2/2]

template<typename... Ts>
bool matchesAnyAsWritten ( const CallExpr CE,
const CallDescription CD1,
const Ts &...  CDs 
)
friend

Returns true whether the CallExpr matches on any of the CallDescriptions supplied.

Note
This function is not intended to be used to match Obj-C method calls.

Definition at line 177 of file CallDescription.h.


The documentation for this class was generated from the following files: