2018-08-15 00:03:32 +08:00
|
|
|
//===--- Diagnostics.h -------------------------------------------*- C++-*-===//
|
2018-03-12 23:28:22 +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-03-12 23:28:22 +08:00
|
|
|
//
|
2018-08-15 00:03:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-03-12 23:28:22 +08:00
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_DIAGNOSTICS_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_DIAGNOSTICS_H
|
|
|
|
|
|
|
|
#include "Path.h"
|
|
|
|
#include "Protocol.h"
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Basic/LangOptions.h"
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2019-04-29 18:25:44 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
#include "llvm/ADT/None.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
2018-03-12 23:28:22 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/StringSet.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace clang {
|
2019-04-17 20:35:16 +08:00
|
|
|
namespace tidy {
|
|
|
|
class ClangTidyContext;
|
|
|
|
} // namespace tidy
|
2018-03-12 23:28:22 +08:00
|
|
|
namespace clangd {
|
|
|
|
|
2018-08-11 01:25:07 +08:00
|
|
|
struct ClangdDiagnosticOptions {
|
|
|
|
/// If true, Clangd uses an LSP extension to embed the fixes with the
|
|
|
|
/// diagnostics that are sent to the client.
|
|
|
|
bool EmbedFixesInDiagnostics = false;
|
2018-08-23 04:30:06 +08:00
|
|
|
|
2019-04-18 23:17:07 +08:00
|
|
|
/// If true, Clangd uses the relatedInformation field to include other
|
|
|
|
/// locations (in particular attached notes).
|
|
|
|
/// Otherwise, these are flattened into the diagnostic message.
|
|
|
|
bool EmitRelatedLocations = false;
|
|
|
|
|
2018-08-23 04:30:06 +08:00
|
|
|
/// If true, Clangd uses an LSP extension to send the diagnostic's
|
|
|
|
/// category to the client. The category typically describes the compilation
|
|
|
|
/// stage during which the issue was produced, e.g. "Semantic Issue" or "Parse
|
|
|
|
/// Issue".
|
|
|
|
bool SendDiagnosticCategory = false;
|
2019-02-20 00:50:37 +08:00
|
|
|
|
|
|
|
/// If true, Clangd will add a number of available fixes to the diagnostic's
|
|
|
|
/// message.
|
|
|
|
bool DisplayFixesCount = true;
|
2018-08-11 01:25:07 +08:00
|
|
|
};
|
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
/// Contains basic information about a diagnostic.
|
|
|
|
struct DiagBase {
|
|
|
|
std::string Message;
|
|
|
|
// Intended to be used only in error messages.
|
|
|
|
// May be relative, absolute or even artifically constructed.
|
|
|
|
std::string File;
|
2019-04-18 23:17:07 +08:00
|
|
|
// Absolute path to containing file, if available.
|
|
|
|
llvm::Optional<std::string> AbsFile;
|
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
clangd::Range Range;
|
|
|
|
DiagnosticsEngine::Level Severity = DiagnosticsEngine::Note;
|
2018-08-15 06:21:40 +08:00
|
|
|
std::string Category;
|
2018-03-12 23:28:22 +08:00
|
|
|
// Since File is only descriptive, we store a separate flag to distinguish
|
|
|
|
// diags from the main file.
|
|
|
|
bool InsideMainFile = false;
|
|
|
|
};
|
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const DiagBase &D);
|
|
|
|
|
|
|
|
/// Represents a single fix-it that editor can apply to fix the error.
|
|
|
|
struct Fix {
|
|
|
|
/// Message for the fix-it.
|
|
|
|
std::string Message;
|
|
|
|
/// TextEdits from clang's fix-its. Must be non-empty.
|
|
|
|
llvm::SmallVector<TextEdit, 1> Edits;
|
|
|
|
};
|
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Fix &F);
|
|
|
|
|
|
|
|
/// Represents a note for the diagnostic. Severity of notes can only be 'note'
|
|
|
|
/// or 'remark'.
|
|
|
|
struct Note : DiagBase {};
|
|
|
|
|
|
|
|
/// A top-level diagnostic that may have Notes and Fixes.
|
|
|
|
struct Diag : DiagBase {
|
2019-04-17 20:35:16 +08:00
|
|
|
unsigned ID; // e.g. member of clang::diag, or clang-tidy assigned ID.
|
|
|
|
std::string Name; // if ID was recognized.
|
2019-03-06 18:51:38 +08:00
|
|
|
// The source of this diagnostic.
|
2019-04-17 20:35:16 +08:00
|
|
|
enum {
|
|
|
|
Unknown,
|
2019-03-06 18:51:38 +08:00
|
|
|
Clang,
|
|
|
|
ClangTidy,
|
2019-04-17 20:35:16 +08:00
|
|
|
} Source = Unknown;
|
2018-03-12 23:28:22 +08:00
|
|
|
/// 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.
|
|
|
|
std::vector<Fix> Fixes;
|
|
|
|
};
|
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Diag &D);
|
|
|
|
|
|
|
|
/// Conversion to LSP diagnostics. Formats the error message of each diagnostic
|
|
|
|
/// to include all its notes. Notes inside main file are also provided as
|
|
|
|
/// separate diagnostics with their corresponding fixits. Notes outside main
|
|
|
|
/// file do not have a corresponding LSP diagnostic, but can still be included
|
|
|
|
/// as part of their main diagnostic's message.
|
|
|
|
void toLSPDiags(
|
[clangd] Embed fixes as CodeAction, instead of clangd_fixes. Clean up serialization.
Summary:
CodeAction provides us with a standard way of representing fixes inline, so
use it, replacing our existing ad-hoc extension.
After this, it's easy to serialize diagnostics using the structured
toJSON/Protocol.h mechanism rather than assembling JSON ad-hoc.
Reviewers: hokein, arphaman
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53391
llvm-svn: 345119
2018-10-24 15:59:38 +08:00
|
|
|
const Diag &D, const URIForFile &File, const ClangdDiagnosticOptions &Opts,
|
2018-03-12 23:28:22 +08:00
|
|
|
llvm::function_ref<void(clangd::Diagnostic, llvm::ArrayRef<Fix>)> OutFn);
|
|
|
|
|
[clangd] Embed fixes as CodeAction, instead of clangd_fixes. Clean up serialization.
Summary:
CodeAction provides us with a standard way of representing fixes inline, so
use it, replacing our existing ad-hoc extension.
After this, it's easy to serialize diagnostics using the structured
toJSON/Protocol.h mechanism rather than assembling JSON ad-hoc.
Reviewers: hokein, arphaman
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53391
llvm-svn: 345119
2018-10-24 15:59:38 +08:00
|
|
|
/// Convert from Fix to LSP CodeAction.
|
|
|
|
CodeAction toCodeAction(const Fix &D, const URIForFile &File);
|
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
/// Convert from clang diagnostic level to LSP severity.
|
|
|
|
int getSeverity(DiagnosticsEngine::Level L);
|
|
|
|
|
|
|
|
/// StoreDiags collects the diagnostics that can later be reported by
|
|
|
|
/// clangd. It groups all notes for a diagnostic into a single Diag
|
|
|
|
/// and filters out diagnostics that don't mention the main file (i.e. neither
|
|
|
|
/// the diag itself nor its notes are in the main file).
|
|
|
|
class StoreDiags : public DiagnosticConsumer {
|
|
|
|
public:
|
2019-04-17 20:35:16 +08:00
|
|
|
// The ClangTidyContext populates Source and Name for clang-tidy diagnostics.
|
|
|
|
std::vector<Diag> take(const clang::tidy::ClangTidyContext *Tidy = nullptr);
|
2018-03-12 23:28:22 +08:00
|
|
|
|
|
|
|
void BeginSourceFile(const LangOptions &Opts, const Preprocessor *) override;
|
|
|
|
void EndSourceFile() override;
|
|
|
|
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
|
|
|
|
const clang::Diagnostic &Info) override;
|
|
|
|
|
2019-01-28 22:01:55 +08:00
|
|
|
using DiagFixer = std::function<std::vector<Fix>(DiagnosticsEngine::Level,
|
|
|
|
const clang::Diagnostic &)>;
|
2019-05-19 12:19:14 +08:00
|
|
|
using LevelAdjuster = std::function<DiagnosticsEngine::Level(
|
|
|
|
DiagnosticsEngine::Level, const clang::Diagnostic &)>;
|
2019-01-28 22:01:55 +08:00
|
|
|
/// If set, possibly adds fixes for diagnostics using \p Fixer.
|
|
|
|
void contributeFixes(DiagFixer Fixer) { this->Fixer = Fixer; }
|
2019-05-19 12:19:14 +08:00
|
|
|
/// If set, this allows the client of this class to adjust the level of
|
|
|
|
/// diagnostics, such as promoting warnings to errors, or ignoring
|
|
|
|
/// diagnostics.
|
|
|
|
void setLevelAdjuster(LevelAdjuster Adjuster) { this->Adjuster = Adjuster; }
|
2019-01-28 22:01:55 +08:00
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
private:
|
|
|
|
void flushLastDiag();
|
|
|
|
|
2019-01-28 22:01:55 +08:00
|
|
|
DiagFixer Fixer = nullptr;
|
2019-05-19 12:19:14 +08:00
|
|
|
LevelAdjuster Adjuster = nullptr;
|
2018-03-12 23:28:22 +08:00
|
|
|
std::vector<Diag> Output;
|
|
|
|
llvm::Optional<LangOptions> LangOpts;
|
|
|
|
llvm::Optional<Diag> LastDiag;
|
2019-07-30 18:26:51 +08:00
|
|
|
/// Set iff adjustDiagFromHeader resulted in changes to LastDiag.
|
|
|
|
bool LastDiagWasAdjusted = false;
|
2019-04-29 18:25:44 +08:00
|
|
|
llvm::DenseSet<int> IncludeLinesWithErrors;
|
2019-05-19 12:06:52 +08:00
|
|
|
bool LastPrimaryDiagnosticWasSuppressed = false;
|
2018-03-12 23:28:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
|
|
|
|
2018-08-15 00:03:32 +08:00
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_DIAGNOSTICS_H
|