Skip to content

Commit 6cbd0d8

Browse files
committed
Merge from 'master' to 'sycl-web' (#2)
CONFLICT (content): Merge conflict in clang/include/clang/Basic/DiagnosticSemaKinds.td
2 parents 8e7d8b3 + 1065869 commit 6cbd0d8

File tree

684 files changed

+27321
-17753
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

684 files changed

+27321
-17753
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ ReservedIdentifierCheck::ReservedIdentifierCheck(StringRef Name,
4747
Options.get("AllowedIdentifiers", ""))) {}
4848

4949
void ReservedIdentifierCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
50+
RenamerClangTidyCheck::storeOptions(Opts);
5051
Options.store(Opts, "Invert", Invert);
5152
Options.store(Opts, "AllowedIdentifiers",
5253
utils::options::serializeStringList(AllowedIdentifiers));

clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ IdentifierNamingCheck::IdentifierNamingCheck(StringRef Name,
170170
IdentifierNamingCheck::~IdentifierNamingCheck() = default;
171171

172172
void IdentifierNamingCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
173+
RenamerClangTidyCheck::storeOptions(Opts);
173174
for (size_t i = 0; i < SK_Count; ++i) {
174175
if (NamingStyles[i]) {
175176
if (NamingStyles[i]->Case) {

clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp

Lines changed: 136 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
#include "clang/Lex/PPCallbacks.h"
1515
#include "clang/Lex/Preprocessor.h"
1616
#include "llvm/ADT/DenseMapInfo.h"
17-
#include "llvm/Support/Debug.h"
18-
#include "llvm/Support/Format.h"
17+
#include "llvm/ADT/PointerIntPair.h"
1918

2019
#define DEBUG_TYPE "clang-tidy"
2120

@@ -93,9 +92,16 @@ class RenamerClangTidyCheckPPCallbacks : public PPCallbacks {
9392

9493
RenamerClangTidyCheck::RenamerClangTidyCheck(StringRef CheckName,
9594
ClangTidyContext *Context)
96-
: ClangTidyCheck(CheckName, Context) {}
95+
: ClangTidyCheck(CheckName, Context),
96+
AggressiveDependentMemberLookup(
97+
Options.getLocalOrGlobal("AggressiveDependentMemberLookup", false)) {}
9798
RenamerClangTidyCheck::~RenamerClangTidyCheck() = default;
9899

100+
void RenamerClangTidyCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
101+
Options.store(Opts, "AggressiveDependentMemberLookup",
102+
AggressiveDependentMemberLookup);
103+
}
104+
99105
void RenamerClangTidyCheck::registerMatchers(MatchFinder *Finder) {
100106
Finder->addMatcher(namedDecl().bind("decl"), this);
101107
Finder->addMatcher(usingDecl().bind("using"), this);
@@ -106,20 +112,12 @@ void RenamerClangTidyCheck::registerMatchers(MatchFinder *Finder) {
106112
this);
107113
Finder->addMatcher(typeLoc().bind("typeLoc"), this);
108114
Finder->addMatcher(nestedNameSpecifierLoc().bind("nestedNameLoc"), this);
115+
auto MemberRestrictions =
116+
unless(forFunction(anyOf(isDefaulted(), isImplicit())));
117+
Finder->addMatcher(memberExpr(MemberRestrictions).bind("memberExpr"), this);
109118
Finder->addMatcher(
110-
functionDecl(unless(cxxMethodDecl(isImplicit())),
111-
hasBody(forEachDescendant(memberExpr().bind("memberExpr")))),
119+
cxxDependentScopeMemberExpr(MemberRestrictions).bind("depMemberExpr"),
112120
this);
113-
Finder->addMatcher(
114-
cxxConstructorDecl(
115-
unless(isImplicit()),
116-
forEachConstructorInitializer(
117-
allOf(isWritten(), withInitializer(forEachDescendant(
118-
memberExpr().bind("memberExpr")))))),
119-
this);
120-
Finder->addMatcher(fieldDecl(hasInClassInitializer(
121-
forEachDescendant(memberExpr().bind("memberExpr")))),
122-
this);
123121
}
124122

125123
void RenamerClangTidyCheck::registerPPCallbacks(
@@ -129,9 +127,9 @@ void RenamerClangTidyCheck::registerPPCallbacks(
129127
this));
130128
}
131129

132-
static void addUsage(RenamerClangTidyCheck::NamingCheckFailureMap &Failures,
133-
const RenamerClangTidyCheck::NamingCheckId &Decl,
134-
SourceRange Range, SourceManager *SourceMgr = nullptr) {
130+
void RenamerClangTidyCheck::addUsage(
131+
const RenamerClangTidyCheck::NamingCheckId &Decl, SourceRange Range,
132+
SourceManager *SourceMgr) {
135133
// Do nothing if the provided range is invalid.
136134
if (Range.isInvalid())
137135
return;
@@ -148,7 +146,8 @@ static void addUsage(RenamerClangTidyCheck::NamingCheckFailureMap &Failures,
148146

149147
// Try to insert the identifier location in the Usages map, and bail out if it
150148
// is already in there
151-
RenamerClangTidyCheck::NamingCheckFailure &Failure = Failures[Decl];
149+
RenamerClangTidyCheck::NamingCheckFailure &Failure =
150+
NamingCheckFailures[Decl];
152151
if (!Failure.RawUsageLocs.insert(FixLocation.getRawEncoding()).second)
153152
return;
154153

@@ -159,29 +158,97 @@ static void addUsage(RenamerClangTidyCheck::NamingCheckFailureMap &Failures,
159158
Failure.FixStatus = RenamerClangTidyCheck::ShouldFixStatus::InsideMacro;
160159
}
161160

162-
/// Convenience method when the usage to be added is a NamedDecl
163-
static void addUsage(RenamerClangTidyCheck::NamingCheckFailureMap &Failures,
164-
const NamedDecl *Decl, SourceRange Range,
165-
SourceManager *SourceMgr = nullptr) {
166-
return addUsage(Failures,
167-
RenamerClangTidyCheck::NamingCheckId(Decl->getLocation(),
161+
void RenamerClangTidyCheck::addUsage(const NamedDecl *Decl, SourceRange Range,
162+
SourceManager *SourceMgr) {
163+
164+
return addUsage(RenamerClangTidyCheck::NamingCheckId(Decl->getLocation(),
168165
Decl->getNameAsString()),
169166
Range, SourceMgr);
170167
}
171168

169+
const NamedDecl *findDecl(const RecordDecl &RecDecl, StringRef DeclName) {
170+
for (const Decl *D : RecDecl.decls()) {
171+
if (const auto *ND = dyn_cast<NamedDecl>(D)) {
172+
if (ND->getDeclName().isIdentifier() && ND->getName().equals(DeclName))
173+
return ND;
174+
}
175+
}
176+
return nullptr;
177+
}
178+
179+
namespace {
180+
class NameLookup {
181+
llvm::PointerIntPair<const NamedDecl *, 1, bool> Data;
182+
183+
public:
184+
explicit NameLookup(const NamedDecl *ND) : Data(ND, false) {}
185+
explicit NameLookup(llvm::NoneType) : Data(nullptr, true) {}
186+
explicit NameLookup(std::nullptr_t) : Data(nullptr, false) {}
187+
NameLookup() : NameLookup(nullptr) {}
188+
189+
bool hasMultipleResolutions() const { return Data.getInt(); }
190+
bool hasDecl() const {
191+
assert(!hasMultipleResolutions() && "Found multiple decls");
192+
return Data.getPointer() != nullptr;
193+
}
194+
const NamedDecl *getDecl() const {
195+
assert(!hasMultipleResolutions() && "Found multiple decls");
196+
return Data.getPointer();
197+
}
198+
operator bool() const { return !hasMultipleResolutions(); }
199+
const NamedDecl *operator*() const { return getDecl(); }
200+
};
201+
} // namespace
202+
203+
/// Returns a decl matching the \p DeclName in \p Parent or one of its base
204+
/// classes. If \p AggressiveTemplateLookup is `true` then it will check
205+
/// template dependent base classes as well.
206+
/// If a matching decl is found in multiple base classes then it will return a
207+
/// flag indicating the multiple resolutions.
208+
NameLookup findDeclInBases(const CXXRecordDecl &Parent, StringRef DeclName,
209+
bool AggressiveTemplateLookup) {
210+
if (const NamedDecl *InClassRef = findDecl(Parent, DeclName))
211+
return NameLookup(InClassRef);
212+
const NamedDecl *Found = nullptr;
213+
214+
for (CXXBaseSpecifier Base : Parent.bases()) {
215+
const auto *Record = Base.getType()->getAsCXXRecordDecl();
216+
if (!Record && AggressiveTemplateLookup) {
217+
if (const auto *TST =
218+
Base.getType()->getAs<TemplateSpecializationType>()) {
219+
if (const auto *TD = llvm::dyn_cast_or_null<ClassTemplateDecl>(
220+
TST->getTemplateName().getAsTemplateDecl()))
221+
Record = TD->getTemplatedDecl();
222+
}
223+
}
224+
if (!Record)
225+
continue;
226+
if (auto Search =
227+
findDeclInBases(*Record, DeclName, AggressiveTemplateLookup)) {
228+
if (*Search) {
229+
if (Found)
230+
return NameLookup(
231+
llvm::None); // Multiple decls found in different base classes.
232+
Found = *Search;
233+
continue;
234+
}
235+
} else
236+
return NameLookup(llvm::None); // Propagate multiple resolution back up.
237+
}
238+
return NameLookup(Found); // If nullptr, decl wasnt found.
239+
}
240+
172241
void RenamerClangTidyCheck::check(const MatchFinder::MatchResult &Result) {
173242
if (const auto *Decl =
174243
Result.Nodes.getNodeAs<CXXConstructorDecl>("classRef")) {
175244

176-
addUsage(NamingCheckFailures, Decl->getParent(),
177-
Decl->getNameInfo().getSourceRange());
245+
addUsage(Decl->getParent(), Decl->getNameInfo().getSourceRange());
178246

179247
for (const auto *Init : Decl->inits()) {
180248
if (!Init->isWritten() || Init->isInClassMemberInitializer())
181249
continue;
182250
if (const FieldDecl *FD = Init->getAnyMember())
183-
addUsage(NamingCheckFailures, FD,
184-
SourceRange(Init->getMemberLocation()));
251+
addUsage(FD, SourceRange(Init->getMemberLocation()));
185252
// Note: delegating constructors and base class initializers are handled
186253
// via the "typeLoc" matcher.
187254
}
@@ -198,7 +265,7 @@ void RenamerClangTidyCheck::check(const MatchFinder::MatchResult &Result) {
198265
// we want instead to replace the next token, that will be the identifier.
199266
Range.setBegin(CharSourceRange::getTokenRange(Range).getEnd());
200267

201-
addUsage(NamingCheckFailures, Decl->getParent(), Range);
268+
addUsage(Decl->getParent(), Range);
202269
return;
203270
}
204271

@@ -216,7 +283,7 @@ void RenamerClangTidyCheck::check(const MatchFinder::MatchResult &Result) {
216283
// further TypeLocs handled below
217284

218285
if (Decl) {
219-
addUsage(NamingCheckFailures, Decl, Loc->getSourceRange());
286+
addUsage(Decl, Loc->getSourceRange());
220287
return;
221288
}
222289

@@ -227,15 +294,15 @@ void RenamerClangTidyCheck::check(const MatchFinder::MatchResult &Result) {
227294
SourceRange Range(Ref.getTemplateNameLoc(), Ref.getTemplateNameLoc());
228295
if (const auto *ClassDecl = dyn_cast<TemplateDecl>(Decl)) {
229296
if (const NamedDecl *TemplDecl = ClassDecl->getTemplatedDecl())
230-
addUsage(NamingCheckFailures, TemplDecl, Range);
297+
addUsage(TemplDecl, Range);
231298
return;
232299
}
233300
}
234301

235302
if (const auto &Ref =
236303
Loc->getAs<DependentTemplateSpecializationTypeLoc>()) {
237304
if (const TagDecl *Decl = Ref.getTypePtr()->getAsTagDecl())
238-
addUsage(NamingCheckFailures, Decl, Loc->getSourceRange());
305+
addUsage(Decl, Loc->getSourceRange());
239306
return;
240307
}
241308
}
@@ -244,38 +311,60 @@ void RenamerClangTidyCheck::check(const MatchFinder::MatchResult &Result) {
244311
Result.Nodes.getNodeAs<NestedNameSpecifierLoc>("nestedNameLoc")) {
245312
if (const NestedNameSpecifier *Spec = Loc->getNestedNameSpecifier()) {
246313
if (const NamespaceDecl *Decl = Spec->getAsNamespace()) {
247-
addUsage(NamingCheckFailures, Decl, Loc->getLocalSourceRange());
314+
addUsage(Decl, Loc->getLocalSourceRange());
248315
return;
249316
}
250317
}
251318
}
252319

253320
if (const auto *Decl = Result.Nodes.getNodeAs<UsingDecl>("using")) {
254321
for (const auto *Shadow : Decl->shadows())
255-
addUsage(NamingCheckFailures, Shadow->getTargetDecl(),
256-
Decl->getNameInfo().getSourceRange());
322+
addUsage(Shadow->getTargetDecl(), Decl->getNameInfo().getSourceRange());
257323
return;
258324
}
259325

260326
if (const auto *DeclRef = Result.Nodes.getNodeAs<DeclRefExpr>("declRef")) {
261327
SourceRange Range = DeclRef->getNameInfo().getSourceRange();
262-
addUsage(NamingCheckFailures, DeclRef->getDecl(), Range,
263-
Result.SourceManager);
328+
addUsage(DeclRef->getDecl(), Range, Result.SourceManager);
264329
return;
265330
}
266331

267332
if (const auto *MemberRef =
268333
Result.Nodes.getNodeAs<MemberExpr>("memberExpr")) {
269334
SourceRange Range = MemberRef->getMemberNameInfo().getSourceRange();
270-
addUsage(NamingCheckFailures, MemberRef->getMemberDecl(), Range,
271-
Result.SourceManager);
335+
addUsage(MemberRef->getMemberDecl(), Range, Result.SourceManager);
336+
return;
337+
}
338+
339+
if (const auto *DepMemberRef =
340+
Result.Nodes.getNodeAs<CXXDependentScopeMemberExpr>(
341+
"depMemberExpr")) {
342+
QualType BaseType = DepMemberRef->isArrow()
343+
? DepMemberRef->getBaseType()->getPointeeType()
344+
: DepMemberRef->getBaseType();
345+
if (BaseType.isNull())
346+
return;
347+
const CXXRecordDecl *Base = BaseType.getTypePtr()->getAsCXXRecordDecl();
348+
if (!Base)
349+
return;
350+
DeclarationName DeclName = DepMemberRef->getMemberNameInfo().getName();
351+
if (!DeclName.isIdentifier())
352+
return;
353+
StringRef DependentName = DeclName.getAsIdentifierInfo()->getName();
354+
355+
if (NameLookup Resolved = findDeclInBases(
356+
*Base, DependentName, AggressiveDependentMemberLookup)) {
357+
if (*Resolved)
358+
addUsage(*Resolved, DepMemberRef->getMemberNameInfo().getSourceRange(),
359+
Result.SourceManager);
360+
}
272361
return;
273362
}
274363

275364
if (const auto *Decl = Result.Nodes.getNodeAs<NamedDecl>("decl")) {
276365
// Fix using namespace declarations.
277366
if (const auto *UsingNS = dyn_cast<UsingDirectiveDecl>(Decl))
278-
addUsage(NamingCheckFailures, UsingNS->getNominatedNamespaceAsWritten(),
367+
addUsage(UsingNS->getNominatedNamespaceAsWritten(),
279368
UsingNS->getIdentLocation());
280369

281370
if (!Decl->getIdentifier() || Decl->getName().empty() || Decl->isImplicit())
@@ -285,22 +374,19 @@ void RenamerClangTidyCheck::check(const MatchFinder::MatchResult &Result) {
285374
if (const auto *Value = Result.Nodes.getNodeAs<ValueDecl>("decl")) {
286375
if (const Type *TypePtr = Value->getType().getTypePtrOrNull()) {
287376
if (const auto *Typedef = TypePtr->getAs<TypedefType>())
288-
addUsage(NamingCheckFailures, Typedef->getDecl(),
289-
Value->getSourceRange());
377+
addUsage(Typedef->getDecl(), Value->getSourceRange());
290378
}
291379
}
292380

293381
// Fix type aliases in function declarations.
294382
if (const auto *Value = Result.Nodes.getNodeAs<FunctionDecl>("decl")) {
295383
if (const auto *Typedef =
296384
Value->getReturnType().getTypePtr()->getAs<TypedefType>())
297-
addUsage(NamingCheckFailures, Typedef->getDecl(),
298-
Value->getSourceRange());
385+
addUsage(Typedef->getDecl(), Value->getSourceRange());
299386
for (const ParmVarDecl *Param : Value->parameters()) {
300387
if (const TypedefType *Typedef =
301388
Param->getType().getTypePtr()->getAs<TypedefType>())
302-
addUsage(NamingCheckFailures, Typedef->getDecl(),
303-
Value->getSourceRange());
389+
addUsage(Typedef->getDecl(), Value->getSourceRange());
304390
}
305391
}
306392

@@ -331,7 +417,7 @@ void RenamerClangTidyCheck::check(const MatchFinder::MatchResult &Result) {
331417
}
332418

333419
Failure.Info = std::move(Info);
334-
addUsage(NamingCheckFailures, Decl, Range);
420+
addUsage(Decl, Range);
335421
}
336422
}
337423

@@ -349,7 +435,7 @@ void RenamerClangTidyCheck::checkMacro(SourceManager &SourceMgr,
349435
SourceRange Range(MacroNameTok.getLocation(), MacroNameTok.getEndLoc());
350436

351437
Failure.Info = std::move(Info);
352-
addUsage(NamingCheckFailures, ID, Range);
438+
addUsage(ID, Range);
353439
}
354440

355441
void RenamerClangTidyCheck::expandMacro(const Token &MacroNameTok,
@@ -362,7 +448,7 @@ void RenamerClangTidyCheck::expandMacro(const Token &MacroNameTok,
362448
return;
363449

364450
SourceRange Range(MacroNameTok.getLocation(), MacroNameTok.getEndLoc());
365-
addUsage(NamingCheckFailures, ID, Range);
451+
addUsage(ID, Range);
366452
}
367453

368454
static std::string

clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ class RenamerClangTidyCheck : public ClangTidyCheck {
4040
Preprocessor *ModuleExpanderPP) override final;
4141
void onEndOfTranslationUnit() override final;
4242

43+
/// Derived classes that override this function should call this method from
44+
/// the overridden method.
45+
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
46+
4347
/// This enum will be used in %select of the diagnostic message.
4448
/// Each value below IgnoreFailureThreshold should have an error message.
4549
enum class ShouldFixStatus {
@@ -108,6 +112,13 @@ class RenamerClangTidyCheck : public ClangTidyCheck {
108112
/// Add a usage of a macro if it already has a violation.
109113
void expandMacro(const Token &MacroNameTok, const MacroInfo *MI);
110114

115+
void addUsage(const RenamerClangTidyCheck::NamingCheckId &Decl,
116+
SourceRange Range, SourceManager *SourceMgr = nullptr);
117+
118+
/// Convenience method when the usage to be added is a NamedDecl.
119+
void addUsage(const NamedDecl *Decl, SourceRange Range,
120+
SourceManager *SourceMgr = nullptr);
121+
111122
protected:
112123
/// Overridden by derived classes, returns information about if and how a Decl
113124
/// failed the check. A 'None' result means the Decl did not fail the check.
@@ -142,6 +153,7 @@ class RenamerClangTidyCheck : public ClangTidyCheck {
142153

143154
private:
144155
NamingCheckFailureMap NamingCheckFailures;
156+
const bool AggressiveDependentMemberLookup;
145157
};
146158

147159
} // namespace tidy

0 commit comments

Comments
 (0)