2018-02-16 22:15:55 +08:00
|
|
|
//===-- HeadersTests.cpp - Include headers unit 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-02-16 22:15:55 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Headers.h"
|
2018-05-15 23:29:32 +08:00
|
|
|
|
|
|
|
#include "Compiler.h"
|
2018-02-16 22:15:55 +08:00
|
|
|
#include "TestFS.h"
|
2018-11-20 18:58:48 +08:00
|
|
|
#include "TestTU.h"
|
2018-05-15 23:29:32 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
|
|
|
#include "clang/Frontend/FrontendActions.h"
|
|
|
|
#include "clang/Lex/PreprocessorOptions.h"
|
2019-04-24 17:42:53 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2018-02-16 22:15:55 +08:00
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
namespace {
|
|
|
|
|
2018-05-15 23:29:32 +08:00
|
|
|
using ::testing::AllOf;
|
2018-11-20 18:58:48 +08:00
|
|
|
using ::testing::ElementsAre;
|
2018-05-15 23:29:32 +08:00
|
|
|
using ::testing::UnorderedElementsAre;
|
|
|
|
|
2018-02-16 22:15:55 +08:00
|
|
|
class HeadersTest : public ::testing::Test {
|
|
|
|
public:
|
|
|
|
HeadersTest() {
|
|
|
|
CDB.ExtraClangFlags = {SearchDirArg.c_str()};
|
|
|
|
FS.Files[MainFile] = "";
|
2018-05-15 23:29:32 +08:00
|
|
|
// Make sure directory sub/ exists.
|
|
|
|
FS.Files[testPath("sub/EMPTY")] = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<CompilerInstance> setupClang() {
|
|
|
|
auto Cmd = CDB.getCompileCommand(MainFile);
|
|
|
|
assert(static_cast<bool>(Cmd));
|
|
|
|
auto VFS = FS.getFileSystem();
|
|
|
|
VFS->setCurrentWorkingDirectory(Cmd->Directory);
|
|
|
|
|
2018-12-14 21:49:00 +08:00
|
|
|
ParseInputs PI;
|
|
|
|
PI.CompileCommand = *Cmd;
|
|
|
|
PI.FS = VFS;
|
[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
|
|
|
auto CI = buildCompilerInvocation(PI, IgnoreDiags);
|
2018-05-15 23:29:32 +08:00
|
|
|
EXPECT_TRUE(static_cast<bool>(CI));
|
|
|
|
// The diagnostic options must be set before creating a CompilerInstance.
|
|
|
|
CI->getDiagnosticOpts().IgnoreWarnings = true;
|
|
|
|
auto Clang = prepareCompilerInstance(
|
|
|
|
std::move(CI), /*Preamble=*/nullptr,
|
2019-04-04 20:56:03 +08:00
|
|
|
llvm::MemoryBuffer::getMemBuffer(FS.Files[MainFile], MainFile), VFS,
|
|
|
|
IgnoreDiags);
|
2018-05-15 23:29:32 +08:00
|
|
|
|
|
|
|
EXPECT_FALSE(Clang->getFrontendOpts().Inputs.empty());
|
|
|
|
return Clang;
|
2018-02-16 22:15:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-07-03 16:09:29 +08:00
|
|
|
IncludeStructure collectIncludes() {
|
2018-05-15 23:29:32 +08:00
|
|
|
auto Clang = setupClang();
|
|
|
|
PreprocessOnlyAction Action;
|
|
|
|
EXPECT_TRUE(
|
|
|
|
Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0]));
|
2018-07-03 16:09:29 +08:00
|
|
|
IncludeStructure Includes;
|
|
|
|
Clang->getPreprocessor().addPPCallbacks(
|
|
|
|
collectIncludeStructureCallback(Clang->getSourceManager(), &Includes));
|
2019-06-27 03:50:12 +08:00
|
|
|
EXPECT_FALSE(Action.Execute());
|
2018-05-15 23:29:32 +08:00
|
|
|
Action.EndSourceFile();
|
2018-07-03 16:09:29 +08:00
|
|
|
return Includes;
|
2018-05-15 23:29:32 +08:00
|
|
|
}
|
|
|
|
|
2018-06-15 21:34:18 +08:00
|
|
|
// Calculates the include path, or returns "" on error or header should not be
|
|
|
|
// inserted.
|
2018-02-26 16:32:13 +08:00
|
|
|
std::string calculate(PathRef Original, PathRef Preferred = "",
|
2018-06-15 21:34:18 +08:00
|
|
|
const std::vector<Inclusion> &Inclusions = {}) {
|
2018-05-15 23:29:32 +08:00
|
|
|
auto Clang = setupClang();
|
|
|
|
PreprocessOnlyAction Action;
|
|
|
|
EXPECT_TRUE(
|
|
|
|
Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0]));
|
|
|
|
|
2018-02-26 16:32:13 +08:00
|
|
|
if (Preferred.empty())
|
|
|
|
Preferred = Original;
|
2019-01-07 23:45:19 +08:00
|
|
|
auto ToHeaderFile = [](llvm::StringRef Header) {
|
2018-02-26 16:32:13 +08:00
|
|
|
return HeaderFile{Header,
|
2019-01-07 23:45:19 +08:00
|
|
|
/*Verbatim=*/!llvm::sys::path::is_absolute(Header)};
|
2018-02-26 16:32:13 +08:00
|
|
|
};
|
2018-05-15 23:29:32 +08:00
|
|
|
|
2018-06-15 21:34:18 +08:00
|
|
|
IncludeInserter Inserter(MainFile, /*Code=*/"", format::getLLVMStyle(),
|
|
|
|
CDB.getCompileCommand(MainFile)->Directory,
|
2019-04-11 17:36:36 +08:00
|
|
|
&Clang->getPreprocessor().getHeaderSearchInfo());
|
2018-06-15 21:34:18 +08:00
|
|
|
for (const auto &Inc : Inclusions)
|
|
|
|
Inserter.addExisting(Inc);
|
|
|
|
auto Inserted = ToHeaderFile(Preferred);
|
2019-04-16 22:35:49 +08:00
|
|
|
if (!Inserter.shouldInsertInclude(Original, Inserted))
|
2018-06-15 21:34:18 +08:00
|
|
|
return "";
|
2019-07-09 02:07:46 +08:00
|
|
|
auto Path = Inserter.calculateIncludePath(Inserted, MainFile);
|
2018-05-15 23:29:32 +08:00
|
|
|
Action.EndSourceFile();
|
2019-07-09 02:07:46 +08:00
|
|
|
return Path.getValueOr("");
|
2018-02-16 22:15:55 +08:00
|
|
|
}
|
2018-05-15 23:29:32 +08:00
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Optional<TextEdit> insert(llvm::StringRef VerbatimHeader) {
|
2018-05-15 23:29:32 +08:00
|
|
|
auto Clang = setupClang();
|
|
|
|
PreprocessOnlyAction Action;
|
|
|
|
EXPECT_TRUE(
|
|
|
|
Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0]));
|
|
|
|
|
|
|
|
IncludeInserter Inserter(MainFile, /*Code=*/"", format::getLLVMStyle(),
|
|
|
|
CDB.getCompileCommand(MainFile)->Directory,
|
2019-04-11 17:36:36 +08:00
|
|
|
&Clang->getPreprocessor().getHeaderSearchInfo());
|
2018-06-15 21:34:18 +08:00
|
|
|
auto Edit = Inserter.insert(VerbatimHeader);
|
2018-05-15 23:29:32 +08:00
|
|
|
Action.EndSourceFile();
|
|
|
|
return Edit;
|
|
|
|
}
|
|
|
|
|
2018-02-16 22:15:55 +08:00
|
|
|
MockFSProvider FS;
|
|
|
|
MockCompilationDatabase CDB;
|
|
|
|
std::string MainFile = testPath("main.cpp");
|
|
|
|
std::string Subdir = testPath("sub");
|
2019-01-07 23:45:19 +08:00
|
|
|
std::string SearchDirArg = (llvm::Twine("-I") + Subdir).str();
|
2018-05-15 23:29:32 +08:00
|
|
|
IgnoringDiagConsumer IgnoreDiags;
|
2018-02-16 22:15:55 +08:00
|
|
|
};
|
|
|
|
|
2018-05-15 23:29:32 +08:00
|
|
|
MATCHER_P(Written, Name, "") { return arg.Written == Name; }
|
|
|
|
MATCHER_P(Resolved, Name, "") { return arg.Resolved == Name; }
|
2018-11-20 18:58:48 +08:00
|
|
|
MATCHER_P(IncludeLine, N, "") { return arg.R.start.line == N; }
|
2018-02-16 22:15:55 +08:00
|
|
|
|
2018-07-03 16:09:29 +08:00
|
|
|
MATCHER_P2(Distance, File, D, "") {
|
|
|
|
if (arg.getKey() != File)
|
|
|
|
*result_listener << "file =" << arg.getKey().str();
|
|
|
|
if (arg.getValue() != D)
|
|
|
|
*result_listener << "distance =" << arg.getValue();
|
|
|
|
return arg.getKey() == File && arg.getValue() == D;
|
|
|
|
}
|
|
|
|
|
2018-05-15 23:29:32 +08:00
|
|
|
TEST_F(HeadersTest, CollectRewrittenAndResolved) {
|
2018-02-16 22:15:55 +08:00
|
|
|
FS.Files[MainFile] = R"cpp(
|
2018-05-15 23:29:32 +08:00
|
|
|
#include "sub/bar.h" // not shortest
|
2018-02-16 22:15:55 +08:00
|
|
|
)cpp";
|
|
|
|
std::string BarHeader = testPath("sub/bar.h");
|
|
|
|
FS.Files[BarHeader] = "";
|
|
|
|
|
2018-07-03 16:09:29 +08:00
|
|
|
EXPECT_THAT(collectIncludes().MainFileIncludes,
|
2018-05-15 23:29:32 +08:00
|
|
|
UnorderedElementsAre(
|
|
|
|
AllOf(Written("\"sub/bar.h\""), Resolved(BarHeader))));
|
2018-07-03 16:09:29 +08:00
|
|
|
EXPECT_THAT(collectIncludes().includeDepth(MainFile),
|
|
|
|
UnorderedElementsAre(Distance(MainFile, 0u),
|
|
|
|
Distance(testPath("sub/bar.h"), 1u)));
|
2018-02-26 16:32:13 +08:00
|
|
|
}
|
|
|
|
|
2018-05-15 23:29:32 +08:00
|
|
|
TEST_F(HeadersTest, OnlyCollectInclusionsInMain) {
|
2018-02-16 22:15:55 +08:00
|
|
|
std::string BazHeader = testPath("sub/baz.h");
|
|
|
|
FS.Files[BazHeader] = "";
|
|
|
|
std::string BarHeader = testPath("sub/bar.h");
|
|
|
|
FS.Files[BarHeader] = R"cpp(
|
|
|
|
#include "baz.h"
|
|
|
|
)cpp";
|
|
|
|
FS.Files[MainFile] = R"cpp(
|
|
|
|
#include "bar.h"
|
|
|
|
)cpp";
|
2018-05-15 23:29:32 +08:00
|
|
|
EXPECT_THAT(
|
2018-07-03 16:09:29 +08:00
|
|
|
collectIncludes().MainFileIncludes,
|
2018-05-15 23:29:32 +08:00
|
|
|
UnorderedElementsAre(AllOf(Written("\"bar.h\""), Resolved(BarHeader))));
|
2018-07-03 16:09:29 +08:00
|
|
|
EXPECT_THAT(collectIncludes().includeDepth(MainFile),
|
|
|
|
UnorderedElementsAre(Distance(MainFile, 0u),
|
|
|
|
Distance(testPath("sub/bar.h"), 1u),
|
|
|
|
Distance(testPath("sub/baz.h"), 2u)));
|
|
|
|
// includeDepth() also works for non-main files.
|
|
|
|
EXPECT_THAT(collectIncludes().includeDepth(testPath("sub/bar.h")),
|
|
|
|
UnorderedElementsAre(Distance(testPath("sub/bar.h"), 0u),
|
|
|
|
Distance(testPath("sub/baz.h"), 1u)));
|
2018-05-15 23:29:32 +08:00
|
|
|
}
|
|
|
|
|
2018-11-20 18:58:48 +08:00
|
|
|
TEST_F(HeadersTest, PreambleIncludesPresentOnce) {
|
|
|
|
// We use TestTU here, to ensure we use the preamble replay logic.
|
|
|
|
// We're testing that the logic doesn't crash, and doesn't result in duplicate
|
|
|
|
// includes. (We'd test more directly, but it's pretty well encapsulated!)
|
|
|
|
auto TU = TestTU::withCode(R"cpp(
|
|
|
|
#include "a.h"
|
2019-07-03 15:47:19 +08:00
|
|
|
|
2018-11-20 18:58:48 +08:00
|
|
|
#include "a.h"
|
|
|
|
void foo();
|
|
|
|
#include "a.h"
|
|
|
|
)cpp");
|
|
|
|
TU.HeaderFilename = "a.h"; // suppress "not found".
|
|
|
|
EXPECT_THAT(TU.build().getIncludeStructure().MainFileIncludes,
|
2019-07-03 15:47:19 +08:00
|
|
|
ElementsAre(IncludeLine(1), IncludeLine(3), IncludeLine(5)));
|
2018-11-20 18:58:48 +08:00
|
|
|
}
|
|
|
|
|
2018-05-15 23:29:32 +08:00
|
|
|
TEST_F(HeadersTest, UnResolvedInclusion) {
|
|
|
|
FS.Files[MainFile] = R"cpp(
|
|
|
|
#include "foo.h"
|
|
|
|
)cpp";
|
|
|
|
|
2018-07-03 16:09:29 +08:00
|
|
|
EXPECT_THAT(collectIncludes().MainFileIncludes,
|
2018-05-15 23:29:32 +08:00
|
|
|
UnorderedElementsAre(AllOf(Written("\"foo.h\""), Resolved(""))));
|
2018-07-03 16:09:29 +08:00
|
|
|
EXPECT_THAT(collectIncludes().includeDepth(MainFile),
|
|
|
|
UnorderedElementsAre(Distance(MainFile, 0u)));
|
2018-05-15 23:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(HeadersTest, InsertInclude) {
|
|
|
|
std::string Path = testPath("sub/bar.h");
|
|
|
|
FS.Files[Path] = "";
|
|
|
|
EXPECT_EQ(calculate(Path), "\"bar.h\"");
|
2018-02-20 02:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(HeadersTest, DoNotInsertIfInSameFile) {
|
|
|
|
MainFile = testPath("main.h");
|
|
|
|
EXPECT_EQ(calculate(MainFile), "");
|
2018-02-16 22:15:55 +08:00
|
|
|
}
|
|
|
|
|
2019-07-09 02:07:46 +08:00
|
|
|
TEST_F(HeadersTest, DoNotInsertOffIncludePath) {
|
|
|
|
MainFile = testPath("sub/main.cpp");
|
|
|
|
EXPECT_EQ(calculate(testPath("sub2/main.cpp")), "");
|
|
|
|
}
|
|
|
|
|
2019-07-03 15:47:19 +08:00
|
|
|
TEST_F(HeadersTest, ShortenIncludesInSearchPath) {
|
2018-05-15 23:29:32 +08:00
|
|
|
std::string BarHeader = testPath("sub/bar.h");
|
|
|
|
EXPECT_EQ(calculate(BarHeader), "\"bar.h\"");
|
2019-04-24 17:23:31 +08:00
|
|
|
|
|
|
|
SearchDirArg = (llvm::Twine("-I") + Subdir + "/..").str();
|
|
|
|
CDB.ExtraClangFlags = {SearchDirArg.c_str()};
|
|
|
|
BarHeader = testPath("sub/bar.h");
|
|
|
|
EXPECT_EQ(calculate(BarHeader), "\"sub/bar.h\"");
|
2018-05-15 23:29:32 +08:00
|
|
|
}
|
|
|
|
|
2019-07-03 15:47:19 +08:00
|
|
|
TEST_F(HeadersTest, ShortenedIncludeNotInSearchPath) {
|
2019-04-24 17:42:53 +08:00
|
|
|
std::string BarHeader =
|
|
|
|
llvm::sys::path::convert_to_slash(testPath("sub-2/bar.h"));
|
2019-07-03 15:47:19 +08:00
|
|
|
EXPECT_EQ(calculate(BarHeader, ""), "\"sub-2/bar.h\"");
|
2018-05-15 23:29:32 +08:00
|
|
|
}
|
|
|
|
|
2018-02-26 16:32:13 +08:00
|
|
|
TEST_F(HeadersTest, PreferredHeader) {
|
|
|
|
std::string BarHeader = testPath("sub/bar.h");
|
|
|
|
EXPECT_EQ(calculate(BarHeader, "<bar>"), "<bar>");
|
|
|
|
|
|
|
|
std::string BazHeader = testPath("sub/baz.h");
|
|
|
|
EXPECT_EQ(calculate(BarHeader, BazHeader), "\"baz.h\"");
|
|
|
|
}
|
|
|
|
|
2018-05-15 23:29:32 +08:00
|
|
|
TEST_F(HeadersTest, DontInsertDuplicatePreferred) {
|
2018-11-20 18:58:48 +08:00
|
|
|
Inclusion Inc;
|
|
|
|
Inc.Written = "\"bar.h\"";
|
|
|
|
Inc.Resolved = "";
|
|
|
|
EXPECT_EQ(calculate(testPath("sub/bar.h"), "\"bar.h\"", {Inc}), "");
|
|
|
|
EXPECT_EQ(calculate("\"x.h\"", "\"bar.h\"", {Inc}), "");
|
2018-05-15 23:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(HeadersTest, DontInsertDuplicateResolved) {
|
2018-11-20 18:58:48 +08:00
|
|
|
Inclusion Inc;
|
|
|
|
Inc.Written = "fake-bar.h";
|
|
|
|
Inc.Resolved = testPath("sub/bar.h");
|
|
|
|
EXPECT_EQ(calculate(Inc.Resolved, "", {Inc}), "");
|
2018-05-15 23:29:32 +08:00
|
|
|
// Do not insert preferred.
|
2018-11-20 18:58:48 +08:00
|
|
|
EXPECT_EQ(calculate(Inc.Resolved, "\"BAR.h\"", {Inc}), "");
|
2018-05-15 23:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(HeadersTest, PreferInserted) {
|
2018-06-15 21:34:18 +08:00
|
|
|
auto Edit = insert("<y>");
|
|
|
|
EXPECT_TRUE(Edit.hasValue());
|
2018-10-20 23:30:37 +08:00
|
|
|
EXPECT_TRUE(StringRef(Edit->newText).contains("<y>"));
|
2018-05-15 23:29:32 +08:00
|
|
|
}
|
|
|
|
|
2019-04-11 17:36:36 +08:00
|
|
|
TEST(Headers, NoHeaderSearchInfo) {
|
|
|
|
std::string MainFile = testPath("main.cpp");
|
|
|
|
IncludeInserter Inserter(MainFile, /*Code=*/"", format::getLLVMStyle(),
|
|
|
|
/*BuildDir=*/"", /*HeaderSearchInfo=*/nullptr);
|
|
|
|
|
|
|
|
auto HeaderPath = testPath("sub/bar.h");
|
|
|
|
auto Inserting = HeaderFile{HeaderPath, /*Verbatim=*/false};
|
|
|
|
auto Verbatim = HeaderFile{"<x>", /*Verbatim=*/true};
|
|
|
|
|
2019-07-03 15:47:19 +08:00
|
|
|
EXPECT_EQ(Inserter.calculateIncludePath(Inserting, MainFile),
|
2019-07-09 02:07:46 +08:00
|
|
|
std::string("\"sub/bar.h\""));
|
2019-04-16 22:35:49 +08:00
|
|
|
EXPECT_EQ(Inserter.shouldInsertInclude(HeaderPath, Inserting), false);
|
2019-04-11 17:36:36 +08:00
|
|
|
|
2019-07-09 02:07:46 +08:00
|
|
|
EXPECT_EQ(Inserter.calculateIncludePath(Verbatim, MainFile),
|
|
|
|
std::string("<x>"));
|
2019-04-16 22:35:49 +08:00
|
|
|
EXPECT_EQ(Inserter.shouldInsertInclude(HeaderPath, Verbatim), true);
|
2019-07-09 02:07:46 +08:00
|
|
|
|
|
|
|
EXPECT_EQ(Inserter.calculateIncludePath(Inserting, "sub2/main2.cpp"),
|
|
|
|
llvm::None);
|
2019-04-11 17:36:36 +08:00
|
|
|
}
|
|
|
|
|
2018-02-16 22:15:55 +08:00
|
|
|
} // namespace
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|