2019-09-04 17:46:06 +08:00
|
|
|
//===-- ParsedASTTests.cpp ------------------------------------------------===//
|
2018-01-26 01:29:17 +08:00
|
|
|
//
|
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-09-04 17:46:06 +08:00
|
|
|
//
|
|
|
|
// These tests cover clangd's logic to build a TU, which generally uses the APIs
|
|
|
|
// in ParsedAST and Preamble, via the TestTU helper.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2018-01-26 01:29:17 +08:00
|
|
|
|
2020-06-29 23:05:51 +08:00
|
|
|
#include "../../clang-tidy/ClangTidyCheck.h"
|
2020-02-20 00:36:41 +08:00
|
|
|
#include "../../clang-tidy/ClangTidyModule.h"
|
|
|
|
#include "../../clang-tidy/ClangTidyModuleRegistry.h"
|
2019-08-08 15:21:06 +08:00
|
|
|
#include "AST.h"
|
2018-01-26 01:29:17 +08:00
|
|
|
#include "Annotations.h"
|
2019-08-27 18:02:18 +08:00
|
|
|
#include "Compiler.h"
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
#include "Diagnostics.h"
|
2020-04-02 16:53:45 +08:00
|
|
|
#include "Headers.h"
|
2019-09-04 17:46:06 +08:00
|
|
|
#include "ParsedAST.h"
|
2020-04-02 16:53:45 +08:00
|
|
|
#include "Preamble.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"
|
2019-08-27 18:02:18 +08:00
|
|
|
#include "TestFS.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"
|
2020-11-26 02:35:34 +08:00
|
|
|
#include "TidyProvider.h"
|
2019-08-08 15:21:06 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2020-02-20 00:36:41 +08:00
|
|
|
#include "clang/Basic/SourceLocation.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2019-06-19 22:03:19 +08:00
|
|
|
#include "clang/Basic/TokenKinds.h"
|
2020-02-20 00:36:41 +08:00
|
|
|
#include "clang/Lex/PPCallbacks.h"
|
|
|
|
#include "clang/Lex/Token.h"
|
2019-06-19 22:03:19 +08:00
|
|
|
#include "clang/Tooling/Syntax/Tokens.h"
|
2020-04-02 16:53:45 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2020-02-20 00:36:41 +08:00
|
|
|
#include "llvm/ADT/StringRef.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-09-04 17:46:06 +08:00
|
|
|
using ::testing::AllOf;
|
2019-05-06 18:08:47 +08:00
|
|
|
using ::testing::ElementsAre;
|
2019-08-08 15:21:06 +08:00
|
|
|
using ::testing::ElementsAreArray;
|
2018-03-12 23:28:22 +08:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-12 16:45:37 +08:00
|
|
|
MATCHER_P(DeclKind, Kind, "") {
|
|
|
|
if (NamedDecl *ND = dyn_cast<NamedDecl>(arg))
|
2020-11-02 15:25:45 +08:00
|
|
|
if (ND->getDeclKindName() == llvm::StringRef(Kind))
|
2020-10-12 21:41:04 +08:00
|
|
|
return true;
|
2020-10-12 18:04:44 +08:00
|
|
|
if (auto *Stream = result_listener->stream()) {
|
|
|
|
llvm::raw_os_ostream OS(*Stream);
|
|
|
|
arg->dump(OS);
|
|
|
|
}
|
2020-10-12 16:45:37 +08:00
|
|
|
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();
|
2020-01-08 02:58:17 +08:00
|
|
|
for (const auto &Arg : Args->asArray()) {
|
2019-08-08 15:21:06 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-02-20 00:36:41 +08:00
|
|
|
MATCHER_P(RangeIs, R, "") {
|
|
|
|
return arg.beginOffset() == R.Begin && arg.endOffset() == R.End;
|
|
|
|
}
|
|
|
|
|
2020-04-02 16:53:45 +08:00
|
|
|
MATCHER(EqInc, "") {
|
|
|
|
Inclusion Actual = testing::get<0>(arg);
|
|
|
|
Inclusion Expected = testing::get<1>(arg);
|
|
|
|
return std::tie(Actual.HashLine, Actual.Written) ==
|
|
|
|
std::tie(Expected.HashLine, Expected.Written);
|
|
|
|
}
|
|
|
|
|
2020-11-02 15:25:45 +08:00
|
|
|
TEST(ParsedASTTest, TopLevelDecls) {
|
2018-11-09 23:35:00 +08:00
|
|
|
TestTU TU;
|
|
|
|
TU.HeaderCode = R"(
|
|
|
|
int header1();
|
|
|
|
int header2;
|
|
|
|
)";
|
2020-10-12 16:45:37 +08:00
|
|
|
TU.Code = R"cpp(
|
|
|
|
int main();
|
|
|
|
template <typename> bool X = true;
|
|
|
|
)cpp";
|
2018-11-09 23:35:00 +08:00
|
|
|
auto AST = TU.build();
|
2020-10-12 18:04:44 +08:00
|
|
|
EXPECT_THAT(AST.getLocalTopLevelDecls(),
|
|
|
|
testing::UnorderedElementsAreArray(
|
|
|
|
{AllOf(DeclNamed("main"), DeclKind("Function")),
|
|
|
|
AllOf(DeclNamed("X"), DeclKind("VarTemplate"))}));
|
2019-07-01 19:49:01 +08:00
|
|
|
}
|
|
|
|
|
2019-09-04 17:46:06 +08:00
|
|
|
TEST(ParsedASTTest, DoesNotGetIncludedTopDecls) {
|
2019-07-01 19:49:01 +08:00
|
|
|
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-09-04 17:46:06 +08:00
|
|
|
TEST(ParsedASTTest, DoesNotGetImplicitTemplateTopDecls) {
|
2019-08-08 15:21:06 +08:00
|
|
|
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")));
|
|
|
|
}
|
|
|
|
|
2019-09-04 17:46:06 +08:00
|
|
|
TEST(ParsedASTTest,
|
2019-08-08 15:21:06 +08:00
|
|
|
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";
|
2019-10-13 21:15:27 +08:00
|
|
|
// FIXME: Auto-completion in a template requires disabling delayed template
|
|
|
|
// parsing.
|
|
|
|
TU.ExtraArgs.push_back("-fno-delayed-template-parsing");
|
2019-08-08 15:21:06 +08:00
|
|
|
|
|
|
|
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>"))}));
|
|
|
|
}
|
|
|
|
|
2020-04-25 09:03:01 +08:00
|
|
|
TEST(ParsedASTTest, IgnoresDelayedTemplateParsing) {
|
|
|
|
auto TU = TestTU::withCode(R"cpp(
|
|
|
|
template <typename T> void xxx() {
|
|
|
|
int yyy = 0;
|
|
|
|
}
|
|
|
|
)cpp");
|
|
|
|
TU.ExtraArgs.push_back("-fdelayed-template-parsing");
|
|
|
|
auto AST = TU.build();
|
|
|
|
EXPECT_EQ(Decl::Var, findUnqualifiedDecl(AST, "yyy").getKind());
|
|
|
|
}
|
|
|
|
|
2019-09-04 17:46:06 +08:00
|
|
|
TEST(ParsedASTTest, TokensAfterPreamble) {
|
2019-06-19 22:03:19 +08:00
|
|
|
TestTU TU;
|
|
|
|
TU.AdditionalFiles["foo.h"] = R"(
|
|
|
|
int foo();
|
|
|
|
)";
|
|
|
|
TU.Code = R"cpp(
|
|
|
|
#include "foo.h"
|
|
|
|
first_token;
|
|
|
|
void test() {
|
[clangd] Errors in TestTU cause test failures unless suppressed with error-ok.
Summary:
The historic behavior of TestTU is to gather diagnostics and otherwise ignore
them. So if a test has a syntax error, and doesn't assert diagnostics, it
silently misbehaves.
This can be annoying when developing tests, as evidenced by various tests
gaining "assert no diagnostics" where that's not really the point of the test.
This patch aims to make that default behavior. For the first error
(not warning), TestTU will call ADD_FAILURE().
This can be suppressed with a comment containing "error-ok". For now that will
suppress any errors in the TU. We can make this stricter later -verify style.
(-verify itself is hard to reuse because of DiagnosticConsumer interfaces...)
A magic-comment was chosen over a TestTU option because of table-driven tests.
In addition to the behavior change, this patch:
- adds //error-ok where we're knowingly testing invalid code
(e.g. for diagnostics, crash-resilience, or token-level tests)
- fixes a bunch of errors in the checked-in tests, mostly trivial (missing ;)
- removes a bunch of now-redundant instances of "assert no diagnostics"
Reviewers: kadircet
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D73199
2020-01-22 23:38:41 +08:00
|
|
|
// error-ok: invalid syntax, just examining token stream
|
2019-06-19 22:03:19 +08:00
|
|
|
}
|
|
|
|
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-09-04 17:46:06 +08:00
|
|
|
TEST(ParsedASTTest, NoCrashOnTokensWithTidyCheck) {
|
2019-07-10 17:28:35 +08:00
|
|
|
TestTU TU;
|
|
|
|
// this check runs the preprocessor, we need to make sure it does not break
|
|
|
|
// our recording logic.
|
2020-11-26 02:35:34 +08:00
|
|
|
TU.ClangTidyProvider = addTidyChecks("modernize-use-trailing-return-type");
|
2019-07-10 17:28:35 +08:00
|
|
|
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), "}");
|
|
|
|
}
|
|
|
|
|
2019-09-04 17:46:06 +08:00
|
|
|
TEST(ParsedASTTest, CanBuildInvocationWithUnknownArgs) {
|
2020-06-18 00:09:54 +08:00
|
|
|
MockFS FS;
|
|
|
|
FS.Files = {{testPath("foo.cpp"), "void test() {}"}};
|
2019-08-27 18:02:18 +08:00
|
|
|
// Unknown flags should not prevent a build of compiler invocation.
|
|
|
|
ParseInputs Inputs;
|
2020-06-18 00:09:54 +08:00
|
|
|
Inputs.TFS = &FS;
|
2019-08-27 18:02:18 +08:00
|
|
|
Inputs.CompileCommand.CommandLine = {"clang", "-fsome-unknown-flag",
|
|
|
|
testPath("foo.cpp")};
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
IgnoreDiagnostics IgnoreDiags;
|
|
|
|
EXPECT_NE(buildCompilerInvocation(Inputs, IgnoreDiags), nullptr);
|
2019-08-27 18:02:18 +08:00
|
|
|
|
|
|
|
// Unknown forwarded to -cc1 should not a failure either.
|
|
|
|
Inputs.CompileCommand.CommandLine = {
|
|
|
|
"clang", "-Xclang", "-fsome-unknown-flag", testPath("foo.cpp")};
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
EXPECT_NE(buildCompilerInvocation(Inputs, IgnoreDiags), nullptr);
|
2019-08-27 18:02:18 +08:00
|
|
|
}
|
|
|
|
|
2019-09-04 17:46:06 +08:00
|
|
|
TEST(ParsedASTTest, CollectsMainFileMacroExpansions) {
|
2019-08-30 17:33:27 +08:00
|
|
|
Annotations TestCase(R"cpp(
|
2019-09-24 19:14:06 +08:00
|
|
|
#define ^MACRO_ARGS(X, Y) X Y
|
|
|
|
// - preamble ends
|
2019-08-30 17:33:27 +08:00
|
|
|
^ID(int A);
|
|
|
|
// Macro arguments included.
|
[clangd] Errors in TestTU cause test failures unless suppressed with error-ok.
Summary:
The historic behavior of TestTU is to gather diagnostics and otherwise ignore
them. So if a test has a syntax error, and doesn't assert diagnostics, it
silently misbehaves.
This can be annoying when developing tests, as evidenced by various tests
gaining "assert no diagnostics" where that's not really the point of the test.
This patch aims to make that default behavior. For the first error
(not warning), TestTU will call ADD_FAILURE().
This can be suppressed with a comment containing "error-ok". For now that will
suppress any errors in the TU. We can make this stricter later -verify style.
(-verify itself is hard to reuse because of DiagnosticConsumer interfaces...)
A magic-comment was chosen over a TestTU option because of table-driven tests.
In addition to the behavior change, this patch:
- adds //error-ok where we're knowingly testing invalid code
(e.g. for diagnostics, crash-resilience, or token-level tests)
- fixes a bunch of errors in the checked-in tests, mostly trivial (missing ;)
- removes a bunch of now-redundant instances of "assert no diagnostics"
Reviewers: kadircet
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D73199
2020-01-22 23:38:41 +08:00
|
|
|
^MACRO_ARGS(^MACRO_ARGS(^MACRO_EXP(int), E), ^ID(= 2));
|
2019-08-30 17:33:27 +08:00
|
|
|
|
|
|
|
// Macro names inside other macros not included.
|
2019-09-10 18:10:36 +08:00
|
|
|
#define ^MACRO_ARGS2(X, Y) X Y
|
|
|
|
#define ^FOO BAR
|
|
|
|
#define ^BAR 1
|
[clangd] Errors in TestTU cause test failures unless suppressed with error-ok.
Summary:
The historic behavior of TestTU is to gather diagnostics and otherwise ignore
them. So if a test has a syntax error, and doesn't assert diagnostics, it
silently misbehaves.
This can be annoying when developing tests, as evidenced by various tests
gaining "assert no diagnostics" where that's not really the point of the test.
This patch aims to make that default behavior. For the first error
(not warning), TestTU will call ADD_FAILURE().
This can be suppressed with a comment containing "error-ok". For now that will
suppress any errors in the TU. We can make this stricter later -verify style.
(-verify itself is hard to reuse because of DiagnosticConsumer interfaces...)
A magic-comment was chosen over a TestTU option because of table-driven tests.
In addition to the behavior change, this patch:
- adds //error-ok where we're knowingly testing invalid code
(e.g. for diagnostics, crash-resilience, or token-level tests)
- fixes a bunch of errors in the checked-in tests, mostly trivial (missing ;)
- removes a bunch of now-redundant instances of "assert no diagnostics"
Reviewers: kadircet
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D73199
2020-01-22 23:38:41 +08:00
|
|
|
int F = ^FOO;
|
2019-08-30 17:33:27 +08:00
|
|
|
|
|
|
|
// Macros from token concatenations not included.
|
2019-09-10 18:10:36 +08:00
|
|
|
#define ^CONCAT(X) X##A()
|
|
|
|
#define ^PREPEND(X) MACRO##X()
|
|
|
|
#define ^MACROA() 123
|
[clangd] Errors in TestTU cause test failures unless suppressed with error-ok.
Summary:
The historic behavior of TestTU is to gather diagnostics and otherwise ignore
them. So if a test has a syntax error, and doesn't assert diagnostics, it
silently misbehaves.
This can be annoying when developing tests, as evidenced by various tests
gaining "assert no diagnostics" where that's not really the point of the test.
This patch aims to make that default behavior. For the first error
(not warning), TestTU will call ADD_FAILURE().
This can be suppressed with a comment containing "error-ok". For now that will
suppress any errors in the TU. We can make this stricter later -verify style.
(-verify itself is hard to reuse because of DiagnosticConsumer interfaces...)
A magic-comment was chosen over a TestTU option because of table-driven tests.
In addition to the behavior change, this patch:
- adds //error-ok where we're knowingly testing invalid code
(e.g. for diagnostics, crash-resilience, or token-level tests)
- fixes a bunch of errors in the checked-in tests, mostly trivial (missing ;)
- removes a bunch of now-redundant instances of "assert no diagnostics"
Reviewers: kadircet
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D73199
2020-01-22 23:38:41 +08:00
|
|
|
int G = ^CONCAT(MACRO);
|
|
|
|
int H = ^PREPEND(A);
|
2019-08-30 17:33:27 +08:00
|
|
|
|
|
|
|
// Macros included not from preamble not included.
|
|
|
|
#include "foo.inc"
|
|
|
|
|
[clangd] Errors in TestTU cause test failures unless suppressed with error-ok.
Summary:
The historic behavior of TestTU is to gather diagnostics and otherwise ignore
them. So if a test has a syntax error, and doesn't assert diagnostics, it
silently misbehaves.
This can be annoying when developing tests, as evidenced by various tests
gaining "assert no diagnostics" where that's not really the point of the test.
This patch aims to make that default behavior. For the first error
(not warning), TestTU will call ADD_FAILURE().
This can be suppressed with a comment containing "error-ok". For now that will
suppress any errors in the TU. We can make this stricter later -verify style.
(-verify itself is hard to reuse because of DiagnosticConsumer interfaces...)
A magic-comment was chosen over a TestTU option because of table-driven tests.
In addition to the behavior change, this patch:
- adds //error-ok where we're knowingly testing invalid code
(e.g. for diagnostics, crash-resilience, or token-level tests)
- fixes a bunch of errors in the checked-in tests, mostly trivial (missing ;)
- removes a bunch of now-redundant instances of "assert no diagnostics"
Reviewers: kadircet
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D73199
2020-01-22 23:38:41 +08:00
|
|
|
int printf(const char*, ...);
|
|
|
|
void exit(int);
|
2019-09-10 18:10:36 +08:00
|
|
|
#define ^assert(COND) if (!(COND)) { printf("%s", #COND); exit(0); }
|
2019-08-30 17:33:27 +08:00
|
|
|
|
|
|
|
void test() {
|
|
|
|
// Includes macro expansions in arguments that are expressions
|
|
|
|
^assert(0 <= ^BAR);
|
|
|
|
}
|
2019-11-07 19:14:38 +08:00
|
|
|
|
|
|
|
#ifdef ^UNDEFINED
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define ^MULTIPLE_DEFINITION 1
|
|
|
|
#undef ^MULTIPLE_DEFINITION
|
|
|
|
|
|
|
|
#define ^MULTIPLE_DEFINITION 2
|
|
|
|
#undef ^MULTIPLE_DEFINITION
|
2019-08-30 17:33:27 +08:00
|
|
|
)cpp");
|
|
|
|
auto TU = TestTU::withCode(TestCase.code());
|
|
|
|
TU.HeaderCode = R"cpp(
|
|
|
|
#define ID(X) X
|
|
|
|
#define MACRO_EXP(X) ID(X)
|
|
|
|
MACRO_EXP(int B);
|
|
|
|
)cpp";
|
|
|
|
TU.AdditionalFiles["foo.inc"] = R"cpp(
|
|
|
|
int C = ID(1);
|
|
|
|
#define DEF 1
|
|
|
|
int D = DEF;
|
|
|
|
)cpp";
|
|
|
|
ParsedAST AST = TU.build();
|
|
|
|
std::vector<Position> MacroExpansionPositions;
|
2019-11-07 19:14:38 +08:00
|
|
|
for (const auto &SIDToRefs : AST.getMacros().MacroRefs) {
|
|
|
|
for (const auto &R : SIDToRefs.second)
|
|
|
|
MacroExpansionPositions.push_back(R.start);
|
|
|
|
}
|
|
|
|
for (const auto &R : AST.getMacros().UnknownMacros)
|
2019-09-24 19:14:06 +08:00
|
|
|
MacroExpansionPositions.push_back(R.start);
|
|
|
|
EXPECT_THAT(MacroExpansionPositions,
|
|
|
|
testing::UnorderedElementsAreArray(TestCase.points()));
|
2019-08-30 17:33:27 +08:00
|
|
|
}
|
|
|
|
|
2020-06-02 17:52:55 +08:00
|
|
|
MATCHER_P(WithFileName, Inc, "") { return arg.FileName == Inc; }
|
|
|
|
|
2020-02-20 00:36:41 +08:00
|
|
|
TEST(ParsedASTTest, ReplayPreambleForTidyCheckers) {
|
|
|
|
struct Inclusion {
|
|
|
|
Inclusion(const SourceManager &SM, SourceLocation HashLoc,
|
|
|
|
const Token &IncludeTok, llvm::StringRef FileName, bool IsAngled,
|
|
|
|
CharSourceRange FilenameRange)
|
|
|
|
: HashOffset(SM.getDecomposedLoc(HashLoc).second), IncTok(IncludeTok),
|
|
|
|
IncDirective(IncludeTok.getIdentifierInfo()->getName()),
|
|
|
|
FileNameOffset(SM.getDecomposedLoc(FilenameRange.getBegin()).second),
|
|
|
|
FileName(FileName), IsAngled(IsAngled) {}
|
|
|
|
size_t HashOffset;
|
|
|
|
syntax::Token IncTok;
|
|
|
|
llvm::StringRef IncDirective;
|
|
|
|
size_t FileNameOffset;
|
|
|
|
llvm::StringRef FileName;
|
|
|
|
bool IsAngled;
|
|
|
|
};
|
|
|
|
static std::vector<Inclusion> Includes;
|
|
|
|
static std::vector<syntax::Token> SkippedFiles;
|
|
|
|
struct ReplayPreamblePPCallback : public PPCallbacks {
|
|
|
|
const SourceManager &SM;
|
|
|
|
explicit ReplayPreamblePPCallback(const SourceManager &SM) : SM(SM) {}
|
|
|
|
|
|
|
|
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
|
|
|
|
StringRef FileName, bool IsAngled,
|
|
|
|
CharSourceRange FilenameRange, const FileEntry *,
|
|
|
|
StringRef, StringRef, const Module *,
|
|
|
|
SrcMgr::CharacteristicKind) override {
|
|
|
|
Includes.emplace_back(SM, HashLoc, IncludeTok, FileName, IsAngled,
|
|
|
|
FilenameRange);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileSkipped(const FileEntryRef &, const Token &FilenameTok,
|
|
|
|
SrcMgr::CharacteristicKind) override {
|
|
|
|
SkippedFiles.emplace_back(FilenameTok);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
struct ReplayPreambleCheck : public tidy::ClangTidyCheck {
|
|
|
|
ReplayPreambleCheck(StringRef Name, tidy::ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context) {}
|
|
|
|
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
|
|
|
|
Preprocessor *ModuleExpanderPP) override {
|
|
|
|
PP->addPPCallbacks(::std::make_unique<ReplayPreamblePPCallback>(SM));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
struct ReplayPreambleModule : public tidy::ClangTidyModule {
|
|
|
|
void
|
|
|
|
addCheckFactories(tidy::ClangTidyCheckFactories &CheckFactories) override {
|
|
|
|
CheckFactories.registerCheck<ReplayPreambleCheck>(
|
|
|
|
"replay-preamble-check");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static tidy::ClangTidyModuleRegistry::Add<ReplayPreambleModule> X(
|
|
|
|
"replay-preamble-module", "");
|
|
|
|
TestTU TU;
|
|
|
|
// This check records inclusion directives replayed by clangd.
|
2020-11-26 02:35:34 +08:00
|
|
|
TU.ClangTidyProvider = addTidyChecks("replay-preamble-check");
|
2020-02-20 00:36:41 +08:00
|
|
|
llvm::Annotations Test(R"cpp(
|
|
|
|
$hash^#$include[[import]] $filebegin^"$filerange[[bar.h]]"
|
|
|
|
$hash^#$include[[include_next]] $filebegin^"$filerange[[baz.h]]"
|
|
|
|
$hash^#$include[[include]] $filebegin^<$filerange[[a.h]]>)cpp");
|
|
|
|
llvm::StringRef Code = Test.code();
|
|
|
|
TU.Code = Code.str();
|
|
|
|
TU.AdditionalFiles["bar.h"] = "";
|
|
|
|
TU.AdditionalFiles["baz.h"] = "";
|
|
|
|
TU.AdditionalFiles["a.h"] = "";
|
2020-03-04 19:07:13 +08:00
|
|
|
// Since we are also testing #import directives, and they don't make much
|
|
|
|
// sense in c++ (also they actually break on windows), just set language to
|
|
|
|
// obj-c.
|
|
|
|
TU.ExtraArgs = {"-isystem.", "-xobjective-c"};
|
2020-02-20 00:36:41 +08:00
|
|
|
|
|
|
|
const auto &AST = TU.build();
|
|
|
|
const auto &SM = AST.getSourceManager();
|
|
|
|
|
|
|
|
auto HashLocs = Test.points("hash");
|
|
|
|
ASSERT_EQ(HashLocs.size(), Includes.size());
|
|
|
|
auto IncludeRanges = Test.ranges("include");
|
|
|
|
ASSERT_EQ(IncludeRanges.size(), Includes.size());
|
|
|
|
auto FileBeginLocs = Test.points("filebegin");
|
|
|
|
ASSERT_EQ(FileBeginLocs.size(), Includes.size());
|
|
|
|
auto FileRanges = Test.ranges("filerange");
|
|
|
|
ASSERT_EQ(FileRanges.size(), Includes.size());
|
|
|
|
|
|
|
|
ASSERT_EQ(SkippedFiles.size(), Includes.size());
|
|
|
|
for (size_t I = 0; I < Includes.size(); ++I) {
|
|
|
|
const auto &Inc = Includes[I];
|
|
|
|
|
|
|
|
EXPECT_EQ(Inc.HashOffset, HashLocs[I]);
|
|
|
|
|
|
|
|
auto IncRange = IncludeRanges[I];
|
|
|
|
EXPECT_THAT(Inc.IncTok.range(SM), RangeIs(IncRange));
|
|
|
|
EXPECT_EQ(Inc.IncTok.kind(), tok::identifier);
|
|
|
|
EXPECT_EQ(Inc.IncDirective,
|
|
|
|
Code.substr(IncRange.Begin, IncRange.End - IncRange.Begin));
|
|
|
|
|
|
|
|
EXPECT_EQ(Inc.FileNameOffset, FileBeginLocs[I]);
|
|
|
|
EXPECT_EQ(Inc.IsAngled, Code[FileBeginLocs[I]] == '<');
|
|
|
|
|
|
|
|
auto FileRange = FileRanges[I];
|
|
|
|
EXPECT_EQ(Inc.FileName,
|
|
|
|
Code.substr(FileRange.Begin, FileRange.End - FileRange.Begin));
|
|
|
|
|
|
|
|
EXPECT_EQ(SM.getDecomposedLoc(SkippedFiles[I].location()).second,
|
|
|
|
Inc.FileNameOffset);
|
|
|
|
// This also contains quotes/angles so increment the range by one from both
|
|
|
|
// sides.
|
|
|
|
EXPECT_EQ(
|
|
|
|
SkippedFiles[I].text(SM),
|
|
|
|
Code.substr(FileRange.Begin - 1, FileRange.End - FileRange.Begin + 2));
|
|
|
|
EXPECT_EQ(SkippedFiles[I].kind(), tok::header_name);
|
|
|
|
}
|
2020-06-02 17:52:55 +08:00
|
|
|
|
2020-06-17 03:21:45 +08:00
|
|
|
TU.AdditionalFiles["a.h"] = "";
|
|
|
|
TU.AdditionalFiles["b.h"] = "";
|
|
|
|
TU.AdditionalFiles["c.h"] = "";
|
2020-06-02 17:52:55 +08:00
|
|
|
// Make sure replay logic works with patched preambles.
|
2020-06-17 03:21:45 +08:00
|
|
|
llvm::StringLiteral Baseline = R"cpp(
|
|
|
|
#include "a.h"
|
|
|
|
#include "c.h")cpp";
|
2020-06-17 17:53:32 +08:00
|
|
|
MockFS FS;
|
2020-06-17 03:21:45 +08:00
|
|
|
TU.Code = Baseline.str();
|
2020-06-05 00:26:52 +08:00
|
|
|
auto Inputs = TU.inputs(FS);
|
2020-06-17 03:21:45 +08:00
|
|
|
auto BaselinePreamble = TU.preamble();
|
|
|
|
ASSERT_TRUE(BaselinePreamble);
|
|
|
|
|
|
|
|
// First make sure we don't crash on various modifications to the preamble.
|
|
|
|
llvm::StringLiteral Cases[] = {
|
|
|
|
// clang-format off
|
|
|
|
// New include in middle.
|
|
|
|
R"cpp(
|
|
|
|
#include "a.h"
|
|
|
|
#include "b.h"
|
|
|
|
#include "c.h")cpp",
|
|
|
|
// New include at top.
|
|
|
|
R"cpp(
|
|
|
|
#include "b.h"
|
|
|
|
#include "a.h"
|
|
|
|
#include "c.h")cpp",
|
|
|
|
// New include at bottom.
|
|
|
|
R"cpp(
|
|
|
|
#include "a.h"
|
|
|
|
#include "c.h"
|
|
|
|
#include "b.h")cpp",
|
|
|
|
// Same size with a missing include.
|
|
|
|
R"cpp(
|
|
|
|
#include "a.h"
|
|
|
|
#include "b.h")cpp",
|
|
|
|
// Smaller with no new includes.
|
|
|
|
R"cpp(
|
|
|
|
#include "a.h")cpp",
|
|
|
|
// Smaller with a new includes.
|
|
|
|
R"cpp(
|
|
|
|
#include "b.h")cpp",
|
|
|
|
// clang-format on
|
|
|
|
};
|
|
|
|
for (llvm::StringLiteral Case : Cases) {
|
|
|
|
TU.Code = Case.str();
|
|
|
|
|
|
|
|
IgnoreDiagnostics Diags;
|
|
|
|
auto CI = buildCompilerInvocation(TU.inputs(FS), Diags);
|
|
|
|
auto PatchedAST = ParsedAST::build(testPath(TU.Filename), TU.inputs(FS),
|
|
|
|
std::move(CI), {}, BaselinePreamble);
|
|
|
|
ASSERT_TRUE(PatchedAST);
|
|
|
|
EXPECT_TRUE(PatchedAST->getDiagnostics().empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then ensure correctness by making sure includes were seen only once.
|
|
|
|
// Note that we first see the includes from the patch, as preamble includes
|
|
|
|
// are replayed after exiting the built-in file.
|
2020-06-02 17:52:55 +08:00
|
|
|
Includes.clear();
|
2020-06-17 03:21:45 +08:00
|
|
|
TU.Code = R"cpp(
|
|
|
|
#include "a.h"
|
|
|
|
#include "b.h")cpp";
|
|
|
|
IgnoreDiagnostics Diags;
|
|
|
|
auto CI = buildCompilerInvocation(TU.inputs(FS), Diags);
|
2020-06-05 00:26:52 +08:00
|
|
|
auto PatchedAST = ParsedAST::build(testPath(TU.Filename), TU.inputs(FS),
|
2020-06-17 03:21:45 +08:00
|
|
|
std::move(CI), {}, BaselinePreamble);
|
2020-06-02 17:52:55 +08:00
|
|
|
ASSERT_TRUE(PatchedAST);
|
2020-06-17 03:21:45 +08:00
|
|
|
EXPECT_TRUE(PatchedAST->getDiagnostics().empty());
|
2020-06-02 17:52:55 +08:00
|
|
|
EXPECT_THAT(Includes,
|
|
|
|
ElementsAre(WithFileName(testPath("__preamble_patch__.h")),
|
2020-06-17 03:21:45 +08:00
|
|
|
WithFileName("b.h"), WithFileName("a.h")));
|
2020-02-20 00:36:41 +08:00
|
|
|
}
|
|
|
|
|
2020-04-02 16:53:45 +08:00
|
|
|
TEST(ParsedASTTest, PatchesAdditionalIncludes) {
|
|
|
|
llvm::StringLiteral ModifiedContents = R"cpp(
|
|
|
|
#include "baz.h"
|
|
|
|
#include "foo.h"
|
|
|
|
#include "sub/aux.h"
|
|
|
|
void bar() {
|
|
|
|
foo();
|
|
|
|
baz();
|
|
|
|
aux();
|
|
|
|
})cpp";
|
|
|
|
// Build expected ast with symbols coming from headers.
|
|
|
|
TestTU TU;
|
|
|
|
TU.Filename = "foo.cpp";
|
|
|
|
TU.AdditionalFiles["foo.h"] = "void foo();";
|
|
|
|
TU.AdditionalFiles["sub/baz.h"] = "void baz();";
|
|
|
|
TU.AdditionalFiles["sub/aux.h"] = "void aux();";
|
|
|
|
TU.ExtraArgs = {"-I" + testPath("sub")};
|
|
|
|
TU.Code = ModifiedContents.str();
|
|
|
|
auto ExpectedAST = TU.build();
|
|
|
|
|
|
|
|
// Build preamble with no includes.
|
|
|
|
TU.Code = "";
|
|
|
|
StoreDiags Diags;
|
2020-06-17 17:53:32 +08:00
|
|
|
MockFS FS;
|
2020-06-05 00:26:52 +08:00
|
|
|
auto Inputs = TU.inputs(FS);
|
2020-04-02 16:53:45 +08:00
|
|
|
auto CI = buildCompilerInvocation(Inputs, Diags);
|
|
|
|
auto EmptyPreamble =
|
|
|
|
buildPreamble(testPath("foo.cpp"), *CI, Inputs, true, nullptr);
|
|
|
|
ASSERT_TRUE(EmptyPreamble);
|
|
|
|
EXPECT_THAT(EmptyPreamble->Includes.MainFileIncludes, testing::IsEmpty());
|
|
|
|
|
|
|
|
// Now build an AST using empty preamble and ensure patched includes worked.
|
|
|
|
TU.Code = ModifiedContents.str();
|
2020-06-05 00:26:52 +08:00
|
|
|
Inputs = TU.inputs(FS);
|
2020-04-02 16:53:45 +08:00
|
|
|
auto PatchedAST = ParsedAST::build(testPath("foo.cpp"), Inputs, std::move(CI),
|
|
|
|
{}, EmptyPreamble);
|
|
|
|
ASSERT_TRUE(PatchedAST);
|
|
|
|
ASSERT_TRUE(PatchedAST->getDiagnostics().empty());
|
|
|
|
|
|
|
|
// Ensure source location information is correct, including resolved paths.
|
|
|
|
EXPECT_THAT(PatchedAST->getIncludeStructure().MainFileIncludes,
|
|
|
|
testing::Pointwise(
|
|
|
|
EqInc(), ExpectedAST.getIncludeStructure().MainFileIncludes));
|
|
|
|
auto StringMapToVector = [](const llvm::StringMap<unsigned> SM) {
|
|
|
|
std::vector<std::pair<std::string, unsigned>> Res;
|
|
|
|
for (const auto &E : SM)
|
|
|
|
Res.push_back({E.first().str(), E.second});
|
|
|
|
llvm::sort(Res);
|
|
|
|
return Res;
|
|
|
|
};
|
|
|
|
// Ensure file proximity signals are correct.
|
|
|
|
EXPECT_EQ(StringMapToVector(PatchedAST->getIncludeStructure().includeDepth(
|
|
|
|
testPath("foo.cpp"))),
|
|
|
|
StringMapToVector(ExpectedAST.getIncludeStructure().includeDepth(
|
|
|
|
testPath("foo.cpp"))));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ParsedASTTest, PatchesDeletedIncludes) {
|
|
|
|
TestTU TU;
|
|
|
|
TU.Filename = "foo.cpp";
|
|
|
|
TU.Code = "";
|
|
|
|
auto ExpectedAST = TU.build();
|
|
|
|
|
|
|
|
// Build preamble with no includes.
|
|
|
|
TU.Code = R"cpp(#include <foo.h>)cpp";
|
|
|
|
StoreDiags Diags;
|
2020-06-17 17:53:32 +08:00
|
|
|
MockFS FS;
|
2020-06-05 00:26:52 +08:00
|
|
|
auto Inputs = TU.inputs(FS);
|
2020-04-02 16:53:45 +08:00
|
|
|
auto CI = buildCompilerInvocation(Inputs, Diags);
|
|
|
|
auto BaselinePreamble =
|
|
|
|
buildPreamble(testPath("foo.cpp"), *CI, Inputs, true, nullptr);
|
|
|
|
ASSERT_TRUE(BaselinePreamble);
|
|
|
|
EXPECT_THAT(BaselinePreamble->Includes.MainFileIncludes,
|
|
|
|
ElementsAre(testing::Field(&Inclusion::Written, "<foo.h>")));
|
|
|
|
|
|
|
|
// Now build an AST using additional includes and check that locations are
|
|
|
|
// correctly parsed.
|
|
|
|
TU.Code = "";
|
2020-06-05 00:26:52 +08:00
|
|
|
Inputs = TU.inputs(FS);
|
2020-04-02 16:53:45 +08:00
|
|
|
auto PatchedAST = ParsedAST::build(testPath("foo.cpp"), Inputs, std::move(CI),
|
|
|
|
{}, BaselinePreamble);
|
|
|
|
ASSERT_TRUE(PatchedAST);
|
|
|
|
|
|
|
|
// Ensure source location information is correct.
|
|
|
|
EXPECT_THAT(PatchedAST->getIncludeStructure().MainFileIncludes,
|
|
|
|
testing::Pointwise(
|
|
|
|
EqInc(), ExpectedAST.getIncludeStructure().MainFileIncludes));
|
|
|
|
auto StringMapToVector = [](const llvm::StringMap<unsigned> SM) {
|
|
|
|
std::vector<std::pair<std::string, unsigned>> Res;
|
|
|
|
for (const auto &E : SM)
|
|
|
|
Res.push_back({E.first().str(), E.second});
|
|
|
|
llvm::sort(Res);
|
|
|
|
return Res;
|
|
|
|
};
|
|
|
|
// Ensure file proximity signals are correct.
|
|
|
|
EXPECT_EQ(StringMapToVector(PatchedAST->getIncludeStructure().includeDepth(
|
|
|
|
testPath("foo.cpp"))),
|
|
|
|
StringMapToVector(ExpectedAST.getIncludeStructure().includeDepth(
|
|
|
|
testPath("foo.cpp"))));
|
|
|
|
}
|
|
|
|
|
2018-01-26 01:29:17 +08:00
|
|
|
} // namespace
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|