[clang-tidy][NFC] Replace llvm::StringLiteral with StringRef (#172765)

`llvm::StringLiteral` isn't necessary now that `StringRef` is
constexpr-constructible, so keeping it around just creates confusion.
This commit is contained in:
Victor Chernyakin 2025-12-17 21:15:41 -07:00 committed by GitHub
parent 686c2a1476
commit b2ce1a981e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
24 changed files with 130 additions and 144 deletions

View File

@ -63,8 +63,7 @@ void (*RegisterCustomChecks)(const ClangTidyOptions &O,
namespace {
#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
#define ANALYZER_CHECK_NAME_PREFIX "clang-analyzer-"
static constexpr llvm::StringLiteral AnalyzerCheckNamePrefix =
ANALYZER_CHECK_NAME_PREFIX;
static constexpr StringRef AnalyzerCheckNamePrefix = ANALYZER_CHECK_NAME_PREFIX;
class AnalyzerDiagnosticConsumer : public ento::PathDiagnosticConsumer {
public:
@ -734,7 +733,7 @@ ChecksAndOptions getAllChecksAndOptions(bool AllowEnablingAnalyzerAlphaCheckers,
Result.Checks.insert(Buffer);
}
static constexpr llvm::StringLiteral OptionNames[] = {
static constexpr StringRef OptionNames[] = {
#define GET_CHECKER_OPTIONS
#define CHECKER_OPTION(TYPE, CHECKER, OPTION_NAME, DESCRIPTION, DEFAULT, \
RELEASE, HIDDEN) \

View File

@ -187,7 +187,7 @@ ClangTidyCheck::OptionsView::getEnumInt(StringRef LocalName,
return std::nullopt;
}
static constexpr llvm::StringLiteral ConfigWarning(
static constexpr StringRef ConfigWarning(
"invalid configuration value '%0' for option '%1'%select{|; expected a "
"bool|; expected an integer|; did you mean '%3'?}2");

View File

@ -108,7 +108,7 @@ private:
// Consume the entire buffer and return all `NoLintToken`s that were found.
static SmallVector<NoLintToken> getNoLints(StringRef Buffer) {
static constexpr llvm::StringLiteral NOLINT = "NOLINT";
static constexpr StringRef NOLINT = "NOLINT";
SmallVector<NoLintToken> NoLints;
size_t Pos = 0;

View File

@ -44,17 +44,17 @@ AST_POLYMORPHIC_MATCHER(
// Determine whether filepath contains "absl/[absl-library]" substring, where
// [absl-library] is AbseilLibraries list entry.
StringRef Path = FileEntry->getName();
static constexpr llvm::StringLiteral AbslPrefix("absl/");
static constexpr StringRef AbslPrefix("absl/");
const size_t PrefixPosition = Path.find(AbslPrefix);
if (PrefixPosition == StringRef::npos)
return false;
Path = Path.drop_front(PrefixPosition + AbslPrefix.size());
static constexpr llvm::StringLiteral AbseilLibraries[] = {
static constexpr StringRef AbseilLibraries[] = {
"algorithm", "base", "container", "debugging", "flags",
"hash", "iterator", "memory", "meta", "numeric",
"profiling", "random", "status", "strings", "synchronization",
"time", "types", "utility"};
return llvm::any_of(AbseilLibraries, [&](llvm::StringLiteral Library) {
return llvm::any_of(AbseilLibraries, [&](StringRef Library) {
return Path.starts_with(Library);
});
}

View File

@ -21,7 +21,7 @@ using ast_matchers::MatchFinder;
using dataflow::statusor_model::UncheckedStatusOrAccessDiagnoser;
using dataflow::statusor_model::UncheckedStatusOrAccessModel;
static constexpr llvm::StringLiteral FuncID("fun");
static constexpr StringRef FuncID = "fun";
void UncheckedStatusOrAccessCheck::registerMatchers(MatchFinder *Finder) {
using namespace ast_matchers;

View File

@ -25,7 +25,7 @@ namespace {
AST_MATCHER(CXXMethodDecl, isVirtual) { return Node.isVirtual(); }
static constexpr llvm::StringLiteral ErrorMsg =
static constexpr StringRef ErrorMsg =
"comparing a pointer to member virtual function with other pointer is "
"unspecified behavior, only compare it with a null-pointer constant for "
"equality.";

View File

@ -14,9 +14,9 @@ using namespace clang::ast_matchers;
namespace clang::tidy::bugprone {
static constexpr llvm::StringLiteral SourceDeclName = "ChangedPVD";
static constexpr llvm::StringLiteral MutatingOperatorName = "MutatingOp";
static constexpr llvm::StringLiteral MutatingCallName = "MutatingCall";
static constexpr StringRef SourceDeclName = "ChangedPVD";
static constexpr StringRef MutatingOperatorName = "MutatingOp";
static constexpr StringRef MutatingCallName = "MutatingCall";
void CopyConstructorMutatesArgumentCheck::registerMatchers(
MatchFinder *Finder) {

View File

@ -18,30 +18,34 @@
#include "llvm/Support/Debug.h"
#include <optional>
using namespace clang::ast_matchers;
namespace clang::tidy::bugprone {
namespace optutils = clang::tidy::utils::options;
/// The default value for the MinimumLength check option.
static constexpr std::size_t DefaultMinimumLength = 2;
/// The default value for ignored parameter names.
static constexpr llvm::StringLiteral DefaultIgnoredParameterNames = "\"\";"
"iterator;"
"Iterator;"
"begin;"
"Begin;"
"end;"
"End;"
"first;"
"First;"
"last;"
"Last;"
"lhs;"
"LHS;"
"rhs;"
"RHS";
static constexpr StringRef DefaultIgnoredParameterNames = "\"\";"
"iterator;"
"Iterator;"
"begin;"
"Begin;"
"end;"
"End;"
"first;"
"First;"
"last;"
"Last;"
"lhs;"
"LHS;"
"rhs;"
"RHS";
/// The default value for ignored parameter type suffixes.
static constexpr llvm::StringLiteral DefaultIgnoredParameterTypeSuffixes =
static constexpr StringRef DefaultIgnoredParameterTypeSuffixes =
"bool;"
"Bool;"
"_Bool;"
@ -87,10 +91,6 @@ static constexpr bool DefaultSuppressParametersUsedTogether = true;
static constexpr std::size_t
DefaultNamePrefixSuffixSilenceDissimilarityTreshold = 1;
using namespace clang::ast_matchers;
namespace clang::tidy::bugprone {
using TheCheck = EasilySwappableParametersCheck;
namespace filter {

View File

@ -19,18 +19,18 @@ using namespace clang::ast_matchers;
namespace clang::tidy::bugprone {
constexpr llvm::StringLiteral FunctionExprName = "FunctionExpr";
constexpr llvm::StringLiteral CastExprName = "CastExpr";
constexpr llvm::StringLiteral UnknownDestName = "UnknownDest";
constexpr llvm::StringLiteral DestArrayTyName = "DestArrayTy";
constexpr llvm::StringLiteral DestVarDeclName = "DestVarDecl";
constexpr llvm::StringLiteral DestMallocExprName = "DestMalloc";
constexpr llvm::StringLiteral DestExprName = "DestExpr";
constexpr llvm::StringLiteral SrcVarDeclName = "SrcVarDecl";
constexpr llvm::StringLiteral SrcExprName = "SrcExpr";
constexpr llvm::StringLiteral LengthExprName = "LengthExpr";
constexpr llvm::StringLiteral WrongLengthExprName = "WrongLength";
constexpr llvm::StringLiteral UnknownLengthName = "UnknownLength";
constexpr StringRef FunctionExprName = "FunctionExpr";
constexpr StringRef CastExprName = "CastExpr";
constexpr StringRef UnknownDestName = "UnknownDest";
constexpr StringRef DestArrayTyName = "DestArrayTy";
constexpr StringRef DestVarDeclName = "DestVarDecl";
constexpr StringRef DestMallocExprName = "DestMalloc";
constexpr StringRef DestExprName = "DestExpr";
constexpr StringRef SrcVarDeclName = "SrcVarDecl";
constexpr StringRef SrcExprName = "SrcExpr";
constexpr StringRef LengthExprName = "LengthExpr";
constexpr StringRef WrongLengthExprName = "WrongLength";
constexpr StringRef UnknownLengthName = "UnknownLength";
enum class LengthHandleKind { Increase, Decrease };

View File

@ -11,10 +11,14 @@
#include "llvm/ADT/DepthFirstIterator.h"
#include "llvm/ADT/STLExtras.h"
using namespace clang::ast_matchers;
namespace clang::tidy {
// This is the minimal set of safe functions.
// https://wiki.sei.cmu.edu/confluence/display/c/SIG30-C.+Call+only+asynchronous-safe+functions+within+signal+handlers
constexpr llvm::StringLiteral MinimalConformingFunctions[] = {
"signal", "abort", "_Exit", "quick_exit"};
constexpr StringRef MinimalConformingFunctions[] = {"signal", "abort", "_Exit",
"quick_exit"};
// The POSIX-defined set of safe functions.
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_04_03
@ -25,7 +29,7 @@ constexpr llvm::StringLiteral MinimalConformingFunctions[] = {
// The list is repeated in bugprone-signal-handler.rst and should be kept up to
// date.
// clang-format off
constexpr llvm::StringLiteral POSIXConformingFunctions[] = {
constexpr StringRef POSIXConformingFunctions[] = {
"_Exit",
"_exit",
"abort",
@ -221,10 +225,6 @@ constexpr llvm::StringLiteral POSIXConformingFunctions[] = {
};
// clang-format on
using namespace clang::ast_matchers;
namespace clang::tidy {
template <>
struct OptionEnumMapping<
bugprone::SignalHandlerCheck::AsyncSafeFunctionSetKind> {

View File

@ -16,24 +16,22 @@ using namespace clang::ast_matchers;
namespace clang::tidy::bugprone {
static constexpr llvm::StringLiteral StrictModeOptionName = "StrictMode";
static constexpr llvm::StringLiteral EnableCountingEnumHeuristicOptionName =
static constexpr StringRef StrictModeOptionName = "StrictMode";
static constexpr StringRef EnableCountingEnumHeuristicOptionName =
"EnableCountingEnumHeuristic";
static constexpr llvm::StringLiteral CountingEnumPrefixesOptionName =
static constexpr StringRef CountingEnumPrefixesOptionName =
"CountingEnumPrefixes";
static constexpr llvm::StringLiteral CountingEnumSuffixesOptionName =
static constexpr StringRef CountingEnumSuffixesOptionName =
"CountingEnumSuffixes";
static constexpr bool StrictModeOptionDefaultValue = false;
static constexpr bool EnableCountingEnumHeuristicOptionDefaultValue = true;
static constexpr llvm::StringLiteral CountingEnumPrefixesOptionDefaultValue =
"";
static constexpr llvm::StringLiteral CountingEnumSuffixesOptionDefaultValue =
"count";
static constexpr StringRef CountingEnumPrefixesOptionDefaultValue = "";
static constexpr StringRef CountingEnumSuffixesOptionDefaultValue = "count";
static constexpr llvm::StringLiteral RootMatchBindName = "root";
static constexpr llvm::StringLiteral UnionMatchBindName = "union";
static constexpr llvm::StringLiteral TagMatchBindName = "tags";
static constexpr StringRef RootMatchBindName = "root";
static constexpr StringRef UnionMatchBindName = "union";
static constexpr StringRef TagMatchBindName = "tags";
namespace {

View File

@ -14,16 +14,11 @@ using namespace clang::ast_matchers;
namespace clang::tidy::bugprone {
static constexpr llvm::StringLiteral LoopName =
llvm::StringLiteral("forLoopName");
static constexpr llvm::StringLiteral LoopVarName =
llvm::StringLiteral("loopVar");
static constexpr llvm::StringLiteral LoopVarCastName =
llvm::StringLiteral("loopVarCast");
static constexpr llvm::StringLiteral LoopUpperBoundName =
llvm::StringLiteral("loopUpperBound");
static constexpr llvm::StringLiteral LoopIncrementName =
llvm::StringLiteral("loopIncrement");
static constexpr StringRef LoopName = "forLoopName";
static constexpr StringRef LoopVarName = "loopVar";
static constexpr StringRef LoopVarCastName = "loopVarCast";
static constexpr StringRef LoopUpperBoundName = "loopUpperBound";
static constexpr StringRef LoopIncrementName = "loopIncrement";
namespace {

View File

@ -22,7 +22,7 @@ using dataflow::UncheckedOptionalAccessDiagnoser;
using dataflow::UncheckedOptionalAccessDiagnostic;
using dataflow::UncheckedOptionalAccessModel;
static constexpr llvm::StringLiteral FuncID("fun");
static constexpr StringRef FuncID = "fun";
void UncheckedOptionalAccessCheck::registerMatchers(MatchFinder *Finder) {
using namespace ast_matchers;

View File

@ -19,21 +19,19 @@ using namespace llvm;
namespace clang::tidy::bugprone {
static constexpr llvm::StringLiteral OptionNameCustomFunctions =
"CustomFunctions";
static constexpr llvm::StringLiteral OptionNameReportDefaultFunctions =
static constexpr StringRef OptionNameCustomFunctions = "CustomFunctions";
static constexpr StringRef OptionNameReportDefaultFunctions =
"ReportDefaultFunctions";
static constexpr llvm::StringLiteral OptionNameReportMoreUnsafeFunctions =
static constexpr StringRef OptionNameReportMoreUnsafeFunctions =
"ReportMoreUnsafeFunctions";
static constexpr llvm::StringLiteral FunctionNamesWithAnnexKReplacementId =
static constexpr StringRef FunctionNamesWithAnnexKReplacementId =
"FunctionNamesWithAnnexKReplacement";
static constexpr llvm::StringLiteral FunctionNamesId = "FunctionsNames";
static constexpr llvm::StringLiteral AdditionalFunctionNamesId =
static constexpr StringRef FunctionNamesId = "FunctionsNames";
static constexpr StringRef AdditionalFunctionNamesId =
"AdditionalFunctionsNames";
static constexpr llvm::StringLiteral CustomFunctionNamesId =
"CustomFunctionNames";
static constexpr llvm::StringLiteral DeclRefId = "DRE";
static constexpr StringRef CustomFunctionNamesId = "CustomFunctionNames";
static constexpr StringRef DeclRefId = "DRE";
static std::optional<std::string>
getAnnexKReplacementFor(StringRef FunctionName) {

View File

@ -383,8 +383,8 @@ static bool isEmpty(const ASTContext &Context, const QualType &Type) {
return isIncompleteOrZeroLengthArrayType(Context, Type);
}
static llvm::StringLiteral getInitializer(QualType QT, bool UseAssignment) {
static constexpr llvm::StringLiteral DefaultInitializer = "{}";
static StringRef getInitializer(QualType QT, bool UseAssignment) {
static constexpr StringRef DefaultInitializer = "{}";
if (!UseAssignment)
return DefaultInitializer;

View File

@ -17,7 +17,7 @@
namespace clang::tidy::google::readability {
constexpr llvm::StringLiteral KDisabledTestPrefix = "DISABLED_";
constexpr StringRef KDisabledTestPrefix = "DISABLED_";
// Determines whether the macro is a Googletest test macro.
static bool isGoogletestTestMacro(StringRef MacroName) {

View File

@ -31,7 +31,7 @@ using namespace clang::tidy::matchers;
namespace clang::tidy::misc {
using llvm::APSInt;
static constexpr llvm::StringLiteral KnownBannedMacroNames[] = {
static constexpr StringRef KnownBannedMacroNames[] = {
"EAGAIN",
"EWOULDBLOCK",
"SIGCLD",
@ -458,7 +458,7 @@ AST_MATCHER(ConditionalOperator, conditionalOperatorIsInMacro) {
AST_MATCHER(Expr, isMacro) { return Node.getExprLoc().isMacroID(); }
AST_MATCHER_P(Expr, expandedByMacro, ArrayRef<llvm::StringLiteral>, Names) {
AST_MATCHER_P(Expr, expandedByMacro, ArrayRef<StringRef>, Names) {
const SourceManager &SM = Finder->getASTContext().getSourceManager();
const LangOptions &LO = Finder->getASTContext().getLangOpts();
SourceLocation Loc = Node.getExprLoc();

View File

@ -140,9 +140,9 @@ AST_MATCHER(LambdaExpr, hasExplicitResultType) {
} // namespace
constexpr llvm::StringLiteral ErrorMessageOnFunction =
constexpr StringRef ErrorMessageOnFunction =
"use a trailing return type for this function";
constexpr llvm::StringLiteral ErrorMessageOnLambda =
constexpr StringRef ErrorMessageOnLambda =
"use a trailing return type for this lambda";
static SourceLocation expandIfMacroId(SourceLocation Loc,

View File

@ -26,11 +26,11 @@ AST_MATCHER(clang::LinkageSpecDecl, isExternCLinkage) {
namespace clang::tidy::modernize {
static constexpr llvm::StringLiteral ExternCDeclName = "extern-c-decl";
static constexpr llvm::StringLiteral ParentDeclName = "parent-decl";
static constexpr llvm::StringLiteral TagDeclName = "tag-decl";
static constexpr llvm::StringLiteral TypedefName = "typedef";
static constexpr llvm::StringLiteral DeclStmtName = "decl-stmt";
static constexpr StringRef ExternCDeclName = "extern-c-decl";
static constexpr StringRef ParentDeclName = "parent-decl";
static constexpr StringRef TagDeclName = "tag-decl";
static constexpr StringRef TypedefName = "typedef";
static constexpr StringRef DeclStmtName = "decl-stmt";
UseUsingCheck::UseUsingCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
@ -119,7 +119,7 @@ void UseUsingCheck::check(const MatchFinder::MatchResult &Result) {
if (StartLoc.isMacroID() && IgnoreMacros)
return;
static constexpr llvm::StringLiteral UseUsingWarning =
static constexpr StringRef UseUsingWarning =
"use 'using' instead of 'typedef'";
// Warn at StartLoc but do not fix if there is macro or array.

View File

@ -17,11 +17,10 @@ using namespace clang::ast_matchers;
namespace clang::tidy::readability {
constexpr llvm::StringLiteral ContainerExprName = "container-expr";
constexpr llvm::StringLiteral DerefContainerExprName = "deref-container-expr";
constexpr llvm::StringLiteral AddrOfContainerExprName =
"addr-of-container-expr";
constexpr llvm::StringLiteral AddressOfName = "address-of";
constexpr StringRef ContainerExprName = "container-expr";
constexpr StringRef DerefContainerExprName = "deref-container-expr";
constexpr StringRef AddrOfContainerExprName = "addr-of-container-expr";
constexpr StringRef AddressOfName = "address-of";
void ContainerDataPointerCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {

View File

@ -91,37 +91,37 @@ struct AllHeuristicsBoundsWellConfigured {
static_assert(AllHeuristicsBoundsWellConfigured::Value);
} // namespace
static constexpr llvm::StringLiteral DefaultAbbreviations = "addr=address;"
"arr=array;"
"attr=attribute;"
"buf=buffer;"
"cl=client;"
"cnt=count;"
"col=column;"
"cpy=copy;"
"dest=destination;"
"dist=distance"
"dst=distance;"
"elem=element;"
"hght=height;"
"i=index;"
"idx=index;"
"len=length;"
"ln=line;"
"lst=list;"
"nr=number;"
"num=number;"
"pos=position;"
"ptr=pointer;"
"ref=reference;"
"src=source;"
"srv=server;"
"stmt=statement;"
"str=string;"
"val=value;"
"var=variable;"
"vec=vector;"
"wdth=width";
static constexpr StringRef DefaultAbbreviations = "addr=address;"
"arr=array;"
"attr=attribute;"
"buf=buffer;"
"cl=client;"
"cnt=count;"
"col=column;"
"cpy=copy;"
"dest=destination;"
"dist=distance"
"dst=distance;"
"elem=element;"
"hght=height;"
"i=index;"
"idx=index;"
"len=length;"
"ln=line;"
"lst=list;"
"nr=number;"
"num=number;"
"pos=position;"
"ptr=pointer;"
"ref=reference;"
"src=source;"
"srv=server;"
"stmt=statement;"
"str=string;"
"val=value;"
"var=variable;"
"vec=vector;"
"wdth=width";
static constexpr std::size_t SmallVectorSize =
SuspiciousCallArgumentCheck::SmallVectorSize;

View File

@ -22,31 +22,28 @@ namespace {
struct IntegerLiteralCheck {
using type = clang::IntegerLiteral;
static constexpr llvm::StringLiteral Name = llvm::StringLiteral("integer");
static constexpr StringRef Name = "integer";
// What should be skipped before looking for the Suffixes? (Nothing here.)
static constexpr llvm::StringLiteral SkipFirst = llvm::StringLiteral("");
static constexpr StringRef SkipFirst = "";
// Suffix can only consist of 'u', 'l', and 'z' chars, can be a bit-precise
// integer (wb), and can be a complex number ('i', 'j'). In MS compatibility
// mode, suffixes like i32 are supported.
static constexpr llvm::StringLiteral Suffixes =
llvm::StringLiteral("uUlLzZwWiIjJ");
static constexpr StringRef Suffixes = "uUlLzZwWiIjJ";
};
struct FloatingLiteralCheck {
using type = clang::FloatingLiteral;
static constexpr llvm::StringLiteral Name =
llvm::StringLiteral("floating point");
static constexpr StringRef Name = "floating point";
// C++17 introduced hexadecimal floating-point literals, and 'f' is both a
// valid hexadecimal digit in a hex float literal and a valid floating-point
// literal suffix.
// So we can't just "skip to the chars that can be in the suffix".
// Since the exponent ('p'/'P') is mandatory for hexadecimal floating-point
// literals, we first skip everything before the exponent.
static constexpr llvm::StringLiteral SkipFirst = llvm::StringLiteral("pP");
static constexpr StringRef SkipFirst = "pP";
// Suffix can only consist of 'f', 'l', "f16", "bf16", "df", "dd", "dl",
// 'h', 'q' chars, and can be a complex number ('i', 'j').
static constexpr llvm::StringLiteral Suffixes =
llvm::StringLiteral("fFlLbBdDhHqQiIjJ");
static constexpr StringRef Suffixes = "fFlLbBdDhHqQiIjJ";
};
struct NewSuffix {

View File

@ -36,7 +36,7 @@ public:
private:
void impl(SourceLocation DirectiveLoc, SourceRange ConditionRange,
const std::array<llvm::StringLiteral, 2> &Replacements) {
const std::array<StringRef, 2> &Replacements) {
// Lexer requires its input range to be null-terminated.
SmallString<128> Condition =
Lexer::getSourceText(CharSourceRange::getTokenRange(ConditionRange),

View File

@ -40,7 +40,7 @@ runHeaderGuardCheck(StringRef Code, const Twine &Filename,
static std::string
runIncludeOrderCheck(StringRef Code, const Twine &Filename,
std::optional<StringRef> ExpectedWarning,
llvm::ArrayRef<llvm::StringLiteral> Includes) {
llvm::ArrayRef<StringRef> Includes) {
std::map<StringRef, StringRef> PathsToContent;
for (auto Include : Includes)
PathsToContent.emplace(Include, "");