Skip to content

Instantly share code, notes, and snippets.

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 o11c/7506460 to your computer and use it in GitHub Desktop.
Save o11c/7506460 to your computer and use it in GitHub Desktop.
Patch for clang-format
From 494e6ef67565f7358b415d015018fad6357484e7 Mon Sep 17 00:00:00 2001
From: Ben Longbons <b.r.longbons@gmail.com>
Date: Sat, 16 Nov 2013 00:52:54 -0800
Subject: [PATCH] Allow different attachment policy for pointer and reference
---
include/clang/Format/Format.h | 6 +++++-
lib/Format/Format.cpp | 23 +++++++++++++++++------
lib/Format/TokenAnnotator.cpp | 6 +++---
3 files changed, 25 insertions(+), 10 deletions(-)
diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h
index 0f27467..3725345 100644
--- a/include/clang/Format/Format.h
+++ b/include/clang/Format/Format.h
@@ -55,9 +55,12 @@ struct FormatStyle {
/// \brief The penalty for breaking a function call after "call(".
unsigned PenaltyBreakBeforeFirstCallParameter;
- /// \brief Set whether & and * bind to the type as opposed to the variable.
+ /// \brief Set whether * binds to the type as opposed to the variable.
bool PointerBindsToType;
+ /// \brief Set whether & binds to the type as opposed to the variable.
+ bool ReferenceBindsToType;
+
/// \brief If \c true, analyze the formatted file for the most common binding.
bool DerivePointerBinding;
@@ -287,6 +290,7 @@ struct FormatStyle {
PenaltyExcessCharacter == R.PenaltyExcessCharacter &&
PenaltyReturnTypeOnItsOwnLine == R.PenaltyReturnTypeOnItsOwnLine &&
PointerBindsToType == R.PointerBindsToType &&
+ ReferenceBindsToType == R.ReferenceBindsToType &&
SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments &&
Cpp11BracedListStyle == R.Cpp11BracedListStyle &&
Standard == R.Standard && TabWidth == R.TabWidth &&
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index a5fc83e..205bce8 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -149,6 +149,7 @@ template <> struct MappingTraits<clang::format::FormatStyle> {
IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
Style.PenaltyReturnTypeOnItsOwnLine);
IO.mapOptional("PointerBindsToType", Style.PointerBindsToType);
+ IO.mapOptional("ReferenceBindsToType", Style.ReferenceBindsToType);
IO.mapOptional("SpacesBeforeTrailingComments",
Style.SpacesBeforeTrailingComments);
IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle);
@@ -213,6 +214,7 @@ FormatStyle getLLVMStyle() {
LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
LLVMStyle.ObjCSpaceBeforeProtocolList = true;
LLVMStyle.PointerBindsToType = false;
+ LLVMStyle.ReferenceBindsToType = false;
LLVMStyle.SpacesBeforeTrailingComments = 1;
LLVMStyle.Standard = FormatStyle::LS_Cpp03;
LLVMStyle.UseTab = FormatStyle::UT_Never;
@@ -260,6 +262,7 @@ FormatStyle getGoogleStyle() {
GoogleStyle.NamespaceIndentation = FormatStyle::NI_None;
GoogleStyle.ObjCSpaceBeforeProtocolList = false;
GoogleStyle.PointerBindsToType = true;
+ GoogleStyle.ReferenceBindsToType = true;
GoogleStyle.SpacesBeforeTrailingComments = 2;
GoogleStyle.Standard = FormatStyle::LS_Auto;
GoogleStyle.UseTab = FormatStyle::UT_Never;
@@ -298,6 +301,7 @@ FormatStyle getMozillaStyle() {
MozillaStyle.ObjCSpaceBeforeProtocolList = false;
MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
MozillaStyle.PointerBindsToType = true;
+ MozillaStyle.ReferenceBindsToType = true;
return MozillaStyle;
}
@@ -312,6 +316,7 @@ FormatStyle getWebKitStyle() {
Style.IndentWidth = 4;
Style.NamespaceIndentation = FormatStyle::NI_Inner;
Style.PointerBindsToType = true;
+ Style.ReferenceBindsToType = true;
return Style;
}
@@ -1280,8 +1285,10 @@ private:
void
deriveLocalStyle(const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
- unsigned CountBoundToVariable = 0;
- unsigned CountBoundToType = 0;
+ unsigned CountPtrBoundToVariable = 0;
+ unsigned CountPtrBoundToType = 0;
+ unsigned CountRefBoundToVariable = 0;
+ unsigned CountRefBoundToType = 0;
bool HasCpp03IncompatibleFormat = false;
bool HasBinPackedFunction = false;
bool HasOnePerLineFunction = false;
@@ -1296,9 +1303,9 @@ private:
bool SpacesAfter = Tok->Next->WhitespaceRange.getBegin() !=
Tok->Next->WhitespaceRange.getEnd();
if (SpacesBefore && !SpacesAfter)
- ++CountBoundToVariable;
+ ++(Tok->is(tok::star) ? CountPtrBoundToVariable : CountRefBoundToVariable);
else if (!SpacesBefore && SpacesAfter)
- ++CountBoundToType;
+ ++(Tok->is(tok::star) ? CountPtrBoundToType : CountRefBoundToType);
}
if (Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) {
@@ -1319,10 +1326,14 @@ private:
}
}
if (Style.DerivePointerBinding) {
- if (CountBoundToType > CountBoundToVariable)
+ if (CountPtrBoundToType > CountPtrBoundToVariable)
Style.PointerBindsToType = true;
- else if (CountBoundToType < CountBoundToVariable)
+ else if (CountPtrBoundToType < CountPtrBoundToVariable)
Style.PointerBindsToType = false;
+ if (CountRefBoundToType > CountRefBoundToVariable)
+ Style.ReferenceBindsToType = true;
+ else if (CountRefBoundToType < CountRefBoundToVariable)
+ Style.ReferenceBindsToType = false;
}
if (Style.Standard == FormatStyle::LS_Auto) {
Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11
diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp
index 074e1d7..6af77a7 100644
--- a/lib/Format/TokenAnnotator.cpp
+++ b/lib/Format/TokenAnnotator.cpp
@@ -1259,14 +1259,14 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
if (Right.Type == TT_PointerOrReference)
return Left.Tok.isLiteral() ||
((Left.Type != TT_PointerOrReference) && Left.isNot(tok::l_paren) &&
- !Style.PointerBindsToType);
+ !(Right.is(tok::star) ? Style.PointerBindsToType : Style.ReferenceBindsToType));
if (Right.Type == TT_FunctionTypeLParen && Left.isNot(tok::l_paren) &&
- (Left.Type != TT_PointerOrReference || Style.PointerBindsToType))
+ (Left.Type != TT_PointerOrReference || (Left.is(tok::star) ? Style.PointerBindsToType : Style.ReferenceBindsToType)))
return true;
if (Left.Type == TT_PointerOrReference)
return Right.Tok.isLiteral() || Right.Type == TT_BlockComment ||
((Right.Type != TT_PointerOrReference) &&
- Right.isNot(tok::l_paren) && Style.PointerBindsToType &&
+ Right.isNot(tok::l_paren) && (Left.is(tok::star) ? Style.PointerBindsToType : Style.ReferenceBindsToType) &&
Left.Previous &&
!Left.Previous->isOneOf(tok::l_paren, tok::coloncolon));
if (Right.is(tok::star) && Left.is(tok::l_paren))
--
1.8.4.2
void p(char *);
void r(char &);
void pr(char *&);
void rr(char &&);
void prr(char *&&);
void pp(char **);
void ppr(char **&);
void pprr(char **&&);
void pc(char *c);
void rc(char &c);
void prc(char *&c);
void rrc(char &&c);
void prrc(char *&&c);
void ppc(char **c);
void pprc(char **&c);
void pprrc(char **&&c);
template <char *> void tp();
template <char &> void tr();
template <char *&> void tpr();
template <char &&> void trr();
template <char *&&> void tprr();
template <char **> void tpp();
template <char **&> void tppr();
template <char **&&> void tpprr();
template <char *c> void tpc();
template <char &c> void trc();
template <char *&c> void tprc();
template <char &&c> void trrc();
template <char *&&c> void tprrc();
template <char **c> void tppc();
template <char **&c> void tpprc();
template <char **&&c> void tpprrc();
Tp<char *>();
Tr<char &>();
Tpr<char *&>();
Trr<char &&>();
Tprr<char *&&>();
Tpp<char **>();
Tppr<char **&>();
Tpprr<char **&&>();
Tpc<char *c>();
Trc<char &c>();
Tprc<char *&c>();
Trrc<char &&c>();
Tprrc<char *&&c>();
Tppc<char **c>();
Tpprc<char **&c>();
Tpprrc<char **&&c>();
void p(char *);
void r(char&);
void pr(char *&);
void rr(char&&);
void prr(char *&&);
void pp(char **);
void ppr(char **&);
void pprr(char **&&);
void pc(char *c);
void rc(char& c);
void prc(char *& c);
void rrc(char&& c);
void prrc(char *&& c);
void ppc(char **c);
void pprc(char **& c);
void pprrc(char **&& c);
template <char *> void tp();
template <char&> void tr();
template <char *&> void tpr();
template <char&&> void trr();
template <char *&&> void tprr();
template <char **> void tpp();
template <char **&> void tppr();
template <char **&&> void tpprr();
template <char *c> void tpc();
template <char& c> void trc();
template <char *& c> void tprc();
template <char&& c> void trrc();
template <char *&& c> void tprrc();
template <char **c> void tppc();
template <char **& c> void tpprc();
template <char **&& c> void tpprrc();
Tp<char *>();
Tr<char&>();
Tpr<char *&>();
Trr<char&&>();
Tprr<char *&&>();
Tpp<char **>();
Tppr<char **&>();
Tpprr<char **&&>();
Tpc<char *c>();
Trc<char& c>();
Tprc<char *& c>();
Trrc<char&& c>();
Tprrc<char *&& c>();
Tppc<char **c>();
Tpprc<char **& c>();
Tpprrc<char **&& c>();
void p(char*);
void r(char &);
void pr(char*&);
void rr(char &&);
void prr(char*&&);
void pp(char**);
void ppr(char**&);
void pprr(char**&&);
void pc(char* c);
void rc(char &c);
void prc(char*&c);
void rrc(char &&c);
void prrc(char*&&c);
void ppc(char** c);
void pprc(char**&c);
void pprrc(char**&&c);
template <char*> void tp();
template <char &> void tr();
template <char*&> void tpr();
template <char &&> void trr();
template <char*&&> void tprr();
template <char**> void tpp();
template <char**&> void tppr();
template <char**&&> void tpprr();
template <char* c> void tpc();
template <char &c> void trc();
template <char*&c> void tprc();
template <char &&c> void trrc();
template <char*&&c> void tprrc();
template <char** c> void tppc();
template <char**&c> void tpprc();
template <char**&&c> void tpprrc();
Tp<char*>();
Tr<char &>();
Tpr<char*&>();
Trr<char &&>();
Tprr<char*&&>();
Tpp<char**>();
Tppr<char**&>();
Tpprr<char**&&>();
Tpc<char* c>();
Trc<char &c>();
Tprc<char*&c>();
Trrc<char &&c>();
Tprrc<char*&&c>();
Tppc<char** c>();
Tpprc<char**&c>();
Tpprrc<char**&&c>();
void p(char*);
void r(char&);
void pr(char*&);
void rr(char&&);
void prr(char*&&);
void pp(char**);
void ppr(char**&);
void pprr(char**&&);
void pc(char* c);
void rc(char& c);
void prc(char*& c);
void rrc(char&& c);
void prrc(char*&& c);
void ppc(char** c);
void pprc(char**& c);
void pprrc(char**&& c);
template <char*> void tp();
template <char&> void tr();
template <char*&> void tpr();
template <char&&> void trr();
template <char*&&> void tprr();
template <char**> void tpp();
template <char**&> void tppr();
template <char**&&> void tpprr();
template <char* c> void tpc();
template <char& c> void trc();
template <char*& c> void tprc();
template <char&& c> void trrc();
template <char*&& c> void tprrc();
template <char** c> void tppc();
template <char**& c> void tpprc();
template <char**&& c> void tpprrc();
Tp<char*>();
Tr<char&>();
Tpr<char*&>();
Trr<char&&>();
Tprr<char*&&>();
Tpp<char**>();
Tppr<char**&>();
Tpprr<char**&&>();
Tpc<char* c>();
Trc<char& c>();
Tprc<char*& c>();
Trrc<char&& c>();
Tprrc<char*&& c>();
Tppc<char** c>();
Tpprc<char**& c>();
Tpprrc<char**&& c>();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment