Anastasiia Lukianenko via llvm-dev
2020-Nov-19 12:40 UTC
[llvm-dev] [PATCH 0/2] [clang-format] Add new configurations
From: Anastasiia Lukianenko <anastasiia_lukianenko at epam.com> We are trying to use the clang-format approach as a base for Xen [1] style formatting. During the state of testing clang-format with different configurations, we found that some points regarding the Xen coding style are not configurable. Therefore, we decided to add them to be able to make a choice in different cases. Regards, Anastasiia [1] - https://xenproject.org/ Anastasiia Lukianenko (2): [clang-format] Add BreakBeforeStructInitialization configuration [clang-format] Add BreakBeforeInlineASMColon configuration clang/include/clang/Format/Format.h | 32 +++++++++++++++++++++++ clang/lib/Format/ContinuationIndenter.cpp | 4 ++- clang/lib/Format/Format.cpp | 6 +++++ clang/lib/Format/TokenAnnotator.cpp | 3 +++ 4 files changed, 44 insertions(+), 1 deletion(-) -- 2.17.1
Anastasiia Lukianenko via llvm-dev
2020-Nov-19 12:40 UTC
[llvm-dev] [PATCH 1/2] [clang-format] Add BreakBeforeStructInitialization configuration
From: Anastasiia Lukianenko <anastasiia_lukianenko at epam.com>
If ``true``, struct left brace will be placed after line breaks.
true:
struct new_struct struct_name {...};
false:
struct new_struct struct_name = {
...};
Signed-off-by: Anastasiia Lukianenko <anastasiia_lukianenko at epam.com>
---
clang/include/clang/Format/Format.h | 18 ++++++++++++++++++
clang/lib/Format/ContinuationIndenter.cpp | 2 ++
clang/lib/Format/Format.cpp | 3 +++
clang/lib/Format/TokenAnnotator.cpp | 3 +++
4 files changed, 26 insertions(+)
diff --git a/clang/include/clang/Format/Format.h
b/clang/include/clang/Format/Format.h
index 587e588525d..a3e04e1ee6f 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -1160,6 +1160,23 @@ struct FormatStyle {
/// \endcode
BraceWrappingFlags BraceWrapping;
+ /// If ``true``, struct left brace will be placed after line breaks.
+ /// \code
+ /// true:
+ /// struct new_struct struct_name + /// {
+ /// a = 1,
+ /// b = 2,
+ /// };
+ ///
+ /// false:
+ /// struct new_struct struct_name = {
+ /// a = 1,
+ /// b = 2,
+ /// };
+ /// \endcode
+ bool BreakBeforeStructInitialization;
+
/// If ``true``, ternary operators will be placed after line breaks.
/// \code
/// true:
@@ -2431,6 +2448,7 @@ struct FormatStyle {
BinPackParameters == R.BinPackParameters &&
BreakBeforeBinaryOperators == R.BreakBeforeBinaryOperators
&&
BreakBeforeBraces == R.BreakBeforeBraces &&
+ BreakBeforeStructInitialization == R.BreakBeforeStructInitialization
&&
BreakBeforeTernaryOperators == R.BreakBeforeTernaryOperators
&&
BreakConstructorInitializers == R.BreakConstructorInitializers
&&
CompactNamespaces == R.CompactNamespaces &&
diff --git a/clang/lib/Format/ContinuationIndenter.cpp
b/clang/lib/Format/ContinuationIndenter.cpp
index d99107cb8b2..af6be432b5f 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -953,6 +953,8 @@ unsigned ContinuationIndenter::getNewLineColumn(const
LineState &State) {
const FormatToken &Previous = *Current.Previous;
// If we are continuing an expression, we want to use the continuation
indent.
+ if (Style.BreakBeforeStructInitialization)
+ Style.ContinuationIndentWidth = 0;
unsigned ContinuationIndent std::max(State.Stack.back().LastSpace,
State.Stack.back().Indent) +
Style.ContinuationIndentWidth;
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 1c566c9ea49..eb4d64aa919 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -513,6 +513,9 @@ template <> struct MappingTraits<FormatStyle> {
Style.BreakInheritanceList == FormatStyle::BILS_BeforeColon)
Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
+ IO.mapOptional("BreakBeforeStructInitialization",
+ Style.BreakBeforeStructInitialization);
+
IO.mapOptional("BreakBeforeTernaryOperators",
Style.BreakBeforeTernaryOperators);
diff --git a/clang/lib/Format/TokenAnnotator.cpp
b/clang/lib/Format/TokenAnnotator.cpp
index 0fdcca867e3..37844ca845e 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3489,6 +3489,9 @@ static bool isAllmanBraceIncludedBreakableLambda(
bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
const FormatToken &Right) {
const FormatToken &Left = *Right.Previous;
+ if (Style.BreakBeforeStructInitialization && Right.is(tok::l_brace)
&&
+ (Right.is(BK_BracedInit) || Left.is(tok::equal)))
+ return true;
if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0)
return true;
--
2.17.1
Anastasiia Lukianenko via llvm-dev
2020-Nov-19 12:40 UTC
[llvm-dev] [PATCH 2/2] [clang-format] Add BreakBeforeInlineASMColon configuration
From: Anastasiia Lukianenko <anastasiia_lukianenko at epam.com>
If ``true``, colons in ASM parameters will be placed after line breaks.
true:
asm volatile("loooooooooooooooooooooooooooooooooooooooooooooong",
:
: val);
false:
asm volatile("loooooooooooooooooooooooooooooooooooooooooooooong",
: : val);
Signed-off-by: Anastasiia Lukianenko <anastasiia_lukianenko at epam.com>
---
clang/include/clang/Format/Format.h | 14 ++++++++++++++
clang/lib/Format/ContinuationIndenter.cpp | 2 +-
clang/lib/Format/Format.cpp | 3 +++
3 files changed, 18 insertions(+), 1 deletion(-)
diff --git a/clang/include/clang/Format/Format.h
b/clang/include/clang/Format/Format.h
index a3e04e1ee6f..c3290949b6b 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -1160,6 +1160,19 @@ struct FormatStyle {
/// \endcode
BraceWrappingFlags BraceWrapping;
+ /// If ``true``, colons in ASM parameters will be placed after line breaks.
+ /// \code
+ /// true:
+ /// asm
volatile("loooooooooooooooooooooooooooooooooooooooooooooong",
+ /// :
+ /// : val);
+ ///
+ /// false:
+ /// asm
volatile("loooooooooooooooooooooooooooooooooooooooooooooong",
+ /// : : val);
+ /// \endcode
+ bool BreakBeforeInlineASMColon;
+
/// If ``true``, struct left brace will be placed after line breaks.
/// \code
/// true:
@@ -2448,6 +2461,7 @@ struct FormatStyle {
BinPackParameters == R.BinPackParameters &&
BreakBeforeBinaryOperators == R.BreakBeforeBinaryOperators
&&
BreakBeforeBraces == R.BreakBeforeBraces &&
+ BreakBeforeInlineASMColon == R.BreakBeforeInlineASMColon &&
BreakBeforeStructInitialization == R.BreakBeforeStructInitialization
&&
BreakBeforeTernaryOperators == R.BreakBeforeTernaryOperators
&&
BreakConstructorInitializers == R.BreakConstructorInitializers
&&
diff --git a/clang/lib/Format/ContinuationIndenter.cpp
b/clang/lib/Format/ContinuationIndenter.cpp
index af6be432b5f..bb5d5475505 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -334,7 +334,7 @@ bool ContinuationIndenter::mustBreak(const LineState
&State) {
auto LambdaBodyLength = getLengthToMatchingParen(Current, State.Stack);
return (LambdaBodyLength > getColumnLimit(State));
}
- if (Current.MustBreakBefore || Current.is(TT_InlineASMColon))
+ if (Current.MustBreakBefore || (Current.is(TT_InlineASMColon) &&
Style.BreakBeforeInlineASMColon))
return true;
if (State.Stack.back().BreakBeforeClosingBrace &&
Current.closesBlockOrBlockTypeList(Style))
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index eb4d64aa919..1ec2110714e 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -513,6 +513,9 @@ template <> struct MappingTraits<FormatStyle> {
Style.BreakInheritanceList == FormatStyle::BILS_BeforeColon)
Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
+ IO.mapOptional("BreakBeforeInlineASMColon",
+ Style.BreakBeforeInlineASMColon);
+
IO.mapOptional("BreakBeforeStructInitialization",
Style.BreakBeforeStructInitialization);
--
2.17.1
Anton Korobeynikov via llvm-dev
2020-Nov-19 22:10 UTC
[llvm-dev] [PATCH 0/2] [clang-format] Add new configurations
Hello Please refer to Developer policy (http://llvm.org/docs/DeveloperPolicy.html#making-and-submitting-a-patch) for guidance about patch submission. On Thu, Nov 19, 2020 at 3:40 PM Anastasiia Lukianenko via llvm-dev <llvm-dev at lists.llvm.org> wrote:> > From: Anastasiia Lukianenko <anastasiia_lukianenko at epam.com> > > We are trying to use the clang-format approach as a base for Xen [1] > style formatting. > During the state of testing clang-format with different configurations, > we found that some points regarding the Xen coding style are not > configurable. Therefore, we decided to add them to be able to make a > choice in different cases. > > Regards, > Anastasiia > > [1] - https://xenproject.org/ > > Anastasiia Lukianenko (2): > [clang-format] Add BreakBeforeStructInitialization configuration > [clang-format] Add BreakBeforeInlineASMColon configuration > > clang/include/clang/Format/Format.h | 32 +++++++++++++++++++++++ > clang/lib/Format/ContinuationIndenter.cpp | 4 ++- > clang/lib/Format/Format.cpp | 6 +++++ > clang/lib/Format/TokenAnnotator.cpp | 3 +++ > 4 files changed, 44 insertions(+), 1 deletion(-) > > -- > 2.17.1 > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev-- With best regards, Anton Korobeynikov Department of Statistical Modelling, Saint Petersburg State University