clang-tools 22.0.0git
clang::tidy::abseil Namespace Reference

Classes

class  AbseilModule
class  CleanupCtadCheck
 Suggests switching the initialization pattern of absl::Cleanup instances from the factory function to class template argument deduction (CTAD), in C++17 and higher. More...
class  DurationAdditionCheck
 Checks for cases where addition should be performed in the absl::Time domain. More...
class  DurationComparisonCheck
 Prefer comparison in the absl::Duration domain instead of the numeric domain. More...
class  DurationConversionCastCheck
 Checks for casts of absl::Duration conversion functions, and recommends the right conversion function instead. More...
class  DurationDivisionCheck
class  DurationFactoryFloatCheck
 This check finds cases where Duration factories are being called with floating point arguments, but could be called using integer arguments. More...
class  DurationFactoryScaleCheck
 This check finds cases where the incorrect Duration factory function is being used by looking for scaling constants inside the factory argument and suggesting a more appropriate factory. More...
class  DurationSubtractionCheck
 Checks for cases where subtraction should be performed in the absl::Duration domain. More...
class  DurationUnnecessaryConversionCheck
 Finds and fixes cases where absl::Duration values are being converted to numeric types and back again. More...
class  FasterStrsplitDelimiterCheck
 Finds instances of absl::StrSplit() or absl::MaxSplits() where the delimiter is a single character string literal and replaces it with a character. More...
class  NoInternalDependenciesCheck
 Finds instances where the user depends on internal details and warns them against doing so. More...
class  NoNamespaceCheck
 This check ensures users don't open namespace absl, as that violates Abseil's compatibility guidelines. More...
class  RedundantStrcatCallsCheck
 Flags redundant calls to absl::StrCat when the result is being passed to another call of absl::StrCat/absl::StrAppend. More...
class  StrCatAppendCheck
 Flags uses of absl::StrCat to append to a string. More...
class  StringFindStartswithCheck
class  StringFindStrContainsCheck
 Finds s.find(...) == string::npos comparisons (for various string-like types) and suggests replacing with absl::StrContains. More...
class  TimeComparisonCheck
 Prefer comparison in the absl::Time domain instead of the numeric domain. More...
class  TimeSubtractionCheck
 Finds and fixes absl::Time subtraction expressions to do subtraction in the time domain instead of the numeric domain. More...
class  UpgradeDurationConversionsCheck
 Finds deprecated uses of absl::Duration arithmetic operators and factories. More...

Enumerations

enum class  DurationScale : std::uint8_t {
  Hours = 0 , Minutes , Seconds , Milliseconds ,
  Microseconds , Nanoseconds
}
 Duration factory and conversion scales. More...

Functions

static ClangTidyModuleRegistry::Add< AbseilModuleX ("abseil-module", "Add Abseil checks.")
static RewriteRuleWith< std::string > cleanupCtadCheckImpl ()
static bool insideMacroDefinition (const MatchFinder::MatchResult &Result, SourceRange Range)
static std::optional< DurationScalegetScaleForFactory (llvm::StringRef FactoryName)
static double getValue (const IntegerLiteral *IntLit, const FloatingLiteral *FloatLit)
static std::optional< std::tuple< DurationScale, double > > getNewScaleSingleStep (DurationScale OldScale, double Multiplier)
static std::optional< DurationScalegetNewScale (DurationScale OldScale, double Multiplier)
static std::optional< llvm::APSInt > truncateIfIntegral (const FloatingLiteral &FloatLiteral)
 Returns an integer if the fractional part of a FloatingLiteral is 0.
const std::pair< llvm::StringRef, llvm::StringRef > & getDurationInverseForScale (DurationScale Scale)
 Given a Scale return the fully qualified inverse functions for it.
static std::optional< std::string > rewriteInverseDurationCall (const MatchFinder::MatchResult &Result, DurationScale Scale, const Expr &Node)
 If Node is a call to the inverse of Scale, return that inverse's argument, otherwise std::nullopt.
static std::optional< std::string > rewriteInverseTimeCall (const MatchFinder::MatchResult &Result, DurationScale Scale, const Expr &Node)
 If Node is a call to the inverse of Scale, return that inverse's argument, otherwise std::nullopt.
llvm::StringRef getDurationFactoryForScale (DurationScale Scale)
 Returns the factory function name for a given Scale.
llvm::StringRef getTimeFactoryForScale (DurationScale Scale)
 Given a 'Scale', return the appropriate factory function call for constructing a Time for that scale.
llvm::StringRef getTimeInverseForScale (DurationScale Scale)
 Returns the Time factory function name for a given Scale.
bool isLiteralZero (const MatchFinder::MatchResult &Result, const Expr &Node)
 Returns true if Node is a value which evaluates to a literal 0.
std::optional< std::string > stripFloatCast (const ast_matchers::MatchFinder::MatchResult &Result, const Expr &Node)
 Possibly strip a floating point cast expression.
std::optional< std::string > stripFloatLiteralFraction (const MatchFinder::MatchResult &Result, const Expr &Node)
std::string simplifyDurationFactoryArg (const MatchFinder::MatchResult &Result, const Expr &Node)
std::optional< DurationScalegetScaleForDurationInverse (llvm::StringRef Name)
 Given the name of an inverse Duration function (e.g., ToDoubleSeconds), return its DurationScale, or std::nullopt if a match is not found.
std::optional< DurationScalegetScaleForTimeInverse (llvm::StringRef Name)
 Given the name of an inverse Time function (e.g., ToUnixSeconds), return its DurationScale, or std::nullopt if a match is not found.
std::string rewriteExprFromNumberToDuration (const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale, const Expr *Node)
 Assuming Node has type double or int representing a time interval of Scale, return the expression to make it a suitable Duration.
std::string rewriteExprFromNumberToTime (const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale, const Expr *Node)
 Assuming Node has a type int representing a time instant of Scale since The Epoch, return the expression to make it a suitable Time.
bool isInMacro (const MatchFinder::MatchResult &Result, const Expr *E)
bool isLiteralZero (const ast_matchers::MatchFinder::MatchResult &Result, const Expr &Node)
std::optional< std::string > stripFloatLiteralFraction (const ast_matchers::MatchFinder::MatchResult &Result, const Expr &Node)
 Possibly remove the fractional part of a floating point literal.
std::string simplifyDurationFactoryArg (const ast_matchers::MatchFinder::MatchResult &Result, const Expr &Node)
 Possibly further simplify a duration factory function's argument, without changing the scale of the factory function.
bool isInMacro (const ast_matchers::MatchFinder::MatchResult &Result, const Expr *E)
 Return false if E is a either: not a macro at all; or an argument to one.
 AST_MATCHER_FUNCTION (ast_matchers::internal::Matcher< FunctionDecl >, DurationConversionFunction)
 AST_MATCHER_FUNCTION (ast_matchers::internal::Matcher< FunctionDecl >, DurationFactoryFunction)
 AST_MATCHER_FUNCTION (ast_matchers::internal::Matcher< FunctionDecl >, TimeConversionFunction)
 AST_MATCHER_FUNCTION_P (ast_matchers::internal::Matcher< Stmt >, comparisonOperatorWithCallee, ast_matchers::internal::Matcher< Decl >, funcDecl)
static transformer::RewriteRuleWith< std::string > makeRewriteRule (ArrayRef< StringRef > StringLikeClassNames, StringRef AbseilStringsMatchHeader)
static bool insideMacroDefinition (const MatchFinder::MatchResult &Result, SourceRange Range)
static bool isConstructorAssignment (const MatchFinder::MatchResult &Result, const Expr *Node)
static bool isArgument (const MatchFinder::MatchResult &Result, const Expr *Node)
static bool isReturn (const MatchFinder::MatchResult &Result, const Expr *Node)
static bool parensRequired (const MatchFinder::MatchResult &Result, const Expr *Node)

Variables

const auto DefaultStringLikeClasses
static const char DefaultAbseilStringsMatchHeader [] = "absl/strings/match.h"

Enumeration Type Documentation

◆ DurationScale

enum class clang::tidy::abseil::DurationScale : std::uint8_t
strong

Duration factory and conversion scales.

Enumerator
Hours 
Minutes 
Seconds 
Milliseconds 
Microseconds 
Nanoseconds 

Definition at line 20 of file DurationRewriter.h.

Function Documentation

◆ AST_MATCHER_FUNCTION() [1/3]

clang::tidy::abseil::AST_MATCHER_FUNCTION ( ast_matchers::internal::Matcher< FunctionDecl > ,
DurationConversionFunction  )

Definition at line 98 of file DurationRewriter.h.

◆ AST_MATCHER_FUNCTION() [2/3]

clang::tidy::abseil::AST_MATCHER_FUNCTION ( ast_matchers::internal::Matcher< FunctionDecl > ,
DurationFactoryFunction  )

Definition at line 110 of file DurationRewriter.h.

◆ AST_MATCHER_FUNCTION() [3/3]

clang::tidy::abseil::AST_MATCHER_FUNCTION ( ast_matchers::internal::Matcher< FunctionDecl > ,
TimeConversionFunction  )

Definition at line 118 of file DurationRewriter.h.

◆ AST_MATCHER_FUNCTION_P()

clang::tidy::abseil::AST_MATCHER_FUNCTION_P ( ast_matchers::internal::Matcher< Stmt > ,
comparisonOperatorWithCallee ,
ast_matchers::internal::Matcher< Decl > ,
funcDecl  )

Definition at line 126 of file DurationRewriter.h.

◆ cleanupCtadCheckImpl()

RewriteRuleWith< std::string > clang::tidy::abseil::cleanupCtadCheckImpl ( )
static

◆ getDurationFactoryForScale()

llvm::StringRef clang::tidy::abseil::getDurationFactoryForScale ( DurationScale Scale)

Returns the factory function name for a given Scale.

Given a Scale, return the appropriate factory function call for constructing a Duration for that scale.

Definition at line 84 of file DurationRewriter.cpp.

Referenced by clang::tidy::abseil::DurationFactoryScaleCheck::check(), clang::tidy::abseil::TimeSubtractionCheck::check(), clang::tidy::abseil::TimeSubtractionCheck::registerMatchers(), and rewriteExprFromNumberToDuration().

◆ getDurationInverseForScale()

const std::pair< llvm::StringRef, llvm::StringRef > & clang::tidy::abseil::getDurationInverseForScale ( DurationScale Scale)

Given a Scale return the fully qualified inverse functions for it.

The first returned value is the inverse for double, and the second returned value is the inverse for int64.

Definition at line 34 of file DurationRewriter.cpp.

Referenced by clang::tidy::abseil::DurationConversionCastCheck::check(), clang::tidy::abseil::TimeSubtractionCheck::check(), and rewriteInverseDurationCall().

◆ getNewScale()

std::optional< DurationScale > clang::tidy::abseil::getNewScale ( DurationScale OldScale,
double Multiplier )
static

◆ getNewScaleSingleStep()

std::optional< std::tuple< DurationScale, double > > clang::tidy::abseil::getNewScaleSingleStep ( DurationScale OldScale,
double Multiplier )
static

Definition at line 50 of file DurationFactoryScaleCheck.cpp.

References Hours, Microseconds, Milliseconds, Minutes, Nanoseconds, and Seconds.

Referenced by getNewScale().

◆ getScaleForDurationInverse()

std::optional< DurationScale > clang::tidy::abseil::getScaleForDurationInverse ( llvm::StringRef Name)

Given the name of an inverse Duration function (e.g., ToDoubleSeconds), return its DurationScale, or std::nullopt if a match is not found.

Definition at line 184 of file DurationRewriter.cpp.

References Hours, Microseconds, Milliseconds, Minutes, Nanoseconds, and Seconds.

Referenced by clang::tidy::abseil::DurationComparisonCheck::check(), clang::tidy::abseil::DurationConversionCastCheck::check(), and clang::tidy::abseil::DurationSubtractionCheck::check().

◆ getScaleForFactory()

std::optional< DurationScale > clang::tidy::abseil::getScaleForFactory ( llvm::StringRef FactoryName)
static

◆ getScaleForTimeInverse()

std::optional< DurationScale > clang::tidy::abseil::getScaleForTimeInverse ( llvm::StringRef Name)

Given the name of an inverse Time function (e.g., ToUnixSeconds), return its DurationScale, or std::nullopt if a match is not found.

Definition at line 206 of file DurationRewriter.cpp.

References Hours, Microseconds, Milliseconds, Minutes, Nanoseconds, and Seconds.

Referenced by clang::tidy::abseil::DurationAdditionCheck::check(), clang::tidy::abseil::TimeComparisonCheck::check(), clang::tidy::abseil::TimeSubtractionCheck::check(), and clang::tidy::abseil::TimeSubtractionCheck::registerMatchers().

◆ getTimeFactoryForScale()

llvm::StringRef clang::tidy::abseil::getTimeFactoryForScale ( DurationScale Scale)

Given a 'Scale', return the appropriate factory function call for constructing a Time for that scale.

Definition at line 93 of file DurationRewriter.cpp.

Referenced by rewriteExprFromNumberToTime().

◆ getTimeInverseForScale()

llvm::StringRef clang::tidy::abseil::getTimeInverseForScale ( DurationScale Scale)

Returns the Time factory function name for a given Scale.

Returns the Time inverse function name for a given Scale.

Definition at line 103 of file DurationRewriter.cpp.

Referenced by clang::tidy::abseil::DurationAdditionCheck::check(), and rewriteInverseTimeCall().

◆ getValue()

double clang::tidy::abseil::getValue ( const IntegerLiteral * IntLit,
const FloatingLiteral * FloatLit )
static

◆ insideMacroDefinition() [1/2]

bool clang::tidy::abseil::insideMacroDefinition ( const MatchFinder::MatchResult & Result,
SourceRange Range )
static

◆ insideMacroDefinition() [2/2]

bool clang::tidy::abseil::insideMacroDefinition ( const MatchFinder::MatchResult & Result,
SourceRange Range )
static

Definition at line 21 of file TimeSubtractionCheck.cpp.

◆ isArgument()

bool clang::tidy::abseil::isArgument ( const MatchFinder::MatchResult & Result,
const Expr * Node )
static

Definition at line 46 of file TimeSubtractionCheck.cpp.

Referenced by parensRequired().

◆ isConstructorAssignment()

bool clang::tidy::abseil::isConstructorAssignment ( const MatchFinder::MatchResult & Result,
const Expr * Node )
static

Definition at line 29 of file TimeSubtractionCheck.cpp.

Referenced by parensRequired().

◆ isInMacro() [1/2]

bool clang::tidy::abseil::isInMacro ( const ast_matchers::MatchFinder::MatchResult & Result,
const Expr * E )

Return false if E is a either: not a macro at all; or an argument to one.

In the both cases, we often want to do the transformation.

◆ isInMacro() [2/2]

◆ isLiteralZero() [1/2]

bool clang::tidy::abseil::isLiteralZero ( const ast_matchers::MatchFinder::MatchResult & Result,
const Expr & Node )

◆ isLiteralZero() [2/2]

bool clang::tidy::abseil::isLiteralZero ( const MatchFinder::MatchResult & Result,
const Expr & Node )

Returns true if Node is a value which evaluates to a literal 0.

Definition at line 113 of file DurationRewriter.cpp.

Referenced by clang::tidy::abseil::DurationFactoryScaleCheck::check(), rewriteExprFromNumberToDuration(), and rewriteExprFromNumberToTime().

◆ isReturn()

bool clang::tidy::abseil::isReturn ( const MatchFinder::MatchResult & Result,
const Expr * Node )
static

Definition at line 65 of file TimeSubtractionCheck.cpp.

Referenced by parensRequired().

◆ makeRewriteRule()

transformer::RewriteRuleWith< std::string > clang::tidy::abseil::makeRewriteRule ( ArrayRef< StringRef > StringLikeClassNames,
StringRef AbseilStringsMatchHeader )
static

◆ parensRequired()

bool clang::tidy::abseil::parensRequired ( const MatchFinder::MatchResult & Result,
const Expr * Node )
static

◆ rewriteExprFromNumberToDuration()

std::string clang::tidy::abseil::rewriteExprFromNumberToDuration ( const ast_matchers::MatchFinder::MatchResult & Result,
DurationScale Scale,
const Expr * Node )

Assuming Node has type double or int representing a time interval of Scale, return the expression to make it a suitable Duration.

Definition at line 222 of file DurationRewriter.cpp.

References getDurationFactoryForScale(), isLiteralZero(), rewriteInverseDurationCall(), and simplifyDurationFactoryArg().

Referenced by clang::tidy::abseil::DurationAdditionCheck::check(), clang::tidy::abseil::DurationComparisonCheck::check(), and clang::tidy::abseil::DurationSubtractionCheck::check().

◆ rewriteExprFromNumberToTime()

std::string clang::tidy::abseil::rewriteExprFromNumberToTime ( const ast_matchers::MatchFinder::MatchResult & Result,
DurationScale Scale,
const Expr * Node )

Assuming Node has a type int representing a time instant of Scale since The Epoch, return the expression to make it a suitable Time.

Definition at line 240 of file DurationRewriter.cpp.

References getTimeFactoryForScale(), isLiteralZero(), and rewriteInverseTimeCall().

Referenced by clang::tidy::abseil::TimeComparisonCheck::check(), and clang::tidy::abseil::TimeSubtractionCheck::check().

◆ rewriteInverseDurationCall()

std::optional< std::string > clang::tidy::abseil::rewriteInverseDurationCall ( const MatchFinder::MatchResult & Result,
DurationScale Scale,
const Expr & Node )
static

If Node is a call to the inverse of Scale, return that inverse's argument, otherwise std::nullopt.

Definition at line 51 of file DurationRewriter.cpp.

References getDurationInverseForScale().

Referenced by rewriteExprFromNumberToDuration().

◆ rewriteInverseTimeCall()

std::optional< std::string > clang::tidy::abseil::rewriteInverseTimeCall ( const MatchFinder::MatchResult & Result,
DurationScale Scale,
const Expr & Node )
static

If Node is a call to the inverse of Scale, return that inverse's argument, otherwise std::nullopt.

Definition at line 70 of file DurationRewriter.cpp.

References getTimeInverseForScale().

Referenced by rewriteExprFromNumberToTime().

◆ simplifyDurationFactoryArg() [1/2]

std::string clang::tidy::abseil::simplifyDurationFactoryArg ( const ast_matchers::MatchFinder::MatchResult & Result,
const Expr & Node )

Possibly further simplify a duration factory function's argument, without changing the scale of the factory function.

Return that simplification or the text of the argument if no simplification is possible.

◆ simplifyDurationFactoryArg() [2/2]

std::string clang::tidy::abseil::simplifyDurationFactoryArg ( const MatchFinder::MatchResult & Result,
const Expr & Node )

Definition at line 169 of file DurationRewriter.cpp.

References stripFloatCast(), and stripFloatLiteralFraction().

Referenced by rewriteExprFromNumberToDuration().

◆ stripFloatCast()

std::optional< std::string > clang::tidy::abseil::stripFloatCast ( const ast_matchers::MatchFinder::MatchResult & Result,
const Expr & Node )

Possibly strip a floating point cast expression.

If Node represents an explicit cast to a floating point type, return the textual context of the cast argument, otherwise std::nullopt.

Definition at line 139 of file DurationRewriter.cpp.

Referenced by clang::tidy::abseil::DurationFactoryFloatCheck::check(), and simplifyDurationFactoryArg().

◆ stripFloatLiteralFraction() [1/2]

std::optional< std::string > clang::tidy::abseil::stripFloatLiteralFraction ( const ast_matchers::MatchFinder::MatchResult & Result,
const Expr & Node )

Possibly remove the fractional part of a floating point literal.

If Node represents a floating point literal with a zero fractional part, return the textual context of the integral part, otherwise std::nullopt.

◆ stripFloatLiteralFraction() [2/2]

std::optional< std::string > clang::tidy::abseil::stripFloatLiteralFraction ( const MatchFinder::MatchResult & Result,
const Expr & Node )

◆ truncateIfIntegral()

std::optional< llvm::APSInt > clang::tidy::abseil::truncateIfIntegral ( const FloatingLiteral & FloatLiteral)
static

Returns an integer if the fractional part of a FloatingLiteral is 0.

Definition at line 22 of file DurationRewriter.cpp.

Referenced by stripFloatLiteralFraction().

◆ X()

ClangTidyModuleRegistry::Add< AbseilModule > clang::tidy::abseil::X ( "abseil-module" ,
"Add Abseil checks."  )
static

Variable Documentation

◆ DefaultAbseilStringsMatchHeader

const char clang::tidy::abseil::DefaultAbseilStringsMatchHeader[] = "absl/strings/match.h"
static

◆ DefaultStringLikeClasses

const char clang::tidy::abseil::DefaultStringLikeClasses