summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clangd/unittests
diff options
context:
space:
mode:
Diffstat (limited to 'clang-tools-extra/clangd/unittests')
-rw-r--r--clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp6
-rw-r--r--clang-tools-extra/clangd/unittests/FileIndexTests.cpp4
-rw-r--r--clang-tools-extra/clangd/unittests/HeadersTests.cpp2
-rw-r--r--clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp67
-rw-r--r--clang-tools-extra/clangd/unittests/TestTU.cpp8
5 files changed, 79 insertions, 8 deletions
diff --git a/clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp b/clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp
index 7fe57025dc7..430a056c1ea 100644
--- a/clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp
+++ b/clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp
@@ -10,6 +10,7 @@
#include "Annotations.h"
#include "ClangdUnit.h"
#include "Compiler.h"
+#include "Diagnostics.h"
#include "SourceCode.h"
#include "TestFS.h"
#include "TestTU.h"
@@ -252,12 +253,13 @@ TEST(ClangdUnitTest, CanBuildInvocationWithUnknownArgs) {
Inputs.FS = buildTestFS({{testPath("foo.cpp"), "void test() {}"}});
Inputs.CompileCommand.CommandLine = {"clang", "-fsome-unknown-flag",
testPath("foo.cpp")};
- EXPECT_NE(buildCompilerInvocation(Inputs), nullptr);
+ IgnoreDiagnostics IgnoreDiags;
+ EXPECT_NE(buildCompilerInvocation(Inputs, IgnoreDiags), nullptr);
// Unknown forwarded to -cc1 should not a failure either.
Inputs.CompileCommand.CommandLine = {
"clang", "-Xclang", "-fsome-unknown-flag", testPath("foo.cpp")};
- EXPECT_NE(buildCompilerInvocation(Inputs), nullptr);
+ EXPECT_NE(buildCompilerInvocation(Inputs, IgnoreDiags), nullptr);
}
} // namespace
diff --git a/clang-tools-extra/clangd/unittests/FileIndexTests.cpp b/clang-tools-extra/clangd/unittests/FileIndexTests.cpp
index f1f304f935e..f7b5ecafb8a 100644
--- a/clang-tools-extra/clangd/unittests/FileIndexTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FileIndexTests.cpp
@@ -9,6 +9,7 @@
#include "AST.h"
#include "Annotations.h"
#include "ClangdUnit.h"
+#include "Compiler.h"
#include "SyncAPI.h"
#include "TestFS.h"
#include "TestTU.h"
@@ -280,7 +281,8 @@ TEST(FileIndexTest, RebuildWithPreamble) {
)cpp";
// Rebuild the file.
- auto CI = buildCompilerInvocation(PI);
+ IgnoreDiagnostics IgnoreDiags;
+ auto CI = buildCompilerInvocation(PI, IgnoreDiags);
FileIndex Index;
bool IndexUpdated = false;
diff --git a/clang-tools-extra/clangd/unittests/HeadersTests.cpp b/clang-tools-extra/clangd/unittests/HeadersTests.cpp
index da701309e35..d07312ca588 100644
--- a/clang-tools-extra/clangd/unittests/HeadersTests.cpp
+++ b/clang-tools-extra/clangd/unittests/HeadersTests.cpp
@@ -46,7 +46,7 @@ private:
ParseInputs PI;
PI.CompileCommand = *Cmd;
PI.FS = VFS;
- auto CI = buildCompilerInvocation(PI);
+ auto CI = buildCompilerInvocation(PI, IgnoreDiags);
EXPECT_TRUE(static_cast<bool>(CI));
// The diagnostic options must be set before creating a CompilerInstance.
CI->getDiagnosticOpts().IgnoreWarnings = true;
diff --git a/clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp b/clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
index b605c940360..274c07f99cd 100644
--- a/clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
+++ b/clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
@@ -14,6 +14,9 @@
#include "Path.h"
#include "TUScheduler.h"
#include "TestFS.h"
+#include "Threading.h"
+#include "clang/Basic/DiagnosticDriver.h"
+#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/ScopeExit.h"
#include "gmock/gmock.h"
@@ -28,6 +31,9 @@ namespace {
using ::testing::AnyOf;
using ::testing::Each;
using ::testing::ElementsAre;
+using ::testing::Eq;
+using ::testing::Field;
+using ::testing::IsEmpty;
using ::testing::Pointee;
using ::testing::UnorderedElementsAre;
@@ -60,12 +66,22 @@ protected:
/// in updateWithDiags.
static std::unique_ptr<ParsingCallbacks> captureDiags() {
class CaptureDiags : public ParsingCallbacks {
+ public:
void onMainAST(PathRef File, ParsedAST &AST, PublishFn Publish) override {
- auto Diags = AST.getDiagnostics();
+ reportDiagnostics(File, AST.getDiagnostics(), Publish);
+ }
+
+ void onFailedAST(PathRef File, std::vector<Diag> Diags,
+ PublishFn Publish) override {
+ reportDiagnostics(File, Diags, Publish);
+ }
+
+ private:
+ void reportDiagnostics(PathRef File, llvm::ArrayRef<Diag> Diags,
+ PublishFn Publish) {
auto D = Context::current().get(DiagsCallbackKey);
if (!D)
return;
-
Publish([&]() {
const_cast<
llvm::unique_function<void(PathRef, std::vector<Diag>)> &> (*D)(
@@ -720,6 +736,53 @@ TEST_F(TUSchedulerTests, TUStatus) {
TUState(TUAction::Idle, /*No action*/ "")));
}
+TEST_F(TUSchedulerTests, CommandLineErrors) {
+ // We should see errors from command-line parsing inside the main file.
+ CDB.ExtraClangFlags = {"-fsome-unknown-flag"};
+
+ TUScheduler S(CDB, /*AsyncThreadsCount=*/getDefaultAsyncThreadsCount(),
+ /*StorePreambleInMemory=*/true, /*ASTCallbacks=*/captureDiags(),
+ /*UpdateDebounce=*/std::chrono::steady_clock::duration::zero(),
+ ASTRetentionPolicy());
+
+ Notification Ready;
+ std::vector<Diag> Diagnostics;
+ updateWithDiags(S, testPath("foo.cpp"), "void test() {}",
+ WantDiagnostics::Yes, [&](std::vector<Diag> D) {
+ Diagnostics = std::move(D);
+ Ready.notify();
+ });
+ Ready.wait();
+
+ EXPECT_THAT(
+ Diagnostics,
+ ElementsAre(AllOf(
+ Field(&Diag::ID, Eq(diag::err_drv_unknown_argument)),
+ Field(&Diag::Name, Eq("drv_unknown_argument")),
+ Field(&Diag::Message, "unknown argument: '-fsome-unknown-flag'"))));
+}
+
+TEST_F(TUSchedulerTests, CommandLineWarnings) {
+ // We should not see warnings from command-line parsing.
+ CDB.ExtraClangFlags = {"-Wsome-unknown-warning"};
+
+ TUScheduler S(CDB, /*AsyncThreadsCount=*/getDefaultAsyncThreadsCount(),
+ /*StorePreambleInMemory=*/true, /*ASTCallbacks=*/captureDiags(),
+ /*UpdateDebounce=*/std::chrono::steady_clock::duration::zero(),
+ ASTRetentionPolicy());
+
+ Notification Ready;
+ std::vector<Diag> Diagnostics;
+ updateWithDiags(S, testPath("foo.cpp"), "void test() {}",
+ WantDiagnostics::Yes, [&](std::vector<Diag> D) {
+ Diagnostics = std::move(D);
+ Ready.notify();
+ });
+ Ready.wait();
+
+ EXPECT_THAT(Diagnostics, IsEmpty());
+}
+
} // namespace
} // namespace clangd
} // namespace clang
diff --git a/clang-tools-extra/clangd/unittests/TestTU.cpp b/clang-tools-extra/clangd/unittests/TestTU.cpp
index 0c1727eccad..75393f1415b 100644
--- a/clang-tools-extra/clangd/unittests/TestTU.cpp
+++ b/clang-tools-extra/clangd/unittests/TestTU.cpp
@@ -7,6 +7,8 @@
//===----------------------------------------------------------------------===//
#include "TestTU.h"
+#include "Compiler.h"
+#include "Diagnostics.h"
#include "TestFS.h"
#include "index/FileIndex.h"
#include "index/MemIndex.h"
@@ -59,14 +61,16 @@ ParsedAST TestTU::build() const {
Inputs.Index = ExternalIndex;
if (Inputs.Index)
Inputs.Opts.SuggestMissingIncludes = true;
- auto CI = buildCompilerInvocation(Inputs);
+ StoreDiags Diags;
+ auto CI = buildCompilerInvocation(Inputs, Diags);
assert(CI && "Failed to build compilation invocation.");
auto Preamble =
buildPreamble(FullFilename, *CI,
/*OldPreamble=*/nullptr,
/*OldCompileCommand=*/Inputs.CompileCommand, Inputs,
/*StoreInMemory=*/true, /*PreambleCallback=*/nullptr);
- auto AST = buildAST(FullFilename, std::move(CI), Inputs, Preamble);
+ auto AST =
+ buildAST(FullFilename, std::move(CI), Diags.take(), Inputs, Preamble);
if (!AST.hasValue()) {
ADD_FAILURE() << "Failed to build code:\n" << Code;
llvm_unreachable("Failed to build TestTU!");
OpenPOWER on IntegriCloud