r323668 - [clang-format] Fix bug where -dump-config failed on ObjC header
Ben Hamilton via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 29 09:36:44 PST 2018
Author: benhamilton
Date: Mon Jan 29 09:36:43 2018
New Revision: 323668
URL: http://llvm.org/viewvc/llvm-project?rev=323668&view=rev
Log:
[clang-format] Fix bug where -dump-config failed on ObjC header
Summary:
`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
Reviewers: jolesiak, krasimir
Reviewed By: jolesiak, krasimir
Subscribers: Wizard, klimek, cfe-commits, djasper
Differential Revision: https://reviews.llvm.org/D42395
Added:
cfe/trunk/test/Format/dump-config-cxx.h
cfe/trunk/test/Format/dump-config-objc.h
cfe/trunk/test/Format/lit.local.cfg
Modified:
cfe/trunk/tools/clang-format/ClangFormat.cpp
Added: cfe/trunk/test/Format/dump-config-cxx.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Format/dump-config-cxx.h?rev=323668&view=auto
==============================================================================
--- cfe/trunk/test/Format/dump-config-cxx.h (added)
+++ cfe/trunk/test/Format/dump-config-cxx.h Mon Jan 29 09:36:43 2018
@@ -0,0 +1,3 @@
+// RUN: clang-format -dump-config %s | FileCheck %s
+
+// CHECK: Language: Cpp
Added: cfe/trunk/test/Format/dump-config-objc.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Format/dump-config-objc.h?rev=323668&view=auto
==============================================================================
--- cfe/trunk/test/Format/dump-config-objc.h (added)
+++ cfe/trunk/test/Format/dump-config-objc.h Mon Jan 29 09:36:43 2018
@@ -0,0 +1,5 @@
+// RUN: clang-format -dump-config %s | FileCheck %s
+
+// CHECK: Language: ObjC
+ at interface Foo
+ at end
Added: cfe/trunk/test/Format/lit.local.cfg
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Format/lit.local.cfg?rev=323668&view=auto
==============================================================================
--- cfe/trunk/test/Format/lit.local.cfg (added)
+++ cfe/trunk/test/Format/lit.local.cfg Mon Jan 29 09:36:43 2018
@@ -0,0 +1,4 @@
+# Suffixes supported by clang-format.
+config.suffixes = ['.c', '.cc', '.cpp', '.h', '.m', '.mm', '.java', '.js',
+ '.ts', '.proto', '.protodevel', '.pb.txt', '.textproto',
+ '.textpb', '.asciipb', '.td']
Modified: cfe/trunk/tools/clang-format/ClangFormat.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/ClangFormat.cpp?rev=323668&r1=323667&r2=323668&view=diff
==============================================================================
--- cfe/trunk/tools/clang-format/ClangFormat.cpp (original)
+++ cfe/trunk/tools/clang-format/ClangFormat.cpp Mon Jan 29 09:36:43 2018
@@ -357,10 +357,27 @@ int main(int argc, const char **argv) {
}
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;
More information about the cfe-commits
mailing list