From e88bf5d2a8d380ab44ec2063417dd725c302697c Mon Sep 17 00:00:00 2001 From: Ben Hamilton Date: Mon, 29 Jan 2018 17:36:43 +0000 Subject: [PATCH] [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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@323668 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/Format/dump-config-cxx.h | 3 +++ test/Format/dump-config-objc.h | 5 +++++ test/Format/lit.local.cfg | 4 ++++ tools/clang-format/ClangFormat.cpp | 23 ++++++++++++++++++++--- 4 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 test/Format/dump-config-cxx.h create mode 100644 test/Format/dump-config-objc.h create mode 100644 test/Format/lit.local.cfg diff --git a/test/Format/dump-config-cxx.h b/test/Format/dump-config-cxx.h new file mode 100644 index 0000000000..57f3c975f9 --- /dev/null +++ b/test/Format/dump-config-cxx.h @@ -0,0 +1,3 @@ +// RUN: clang-format -dump-config %s | FileCheck %s + +// CHECK: Language: Cpp diff --git a/test/Format/dump-config-objc.h b/test/Format/dump-config-objc.h new file mode 100644 index 0000000000..d33fd366a4 --- /dev/null +++ b/test/Format/dump-config-objc.h @@ -0,0 +1,5 @@ +// RUN: clang-format -dump-config %s | FileCheck %s + +// CHECK: Language: ObjC +@interface Foo +@end diff --git a/test/Format/lit.local.cfg b/test/Format/lit.local.cfg new file mode 100644 index 0000000000..abf4e8a65c --- /dev/null +++ b/test/Format/lit.local.cfg @@ -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'] diff --git a/tools/clang-format/ClangFormat.cpp b/tools/clang-format/ClangFormat.cpp index b7179ffd64..2718a72f43 100644 --- a/tools/clang-format/ClangFormat.cpp +++ b/tools/clang-format/ClangFormat.cpp @@ -357,10 +357,27 @@ int main(int argc, const char **argv) { } if (DumpConfig) { + StringRef FileName; + std::unique_ptr 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> 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 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; -- 2.40.0