Skip to content

Commit 93abf26

Browse files
committed
Merge remote-tracking branch 'llvm/release/9.x' into rustc/9.0-2019-07-12
2 parents f6446fa + 1931d3c commit 93abf26

File tree

1,288 files changed

+48086
-13809
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,288 files changed

+48086
-13809
lines changed

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ void AssertSideEffectCheck::check(const MatchFinder::MatchResult &Result) {
108108
StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM, LangOpts);
109109

110110
// Check if this macro is an assert.
111-
if (std::find(AssertMacros.begin(), AssertMacros.end(), MacroName) !=
112-
AssertMacros.end()) {
111+
if (llvm::is_contained(AssertMacros, MacroName)) {
113112
AssertMacroName = MacroName;
114113
break;
115114
}

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

+11-6
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,12 @@ void BranchCloneCheck::check(const MatchFinder::MatchResult &Result) {
132132
// We report the first occurence only when we find the second one.
133133
diag(Branches[i]->getBeginLoc(),
134134
"repeated branch in conditional chain");
135-
diag(Lexer::getLocForEndOfToken(Branches[i]->getEndLoc(), 0,
136-
*Result.SourceManager, getLangOpts()),
137-
"end of the original", DiagnosticIDs::Note);
135+
SourceLocation End =
136+
Lexer::getLocForEndOfToken(Branches[i]->getEndLoc(), 0,
137+
*Result.SourceManager, getLangOpts());
138+
if (End.isValid()) {
139+
diag(End, "end of the original", DiagnosticIDs::Note);
140+
}
138141
}
139142

140143
diag(Branches[j]->getBeginLoc(), "clone %0 starts here",
@@ -208,10 +211,12 @@ void BranchCloneCheck::check(const MatchFinder::MatchResult &Result) {
208211

209212
if (EndLoc.isMacroID())
210213
EndLoc = Context.getSourceManager().getExpansionLoc(EndLoc);
214+
EndLoc = Lexer::getLocForEndOfToken(EndLoc, 0, *Result.SourceManager,
215+
getLangOpts());
211216

212-
diag(Lexer::getLocForEndOfToken(EndLoc, 0, *Result.SourceManager,
213-
getLangOpts()),
214-
"last of these clones ends here", DiagnosticIDs::Note);
217+
if (EndLoc.isValid()) {
218+
diag(EndLoc, "last of these clones ends here", DiagnosticIDs::Note);
219+
}
215220
}
216221
BeginCurrent = EndCurrent;
217222
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void ForwardingReferenceOverloadCheck::check(
105105
// template as the function parameter of that type. (This implies that type
106106
// deduction will happen on the type.)
107107
const TemplateParameterList *Params = FuncTemplate->getTemplateParameters();
108-
if (std::find(Params->begin(), Params->end(), TypeParmDecl) == Params->end())
108+
if (!llvm::is_contained(*Params, TypeParmDecl))
109109
return;
110110

111111
// Every parameter after the first must have a default value.

clang-tools-extra/clang-tidy/fuchsia/MultipleInheritanceCheck.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ void MultipleInheritanceCheck::registerMatchers(MatchFinder *Finder) {
9393
return;
9494

9595
// Match declarations which have bases.
96-
Finder->addMatcher(cxxRecordDecl(hasBases()).bind("decl"), this);
96+
Finder->addMatcher(
97+
cxxRecordDecl(allOf(hasBases(), isDefinition())).bind("decl"), this);
9798
}
9899

99100
void MultipleInheritanceCheck::check(const MatchFinder::MatchResult &Result) {

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,8 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {
242242
getOutermostMacroName(StartLoc, SM, Context.getLangOpts());
243243

244244
// Check to see if the user wants to replace the macro being expanded.
245-
if (std::find(NullMacros.begin(), NullMacros.end(), OutermostMacroName) ==
246-
NullMacros.end()) {
245+
if (!llvm::is_contained(NullMacros, OutermostMacroName))
247246
return skipSubTree();
248-
}
249247

250248
StartLoc = SM.getFileLoc(StartLoc);
251249
EndLoc = SM.getFileLoc(EndLoc);
@@ -327,8 +325,7 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {
327325

328326
StringRef Name =
329327
Lexer::getImmediateMacroName(OldArgLoc, SM, Context.getLangOpts());
330-
return std::find(NullMacros.begin(), NullMacros.end(), Name) !=
331-
NullMacros.end();
328+
return llvm::is_contained(NullMacros, Name);
332329
}
333330

334331
MacroLoc = SM.getExpansionRange(ArgLoc).getBegin();

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

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_clang_library(clangTidyReadabilityModule
55
BracesAroundStatementsCheck.cpp
66
ConstReturnTypeCheck.cpp
77
ContainerSizeEmptyCheck.cpp
8+
ConvertMemberFunctionsToStatic.cpp
89
DeleteNullPointerCheck.cpp
910
DeletedDefaultCheck.cpp
1011
ElseAfterReturnCheck.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
//===--- ConvertMemberFunctionsToStatic.cpp - clang-tidy ------------------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "ConvertMemberFunctionsToStatic.h"
11+
#include "clang/AST/ASTContext.h"
12+
#include "clang/AST/DeclCXX.h"
13+
#include "clang/AST/RecursiveASTVisitor.h"
14+
#include "clang/ASTMatchers/ASTMatchFinder.h"
15+
#include "clang/Basic/SourceLocation.h"
16+
17+
using namespace clang::ast_matchers;
18+
19+
namespace clang {
20+
namespace tidy {
21+
namespace readability {
22+
23+
AST_MATCHER(CXXMethodDecl, isStatic) { return Node.isStatic(); }
24+
25+
AST_MATCHER(CXXMethodDecl, hasTrivialBody) { return Node.hasTrivialBody(); }
26+
27+
AST_MATCHER(CXXMethodDecl, isOverloadedOperator) {
28+
return Node.isOverloadedOperator();
29+
}
30+
31+
AST_MATCHER(CXXRecordDecl, hasAnyDependentBases) {
32+
return Node.hasAnyDependentBases();
33+
}
34+
35+
AST_MATCHER(CXXMethodDecl, isTemplate) {
36+
return Node.getTemplatedKind() != FunctionDecl::TK_NonTemplate;
37+
}
38+
39+
AST_MATCHER(CXXMethodDecl, isDependentContext) {
40+
return Node.isDependentContext();
41+
}
42+
43+
AST_MATCHER(CXXMethodDecl, isInsideMacroDefinition) {
44+
const ASTContext &Ctxt = Finder->getASTContext();
45+
return clang::Lexer::makeFileCharRange(
46+
clang::CharSourceRange::getCharRange(
47+
Node.getTypeSourceInfo()->getTypeLoc().getSourceRange()),
48+
Ctxt.getSourceManager(), Ctxt.getLangOpts())
49+
.isInvalid();
50+
}
51+
52+
AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl,
53+
ast_matchers::internal::Matcher<CXXMethodDecl>, InnerMatcher) {
54+
return InnerMatcher.matches(*Node.getCanonicalDecl(), Finder, Builder);
55+
}
56+
57+
AST_MATCHER(CXXMethodDecl, usesThis) {
58+
class FindUsageOfThis : public RecursiveASTVisitor<FindUsageOfThis> {
59+
public:
60+
bool Used = false;
61+
62+
bool VisitCXXThisExpr(const CXXThisExpr *E) {
63+
Used = true;
64+
return false; // Stop traversal.
65+
}
66+
} UsageOfThis;
67+
68+
// TraverseStmt does not modify its argument.
69+
UsageOfThis.TraverseStmt(const_cast<Stmt *>(Node.getBody()));
70+
71+
return UsageOfThis.Used;
72+
}
73+
74+
void ConvertMemberFunctionsToStatic::registerMatchers(MatchFinder *Finder) {
75+
Finder->addMatcher(
76+
cxxMethodDecl(
77+
isDefinition(), isUserProvided(),
78+
unless(anyOf(
79+
isExpansionInSystemHeader(), isVirtual(), isStatic(),
80+
hasTrivialBody(), isOverloadedOperator(), cxxConstructorDecl(),
81+
cxxDestructorDecl(), cxxConversionDecl(), isTemplate(),
82+
isDependentContext(),
83+
ofClass(anyOf(
84+
isLambda(),
85+
hasAnyDependentBases()) // Method might become virtual
86+
// depending on template base class.
87+
),
88+
isInsideMacroDefinition(),
89+
hasCanonicalDecl(isInsideMacroDefinition()), usesThis())))
90+
.bind("x"),
91+
this);
92+
}
93+
94+
/// \brief Obtain the original source code text from a SourceRange.
95+
static StringRef getStringFromRange(SourceManager &SourceMgr,
96+
const LangOptions &LangOpts,
97+
SourceRange Range) {
98+
if (SourceMgr.getFileID(Range.getBegin()) !=
99+
SourceMgr.getFileID(Range.getEnd()))
100+
return {};
101+
102+
return Lexer::getSourceText(CharSourceRange(Range, true), SourceMgr,
103+
LangOpts);
104+
}
105+
106+
static SourceRange getLocationOfConst(const TypeSourceInfo *TSI,
107+
SourceManager &SourceMgr,
108+
const LangOptions &LangOpts) {
109+
assert(TSI);
110+
const auto FTL = TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>();
111+
assert(FTL);
112+
113+
SourceRange Range{FTL.getRParenLoc().getLocWithOffset(1),
114+
FTL.getLocalRangeEnd()};
115+
// Inside Range, there might be other keywords and trailing return types.
116+
// Find the exact position of "const".
117+
StringRef Text = getStringFromRange(SourceMgr, LangOpts, Range);
118+
size_t Offset = Text.find("const");
119+
if (Offset == StringRef::npos)
120+
return {};
121+
122+
SourceLocation Start = Range.getBegin().getLocWithOffset(Offset);
123+
return {Start, Start.getLocWithOffset(strlen("const") - 1)};
124+
}
125+
126+
void ConvertMemberFunctionsToStatic::check(
127+
const MatchFinder::MatchResult &Result) {
128+
const auto *Definition = Result.Nodes.getNodeAs<CXXMethodDecl>("x");
129+
130+
// TODO: For out-of-line declarations, don't modify the source if the header
131+
// is excluded by the -header-filter option.
132+
DiagnosticBuilder Diag =
133+
diag(Definition->getLocation(), "method %0 can be made static")
134+
<< Definition;
135+
136+
// TODO: Would need to remove those in a fix-it.
137+
if (Definition->getMethodQualifiers().hasVolatile() ||
138+
Definition->getMethodQualifiers().hasRestrict() ||
139+
Definition->getRefQualifier() != RQ_None)
140+
return;
141+
142+
const CXXMethodDecl *Declaration = Definition->getCanonicalDecl();
143+
144+
if (Definition->isConst()) {
145+
// Make sure that we either remove 'const' on both declaration and
146+
// definition or emit no fix-it at all.
147+
SourceRange DefConst = getLocationOfConst(Definition->getTypeSourceInfo(),
148+
*Result.SourceManager,
149+
Result.Context->getLangOpts());
150+
151+
if (DefConst.isInvalid())
152+
return;
153+
154+
if (Declaration != Definition) {
155+
SourceRange DeclConst = getLocationOfConst(
156+
Declaration->getTypeSourceInfo(), *Result.SourceManager,
157+
Result.Context->getLangOpts());
158+
159+
if (DeclConst.isInvalid())
160+
return;
161+
Diag << FixItHint::CreateRemoval(DeclConst);
162+
}
163+
164+
// Remove existing 'const' from both declaration and definition.
165+
Diag << FixItHint::CreateRemoval(DefConst);
166+
}
167+
Diag << FixItHint::CreateInsertion(Declaration->getBeginLoc(), "static ");
168+
}
169+
170+
} // namespace readability
171+
} // namespace tidy
172+
} // namespace clang
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===--- ConvertMemberFunctionsToStatic.h - clang-tidy ----------*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_CONVERTMEMFUNCTOSTATIC_H
11+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_CONVERTMEMFUNCTOSTATIC_H
12+
13+
#include "../ClangTidy.h"
14+
15+
namespace clang {
16+
namespace tidy {
17+
namespace readability {
18+
19+
/// This check finds C++ class methods than can be made static
20+
/// because they don't use the 'this' pointer.
21+
///
22+
/// For the user-facing documentation see:
23+
/// http://clang.llvm.org/extra/clang-tidy/checks/
24+
/// readability-convert-member-functions-to-static.html
25+
class ConvertMemberFunctionsToStatic : public ClangTidyCheck {
26+
public:
27+
ConvertMemberFunctionsToStatic(StringRef Name, ClangTidyContext *Context)
28+
: ClangTidyCheck(Name, Context) {}
29+
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
30+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
31+
};
32+
33+
} // namespace readability
34+
} // namespace tidy
35+
} // namespace clang
36+
37+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_CONVERTMEMFUNCTOSTATIC_H

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "NamespaceCommentCheck.h"
1010
#include "clang/AST/ASTContext.h"
1111
#include "clang/ASTMatchers/ASTMatchers.h"
12+
#include "clang/Basic/SourceLocation.h"
1213
#include "clang/Lex/Lexer.h"
1314
#include "llvm/ADT/StringExtras.h"
1415

@@ -181,7 +182,13 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
181182
? "anonymous namespace"
182183
: ("namespace '" + NestedNamespaceName.str() + "'");
183184

184-
diag(AfterRBrace, Message)
185+
// Place diagnostic at an old comment, or closing brace if we did not have it.
186+
SourceLocation DiagLoc =
187+
OldCommentRange.getBegin() != OldCommentRange.getEnd()
188+
? OldCommentRange.getBegin()
189+
: ND->getRBraceLoc();
190+
191+
diag(DiagLoc, Message)
185192
<< NamespaceName
186193
<< FixItHint::CreateReplacement(
187194
CharSourceRange::getCharRange(OldCommentRange),

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

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "BracesAroundStatementsCheck.h"
1414
#include "ConstReturnTypeCheck.h"
1515
#include "ContainerSizeEmptyCheck.h"
16+
#include "ConvertMemberFunctionsToStatic.h"
1617
#include "DeleteNullPointerCheck.h"
1718
#include "DeletedDefaultCheck.h"
1819
#include "ElseAfterReturnCheck.h"
@@ -57,6 +58,8 @@ class ReadabilityModule : public ClangTidyModule {
5758
"readability-const-return-type");
5859
CheckFactories.registerCheck<ContainerSizeEmptyCheck>(
5960
"readability-container-size-empty");
61+
CheckFactories.registerCheck<ConvertMemberFunctionsToStatic>(
62+
"readability-convert-member-functions-to-static");
6063
CheckFactories.registerCheck<DeleteNullPointerCheck>(
6164
"readability-delete-null-pointer");
6265
CheckFactories.registerCheck<DeletedDefaultCheck>(

clang-tools-extra/clangd/ClangdLSPServer.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,13 @@ void ClangdLSPServer::onTypeHierarchy(
926926
Params.resolve, Params.direction, std::move(Reply));
927927
}
928928

929+
void ClangdLSPServer::onResolveTypeHierarchy(
930+
const ResolveTypeHierarchyItemParams &Params,
931+
Callback<Optional<TypeHierarchyItem>> Reply) {
932+
Server->resolveTypeHierarchy(Params.item, Params.resolve, Params.direction,
933+
std::move(Reply));
934+
}
935+
929936
void ClangdLSPServer::applyConfiguration(
930937
const ConfigurationSettings &Settings) {
931938
// Per-file update to the compilation database.
@@ -1021,6 +1028,7 @@ ClangdLSPServer::ClangdLSPServer(
10211028
MsgHandler->bind("workspace/didChangeConfiguration", &ClangdLSPServer::onChangeConfiguration);
10221029
MsgHandler->bind("textDocument/symbolInfo", &ClangdLSPServer::onSymbolInfo);
10231030
MsgHandler->bind("textDocument/typeHierarchy", &ClangdLSPServer::onTypeHierarchy);
1031+
MsgHandler->bind("typeHierarchy/resolve", &ClangdLSPServer::onResolveTypeHierarchy);
10241032
// clang-format on
10251033
}
10261034

clang-tools-extra/clangd/ClangdLSPServer.h

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ class ClangdLSPServer : private DiagnosticsConsumer {
100100
Callback<llvm::Optional<Hover>>);
101101
void onTypeHierarchy(const TypeHierarchyParams &,
102102
Callback<llvm::Optional<TypeHierarchyItem>>);
103+
void onResolveTypeHierarchy(const ResolveTypeHierarchyItemParams &,
104+
Callback<llvm::Optional<TypeHierarchyItem>>);
103105
void onChangeConfiguration(const DidChangeConfigurationParams &);
104106
void onSymbolInfo(const TextDocumentPositionParams &,
105107
Callback<std::vector<SymbolDetails>>);

clang-tools-extra/clangd/ClangdServer.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,13 @@ void ClangdServer::typeHierarchy(PathRef File, Position Pos, int Resolve,
528528
WorkScheduler.runWithAST("Type Hierarchy", File, Bind(Action, std::move(CB)));
529529
}
530530

531+
void ClangdServer::resolveTypeHierarchy(
532+
TypeHierarchyItem Item, int Resolve, TypeHierarchyDirection Direction,
533+
Callback<llvm::Optional<TypeHierarchyItem>> CB) {
534+
clangd::resolveTypeHierarchy(Item, Resolve, Direction, Index);
535+
CB(Item);
536+
}
537+
531538
void ClangdServer::onFileEvent(const DidChangeWatchedFilesParams &Params) {
532539
// FIXME: Do nothing for now. This will be used for indexing and potentially
533540
// invalidating other caches.

clang-tools-extra/clangd/ClangdServer.h

+5
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ class ClangdServer {
210210
TypeHierarchyDirection Direction,
211211
Callback<llvm::Optional<TypeHierarchyItem>> CB);
212212

213+
/// Resolve type hierarchy item in the given direction.
214+
void resolveTypeHierarchy(TypeHierarchyItem Item, int Resolve,
215+
TypeHierarchyDirection Direction,
216+
Callback<llvm::Optional<TypeHierarchyItem>> CB);
217+
213218
/// Retrieve the top symbols from the workspace matching a query.
214219
void workspaceSymbols(StringRef Query, int Limit,
215220
Callback<std::vector<SymbolInformation>> CB);

0 commit comments

Comments
 (0)