Skip to content

Commit d87a4a0

Browse files
committed
Merge remote-tracking branch 'llvm.org/master' into apple/master
2 parents 76d166b + 53bcd1e commit d87a4a0

File tree

495 files changed

+18600
-6441
lines changed

Some content is hidden

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

495 files changed

+18600
-6441
lines changed

clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "DontModifyStdNamespaceCheck.h"
2424
#include "FloatLoopCounter.h"
2525
#include "LimitedRandomnessCheck.h"
26+
#include "MutatingCopyCheck.h"
2627
#include "PostfixOperatorCheck.h"
2728
#include "ProperlySeededRandomGeneratorCheck.h"
2829
#include "SetLongJmpCheck.h"
@@ -69,6 +70,8 @@ class CERTModule : public ClangTidyModule {
6970
"cert-oop11-cpp");
7071
CheckFactories.registerCheck<bugprone::UnhandledSelfAssignmentCheck>(
7172
"cert-oop54-cpp");
73+
CheckFactories.registerCheck<MutatingCopyCheck>(
74+
"cert-oop58-cpp");
7275

7376
// C checkers
7477
// DCL

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ add_clang_library(clangTidyCERTModule
77
DontModifyStdNamespaceCheck.cpp
88
FloatLoopCounter.cpp
99
LimitedRandomnessCheck.cpp
10+
MutatingCopyCheck.cpp
1011
PostfixOperatorCheck.cpp
1112
ProperlySeededRandomGeneratorCheck.cpp
1213
SetLongJmpCheck.cpp
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//===--- MutatingCopyCheck.cpp - clang-tidy -------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "MutatingCopyCheck.h"
10+
#include "clang/AST/ASTContext.h"
11+
#include "clang/ASTMatchers/ASTMatchFinder.h"
12+
13+
using namespace clang::ast_matchers;
14+
15+
namespace clang {
16+
namespace tidy {
17+
namespace cert {
18+
19+
static constexpr llvm::StringLiteral SourceDeclName = "ChangedPVD";
20+
static constexpr llvm::StringLiteral MutatingOperatorName = "MutatingOp";
21+
static constexpr llvm::StringLiteral MutatingCallName = "MutatingCall";
22+
23+
void MutatingCopyCheck::registerMatchers(MatchFinder *Finder) {
24+
if (!getLangOpts().CPlusPlus)
25+
return;
26+
27+
const auto MemberExprOrSourceObject = anyOf(
28+
memberExpr(), declRefExpr(to(decl(equalsBoundNode(SourceDeclName)))));
29+
30+
const auto IsPartOfSource =
31+
allOf(unless(hasDescendant(expr(unless(MemberExprOrSourceObject)))),
32+
MemberExprOrSourceObject);
33+
34+
const auto IsSourceMutatingAssignment =
35+
expr(anyOf(binaryOperator(isAssignmentOperator(), hasLHS(IsPartOfSource))
36+
.bind(MutatingOperatorName),
37+
cxxOperatorCallExpr(isAssignmentOperator(),
38+
hasArgument(0, IsPartOfSource))
39+
.bind(MutatingOperatorName)));
40+
41+
const auto MemberExprOrSelf = anyOf(memberExpr(), cxxThisExpr());
42+
43+
const auto IsPartOfSelf = allOf(
44+
unless(hasDescendant(expr(unless(MemberExprOrSelf)))), MemberExprOrSelf);
45+
46+
const auto IsSelfMutatingAssignment =
47+
expr(anyOf(binaryOperator(isAssignmentOperator(), hasLHS(IsPartOfSelf)),
48+
cxxOperatorCallExpr(isAssignmentOperator(),
49+
hasArgument(0, IsPartOfSelf))));
50+
51+
const auto IsSelfMutatingMemberFunction =
52+
functionDecl(hasBody(hasDescendant(IsSelfMutatingAssignment)));
53+
54+
const auto IsSourceMutatingMemberCall =
55+
cxxMemberCallExpr(on(IsPartOfSource),
56+
callee(IsSelfMutatingMemberFunction))
57+
.bind(MutatingCallName);
58+
59+
const auto MutatesSource = allOf(
60+
hasParameter(
61+
0, parmVarDecl(hasType(lValueReferenceType())).bind(SourceDeclName)),
62+
anyOf(forEachDescendant(IsSourceMutatingAssignment),
63+
forEachDescendant(IsSourceMutatingMemberCall)));
64+
65+
Finder->addMatcher(cxxConstructorDecl(isCopyConstructor(), MutatesSource),
66+
this);
67+
68+
Finder->addMatcher(cxxMethodDecl(isCopyAssignmentOperator(), MutatesSource),
69+
this);
70+
}
71+
72+
void MutatingCopyCheck::check(const MatchFinder::MatchResult &Result) {
73+
if (const auto *MemberCall =
74+
Result.Nodes.getNodeAs<CXXMemberCallExpr>(MutatingCallName))
75+
diag(MemberCall->getBeginLoc(), "call mutates copied object");
76+
else if (const auto *Assignment =
77+
Result.Nodes.getNodeAs<Expr>(MutatingOperatorName))
78+
diag(Assignment->getBeginLoc(), "mutating copied object");
79+
}
80+
81+
} // namespace cert
82+
} // namespace tidy
83+
} // namespace clang
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===--- MutatingCopyCheck.h - clang-tidy -----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_MUTATINGCOPYCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_MUTATINGCOPYCHECK_H
11+
12+
#include "../ClangTidyCheck.h"
13+
14+
namespace clang {
15+
namespace tidy {
16+
namespace cert {
17+
18+
/// Finds assignments to the copied object and its direct or indirect members
19+
/// in copy constructors and copy assignment operators.
20+
///
21+
/// For the user-facing documentation see:
22+
/// http://clang.llvm.org/extra/clang-tidy/checks/cert-oop58-cpp.html
23+
class MutatingCopyCheck : public ClangTidyCheck {
24+
public:
25+
MutatingCopyCheck(StringRef Name, ClangTidyContext *Context)
26+
: ClangTidyCheck(Name, Context) {}
27+
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
28+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
29+
};
30+
31+
} // namespace cert
32+
} // namespace tidy
33+
} // namespace clang
34+
35+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_MUTATINGCOPYCHECK_H

clang-tools-extra/clangd/AST.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ class DeducedTypeVisitor : public RecursiveASTVisitor<DeducedTypeVisitor> {
377377
// Loc of "auto" in operator auto()
378378
if (CurLoc.isInvalid() && dyn_cast<CXXConversionDecl>(D))
379379
CurLoc = D->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
380-
// Loc of "auto" in function with traling return type (c++11).
380+
// Loc of "auto" in function with trailing return type (c++11).
381381
if (CurLoc.isInvalid())
382382
CurLoc = D->getSourceRange().getBegin();
383383
if (CurLoc != SearchedLocation)

clang-tools-extra/clangd/ClangdLSPServer.cpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -566,10 +566,6 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params,
566566
{"declarationProvider", true},
567567
{"definitionProvider", true},
568568
{"documentHighlightProvider", true},
569-
{"documentLinkProvider",
570-
llvm::json::Object{
571-
{"resolveProvider", false},
572-
}},
573569
{"hoverProvider", true},
574570
{"renameProvider", std::move(RenameProvider)},
575571
{"selectionRangeProvider", true},
@@ -1204,25 +1200,6 @@ void ClangdLSPServer::onSelectionRange(
12041200
});
12051201
}
12061202

1207-
void ClangdLSPServer::onDocumentLink(
1208-
const DocumentLinkParams &Params,
1209-
Callback<std::vector<DocumentLink>> Reply) {
1210-
1211-
// TODO(forster): This currently resolves all targets eagerly. This is slow,
1212-
// because it blocks on the preamble/AST being built. We could respond to the
1213-
// request faster by using string matching or the lexer to find the includes
1214-
// and resolving the targets lazily.
1215-
Server->documentLinks(
1216-
Params.textDocument.uri.file(),
1217-
[Reply = std::move(Reply)](
1218-
llvm::Expected<std::vector<DocumentLink>> Links) mutable {
1219-
if (!Links) {
1220-
return Reply(Links.takeError());
1221-
}
1222-
return Reply(std::move(Links));
1223-
});
1224-
}
1225-
12261203
ClangdLSPServer::ClangdLSPServer(
12271204
class Transport &Transp, const FileSystemProvider &FSProvider,
12281205
const clangd::CodeCompleteOptions &CCOpts,
@@ -1266,7 +1243,6 @@ ClangdLSPServer::ClangdLSPServer(
12661243
MsgHandler->bind("textDocument/typeHierarchy", &ClangdLSPServer::onTypeHierarchy);
12671244
MsgHandler->bind("typeHierarchy/resolve", &ClangdLSPServer::onResolveTypeHierarchy);
12681245
MsgHandler->bind("textDocument/selectionRange", &ClangdLSPServer::onSelectionRange);
1269-
MsgHandler->bind("textDocument/documentLink", &ClangdLSPServer::onDocumentLink);
12701246
// clang-format on
12711247
}
12721248

clang-tools-extra/clangd/ClangdLSPServer.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ class ClangdLSPServer : private DiagnosticsConsumer {
111111
Callback<std::vector<SymbolDetails>>);
112112
void onSelectionRange(const SelectionRangeParams &,
113113
Callback<std::vector<SelectionRange>>);
114-
void onDocumentLink(const DocumentLinkParams &,
115-
Callback<std::vector<DocumentLink>>);
116114

117115
std::vector<Fix> getFixes(StringRef File, const clangd::Diagnostic &D);
118116

clang-tools-extra/clangd/ClangdServer.cpp

Lines changed: 23 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -374,24 +374,15 @@ void ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName,
374374
WorkScheduler.runWithAST("Rename", File, std::move(Action));
375375
}
376376

377-
// May generate several candidate selections, due to SelectionTree ambiguity.
378-
static llvm::Expected<std::vector<Tweak::Selection>>
377+
static llvm::Expected<Tweak::Selection>
379378
tweakSelection(const Range &Sel, const InputsAndAST &AST) {
380379
auto Begin = positionToOffset(AST.Inputs.Contents, Sel.start);
381380
if (!Begin)
382381
return Begin.takeError();
383382
auto End = positionToOffset(AST.Inputs.Contents, Sel.end);
384383
if (!End)
385384
return End.takeError();
386-
std::vector<Tweak::Selection> Result;
387-
SelectionTree::createEach(AST.AST.getASTContext(), AST.AST.getTokens(),
388-
*Begin, *End, [&](SelectionTree T) {
389-
Result.emplace_back(AST.Inputs.Index, AST.AST,
390-
*Begin, *End, std::move(T));
391-
return false;
392-
});
393-
assert(!Result.empty() && "Expected at least one SelectionTree");
394-
return Result;
385+
return Tweak::Selection(AST.Inputs.Index, AST.AST, *Begin, *End);
395386
}
396387

397388
void ClangdServer::enumerateTweaks(PathRef File, Range Sel,
@@ -400,21 +391,12 @@ void ClangdServer::enumerateTweaks(PathRef File, Range Sel,
400391
this](Expected<InputsAndAST> InpAST) mutable {
401392
if (!InpAST)
402393
return CB(InpAST.takeError());
403-
auto Selections = tweakSelection(Sel, *InpAST);
404-
if (!Selections)
405-
return CB(Selections.takeError());
394+
auto Selection = tweakSelection(Sel, *InpAST);
395+
if (!Selection)
396+
return CB(Selection.takeError());
406397
std::vector<TweakRef> Res;
407-
// Don't allow a tweak to fire more than once across ambiguous selections.
408-
llvm::DenseSet<llvm::StringRef> PreparedTweaks;
409-
auto Filter = [&](const Tweak &T) {
410-
return TweakFilter(T) && !PreparedTweaks.count(T.id());
411-
};
412-
for (const auto &Sel : *Selections) {
413-
for (auto &T : prepareTweaks(Sel, Filter)) {
414-
Res.push_back({T->id(), T->title(), T->intent()});
415-
PreparedTweaks.insert(T->id());
416-
}
417-
}
398+
for (auto &T : prepareTweaks(*Selection, TweakFilter))
399+
Res.push_back({T->id(), T->title(), T->intent()});
418400

419401
CB(std::move(Res));
420402
};
@@ -429,30 +411,21 @@ void ClangdServer::applyTweak(PathRef File, Range Sel, StringRef TweakID,
429411
FS = FSProvider.getFileSystem()](Expected<InputsAndAST> InpAST) mutable {
430412
if (!InpAST)
431413
return CB(InpAST.takeError());
432-
auto Selections = tweakSelection(Sel, *InpAST);
433-
if (!Selections)
434-
return CB(Selections.takeError());
435-
llvm::Optional<llvm::Expected<Tweak::Effect>> Effect;
436-
// Try each selection, take the first one that prepare()s.
437-
// If they all fail, Effect will hold get the last error.
438-
for (const auto &Selection : *Selections) {
439-
auto T = prepareTweak(TweakID, Selection);
440-
if (T) {
441-
Effect = (*T)->apply(Selection);
442-
break;
443-
}
444-
Effect = T.takeError();
445-
}
446-
assert(Effect.hasValue() && "Expected at least one selection");
447-
if (*Effect) {
448-
// Tweaks don't apply clang-format, do that centrally here.
449-
for (auto &It : (*Effect)->ApplyEdits) {
450-
Edit &E = It.second;
451-
format::FormatStyle Style =
452-
getFormatStyleForFile(File, E.InitialCode, FS.get());
453-
if (llvm::Error Err = reformatEdit(E, Style))
454-
elog("Failed to format {0}: {1}", It.first(), std::move(Err));
455-
}
414+
auto Selection = tweakSelection(Sel, *InpAST);
415+
if (!Selection)
416+
return CB(Selection.takeError());
417+
auto A = prepareTweak(TweakID, *Selection);
418+
if (!A)
419+
return CB(A.takeError());
420+
auto Effect = (*A)->apply(*Selection);
421+
if (!Effect)
422+
return CB(Effect.takeError());
423+
for (auto &It : Effect->ApplyEdits) {
424+
Edit &E = It.second;
425+
format::FormatStyle Style =
426+
getFormatStyleForFile(File, E.InitialCode, FS.get());
427+
if (llvm::Error Err = reformatEdit(E, Style))
428+
elog("Failed to format {0}: {1}", It.first(), std::move(Err));
456429
}
457430
return CB(std::move(*Effect));
458431
};
@@ -493,7 +466,7 @@ void ClangdServer::locateSymbolAt(PathRef File, Position Pos,
493466

494467
void ClangdServer::switchSourceHeader(
495468
PathRef Path, Callback<llvm::Optional<clangd::Path>> CB) {
496-
// We want to return the result as fast as possible, stragety is:
469+
// We want to return the result as fast as possible, strategy is:
497470
// 1) use the file-only heuristic, it requires some IO but it is much
498471
// faster than building AST, but it only works when .h/.cc files are in
499472
// the same directory.
@@ -638,17 +611,6 @@ void ClangdServer::semanticRanges(PathRef File, Position Pos,
638611
WorkScheduler.runWithAST("SemanticRanges", File, std::move(Action));
639612
}
640613

641-
void ClangdServer::documentLinks(PathRef File,
642-
Callback<std::vector<DocumentLink>> CB) {
643-
auto Action =
644-
[CB = std::move(CB)](llvm::Expected<InputsAndAST> InpAST) mutable {
645-
if (!InpAST)
646-
return CB(InpAST.takeError());
647-
CB(clangd::getDocumentLinks(InpAST->AST));
648-
};
649-
WorkScheduler.runWithAST("DocumentLinks", File, std::move(Action));
650-
}
651-
652614
std::vector<std::pair<Path, std::size_t>>
653615
ClangdServer::getUsedBytesPerFile() const {
654616
return WorkScheduler.getUsedBytesPerFile();

clang-tools-extra/clangd/ClangdServer.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,6 @@ class ClangdServer {
287287
void semanticRanges(PathRef File, Position Pos,
288288
Callback<std::vector<Range>> CB);
289289

290-
/// Get all document links in a file.
291-
void documentLinks(PathRef File, Callback<std::vector<DocumentLink>> CB);
292-
293290
/// Returns estimated memory usage for each of the currently open files.
294291
/// The order of results is unspecified.
295292
/// Overall memory usage of clangd may be significantly more than reported

clang-tools-extra/clangd/CodeComplete.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ struct CodeCompletionBuilder {
395395
std::string ReturnType;
396396
};
397397

398-
// If all BundledEntrys have the same value for a property, return it.
398+
// If all BundledEntries have the same value for a property, return it.
399399
template <std::string BundledEntry::*Member>
400400
const std::string *onlyValue() const {
401401
auto B = Bundled.begin(), E = Bundled.end();
@@ -499,7 +499,7 @@ llvm::Optional<SymbolID> getSymbolID(const CodeCompletionResult &R,
499499
llvm_unreachable("unknown CodeCompletionResult kind");
500500
}
501501

502-
// Scopes of the paritial identifier we're trying to complete.
502+
// Scopes of the partial identifier we're trying to complete.
503503
// It is used when we query the index for more completion results.
504504
struct SpecifiedScope {
505505
// The scopes we should look in, determined by Sema.
@@ -874,7 +874,7 @@ class SignatureHelpCollector final : public CodeCompleteConsumer {
874874
// Function Template.
875875
// - High score is better.
876876
// - Shorter signature is better.
877-
// - Alphebatically smaller is better.
877+
// - Alphabetically smaller is better.
878878
if (L.Quality.NumberOfParameters != R.Quality.NumberOfParameters)
879879
return L.Quality.NumberOfParameters < R.Quality.NumberOfParameters;
880880
if (L.Quality.NumberOfOptionalParameters !=
@@ -1510,7 +1510,7 @@ class CodeCompleteFlow {
15101510
}
15111511

15121512
// Merges Sema and Index results where possible, to form CompletionCandidates.
1513-
// \p Identifiers is raw idenfiers that can also be completion condidates.
1513+
// \p Identifiers is raw idenfiers that can also be completion candidates.
15141514
// Identifiers are not merged with results from index or sema.
15151515
// Groups overloads if desired, to form CompletionCandidate::Bundles. The
15161516
// bundles are scored and top results are returned, best to worst.

clang-tools-extra/clangd/Diagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ std::string capitalize(std::string Message) {
232232
}
233233

234234
/// Returns a message sent to LSP for the main diagnostic in \p D.
235-
/// This message may include notes, if they're not emited in some other way.
235+
/// This message may include notes, if they're not emitted in some other way.
236236
/// Example output:
237237
///
238238
/// no matching function for call to 'foo'

clang-tools-extra/clangd/Diagnostics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct ClangdDiagnosticOptions {
5353
struct DiagBase {
5454
std::string Message;
5555
// Intended to be used only in error messages.
56-
// May be relative, absolute or even artifically constructed.
56+
// May be relative, absolute or even artificially constructed.
5757
std::string File;
5858
// Absolute path to containing file, if available.
5959
llvm::Optional<std::string> AbsFile;

0 commit comments

Comments
 (0)