diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -19,6 +19,7 @@ #include "clang/Tooling/Inclusions/IncludeStyle.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/Support/Regex.h" +#include "llvm/Support/SourceMgr.h" #include namespace llvm { @@ -3269,9 +3270,11 @@ private: FormatStyleSet StyleSet; - friend std::error_code parseConfiguration(llvm::MemoryBufferRef Config, - FormatStyle *Style, - bool AllowUnknownOptions); + friend std::error_code + parseConfiguration(llvm::MemoryBufferRef Config, FormatStyle *Style, + bool AllowUnknownOptions, + llvm::SourceMgr::DiagHandlerTy DiagHandler, + void *DiagHandlerCtxt); }; /// Returns a format style complying with the LLVM coding standards: @@ -3329,9 +3332,13 @@ /// /// If AllowUnknownOptions is true, no errors are emitted if unknown /// format options are occured. -std::error_code parseConfiguration(llvm::MemoryBufferRef Config, - FormatStyle *Style, - bool AllowUnknownOptions = false); +/// +/// If set all diagnostics are emitted through the DiagHandler. +std::error_code +parseConfiguration(llvm::MemoryBufferRef Config, FormatStyle *Style, + bool AllowUnknownOptions = false, + llvm::SourceMgr::DiagHandlerTy DiagHandler = nullptr, + void *DiagHandlerCtx = nullptr); /// Like above but accepts an unnamed buffer. inline std::error_code parseConfiguration(StringRef Config, FormatStyle *Style, diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1394,8 +1394,9 @@ } std::error_code parseConfiguration(llvm::MemoryBufferRef Config, - FormatStyle *Style, - bool AllowUnknownOptions) { + FormatStyle *Style, bool AllowUnknownOptions, + llvm::SourceMgr::DiagHandlerTy DiagHandler, + void *DiagHandlerCtxt) { assert(Style); FormatStyle::LanguageKind Language = Style->Language; assert(Language != FormatStyle::LK_None); @@ -1403,7 +1404,8 @@ return make_error_code(ParseError::Error); Style->StyleSet.Clear(); std::vector Styles; - llvm::yaml::Input Input(Config); + llvm::yaml::Input Input(Config, /*Ctxt=*/nullptr, DiagHandler, + DiagHandlerCtxt); // DocumentListTraits> uses the context to get default // values for the fields, keys for which are missing from the configuration. // Mapping also uses the context to get the language to find the correct @@ -2990,6 +2992,8 @@ FilesToLookFor.push_back(".clang-format"); FilesToLookFor.push_back("_clang-format"); + auto dropDiagnosticHandler = [](const llvm::SMDiagnostic &, void *) {}; + for (StringRef Directory = Path; !Directory.empty(); Directory = llvm::sys::path::parent_path(Directory)) { @@ -3034,7 +3038,8 @@ LLVM_DEBUG(llvm::dbgs() << "Applying child configurations\n"); for (const auto& MemBuf : llvm::reverse(ChildFormatTextToApply)){ - auto Ec = parseConfiguration(*MemBuf, &Style, AllowUnknownOptions); + auto Ec = parseConfiguration(*MemBuf, &Style, AllowUnknownOptions, + dropDiagnosticHandler); // It was already correctly parsed. assert(!Ec); static_cast(Ec); @@ -3069,8 +3074,9 @@ LLVM_DEBUG(llvm::dbgs() << "Applying child configuration on fallback style\n"); - auto Ec = parseConfiguration(*ChildFormatTextToApply.front(), - &FallbackStyle, AllowUnknownOptions); + auto Ec = + parseConfiguration(*ChildFormatTextToApply.front(), &FallbackStyle, + AllowUnknownOptions, dropDiagnosticHandler); // It was already correctly parsed. assert(!Ec); static_cast(Ec);