clang-tools 19.0.0git
ReservedIdentifierCheck.cpp
Go to the documentation of this file.
1//===--- ReservedIdentifierCheck.cpp - clang-tidy -------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
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"
15#include <algorithm>
16#include <cctype>
17#include <optional>
18
19// FixItHint
20
21using namespace clang::ast_matchers;
22
23namespace clang::tidy::bugprone {
24
25static const char DoubleUnderscoreTag[] = "du";
26static const char UnderscoreCapitalTag[] = "uc";
27static const char GlobalUnderscoreTag[] = "global-under";
28static const char NonReservedTag[] = "non-reserved";
29
30static const char Message[] =
31 "declaration uses identifier '%0', which is %select{a reserved "
32 "identifier|not a reserved identifier|reserved in the global namespace}1";
33
34static int getMessageSelectIndex(StringRef Tag) {
35 if (Tag == NonReservedTag)
36 return 1;
38 return 2;
39 return 0;
40}
41
42llvm::SmallVector<llvm::Regex>
43ReservedIdentifierCheck::parseAllowedIdentifiers() const {
44 llvm::SmallVector<llvm::Regex> AllowedIdentifiers;
45 AllowedIdentifiers.reserve(AllowedIdentifiersRaw.size());
46
47 for (const auto &Identifier : AllowedIdentifiersRaw) {
48 AllowedIdentifiers.emplace_back(Identifier.str());
49 if (!AllowedIdentifiers.back().isValid()) {
50 configurationDiag("Invalid allowed identifier regex '%0'") << Identifier;
51 AllowedIdentifiers.pop_back();
52 }
53 }
54
55 return AllowedIdentifiers;
56}
57
59 ClangTidyContext *Context)
60 : RenamerClangTidyCheck(Name, Context),
61 Invert(Options.get("Invert", false)),
62 AllowedIdentifiersRaw(utils::options::parseStringList(
63 Options.get("AllowedIdentifiers", ""))),
64 AllowedIdentifiers(parseAllowedIdentifiers()) {}
65
68 Options.store(Opts, "Invert", Invert);
69 Options.store(Opts, "AllowedIdentifiers",
70 utils::options::serializeStringList(AllowedIdentifiersRaw));
71}
72
73static std::string collapseConsecutive(StringRef Str, char C) {
74 std::string Result;
75 std::unique_copy(Str.begin(), Str.end(), std::back_inserter(Result),
76 [C](char A, char B) { return A == C && B == C; });
77 return Result;
78}
79
80static bool hasReservedDoubleUnderscore(StringRef Name,
81 const LangOptions &LangOpts) {
82 if (LangOpts.CPlusPlus)
83 return Name.contains("__");
84 return Name.starts_with("__");
85}
86
87static std::optional<std::string>
88getDoubleUnderscoreFixup(StringRef Name, const LangOptions &LangOpts) {
89 if (hasReservedDoubleUnderscore(Name, LangOpts))
90 return collapseConsecutive(Name, '_');
91 return std::nullopt;
92}
93
94static bool startsWithUnderscoreCapital(StringRef Name) {
95 return Name.size() >= 2 && Name[0] == '_' && std::isupper(Name[1]);
96}
97
98static std::optional<std::string> getUnderscoreCapitalFixup(StringRef Name) {
100 return std::string(Name.drop_front(1));
101 return std::nullopt;
102}
103
105 bool IsInGlobalNamespace,
106 bool IsMacro) {
107 return !IsMacro && IsInGlobalNamespace && Name.starts_with("_");
108}
109
110static std::optional<std::string>
111getUnderscoreGlobalNamespaceFixup(StringRef Name, bool IsInGlobalNamespace,
112 bool IsMacro) {
113 if (startsWithUnderscoreInGlobalNamespace(Name, IsInGlobalNamespace, IsMacro))
114 return std::string(Name.drop_front(1));
115 return std::nullopt;
116}
117
118static std::string getNonReservedFixup(std::string Name) {
119 assert(!Name.empty());
120 if (Name[0] == '_' || std::isupper(Name[0]))
121 Name.insert(Name.begin(), '_');
122 else
123 Name.insert(Name.begin(), 2, '_');
124 return Name;
125}
126
127static std::optional<RenamerClangTidyCheck::FailureInfo>
128getFailureInfoImpl(StringRef Name, bool IsInGlobalNamespace, bool IsMacro,
129 const LangOptions &LangOpts, bool Invert,
130 ArrayRef<llvm::Regex> AllowedIdentifiers) {
131 assert(!Name.empty());
132
133 if (llvm::any_of(AllowedIdentifiers, [&](const llvm::Regex &Regex) {
134 return Regex.match(Name);
135 })) {
136 return std::nullopt;
137 }
138 // TODO: Check for names identical to language keywords, and other names
139 // specifically reserved by language standards, e.g. C++ 'zombie names' and C
140 // future library directions
141
142 using FailureInfo = RenamerClangTidyCheck::FailureInfo;
143 if (!Invert) {
144 std::optional<FailureInfo> Info;
145 auto AppendFailure = [&](StringRef Kind, std::string &&Fixup) {
146 if (!Info) {
147 Info = FailureInfo{std::string(Kind), std::move(Fixup)};
148 } else {
149 Info->KindName += Kind;
150 Info->Fixup = std::move(Fixup);
151 }
152 };
153 auto InProgressFixup = [&] {
154 return llvm::transformOptional(
155 Info,
156 [](const FailureInfo &Info) { return StringRef(Info.Fixup); })
157 .value_or(Name);
158 };
159 if (auto Fixup = getDoubleUnderscoreFixup(InProgressFixup(), LangOpts))
160 AppendFailure(DoubleUnderscoreTag, std::move(*Fixup));
161 if (auto Fixup = getUnderscoreCapitalFixup(InProgressFixup()))
162 AppendFailure(UnderscoreCapitalTag, std::move(*Fixup));
163 if (auto Fixup = getUnderscoreGlobalNamespaceFixup(
164 InProgressFixup(), IsInGlobalNamespace, IsMacro))
165 AppendFailure(GlobalUnderscoreTag, std::move(*Fixup));
166
167 return Info;
168 }
169 if (!(hasReservedDoubleUnderscore(Name, LangOpts) ||
171 startsWithUnderscoreInGlobalNamespace(Name, IsInGlobalNamespace,
172 IsMacro)))
173 return FailureInfo{NonReservedTag, getNonReservedFixup(std::string(Name))};
174 return std::nullopt;
175}
176
177std::optional<RenamerClangTidyCheck::FailureInfo>
178ReservedIdentifierCheck::getDeclFailureInfo(const NamedDecl *Decl,
179 const SourceManager &) const {
180 assert(Decl && Decl->getIdentifier() && !Decl->getName().empty() &&
181 "Decl must be an explicit identifier with a name.");
182 // Implicit identifiers cannot fail.
183 if (Decl->isImplicit())
184 return std::nullopt;
185
186 return getFailureInfoImpl(
187 Decl->getName(), isa<TranslationUnitDecl>(Decl->getDeclContext()),
188 /*IsMacro = */ false, getLangOpts(), Invert, AllowedIdentifiers);
189}
190
191std::optional<RenamerClangTidyCheck::FailureInfo>
192ReservedIdentifierCheck::getMacroFailureInfo(const Token &MacroNameTok,
193 const SourceManager &) const {
194 return getFailureInfoImpl(MacroNameTok.getIdentifierInfo()->getName(), true,
195 /*IsMacro = */ true, getLangOpts(), Invert,
196 AllowedIdentifiers);
197}
198
199RenamerClangTidyCheck::DiagInfo
200ReservedIdentifierCheck::getDiagInfo(const NamingCheckId &ID,
201 const NamingCheckFailure &Failure) const {
202 return DiagInfo{Message, [&](DiagnosticBuilder &Diag) {
203 Diag << ID.second
204 << getMessageSelectIndex(Failure.Info.KindName);
205 }};
206}
207
208} // namespace clang::tidy::bugprone
const FunctionDecl * Decl
BindArgumentKind Kind
llvm::SmallString< 256U > Name
const Criteria C
FunctionInfo Info
HTMLTag Tag
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.
DiagnosticBuilder configurationDiag(StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning) const
Adds a diagnostic to report errors in the check's configuration.
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, bool IsMacro)
static const char UnderscoreCapitalTag[]
static std::string getNonReservedFixup(std::string Name)
static const char NonReservedTag[]
static std::optional< RenamerClangTidyCheck::FailureInfo > getFailureInfoImpl(StringRef Name, bool IsInGlobalNamespace, bool IsMacro, const LangOptions &LangOpts, bool Invert, ArrayRef< llvm::Regex > AllowedIdentifiers)
static std::string collapseConsecutive(StringRef Str, char C)
static bool hasReservedDoubleUnderscore(StringRef Name, const LangOptions &LangOpts)
static bool startsWithUnderscoreCapital(StringRef Name)
static bool startsWithUnderscoreInGlobalNamespace(StringRef Name, bool IsInGlobalNamespace, bool IsMacro)
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.