Skip to content

Instantly share code, notes, and snippets.

@kwk
Created March 3, 2022 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwk/bf3ba4570bd40e053e7dcd8837218b35 to your computer and use it in GitHub Desktop.
Save kwk/bf3ba4570bd40e053e7dcd8837218b35 to your computer and use it in GitHub Desktop.
disable one option in clang-format
commit c4c7c2092c34d1524f4afea18743d36d3b1f8ec8 (HEAD -> clang-format-unset)
Author: Konrad Kleine <kkleine@redhat.com>
Date: Thu Mar 3 16:17:35 2022 +0000
[clang-format] Allow AlignAfterOpenBracket to be disabled
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index d4e859f4decc..8275cc77e604 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -95,6 +95,9 @@ struct FormatStyle {
/// Note: This currently only applies to parentheses.
/// \endwarning
BAS_BlockIndent,
+ /// Turn off any change that would influence the aligning after open
+ /// brackets.
+ BAS_Disable
};
/// If ``true``, horizontally aligns arguments after an open bracket.
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 93d409118128..a3dd64c3a84c 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -666,7 +666,8 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
if (Previous.is(TT_TemplateString) && Previous.opensScope())
State.Stack.back().NoLineBreak = true;
- if (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign &&
+ if (enabled(Style.AlignAfterOpenBracket) &&
+ Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign &&
!State.Stack.back().IsCSharpGenericTypeConstraint &&
Previous.opensScope() && Previous.isNot(TT_ObjCMethodExpr) &&
(Current.isNot(TT_LineComment) || Previous.is(BK_BracedInit))) {
@@ -1390,8 +1391,9 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
PrecedenceLevel < prec::Assignment) &&
(!Previous || Previous->isNot(tok::kw_return) ||
(Style.Language != FormatStyle::LK_Java && PrecedenceLevel > 0)) &&
- (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign ||
- PrecedenceLevel != prec::Comma || Current.NestingLevel == 0)) {
+ (enabled(Style.AlignAfterOpenBracket) &&
+ (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign ||
+ PrecedenceLevel != prec::Comma || Current.NestingLevel == 0))) {
NewParenState.Indent =
std::max(std::max(State.Column, NewParenState.Indent),
State.Stack.back().LastSpace);
@@ -1421,6 +1423,7 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
if (PrecedenceLevel > prec::Unknown)
NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column);
if (PrecedenceLevel != prec::Conditional && !Current.is(TT_UnaryOperator) &&
+ enabled(Style.AlignAfterOpenBracket) &&
Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign)
NewParenState.StartOfFunctionCall = State.Column;
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 551d8cfe7ec1..d26d01299077 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -387,6 +387,7 @@ template <> struct ScalarEnumerationTraits<FormatStyle::BracketAlignmentStyle> {
IO.enumCase(Value, "DontAlign", FormatStyle::BAS_DontAlign);
IO.enumCase(Value, "AlwaysBreak", FormatStyle::BAS_AlwaysBreak);
IO.enumCase(Value, "BlockIndent", FormatStyle::BAS_BlockIndent);
+ IO.enumCase(Value, "Disable", FormatStyle::BAS_Disable);
// For backward compatibility.
IO.enumCase(Value, "true", FormatStyle::BAS_Align);
@@ -929,6 +930,10 @@ template <> struct DocumentListTraits<std::vector<FormatStyle>> {
namespace clang {
namespace format {
+constexpr bool enabled(const BracketAlignmentStyle c) {
+ return c != FormatStyle::BAS_Disable;
+}
+
const std::error_category &getParseCategory() {
static const ParseErrorCategory C{};
return C;
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 70f92c26fa8d..3563d248fd71 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2871,6 +2871,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
if (Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
return Style.PenaltyBreakOpenParenthesis;
if (Left.is(tok::l_paren) && InFunctionDecl &&
+ enabled(Style.AlignAfterOpenBracket) &&
Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign)
return 100;
if (Left.is(tok::l_paren) && Left.Previous &&
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment