[PATCH] D96760: [clang-format] Suppress diagnostics on second parse

Björn Schäpers via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 16 01:19:38 PST 2021


HazardyKnusperkeks created this revision.
HazardyKnusperkeks added reviewers: njames93, MyDeveloperDay, curdeius.
HazardyKnusperkeks added a project: clang-format.
HazardyKnusperkeks requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This is the follow up to D93844 <https://reviews.llvm.org/D93844>.

Suppress the diagnostics when applying the child configurations, since they were printed on the first parse.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D96760

Files:
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp


Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -1394,8 +1394,8 @@
 }
 
 std::error_code parseConfiguration(llvm::MemoryBufferRef Config,
-                                   FormatStyle *Style,
-                                   bool AllowUnknownOptions) {
+                                   FormatStyle *Style, bool AllowUnknownOptions,
+                                   llvm::SourceMgr::DiagHandlerTy DiagHandler) {
   assert(Style);
   FormatStyle::LanguageKind Language = Style->Language;
   assert(Language != FormatStyle::LK_None);
@@ -1403,7 +1403,8 @@
     return make_error_code(ParseError::Error);
   Style->StyleSet.Clear();
   std::vector<FormatStyle> Styles;
-  llvm::yaml::Input Input(Config);
+  llvm::yaml::Input Input(Config, /*Ctxt=*/nullptr, DiagHandler,
+                          /*DiagHandlerCtxt=*/nullptr);
   // DocumentListTraits<vector<FormatStyle>> 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 +2991,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 +3037,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<void>(Ec);
@@ -3069,8 +3073,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<void>(Ec);
Index: clang/include/clang/Format/Format.h
===================================================================
--- clang/include/clang/Format/Format.h
+++ 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 <system_error>
 
 namespace llvm {
@@ -3269,9 +3270,10 @@
 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);
 };
 
 /// Returns a format style complying with the LLVM coding standards:
@@ -3329,9 +3331,12 @@
 ///
 /// 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);
 
 /// Like above but accepts an unnamed buffer.
 inline std::error_code parseConfiguration(StringRef Config, FormatStyle *Style,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D96760.323912.patch
Type: text/x-patch
Size: 4246 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210216/4f919fa1/attachment.bin>


More information about the cfe-commits mailing list