Debian Codegraph @

llvm-project/clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.cpp

raw
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
9 #include "ReservedIdentifierCheck.h"
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
21 using namespace clang::ast_matchers;
22
23 namespace clang::tidy::bugprone {
24
25 static const char DoubleUnderscoreTag[] = "du";
26 static const char UnderscoreCapitalTag[] = "uc";
27 static const char GlobalUnderscoreTag[] = "global-under";
28 static const char NonReservedTag[] = "non-reserved";
29
30 static 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
34 static int getMessageSelectIndex(StringRef Tag) {
35 if (Tag == NonReservedTag)
36 return 1;
37 if (Tag == GlobalUnderscoreTag)
38 return 2;
39 return 0;
40 }
41
42 ReservedIdentifierCheck::ReservedIdentifierCheck(StringRef Name,
43 ClangTidyContext *Context)
44 : RenamerClangTidyCheck(Name, Context),
45 Invert(Options.get("Invert", false)),
46 AllowedIdentifiers(utils::options::parseStringList(
47 Options.get("AllowedIdentifiers", ""))) {}
48
49 void ReservedIdentifierCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
50 RenamerClangTidyCheck::storeOptions(Opts);
51 Options.store(Opts, "Invert", Invert);
52 Options.store(Opts, "AllowedIdentifiers",
53 utils::options::serializeStringList(AllowedIdentifiers));
54 }
55
56 static std::string collapseConsecutive(StringRef Str, char C) {
57 std::string Result;
58 std::unique_copy(Str.begin(), Str.end(), std::back_inserter(Result),
59 [C](char A, char B) { return A == C && B == C; });
60 return Result;
61 }
62
63 static bool hasReservedDoubleUnderscore(StringRef Name,
64 const LangOptions &LangOpts) {
65 if (LangOpts.CPlusPlus)
66 return Name.contains("__");
67 return Name.startswith("__");
68 }
69
70 static std::optional<std::string>
71 getDoubleUnderscoreFixup(StringRef Name, const LangOptions &LangOpts) {
72 if (hasReservedDoubleUnderscore(Name, LangOpts))
73 return collapseConsecutive(Name, '_');
74 return std::nullopt;
75 }
76
77 static bool startsWithUnderscoreCapital(StringRef Name) {
78 return Name.size() >= 2 && Name[0] == '_' && std::isupper(Name[1]);
79 }
80
81 static std::optional<std::string> getUnderscoreCapitalFixup(StringRef Name) {
82 if (startsWithUnderscoreCapital(Name))
83 return std::string(Name.drop_front(1));
84 return std::nullopt;
85 }
86
87 static bool startsWithUnderscoreInGlobalNamespace(StringRef Name,
88 bool IsInGlobalNamespace) {
89 return IsInGlobalNamespace && Name.size() >= 1 && Name[0] == '_';
90 }
91
92 static std::optional<std::string>
93 getUnderscoreGlobalNamespaceFixup(StringRef Name, bool IsInGlobalNamespace) {
94 if (startsWithUnderscoreInGlobalNamespace(Name, IsInGlobalNamespace))
95 return std::string(Name.drop_front(1));
96 return std::nullopt;
97 }
98
99 static std::string getNonReservedFixup(std::string Name) {
100 assert(!Name.empty());
101 if (Name[0] == '_' || std::isupper(Name[0]))
102 Name.insert(Name.begin(), '_');
103 else
104 Name.insert(Name.begin(), 2, '_');
105 return Name;
106 }
107
108 static std::optional<RenamerClangTidyCheck::FailureInfo>
109 getFailureInfoImpl(StringRef Name, bool IsInGlobalNamespace,
110 const LangOptions &LangOpts, bool Invert,
111 ArrayRef<StringRef> AllowedIdentifiers) {
112 assert(!Name.empty());
113 if (llvm::is_contained(AllowedIdentifiers, Name))
114 return std::nullopt;
115
116 // TODO: Check for names identical to language keywords, and other names
117 // specifically reserved by language standards, e.g. C++ 'zombie names' and C
118 // future library directions
119
120 using FailureInfo = RenamerClangTidyCheck::FailureInfo;
121 if (!Invert) {
122 std::optional<FailureInfo> Info;
123 auto AppendFailure = [&](StringRef Kind, std::string &&Fixup) {
124 if (!Info) {
125 Info = FailureInfo{std::string(Kind), std::move(Fixup)};
126 } else {
127 Info->KindName += Kind;
128 Info->Fixup = std::move(Fixup);
129 }
130 };
131 auto InProgressFixup = [&] {
132 return llvm::transformOptional(
133 Info,
134 [](const FailureInfo &Info) { return StringRef(Info.Fixup); })
135 .value_or(Name);
136 };
137 if (auto Fixup = getDoubleUnderscoreFixup(InProgressFixup(), LangOpts))
138 AppendFailure(DoubleUnderscoreTag, std::move(*Fixup));
139 if (auto Fixup = getUnderscoreCapitalFixup(InProgressFixup()))
140 AppendFailure(UnderscoreCapitalTag, std::move(*Fixup));
141 if (auto Fixup = getUnderscoreGlobalNamespaceFixup(InProgressFixup(),
142 IsInGlobalNamespace))
143 AppendFailure(GlobalUnderscoreTag, std::move(*Fixup));
144
145 return Info;
146 }
147 if (!(hasReservedDoubleUnderscore(Name, LangOpts) ||
148 startsWithUnderscoreCapital(Name) ||
149 startsWithUnderscoreInGlobalNamespace(Name, IsInGlobalNamespace)))
150 return FailureInfo{NonReservedTag, getNonReservedFixup(std::string(Name))};
151 return std::nullopt;
152 }
153
154 std::optional<RenamerClangTidyCheck::FailureInfo>
155 ReservedIdentifierCheck::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.");
160 return getFailureInfoImpl(Decl->getName(),
161 isa<TranslationUnitDecl>(Decl->getDeclContext()),
162 getLangOpts(), Invert, AllowedIdentifiers);
163 }
164
165 std::optional<RenamerClangTidyCheck::FailureInfo>
166 ReservedIdentifierCheck::getMacroFailureInfo(const Token &MacroNameTok,
167 const SourceManager &) const {
168 return getFailureInfoImpl(MacroNameTok.getIdentifierInfo()->getName(), true,
169 getLangOpts(), Invert, AllowedIdentifiers);
170 }
171
172 RenamerClangTidyCheck::DiagInfo
173 ReservedIdentifierCheck::getDiagInfo(const NamingCheckId &ID,
174 const NamingCheckFailure &Failure) const {
175 return DiagInfo{Message, [&](DiagnosticBuilder &Diag) {
176 Diag << ID.second
177 << getMessageSelectIndex(Failure.Info.KindName);
178 }};
179 }
180
181 } // namespace clang::tidy::bugprone