Index: clangd/ClangdUnit.cpp =================================================================== --- clangd/ClangdUnit.cpp +++ clangd/ClangdUnit.cpp @@ -26,7 +26,6 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/SmallVector.h" #include "llvm/Support/CrashRecoveryContext.h" -#include "llvm/Support/Format.h" #include "llvm/Support/raw_ostream.h" #include @@ -178,27 +177,15 @@ llvm::Optional toClangdDiag(const clang::Diagnostic &D, DiagnosticsEngine::Level Level, const LangOptions &LangOpts) { - SmallString<64> Message; - D.FormatDiagnostic(Message); - if (!D.hasSourceManager() || !D.getLocation().isValid() || !D.getSourceManager().isInMainFile(D.getLocation())) { - - SmallString<64> Location; - if (D.hasSourceManager() && D.getLocation().isValid()) { - auto &SourceMgr = D.getSourceManager(); - auto Loc = SourceMgr.getFileLoc(D.getLocation()); - llvm::raw_svector_ostream OS(Location); - Loc.print(OS, SourceMgr); - } else { - Location = ""; - } - - log(llvm::formatv("Ignored diagnostic outside main file. {0}: {1}", - Location, Message)); + IgnoreDiagnostics::log(Level, D); return llvm::None; } + SmallString<64> Message; + D.FormatDiagnostic(Message); + DiagWithFixIts Result; Result.Diag.range = diagnosticRange(D, LangOpts); Result.Diag.severity = getSeverity(Level); Index: clangd/Compiler.h =================================================================== --- clangd/Compiler.h +++ clangd/Compiler.h @@ -24,8 +24,11 @@ class IgnoreDiagnostics : public DiagnosticConsumer { public: + static void log(DiagnosticsEngine::Level DiagLevel, + const clang::Diagnostic &Info); + void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, - const clang::Diagnostic &Info) override {} + const clang::Diagnostic &Info) override; }; /// Creates a compiler instance, configured so that: Index: clangd/Compiler.cpp =================================================================== --- clangd/Compiler.cpp +++ clangd/Compiler.cpp @@ -8,12 +8,37 @@ //===---------------------------------------------------------------------===// #include "Compiler.h" +#include "Logger.h" #include "clang/Basic/TargetInfo.h" #include "clang/Lex/PreprocessorOptions.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/FormatVariadic.h" namespace clang { namespace clangd { +void IgnoreDiagnostics::log(DiagnosticsEngine::Level DiagLevel, + const clang::Diagnostic &Info) { + SmallString<64> Message; + Info.FormatDiagnostic(Message); + + SmallString<64> Location; + if (Info.hasSourceManager() && Info.getLocation().isValid()) { + auto &SourceMgr = Info.getSourceManager(); + auto Loc = SourceMgr.getFileLoc(Info.getLocation()); + llvm::raw_svector_ostream OS(Location); + Loc.print(OS, SourceMgr); + OS << ":"; + } + + clangd::log(llvm::formatv("Ignored diagnostic. {0}{1}", Location, Message)); +} + +void IgnoreDiagnostics::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, + const clang::Diagnostic &Info) { + IgnoreDiagnostics::log(DiagLevel, Info); +} + std::unique_ptr prepareCompilerInstance(std::unique_ptr CI, const PrecompiledPreamble *Preamble,