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

llvm-svn: 196108
This commit is contained in:
Alexander Kornienko
2013-12-02 15:21:38 +00:00
parent 9beb517307
commit bc4ae44928
4 changed files with 29 additions and 16 deletions

View File

@@ -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

View File

@@ -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;

View File

@@ -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;

View File

@@ -62,6 +62,12 @@ static cl::opt<std::string>
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 @@ 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;
}