[clang] a92cf5a - [clang-format] [PR42014,PR52021] don't let clang-format assert/crash when file being formatted is read-only/locked

via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 15 01:47:49 PDT 2021


Author: mydeveloperday
Date: 2021-10-15T09:46:35+01:00
New Revision: a92cf5a5a0cd01145f8db2ae09334a8b43a1271b

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

LOG: [clang-format] [PR42014,PR52021] don't let clang-format assert/crash when file being formatted is read-only/locked

This is a bug which gets reported from time to time and we've had multiple attempts to fix it, but don't want to fix it by adding frontEnd to the mix.

This patch aim to find a trivial, but not that sophisticated way of emitting the error without the additional impact of adding libFrontEnd to clang-format.
See {D90121} for analysis of why we don't want those previous attempts

Reviewed By: HazardyKnusperkeks

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

Added: 
    

Modified: 
    clang/tools/clang-format/ClangFormat.cpp

Removed: 
    


################################################################################
diff  --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp
index 04e0bd2d69530..56dc628869a4d 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -366,6 +366,18 @@ static void outputXML(const Replacements &Replaces,
   outs() << "</replacements>\n";
 }
 
+class ClangFormatDiagConsumer : public DiagnosticConsumer {
+  virtual void anchor() {}
+
+  void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
+                        const Diagnostic &Info) override {
+
+    SmallVector<char, 16> vec;
+    Info.FormatDiagnostic(vec);
+    errs() << "clang-format error:" << vec << "\n";
+  }
+};
+
 // Returns true on error.
 static bool format(StringRef FileName) {
   if (!OutputXML && Inplace && FileName == "-") {
@@ -477,9 +489,12 @@ static bool format(StringRef FileName) {
     IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
         new llvm::vfs::InMemoryFileSystem);
     FileManager Files(FileSystemOptions(), InMemoryFileSystem);
+
+    IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions());
+    ClangFormatDiagConsumer IgnoreDiagnostics;
     DiagnosticsEngine Diagnostics(
-        IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
-        new DiagnosticOptions);
+        IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), &*DiagOpts,
+        &IgnoreDiagnostics, false);
     SourceManager Sources(Diagnostics, Files);
     FileID ID = createInMemoryFile(AssumedFileName, *Code, Sources, Files,
                                    InMemoryFileSystem.get());


        


More information about the cfe-commits mailing list