parent
a6d5e3e9a2
commit
bd16eea59f
|
@ -86,12 +86,6 @@ static bool isTrailingComment(const AnnotatedToken &Tok) {
|
||||||
(Tok.Children.empty() || Tok.Children[0].MustBreakBefore);
|
(Tok.Children.empty() || Tok.Children[0].MustBreakBefore);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool isComparison(const AnnotatedToken &Tok) {
|
|
||||||
prec::Level Precedence = getPrecedence(Tok);
|
|
||||||
return Tok.Type == TT_BinaryOperator &&
|
|
||||||
(Precedence == prec::Equality || Precedence == prec::Relational);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns the length of everything up to the first possible line break after
|
// Returns the length of everything up to the first possible line break after
|
||||||
// the ), ], } or > matching \c Tok.
|
// the ), ], } or > matching \c Tok.
|
||||||
static unsigned getLengthToMatchingParen(const AnnotatedToken &Tok) {
|
static unsigned getLengthToMatchingParen(const AnnotatedToken &Tok) {
|
||||||
|
@ -492,10 +486,6 @@ public:
|
||||||
State.StartOfStringLiteral = 0;
|
State.StartOfStringLiteral = 0;
|
||||||
State.StartOfLineLevel = State.ParenLevel;
|
State.StartOfLineLevel = State.ParenLevel;
|
||||||
|
|
||||||
DEBUG({
|
|
||||||
DebugTokenState(*State.NextToken);
|
|
||||||
});
|
|
||||||
|
|
||||||
// The first token has already been indented and thus consumed.
|
// The first token has already been indented and thus consumed.
|
||||||
moveStateToNextToken(State, /*DryRun=*/ false);
|
moveStateToNextToken(State, /*DryRun=*/ false);
|
||||||
|
|
||||||
|
@ -741,8 +731,7 @@ private:
|
||||||
State.Stack.back().ColonPos =
|
State.Stack.back().ColonPos =
|
||||||
State.Column + Current.FormatTok.TokenLength;
|
State.Column + Current.FormatTok.TokenLength;
|
||||||
}
|
}
|
||||||
} else if (Current.Type == TT_StartOfName || Current.is(tok::question) ||
|
} else if (Current.Type == TT_StartOfName || Previous.is(tok::equal) ||
|
||||||
Previous.is(tok::equal) || isComparison(Previous) ||
|
|
||||||
Previous.Type == TT_ObjCMethodExpr) {
|
Previous.Type == TT_ObjCMethodExpr) {
|
||||||
State.Column = ContinuationIndent;
|
State.Column = ContinuationIndent;
|
||||||
} else {
|
} else {
|
||||||
|
@ -879,10 +868,13 @@ private:
|
||||||
State.Stack.back().StartOfFunctionCall =
|
State.Stack.back().StartOfFunctionCall =
|
||||||
Current.LastInChainOfCalls ? 0 : State.Column;
|
Current.LastInChainOfCalls ? 0 : State.Column;
|
||||||
if (Current.Type == TT_CtorInitializerColon) {
|
if (Current.Type == TT_CtorInitializerColon) {
|
||||||
|
State.Stack.back().Indent = State.Column + 2;
|
||||||
if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
|
if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
|
||||||
State.Stack.back().AvoidBinPacking = true;
|
State.Stack.back().AvoidBinPacking = true;
|
||||||
State.Stack.back().BreakBeforeParameter = false;
|
State.Stack.back().BreakBeforeParameter = false;
|
||||||
}
|
}
|
||||||
|
if (Current.is(tok::kw_return))
|
||||||
|
State.Stack.back().LastSpace = State.Column + 7;
|
||||||
|
|
||||||
// In ObjC method declaration we align on the ":" of parameters, but we need
|
// In ObjC method declaration we align on the ":" of parameters, but we need
|
||||||
// to ensure that we indent parameters on subsequent lines by at least 4.
|
// to ensure that we indent parameters on subsequent lines by at least 4.
|
||||||
|
@ -890,9 +882,15 @@ private:
|
||||||
State.Stack.back().Indent += 4;
|
State.Stack.back().Indent += 4;
|
||||||
|
|
||||||
// Insert scopes created by fake parenthesis.
|
// Insert scopes created by fake parenthesis.
|
||||||
for (unsigned i = 0, e = Current.FakeLParens; i != e; ++i) {
|
for (SmallVector<unsigned, 4>::const_reverse_iterator
|
||||||
|
I = Current.FakeLParens.rbegin(),
|
||||||
|
E = Current.FakeLParens.rend();
|
||||||
|
I != E; ++I) {
|
||||||
ParenState NewParenState = State.Stack.back();
|
ParenState NewParenState = State.Stack.back();
|
||||||
NewParenState.Indent = std::max(State.Column, State.Stack.back().Indent);
|
NewParenState.Indent = //State.Stack.back().LastSpace;
|
||||||
|
std::max(State.Column, State.Stack.back().LastSpace);
|
||||||
|
if ((*I > 3 && State.ParenLevel != 0) || *I == 4)
|
||||||
|
NewParenState.Indent += 4;
|
||||||
NewParenState.BreakBeforeParameter = false;
|
NewParenState.BreakBeforeParameter = false;
|
||||||
State.Stack.push_back(NewParenState);
|
State.Stack.push_back(NewParenState);
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
#include "TokenAnnotator.h"
|
#include "TokenAnnotator.h"
|
||||||
#include "clang/Basic/SourceManager.h"
|
#include "clang/Basic/SourceManager.h"
|
||||||
#include "clang/Lex/Lexer.h"
|
#include "clang/Lex/Lexer.h"
|
||||||
|
#include "llvm/Support/Debug.h"
|
||||||
|
|
||||||
namespace clang {
|
namespace clang {
|
||||||
namespace format {
|
namespace format {
|
||||||
|
@ -782,12 +783,9 @@ public:
|
||||||
if (Precedence > prec::PointerToMember || Current == NULL)
|
if (Precedence > prec::PointerToMember || Current == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Skip over "return" until we can properly parse it.
|
|
||||||
if (Current->is(tok::kw_return))
|
|
||||||
next();
|
|
||||||
|
|
||||||
// Eagerly consume trailing comments.
|
// Eagerly consume trailing comments.
|
||||||
while (isTrailingComment(Current)) {
|
while (Current && Current->FormatTok.NewlinesBefore == 0 &&
|
||||||
|
isTrailingComment(Current)) {
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -802,8 +800,7 @@ public:
|
||||||
if (Current) {
|
if (Current) {
|
||||||
if (Current->Type == TT_ConditionalExpr)
|
if (Current->Type == TT_ConditionalExpr)
|
||||||
CurrentPrecedence = 1 + (int) prec::Conditional;
|
CurrentPrecedence = 1 + (int) prec::Conditional;
|
||||||
else if (Current->is(tok::semi) || Current->Type == TT_InlineASMColon ||
|
else if (Current->is(tok::semi) || Current->Type == TT_InlineASMColon)
|
||||||
Current->Type == TT_CtorInitializerColon)
|
|
||||||
CurrentPrecedence = 1;
|
CurrentPrecedence = 1;
|
||||||
else if (Current->Type == TT_BinaryOperator || Current->is(tok::comma))
|
else if (Current->Type == TT_BinaryOperator || Current->is(tok::comma))
|
||||||
CurrentPrecedence = 1 + (int) getPrecedence(*Current);
|
CurrentPrecedence = 1 + (int) getPrecedence(*Current);
|
||||||
|
@ -814,7 +811,7 @@ public:
|
||||||
if (Current == NULL || closesScope(*Current) ||
|
if (Current == NULL || closesScope(*Current) ||
|
||||||
(CurrentPrecedence != 0 && CurrentPrecedence < Precedence)) {
|
(CurrentPrecedence != 0 && CurrentPrecedence < Precedence)) {
|
||||||
if (OperatorFound) {
|
if (OperatorFound) {
|
||||||
++Start->FakeLParens;
|
Start->FakeLParens.push_back(Precedence);
|
||||||
if (Current)
|
if (Current)
|
||||||
++Current->Parent->FakeRParens;
|
++Current->Parent->FakeRParens;
|
||||||
}
|
}
|
||||||
|
@ -829,9 +826,9 @@ public:
|
||||||
parse();
|
parse();
|
||||||
}
|
}
|
||||||
// Remove fake parens that just duplicate the real parens.
|
// Remove fake parens that just duplicate the real parens.
|
||||||
if (Current && Left->Children[0].FakeLParens > 0 &&
|
if (Current && !Left->Children[0].FakeLParens.empty() &&
|
||||||
Current->Parent->FakeRParens > 0) {
|
Current->Parent->FakeRParens > 0) {
|
||||||
--Left->Children[0].FakeLParens;
|
Left->Children[0].FakeLParens.pop_back();
|
||||||
--Current->Parent->FakeRParens;
|
--Current->Parent->FakeRParens;
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
|
@ -863,6 +860,10 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
|
||||||
ExpressionParser ExprParser(Line);
|
ExpressionParser ExprParser(Line);
|
||||||
ExprParser.parse();
|
ExprParser.parse();
|
||||||
|
|
||||||
|
DEBUG({
|
||||||
|
printDebugInfo(Line);
|
||||||
|
});
|
||||||
|
|
||||||
if (Line.First.Type == TT_ObjCMethodSpecifier)
|
if (Line.First.Type == TT_ObjCMethodSpecifier)
|
||||||
Line.Type = LT_ObjCMethodDecl;
|
Line.Type = LT_ObjCMethodDecl;
|
||||||
else if (Line.First.Type == TT_ObjCDecl)
|
else if (Line.First.Type == TT_ObjCDecl)
|
||||||
|
@ -1187,5 +1188,19 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
|
||||||
(Left.is(tok::l_square) && !Right.is(tok::r_square));
|
(Left.is(tok::l_square) && !Right.is(tok::r_square));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
|
||||||
|
llvm::errs() << "AnnotatedTokens:\n";
|
||||||
|
const AnnotatedToken *Tok = &Line.First;
|
||||||
|
while (Tok) {
|
||||||
|
llvm::errs() << Tok->FormatTok.Tok.getName() << ":"
|
||||||
|
<< " Type=" << Tok->Type << " FakeLParens=";
|
||||||
|
for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
|
||||||
|
llvm::errs() << Tok->FakeLParens[i] << "/";
|
||||||
|
llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n";
|
||||||
|
Tok = Tok->Children.empty() ? NULL : &Tok->Children[0];
|
||||||
|
}
|
||||||
|
llvm::errs() << "----\n";
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace format
|
} // namespace format
|
||||||
} // namespace clang
|
} // namespace clang
|
||||||
|
|
|
@ -75,7 +75,7 @@ public:
|
||||||
CanBreakBefore(false), MustBreakBefore(false),
|
CanBreakBefore(false), MustBreakBefore(false),
|
||||||
ClosesTemplateDeclaration(false), MatchingParen(NULL),
|
ClosesTemplateDeclaration(false), MatchingParen(NULL),
|
||||||
ParameterCount(0), BindingStrength(0), SplitPenalty(0),
|
ParameterCount(0), BindingStrength(0), SplitPenalty(0),
|
||||||
LongestObjCSelectorName(0), Parent(NULL), FakeLParens(0),
|
LongestObjCSelectorName(0), Parent(NULL),
|
||||||
FakeRParens(0), LastInChainOfCalls(false),
|
FakeRParens(0), LastInChainOfCalls(false),
|
||||||
PartOfMultiVariableDeclStmt(false) {}
|
PartOfMultiVariableDeclStmt(false) {}
|
||||||
|
|
||||||
|
@ -158,8 +158,9 @@ public:
|
||||||
std::vector<AnnotatedToken> Children;
|
std::vector<AnnotatedToken> Children;
|
||||||
AnnotatedToken *Parent;
|
AnnotatedToken *Parent;
|
||||||
|
|
||||||
/// \brief Insert this many fake ( before this token for correct indentation.
|
/// \brief Stores the number of required fake parenthesis and the
|
||||||
unsigned FakeLParens;
|
/// corresponding operator precedence.
|
||||||
|
SmallVector<unsigned, 4> FakeLParens;
|
||||||
/// \brief Insert this many fake ) after this token for correct indentation.
|
/// \brief Insert this many fake ) after this token for correct indentation.
|
||||||
unsigned FakeRParens;
|
unsigned FakeRParens;
|
||||||
|
|
||||||
|
@ -248,6 +249,8 @@ private:
|
||||||
|
|
||||||
bool canBreakBefore(const AnnotatedLine &Line, const AnnotatedToken &Right);
|
bool canBreakBefore(const AnnotatedLine &Line, const AnnotatedToken &Right);
|
||||||
|
|
||||||
|
void printDebugInfo(const AnnotatedLine &Line);
|
||||||
|
|
||||||
const FormatStyle &Style;
|
const FormatStyle &Style;
|
||||||
SourceManager &SourceMgr;
|
SourceManager &SourceMgr;
|
||||||
Lexer &Lex;
|
Lexer &Lex;
|
||||||
|
|
|
@ -1682,9 +1682,9 @@ TEST_F(FormatTest, FormatsBuilderPattern) {
|
||||||
verifyFormat(
|
verifyFormat(
|
||||||
"return llvm::StringSwitch<Reference::Kind>(name)\n"
|
"return llvm::StringSwitch<Reference::Kind>(name)\n"
|
||||||
" .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
|
" .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
|
||||||
" .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n"
|
" .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
|
||||||
" .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n"
|
" .StartsWith(\".init\", ORDER_INIT).StartsWith(\".fini\", ORDER_FINI)\n"
|
||||||
" .Default(ORDER_TEXT);\n");
|
" .StartsWith(\".hash\", ORDER_HASH).Default(ORDER_TEXT);\n");
|
||||||
|
|
||||||
verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
|
verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
|
||||||
" aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
|
" aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
|
||||||
|
|
Loading…
Reference in New Issue