[PATCH] clang-format: Add option to disable fallback
Adam Strzelecki
ono at java.pl
Thu May 15 01:24:25 PDT 2014
>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.
///
/// \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)
More information about the cfe-commits
mailing list