[PATCH] D42395: [clang-format] Fix bug where -dump-config failed on ObjC header

Ben Hamilton via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 22 15:13:00 PST 2018


benhamilton updated this revision to Diff 130970.
benhamilton added a comment.

- Support AssumeFileName with stdin.


Repository:
  rC Clang

https://reviews.llvm.org/D42395

Files:
  test/Format/dump-config-cxx.h
  test/Format/dump-config-objc.h
  test/Format/lit.local.cfg
  tools/clang-format/ClangFormat.cpp


Index: tools/clang-format/ClangFormat.cpp
===================================================================
--- tools/clang-format/ClangFormat.cpp
+++ tools/clang-format/ClangFormat.cpp
@@ -357,10 +357,27 @@
   }
 
   if (DumpConfig) {
+    StringRef FileName;
+    std::unique_ptr<llvm::MemoryBuffer> Code;
+    if (FileNames.empty()) {
+      // We can't read the code to detect the language if there's no
+      // file name, so leave Code empty here.
+      FileName = AssumeFileName;
+    } else {
+      // Read in the code in case the filename alone isn't enough to
+      // detect the language.
+      ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr =
+          MemoryBuffer::getFileOrSTDIN(FileNames[0]);
+      if (std::error_code EC = CodeOrErr.getError()) {
+        llvm::errs() << EC.message() << "\n";
+        return 1;
+      }
+      FileName = (FileNames[0] == "-") ? AssumeFileName : FileNames[0];
+      Code = std::move(CodeOrErr.get());
+    }
     llvm::Expected<clang::format::FormatStyle> FormatStyle =
-        clang::format::getStyle(
-            Style, FileNames.empty() ? AssumeFileName : FileNames[0],
-            FallbackStyle);
+        clang::format::getStyle(Style, FileName, FallbackStyle,
+                                Code ? Code->getBuffer() : "");
     if (!FormatStyle) {
       llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n";
       return 1;
Index: test/Format/lit.local.cfg
===================================================================
--- /dev/null
+++ test/Format/lit.local.cfg
@@ -0,0 +1,3 @@
+# Suffixes supported by clang-format.
+config.suffixes = ['.cpp', '.h', '.m', '.mm', '.java', '.js', '.ts', '.proto',
+                   '.protodevel', '.pb.txt', '.textproto', '.asciipb', '.td']
Index: test/Format/dump-config-objc.h
===================================================================
--- /dev/null
+++ test/Format/dump-config-objc.h
@@ -0,0 +1,5 @@
+// RUN: clang-format -dump-config %s | FileCheck %s
+
+// CHECK: Language: ObjC
+ at interface Foo
+ at end
Index: test/Format/dump-config-cxx.h
===================================================================
--- /dev/null
+++ test/Format/dump-config-cxx.h
@@ -0,0 +1,3 @@
+// RUN: clang-format -dump-config %s | FileCheck %s
+
+// CHECK: Language: Cpp


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42395.130970.patch
Type: text/x-patch
Size: 2294 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180122/46c4ea0f/attachment.bin>


More information about the cfe-commits mailing list