12#include "clang/Frontend/CompilerInstance.h"
13#include "clang/Lex/Preprocessor.h"
14#include "llvm/Support/ConvertUTF.h"
21#include "Confusables.inc"
48std::string ConfusableIdentifierCheck::skeleton(StringRef
Name) {
50 std::string SName =
Name.str();
52 Skeleton.reserve(1 +
Name.size());
54 const char *Curr = SName.c_str();
55 const char *End = Curr + SName.size();
58 const char *Prev = Curr;
60 ConversionResult Result = convertUTF8Sequence(
61 reinterpret_cast<const UTF8 **
>(&Curr),
62 reinterpret_cast<const UTF8 *
>(End), &CodePoint, strictConversion);
63 if (Result != conversionOK) {
64 errs() <<
"Unicode conversion issue\n";
68 StringRef Key(Prev, Curr - Prev);
69 auto Where = llvm::lower_bound(ConfusableEntries, CodePoint,
70 [](
decltype(ConfusableEntries[0]) x,
71 UTF32 y) {
return x.codepoint < y; });
72 if (
Where == std::end(ConfusableEntries) || CodePoint !=
Where->codepoint) {
73 Skeleton.append(Prev, Curr);
76 UTF8 *BufferStart = std::begin(Buffer);
77 UTF8 *IBuffer = BufferStart;
78 const UTF32 *ValuesStart = std::begin(
Where->values);
79 const UTF32 *ValuesEnd = llvm::find(
Where->values,
'\0');
80 if (ConvertUTF32toUTF8(&ValuesStart, ValuesEnd, &IBuffer,
82 strictConversion) != conversionOK) {
83 errs() <<
"Unicode conversion issue\n";
86 Skeleton.append((
char *)BufferStart, (
char *)IBuffer);
93 const DeclContext *DC0 = ND0->getDeclContext()->getPrimaryContext();
94 const DeclContext *DC1 = ND1->getDeclContext()->getPrimaryContext();
96 if (isa<TemplateTypeParmDecl>(ND0) || isa<TemplateTypeParmDecl>(ND0))
99 while (DC0->isTransparentContext())
100 DC0 = DC0->getParent();
101 while (DC1->isTransparentContext())
102 DC1 = DC1->getParent();
104 if (DC0->Equals(DC1))
110static bool isMemberOf(
const NamedDecl *ND,
const CXXRecordDecl *RD) {
111 const DeclContext *NDParent = ND->getDeclContext();
112 if (!NDParent || !isa<CXXRecordDecl>(NDParent))
116 return !RD->forallBases(
117 [NDParent](
const CXXRecordDecl *Base) {
return NDParent != Base; });
120static bool mayShadow(
const NamedDecl *ND0,
const NamedDecl *ND1) {
122 const DeclContext *DC0 = ND0->getDeclContext()->getPrimaryContext();
123 const DeclContext *DC1 = ND1->getDeclContext()->getPrimaryContext();
125 if (
const CXXRecordDecl *RD0 = dyn_cast<CXXRecordDecl>(DC0)) {
126 RD0 = RD0->getDefinition();
127 if (RD0 && ND1->getAccess() != AS_private &&
isMemberOf(ND1, RD0))
130 if (
const CXXRecordDecl *RD1 = dyn_cast<CXXRecordDecl>(DC1)) {
131 RD1 = RD1->getDefinition();
132 if (RD1 && ND0->getAccess() != AS_private &&
isMemberOf(ND0, RD1))
136 if (DC0->Encloses(DC1))
138 if (DC1->Encloses(DC0))
144 const ast_matchers::MatchFinder::MatchResult &Result) {
145 if (
const auto *ND = Result.Nodes.getNodeAs<NamedDecl>(
"nameddecl")) {
146 if (IdentifierInfo *NDII = ND->getIdentifier()) {
147 StringRef NDName = NDII->getName();
148 llvm::SmallVector<const NamedDecl *> &Mapped = Mapper[skeleton(NDName)];
149 for (
const NamedDecl *OND : Mapped) {
150 const IdentifierInfo *ONDII = OND->getIdentifier();
152 StringRef ONDName = ONDII->getName();
153 if (ONDName != NDName) {
154 diag(ND->getLocation(),
"%0 is confusable with %1") << ND << OND;
155 diag(OND->getLocation(),
"other declaration found here",
156 DiagnosticIDs::Note);
160 Mapped.push_back(ND);
166 ast_matchers::MatchFinder *Finder) {
167 Finder->addMatcher(ast_matchers::namedDecl().bind(
"nameddecl"),
this);
const CXXCtorInitializer * Where
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
~ConfusableIdentifierCheck()
ConfusableIdentifierCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
static bool mayShadowImpl(const NamedDecl *ND0, const NamedDecl *ND1)
static bool isMemberOf(const NamedDecl *ND, const CXXRecordDecl *RD)
static bool mayShadow(const NamedDecl *ND0, const NamedDecl *ND1)
Some operations such as code completion produce a set of candidates.