clang-format: Introduce DisableFormat that prevents formatting.
And "none" pseudo-style indicating that formatting should be not applied. (1) Using .clang-format with "DisableFormat: true" effectively prevents formatting for all files within the folder containing such .clang-format file. (2) Using -fallback-style=none together with -style=file prevents formatting when .clang-format is not found, which can be used in on-save callback. Patch by Adam Strzelecki. Thank you! llvm-svn: 209446
This commit is contained in:
parent
e31d5b6a2c
commit
c64b09acc3
|
@ -322,6 +322,9 @@ struct FormatStyle {
|
||||||
/// which should not be split into lines or otherwise changed.
|
/// which should not be split into lines or otherwise changed.
|
||||||
std::string CommentPragmas;
|
std::string CommentPragmas;
|
||||||
|
|
||||||
|
/// \brief Disables formatting at all.
|
||||||
|
bool DisableFormat;
|
||||||
|
|
||||||
/// \brief A vector of macros that should be interpreted as foreach loops
|
/// \brief A vector of macros that should be interpreted as foreach loops
|
||||||
/// instead of as function calls.
|
/// instead of as function calls.
|
||||||
///
|
///
|
||||||
|
@ -422,6 +425,9 @@ FormatStyle getWebKitStyle();
|
||||||
/// http://www.gnu.org/prep/standards/standards.html
|
/// http://www.gnu.org/prep/standards/standards.html
|
||||||
FormatStyle getGNUStyle();
|
FormatStyle getGNUStyle();
|
||||||
|
|
||||||
|
/// \brief Returns style indicating formatting should be not applied at all.
|
||||||
|
FormatStyle getNoStyle();
|
||||||
|
|
||||||
/// \brief Gets a predefined style for the specified language by name.
|
/// \brief Gets a predefined style for the specified language by name.
|
||||||
///
|
///
|
||||||
/// Currently supported names: LLVM, Google, Chromium, Mozilla. Names are
|
/// Currently supported names: LLVM, Google, Chromium, Mozilla. Names are
|
||||||
|
|
|
@ -223,6 +223,7 @@ template <> struct MappingTraits<FormatStyle> {
|
||||||
Style.SpaceBeforeParens);
|
Style.SpaceBeforeParens);
|
||||||
}
|
}
|
||||||
IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens);
|
IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens);
|
||||||
|
IO.mapOptional("DisableFormat", Style.DisableFormat);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -314,6 +315,8 @@ FormatStyle getLLVMStyle() {
|
||||||
LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
|
LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
|
||||||
LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;
|
LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;
|
||||||
|
|
||||||
|
LLVMStyle.DisableFormat = false;
|
||||||
|
|
||||||
return LLVMStyle;
|
return LLVMStyle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -409,6 +412,12 @@ FormatStyle getGNUStyle() {
|
||||||
return Style;
|
return Style;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FormatStyle getNoStyle() {
|
||||||
|
FormatStyle NoStyle = getLLVMStyle();
|
||||||
|
NoStyle.DisableFormat = true;
|
||||||
|
return NoStyle;
|
||||||
|
}
|
||||||
|
|
||||||
bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language,
|
bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language,
|
||||||
FormatStyle *Style) {
|
FormatStyle *Style) {
|
||||||
if (Name.equals_lower("llvm")) {
|
if (Name.equals_lower("llvm")) {
|
||||||
|
@ -423,6 +432,8 @@ bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language,
|
||||||
*Style = getWebKitStyle();
|
*Style = getWebKitStyle();
|
||||||
} else if (Name.equals_lower("gnu")) {
|
} else if (Name.equals_lower("gnu")) {
|
||||||
*Style = getGNUStyle();
|
*Style = getGNUStyle();
|
||||||
|
} else if (Name.equals_lower("none")) {
|
||||||
|
*Style = getNoStyle();
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -1909,6 +1920,11 @@ private:
|
||||||
tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
|
tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
|
||||||
SourceManager &SourceMgr,
|
SourceManager &SourceMgr,
|
||||||
std::vector<CharSourceRange> Ranges) {
|
std::vector<CharSourceRange> Ranges) {
|
||||||
|
if (Style.DisableFormat) {
|
||||||
|
tooling::Replacements EmptyResult;
|
||||||
|
return EmptyResult;
|
||||||
|
}
|
||||||
|
|
||||||
Formatter formatter(Style, Lex, SourceMgr, Ranges);
|
Formatter formatter(Style, Lex, SourceMgr, Ranges);
|
||||||
return formatter.format();
|
return formatter.format();
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,7 +68,8 @@ FallbackStyle("fallback-style",
|
||||||
cl::desc("The name of the predefined style used as a\n"
|
cl::desc("The name of the predefined style used as a\n"
|
||||||
"fallback in case clang-format is invoked with\n"
|
"fallback in case clang-format is invoked with\n"
|
||||||
"-style=file, but can not find the .clang-format\n"
|
"-style=file, but can not find the .clang-format\n"
|
||||||
"file to use."),
|
"file to use.\n"
|
||||||
|
"Use -fallback-style=none to skip formatting."),
|
||||||
cl::init("LLVM"), cl::cat(ClangFormatCategory));
|
cl::init("LLVM"), cl::cat(ClangFormatCategory));
|
||||||
|
|
||||||
static cl::opt<std::string>
|
static cl::opt<std::string>
|
||||||
|
|
Loading…
Reference in New Issue