10#include "../utils/Matchers.h"
11#include "../utils/OptionsUtils.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "clang/Lex/Token.h"
31 "declaration uses identifier '%0', which is %select{a reserved "
32 "identifier|not a reserved identifier|reserved in the global namespace}1";
45 Invert(Options.get(
"Invert", false)),
46 AllowedIdentifiers(utils::options::parseStringList(
47 Options.get(
"AllowedIdentifiers",
""))) {}
58 std::unique_copy(Str.begin(), Str.end(), std::back_inserter(Result),
59 [
C](
char A,
char B) { return A == C && B == C; });
64 const LangOptions &LangOpts) {
65 if (LangOpts.CPlusPlus)
66 return Name.contains(
"__");
67 return Name.startswith(
"__");
70static std::optional<std::string>
78 return Name.size() >= 2 &&
Name[0] ==
'_' && std::isupper(
Name[1]);
83 return std::string(
Name.drop_front(1));
88 bool IsInGlobalNamespace) {
89 return IsInGlobalNamespace &&
Name.size() >= 1 &&
Name[0] ==
'_';
92static std::optional<std::string>
95 return std::string(
Name.drop_front(1));
100 assert(!
Name.empty());
101 if (
Name[0] ==
'_' || std::isupper(
Name[0]))
108static std::optional<RenamerClangTidyCheck::FailureInfo>
110 const LangOptions &LangOpts,
bool Invert,
111 ArrayRef<StringRef> AllowedIdentifiers) {
112 assert(!
Name.empty());
113 if (llvm::is_contained(AllowedIdentifiers,
Name))
122 std::optional<FailureInfo>
Info;
123 auto AppendFailure = [&](StringRef
Kind, std::string &&Fixup) {
125 Info = FailureInfo{std::string(
Kind), std::move(Fixup)};
128 Info->Fixup = std::move(Fixup);
131 auto InProgressFixup = [&] {
132 return llvm::transformOptional(
134 [](
const FailureInfo &
Info) {
return StringRef(
Info.Fixup); })
142 IsInGlobalNamespace))
154std::optional<RenamerClangTidyCheck::FailureInfo>
155ReservedIdentifierCheck::getDeclFailureInfo(
const NamedDecl *
Decl,
156 const SourceManager &)
const {
157 assert(
Decl &&
Decl->getIdentifier() && !
Decl->getName().empty() &&
158 !
Decl->isImplicit() &&
159 "Decl must be an explicit identifier with a name.");
161 isa<TranslationUnitDecl>(
Decl->getDeclContext()),
165std::optional<RenamerClangTidyCheck::FailureInfo>
166ReservedIdentifierCheck::getMacroFailureInfo(
const Token &MacroNameTok,
167 const SourceManager &)
const {
172RenamerClangTidyCheck::DiagInfo
173ReservedIdentifierCheck::getDiagInfo(
const NamingCheckId &ID,
174 const NamingCheckFailure &Failure)
const {
175 return DiagInfo{
Message, [&](DiagnosticBuilder &Diag) {
const FunctionDecl * Decl
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
const LangOptions & getLangOpts() const
Returns the language options from the context.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Base class for clang-tidy checks that want to flag declarations and/or macros for renaming based on c...
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Derived classes that override this function should call this method from the overridden method.
ReservedIdentifierCheck(StringRef Name, ClangTidyContext *Context)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
static std::optional< std::string > getDoubleUnderscoreFixup(StringRef Name, const LangOptions &LangOpts)
static const char DoubleUnderscoreTag[]
static const char GlobalUnderscoreTag[]
static std::optional< std::string > getUnderscoreGlobalNamespaceFixup(StringRef Name, bool IsInGlobalNamespace)
static const char Message[]
static const char UnderscoreCapitalTag[]
static std::string getNonReservedFixup(std::string Name)
static bool startsWithUnderscoreInGlobalNamespace(StringRef Name, bool IsInGlobalNamespace)
static std::optional< RenamerClangTidyCheck::FailureInfo > getFailureInfoImpl(StringRef Name, bool IsInGlobalNamespace, const LangOptions &LangOpts, bool Invert, ArrayRef< StringRef > AllowedIdentifiers)
static const char NonReservedTag[]
static std::string collapseConsecutive(StringRef Str, char C)
static bool hasReservedDoubleUnderscore(StringRef Name, const LangOptions &LangOpts)
static bool startsWithUnderscoreCapital(StringRef Name)
static std::optional< std::string > getUnderscoreCapitalFixup(StringRef Name)
static int getMessageSelectIndex(StringRef Tag)
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
llvm::StringMap< ClangTidyValue > OptionMap
Information describing a failed check.