diff options
| author | Alexander Kornienko <alexfh@google.com> | 2013-12-02 15:21:38 +0000 | 
|---|---|---|
| committer | Alexander Kornienko <alexfh@google.com> | 2013-12-02 15:21:38 +0000 | 
| commit | bc4ae44928f5b96c554b83f926c4caebe728d18f (patch) | |
| tree | 416010eb1dde963e76606a0e4e3d7c0ba6bff719 | |
| parent | 9beb517307aa5617e7597af6d8b64eebf21d8e68 (diff) | |
| download | bcm5719-llvm-bc4ae44928f5b96c554b83f926c4caebe728d18f.tar.gz bcm5719-llvm-bc4ae44928f5b96c554b83f926c4caebe728d18f.zip  | |
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
| -rw-r--r-- | clang/include/clang/Format/Format.h | 5 | ||||
| -rw-r--r-- | clang/lib/Format/Format.cpp | 22 | ||||
| -rw-r--r-- | clang/test/Format/style-on-command-line.cpp | 5 | ||||
| -rw-r--r-- | clang/tools/clang-format/ClangFormat.cpp | 13 | 
4 files changed, 29 insertions, 16 deletions
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 3ef429efa07..05cdccb756c 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/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/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 28ef19e192f..ee9fe7625db 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/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/clang/test/Format/style-on-command-line.cpp b/clang/test/Format/style-on-command-line.cpp index 22131a1ebe1..554a67e264d 100644 --- a/clang/test/Format/style-on-command-line.cpp +++ b/clang/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/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp index 768165b92e9..513439649d2 100644 --- a/clang/tools/clang-format/ClangFormat.cpp +++ b/clang/tools/clang-format/ClangFormat.cpp @@ -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;    }  | 

