<div dir="ltr">Is this identical to the latest attachment on <a href="http://llvm.org/PR18732">llvm.org/PR18732</a>?<div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, May 15, 2014 at 10:24 AM, Adam Strzelecki <span dir="ltr"><<a href="mailto:ono@java.pl" target="_blank" class="cremed">ono@java.pl</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From command perspective -no-fallback may be used to indicate that file shall<br>
be not re-formatted if no valid style specified by -style was found.<br>
<br>
>From API perspective added in,out Fallback parameter indicating if fallback is<br>
desired and if it was used when resolving style.<br>
---<br>
 include/clang/Format/Format.h      |  4 +++-<br>
 lib/Format/Format.cpp              | 23 ++++++++++++++++-------<br>
 tools/clang-format/ClangFormat.cpp | 22 ++++++++++++++++++----<br>
 3 files changed, 37 insertions(+), 12 deletions(-)<br>
<br>
diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h<br>
index 09c37d1..034f217 100644<br>
--- a/include/clang/Format/Format.h<br>
+++ b/include/clang/Format/Format.h<br>
@@ -484,11 +484,13 @@ extern const char *StyleOptionHelpDescription;<br>
 /// == "file".<br>
 /// \param[in] FallbackStyle The name of a predefined style used to fallback to<br>
 /// in case the style can't be determined from \p StyleName.<br>
+/// \param[in,out] Fallback indicates if fallback style is intended to be used<br>
+/// and if fallback style was returned.<br></blockquote><div><br></div><div>I don't like [in, out] parameters, especially if they can be avoided easily like in this case.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

 ///<br>
 /// \returns FormatStyle as specified by \c StyleName. If no style could be<br>
 /// determined, the default is LLVM Style (see getLLVMStyle()).<br>
 FormatStyle getStyle(StringRef StyleName, StringRef FileName,<br>
-                     StringRef FallbackStyle);<br>
+                     StringRef FallbackStyle, bool &Fallback);<br>
<br>
 } // end namespace format<br>
 } // end namespace clang<br>
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp<br>
index 60706b1..eef9dfc 100644<br>
--- a/lib/Format/Format.cpp<br>
+++ b/lib/Format/Format.cpp<br>
@@ -1940,18 +1940,20 @@ static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) {<br>
 }<br>
<br>
 FormatStyle getStyle(StringRef StyleName, StringRef FileName,<br>
-                     StringRef FallbackStyle) {<br>
+                     StringRef FallbackStyle, bool &Fallback) {<br>
   FormatStyle Style = getLLVMStyle();<br>
   Style.Language = getLanguageByFileName(FileName);<br>
   if (!getPredefinedStyle(FallbackStyle, Style.Language, &Style)) {<br>
     llvm::errs() << "Invalid fallback style \"" << FallbackStyle<br>
                  << "\" using LLVM style\n";<br>
+    Fallback = true;<br>
     return Style;<br>
   }<br>
<br>
   if (StyleName.startswith("{")) {<br>
     // Parse YAML/JSON style from the command line.<br>
-    if (llvm::error_code ec = parseConfiguration(StyleName, &Style)) {<br>
+    llvm::error_code ec = parseConfiguration(StyleName, &Style);<br>
+    if ((Fallback = !!ec)) {<br>
       llvm::errs() << "Error parsing -style: " << ec.message() << ", using "<br>
                    << FallbackStyle << " style\n";<br>
     }<br>
@@ -1959,9 +1961,10 @@ FormatStyle getStyle(StringRef StyleName, StringRef FileName,<br>
   }<br>
<br>
   if (!StyleName.equals_lower("file")) {<br>
-    if (!getPredefinedStyle(StyleName, Style.Language, &Style))<br>
+    if ((Fallback = !getPredefinedStyle(StyleName, Style.Language, &Style))) {<br>
       llvm::errs() << "Invalid value for -style, using " << FallbackStyle<br>
                    << " style\n";<br>
+    }<br>
     return Style;<br>
   }<br>
<br>
@@ -2004,16 +2007,22 @@ FormatStyle getStyle(StringRef StyleName, StringRef FileName,<br>
           UnsuitableConfigFiles.append(ConfigFile);<br>
           continue;<br>
         }<br>
-        llvm::errs() << "Error reading " << ConfigFile << ": " << ec.message()<br>
-                     << "\n";<br>
+        if (Fallback) {<br>
+          llvm::errs() << "Error reading " << ConfigFile << ": " << ec.message()<br>
+                       << "\n";<br>
+        }<br>
         break;<br>
       }<br>
       DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n");<br>
+      Fallback = false;<br>
       return Style;<br>
     }<br>
   }<br>
-  llvm::errs() << "Can't find usable .clang-format, using " << FallbackStyle<br>
-               << " style\n";<br>
+  if (Fallback) {<br>
+    llvm::errs() << "Can't find usable .clang-format, using " << FallbackStyle<br>
+                 << " style\n";<br>
+  }<br>
+  Fallback = true;<br>
   if (!UnsuitableConfigFiles.empty()) {<br>
     llvm::errs() << "Configuration file(s) do(es) not support "<br>
                  << getLanguageName(Style.Language) << ": "<br>
diff --git a/tools/clang-format/ClangFormat.cpp b/tools/clang-format/ClangFormat.cpp<br>
index d26659d..6448cfc 100644<br>
--- a/tools/clang-format/ClangFormat.cpp<br>
+++ b/tools/clang-format/ClangFormat.cpp<br>
@@ -71,6 +71,14 @@ FallbackStyle("fallback-style",<br>
                        "file to use."),<br>
               cl::init("LLVM"), cl::cat(ClangFormatCategory));<br>
<br>
+static cl::opt<bool><br>
+NoFallback("no-fallback",<br>
+           cl::desc("Skip formatting when style specified with -style\n"<br>
+                    "is not found or is invalid. Empty .clang-format\n"<br>
+                    "file in conjunction with -style=file effectively\n"<br>
+                    "disables formatting inside specific folder."),<br>
+           cl::cat(ClangFormatCategory));<br>
+<br>
 static cl::opt<std::string><br>
 AssumeFilename("assume-filename",<br>
                cl::desc("When reading from stdin, clang-format assumes this\n"<br>
@@ -220,11 +228,16 @@ static bool format(StringRef FileName) {<br>
   if (fillRanges(Sources, ID, Code.get(), Ranges))<br>
     return true;<br>
<br>
-  FormatStyle FormatStyle = getStyle(<br>
-      Style, (FileName == "-") ? AssumeFilename : FileName, FallbackStyle);<br>
+  bool Fallback = !NoFallback;<br>
+  FormatStyle FormatStyle =<br>
+      getStyle(Style, (FileName == "-") ? AssumeFilename : FileName,<br>
+               FallbackStyle, Fallback);<br>
   Lexer Lex(ID, Sources.getBuffer(ID), Sources,<br>
             getFormattingLangOpts(FormatStyle.Standard));<br>
-  tooling::Replacements Replaces = reformat(FormatStyle, Lex, Sources, Ranges);<br>
+  tooling::Replacements Replaces;<br>
+  if (!Fallback || !NoFallback) {<br>
+    Replaces = reformat(FormatStyle, Lex, Sources, Ranges);<br>
+  }<br>
   if (OutputXML) {<br>
     llvm::outs()<br>
         << "<?xml version='1.0'?>\n<replacements xml:space='preserve'>\n";<br>
@@ -289,10 +302,11 @@ int main(int argc, const char **argv) {<br>
     cl::PrintHelpMessage();<br>
<br>
   if (DumpConfig) {<br>
+    bool Fallback = !NoFallback;<br>
     std::string Config =<br>
         clang::format::configurationAsText(clang::format::getStyle(<br>
             Style, FileNames.empty() ? AssumeFilename : FileNames[0],<br>
-            FallbackStyle));<br>
+            FallbackStyle, Fallback));<br>
     llvm::outs() << Config << "\n";<br>
     return 0;<br>
   }<br>
<span class="HOEnZb"><font color="#888888">--<br>
1.8.5.2 (Apple Git-48)<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu" class="cremed">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank" class="cremed">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</font></span></blockquote></div><br></div></div>