diff options
author | Ben Hamilton <benhamilton@google.com> | 2018-01-29 17:36:43 +0000 |
---|---|---|
committer | Ben Hamilton <benhamilton@google.com> | 2018-01-29 17:36:43 +0000 |
commit | 6845dec91715ee1fc9939053b8add76cacced6ef (patch) | |
tree | 594501728333bba476238c033a456233b2480a7c /clang/tools | |
parent | 073971b2435687727787675b55cf78655425311d (diff) | |
download | bcm5719-llvm-6845dec91715ee1fc9939053b8add76cacced6ef.tar.gz bcm5719-llvm-6845dec91715ee1fc9939053b8add76cacced6ef.zip |
[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
llvm-svn: 323668
Diffstat (limited to 'clang/tools')
-rw-r--r-- | clang/tools/clang-format/ClangFormat.cpp | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp index b7179ffd641..2718a72f43c 100644 --- a/clang/tools/clang-format/ClangFormat.cpp +++ b/clang/tools/clang-format/ClangFormat.cpp @@ -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; |