Index: include/clang/Format/Format.h =================================================================== --- include/clang/Format/Format.h +++ include/clang/Format/Format.h @@ -491,11 +491,13 @@ /// == "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. /// /// \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 Index: lib/Format/Format.cpp =================================================================== --- lib/Format/Format.cpp +++ lib/Format/Format.cpp @@ -1974,18 +1974,20 @@ } 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"; } @@ -1993,9 +1995,10 @@ } 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; } @@ -2038,16 +2041,22 @@ 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) << ": " Index: tools/clang-format/ClangFormat.cpp =================================================================== --- tools/clang-format/ClangFormat.cpp +++ tools/clang-format/ClangFormat.cpp @@ -71,6 +71,14 @@ "file to use."), cl::init("LLVM"), cl::cat(ClangFormatCategory)); +static cl::opt +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 AssumeFilename("assume-filename", cl::desc("When reading from stdin, clang-format assumes this\n" @@ -220,11 +228,16 @@ 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() << "\n\n"; @@ -289,10 +302,11 @@ 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; }