[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 14:08:53 PST 2018
benhamilton created this revision.
benhamilton added reviewers: jolesiak, krasimir.
Herald added a subscriber: cfe-commits.
`clang-format -dump-config path/to/file.h` never passed
anything for the Code parameter to clang::format::getStyle().
This meant the logic to guess Objective-C from the contents
of a .h file never worked, because LibFormat didn't have the
code to work with.
With this fix, we now correctly read in the contents of the
file if possible with -dump-config.
I had to update the lit config for test/Format/ because
the default config ignores .h files.
Test Plan: make -j12 check-clang
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.
+ FileName = FileNames[0];
+ ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr =
+ MemoryBuffer::getFileOrSTDIN(FileName);
+ if (std::error_code EC = CodeOrErr.getError()) {
+ llvm::errs() << EC.message() << "\n";
+ return 1;
+ }
+ 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.130960.patch
Type: text/x-patch
Size: 2249 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180122/cd393384/attachment.bin>
More information about the cfe-commits
mailing list