From 34451b1a84817de73e4db3bd5e15db1a91c9009b Mon Sep 17 00:00:00 2001 From: Alexander Kornienko Date: Mon, 2 Dec 2013 15:21:38 +0000 Subject: [PATCH] Added an option to specify fallback style. Summary: Added -fallback-style option. Changed clang-format to stop searching for .clang-format when an invalid file is found. Reviewers: djasper, klimek Reviewed By: djasper CC: cfe-commits, klimek Differential Revision: http://llvm-reviews.chandlerc.com/D2292 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@196108 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Format/Format.h | 5 ++++- lib/Format/Format.cpp | 22 ++++++++++++---------- test/Format/style-on-command-line.cpp | 5 +++-- tools/clang-format/ClangFormat.cpp | 13 ++++++++++--- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index 3ef429efa0..05cdccb756 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -402,10 +402,13 @@ extern const char *StyleOptionHelpDescription; /// above. /// \param[in] FileName Path to start search for .clang-format if \c StyleName /// == "file". +/// \param[in] FallbackStyle The name of a predefined style used to fallback to +/// in case the style can't be determined from \p StyleName. /// /// \returns FormatStyle as specified by \c StyleName. If no style could be /// determined, the default is LLVM Style (see getLLVMStyle()). -FormatStyle getStyle(StringRef StyleName, StringRef FileName); +FormatStyle getStyle(StringRef StyleName, StringRef FileName, + StringRef FallbackStyle); } // end namespace format } // end namespace clang diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 28ef19e192..ee9fe7625d 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -1661,12 +1661,14 @@ static void fillLanguageByFileName(StringRef FileName, FormatStyle *Style) { } } -FormatStyle getStyle(StringRef StyleName, StringRef FileName) { - // FIXME: Configure fallback style from outside (add a command line option). - // Fallback style in case the rest of this function can't determine a style. - StringRef FallbackStyle = "LLVM"; +FormatStyle getStyle(StringRef StyleName, StringRef FileName, + StringRef FallbackStyle) { FormatStyle Style; - getPredefinedStyle(FallbackStyle, &Style); + if (!getPredefinedStyle(FallbackStyle, &Style)) { + llvm::errs() << "Invalid fallback style \"" << FallbackStyle + << "\" using LLVM style\n"; + return getLLVMStyle(); + } fillLanguageByFileName(FileName, &Style); if (StyleName.startswith("{")) { @@ -1715,18 +1717,18 @@ FormatStyle getStyle(StringRef StyleName, StringRef FileName) { if (llvm::error_code ec = llvm::MemoryBuffer::getFile(ConfigFile.c_str(), Text)) { llvm::errs() << ec.message() << "\n"; - continue; + break; } if (llvm::error_code ec = parseConfiguration(Text->getBuffer(), &Style)) { if (ec == llvm::errc::not_supported) { if (!UnsuitableConfigFiles.empty()) UnsuitableConfigFiles.append(", "); UnsuitableConfigFiles.append(ConfigFile); - } else { - llvm::errs() << "Error reading " << ConfigFile << ": " << ec.message() - << "\n"; + continue; } - continue; + llvm::errs() << "Error reading " << ConfigFile << ": " << ec.message() + << "\n"; + break; } DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n"); return Style; diff --git a/test/Format/style-on-command-line.cpp b/test/Format/style-on-command-line.cpp index 22131a1ebe..554a67e264 100644 --- a/test/Format/style-on-command-line.cpp +++ b/test/Format/style-on-command-line.cpp @@ -7,7 +7,7 @@ // RUN: printf "BasedOnStyle: google\nIndentWidth: 5\n" > %T/.clang-format // RUN: clang-format -style=file %t.cpp 2>&1 | FileCheck -strict-whitespace -check-prefix=CHECK5 %s // RUN: printf "\n" > %T/.clang-format -// RUN: clang-format -style=file %t.cpp 2>&1 | FileCheck -strict-whitespace -check-prefix=CHECK6 %s +// RUN: clang-format -style=file -fallback-style=webkit %t.cpp 2>&1 | FileCheck -strict-whitespace -check-prefix=CHECK6 %s // RUN: [ ! -e %T/.clang-format ] || rm %T/.clang-format // RUN: [ ! -e %T/_clang-format ] || rm %T/_clang-format // RUN: printf "BasedOnStyle: google\nIndentWidth: 6\n" > %T/_clang-format @@ -22,7 +22,8 @@ void f() { // CHECK4: {{^ int \*i;$}} // CHECK5: {{^ int\* i;$}} // CHECK6: {{^Error reading .*\.clang-format: Invalid argument}} -// XCHECK6X: {{^ int \*i;$}} +// CHECK6: {{^Can't find usable .clang-format, using webkit style$}} +// CHECK6: {{^ int\* i;$}} // CHECK7: {{^ int\* i;$}} int*i; int j; diff --git a/tools/clang-format/ClangFormat.cpp b/tools/clang-format/ClangFormat.cpp index 768165b92e..513439649d 100644 --- a/tools/clang-format/ClangFormat.cpp +++ b/tools/clang-format/ClangFormat.cpp @@ -62,6 +62,12 @@ static cl::opt Style("style", cl::desc(clang::format::StyleOptionHelpDescription), cl::init("file"), cl::cat(ClangFormatCategory)); +static cl::opt +FallbackStyle("fallback-style", + cl::desc("The name of the predefined style used as a fallback in " + "case clang-format is invoked with -style=file, but can " + "not find the .clang-format file to use."), + cl::init("LLVM"), cl::cat(ClangFormatCategory)); static cl::opt AssumeFilename("assume-filename", @@ -192,8 +198,8 @@ static bool format(StringRef FileName) { if (fillRanges(Sources, ID, Code.get(), Ranges)) return true; - FormatStyle FormatStyle = - getStyle(Style, (FileName == "-") ? AssumeFilename : FileName); + FormatStyle FormatStyle = getStyle( + Style, (FileName == "-") ? AssumeFilename : FileName, FallbackStyle); Lexer Lex(ID, Sources.getBuffer(ID), Sources, getFormattingLangOpts(FormatStyle.Standard)); tooling::Replacements Replaces = reformat(FormatStyle, Lex, Sources, Ranges); @@ -256,7 +262,8 @@ int main(int argc, const char **argv) { if (DumpConfig) { std::string Config = clang::format::configurationAsText(clang::format::getStyle( - Style, FileNames.empty() ? AssumeFilename : FileNames[0])); + Style, FileNames.empty() ? AssumeFilename : FileNames[0], + FallbackStyle)); llvm::outs() << Config << "\n"; return 0; } -- 2.40.0