[clang] e730940 - [clang-format] Suppress diagnostics on second parse

Björn Schäpers via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 5 12:43:20 PST 2021


Author: Björn Schäpers
Date: 2021-03-05T21:42:45+01:00
New Revision: e7309404536cf7f389afc8678688f9e3726f1eb8

URL: https://github.com/llvm/llvm-project/commit/e7309404536cf7f389afc8678688f9e3726f1eb8
DIFF: https://github.com/llvm/llvm-project/commit/e7309404536cf7f389afc8678688f9e3726f1eb8.diff

LOG: [clang-format] Suppress diagnostics on second parse

This amends 25f753c51e7b17bfca08155c1d777c5667110970.

When applying the child configurations we don't need any diagnostic,
because it was issued when first parsing them. So just drop everything
on the second parse.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 2bb2e670cb14..1e8456b0dfd8 100644
--- 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 <system_error>
 
 namespace llvm {
@@ -3320,9 +3321,11 @@ struct FormatStyle {
 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:
@@ -3380,9 +3383,13 @@ bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language,
 ///
 /// 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
index fd8080fdd2fa..b815a5255296 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1398,8 +1398,9 @@ bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language,
 }
 
 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);
@@ -1407,7 +1408,8 @@ std::error_code parseConfiguration(llvm::MemoryBufferRef Config,
     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);
   // 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
@@ -2994,6 +2996,8 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
   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)) {
 
@@ -3038,7 +3042,8 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
           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);
@@ -3073,8 +3078,9 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
     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);


        


More information about the cfe-commits mailing list