Index: test/Format/dump-config-cxx.h =================================================================== --- test/Format/dump-config-cxx.h +++ test/Format/dump-config-cxx.h @@ -0,0 +1,3 @@ +// RUN: clang-format -dump-config %s | FileCheck %s + +// CHECK: Language: Cpp Index: test/Format/dump-config-objc.h =================================================================== --- test/Format/dump-config-objc.h +++ test/Format/dump-config-objc.h @@ -0,0 +1,5 @@ +// RUN: clang-format -dump-config %s | FileCheck %s + +// CHECK: Language: ObjC +@interface Foo +@end Index: test/Format/lit.local.cfg =================================================================== --- test/Format/lit.local.cfg +++ 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'] 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 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;