Skip to content

Commit

Permalink
[clangd] Add Source to clangd::Diagnostic.
Browse files Browse the repository at this point in the history
Summary:
clangd embedder can distinguish diagnostics from clang or clang-tidy.

Reviewers: ilya-biryukov

Subscribers: ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D58600

llvm-svn: 355493
  • Loading branch information
hokein committed Mar 6, 2019
1 parent dd1ea8a commit 24a8f1c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
9 changes: 8 additions & 1 deletion clang-tools-extra/clangd/ClangdUnit.cpp
Expand Up @@ -372,6 +372,10 @@ ParsedAST::build(std::unique_ptr<CompilerInvocation> CI,
Clang->getPreprocessor().EndSourceFile();

std::vector<Diag> Diags = ASTDiags.take();
// Populate diagnostic source.
for (auto &D : Diags)
D.S =
!CTContext->getCheckName(D.ID).empty() ? Diag::ClangTidy : Diag::Clang;
// Add diagnostics from the preamble, if any.
if (Preamble)
Diags.insert(Diags.begin(), Preamble->Diags.begin(), Preamble->Diags.end());
Expand Down Expand Up @@ -539,8 +543,11 @@ buildPreamble(PathRef FileName, CompilerInvocation &CI,
if (BuiltPreamble) {
vlog("Built preamble of size {0} for file {1}", BuiltPreamble->getSize(),
FileName);
std::vector<Diag> Diags = PreambleDiagnostics.take();
for (auto &Diag : Diags)
Diag.S = Diag::Clang;
return std::make_shared<PreambleData>(
std::move(*BuiltPreamble), PreambleDiagnostics.take(),
std::move(*BuiltPreamble), std::move(Diags),
SerializedDeclsCollector.takeIncludes(), std::move(StatCache),
SerializedDeclsCollector.takeCanonicalIncludes());
} else {
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clangd/Diagnostics.cpp
Expand Up @@ -377,6 +377,7 @@ void StoreDiags::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
flushLastDiag();

LastDiag = Diag();
LastDiag->ID = Info.getID();
FillDiagBase(*LastDiag);

if (!Info.getFixItHints().empty())
Expand Down
8 changes: 8 additions & 0 deletions clang-tools-extra/clangd/Diagnostics.h
Expand Up @@ -68,6 +68,14 @@ struct Note : DiagBase {};

/// A top-level diagnostic that may have Notes and Fixes.
struct Diag : DiagBase {
// Diagnostic enum ID.
unsigned ID;
// The source of this diagnostic.
enum Source {
Clang,
ClangTidy,
};
Source S = Clang;
/// Elaborate on the problem, usually pointing to a related piece of code.
std::vector<Note> Notes;
/// *Alternative* fixes for this diagnostic, one should be chosen.
Expand Down
17 changes: 17 additions & 0 deletions clang-tools-extra/unittests/clangd/DiagnosticsTests.cpp
Expand Up @@ -58,6 +58,8 @@ MATCHER_P(EqualToLSPDiag, LSPDiag,
std::tie(LSPDiag.range, LSPDiag.severity, LSPDiag.message);
}

MATCHER_P(DiagSource, Source, "") { return arg.S == Source; }

MATCHER_P(EqualToFix, Fix, "LSP fix " + llvm::to_string(Fix)) {
if (arg.Message != Fix.Message)
return false;
Expand Down Expand Up @@ -102,6 +104,7 @@ o]]();
// This range spans lines.
AllOf(Diag(Test.range("typo"),
"use of undeclared identifier 'goo'; did you mean 'foo'?"),
DiagSource(Diag::Clang),
WithFix(
Fix(Test.range("typo"), "foo", "change 'go\\ o' to 'foo'")),
// This is a pretty normal range.
Expand Down Expand Up @@ -137,6 +140,18 @@ TEST(DiagnosticsTest, FlagsMatter) {
WithFix(Fix(Test.range(), "int", "change return type to 'int'")))));
}

TEST(DiagnosticsTest, DiagnosticPreamble) {
Annotations Test(R"cpp(
#include $[["not-found.h"]]
)cpp");

auto TU = TestTU::withCode(Test.code());
EXPECT_THAT(TU.build().getDiagnostics(),
ElementsAre(testing::AllOf(
Diag(Test.range(), "'not-found.h' file not found"),
DiagSource(Diag::Clang))));
}

TEST(DiagnosticsTest, ClangTidy) {
Annotations Test(R"cpp(
#include $deprecated[["assert.h"]]
Expand All @@ -159,6 +174,7 @@ TEST(DiagnosticsTest, ClangTidy) {
AllOf(Diag(Test.range("deprecated"),
"inclusion of deprecated C++ header 'assert.h'; consider "
"using 'cassert' instead [modernize-deprecated-headers]"),
DiagSource(Diag::ClangTidy),
WithFix(Fix(Test.range("deprecated"), "<cassert>",
"change '\"assert.h\"' to '<cassert>'"))),
Diag(Test.range("doubled"),
Expand All @@ -168,6 +184,7 @@ TEST(DiagnosticsTest, ClangTidy) {
Diag(Test.range("macroarg"),
"side effects in the 1st macro argument 'X' are repeated in "
"macro expansion [bugprone-macro-repeated-side-effects]"),
DiagSource(Diag::ClangTidy),
WithNote(Diag(Test.range("macrodef"),
"macro 'SQUARE' defined here "
"[bugprone-macro-repeated-side-effects]"))),
Expand Down

0 comments on commit 24a8f1c

Please sign in to comment.