r196108 - Added an option to specify fallback style.

Alexander Kornienko alexfh at google.com
Mon Dec 2 07:21:39 PST 2013


Author: alexfh
Date: Mon Dec  2 09:21:38 2013
New Revision: 196108

URL: http://llvm.org/viewvc/llvm-project?rev=196108&view=rev
Log:
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

Modified:
    cfe/trunk/include/clang/Format/Format.h
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/test/Format/style-on-command-line.cpp
    cfe/trunk/tools/clang-format/ClangFormat.cpp

Modified: cfe/trunk/include/clang/Format/Format.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Format/Format.h?rev=196108&r1=196107&r2=196108&view=diff
==============================================================================
--- cfe/trunk/include/clang/Format/Format.h (original)
+++ cfe/trunk/include/clang/Format/Format.h Mon Dec  2 09:21:38 2013
@@ -402,10 +402,13 @@ extern const char *StyleOptionHelpDescri
 /// 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

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=196108&r1=196107&r2=196108&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon Dec  2 09:21:38 2013
@@ -1661,12 +1661,14 @@ static void fillLanguageByFileName(Strin
   }
 }
 
-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
       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;

Modified: cfe/trunk/test/Format/style-on-command-line.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Format/style-on-command-line.cpp?rev=196108&r1=196107&r2=196108&view=diff
==============================================================================
--- cfe/trunk/test/Format/style-on-command-line.cpp (original)
+++ cfe/trunk/test/Format/style-on-command-line.cpp Mon Dec  2 09:21:38 2013
@@ -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;

Modified: cfe/trunk/tools/clang-format/ClangFormat.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/ClangFormat.cpp?rev=196108&r1=196107&r2=196108&view=diff
==============================================================================
--- cfe/trunk/tools/clang-format/ClangFormat.cpp (original)
+++ cfe/trunk/tools/clang-format/ClangFormat.cpp Mon Dec  2 09:21:38 2013
@@ -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;
   }





More information about the cfe-commits mailing list