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