[PATCH] Added an option to specify fallback style.
Alexander Kornienko
alexfh at google.com
Fri Nov 29 09:25:00 PST 2013
Hi djasper, klimek,
Added -fallback-style option. Changed clang-format to stop searching
for .clang-format when an invalid file is found.
http://llvm-reviews.chandlerc.com/D2292
Files:
include/clang/Format/Format.h
lib/Format/Format.cpp
test/Format/style-on-command-line.cpp
tools/clang-format/ClangFormat.cpp
Index: include/clang/Format/Format.h
===================================================================
--- include/clang/Format/Format.h
+++ include/clang/Format/Format.h
@@ -402,10 +402,13 @@
/// 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
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -1660,12 +1660,14 @@
}
}
-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("{")) {
@@ -1714,18 +1716,18 @@
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;
Index: test/Format/style-on-command-line.cpp
===================================================================
--- test/Format/style-on-command-line.cpp
+++ 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 @@
// 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;
Index: tools/clang-format/ClangFormat.cpp
===================================================================
--- tools/clang-format/ClangFormat.cpp
+++ tools/clang-format/ClangFormat.cpp
@@ -62,6 +62,12 @@
Style("style",
cl::desc(clang::format::StyleOptionHelpDescription),
cl::init("file"), cl::cat(ClangFormatCategory));
+static cl::opt<std::string>
+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<std::string>
AssumeFilename("assume-filename",
@@ -192,8 +198,8 @@
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 @@
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;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2292.1.patch
Type: text/x-patch
Size: 5462 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131129/5e76f99c/attachment.bin>
More information about the cfe-commits
mailing list