[PATCH] clang-format: Add option to disable fallback

Daniel Jasper djasper at google.com
Thu May 15 02:07:09 PDT 2014


Is this identical to the latest attachment on llvm.org/PR18732?


On Thu, May 15, 2014 at 10:24 AM, Adam Strzelecki <ono at java.pl> wrote:

> From command perspective -no-fallback may be used to indicate that file
> shall
> be not re-formatted if no valid style specified by -style was found.
>
> From API perspective added in,out Fallback parameter indicating if
> fallback is
> desired and if it was used when resolving style.
> ---
>  include/clang/Format/Format.h      |  4 +++-
>  lib/Format/Format.cpp              | 23 ++++++++++++++++-------
>  tools/clang-format/ClangFormat.cpp | 22 ++++++++++++++++++----
>  3 files changed, 37 insertions(+), 12 deletions(-)
>
> diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h
> index 09c37d1..034f217 100644
> --- a/include/clang/Format/Format.h
> +++ b/include/clang/Format/Format.h
> @@ -484,11 +484,13 @@ extern const char *StyleOptionHelpDescription;
>  /// == "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.
> +/// \param[in,out] Fallback indicates if fallback style is intended to be
> used
> +/// and if fallback style was returned.
>

I don't like [in, out] parameters, especially if they can be avoided easily
like in this case.


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


More information about the cfe-commits mailing list