clang-tools 19.0.0git
Namespaces | Classes | Functions
clang::tidy::utils Namespace Reference

Namespaces

namespace  decl_ref_expr
 
namespace  fixit
 
namespace  lexer
 
namespace  options
 
namespace  type_traits
 

Classes

struct  BraceInsertionHints
 A provider of fix-it hints to insert opening and closing braces. More...
 
class  ExceptionAnalyzer
 This class analysis if a FunctionDecl can in principle throw an exception, either directly or indirectly. More...
 
class  ExceptionSpecAnalyzer
 This class analysis if a FunctionDecl has been declared implicitly through defaulting or explicitly as throwing or not and evaluates noexcept expressions if needed. More...
 
class  ExprSequence
 Provides information about the evaluation order of (sub-)expressions within a CFGBlock. More...
 
class  FormatStringConverter
 Convert a printf-style format string to a std::formatter-style one, and prepare any casts that are required to wrap the arguments to retain printf compatibility. More...
 
class  HeaderGuardCheck
 Finds and fixes header guards. More...
 
class  IncludeInserter
 Produces fixes to insert specified includes to source files, if not yet present. More...
 
class  IncludeInserterCallback
 
class  IncludeSorter
 Class used by IncludeInserterCallback to record the names of the inclusions in a given source file being processed and generate the necessary commands to sort the inclusions according to the precedence encoded in IncludeKinds. More...
 
class  NamespaceAliaser
 
class  StmtToBlockMap
 Maps Stmts to the CFGBlock that contains them. More...
 
class  TransformerClangTidyCheck
 A base class for defining a ClangTidy check based on a RewriteRule. More...
 
class  UsingInserter
 

Functions

static bool isAccessForVar (const Stmt *S, const VarDecl *Var)
 Return whether S is a reference to the declaration of Var.
 
static bool capturesByRef (const CXXRecordDecl *RD, const VarDecl *Var)
 
static bool isPtrOrReferenceForVar (const Stmt *S, const VarDecl *Var)
 Return whether Var has a pointer or reference in S.
 
static bool hasPtrOrReferenceInStmt (const Stmt *S, const VarDecl *Var)
 Return whether Var has a pointer or reference in S.
 
static bool refersToEnclosingLambdaCaptureByRef (const Decl *Func, const VarDecl *Var)
 
bool hasPtrOrReferenceInFunc (const Decl *Func, const VarDecl *Var)
 Returns whether Var has a pointer or reference in Func.
 
const FunctionDecl * getSurroundingFunction (ASTContext &Context, const Stmt &Statement)
 
bool isBinaryOrTernary (const Expr *E)
 
bool exprHasBitFlagWithSpelling (const Expr *Flags, const SourceManager &SM, const LangOptions &LangOpts, StringRef FlagName)
 Checks whether a macro flag is present in the given argument.
 
bool rangeIsEntirelyWithinMacroArgument (SourceRange Range, const SourceManager *SM)
 
bool rangeContainsMacroExpansion (SourceRange Range, const SourceManager *SM)
 
bool rangeCanBeFixed (SourceRange Range, const SourceManager *SM)
 
bool areStatementsIdentical (const Stmt *FirstStmt, const Stmt *SecondStmt, const ASTContext &Context, bool Canonical)
 
const IndirectFieldDecl * findOutermostIndirectFieldDeclForField (const FieldDecl *FD)
 
static tok::TokenKind getTokenKind (SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
 
static SourceLocation findEndLocation (const Stmt &S, const SourceManager &SM, const LangOptions &LangOpts)
 
BraceInsertionHints getBraceInsertionsHints (const Stmt *const S, const LangOptions &LangOpts, const SourceManager &SM, SourceLocation StartLoc, SourceLocation EndLocHint=SourceLocation())
 Create fix-it hints for braces that wrap the given statement when applied.
 
llvm::DenseMap< SourceLocation, std::string > getUnwrittenDesignators (const InitListExpr *Syn)
 
llvm::DenseMap< clang::SourceLocation, std::string > getUnwrittenDesignators (const clang::InitListExpr *Syn)
 Get designators describing the elements of a (syntactic) init list.
 
static bool canThrow (const FunctionDecl *Func)
 
static SmallVector< const Stmt *, 1 > getParentStmts (const Stmt *S, ASTContext *Context)
 
bool isExpansionLocInHeaderFile (SourceLocation Loc, const SourceManager &SM, const FileExtensionsSet &HeaderFileExtensions)
 Checks whether expansion location of Loc is in header file.
 
bool isPresumedLocInHeaderFile (SourceLocation Loc, SourceManager &SM, const FileExtensionsSet &HeaderFileExtensions)
 Checks whether presumed location of Loc is in header file.
 
bool isSpellingLocInHeaderFile (SourceLocation Loc, SourceManager &SM, const FileExtensionsSet &HeaderFileExtensions)
 Checks whether spelling location of Loc is in header file.
 
bool parseFileExtensions (StringRef AllFileExtensions, FileExtensionsSet &FileExtensions, StringRef Delimiters)
 Parses header file extensions from a semicolon-separated list.
 
std::optional< StringRef > getFileExtension (StringRef FileName, const FileExtensionsSet &FileExtensions)
 Decides whether a file has a header file extension.
 
bool isFileExtension (StringRef FileName, const FileExtensionsSet &FileExtensions)
 Decides whether a file has one of the specified file extensions.
 
StringRef defaultHeaderFileExtensions ()
 Returns recommended default value for the list of header file extensions.
 
StringRef defaultImplementationFileExtensions ()
 Returns recommended default value for the list of implementation file extensions.
 
StringRef defaultFileExtensionDelimiters ()
 Returns recommended default value for the list of file extension delimiters.
 
static bool isRealCharType (const clang::QualType &Ty)
 Is the passed type the actual "char" type, whether that be signed or unsigned, rather than explicit signed char or unsigned char types.
 
static std::optional< std::string > getCorrespondingSignedTypeName (const clang::QualType &QT)
 If possible, return the text name of the signed type that corresponds to the passed integer type.
 
static std::optional< std::string > getCorrespondingUnsignedTypeName (const clang::QualType &QT)
 If possible, return the text name of the unsigned type that corresponds to the passed integer type.
 
static std::optional< std::string > castTypeForArgument (ConversionSpecifier::Kind ArgKind, const clang::QualType &QT)
 
static bool isMatchingSignedness (ConversionSpecifier::Kind ArgKind, const clang::QualType &ArgType)
 
static bool castMismatchedIntegerTypes (const CallExpr *Call, bool StrictMode)
 
static std::string withoutCStrReplacement (const BoundNodes &CStrRemovalMatch, ASTContext &Context)
 
static std::string cleanPath (StringRef Path)
 canonicalize a path by removing ./ and ../ components.
 
 AST_MATCHER_P (NamespaceAliasDecl, hasTargetNamespace, ast_matchers::internal::Matcher< NamespaceDecl >, innerMatcher)
 
static bool hasGenerator (const transformer::Generator< std::string > &G)
 
static void verifyRule (const RewriteRuleWith< std::string > &Rule)
 
std::string escapeForDiagnostic (std::string ToEscape)
 
static StringRef getUnqualifiedName (StringRef QualifiedName)
 

Function Documentation

◆ areStatementsIdentical()

bool clang::tidy::utils::areStatementsIdentical ( const Stmt *  FirstStmt,
const Stmt *  SecondStmt,
const ASTContext &  Context,
bool  Canonical 
)

◆ AST_MATCHER_P()

clang::tidy::utils::AST_MATCHER_P ( NamespaceAliasDecl  ,
hasTargetNamespace  ,
ast_matchers::internal::Matcher< NamespaceDecl >  ,
innerMatcher   
)

Definition at line 23 of file NamespaceAliaser.cpp.

References Builder, and Node.

◆ canThrow()

static bool clang::tidy::utils::canThrow ( const FunctionDecl *  Func)
static

Definition at line 319 of file ExceptionAnalyzer.cpp.

◆ capturesByRef()

static bool clang::tidy::utils::capturesByRef ( const CXXRecordDecl *  RD,
const VarDecl *  Var 
)
static

Definition at line 24 of file Aliasing.cpp.

References C.

Referenced by isPtrOrReferenceForVar(), and refersToEnclosingLambdaCaptureByRef().

◆ castMismatchedIntegerTypes()

static bool clang::tidy::utils::castMismatchedIntegerTypes ( const CallExpr *  Call,
bool  StrictMode 
)
static

For printf-style functions, the signedness of the type printed is indicated by the corresponding type in the format string. std::print will determine the signedness from the type of the argument. This means that it is necessary to generate a cast in StrictMode to ensure that the exact behaviour is maintained. However, for templated functions like absl::PrintF and fmt::printf, the signedness of the type printed is also taken from the actual argument like std::print, so such casts are never necessary. printf-style functions are variadic, whereas templated ones aren't, so we can use that to distinguish between the two cases.

Definition at line 178 of file FormatStringConverter.cpp.

◆ castTypeForArgument()

static std::optional< std::string > clang::tidy::utils::castTypeForArgument ( ConversionSpecifier::Kind  ArgKind,
const clang::QualType &  QT 
)
static

◆ cleanPath()

static std::string clang::tidy::utils::cleanPath ( StringRef  Path)
static

canonicalize a path by removing ./ and ../ components.

Definition at line 19 of file HeaderGuard.cpp.

References Path.

◆ defaultFileExtensionDelimiters()

StringRef clang::tidy::utils::defaultFileExtensionDelimiters ( )
inline

Returns recommended default value for the list of file extension delimiters.

Definition at line 45 of file FileExtensionsUtils.h.

◆ defaultHeaderFileExtensions()

StringRef clang::tidy::utils::defaultHeaderFileExtensions ( )
inline

Returns recommended default value for the list of header file extensions.

Definition at line 35 of file FileExtensionsUtils.h.

◆ defaultImplementationFileExtensions()

StringRef clang::tidy::utils::defaultImplementationFileExtensions ( )
inline

Returns recommended default value for the list of implementation file extensions.

Definition at line 39 of file FileExtensionsUtils.h.

◆ escapeForDiagnostic()

std::string clang::tidy::utils::escapeForDiagnostic ( std::string  ToEscape)

Definition at line 33 of file TransformerClangTidyCheck.cpp.

References C, and Pos.

Referenced by clang::tidy::utils::TransformerClangTidyCheck::check().

◆ exprHasBitFlagWithSpelling()

bool clang::tidy::utils::exprHasBitFlagWithSpelling ( const Expr *  Flags,
const SourceManager &  SM,
const LangOptions &  LangOpts,
StringRef  FlagName 
)

Checks whether a macro flag is present in the given argument.

Only considers cases of single match or match in a binary OR expression. For example, <needed-flag> or <flag> | <needed-flag> | ...

Definition at line 38 of file ASTUtils.cpp.

References exprHasBitFlagWithSpelling(), Flags, and MacroName.

Referenced by exprHasBitFlagWithSpelling(), and clang::tidy::android::CloexecCheck::insertMacroFlag().

◆ findEndLocation()

static SourceLocation clang::tidy::utils::findEndLocation ( const Stmt &  S,
const SourceManager &  SM,
const LangOptions &  LangOpts 
)
static

◆ findOutermostIndirectFieldDeclForField()

const IndirectFieldDecl * clang::tidy::utils::findOutermostIndirectFieldDeclForField ( const FieldDecl *  FD)

Definition at line 117 of file ASTUtils.cpp.

◆ getBraceInsertionsHints()

BraceInsertionHints clang::tidy::utils::getBraceInsertionsHints ( const Stmt *const  S,
const LangOptions &  LangOpts,
const SourceManager &  SM,
SourceLocation  StartLoc,
SourceLocation  EndLocHint = SourceLocation() 
)

Create fix-it hints for braces that wrap the given statement when applied.

The algorithm computing them respects comment before and after the statement and adds line breaks before the braces accordingly.

Definition at line 102 of file BracesAroundStatement.cpp.

References findEndLocation(), and getTokenKind().

Referenced by clang::tidy::readability::AvoidReturnWithVoidValueCheck::check().

◆ getCorrespondingSignedTypeName()

static std::optional< std::string > clang::tidy::utils::getCorrespondingSignedTypeName ( const clang::QualType &  QT)
static

If possible, return the text name of the signed type that corresponds to the passed integer type.

If the passed type is already signed then its name is just returned. Only supports BuiltinTypes.

Definition at line 46 of file FormatStringConverter.cpp.

Referenced by castTypeForArgument().

◆ getCorrespondingUnsignedTypeName()

static std::optional< std::string > clang::tidy::utils::getCorrespondingUnsignedTypeName ( const clang::QualType &  QT)
static

If possible, return the text name of the unsigned type that corresponds to the passed integer type.

If the passed type is already unsigned then its name is just returned. Only supports BuiltinTypes.

Definition at line 98 of file FormatStringConverter.cpp.

Referenced by castTypeForArgument().

◆ getFileExtension()

std::optional< StringRef > clang::tidy::utils::getFileExtension ( StringRef  FileName,
const FileExtensionsSet FileExtensions 
)

Decides whether a file has a header file extension.

Returns the file extension, if included in the provided set.

Definition at line 56 of file FileExtensionsUtils.cpp.

References FileName.

Referenced by isFileExtension().

◆ getParentStmts()

static SmallVector< const Stmt *, 1 > clang::tidy::utils::getParentStmts ( const Stmt *  S,
ASTContext *  Context 
)
static

◆ getSurroundingFunction()

const FunctionDecl * clang::tidy::utils::getSurroundingFunction ( ASTContext &  Context,
const Stmt &  Statement 
)

◆ getTokenKind()

static tok::TokenKind clang::tidy::utils::getTokenKind ( SourceLocation  Loc,
const SourceManager &  SM,
const LangOptions &  LangOpts 
)
static

Definition at line 48 of file BracesAroundStatement.cpp.

References Loc.

Referenced by findEndLocation(), and getBraceInsertionsHints().

◆ getUnqualifiedName()

static StringRef clang::tidy::utils::getUnqualifiedName ( StringRef  QualifiedName)
static

◆ getUnwrittenDesignators() [1/2]

llvm::DenseMap< clang::SourceLocation, std::string > clang::tidy::utils::getUnwrittenDesignators ( const clang::InitListExpr *  Syn)

Get designators describing the elements of a (syntactic) init list.

Given for example the type

struct S { int i, j; };

and the definition

S s{1, 2};

calling getUnwrittenDesignators for the initializer list expression {1, 2} would produce the map {loc(1): ".i", loc(2): ".j"}.

It does not produce designators for any explicitly-written nested lists, e.g. {1, .j=2} would only return {loc(1): ".i"}.

It also considers structs with fields of record types like struct T { S s; };. In this case, there would be designators of the form .s.i and .s.j in the returned map.

◆ getUnwrittenDesignators() [2/2]

llvm::DenseMap< SourceLocation, std::string > clang::tidy::utils::getUnwrittenDesignators ( const InitListExpr *  Syn)

Definition at line 175 of file DesignatedInitializers.cpp.

◆ hasGenerator()

static bool clang::tidy::utils::hasGenerator ( const transformer::Generator< std::string > &  G)
static

Definition at line 19 of file TransformerClangTidyCheck.cpp.

Referenced by verifyRule().

◆ hasPtrOrReferenceInFunc()

bool clang::tidy::utils::hasPtrOrReferenceInFunc ( const Decl Func,
const VarDecl *  Var 
)

Returns whether Var has a pointer or reference in Func.

Example: void f() { int n; ... int *p = &n; }

For f() and n the function returns true because p is a pointer to n created in f().

Definition at line 92 of file Aliasing.cpp.

References hasPtrOrReferenceInFunc(), hasPtrOrReferenceInStmt(), and refersToEnclosingLambdaCaptureByRef().

Referenced by hasPtrOrReferenceInFunc().

◆ hasPtrOrReferenceInStmt()

static bool clang::tidy::utils::hasPtrOrReferenceInStmt ( const Stmt *  S,
const VarDecl *  Var 
)
static

Return whether Var has a pointer or reference in S.

Definition at line 64 of file Aliasing.cpp.

References hasPtrOrReferenceInStmt(), and isPtrOrReferenceForVar().

Referenced by hasPtrOrReferenceInFunc(), and hasPtrOrReferenceInStmt().

◆ isAccessForVar()

static bool clang::tidy::utils::isAccessForVar ( const Stmt *  S,
const VarDecl *  Var 
)
static

Return whether S is a reference to the declaration of Var.

Definition at line 17 of file Aliasing.cpp.

Referenced by isPtrOrReferenceForVar().

◆ isBinaryOrTernary()

bool clang::tidy::utils::isBinaryOrTernary ( const Expr *  E)

Definition at line 25 of file ASTUtils.cpp.

References E, and isBinaryOrTernary().

Referenced by isBinaryOrTernary().

◆ isExpansionLocInHeaderFile()

bool clang::tidy::utils::isExpansionLocInHeaderFile ( SourceLocation  Loc,
const SourceManager &  SM,
const FileExtensionsSet HeaderFileExtensions 
)

Checks whether expansion location of Loc is in header file.

Definition at line 16 of file FileExtensionsUtils.cpp.

References isFileExtension(), and Loc.

◆ isFileExtension()

bool clang::tidy::utils::isFileExtension ( StringRef  FileName,
const FileExtensionsSet FileExtensions 
)

◆ isMatchingSignedness()

static bool clang::tidy::utils::isMatchingSignedness ( ConversionSpecifier::Kind  ArgKind,
const clang::QualType &  ArgType 
)
static

Definition at line 156 of file FormatStringConverter.cpp.

◆ isPresumedLocInHeaderFile()

bool clang::tidy::utils::isPresumedLocInHeaderFile ( SourceLocation  Loc,
SourceManager &  SM,
const FileExtensionsSet HeaderFileExtensions 
)

Checks whether presumed location of Loc is in header file.

Definition at line 22 of file FileExtensionsUtils.cpp.

References isFileExtension(), and Loc.

Referenced by clang::tidy::bugprone::DynamicStaticInitializersCheck::check(), and clang::tidy::google::build::UnnamedNamespaceInHeaderCheck::check().

◆ isPtrOrReferenceForVar()

static bool clang::tidy::utils::isPtrOrReferenceForVar ( const Stmt *  S,
const VarDecl *  Var 
)
static

Return whether Var has a pointer or reference in S.

Definition at line 32 of file Aliasing.cpp.

References capturesByRef(), Decl, and isAccessForVar().

Referenced by hasPtrOrReferenceInStmt().

◆ isRealCharType()

static bool clang::tidy::utils::isRealCharType ( const clang::QualType &  Ty)
static

Is the passed type the actual "char" type, whether that be signed or unsigned, rather than explicit signed char or unsigned char types.

Definition at line 33 of file FormatStringConverter.cpp.

References Type.

◆ isSpellingLocInHeaderFile()

bool clang::tidy::utils::isSpellingLocInHeaderFile ( SourceLocation  Loc,
SourceManager &  SM,
const FileExtensionsSet HeaderFileExtensions 
)

Checks whether spelling location of Loc is in header file.

Definition at line 28 of file FileExtensionsUtils.cpp.

References isFileExtension(), and Loc.

Referenced by clang::tidy::google::readability::GlobalNamesInHeadersCheck::check(), and clang::tidy::llvm_libc::InlineFunctionDeclCheck::check().

◆ parseFileExtensions()

bool clang::tidy::utils::parseFileExtensions ( StringRef  AllFileExtensions,
FileExtensionsSet FileExtensions,
StringRef  Delimiters 
)

Parses header file extensions from a semicolon-separated list.

Definition at line 34 of file FileExtensionsUtils.cpp.

References Suffix, and Suffixes.

◆ rangeCanBeFixed()

bool clang::tidy::utils::rangeCanBeFixed ( SourceRange  Range,
const SourceManager *  SM 
)

◆ rangeContainsMacroExpansion()

bool clang::tidy::utils::rangeContainsMacroExpansion ( SourceRange  Range,
const SourceManager *  SM 
)

Definition at line 81 of file ASTUtils.cpp.

References Range, and rangeIsEntirelyWithinMacroArgument().

Referenced by rangeCanBeFixed().

◆ rangeIsEntirelyWithinMacroArgument()

bool clang::tidy::utils::rangeIsEntirelyWithinMacroArgument ( SourceRange  Range,
const SourceManager *  SM 
)

Definition at line 65 of file ASTUtils.cpp.

References Range.

Referenced by rangeCanBeFixed(), and rangeContainsMacroExpansion().

◆ refersToEnclosingLambdaCaptureByRef()

static bool clang::tidy::utils::refersToEnclosingLambdaCaptureByRef ( const Decl Func,
const VarDecl *  Var 
)
static

Definition at line 79 of file Aliasing.cpp.

References capturesByRef().

Referenced by hasPtrOrReferenceInFunc().

◆ verifyRule()

static void clang::tidy::utils::verifyRule ( const RewriteRuleWith< std::string > &  Rule)
static

◆ withoutCStrReplacement()

static std::string clang::tidy::utils::withoutCStrReplacement ( const BoundNodes &  CStrRemovalMatch,
ASTContext &  Context 
)
static