2018-01-26 01:29:17 +08:00
|
|
|
//===-- ClangdUnitTests.cpp - ClangdUnit tests ------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-01-26 01:29:17 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-08-08 15:21:06 +08:00
|
|
|
#include "AST.h"
|
2018-01-26 01:29:17 +08:00
|
|
|
#include "Annotations.h"
|
2018-03-12 23:28:22 +08:00
|
|
|
#include "ClangdUnit.h"
|
[clangd] Fix unicode handling, using UTF-16 where LSP requires it.
Summary:
The Language Server Protocol unfortunately mandates that locations in files
be represented by line/column pairs, where the "column" is actually an index
into the UTF-16-encoded text of the line.
(This is because VSCode is written in JavaScript, which is UTF-16-native).
Internally clangd treats source files at UTF-8, the One True Encoding, and
generally deals with byte offsets (though there are exceptions).
Before this patch, conversions between offsets and LSP Position pretended
that Position.character was UTF-8 bytes, which is only true for ASCII lines.
Now we examine the text to convert correctly (but don't actually need to
transcode it, due to some nice details of the encodings).
The updated functions in SourceCode are the blessed way to interact with
the Position.character field, and anything else is likely to be wrong.
So I also updated the other accesses:
- CodeComplete needs a "clang-style" line/column, with column in utf-8 bytes.
This is now converted via Position -> offset -> clang line/column
(a new function is added to SourceCode.h for the second conversion).
- getBeginningOfIdentifier skipped backwards in UTF-16 space, which is will
behave badly when it splits a surrogate pair. Skipping backwards in UTF-8
coordinates gives the lexer a fighting chance of getting this right.
While here, I clarified(?) the logic comments, fixed a bug with identifiers
containing digits, simplified the signature slightly and added a test.
This seems likely to cause problems with editors that have the same bug, and
treat the protocol as if columns are UTF-8 bytes. But we can find and fix those.
Reviewers: hokein
Subscribers: klimek, ilya-biryukov, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D46035
llvm-svn: 331029
2018-04-27 19:59:28 +08:00
|
|
|
#include "SourceCode.h"
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-16 01:43:27 +08:00
|
|
|
#include "TestTU.h"
|
2019-08-08 15:21:06 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2019-06-19 22:03:19 +08:00
|
|
|
#include "clang/Basic/TokenKinds.h"
|
|
|
|
#include "clang/Tooling/Syntax/Tokens.h"
|
2018-01-26 01:29:17 +08:00
|
|
|
#include "llvm/Support/ScopedPrinter.h"
|
2019-08-08 15:21:06 +08:00
|
|
|
#include "gmock/gmock-matchers.h"
|
2018-01-26 01:29:17 +08:00
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
namespace {
|
2018-10-20 23:30:37 +08:00
|
|
|
|
2019-05-06 18:08:47 +08:00
|
|
|
using ::testing::ElementsAre;
|
2019-08-08 15:21:06 +08:00
|
|
|
using ::testing::ElementsAreArray;
|
|
|
|
using ::testing::AllOf;
|
2018-03-12 23:28:22 +08:00
|
|
|
|
[clangd] Fix unicode handling, using UTF-16 where LSP requires it.
Summary:
The Language Server Protocol unfortunately mandates that locations in files
be represented by line/column pairs, where the "column" is actually an index
into the UTF-16-encoded text of the line.
(This is because VSCode is written in JavaScript, which is UTF-16-native).
Internally clangd treats source files at UTF-8, the One True Encoding, and
generally deals with byte offsets (though there are exceptions).
Before this patch, conversions between offsets and LSP Position pretended
that Position.character was UTF-8 bytes, which is only true for ASCII lines.
Now we examine the text to convert correctly (but don't actually need to
transcode it, due to some nice details of the encodings).
The updated functions in SourceCode are the blessed way to interact with
the Position.character field, and anything else is likely to be wrong.
So I also updated the other accesses:
- CodeComplete needs a "clang-style" line/column, with column in utf-8 bytes.
This is now converted via Position -> offset -> clang line/column
(a new function is added to SourceCode.h for the second conversion).
- getBeginningOfIdentifier skipped backwards in UTF-16 space, which is will
behave badly when it splits a surrogate pair. Skipping backwards in UTF-8
coordinates gives the lexer a fighting chance of getting this right.
While here, I clarified(?) the logic comments, fixed a bug with identifiers
containing digits, simplified the signature slightly and added a test.
This seems likely to cause problems with editors that have the same bug, and
treat the protocol as if columns are UTF-8 bytes. But we can find and fix those.
Reviewers: hokein
Subscribers: klimek, ilya-biryukov, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D46035
llvm-svn: 331029
2018-04-27 19:59:28 +08:00
|
|
|
TEST(ClangdUnitTest, GetBeginningOfIdentifier) {
|
2018-10-05 20:08:06 +08:00
|
|
|
std::string Preamble = R"cpp(
|
|
|
|
struct Bar { int func(); };
|
|
|
|
#define MACRO(X) void f() { X; }
|
|
|
|
Bar* bar;
|
|
|
|
)cpp";
|
[clangd] Fix unicode handling, using UTF-16 where LSP requires it.
Summary:
The Language Server Protocol unfortunately mandates that locations in files
be represented by line/column pairs, where the "column" is actually an index
into the UTF-16-encoded text of the line.
(This is because VSCode is written in JavaScript, which is UTF-16-native).
Internally clangd treats source files at UTF-8, the One True Encoding, and
generally deals with byte offsets (though there are exceptions).
Before this patch, conversions between offsets and LSP Position pretended
that Position.character was UTF-8 bytes, which is only true for ASCII lines.
Now we examine the text to convert correctly (but don't actually need to
transcode it, due to some nice details of the encodings).
The updated functions in SourceCode are the blessed way to interact with
the Position.character field, and anything else is likely to be wrong.
So I also updated the other accesses:
- CodeComplete needs a "clang-style" line/column, with column in utf-8 bytes.
This is now converted via Position -> offset -> clang line/column
(a new function is added to SourceCode.h for the second conversion).
- getBeginningOfIdentifier skipped backwards in UTF-16 space, which is will
behave badly when it splits a surrogate pair. Skipping backwards in UTF-8
coordinates gives the lexer a fighting chance of getting this right.
While here, I clarified(?) the logic comments, fixed a bug with identifiers
containing digits, simplified the signature slightly and added a test.
This seems likely to cause problems with editors that have the same bug, and
treat the protocol as if columns are UTF-8 bytes. But we can find and fix those.
Reviewers: hokein
Subscribers: klimek, ilya-biryukov, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D46035
llvm-svn: 331029
2018-04-27 19:59:28 +08:00
|
|
|
// First ^ is the expected beginning, last is the search position.
|
2018-10-05 20:08:06 +08:00
|
|
|
for (std::string Text : std::vector<std::string>{
|
[clangd] Fix unicode handling, using UTF-16 where LSP requires it.
Summary:
The Language Server Protocol unfortunately mandates that locations in files
be represented by line/column pairs, where the "column" is actually an index
into the UTF-16-encoded text of the line.
(This is because VSCode is written in JavaScript, which is UTF-16-native).
Internally clangd treats source files at UTF-8, the One True Encoding, and
generally deals with byte offsets (though there are exceptions).
Before this patch, conversions between offsets and LSP Position pretended
that Position.character was UTF-8 bytes, which is only true for ASCII lines.
Now we examine the text to convert correctly (but don't actually need to
transcode it, due to some nice details of the encodings).
The updated functions in SourceCode are the blessed way to interact with
the Position.character field, and anything else is likely to be wrong.
So I also updated the other accesses:
- CodeComplete needs a "clang-style" line/column, with column in utf-8 bytes.
This is now converted via Position -> offset -> clang line/column
(a new function is added to SourceCode.h for the second conversion).
- getBeginningOfIdentifier skipped backwards in UTF-16 space, which is will
behave badly when it splits a surrogate pair. Skipping backwards in UTF-8
coordinates gives the lexer a fighting chance of getting this right.
While here, I clarified(?) the logic comments, fixed a bug with identifiers
containing digits, simplified the signature slightly and added a test.
This seems likely to cause problems with editors that have the same bug, and
treat the protocol as if columns are UTF-8 bytes. But we can find and fix those.
Reviewers: hokein
Subscribers: klimek, ilya-biryukov, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D46035
llvm-svn: 331029
2018-04-27 19:59:28 +08:00
|
|
|
"int ^f^oo();", // inside identifier
|
|
|
|
"int ^foo();", // beginning of identifier
|
|
|
|
"int ^foo^();", // end of identifier
|
|
|
|
"int foo(^);", // non-identifier
|
|
|
|
"^int foo();", // beginning of file (can't back up)
|
|
|
|
"int ^f0^0();", // after a digit (lexing at N-1 is wrong)
|
|
|
|
"int ^λλ^λ();", // UTF-8 handled properly when backing up
|
2018-10-05 20:08:06 +08:00
|
|
|
|
|
|
|
// identifier in macro arg
|
|
|
|
"MACRO(bar->^func())", // beginning of identifier
|
|
|
|
"MACRO(bar->^fun^c())", // inside identifier
|
|
|
|
"MACRO(bar->^func^())", // end of identifier
|
|
|
|
"MACRO(^bar->func())", // begin identifier
|
|
|
|
"MACRO(^bar^->func())", // end identifier
|
|
|
|
"^MACRO(bar->func())", // beginning of macro name
|
|
|
|
"^MAC^RO(bar->func())", // inside macro name
|
|
|
|
"^MACRO^(bar->func())", // end of macro name
|
[clangd] Fix unicode handling, using UTF-16 where LSP requires it.
Summary:
The Language Server Protocol unfortunately mandates that locations in files
be represented by line/column pairs, where the "column" is actually an index
into the UTF-16-encoded text of the line.
(This is because VSCode is written in JavaScript, which is UTF-16-native).
Internally clangd treats source files at UTF-8, the One True Encoding, and
generally deals with byte offsets (though there are exceptions).
Before this patch, conversions between offsets and LSP Position pretended
that Position.character was UTF-8 bytes, which is only true for ASCII lines.
Now we examine the text to convert correctly (but don't actually need to
transcode it, due to some nice details of the encodings).
The updated functions in SourceCode are the blessed way to interact with
the Position.character field, and anything else is likely to be wrong.
So I also updated the other accesses:
- CodeComplete needs a "clang-style" line/column, with column in utf-8 bytes.
This is now converted via Position -> offset -> clang line/column
(a new function is added to SourceCode.h for the second conversion).
- getBeginningOfIdentifier skipped backwards in UTF-16 space, which is will
behave badly when it splits a surrogate pair. Skipping backwards in UTF-8
coordinates gives the lexer a fighting chance of getting this right.
While here, I clarified(?) the logic comments, fixed a bug with identifiers
containing digits, simplified the signature slightly and added a test.
This seems likely to cause problems with editors that have the same bug, and
treat the protocol as if columns are UTF-8 bytes. But we can find and fix those.
Reviewers: hokein
Subscribers: klimek, ilya-biryukov, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D46035
llvm-svn: 331029
2018-04-27 19:59:28 +08:00
|
|
|
}) {
|
2018-10-05 20:08:06 +08:00
|
|
|
std::string WithPreamble = Preamble + Text;
|
|
|
|
Annotations TestCase(WithPreamble);
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-16 01:43:27 +08:00
|
|
|
auto AST = TestTU::withCode(TestCase.code()).build();
|
2019-05-29 05:52:34 +08:00
|
|
|
const auto &SourceMgr = AST.getSourceManager();
|
[clangd] Fix unicode handling, using UTF-16 where LSP requires it.
Summary:
The Language Server Protocol unfortunately mandates that locations in files
be represented by line/column pairs, where the "column" is actually an index
into the UTF-16-encoded text of the line.
(This is because VSCode is written in JavaScript, which is UTF-16-native).
Internally clangd treats source files at UTF-8, the One True Encoding, and
generally deals with byte offsets (though there are exceptions).
Before this patch, conversions between offsets and LSP Position pretended
that Position.character was UTF-8 bytes, which is only true for ASCII lines.
Now we examine the text to convert correctly (but don't actually need to
transcode it, due to some nice details of the encodings).
The updated functions in SourceCode are the blessed way to interact with
the Position.character field, and anything else is likely to be wrong.
So I also updated the other accesses:
- CodeComplete needs a "clang-style" line/column, with column in utf-8 bytes.
This is now converted via Position -> offset -> clang line/column
(a new function is added to SourceCode.h for the second conversion).
- getBeginningOfIdentifier skipped backwards in UTF-16 space, which is will
behave badly when it splits a surrogate pair. Skipping backwards in UTF-8
coordinates gives the lexer a fighting chance of getting this right.
While here, I clarified(?) the logic comments, fixed a bug with identifiers
containing digits, simplified the signature slightly and added a test.
This seems likely to cause problems with editors that have the same bug, and
treat the protocol as if columns are UTF-8 bytes. But we can find and fix those.
Reviewers: hokein
Subscribers: klimek, ilya-biryukov, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D46035
llvm-svn: 331029
2018-04-27 19:59:28 +08:00
|
|
|
SourceLocation Actual = getBeginningOfIdentifier(
|
|
|
|
AST, TestCase.points().back(), SourceMgr.getMainFileID());
|
2018-10-05 20:08:06 +08:00
|
|
|
Position ActualPos = offsetToPosition(
|
|
|
|
TestCase.code(),
|
|
|
|
SourceMgr.getFileOffset(SourceMgr.getSpellingLoc(Actual)));
|
[clangd] Fix unicode handling, using UTF-16 where LSP requires it.
Summary:
The Language Server Protocol unfortunately mandates that locations in files
be represented by line/column pairs, where the "column" is actually an index
into the UTF-16-encoded text of the line.
(This is because VSCode is written in JavaScript, which is UTF-16-native).
Internally clangd treats source files at UTF-8, the One True Encoding, and
generally deals with byte offsets (though there are exceptions).
Before this patch, conversions between offsets and LSP Position pretended
that Position.character was UTF-8 bytes, which is only true for ASCII lines.
Now we examine the text to convert correctly (but don't actually need to
transcode it, due to some nice details of the encodings).
The updated functions in SourceCode are the blessed way to interact with
the Position.character field, and anything else is likely to be wrong.
So I also updated the other accesses:
- CodeComplete needs a "clang-style" line/column, with column in utf-8 bytes.
This is now converted via Position -> offset -> clang line/column
(a new function is added to SourceCode.h for the second conversion).
- getBeginningOfIdentifier skipped backwards in UTF-16 space, which is will
behave badly when it splits a surrogate pair. Skipping backwards in UTF-8
coordinates gives the lexer a fighting chance of getting this right.
While here, I clarified(?) the logic comments, fixed a bug with identifiers
containing digits, simplified the signature slightly and added a test.
This seems likely to cause problems with editors that have the same bug, and
treat the protocol as if columns are UTF-8 bytes. But we can find and fix those.
Reviewers: hokein
Subscribers: klimek, ilya-biryukov, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D46035
llvm-svn: 331029
2018-04-27 19:59:28 +08:00
|
|
|
EXPECT_EQ(TestCase.points().front(), ActualPos) << Text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-09 23:35:00 +08:00
|
|
|
MATCHER_P(DeclNamed, Name, "") {
|
|
|
|
if (NamedDecl *ND = dyn_cast<NamedDecl>(arg))
|
|
|
|
if (ND->getName() == Name)
|
|
|
|
return true;
|
|
|
|
if (auto *Stream = result_listener->stream()) {
|
|
|
|
llvm::raw_os_ostream OS(*Stream);
|
|
|
|
arg->dump(OS);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-08 15:21:06 +08:00
|
|
|
// Matches if the Decl has template args equal to ArgName. If the decl is a
|
|
|
|
// NamedDecl and ArgName is an empty string it also matches.
|
|
|
|
MATCHER_P(WithTemplateArgs, ArgName, "") {
|
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(arg)) {
|
|
|
|
if (const auto *Args = FD->getTemplateSpecializationArgs()) {
|
|
|
|
std::string SpecializationArgs;
|
|
|
|
// Without the PrintingPolicy "bool" will be printed as "_Bool".
|
|
|
|
LangOptions LO;
|
|
|
|
PrintingPolicy Policy(LO);
|
|
|
|
Policy.adjustForCPlusPlus();
|
|
|
|
for (const auto Arg : Args->asArray()) {
|
|
|
|
if (SpecializationArgs.size() > 0)
|
|
|
|
SpecializationArgs += ",";
|
|
|
|
SpecializationArgs += Arg.getAsType().getAsString(Policy);
|
|
|
|
}
|
|
|
|
if (Args->size() == 0)
|
|
|
|
return ArgName == SpecializationArgs;
|
|
|
|
return ArgName == "<" + SpecializationArgs + ">";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (const NamedDecl *ND = dyn_cast<NamedDecl>(arg))
|
|
|
|
return printTemplateSpecializationArgs(*ND) == ArgName;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-09 23:35:00 +08:00
|
|
|
TEST(ClangdUnitTest, TopLevelDecls) {
|
|
|
|
TestTU TU;
|
|
|
|
TU.HeaderCode = R"(
|
|
|
|
int header1();
|
|
|
|
int header2;
|
|
|
|
)";
|
|
|
|
TU.Code = "int main();";
|
|
|
|
auto AST = TU.build();
|
|
|
|
EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main")));
|
2019-07-01 19:49:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ClangdUnitTest, DoesNotGetIncludedTopDecls) {
|
|
|
|
TestTU TU;
|
|
|
|
TU.HeaderCode = R"cpp(
|
|
|
|
#define LL void foo(){}
|
|
|
|
template<class T>
|
|
|
|
struct H {
|
|
|
|
H() {}
|
|
|
|
LL
|
|
|
|
};
|
|
|
|
)cpp";
|
|
|
|
TU.Code = R"cpp(
|
|
|
|
int main() {
|
|
|
|
H<int> h;
|
|
|
|
h.foo();
|
|
|
|
}
|
|
|
|
)cpp";
|
|
|
|
auto AST = TU.build();
|
|
|
|
EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main")));
|
2018-11-09 23:35:00 +08:00
|
|
|
}
|
|
|
|
|
2019-08-08 15:21:06 +08:00
|
|
|
TEST(ClangdUnitTest, DoesNotGetImplicitTemplateTopDecls) {
|
|
|
|
TestTU TU;
|
|
|
|
TU.Code = R"cpp(
|
|
|
|
template<typename T>
|
|
|
|
void f(T) {}
|
|
|
|
void s() {
|
|
|
|
f(10UL);
|
|
|
|
}
|
|
|
|
)cpp";
|
|
|
|
|
|
|
|
auto AST = TU.build();
|
|
|
|
EXPECT_THAT(AST.getLocalTopLevelDecls(),
|
|
|
|
ElementsAre(DeclNamed("f"), DeclNamed("s")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ClangdUnitTest,
|
|
|
|
GetsExplicitInstantiationAndSpecializationTemplateTopDecls) {
|
|
|
|
TestTU TU;
|
|
|
|
TU.Code = R"cpp(
|
|
|
|
template <typename T>
|
|
|
|
void f(T) {}
|
|
|
|
template<>
|
|
|
|
void f(bool);
|
|
|
|
template void f(double);
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
struct V {};
|
|
|
|
template<class T>
|
|
|
|
struct V<T*> {};
|
|
|
|
template <>
|
|
|
|
struct V<bool> {};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T foo = T(10);
|
|
|
|
int i = foo<int>;
|
|
|
|
double d = foo<double>;
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
int foo<T*> = 0;
|
|
|
|
template <>
|
|
|
|
int foo<bool> = 0;
|
|
|
|
)cpp";
|
|
|
|
|
|
|
|
auto AST = TU.build();
|
|
|
|
EXPECT_THAT(
|
|
|
|
AST.getLocalTopLevelDecls(),
|
|
|
|
ElementsAreArray({AllOf(DeclNamed("f"), WithTemplateArgs("")),
|
|
|
|
AllOf(DeclNamed("f"), WithTemplateArgs("<bool>")),
|
|
|
|
AllOf(DeclNamed("f"), WithTemplateArgs("<double>")),
|
|
|
|
AllOf(DeclNamed("V"), WithTemplateArgs("")),
|
|
|
|
AllOf(DeclNamed("V"), WithTemplateArgs("<T *>")),
|
|
|
|
AllOf(DeclNamed("V"), WithTemplateArgs("<bool>")),
|
|
|
|
AllOf(DeclNamed("foo"), WithTemplateArgs("")),
|
|
|
|
AllOf(DeclNamed("i"), WithTemplateArgs("")),
|
|
|
|
AllOf(DeclNamed("d"), WithTemplateArgs("")),
|
2019-08-09 15:35:16 +08:00
|
|
|
AllOf(DeclNamed("foo"), WithTemplateArgs("<T *>")),
|
2019-08-08 15:21:06 +08:00
|
|
|
AllOf(DeclNamed("foo"), WithTemplateArgs("<bool>"))}));
|
|
|
|
}
|
|
|
|
|
2019-06-19 22:03:19 +08:00
|
|
|
TEST(ClangdUnitTest, TokensAfterPreamble) {
|
|
|
|
TestTU TU;
|
|
|
|
TU.AdditionalFiles["foo.h"] = R"(
|
|
|
|
int foo();
|
|
|
|
)";
|
|
|
|
TU.Code = R"cpp(
|
|
|
|
#include "foo.h"
|
|
|
|
first_token;
|
|
|
|
void test() {
|
|
|
|
}
|
|
|
|
last_token
|
|
|
|
)cpp";
|
|
|
|
auto AST = TU.build();
|
|
|
|
const syntax::TokenBuffer &T = AST.getTokens();
|
|
|
|
const auto &SM = AST.getSourceManager();
|
|
|
|
|
|
|
|
ASSERT_GT(T.expandedTokens().size(), 2u);
|
|
|
|
// Check first token after the preamble.
|
|
|
|
EXPECT_EQ(T.expandedTokens().front().text(SM), "first_token");
|
|
|
|
// Last token is always 'eof'.
|
|
|
|
EXPECT_EQ(T.expandedTokens().back().kind(), tok::eof);
|
|
|
|
// Check the token before 'eof'.
|
|
|
|
EXPECT_EQ(T.expandedTokens().drop_back().back().text(SM), "last_token");
|
|
|
|
|
|
|
|
// The spelled tokens for the main file should have everything.
|
|
|
|
auto Spelled = T.spelledTokens(SM.getMainFileID());
|
|
|
|
ASSERT_FALSE(Spelled.empty());
|
|
|
|
EXPECT_EQ(Spelled.front().kind(), tok::hash);
|
|
|
|
EXPECT_EQ(Spelled.back().text(SM), "last_token");
|
|
|
|
}
|
|
|
|
|
2019-07-10 17:28:35 +08:00
|
|
|
|
|
|
|
TEST(ClangdUnitTest, NoCrashOnTokensWithTidyCheck) {
|
|
|
|
TestTU TU;
|
|
|
|
// this check runs the preprocessor, we need to make sure it does not break
|
|
|
|
// our recording logic.
|
|
|
|
TU.ClangTidyChecks = "modernize-use-trailing-return-type";
|
|
|
|
TU.Code = "inline int foo() {}";
|
|
|
|
|
|
|
|
auto AST = TU.build();
|
|
|
|
const syntax::TokenBuffer &T = AST.getTokens();
|
|
|
|
const auto &SM = AST.getSourceManager();
|
|
|
|
|
|
|
|
ASSERT_GT(T.expandedTokens().size(), 7u);
|
|
|
|
// Check first token after the preamble.
|
|
|
|
EXPECT_EQ(T.expandedTokens().front().text(SM), "inline");
|
|
|
|
// Last token is always 'eof'.
|
|
|
|
EXPECT_EQ(T.expandedTokens().back().kind(), tok::eof);
|
|
|
|
// Check the token before 'eof'.
|
|
|
|
EXPECT_EQ(T.expandedTokens().drop_back().back().text(SM), "}");
|
|
|
|
}
|
|
|
|
|
2018-01-26 01:29:17 +08:00
|
|
|
} // namespace
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|