Skip to content

[clang-tidy][NFC] Prefer constexpr llvm::StringLiteral over const char * #147301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions clang-tools-extra/clang-tidy/ClangTidy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ namespace clang::tidy {

namespace {
#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
static const char *AnalyzerCheckNamePrefix = "clang-analyzer-";
static constexpr llvm::StringLiteral AnalyzerCheckNamePrefix =
"clang-analyzer-";

class AnalyzerDiagnosticConsumer : public ento::PathDiagnosticConsumer {
public:
Expand Down Expand Up @@ -351,10 +352,9 @@ ClangTidyASTConsumerFactory::ClangTidyASTConsumerFactory(
static void
setStaticAnalyzerCheckerOpts(const ClangTidyOptions &Opts,
clang::AnalyzerOptions &AnalyzerOptions) {
StringRef AnalyzerPrefix(AnalyzerCheckNamePrefix);
for (const auto &Opt : Opts.CheckOptions) {
StringRef OptName(Opt.getKey());
if (!OptName.consume_front(AnalyzerPrefix))
if (!OptName.consume_front(AnalyzerCheckNamePrefix))
continue;
// Analyzer options are always local options so we can ignore priority.
AnalyzerOptions.Config[OptName] = Opt.getValue().Value;
Expand Down Expand Up @@ -476,7 +476,8 @@ std::vector<std::string> ClangTidyASTConsumerFactory::getCheckNames() {
#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
for (const auto &AnalyzerCheck : getAnalyzerCheckersAndPackages(
Context, Context.canEnableAnalyzerAlphaCheckers()))
CheckNames.push_back(AnalyzerCheckNamePrefix + AnalyzerCheck.first);
CheckNames.emplace_back(
(AnalyzerCheckNamePrefix + AnalyzerCheck.first).str());
#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER

llvm::sort(CheckNames);
Expand Down
4 changes: 2 additions & 2 deletions clang-tools-extra/clang-tidy/abseil/AbseilMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ AST_POLYMORPHIC_MATCHER(
if (PrefixPosition == StringRef::npos)
return false;
Path = Path.drop_front(PrefixPosition + AbslPrefix.size());
static const char *AbseilLibraries[] = {
static constexpr llvm::StringLiteral AbseilLibraries[] = {
"algorithm", "base", "container", "debugging", "flags",
"hash", "iterator", "memory", "meta", "numeric",
"profiling", "random", "status", "strings", "synchronization",
"time", "types", "utility"};
return llvm::any_of(AbseilLibraries, [&](const char *Library) {
return llvm::any_of(AbseilLibraries, [&](llvm::StringLiteral Library) {
return Path.starts_with(Library);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace {

AST_MATCHER(CXXMethodDecl, isVirtual) { return Node.isVirtual(); }

static const char *const ErrorMsg =
static constexpr llvm::StringLiteral ErrorMsg =
"comparing a pointer to member virtual function with other pointer is "
"unspecified behavior, only compare it with a null-pointer constant for "
"equality.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ static bool isEmpty(ASTContext &Context, const QualType &Type) {
return isIncompleteOrZeroLengthArrayType(Context, Type);
}

static const char *getInitializer(QualType QT, bool UseAssignment) {
const char *DefaultInitializer = "{}";
static llvm::StringLiteral getInitializer(QualType QT, bool UseAssignment) {
static constexpr llvm::StringLiteral DefaultInitializer = "{}";
if (!UseAssignment)
return DefaultInitializer;

Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ void UseUsingCheck::check(const MatchFinder::MatchResult &Result) {
if (StartLoc.isMacroID() && IgnoreMacros)
return;

static const char *UseUsingWarning = "use 'using' instead of 'typedef'";
static constexpr llvm::StringLiteral UseUsingWarning =
"use 'using' instead of 'typedef'";

// Warn at StartLoc but do not fix if there is macro or array.
if (MatchedDecl->getUnderlyingType()->isArrayType() || StartLoc.isMacroID()) {
Expand Down