Skip to content

Commit 80f8d7c

Browse files
AutomergerAutomerger
Automerger
authored and
Automerger
committed
Propagating prior merge from 'llvm.org/master'.
apple-llvm-split-commit: bb1875bd558eb40da3aecad35fc20c99f67b8b4b apple-llvm-split-dir: clang-tools-extra/
2 parents 43c1b1b + 51cdd51 commit 80f8d7c

19 files changed

+200
-102
lines changed

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

Lines changed: 11 additions & 6 deletions
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/fuchsia/MultipleInheritanceCheck.cpp

Lines changed: 2 additions & 1 deletion
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/readability/NamespaceCommentCheck.cpp

Lines changed: 8 additions & 1 deletion
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/clangd/QueryDriverDatabase.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ namespace {
5858

5959
std::vector<std::string> parseDriverOutput(llvm::StringRef Output) {
6060
std::vector<std::string> SystemIncludes;
61-
constexpr char const *SIS = "#include <...> search starts here:";
61+
const char SIS[] = "#include <...> search starts here:";
6262
constexpr char const *SIE = "End of search list.";
6363
llvm::SmallVector<llvm::StringRef, 8> Lines;
6464
Output.split(Lines, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
6565

66-
auto StartIt = std::find(Lines.begin(), Lines.end(), SIS);
66+
auto StartIt = llvm::find_if(
67+
Lines, [SIS](llvm::StringRef Line) { return Line.trim() == SIS; });
6768
if (StartIt == Lines.end()) {
6869
elog("System include extraction: start marker not found: {0}", Output);
6970
return {};

clang-tools-extra/clangd/SemanticHighlighting.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ class HighlightingTokenCollector
9999
return true;
100100
}
101101

102+
bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc &TL) {
103+
// TemplateTypeParmTypeLoc does not have a TagDecl in its type ptr.
104+
addToken(TL.getBeginLoc(), TL.getDecl());
105+
return true;
106+
}
107+
108+
bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc &TL) {
109+
if (const TemplateDecl *TD =
110+
TL.getTypePtr()->getTemplateName().getAsTemplateDecl())
111+
addToken(TL.getBeginLoc(), TD);
112+
return true;
113+
}
114+
102115
bool VisitTypeLoc(TypeLoc &TL) {
103116
// This check is for not getting two entries when there are anonymous
104117
// structs. It also makes us not highlight certain namespace qualifiers
@@ -135,6 +148,10 @@ class HighlightingTokenCollector
135148
// We highlight class decls, constructor decls and destructor decls as
136149
// `Class` type. The destructor decls are handled in `VisitTypeLoc` (we will
137150
// visit a TypeLoc where the underlying Type is a CXXRecordDecl).
151+
if (isa<ClassTemplateDecl>(D)) {
152+
addToken(Loc, HighlightingKind::Class);
153+
return;
154+
}
138155
if (isa<RecordDecl>(D)) {
139156
addToken(Loc, HighlightingKind::Class);
140157
return;
@@ -175,6 +192,14 @@ class HighlightingTokenCollector
175192
addToken(Loc, HighlightingKind::Namespace);
176193
return;
177194
}
195+
if (isa<TemplateTemplateParmDecl>(D)) {
196+
addToken(Loc, HighlightingKind::TemplateParameter);
197+
return;
198+
}
199+
if (isa<TemplateTypeParmDecl>(D)) {
200+
addToken(Loc, HighlightingKind::TemplateParameter);
201+
return;
202+
}
178203
}
179204

180205
void addToken(SourceLocation Loc, HighlightingKind Kind) {
@@ -297,6 +322,8 @@ llvm::StringRef toTextMateScope(HighlightingKind Kind) {
297322
return "variable.other.enummember.cpp";
298323
case HighlightingKind::Namespace:
299324
return "entity.name.namespace.cpp";
325+
case HighlightingKind::TemplateParameter:
326+
return "entity.name.type.template.cpp";
300327
case HighlightingKind::NumKinds:
301328
llvm_unreachable("must not pass NumKinds to the function");
302329
}

clang-tools-extra/clangd/SemanticHighlighting.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ enum class HighlightingKind {
3232
Enum,
3333
EnumConstant,
3434
Namespace,
35+
TemplateParameter,
3536

3637
NumKinds,
3738
};

clang-tools-extra/clangd/XRefs.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,15 +1132,9 @@ static void fillSubTypes(const SymbolID &ID,
11321132

11331133
using RecursionProtectionSet = llvm::SmallSet<const CXXRecordDecl *, 4>;
11341134

1135-
static Optional<TypeHierarchyItem>
1136-
getTypeAncestors(const CXXRecordDecl &CXXRD, ASTContext &ASTCtx,
1137-
RecursionProtectionSet &RPSet) {
1138-
Optional<TypeHierarchyItem> Result = declToTypeHierarchyItem(ASTCtx, CXXRD);
1139-
if (!Result)
1140-
return Result;
1141-
1142-
Result->parents.emplace();
1143-
1135+
static void fillSuperTypes(const CXXRecordDecl &CXXRD, ASTContext &ASTCtx,
1136+
std::vector<TypeHierarchyItem> &SuperTypes,
1137+
RecursionProtectionSet &RPSet) {
11441138
// typeParents() will replace dependent template specializations
11451139
// with their class template, so to avoid infinite recursion for
11461140
// certain types of hierarchies, keep the templates encountered
@@ -1149,22 +1143,22 @@ getTypeAncestors(const CXXRecordDecl &CXXRD, ASTContext &ASTCtx,
11491143
auto *Pattern = CXXRD.getDescribedTemplate() ? &CXXRD : nullptr;
11501144
if (Pattern) {
11511145
if (!RPSet.insert(Pattern).second) {
1152-
return Result;
1146+
return;
11531147
}
11541148
}
11551149

11561150
for (const CXXRecordDecl *ParentDecl : typeParents(&CXXRD)) {
11571151
if (Optional<TypeHierarchyItem> ParentSym =
1158-
getTypeAncestors(*ParentDecl, ASTCtx, RPSet)) {
1159-
Result->parents->emplace_back(std::move(*ParentSym));
1152+
declToTypeHierarchyItem(ASTCtx, *ParentDecl)) {
1153+
ParentSym->parents.emplace();
1154+
fillSuperTypes(*ParentDecl, ASTCtx, *ParentSym->parents, RPSet);
1155+
SuperTypes.emplace_back(std::move(*ParentSym));
11601156
}
11611157
}
11621158

11631159
if (Pattern) {
11641160
RPSet.erase(Pattern);
11651161
}
1166-
1167-
return Result;
11681162
}
11691163

11701164
const CXXRecordDecl *findRecordTypeAt(ParsedAST &AST, Position Pos) {
@@ -1231,12 +1225,19 @@ getTypeHierarchy(ParsedAST &AST, Position Pos, int ResolveLevels,
12311225
if (!CXXRD)
12321226
return llvm::None;
12331227

1234-
RecursionProtectionSet RPSet;
12351228
Optional<TypeHierarchyItem> Result =
1236-
getTypeAncestors(*CXXRD, AST.getASTContext(), RPSet);
1229+
declToTypeHierarchyItem(AST.getASTContext(), *CXXRD);
12371230
if (!Result)
12381231
return Result;
12391232

1233+
if (Direction == TypeHierarchyDirection::Parents ||
1234+
Direction == TypeHierarchyDirection::Both) {
1235+
Result->parents.emplace();
1236+
1237+
RecursionProtectionSet RPSet;
1238+
fillSuperTypes(*CXXRD, AST.getASTContext(), *Result->parents, RPSet);
1239+
}
1240+
12401241
if ((Direction == TypeHierarchyDirection::Children ||
12411242
Direction == TypeHierarchyDirection::Both) &&
12421243
ResolveLevels > 0) {

clang-tools-extra/clangd/test/semantic-highlighting.test

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
# CHECK-NEXT: ],
2828
# CHECK-NEXT: [
2929
# CHECK-NEXT: "entity.name.namespace.cpp"
30+
# CHECK-NEXT: ],
31+
# CHECK-NEXT: [
32+
# CHECK-NEXT: "entity.name.type.template.cpp"
3033
# CHECK-NEXT: ]
3134
# CHECK-NEXT: ]
3235
# CHECK-NEXT: },

clang-tools-extra/clangd/test/system-include-extractor.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# RUN: echo '#!/bin/bash' >> %t.dir/my_driver.sh
66
# RUN: echo '[ "$0" = "%t.dir/my_driver.sh" ] || exit' >> %t.dir/my_driver.sh
77
# RUN: echo 'echo line to ignore >&2' >> %t.dir/my_driver.sh
8-
# RUN: echo 'echo \#include \<...\> search starts here: >&2' >> %t.dir/my_driver.sh
8+
# RUN: echo 'echo -e "#include <...> search starts here:\r" >&2' >> %t.dir/my_driver.sh
99
# RUN: echo 'echo %t.dir/my/dir/ >&2' >> %t.dir/my_driver.sh
1010
# RUN: echo 'echo %t.dir/my/dir2/ >&2' >> %t.dir/my_driver.sh
1111
# RUN: echo 'echo End of search list. >&2' >> %t.dir/my_driver.sh

clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ void checkHighlightings(llvm::StringRef Code) {
4040
{HighlightingKind::Namespace, "Namespace"},
4141
{HighlightingKind::EnumConstant, "EnumConstant"},
4242
{HighlightingKind::Field, "Field"},
43-
{HighlightingKind::Method, "Method"}};
43+
{HighlightingKind::Method, "Method"},
44+
{HighlightingKind::TemplateParameter, "TemplateParameter"}};
4445
std::vector<HighlightingToken> ExpectedTokens;
4546
for (const auto &KindString : KindToString) {
4647
std::vector<HighlightingToken> Toks = makeHighlightingTokens(
@@ -80,14 +81,14 @@ TEST(SemanticHighlighting, GetsCorrectTokens) {
8081
)cpp",
8182
R"cpp(
8283
namespace $Namespace[[abc]] {
83-
template<typename T>
84+
template<typename $TemplateParameter[[T]]>
8485
struct $Class[[A]] {
85-
T $Field[[t]];
86+
$TemplateParameter[[T]] $Field[[t]];
8687
};
8788
}
88-
template<typename T>
89-
struct $Class[[C]] : $Namespace[[abc]]::A<T> {
90-
typename T::A* $Field[[D]];
89+
template<typename $TemplateParameter[[T]]>
90+
struct $Class[[C]] : $Namespace[[abc]]::$Class[[A]]<$TemplateParameter[[T]]> {
91+
typename $TemplateParameter[[T]]::A* $Field[[D]];
9192
};
9293
$Namespace[[abc]]::$Class[[A]]<int> $Variable[[AA]];
9394
typedef $Namespace[[abc]]::$Class[[A]]<int> $Class[[AAA]];
@@ -186,6 +187,32 @@ TEST(SemanticHighlighting, GetsCorrectTokens) {
186187
using $Enum[[CD]] = $Enum[[CC]];
187188
$Enum[[CC]] $Function[[f]]($Class[[B]]);
188189
$Enum[[CD]] $Function[[f]]($Class[[BB]]);
190+
)cpp",
191+
R"cpp(
192+
template<typename $TemplateParameter[[T]], typename = void>
193+
class $Class[[A]] {
194+
$TemplateParameter[[T]] $Field[[AA]];
195+
$TemplateParameter[[T]] $Method[[foo]]();
196+
};
197+
template<class $TemplateParameter[[TT]]>
198+
class $Class[[B]] {
199+
$Class[[A]]<$TemplateParameter[[TT]]> $Field[[AA]];
200+
};
201+
template<class $TemplateParameter[[TT]], class $TemplateParameter[[GG]]>
202+
class $Class[[BB]] {};
203+
template<class $TemplateParameter[[T]]>
204+
class $Class[[BB]]<$TemplateParameter[[T]], int> {};
205+
template<class $TemplateParameter[[T]]>
206+
class $Class[[BB]]<$TemplateParameter[[T]], $TemplateParameter[[T]]*> {};
207+
208+
template<template<class> class $TemplateParameter[[T]], class $TemplateParameter[[C]]>
209+
$TemplateParameter[[T]]<$TemplateParameter[[C]]> $Function[[f]]();
210+
211+
template<typename>
212+
class $Class[[Foo]] {};
213+
214+
template<typename $TemplateParameter[[T]]>
215+
void $Function[[foo]]($TemplateParameter[[T]] ...);
189216
)cpp"};
190217
for (const auto &TestCase : TestCases) {
191218
checkHighlightings(TestCase);

clang-tools-extra/clangd/unittests/TestTU.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ ParsedAST TestTU::build() const {
3838
Cmd.push_back("-include");
3939
Cmd.push_back(ImplicitHeaderGuard ? ImportThunk.c_str()
4040
: FullHeaderName.c_str());
41+
// ms-compatibility changes the meaning of #import.
42+
// The default is OS-dependent (on on windows), ensure it's off.
43+
if (ImplicitHeaderGuard)
44+
Cmd.push_back("-fno-ms-compatibility");
4145
}
4246
Cmd.insert(Cmd.end(), ExtraArgs.begin(), ExtraArgs.end());
4347
// Put the file name at the end -- this allows the extra arg (-xc++) to

clang-tools-extra/clangd/unittests/TweakTests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/ADT/StringRef.h"
1818
#include "llvm/Support/Error.h"
1919
#include "llvm/Testing/Support/Error.h"
20+
#include "gmock/gmock-matchers.h"
2021
#include "gmock/gmock.h"
2122
#include "gtest/gtest.h"
2223
#include <cassert>
@@ -134,10 +135,9 @@ void checkApplyContainsError(llvm::StringRef ID, llvm::StringRef Input,
134135
auto Result = apply(ID, Input);
135136
ASSERT_FALSE(Result) << "expected error message:\n " << ErrorMessage <<
136137
"\non input:" << Input;
137-
EXPECT_NE(std::string::npos,
138-
llvm::toString(Result.takeError()).find(ErrorMessage))
139-
<< "Wrong error message:\n " << llvm::toString(Result.takeError())
140-
<< "\nexpected:\n " << ErrorMessage;
138+
EXPECT_THAT(llvm::toString(Result.takeError()),
139+
testing::HasSubstr(ErrorMessage))
140+
<< Input;
141141
}
142142

143143
TEST(TweakTest, SwapIfBranches) {

clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,8 @@ struct Child2b : Child1 {};
630630
ASSERT_TRUE(bool(Result));
631631
EXPECT_THAT(
632632
*Result,
633-
AllOf(WithName("Parent"), WithKind(SymbolKind::Struct), Parents(),
633+
AllOf(WithName("Parent"), WithKind(SymbolKind::Struct),
634+
ParentsNotResolved(),
634635
Children(AllOf(WithName("Child1"), WithKind(SymbolKind::Struct),
635636
ParentsNotResolved(), ChildrenNotResolved()))));
636637

0 commit comments

Comments
 (0)