Skip to content

Commit feb18a4

Browse files
committed
Merge commit 'ab2e7666c20d00a43b958e91c24991c973c81393' into llvmspirv_pulldown
2 parents af2faa8 + ab2e766 commit feb18a4

File tree

1,123 files changed

+38990
-14309
lines changed

Some content is hidden

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

1,123 files changed

+38990
-14309
lines changed

bolt/include/bolt/Core/BinaryContext.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,8 +1227,7 @@ class BinaryContext {
12271227
Emitter = this->MCE.get();
12281228
SmallString<256> Code;
12291229
SmallVector<MCFixup, 4> Fixups;
1230-
raw_svector_ostream VecOS(Code);
1231-
Emitter->encodeInstruction(Inst, VecOS, Fixups, *STI);
1230+
Emitter->encodeInstruction(Inst, Code, Fixups, *STI);
12321231
return Code.size();
12331232
}
12341233

bolt/lib/Core/BinaryContext.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2289,9 +2289,8 @@ bool BinaryContext::validateInstructionEncoding(
22892289

22902290
SmallString<256> Code;
22912291
SmallVector<MCFixup, 4> Fixups;
2292-
raw_svector_ostream VecOS(Code);
22932292

2294-
MCE->encodeInstruction(Inst, VecOS, Fixups, *STI);
2293+
MCE->encodeInstruction(Inst, Code, Fixups, *STI);
22952294
auto OutputSequence = ArrayRef<uint8_t>((uint8_t *)Code.data(), Code.size());
22962295
if (InputSequence != OutputSequence) {
22972296
if (opts::Verbosity > 1) {

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,8 +1536,7 @@ bool BinaryFunction::scanExternalRefs() {
15361536
// Emit the instruction using temp emitter and generate relocations.
15371537
SmallString<256> Code;
15381538
SmallVector<MCFixup, 4> Fixups;
1539-
raw_svector_ostream VecOS(Code);
1540-
Emitter.MCE->encodeInstruction(Instruction, VecOS, Fixups, *BC.STI);
1539+
Emitter.MCE->encodeInstruction(Instruction, Code, Fixups, *BC.STI);
15411540

15421541
// Create relocation for every fixup.
15431542
for (const MCFixup &Fixup : Fixups) {

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,24 @@ void FoldInitTypeCheck::registerMatchers(MatchFinder *Finder) {
2424
return anyOf(
2525
// Pointer types.
2626
pointsTo(BuiltinTypeWithId(ID)),
27-
// Iterator types.
28-
recordType(hasDeclaration(has(typedefNameDecl(
29-
hasName("value_type"), hasType(BuiltinTypeWithId(ID)))))));
27+
// Iterator types have an `operator*` whose return type is the type we
28+
// care about.
29+
// Notes:
30+
// - `operator*` can be in one of the bases of the iterator class.
31+
// - this does not handle cases when the `operator*` is defined
32+
// outside the iterator class.
33+
recordType(
34+
hasDeclaration(cxxRecordDecl(isSameOrDerivedFrom(has(functionDecl(
35+
hasOverloadedOperatorName("*"),
36+
returns(qualType(hasCanonicalType(anyOf(
37+
// `value_type& operator*();`
38+
references(BuiltinTypeWithId(ID)),
39+
// `value_type operator*();`
40+
BuiltinTypeWithId(ID),
41+
// `auto operator*();`, `decltype(auto) operator*();`
42+
autoType(hasDeducedType(BuiltinTypeWithId(ID)))
43+
//
44+
)))))))))));
3045
};
3146

3247
const auto IteratorParam = parmVarDecl(

clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,12 @@ UnusedParametersCheck::~UnusedParametersCheck() = default;
123123
UnusedParametersCheck::UnusedParametersCheck(StringRef Name,
124124
ClangTidyContext *Context)
125125
: ClangTidyCheck(Name, Context),
126-
StrictMode(Options.getLocalOrGlobal("StrictMode", false)) {}
126+
StrictMode(Options.getLocalOrGlobal("StrictMode", false)),
127+
IgnoreVirtual(Options.get("IgnoreVirtual", false)) {}
127128

128129
void UnusedParametersCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
129130
Options.store(Opts, "StrictMode", StrictMode);
131+
Options.store(Opts, "IgnoreVirtual", IgnoreVirtual);
130132
}
131133

132134
void UnusedParametersCheck::warnOnUnusedParameter(
@@ -176,9 +178,12 @@ void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) {
176178
const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("function");
177179
if (!Function->hasWrittenPrototype() || Function->isTemplateInstantiation())
178180
return;
179-
if (const auto *Method = dyn_cast<CXXMethodDecl>(Function))
181+
if (const auto *Method = dyn_cast<CXXMethodDecl>(Function)) {
182+
if (IgnoreVirtual && Method->isVirtual())
183+
return;
180184
if (Method->isLambdaStaticInvoker())
181185
return;
186+
}
182187
for (unsigned I = 0, E = Function->getNumParams(); I != E; ++I) {
183188
const auto *Param = Function->getParamDecl(I);
184189
if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||

clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class UnusedParametersCheck : public ClangTidyCheck {
2525

2626
private:
2727
const bool StrictMode;
28+
const bool IgnoreVirtual;
2829
class IndexerVisitor;
2930
std::unique_ptr<IndexerVisitor> Indexer;
3031

clang-tools-extra/clang-tidy/modernize/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ add_clang_library(clangTidyModernizeModule
2525
ReplaceRandomShuffleCheck.cpp
2626
ReturnBracedInitListCheck.cpp
2727
ShrinkToFitCheck.cpp
28+
TypeTraitsCheck.cpp
2829
UnaryStaticAssertCheck.cpp
2930
UseAutoCheck.cpp
3031
UseBoolLiteralsCheck.cpp

clang-tools-extra/clang-tidy/modernize/ConcatNestedNamespacesCheck.cpp

Lines changed: 114 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "ConcatNestedNamespacesCheck.h"
10+
#include "../utils/LexerUtils.h"
1011
#include "clang/AST/ASTContext.h"
12+
#include "clang/AST/Decl.h"
1113
#include "clang/ASTMatchers/ASTMatchFinder.h"
12-
#include "clang/Lex/Lexer.h"
14+
#include "clang/Basic/SourceLocation.h"
1315
#include <algorithm>
16+
#include <optional>
1417

1518
namespace clang::tidy::modernize {
1619

@@ -20,8 +23,16 @@ static bool locationsInSameFile(const SourceManager &Sources,
2023
Sources.getFileID(Loc1) == Sources.getFileID(Loc2);
2124
}
2225

23-
static bool anonymousOrInlineNamespace(const NamespaceDecl &ND) {
24-
return ND.isAnonymousNamespace() || ND.isInlineNamespace();
26+
static StringRef getRawStringRef(const SourceRange &Range,
27+
const SourceManager &Sources,
28+
const LangOptions &LangOpts) {
29+
CharSourceRange TextRange = Lexer::getAsCharRange(Range, Sources, LangOpts);
30+
return Lexer::getSourceText(TextRange, Sources, LangOpts);
31+
}
32+
33+
static bool unsupportedNamespace(const NamespaceDecl &ND) {
34+
return ND.isAnonymousNamespace() || ND.isInlineNamespace() ||
35+
!ND.attrs().empty();
2536
}
2637

2738
static bool singleNamedNamespaceChild(const NamespaceDecl &ND) {
@@ -30,33 +41,68 @@ static bool singleNamedNamespaceChild(const NamespaceDecl &ND) {
3041
return false;
3142

3243
const auto *ChildNamespace = dyn_cast<const NamespaceDecl>(*Decls.begin());
33-
return ChildNamespace && !anonymousOrInlineNamespace(*ChildNamespace);
44+
return ChildNamespace && !unsupportedNamespace(*ChildNamespace);
3445
}
3546

36-
static bool alreadyConcatenated(std::size_t NumCandidates,
37-
const SourceRange &ReplacementRange,
38-
const SourceManager &Sources,
39-
const LangOptions &LangOpts) {
40-
// FIXME: This logic breaks when there is a comment with ':'s in the middle.
41-
CharSourceRange TextRange =
42-
Lexer::getAsCharRange(ReplacementRange, Sources, LangOpts);
43-
StringRef CurrentNamespacesText =
44-
Lexer::getSourceText(TextRange, Sources, LangOpts);
45-
return CurrentNamespacesText.count(':') == (NumCandidates - 1) * 2;
47+
template <class R, class F>
48+
static void concatNamespace(NamespaceName &ConcatNameSpace, R &&Range,
49+
F &&Stringify) {
50+
for (auto const &V : Range) {
51+
ConcatNameSpace.append(Stringify(V));
52+
if (V != Range.back())
53+
ConcatNameSpace.append("::");
54+
}
4655
}
4756

48-
ConcatNestedNamespacesCheck::NamespaceString
49-
ConcatNestedNamespacesCheck::concatNamespaces() {
50-
NamespaceString Result("namespace ");
51-
Result.append(Namespaces.front()->getName());
57+
std::optional<SourceRange>
58+
NS::getCleanedNamespaceFrontRange(const SourceManager &SM,
59+
const LangOptions &LangOpts) const {
60+
// Front from namespace tp '{'
61+
std::optional<Token> Tok =
62+
::clang::tidy::utils::lexer::findNextTokenSkippingComments(
63+
back()->getLocation(), SM, LangOpts);
64+
if (!Tok)
65+
return std::nullopt;
66+
while (Tok->getKind() != tok::TokenKind::l_brace) {
67+
Tok = utils::lexer::findNextTokenSkippingComments(Tok->getEndLoc(), SM,
68+
LangOpts);
69+
if (!Tok)
70+
return std::nullopt;
71+
}
72+
return SourceRange{front()->getBeginLoc(), Tok->getEndLoc()};
73+
}
74+
SourceRange NS::getReplacedNamespaceFrontRange() const {
75+
return SourceRange{front()->getBeginLoc(), back()->getLocation()};
76+
}
5277

53-
std::for_each(std::next(Namespaces.begin()), Namespaces.end(),
54-
[&Result](const NamespaceDecl *ND) {
55-
Result.append("::");
56-
Result.append(ND->getName());
57-
});
78+
SourceRange NS::getDefaultNamespaceBackRange() const {
79+
return SourceRange{front()->getRBraceLoc(), front()->getRBraceLoc()};
80+
}
81+
SourceRange NS::getNamespaceBackRange(const SourceManager &SM,
82+
const LangOptions &LangOpts) const {
83+
// Back from '}' to conditional '// namespace xxx'
84+
SourceLocation Loc = front()->getRBraceLoc();
85+
std::optional<Token> Tok =
86+
utils::lexer::findNextTokenIncludingComments(Loc, SM, LangOpts);
87+
if (!Tok)
88+
return getDefaultNamespaceBackRange();
89+
if (Tok->getKind() != tok::TokenKind::comment)
90+
return getDefaultNamespaceBackRange();
91+
SourceRange TokRange = SourceRange{Tok->getLocation(), Tok->getEndLoc()};
92+
StringRef TokText = getRawStringRef(TokRange, SM, LangOpts);
93+
std::string CloseComment = ("namespace " + getName()).str();
94+
// current fix hint in readability/NamespaceCommentCheck.cpp use single line
95+
// comment
96+
if (TokText != "// " + CloseComment && TokText != "//" + CloseComment)
97+
return getDefaultNamespaceBackRange();
98+
return SourceRange{front()->getRBraceLoc(), Tok->getEndLoc()};
99+
}
58100

59-
return Result;
101+
NamespaceName NS::getName() const {
102+
NamespaceName Name{};
103+
concatNamespace(Name, *this,
104+
[](const NamespaceDecl *ND) { return ND->getName(); });
105+
return Name;
60106
}
61107

62108
void ConcatNestedNamespacesCheck::registerMatchers(
@@ -65,11 +111,44 @@ void ConcatNestedNamespacesCheck::registerMatchers(
65111
}
66112

67113
void ConcatNestedNamespacesCheck::reportDiagnostic(
68-
const SourceRange &FrontReplacement, const SourceRange &BackReplacement) {
69-
diag(Namespaces.front()->getBeginLoc(),
70-
"nested namespaces can be concatenated", DiagnosticIDs::Warning)
71-
<< FixItHint::CreateReplacement(FrontReplacement, concatNamespaces())
72-
<< FixItHint::CreateReplacement(BackReplacement, "}");
114+
const SourceManager &SM, const LangOptions &LangOpts) {
115+
DiagnosticBuilder DB =
116+
diag(Namespaces.front().front()->getBeginLoc(),
117+
"nested namespaces can be concatenated", DiagnosticIDs::Warning);
118+
119+
SmallVector<SourceRange, 6> Fronts;
120+
Fronts.reserve(Namespaces.size() - 1U);
121+
SmallVector<SourceRange, 6> Backs;
122+
Backs.reserve(Namespaces.size());
123+
124+
for (const NS &ND : Namespaces) {
125+
std::optional<SourceRange> SR =
126+
ND.getCleanedNamespaceFrontRange(SM, LangOpts);
127+
if (!SR)
128+
return;
129+
Fronts.push_back(SR.value());
130+
Backs.push_back(ND.getNamespaceBackRange(SM, LangOpts));
131+
}
132+
if (Fronts.empty() || Backs.empty())
133+
return;
134+
135+
// the last one should be handled specially
136+
Fronts.pop_back();
137+
SourceRange LastRBrace = Backs.pop_back_val();
138+
139+
NamespaceName ConcatNameSpace{"namespace "};
140+
concatNamespace(ConcatNameSpace, Namespaces,
141+
[](const NS &NS) { return NS.getName(); });
142+
143+
for (SourceRange const &Front : Fronts)
144+
DB << FixItHint::CreateRemoval(Front);
145+
DB << FixItHint::CreateReplacement(
146+
Namespaces.back().getReplacedNamespaceFrontRange(), ConcatNameSpace);
147+
if (LastRBrace != Namespaces.back().getDefaultNamespaceBackRange())
148+
DB << FixItHint::CreateReplacement(LastRBrace,
149+
("} // " + ConcatNameSpace).str());
150+
for (SourceRange const &Back : llvm::reverse(Backs))
151+
DB << FixItHint::CreateRemoval(Back);
73152
}
74153

75154
void ConcatNestedNamespacesCheck::check(
@@ -80,22 +159,18 @@ void ConcatNestedNamespacesCheck::check(
80159
if (!locationsInSameFile(Sources, ND.getBeginLoc(), ND.getRBraceLoc()))
81160
return;
82161

83-
if (anonymousOrInlineNamespace(ND))
162+
if (unsupportedNamespace(ND))
84163
return;
85164

86-
Namespaces.push_back(&ND);
165+
if (!ND.isNested())
166+
Namespaces.push_back(NS{});
167+
Namespaces.back().push_back(&ND);
87168

88169
if (singleNamedNamespaceChild(ND))
89170
return;
90171

91-
SourceRange FrontReplacement(Namespaces.front()->getBeginLoc(),
92-
Namespaces.back()->getLocation());
93-
SourceRange BackReplacement(Namespaces.back()->getRBraceLoc(),
94-
Namespaces.front()->getRBraceLoc());
95-
96-
if (!alreadyConcatenated(Namespaces.size(), FrontReplacement, Sources,
97-
getLangOpts()))
98-
reportDiagnostic(FrontReplacement, BackReplacement);
172+
if (Namespaces.size() > 1)
173+
reportDiagnostic(Sources, getLangOpts());
99174

100175
Namespaces.clear();
101176
}

clang-tools-extra/clang-tidy/modernize/ConcatNestedNamespacesCheck.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@
1515

1616
namespace clang::tidy::modernize {
1717

18+
using NamespaceName = llvm::SmallString<40>;
19+
20+
class NS : public llvm::SmallVector<const NamespaceDecl *, 6> {
21+
public:
22+
std::optional<SourceRange>
23+
getCleanedNamespaceFrontRange(const SourceManager &SM,
24+
const LangOptions &LangOpts) const;
25+
SourceRange getReplacedNamespaceFrontRange() const;
26+
SourceRange getNamespaceBackRange(const SourceManager &SM,
27+
const LangOptions &LangOpts) const;
28+
SourceRange getDefaultNamespaceBackRange() const;
29+
NamespaceName getName() const;
30+
};
31+
1832
class ConcatNestedNamespacesCheck : public ClangTidyCheck {
1933
public:
2034
ConcatNestedNamespacesCheck(StringRef Name, ClangTidyContext *Context)
@@ -26,12 +40,10 @@ class ConcatNestedNamespacesCheck : public ClangTidyCheck {
2640
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
2741

2842
private:
29-
using NamespaceContextVec = llvm::SmallVector<const NamespaceDecl *, 6>;
30-
using NamespaceString = llvm::SmallString<40>;
43+
using NamespaceContextVec = llvm::SmallVector<NS, 6>;
3144

32-
void reportDiagnostic(const SourceRange &FrontReplacement,
33-
const SourceRange &BackReplacement);
34-
NamespaceString concatNamespaces();
45+
void reportDiagnostic(const SourceManager &Sources,
46+
const LangOptions &LangOpts);
3547
NamespaceContextVec Namespaces;
3648
};
3749
} // namespace clang::tidy::modernize

clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "ReplaceRandomShuffleCheck.h"
2727
#include "ReturnBracedInitListCheck.h"
2828
#include "ShrinkToFitCheck.h"
29+
#include "TypeTraitsCheck.h"
2930
#include "UnaryStaticAssertCheck.h"
3031
#include "UseAutoCheck.h"
3132
#include "UseBoolLiteralsCheck.h"
@@ -76,6 +77,7 @@ class ModernizeModule : public ClangTidyModule {
7677
CheckFactories.registerCheck<ReturnBracedInitListCheck>(
7778
"modernize-return-braced-init-list");
7879
CheckFactories.registerCheck<ShrinkToFitCheck>("modernize-shrink-to-fit");
80+
CheckFactories.registerCheck<TypeTraitsCheck>("modernize-type-traits");
7981
CheckFactories.registerCheck<UnaryStaticAssertCheck>(
8082
"modernize-unary-static-assert");
8183
CheckFactories.registerCheck<UseAutoCheck>("modernize-use-auto");

0 commit comments

Comments
 (0)