2012-12-04 02:12:45 +08:00
|
|
|
//===--- Format.cpp - Format C++ code -------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief This file implements functions declared in Format.h. This will be
|
|
|
|
/// split into separate files as we go.
|
|
|
|
///
|
|
|
|
/// This is EXPERIMENTAL code under heavy development. It is not in a state yet,
|
|
|
|
/// where it can be used to format real code.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-16 22:55:28 +08:00
|
|
|
#define DEBUG_TYPE "format-formatter"
|
|
|
|
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "UnwrappedLineParser.h"
|
2013-01-10 23:05:09 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2012-12-21 18:20:02 +08:00
|
|
|
#include "clang/Basic/OperatorPrecedence.h"
|
2013-01-02 18:28:36 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2013-01-16 22:55:28 +08:00
|
|
|
#include "clang/Format/Format.h"
|
2013-01-10 23:05:09 +08:00
|
|
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
2012-12-04 02:12:45 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2013-01-16 22:55:28 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2012-12-04 21:02:32 +08:00
|
|
|
#include <string>
|
|
|
|
|
2013-01-16 22:55:28 +08:00
|
|
|
// Uncomment to get debug output from tests:
|
|
|
|
// #define DEBUG_WITH_TYPE(T, X) do { X; } while(0)
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
|
2013-01-07 18:48:50 +08:00
|
|
|
enum TokenType {
|
|
|
|
TT_BinaryOperator,
|
2013-01-10 19:14:08 +08:00
|
|
|
TT_BlockComment,
|
|
|
|
TT_CastRParen,
|
2013-01-07 18:48:50 +08:00
|
|
|
TT_ConditionalExpr,
|
|
|
|
TT_CtorInitializerColon,
|
2013-01-15 23:50:27 +08:00
|
|
|
TT_ImplicitStringLiteral,
|
2013-01-10 19:14:08 +08:00
|
|
|
TT_LineComment,
|
2013-01-10 21:08:12 +08:00
|
|
|
TT_ObjCBlockLParen,
|
2013-01-11 03:19:14 +08:00
|
|
|
TT_ObjCDecl,
|
2013-01-10 19:14:08 +08:00
|
|
|
TT_ObjCMethodSpecifier,
|
2013-01-12 14:18:40 +08:00
|
|
|
TT_ObjCMethodExpr,
|
2013-01-11 05:30:42 +08:00
|
|
|
TT_ObjCProperty,
|
2013-01-10 19:14:08 +08:00
|
|
|
TT_OverloadedOperator,
|
|
|
|
TT_PointerOrReference,
|
2013-01-07 18:48:50 +08:00
|
|
|
TT_PureVirtualSpecifier,
|
2013-01-10 19:14:08 +08:00
|
|
|
TT_TemplateCloser,
|
|
|
|
TT_TemplateOpener,
|
|
|
|
TT_TrailingUnaryOperator,
|
|
|
|
TT_UnaryOperator,
|
|
|
|
TT_Unknown
|
2013-01-07 18:48:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
enum LineType {
|
|
|
|
LT_Invalid,
|
|
|
|
LT_Other,
|
2013-01-22 22:28:24 +08:00
|
|
|
LT_BuilderTypeCall,
|
2013-01-07 18:48:50 +08:00
|
|
|
LT_PreprocessorDirective,
|
|
|
|
LT_VirtualFunctionDecl,
|
2013-01-11 03:19:14 +08:00
|
|
|
LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
|
2013-01-11 05:30:42 +08:00
|
|
|
LT_ObjCMethodDecl,
|
|
|
|
LT_ObjCProperty // An @property line.
|
2013-01-07 18:48:50 +08:00
|
|
|
};
|
|
|
|
|
2013-01-08 22:56:18 +08:00
|
|
|
class AnnotatedToken {
|
|
|
|
public:
|
2013-01-18 16:44:07 +08:00
|
|
|
explicit AnnotatedToken(const FormatToken &FormatTok)
|
2013-01-11 03:17:33 +08:00
|
|
|
: FormatTok(FormatTok), Type(TT_Unknown), SpaceRequiredBefore(false),
|
|
|
|
CanBreakBefore(false), MustBreakBefore(false),
|
2013-01-28 15:35:34 +08:00
|
|
|
ClosesTemplateDeclaration(false), MatchingParen(NULL),
|
|
|
|
ParameterCount(1), Parent(NULL) {
|
|
|
|
}
|
2013-01-08 22:56:18 +08:00
|
|
|
|
2013-01-14 22:14:23 +08:00
|
|
|
bool is(tok::TokenKind Kind) const { return FormatTok.Tok.is(Kind); }
|
|
|
|
bool isNot(tok::TokenKind Kind) const { return FormatTok.Tok.isNot(Kind); }
|
|
|
|
|
2013-01-08 22:56:18 +08:00
|
|
|
bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
|
|
|
|
return FormatTok.Tok.isObjCAtKeyword(Kind);
|
|
|
|
}
|
|
|
|
|
|
|
|
FormatToken FormatTok;
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
TokenType Type;
|
|
|
|
|
|
|
|
bool SpaceRequiredBefore;
|
|
|
|
bool CanBreakBefore;
|
|
|
|
bool MustBreakBefore;
|
2013-01-02 23:08:56 +08:00
|
|
|
|
|
|
|
bool ClosesTemplateDeclaration;
|
2013-01-08 22:56:18 +08:00
|
|
|
|
2013-01-16 22:59:02 +08:00
|
|
|
AnnotatedToken *MatchingParen;
|
|
|
|
|
2013-01-28 15:35:34 +08:00
|
|
|
/// \brief Number of parameters, if this is "(", "[" or "<".
|
|
|
|
///
|
|
|
|
/// This is initialized to 1 as we don't need to distinguish functions with
|
|
|
|
/// 0 parameters from functions with 1 parameter. Thus, we can simply count
|
|
|
|
/// the number of commas.
|
|
|
|
unsigned ParameterCount;
|
|
|
|
|
2013-01-16 18:41:46 +08:00
|
|
|
/// \brief The total length of the line up to and including this token.
|
|
|
|
unsigned TotalLength;
|
|
|
|
|
2013-01-08 22:56:18 +08:00
|
|
|
std::vector<AnnotatedToken> Children;
|
|
|
|
AnnotatedToken *Parent;
|
2013-01-17 20:53:34 +08:00
|
|
|
|
|
|
|
const AnnotatedToken *getPreviousNoneComment() const {
|
|
|
|
AnnotatedToken *Tok = Parent;
|
|
|
|
while (Tok != NULL && Tok->is(tok::comment))
|
|
|
|
Tok = Tok->Parent;
|
|
|
|
return Tok;
|
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
};
|
|
|
|
|
2013-01-14 21:08:07 +08:00
|
|
|
class AnnotatedLine {
|
|
|
|
public:
|
2013-01-16 17:10:19 +08:00
|
|
|
AnnotatedLine(const UnwrappedLine &Line)
|
|
|
|
: First(Line.Tokens.front()), Level(Line.Level),
|
2013-01-23 17:32:48 +08:00
|
|
|
InPPDirective(Line.InPPDirective),
|
|
|
|
MustBeDeclaration(Line.MustBeDeclaration) {
|
2013-01-16 17:10:19 +08:00
|
|
|
assert(!Line.Tokens.empty());
|
|
|
|
AnnotatedToken *Current = &First;
|
|
|
|
for (std::list<FormatToken>::const_iterator I = ++Line.Tokens.begin(),
|
|
|
|
E = Line.Tokens.end();
|
|
|
|
I != E; ++I) {
|
2013-01-18 16:44:07 +08:00
|
|
|
Current->Children.push_back(AnnotatedToken(*I));
|
2013-01-16 17:10:19 +08:00
|
|
|
Current->Children[0].Parent = Current;
|
|
|
|
Current = &Current->Children[0];
|
|
|
|
}
|
|
|
|
Last = Current;
|
|
|
|
}
|
|
|
|
AnnotatedLine(const AnnotatedLine &Other)
|
|
|
|
: First(Other.First), Type(Other.Type), Level(Other.Level),
|
2013-01-23 17:32:48 +08:00
|
|
|
InPPDirective(Other.InPPDirective),
|
|
|
|
MustBeDeclaration(Other.MustBeDeclaration) {
|
2013-01-16 17:10:19 +08:00
|
|
|
Last = &First;
|
|
|
|
while (!Last->Children.empty()) {
|
|
|
|
Last->Children[0].Parent = Last;
|
|
|
|
Last = &Last->Children[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-14 21:08:07 +08:00
|
|
|
AnnotatedToken First;
|
|
|
|
AnnotatedToken *Last;
|
|
|
|
|
|
|
|
LineType Type;
|
|
|
|
unsigned Level;
|
|
|
|
bool InPPDirective;
|
2013-01-23 17:32:48 +08:00
|
|
|
bool MustBeDeclaration;
|
2013-01-14 21:08:07 +08:00
|
|
|
};
|
|
|
|
|
2013-01-08 22:56:18 +08:00
|
|
|
static prec::Level getPrecedence(const AnnotatedToken &Tok) {
|
|
|
|
return getBinOpPrecedence(Tok.FormatTok.Tok.getKind(), true, true);
|
2012-12-24 21:43:52 +08:00
|
|
|
}
|
|
|
|
|
2013-01-28 17:35:24 +08:00
|
|
|
bool isBinaryOperator(const AnnotatedToken &Tok) {
|
|
|
|
// Comma is a binary operator, but does not behave as such wrt. formatting.
|
|
|
|
return getPrecedence(Tok) > prec::Comma;
|
|
|
|
}
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
FormatStyle getLLVMStyle() {
|
|
|
|
FormatStyle LLVMStyle;
|
|
|
|
LLVMStyle.ColumnLimit = 80;
|
|
|
|
LLVMStyle.MaxEmptyLinesToKeep = 1;
|
|
|
|
LLVMStyle.PointerAndReferenceBindToType = false;
|
|
|
|
LLVMStyle.AccessModifierOffset = -2;
|
|
|
|
LLVMStyle.SplitTemplateClosingGreater = true;
|
2012-12-07 02:03:27 +08:00
|
|
|
LLVMStyle.IndentCaseLabels = false;
|
2013-01-07 19:09:06 +08:00
|
|
|
LLVMStyle.SpacesBeforeTrailingComments = 1;
|
2013-01-16 22:59:02 +08:00
|
|
|
LLVMStyle.BinPackParameters = true;
|
2013-01-23 18:08:28 +08:00
|
|
|
LLVMStyle.AllowAllParametersOnNextLine = true;
|
2013-01-11 19:37:55 +08:00
|
|
|
LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
|
2013-01-15 00:24:39 +08:00
|
|
|
LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
|
2013-01-11 04:12:55 +08:00
|
|
|
LLVMStyle.ObjCSpaceBeforeProtocolList = true;
|
2012-12-04 02:12:45 +08:00
|
|
|
return LLVMStyle;
|
|
|
|
}
|
|
|
|
|
|
|
|
FormatStyle getGoogleStyle() {
|
|
|
|
FormatStyle GoogleStyle;
|
|
|
|
GoogleStyle.ColumnLimit = 80;
|
|
|
|
GoogleStyle.MaxEmptyLinesToKeep = 1;
|
|
|
|
GoogleStyle.PointerAndReferenceBindToType = true;
|
|
|
|
GoogleStyle.AccessModifierOffset = -1;
|
|
|
|
GoogleStyle.SplitTemplateClosingGreater = false;
|
2012-12-07 02:03:27 +08:00
|
|
|
GoogleStyle.IndentCaseLabels = true;
|
2013-01-07 19:09:06 +08:00
|
|
|
GoogleStyle.SpacesBeforeTrailingComments = 2;
|
2013-01-16 22:59:02 +08:00
|
|
|
GoogleStyle.BinPackParameters = false;
|
2013-01-23 18:08:28 +08:00
|
|
|
GoogleStyle.AllowAllParametersOnNextLine = true;
|
2013-01-11 19:37:55 +08:00
|
|
|
GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
|
2013-01-16 23:44:34 +08:00
|
|
|
GoogleStyle.AllowShortIfStatementsOnASingleLine = false;
|
2013-01-11 04:12:55 +08:00
|
|
|
GoogleStyle.ObjCSpaceBeforeProtocolList = false;
|
2012-12-04 02:12:45 +08:00
|
|
|
return GoogleStyle;
|
|
|
|
}
|
|
|
|
|
2013-01-15 00:24:39 +08:00
|
|
|
FormatStyle getChromiumStyle() {
|
|
|
|
FormatStyle ChromiumStyle = getGoogleStyle();
|
2013-01-23 18:08:28 +08:00
|
|
|
ChromiumStyle.AllowAllParametersOnNextLine = false;
|
2013-01-15 00:24:39 +08:00
|
|
|
return ChromiumStyle;
|
|
|
|
}
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
struct OptimizationParameters {
|
|
|
|
unsigned PenaltyIndentLevel;
|
2013-01-09 18:16:05 +08:00
|
|
|
unsigned PenaltyExcessCharacter;
|
2012-12-04 02:12:45 +08:00
|
|
|
};
|
|
|
|
|
2013-01-18 16:44:07 +08:00
|
|
|
/// \brief Manages the whitespaces around tokens and their replacements.
|
2013-01-11 02:45:26 +08:00
|
|
|
///
|
2013-01-18 16:44:07 +08:00
|
|
|
/// This includes special handling for certain constructs, e.g. the alignment of
|
|
|
|
/// trailing line comments.
|
|
|
|
class WhitespaceManager {
|
|
|
|
public:
|
|
|
|
WhitespaceManager(SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
|
|
|
|
|
|
|
|
/// \brief Replaces the whitespace in front of \p Tok. Only call once for
|
|
|
|
/// each \c AnnotatedToken.
|
|
|
|
void replaceWhitespace(const AnnotatedToken &Tok, unsigned NewLines,
|
|
|
|
unsigned Spaces, unsigned WhitespaceStartColumn,
|
|
|
|
const FormatStyle &Style) {
|
2013-01-22 06:49:20 +08:00
|
|
|
// 2+ newlines mean an empty line separating logic scopes.
|
|
|
|
if (NewLines >= 2)
|
|
|
|
alignComments();
|
|
|
|
|
|
|
|
// Align line comments if they are trailing or if they continue other
|
|
|
|
// trailing comments.
|
|
|
|
if (Tok.Type == TT_LineComment &&
|
2013-01-18 16:44:07 +08:00
|
|
|
(Tok.Parent != NULL || !Comments.empty())) {
|
|
|
|
if (Style.ColumnLimit >=
|
|
|
|
Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) {
|
|
|
|
Comments.push_back(StoredComment());
|
|
|
|
Comments.back().Tok = Tok.FormatTok;
|
|
|
|
Comments.back().Spaces = Spaces;
|
|
|
|
Comments.back().NewLines = NewLines;
|
|
|
|
Comments.back().MinColumn = WhitespaceStartColumn + Spaces;
|
|
|
|
Comments.back().MaxColumn = Style.ColumnLimit -
|
|
|
|
Spaces - Tok.FormatTok.TokenLength;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-01-22 06:49:20 +08:00
|
|
|
|
|
|
|
// If this line does not have a trailing comment, align the stored comments.
|
|
|
|
if (Tok.Children.empty() && Tok.Type != TT_LineComment)
|
|
|
|
alignComments();
|
2013-01-18 16:44:07 +08:00
|
|
|
storeReplacement(Tok.FormatTok,
|
|
|
|
std::string(NewLines, '\n') + std::string(Spaces, ' '));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Like \c replaceWhitespace, but additionally adds right-aligned
|
|
|
|
/// backslashes to escape newlines inside a preprocessor directive.
|
|
|
|
///
|
|
|
|
/// This function and \c replaceWhitespace have the same behavior if
|
|
|
|
/// \c Newlines == 0.
|
|
|
|
void replacePPWhitespace(const AnnotatedToken &Tok, unsigned NewLines,
|
|
|
|
unsigned Spaces, unsigned WhitespaceStartColumn,
|
|
|
|
const FormatStyle &Style) {
|
|
|
|
std::string NewLineText;
|
|
|
|
if (NewLines > 0) {
|
|
|
|
unsigned Offset = std::min<int>(Style.ColumnLimit - 1,
|
|
|
|
WhitespaceStartColumn);
|
|
|
|
for (unsigned i = 0; i < NewLines; ++i) {
|
|
|
|
NewLineText += std::string(Style.ColumnLimit - Offset - 1, ' ');
|
|
|
|
NewLineText += "\\\n";
|
|
|
|
Offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
storeReplacement(Tok.FormatTok, NewLineText + std::string(Spaces, ' '));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Returns all the \c Replacements created during formatting.
|
|
|
|
const tooling::Replacements &generateReplacements() {
|
|
|
|
alignComments();
|
|
|
|
return Replaces;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// \brief Structure to store a comment for later layout and alignment.
|
|
|
|
struct StoredComment {
|
|
|
|
FormatToken Tok;
|
|
|
|
unsigned MinColumn;
|
|
|
|
unsigned MaxColumn;
|
|
|
|
unsigned NewLines;
|
|
|
|
unsigned Spaces;
|
|
|
|
};
|
|
|
|
SmallVector<StoredComment, 16> Comments;
|
|
|
|
typedef SmallVector<StoredComment, 16>::iterator comment_iterator;
|
|
|
|
|
|
|
|
/// \brief Try to align all stashed comments.
|
|
|
|
void alignComments() {
|
|
|
|
unsigned MinColumn = 0;
|
|
|
|
unsigned MaxColumn = UINT_MAX;
|
|
|
|
comment_iterator Start = Comments.begin();
|
|
|
|
for (comment_iterator I = Comments.begin(), E = Comments.end(); I != E;
|
|
|
|
++I) {
|
|
|
|
if (I->MinColumn > MaxColumn || I->MaxColumn < MinColumn) {
|
|
|
|
alignComments(Start, I, MinColumn);
|
|
|
|
MinColumn = I->MinColumn;
|
|
|
|
MaxColumn = I->MaxColumn;
|
|
|
|
Start = I;
|
|
|
|
} else {
|
|
|
|
MinColumn = std::max(MinColumn, I->MinColumn);
|
|
|
|
MaxColumn = std::min(MaxColumn, I->MaxColumn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
alignComments(Start, Comments.end(), MinColumn);
|
|
|
|
Comments.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Put all the comments between \p I and \p E into \p Column.
|
|
|
|
void alignComments(comment_iterator I, comment_iterator E, unsigned Column) {
|
|
|
|
while (I != E) {
|
|
|
|
unsigned Spaces = I->Spaces + Column - I->MinColumn;
|
|
|
|
storeReplacement(I->Tok, std::string(I->NewLines, '\n') +
|
|
|
|
std::string(Spaces, ' '));
|
|
|
|
++I;
|
2013-01-11 02:45:26 +08:00
|
|
|
}
|
|
|
|
}
|
2013-01-18 16:44:07 +08:00
|
|
|
|
|
|
|
/// \brief Stores \p Text as the replacement for the whitespace in front of
|
|
|
|
/// \p Tok.
|
|
|
|
void storeReplacement(const FormatToken &Tok, const std::string Text) {
|
|
|
|
Replaces.insert(tooling::Replacement(SourceMgr, Tok.WhiteSpaceStart,
|
|
|
|
Tok.WhiteSpaceLength, Text));
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceManager &SourceMgr;
|
|
|
|
tooling::Replacements Replaces;
|
|
|
|
};
|
2013-01-11 02:45:26 +08:00
|
|
|
|
2013-01-13 06:48:47 +08:00
|
|
|
/// \brief Returns if a token is an Objective-C selector name.
|
|
|
|
///
|
2013-01-13 06:51:13 +08:00
|
|
|
/// For example, "bar" is a selector name in [foo bar:(4 + 5)].
|
2013-01-13 06:48:47 +08:00
|
|
|
static bool isObjCSelectorName(const AnnotatedToken &Tok) {
|
|
|
|
return Tok.is(tok::identifier) && !Tok.Children.empty() &&
|
|
|
|
Tok.Children[0].is(tok::colon) &&
|
|
|
|
Tok.Children[0].Type == TT_ObjCMethodExpr;
|
|
|
|
}
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
class UnwrappedLineFormatter {
|
|
|
|
public:
|
2013-01-11 03:17:33 +08:00
|
|
|
UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr,
|
2013-01-14 21:08:07 +08:00
|
|
|
const AnnotatedLine &Line, unsigned FirstIndent,
|
2013-01-16 18:41:46 +08:00
|
|
|
const AnnotatedToken &RootToken,
|
2013-01-18 16:44:07 +08:00
|
|
|
WhitespaceManager &Whitespaces, bool StructuralError)
|
2012-12-19 05:05:13 +08:00
|
|
|
: Style(Style), SourceMgr(SourceMgr), Line(Line),
|
2013-01-18 16:44:07 +08:00
|
|
|
FirstIndent(FirstIndent), RootToken(RootToken),
|
|
|
|
Whitespaces(Whitespaces) {
|
Reduce penalty for splitting between ")" and ".".
').' is likely part of a builder pattern statement.
This is based upon a patch developed by Nico Weber. Thank you!
Before:
int foo() {
return llvm::StringSwitch<Reference::Kind>(name).StartsWith(
".eh_frame_hdr", ORDER_EH_FRAMEHDR).StartsWith(
".eh_frame", ORDER_EH_FRAME).StartsWith(".init", ORDER_INIT).StartsWith(
".fini", ORDER_FINI).StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT);
}
After:
int foo() {
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT).StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT);
}
Probably not ideal, but makes many cases much more readable.
The changes to overriding-ftemplate-comments.cpp don't seem better or
worse. We should address those soon.
llvm-svn: 172804
2013-01-18 18:56:38 +08:00
|
|
|
Parameters.PenaltyIndentLevel = 20;
|
2013-01-09 18:16:05 +08:00
|
|
|
Parameters.PenaltyExcessCharacter = 1000000;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2013-01-05 07:34:14 +08:00
|
|
|
/// \brief Formats an \c UnwrappedLine.
|
|
|
|
///
|
|
|
|
/// \returns The column after the last token in the last line of the
|
|
|
|
/// \c UnwrappedLine.
|
|
|
|
unsigned format() {
|
2012-12-06 17:56:08 +08:00
|
|
|
// Initialize state dependent on indent.
|
2013-01-11 18:22:12 +08:00
|
|
|
LineState State;
|
2013-01-11 02:45:26 +08:00
|
|
|
State.Column = FirstIndent;
|
2013-01-08 22:56:18 +08:00
|
|
|
State.NextToken = &RootToken;
|
2013-01-11 19:37:55 +08:00
|
|
|
State.Stack.push_back(ParenState(FirstIndent + 4, FirstIndent));
|
2012-12-21 22:37:20 +08:00
|
|
|
State.ForLoopVariablePos = 0;
|
|
|
|
State.LineContainsContinuedForLoopSection = false;
|
2012-12-06 17:56:08 +08:00
|
|
|
|
2013-01-16 22:55:28 +08:00
|
|
|
DEBUG({
|
|
|
|
DebugTokenState(*State.NextToken);
|
|
|
|
});
|
|
|
|
|
2012-12-06 17:56:08 +08:00
|
|
|
// The first token has already been indented and thus consumed.
|
|
|
|
moveStateToNextToken(State);
|
2012-12-04 02:12:45 +08:00
|
|
|
|
|
|
|
// Start iterating at 1 as we have correctly formatted of Token #0 above.
|
2013-01-08 22:56:18 +08:00
|
|
|
while (State.NextToken != NULL) {
|
2013-01-18 17:19:33 +08:00
|
|
|
if (State.NextToken->Type == TT_ImplicitStringLiteral) {
|
|
|
|
// Calculating the column is important for aligning trailing comments.
|
|
|
|
// FIXME: This does not seem to happen in conjunction with escaped
|
|
|
|
// newlines. If it does, fix!
|
|
|
|
State.Column += State.NextToken->FormatTok.WhiteSpaceLength +
|
|
|
|
State.NextToken->FormatTok.TokenLength;
|
|
|
|
State.NextToken = State.NextToken->Children.empty() ? NULL :
|
|
|
|
&State.NextToken->Children[0];
|
|
|
|
} else if (Line.Last->TotalLength <= getColumnLimit() - FirstIndent) {
|
2012-12-19 05:05:13 +08:00
|
|
|
addTokenToState(false, false, State);
|
|
|
|
} else {
|
|
|
|
unsigned NoBreak = calcPenalty(State, false, UINT_MAX);
|
|
|
|
unsigned Break = calcPenalty(State, true, NoBreak);
|
2013-01-16 22:55:28 +08:00
|
|
|
DEBUG({
|
|
|
|
if (Break < NoBreak)
|
|
|
|
llvm::errs() << "\n";
|
|
|
|
else
|
|
|
|
llvm::errs() << " ";
|
|
|
|
llvm::errs() << "<";
|
|
|
|
DebugPenalty(Break, Break < NoBreak);
|
|
|
|
llvm::errs() << "/";
|
|
|
|
DebugPenalty(NoBreak, !(Break < NoBreak));
|
|
|
|
llvm::errs() << "> ";
|
|
|
|
DebugTokenState(*State.NextToken);
|
|
|
|
});
|
2012-12-19 05:05:13 +08:00
|
|
|
addTokenToState(Break < NoBreak, false, State);
|
2013-01-11 19:37:55 +08:00
|
|
|
if (State.NextToken != NULL &&
|
|
|
|
State.NextToken->Parent->Type == TT_CtorInitializerColon) {
|
|
|
|
if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine &&
|
2013-01-16 18:41:46 +08:00
|
|
|
Line.Last->TotalLength > getColumnLimit() - State.Column - 1)
|
2013-01-11 19:37:55 +08:00
|
|
|
State.Stack.back().BreakAfterComma = true;
|
|
|
|
}
|
2012-12-19 05:05:13 +08:00
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
2013-01-16 22:55:28 +08:00
|
|
|
DEBUG(llvm::errs() << "\n");
|
2013-01-05 07:34:14 +08:00
|
|
|
return State.Column;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-01-16 22:55:28 +08:00
|
|
|
void DebugTokenState(const AnnotatedToken &AnnotatedTok) {
|
|
|
|
const Token &Tok = AnnotatedTok.FormatTok.Tok;
|
|
|
|
llvm::errs()
|
|
|
|
<< StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
|
|
|
|
Tok.getLength());
|
|
|
|
llvm::errs();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DebugPenalty(unsigned Penalty, bool Winner) {
|
|
|
|
llvm::errs().changeColor(Winner ? raw_ostream::GREEN : raw_ostream::RED);
|
|
|
|
if (Penalty == UINT_MAX)
|
|
|
|
llvm::errs() << "MAX";
|
|
|
|
else
|
|
|
|
llvm::errs() << Penalty;
|
|
|
|
llvm::errs().resetColor();
|
|
|
|
}
|
|
|
|
|
2013-01-11 18:22:12 +08:00
|
|
|
struct ParenState {
|
2013-01-11 19:37:55 +08:00
|
|
|
ParenState(unsigned Indent, unsigned LastSpace)
|
2013-01-24 00:58:21 +08:00
|
|
|
: Indent(Indent), LastSpace(LastSpace), AssignmentColumn(0),
|
|
|
|
FirstLessLess(0), BreakBeforeClosingBrace(false),
|
|
|
|
BreakAfterComma(false), HasMultiParameterLine(false) {}
|
2012-12-25 00:43:00 +08:00
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
/// \brief The position to which a specific parenthesis level needs to be
|
|
|
|
/// indented.
|
2013-01-11 18:22:12 +08:00
|
|
|
unsigned Indent;
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2012-12-06 17:56:08 +08:00
|
|
|
/// \brief The position of the last space on each level.
|
|
|
|
///
|
|
|
|
/// Used e.g. to break like:
|
|
|
|
/// functionCall(Parameter, otherCall(
|
|
|
|
/// OtherParameter));
|
2013-01-11 18:22:12 +08:00
|
|
|
unsigned LastSpace;
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-01-24 00:58:21 +08:00
|
|
|
/// \brief This is the column of the first token after an assignment.
|
|
|
|
unsigned AssignmentColumn;
|
|
|
|
|
2012-12-06 17:56:08 +08:00
|
|
|
/// \brief The position the first "<<" operator encountered on each level.
|
|
|
|
///
|
|
|
|
/// Used to align "<<" operators. 0 if no such operator has been encountered
|
|
|
|
/// on a level.
|
2013-01-11 18:22:12 +08:00
|
|
|
unsigned FirstLessLess;
|
2012-12-06 17:56:08 +08:00
|
|
|
|
Fixes layout of right braces.
We now decide whether a newline should go before the closing brace
depending on whether a newline was inserted after the opening brace.
For example, we now insert a newline before '};' in:
static SomeClass WithALoooooooooooooooooooongName = {
100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"
};
... while not inserting a newline here:
static SomeClass = { a, b, c, d, e, f, g, h, i, j,
looooooooooooooooooooooooooooooooooongname,
looooooooooooooooooooooooooooooong };
Also fixes the formating of (column limit 25):
int x = {
avariable,
b(alongervariable)
};
llvm-svn: 172076
2013-01-10 23:58:26 +08:00
|
|
|
/// \brief Whether a newline needs to be inserted before the block's closing
|
|
|
|
/// brace.
|
|
|
|
///
|
|
|
|
/// We only want to insert a newline before the closing brace if there also
|
|
|
|
/// was a newline after the beginning left brace.
|
2013-01-11 18:22:12 +08:00
|
|
|
bool BreakBeforeClosingBrace;
|
|
|
|
|
2013-01-11 19:37:55 +08:00
|
|
|
bool BreakAfterComma;
|
2013-01-16 22:59:02 +08:00
|
|
|
bool HasMultiParameterLine;
|
2013-01-11 19:37:55 +08:00
|
|
|
|
2013-01-11 18:22:12 +08:00
|
|
|
bool operator<(const ParenState &Other) const {
|
|
|
|
if (Indent != Other.Indent)
|
2013-01-11 22:23:32 +08:00
|
|
|
return Indent < Other.Indent;
|
2013-01-11 18:22:12 +08:00
|
|
|
if (LastSpace != Other.LastSpace)
|
|
|
|
return LastSpace < Other.LastSpace;
|
2013-01-24 00:58:21 +08:00
|
|
|
if (AssignmentColumn != Other.AssignmentColumn)
|
|
|
|
return AssignmentColumn < Other.AssignmentColumn;
|
2013-01-11 18:22:12 +08:00
|
|
|
if (FirstLessLess != Other.FirstLessLess)
|
|
|
|
return FirstLessLess < Other.FirstLessLess;
|
2013-01-11 19:37:55 +08:00
|
|
|
if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace)
|
|
|
|
return BreakBeforeClosingBrace;
|
2013-01-12 15:36:22 +08:00
|
|
|
if (BreakAfterComma != Other.BreakAfterComma)
|
|
|
|
return BreakAfterComma;
|
2013-01-16 22:59:02 +08:00
|
|
|
if (HasMultiParameterLine != Other.HasMultiParameterLine)
|
|
|
|
return HasMultiParameterLine;
|
2013-01-12 15:36:22 +08:00
|
|
|
return false;
|
2013-01-11 18:22:12 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief The current state when indenting a unwrapped line.
|
|
|
|
///
|
|
|
|
/// As the indenting tries different combinations this is copied by value.
|
|
|
|
struct LineState {
|
|
|
|
/// \brief The number of used columns in the current line.
|
|
|
|
unsigned Column;
|
|
|
|
|
|
|
|
/// \brief The token that needs to be next formatted.
|
|
|
|
const AnnotatedToken *NextToken;
|
|
|
|
|
2012-12-21 22:37:20 +08:00
|
|
|
/// \brief The column of the first variable in a for-loop declaration.
|
|
|
|
///
|
|
|
|
/// Used to align the second variable if necessary.
|
|
|
|
unsigned ForLoopVariablePos;
|
|
|
|
|
|
|
|
/// \brief \c true if this line contains a continued for-loop section.
|
|
|
|
bool LineContainsContinuedForLoopSection;
|
|
|
|
|
2013-01-11 18:22:12 +08:00
|
|
|
/// \brief A stack keeping track of properties applying to parenthesis
|
|
|
|
/// levels.
|
|
|
|
std::vector<ParenState> Stack;
|
|
|
|
|
|
|
|
/// \brief Comparison operator to be able to used \c LineState in \c map.
|
|
|
|
bool operator<(const LineState &Other) const {
|
2013-01-08 22:56:18 +08:00
|
|
|
if (Other.NextToken != NextToken)
|
|
|
|
return Other.NextToken > NextToken;
|
2012-12-04 02:12:45 +08:00
|
|
|
if (Other.Column != Column)
|
|
|
|
return Other.Column > Column;
|
2012-12-21 22:37:20 +08:00
|
|
|
if (Other.ForLoopVariablePos != ForLoopVariablePos)
|
|
|
|
return Other.ForLoopVariablePos < ForLoopVariablePos;
|
|
|
|
if (Other.LineContainsContinuedForLoopSection !=
|
|
|
|
LineContainsContinuedForLoopSection)
|
|
|
|
return LineContainsContinuedForLoopSection;
|
2013-01-11 18:22:12 +08:00
|
|
|
return Other.Stack < Stack;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-12-04 22:54:30 +08:00
|
|
|
/// \brief Appends the next token to \p State and updates information
|
|
|
|
/// necessary for indentation.
|
|
|
|
///
|
|
|
|
/// Puts the token on the current line if \p Newline is \c true and adds a
|
|
|
|
/// line break and necessary indentation otherwise.
|
|
|
|
///
|
|
|
|
/// If \p DryRun is \c false, also creates and stores the required
|
|
|
|
/// \c Replacement.
|
2013-01-11 18:22:12 +08:00
|
|
|
void addTokenToState(bool Newline, bool DryRun, LineState &State) {
|
2013-01-09 15:06:56 +08:00
|
|
|
const AnnotatedToken &Current = *State.NextToken;
|
|
|
|
const AnnotatedToken &Previous = *State.NextToken->Parent;
|
2013-01-11 18:22:12 +08:00
|
|
|
assert(State.Stack.size());
|
|
|
|
unsigned ParenLevel = State.Stack.size() - 1;
|
2012-12-04 02:12:45 +08:00
|
|
|
|
|
|
|
if (Newline) {
|
2013-01-03 02:33:23 +08:00
|
|
|
unsigned WhitespaceStartColumn = State.Column;
|
2013-01-10 19:52:21 +08:00
|
|
|
if (Current.is(tok::r_brace)) {
|
|
|
|
State.Column = Line.Level * 2;
|
2013-01-09 15:06:56 +08:00
|
|
|
} else if (Current.is(tok::string_literal) &&
|
|
|
|
Previous.is(tok::string_literal)) {
|
|
|
|
State.Column = State.Column - Previous.FormatTok.TokenLength;
|
|
|
|
} else if (Current.is(tok::lessless) &&
|
2013-01-11 18:22:12 +08:00
|
|
|
State.Stack[ParenLevel].FirstLessLess != 0) {
|
|
|
|
State.Column = State.Stack[ParenLevel].FirstLessLess;
|
2012-12-21 22:37:20 +08:00
|
|
|
} else if (ParenLevel != 0 &&
|
2013-01-28 15:43:15 +08:00
|
|
|
(Previous.is(tok::equal) || Previous.is(tok::coloncolon) ||
|
|
|
|
Previous.is(tok::question) ||
|
|
|
|
Previous.Type == TT_ConditionalExpr ||
|
|
|
|
Current.is(tok::period) || Current.is(tok::arrow))) {
|
2013-01-09 15:06:56 +08:00
|
|
|
// Indent and extra 4 spaces after if we know the current expression is
|
|
|
|
// continued. Don't do that on the top level, as we already indent 4
|
|
|
|
// there.
|
2013-01-11 18:22:12 +08:00
|
|
|
State.Column = State.Stack[ParenLevel].Indent + 4;
|
2013-01-09 15:06:56 +08:00
|
|
|
} else if (RootToken.is(tok::kw_for) && Previous.is(tok::comma)) {
|
2012-12-21 22:37:20 +08:00
|
|
|
State.Column = State.ForLoopVariablePos;
|
2013-01-08 22:56:18 +08:00
|
|
|
} else if (State.NextToken->Parent->ClosesTemplateDeclaration) {
|
2013-01-11 18:22:12 +08:00
|
|
|
State.Column = State.Stack[ParenLevel].Indent - 4;
|
2013-01-24 00:58:21 +08:00
|
|
|
} else if (Previous.Type == TT_BinaryOperator &&
|
|
|
|
State.Stack.back().AssignmentColumn != 0) {
|
|
|
|
State.Column = State.Stack.back().AssignmentColumn;
|
2012-12-21 22:37:20 +08:00
|
|
|
} else {
|
2013-01-11 18:22:12 +08:00
|
|
|
State.Column = State.Stack[ParenLevel].Indent;
|
2012-12-21 22:37:20 +08:00
|
|
|
}
|
|
|
|
|
2013-01-08 22:56:18 +08:00
|
|
|
if (RootToken.is(tok::kw_for))
|
2013-01-09 15:06:56 +08:00
|
|
|
State.LineContainsContinuedForLoopSection = Previous.isNot(tok::semi);
|
2012-12-04 22:54:30 +08:00
|
|
|
|
2013-01-03 02:33:23 +08:00
|
|
|
if (!DryRun) {
|
|
|
|
if (!Line.InPPDirective)
|
2013-01-18 16:44:07 +08:00
|
|
|
Whitespaces.replaceWhitespace(Current, 1, State.Column,
|
|
|
|
WhitespaceStartColumn, Style);
|
2013-01-03 02:33:23 +08:00
|
|
|
else
|
2013-01-18 16:44:07 +08:00
|
|
|
Whitespaces.replacePPWhitespace(Current, 1, State.Column,
|
|
|
|
WhitespaceStartColumn, Style);
|
2013-01-03 02:33:23 +08:00
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-01-11 18:22:12 +08:00
|
|
|
State.Stack[ParenLevel].LastSpace = State.Column;
|
2013-01-12 15:05:25 +08:00
|
|
|
if (Current.is(tok::colon) && State.NextToken->Type != TT_ConditionalExpr)
|
2013-01-11 18:22:12 +08:00
|
|
|
State.Stack[ParenLevel].Indent += 2;
|
2012-12-04 02:12:45 +08:00
|
|
|
} else {
|
2013-01-09 15:06:56 +08:00
|
|
|
if (Current.is(tok::equal) && RootToken.is(tok::kw_for))
|
|
|
|
State.ForLoopVariablePos = State.Column -
|
|
|
|
Previous.FormatTok.TokenLength;
|
2012-12-21 22:37:20 +08:00
|
|
|
|
2013-01-08 22:56:18 +08:00
|
|
|
unsigned Spaces = State.NextToken->SpaceRequiredBefore ? 1 : 0;
|
|
|
|
if (State.NextToken->Type == TT_LineComment)
|
2013-01-07 19:09:06 +08:00
|
|
|
Spaces = Style.SpacesBeforeTrailingComments;
|
2012-12-04 22:54:30 +08:00
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
if (!DryRun)
|
2013-01-18 16:44:07 +08:00
|
|
|
Whitespaces.replaceWhitespace(Current, 0, Spaces, State.Column, Style);
|
2012-12-04 22:54:30 +08:00
|
|
|
|
2013-01-09 18:40:23 +08:00
|
|
|
// FIXME: Do we need to do this for assignments nested in other
|
|
|
|
// expressions?
|
|
|
|
if (RootToken.isNot(tok::kw_for) && ParenLevel == 0 &&
|
2013-01-07 21:08:40 +08:00
|
|
|
(getPrecedence(Previous) == prec::Assignment ||
|
2013-01-09 15:06:56 +08:00
|
|
|
Previous.is(tok::kw_return)))
|
2013-01-24 00:58:21 +08:00
|
|
|
State.Stack.back().AssignmentColumn = State.Column + Spaces;
|
2013-01-11 22:23:32 +08:00
|
|
|
if (Previous.is(tok::l_paren) || Previous.is(tok::l_brace) ||
|
2013-01-08 22:56:18 +08:00
|
|
|
State.NextToken->Parent->Type == TT_TemplateOpener)
|
2013-01-11 18:22:12 +08:00
|
|
|
State.Stack[ParenLevel].Indent = State.Column + Spaces;
|
2013-01-23 00:31:55 +08:00
|
|
|
if (Current.getPreviousNoneComment() != NULL &&
|
|
|
|
Current.getPreviousNoneComment()->is(tok::comma) &&
|
2013-01-17 20:53:34 +08:00
|
|
|
Current.isNot(tok::comment))
|
2013-01-16 22:59:02 +08:00
|
|
|
State.Stack[ParenLevel].HasMultiParameterLine = true;
|
|
|
|
|
2012-12-06 17:56:08 +08:00
|
|
|
State.Column += Spaces;
|
2013-01-24 04:41:06 +08:00
|
|
|
if (Current.is(tok::l_paren) && Previous.is(tok::kw_if))
|
|
|
|
// Treat the condition inside an if as if it was a second function
|
|
|
|
// parameter, i.e. let nested calls have an indent of 4.
|
|
|
|
State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(".
|
2013-01-25 23:43:32 +08:00
|
|
|
else if (Previous.is(tok::comma) && ParenLevel != 0)
|
2013-01-24 04:41:06 +08:00
|
|
|
// Top-level spaces are exempt as that mostly leads to better results.
|
|
|
|
State.Stack.back().LastSpace = State.Column;
|
2013-01-28 17:35:24 +08:00
|
|
|
else if (Previous.Type == TT_BinaryOperator &&
|
|
|
|
getPrecedence(Previous) != prec::Assignment)
|
|
|
|
State.Stack.back().LastSpace = State.Column;
|
2013-01-28 15:35:34 +08:00
|
|
|
else if (Previous.ParameterCount > 1 &&
|
|
|
|
(Previous.is(tok::l_paren) || Previous.is(tok::l_square) ||
|
|
|
|
Previous.Type == TT_TemplateOpener))
|
|
|
|
// If this function has multiple parameters, indent nested calls from
|
|
|
|
// the start of the first parameter.
|
|
|
|
State.Stack.back().LastSpace = State.Column;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
2013-01-16 22:59:02 +08:00
|
|
|
|
|
|
|
// If we break after an {, we should also break before the corresponding }.
|
|
|
|
if (Newline && Previous.is(tok::l_brace))
|
2013-01-11 18:22:12 +08:00
|
|
|
State.Stack.back().BreakBeforeClosingBrace = true;
|
2013-01-16 22:59:02 +08:00
|
|
|
|
2013-01-23 18:08:28 +08:00
|
|
|
if (!Style.BinPackParameters && Newline) {
|
|
|
|
// If we are breaking after '(', '{', '<', this is not bin packing unless
|
|
|
|
// AllowAllParametersOnNextLine is false.
|
|
|
|
if ((Previous.isNot(tok::l_paren) && Previous.isNot(tok::l_brace) &&
|
|
|
|
Previous.Type != TT_TemplateOpener) ||
|
|
|
|
!Style.AllowAllParametersOnNextLine)
|
|
|
|
State.Stack.back().BreakAfterComma = true;
|
|
|
|
|
|
|
|
// Any break on this level means that the parent level has been broken
|
|
|
|
// and we need to avoid bin packing there.
|
|
|
|
for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
|
|
|
|
State.Stack[i].BreakAfterComma = true;
|
|
|
|
}
|
|
|
|
}
|
2013-01-16 22:59:02 +08:00
|
|
|
|
Fixes formatting of nested brace initializers.
We now format this correctly:
Status::Rep Status::global_reps[3] = {
{ kGlobalRef, OK_CODE, NULL, NULL, NULL },
{ kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },
{ kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }
};
- fixed a bug where BreakBeforeClosingBrace would be set on the wrong
state
- added penalties for breaking between = and {, and between { and any
other non-{ token
llvm-svn: 172433
2013-01-15 00:41:43 +08:00
|
|
|
moveStateToNextToken(State);
|
2012-12-04 22:54:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Mark the next token as consumed in \p State and modify its stacks
|
|
|
|
/// accordingly.
|
2013-01-11 18:22:12 +08:00
|
|
|
void moveStateToNextToken(LineState &State) {
|
2013-01-08 22:56:18 +08:00
|
|
|
const AnnotatedToken &Current = *State.NextToken;
|
2013-01-11 18:22:12 +08:00
|
|
|
assert(State.Stack.size());
|
2012-12-06 17:56:08 +08:00
|
|
|
|
2013-01-11 18:22:12 +08:00
|
|
|
if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
|
|
|
|
State.Stack.back().FirstLessLess = State.Column;
|
2012-12-06 17:56:08 +08:00
|
|
|
|
2012-12-24 21:43:52 +08:00
|
|
|
// If we encounter an opening (, [, { or <, we add a level to our stacks to
|
2012-12-04 22:54:30 +08:00
|
|
|
// prepare for the following tokens.
|
2013-01-08 22:56:18 +08:00
|
|
|
if (Current.is(tok::l_paren) || Current.is(tok::l_square) ||
|
|
|
|
Current.is(tok::l_brace) ||
|
|
|
|
State.NextToken->Type == TT_TemplateOpener) {
|
2013-01-11 18:22:12 +08:00
|
|
|
unsigned NewIndent;
|
2013-01-10 22:36:46 +08:00
|
|
|
if (Current.is(tok::l_brace)) {
|
|
|
|
// FIXME: This does not work with nested static initializers.
|
|
|
|
// Implement a better handling for static initializers and similar
|
|
|
|
// constructs.
|
2013-01-11 18:22:12 +08:00
|
|
|
NewIndent = Line.Level * 2 + 2;
|
2013-01-10 22:36:46 +08:00
|
|
|
} else {
|
2013-01-11 18:22:12 +08:00
|
|
|
NewIndent = 4 + State.Stack.back().LastSpace;
|
2013-01-10 22:36:46 +08:00
|
|
|
}
|
2013-01-11 18:22:12 +08:00
|
|
|
State.Stack.push_back(
|
2013-01-11 19:37:55 +08:00
|
|
|
ParenState(NewIndent, State.Stack.back().LastSpace));
|
2012-12-04 22:54:30 +08:00
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2012-12-24 21:43:52 +08:00
|
|
|
// If we encounter a closing ), ], } or >, we can remove a level from our
|
2012-12-04 22:54:30 +08:00
|
|
|
// stacks.
|
2013-01-08 22:56:18 +08:00
|
|
|
if (Current.is(tok::r_paren) || Current.is(tok::r_square) ||
|
|
|
|
(Current.is(tok::r_brace) && State.NextToken != &RootToken) ||
|
|
|
|
State.NextToken->Type == TT_TemplateCloser) {
|
2013-01-11 18:22:12 +08:00
|
|
|
State.Stack.pop_back();
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
2013-01-10 22:36:46 +08:00
|
|
|
|
2013-01-08 22:56:18 +08:00
|
|
|
if (State.NextToken->Children.empty())
|
|
|
|
State.NextToken = NULL;
|
|
|
|
else
|
|
|
|
State.NextToken = &State.NextToken->Children[0];
|
2013-01-10 22:36:46 +08:00
|
|
|
|
|
|
|
State.Column += Current.FormatTok.TokenLength;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2013-01-07 23:15:29 +08:00
|
|
|
/// \brief Calculate the penalty for splitting after the token at \p Index.
|
2013-01-08 22:56:18 +08:00
|
|
|
unsigned splitPenalty(const AnnotatedToken &Tok) {
|
|
|
|
const AnnotatedToken &Left = Tok;
|
|
|
|
const AnnotatedToken &Right = Tok.Children[0];
|
2012-12-21 22:37:20 +08:00
|
|
|
|
Fixes formatting of nested brace initializers.
We now format this correctly:
Status::Rep Status::global_reps[3] = {
{ kGlobalRef, OK_CODE, NULL, NULL, NULL },
{ kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },
{ kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }
};
- fixed a bug where BreakBeforeClosingBrace would be set on the wrong
state
- added penalties for breaking between = and {, and between { and any
other non-{ token
llvm-svn: 172433
2013-01-15 00:41:43 +08:00
|
|
|
if (Left.is(tok::l_brace) && Right.isNot(tok::l_brace))
|
|
|
|
return 50;
|
|
|
|
if (Left.is(tok::equal) && Right.is(tok::l_brace))
|
|
|
|
return 150;
|
2013-01-25 18:57:27 +08:00
|
|
|
if (Left.is(tok::coloncolon))
|
|
|
|
return 500;
|
Fixes formatting of nested brace initializers.
We now format this correctly:
Status::Rep Status::global_reps[3] = {
{ kGlobalRef, OK_CODE, NULL, NULL, NULL },
{ kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },
{ kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }
};
- fixed a bug where BreakBeforeClosingBrace would be set on the wrong
state
- added penalties for breaking between = and {, and between { and any
other non-{ token
llvm-svn: 172433
2013-01-15 00:41:43 +08:00
|
|
|
|
2012-12-21 22:37:20 +08:00
|
|
|
// In for-loops, prefer breaking at ',' and ';'.
|
2013-01-08 22:56:18 +08:00
|
|
|
if (RootToken.is(tok::kw_for) &&
|
|
|
|
(Left.isNot(tok::comma) && Left.isNot(tok::semi)))
|
2012-12-21 22:37:20 +08:00
|
|
|
return 20;
|
|
|
|
|
Reduce penalty for splitting between ")" and ".".
').' is likely part of a builder pattern statement.
This is based upon a patch developed by Nico Weber. Thank you!
Before:
int foo() {
return llvm::StringSwitch<Reference::Kind>(name).StartsWith(
".eh_frame_hdr", ORDER_EH_FRAMEHDR).StartsWith(
".eh_frame", ORDER_EH_FRAME).StartsWith(".init", ORDER_INIT).StartsWith(
".fini", ORDER_FINI).StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT);
}
After:
int foo() {
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT).StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT);
}
Probably not ideal, but makes many cases much more readable.
The changes to overriding-ftemplate-comments.cpp don't seem better or
worse. We should address those soon.
llvm-svn: 172804
2013-01-18 18:56:38 +08:00
|
|
|
if (Left.is(tok::semi) || Left.is(tok::comma))
|
2012-12-04 02:12:45 +08:00
|
|
|
return 0;
|
2013-01-13 06:48:47 +08:00
|
|
|
|
|
|
|
// In Objective-C method expressions, prefer breaking before "param:" over
|
|
|
|
// breaking after it.
|
|
|
|
if (isObjCSelectorName(Right))
|
|
|
|
return 0;
|
|
|
|
if (Right.is(tok::colon) && Right.Type == TT_ObjCMethodExpr)
|
|
|
|
return 20;
|
|
|
|
|
2013-01-28 15:35:34 +08:00
|
|
|
if (Left.is(tok::l_paren))
|
2013-01-02 22:40:02 +08:00
|
|
|
return 20;
|
2013-01-28 15:35:34 +08:00
|
|
|
// FIXME: The penalty for a trailing "<" or "[" being higher than the
|
|
|
|
// penalty for a trainling "(" is a temporary workaround until we can
|
|
|
|
// properly avoid breaking in array subscripts or template parameters.
|
|
|
|
if (Left.is(tok::l_square) || Left.Type == TT_TemplateOpener)
|
|
|
|
return 50;
|
2012-12-17 22:34:14 +08:00
|
|
|
|
2013-01-09 15:06:56 +08:00
|
|
|
if (Left.is(tok::question) || Left.Type == TT_ConditionalExpr)
|
|
|
|
return prec::Assignment;
|
2013-01-07 21:08:40 +08:00
|
|
|
prec::Level Level = getPrecedence(Left);
|
|
|
|
|
2012-12-24 08:13:23 +08:00
|
|
|
if (Level != prec::Unknown)
|
|
|
|
return Level;
|
|
|
|
|
Reduce penalty for splitting between ")" and ".".
').' is likely part of a builder pattern statement.
This is based upon a patch developed by Nico Weber. Thank you!
Before:
int foo() {
return llvm::StringSwitch<Reference::Kind>(name).StartsWith(
".eh_frame_hdr", ORDER_EH_FRAMEHDR).StartsWith(
".eh_frame", ORDER_EH_FRAME).StartsWith(".init", ORDER_INIT).StartsWith(
".fini", ORDER_FINI).StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT);
}
After:
int foo() {
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT).StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT);
}
Probably not ideal, but makes many cases much more readable.
The changes to overriding-ftemplate-comments.cpp don't seem better or
worse. We should address those soon.
llvm-svn: 172804
2013-01-18 18:56:38 +08:00
|
|
|
if (Right.is(tok::arrow) || Right.is(tok::period)) {
|
2013-01-22 22:28:24 +08:00
|
|
|
if (Left.is(tok::r_paren) && Line.Type == LT_BuilderTypeCall)
|
2013-01-28 17:35:24 +08:00
|
|
|
return 5; // Should be smaller than breaking at a nested comma.
|
2013-01-07 15:13:20 +08:00
|
|
|
return 150;
|
Reduce penalty for splitting between ")" and ".".
').' is likely part of a builder pattern statement.
This is based upon a patch developed by Nico Weber. Thank you!
Before:
int foo() {
return llvm::StringSwitch<Reference::Kind>(name).StartsWith(
".eh_frame_hdr", ORDER_EH_FRAMEHDR).StartsWith(
".eh_frame", ORDER_EH_FRAME).StartsWith(".init", ORDER_INIT).StartsWith(
".fini", ORDER_FINI).StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT);
}
After:
int foo() {
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT).StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT);
}
Probably not ideal, but makes many cases much more readable.
The changes to overriding-ftemplate-comments.cpp don't seem better or
worse. We should address those soon.
llvm-svn: 172804
2013-01-18 18:56:38 +08:00
|
|
|
}
|
2012-12-17 22:34:14 +08:00
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2013-01-09 18:16:05 +08:00
|
|
|
unsigned getColumnLimit() {
|
|
|
|
return Style.ColumnLimit - (Line.InPPDirective ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
/// \brief Calculate the number of lines needed to format the remaining part
|
|
|
|
/// of the unwrapped line.
|
|
|
|
///
|
|
|
|
/// Assumes the formatting so far has led to
|
2013-01-11 18:22:12 +08:00
|
|
|
/// the \c LineSta \p State. If \p NewLine is set, a new line will be
|
2012-12-04 02:12:45 +08:00
|
|
|
/// added after the previous token.
|
|
|
|
///
|
|
|
|
/// \param StopAt is used for optimization. If we can determine that we'll
|
|
|
|
/// definitely need at least \p StopAt additional lines, we already know of a
|
|
|
|
/// better solution.
|
2013-01-11 18:22:12 +08:00
|
|
|
unsigned calcPenalty(LineState State, bool NewLine, unsigned StopAt) {
|
2012-12-04 02:12:45 +08:00
|
|
|
// We are at the end of the unwrapped line, so we don't need any more lines.
|
2013-01-08 22:56:18 +08:00
|
|
|
if (State.NextToken == NULL)
|
2012-12-04 02:12:45 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-01-08 22:56:18 +08:00
|
|
|
if (!NewLine && State.NextToken->MustBreakBefore)
|
2012-12-04 02:12:45 +08:00
|
|
|
return UINT_MAX;
|
Fixes formatting of nested brace initializers.
We now format this correctly:
Status::Rep Status::global_reps[3] = {
{ kGlobalRef, OK_CODE, NULL, NULL, NULL },
{ kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },
{ kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }
};
- fixed a bug where BreakBeforeClosingBrace would be set on the wrong
state
- added penalties for breaking between = and {, and between { and any
other non-{ token
llvm-svn: 172433
2013-01-15 00:41:43 +08:00
|
|
|
if (NewLine && !State.NextToken->CanBreakBefore &&
|
|
|
|
!(State.NextToken->is(tok::r_brace) &&
|
|
|
|
State.Stack.back().BreakBeforeClosingBrace))
|
2012-12-04 02:12:45 +08:00
|
|
|
return UINT_MAX;
|
Fixes layout of right braces.
We now decide whether a newline should go before the closing brace
depending on whether a newline was inserted after the opening brace.
For example, we now insert a newline before '};' in:
static SomeClass WithALoooooooooooooooooooongName = {
100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"
};
... while not inserting a newline here:
static SomeClass = { a, b, c, d, e, f, g, h, i, j,
looooooooooooooooooooooooooooooooooongname,
looooooooooooooooooooooooooooooong };
Also fixes the formating of (column limit 25):
int x = {
avariable,
b(alongervariable)
};
llvm-svn: 172076
2013-01-10 23:58:26 +08:00
|
|
|
if (!NewLine && State.NextToken->is(tok::r_brace) &&
|
2013-01-11 18:22:12 +08:00
|
|
|
State.Stack.back().BreakBeforeClosingBrace)
|
Fixes layout of right braces.
We now decide whether a newline should go before the closing brace
depending on whether a newline was inserted after the opening brace.
For example, we now insert a newline before '};' in:
static SomeClass WithALoooooooooooooooooooongName = {
100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"
};
... while not inserting a newline here:
static SomeClass = { a, b, c, d, e, f, g, h, i, j,
looooooooooooooooooooooooooooooooooongname,
looooooooooooooooooooooooooooooong };
Also fixes the formating of (column limit 25):
int x = {
avariable,
b(alongervariable)
};
llvm-svn: 172076
2013-01-10 23:58:26 +08:00
|
|
|
return UINT_MAX;
|
2013-01-08 22:56:18 +08:00
|
|
|
if (!NewLine && State.NextToken->Parent->is(tok::semi) &&
|
2012-12-21 22:37:20 +08:00
|
|
|
State.LineContainsContinuedForLoopSection)
|
|
|
|
return UINT_MAX;
|
2013-01-11 19:37:55 +08:00
|
|
|
if (!NewLine && State.NextToken->Parent->is(tok::comma) &&
|
2013-01-17 20:53:34 +08:00
|
|
|
State.NextToken->isNot(tok::comment) &&
|
2013-01-11 19:37:55 +08:00
|
|
|
State.Stack.back().BreakAfterComma)
|
|
|
|
return UINT_MAX;
|
2013-01-16 22:59:02 +08:00
|
|
|
// Trying to insert a parameter on a new line if there are already more than
|
|
|
|
// one parameter on the current line is bin packing.
|
|
|
|
if (NewLine && State.NextToken->Parent->is(tok::comma) &&
|
|
|
|
State.Stack.back().HasMultiParameterLine && !Style.BinPackParameters)
|
|
|
|
return UINT_MAX;
|
Reduce penalty for splitting between ")" and ".".
').' is likely part of a builder pattern statement.
This is based upon a patch developed by Nico Weber. Thank you!
Before:
int foo() {
return llvm::StringSwitch<Reference::Kind>(name).StartsWith(
".eh_frame_hdr", ORDER_EH_FRAMEHDR).StartsWith(
".eh_frame", ORDER_EH_FRAME).StartsWith(".init", ORDER_INIT).StartsWith(
".fini", ORDER_FINI).StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT);
}
After:
int foo() {
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT).StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT);
}
Probably not ideal, but makes many cases much more readable.
The changes to overriding-ftemplate-comments.cpp don't seem better or
worse. We should address those soon.
llvm-svn: 172804
2013-01-18 18:56:38 +08:00
|
|
|
if (!NewLine && (State.NextToken->Type == TT_CtorInitializerColon ||
|
|
|
|
(State.NextToken->Parent->ClosesTemplateDeclaration &&
|
|
|
|
State.Stack.size() == 1)))
|
2013-01-16 18:41:46 +08:00
|
|
|
return UINT_MAX;
|
2012-12-04 02:12:45 +08:00
|
|
|
|
|
|
|
unsigned CurrentPenalty = 0;
|
2013-01-28 17:35:24 +08:00
|
|
|
if (NewLine)
|
2013-01-11 18:22:12 +08:00
|
|
|
CurrentPenalty += Parameters.PenaltyIndentLevel * State.Stack.size() +
|
2013-01-08 22:56:18 +08:00
|
|
|
splitPenalty(*State.NextToken->Parent);
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2012-12-05 22:57:28 +08:00
|
|
|
addTokenToState(NewLine, true, State);
|
|
|
|
|
2013-01-09 18:16:05 +08:00
|
|
|
// Exceeding column limit is bad, assign penalty.
|
|
|
|
if (State.Column > getColumnLimit()) {
|
|
|
|
unsigned ExcessCharacters = State.Column - getColumnLimit();
|
|
|
|
CurrentPenalty += Parameters.PenaltyExcessCharacter * ExcessCharacters;
|
|
|
|
}
|
2012-12-05 22:57:28 +08:00
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
if (StopAt <= CurrentPenalty)
|
|
|
|
return UINT_MAX;
|
|
|
|
StopAt -= CurrentPenalty;
|
|
|
|
StateMap::iterator I = Memory.find(State);
|
2012-12-05 22:57:28 +08:00
|
|
|
if (I != Memory.end()) {
|
|
|
|
// If this state has already been examined, we can safely return the
|
|
|
|
// previous result if we
|
|
|
|
// - have not hit the optimatization (and thus returned UINT_MAX) OR
|
|
|
|
// - are now computing for a smaller or equal StopAt.
|
|
|
|
unsigned SavedResult = I->second.first;
|
|
|
|
unsigned SavedStopAt = I->second.second;
|
2012-12-17 22:34:14 +08:00
|
|
|
if (SavedResult != UINT_MAX)
|
|
|
|
return SavedResult + CurrentPenalty;
|
|
|
|
else if (StopAt <= SavedStopAt)
|
|
|
|
return UINT_MAX;
|
2012-12-05 22:57:28 +08:00
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
|
|
|
|
unsigned NoBreak = calcPenalty(State, false, StopAt);
|
|
|
|
unsigned WithBreak = calcPenalty(State, true, std::min(StopAt, NoBreak));
|
|
|
|
unsigned Result = std::min(NoBreak, WithBreak);
|
2012-12-17 22:34:14 +08:00
|
|
|
|
|
|
|
// We have to store 'Result' without adding 'CurrentPenalty' as the latter
|
|
|
|
// can depend on 'NewLine'.
|
2012-12-05 22:57:28 +08:00
|
|
|
Memory[State] = std::pair<unsigned, unsigned>(Result, StopAt);
|
2012-12-17 22:34:14 +08:00
|
|
|
|
|
|
|
return Result == UINT_MAX ? UINT_MAX : Result + CurrentPenalty;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FormatStyle Style;
|
|
|
|
SourceManager &SourceMgr;
|
2013-01-14 21:08:07 +08:00
|
|
|
const AnnotatedLine &Line;
|
2013-01-11 02:45:26 +08:00
|
|
|
const unsigned FirstIndent;
|
2013-01-08 22:56:18 +08:00
|
|
|
const AnnotatedToken &RootToken;
|
2013-01-18 16:44:07 +08:00
|
|
|
WhitespaceManager &Whitespaces;
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2012-12-05 22:57:28 +08:00
|
|
|
// A map from an indent state to a pair (Result, Used-StopAt).
|
2013-01-11 18:22:12 +08:00
|
|
|
typedef std::map<LineState, std::pair<unsigned, unsigned> > StateMap;
|
2012-12-05 22:57:28 +08:00
|
|
|
StateMap Memory;
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
OptimizationParameters Parameters;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Determines extra information about the tokens comprising an
|
|
|
|
/// \c UnwrappedLine.
|
|
|
|
class TokenAnnotator {
|
|
|
|
public:
|
2013-01-14 21:08:07 +08:00
|
|
|
TokenAnnotator(const FormatStyle &Style, SourceManager &SourceMgr, Lexer &Lex,
|
|
|
|
AnnotatedLine &Line)
|
|
|
|
: Style(Style), SourceMgr(SourceMgr), Lex(Lex), Line(Line) {}
|
2012-12-04 02:12:45 +08:00
|
|
|
|
|
|
|
/// \brief A parser that gathers additional information about tokens.
|
|
|
|
///
|
|
|
|
/// The \c TokenAnnotator tries to matches parenthesis and square brakets and
|
|
|
|
/// store a parenthesis levels. It also tries to resolve matching "<" and ">"
|
|
|
|
/// into template parameter lists.
|
|
|
|
class AnnotatingParser {
|
|
|
|
public:
|
2013-01-08 22:56:18 +08:00
|
|
|
AnnotatingParser(AnnotatedToken &RootToken)
|
2013-01-12 14:18:40 +08:00
|
|
|
: CurrentToken(&RootToken), KeywordVirtualFound(false),
|
|
|
|
ColonIsObjCMethodExpr(false) {}
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-01-18 10:43:57 +08:00
|
|
|
/// \brief A helper class to manage AnnotatingParser::ColonIsObjCMethodExpr.
|
|
|
|
struct ObjCSelectorRAII {
|
|
|
|
AnnotatingParser &P;
|
|
|
|
bool ColonWasObjCMethodExpr;
|
|
|
|
|
|
|
|
ObjCSelectorRAII(AnnotatingParser &P)
|
|
|
|
: P(P), ColonWasObjCMethodExpr(P.ColonIsObjCMethodExpr) {}
|
|
|
|
|
|
|
|
~ObjCSelectorRAII() { P.ColonIsObjCMethodExpr = ColonWasObjCMethodExpr; }
|
|
|
|
|
|
|
|
void markStart(AnnotatedToken &Left) {
|
|
|
|
P.ColonIsObjCMethodExpr = true;
|
|
|
|
Left.Type = TT_ObjCMethodExpr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void markEnd(AnnotatedToken &Right) { Right.Type = TT_ObjCMethodExpr; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-12-04 22:54:30 +08:00
|
|
|
bool parseAngle() {
|
2013-01-16 22:59:02 +08:00
|
|
|
if (CurrentToken == NULL)
|
|
|
|
return false;
|
|
|
|
AnnotatedToken *Left = CurrentToken->Parent;
|
2013-01-08 22:56:18 +08:00
|
|
|
while (CurrentToken != NULL) {
|
|
|
|
if (CurrentToken->is(tok::greater)) {
|
2013-01-16 22:59:02 +08:00
|
|
|
Left->MatchingParen = CurrentToken;
|
|
|
|
CurrentToken->MatchingParen = Left;
|
2013-01-08 22:56:18 +08:00
|
|
|
CurrentToken->Type = TT_TemplateCloser;
|
2012-12-04 02:12:45 +08:00
|
|
|
next();
|
|
|
|
return true;
|
|
|
|
}
|
2013-01-08 22:56:18 +08:00
|
|
|
if (CurrentToken->is(tok::r_paren) || CurrentToken->is(tok::r_square) ||
|
|
|
|
CurrentToken->is(tok::r_brace))
|
2012-12-04 02:12:45 +08:00
|
|
|
return false;
|
2013-01-08 22:56:18 +08:00
|
|
|
if (CurrentToken->is(tok::pipepipe) || CurrentToken->is(tok::ampamp) ||
|
|
|
|
CurrentToken->is(tok::question) || CurrentToken->is(tok::colon))
|
2012-12-04 02:12:45 +08:00
|
|
|
return false;
|
2013-01-28 15:35:34 +08:00
|
|
|
if (CurrentToken->is(tok::comma))
|
|
|
|
++Left->ParameterCount;
|
2013-01-05 02:52:56 +08:00
|
|
|
if (!consumeToken())
|
|
|
|
return false;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-18 01:17:19 +08:00
|
|
|
bool parseParens(bool LookForDecls = false) {
|
2013-01-16 22:59:02 +08:00
|
|
|
if (CurrentToken == NULL)
|
|
|
|
return false;
|
2013-01-18 10:43:57 +08:00
|
|
|
bool StartsObjCMethodExpr = false;
|
2013-01-16 22:59:02 +08:00
|
|
|
AnnotatedToken *Left = CurrentToken->Parent;
|
2013-01-18 10:43:57 +08:00
|
|
|
if (CurrentToken->is(tok::caret)) {
|
|
|
|
// ^( starts a block.
|
2013-01-16 22:59:02 +08:00
|
|
|
Left->Type = TT_ObjCBlockLParen;
|
2013-01-18 10:43:57 +08:00
|
|
|
} else if (AnnotatedToken *MaybeSel = Left->Parent) {
|
|
|
|
// @selector( starts a selector.
|
|
|
|
if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Parent &&
|
|
|
|
MaybeSel->Parent->is(tok::at)) {
|
|
|
|
StartsObjCMethodExpr = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjCSelectorRAII objCSelector(*this);
|
|
|
|
if (StartsObjCMethodExpr)
|
|
|
|
objCSelector.markStart(*Left);
|
|
|
|
|
2013-01-08 22:56:18 +08:00
|
|
|
while (CurrentToken != NULL) {
|
2013-01-18 01:17:19 +08:00
|
|
|
// LookForDecls is set when "if (" has been seen. Check for
|
|
|
|
// 'identifier' '*' 'identifier' followed by not '=' -- this
|
|
|
|
// '*' has to be a binary operator but determineStarAmpUsage() will
|
|
|
|
// categorize it as an unary operator, so set the right type here.
|
|
|
|
if (LookForDecls && !CurrentToken->Children.empty()) {
|
|
|
|
AnnotatedToken &Prev = *CurrentToken->Parent;
|
|
|
|
AnnotatedToken &Next = CurrentToken->Children[0];
|
|
|
|
if (Prev.Parent->is(tok::identifier) &&
|
|
|
|
(Prev.is(tok::star) || Prev.is(tok::amp)) &&
|
|
|
|
CurrentToken->is(tok::identifier) && Next.isNot(tok::equal)) {
|
|
|
|
Prev.Type = TT_BinaryOperator;
|
|
|
|
LookForDecls = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-08 22:56:18 +08:00
|
|
|
if (CurrentToken->is(tok::r_paren)) {
|
2013-01-16 22:59:02 +08:00
|
|
|
Left->MatchingParen = CurrentToken;
|
|
|
|
CurrentToken->MatchingParen = Left;
|
2013-01-18 10:43:57 +08:00
|
|
|
|
|
|
|
if (StartsObjCMethodExpr)
|
|
|
|
objCSelector.markEnd(*CurrentToken);
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
next();
|
|
|
|
return true;
|
|
|
|
}
|
2013-01-08 22:56:18 +08:00
|
|
|
if (CurrentToken->is(tok::r_square) || CurrentToken->is(tok::r_brace))
|
2013-01-05 02:52:56 +08:00
|
|
|
return false;
|
2013-01-28 15:35:34 +08:00
|
|
|
if (CurrentToken->is(tok::comma))
|
|
|
|
++Left->ParameterCount;
|
2013-01-05 02:52:56 +08:00
|
|
|
if (!consumeToken())
|
2012-12-04 02:12:45 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-04 22:54:30 +08:00
|
|
|
bool parseSquare() {
|
2013-01-12 14:18:40 +08:00
|
|
|
if (!CurrentToken)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// A '[' could be an index subscript (after an indentifier or after
|
|
|
|
// ')' or ']'), or it could be the start of an Objective-C method
|
|
|
|
// expression.
|
2013-01-22 03:29:31 +08:00
|
|
|
AnnotatedToken *Left = CurrentToken->Parent;
|
2013-01-12 14:18:40 +08:00
|
|
|
bool StartsObjCMethodExpr =
|
2013-01-22 03:29:31 +08:00
|
|
|
!Left->Parent || Left->Parent->is(tok::colon) ||
|
|
|
|
Left->Parent->is(tok::l_square) || Left->Parent->is(tok::l_paren) ||
|
|
|
|
Left->Parent->is(tok::kw_return) || Left->Parent->is(tok::kw_throw) ||
|
|
|
|
getBinOpPrecedence(Left->Parent->FormatTok.Tok.getKind(),
|
2013-01-12 14:18:40 +08:00
|
|
|
true, true) > prec::Unknown;
|
|
|
|
|
2013-01-18 10:43:57 +08:00
|
|
|
ObjCSelectorRAII objCSelector(*this);
|
|
|
|
if (StartsObjCMethodExpr)
|
2013-01-22 03:29:31 +08:00
|
|
|
objCSelector.markStart(*Left);
|
2013-01-12 14:18:40 +08:00
|
|
|
|
2013-01-08 22:56:18 +08:00
|
|
|
while (CurrentToken != NULL) {
|
|
|
|
if (CurrentToken->is(tok::r_square)) {
|
2013-01-22 19:46:26 +08:00
|
|
|
if (!CurrentToken->Children.empty() &&
|
|
|
|
CurrentToken->Children[0].is(tok::l_paren)) {
|
|
|
|
// An ObjC method call can't be followed by an open parenthesis.
|
|
|
|
// FIXME: Do we incorrectly label ":" with this?
|
|
|
|
StartsObjCMethodExpr = false;
|
|
|
|
Left->Type = TT_Unknown;
|
|
|
|
}
|
2013-01-18 10:43:57 +08:00
|
|
|
if (StartsObjCMethodExpr)
|
|
|
|
objCSelector.markEnd(*CurrentToken);
|
2013-01-22 03:35:06 +08:00
|
|
|
Left->MatchingParen = CurrentToken;
|
|
|
|
CurrentToken->MatchingParen = Left;
|
2012-12-04 02:12:45 +08:00
|
|
|
next();
|
|
|
|
return true;
|
|
|
|
}
|
2013-01-08 22:56:18 +08:00
|
|
|
if (CurrentToken->is(tok::r_paren) || CurrentToken->is(tok::r_brace))
|
2013-01-05 02:52:56 +08:00
|
|
|
return false;
|
2013-01-28 15:35:34 +08:00
|
|
|
if (CurrentToken->is(tok::comma))
|
|
|
|
++Left->ParameterCount;
|
2013-01-05 02:52:56 +08:00
|
|
|
if (!consumeToken())
|
2012-12-04 02:12:45 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-10 17:26:47 +08:00
|
|
|
bool parseBrace() {
|
2013-01-16 22:59:02 +08:00
|
|
|
// Lines are fine to end with '{'.
|
|
|
|
if (CurrentToken == NULL)
|
|
|
|
return true;
|
|
|
|
AnnotatedToken *Left = CurrentToken->Parent;
|
2013-01-10 17:26:47 +08:00
|
|
|
while (CurrentToken != NULL) {
|
|
|
|
if (CurrentToken->is(tok::r_brace)) {
|
2013-01-16 22:59:02 +08:00
|
|
|
Left->MatchingParen = CurrentToken;
|
|
|
|
CurrentToken->MatchingParen = Left;
|
2013-01-10 17:26:47 +08:00
|
|
|
next();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (CurrentToken->is(tok::r_paren) || CurrentToken->is(tok::r_square))
|
|
|
|
return false;
|
|
|
|
if (!consumeToken())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-12-04 22:54:30 +08:00
|
|
|
bool parseConditional() {
|
2013-01-08 22:56:18 +08:00
|
|
|
while (CurrentToken != NULL) {
|
|
|
|
if (CurrentToken->is(tok::colon)) {
|
|
|
|
CurrentToken->Type = TT_ConditionalExpr;
|
2012-12-04 02:12:45 +08:00
|
|
|
next();
|
|
|
|
return true;
|
|
|
|
}
|
2013-01-05 02:52:56 +08:00
|
|
|
if (!consumeToken())
|
|
|
|
return false;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-02 23:08:56 +08:00
|
|
|
bool parseTemplateDeclaration() {
|
2013-01-08 22:56:18 +08:00
|
|
|
if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
|
|
|
|
CurrentToken->Type = TT_TemplateOpener;
|
2013-01-02 23:08:56 +08:00
|
|
|
next();
|
|
|
|
if (!parseAngle())
|
|
|
|
return false;
|
2013-01-08 22:56:18 +08:00
|
|
|
CurrentToken->Parent->ClosesTemplateDeclaration = true;
|
2013-01-02 23:08:56 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-05 02:52:56 +08:00
|
|
|
bool consumeToken() {
|
2013-01-08 22:56:18 +08:00
|
|
|
AnnotatedToken *Tok = CurrentToken;
|
2012-12-04 02:12:45 +08:00
|
|
|
next();
|
2013-01-08 22:56:18 +08:00
|
|
|
switch (Tok->FormatTok.Tok.getKind()) {
|
2013-01-11 07:11:41 +08:00
|
|
|
case tok::plus:
|
|
|
|
case tok::minus:
|
|
|
|
// At the start of the line, +/- specific ObjectiveC method
|
|
|
|
// declarations.
|
|
|
|
if (Tok->Parent == NULL)
|
|
|
|
Tok->Type = TT_ObjCMethodSpecifier;
|
|
|
|
break;
|
2013-01-12 14:18:40 +08:00
|
|
|
case tok::colon:
|
|
|
|
// Colons from ?: are handled in parseConditional().
|
2013-01-17 00:23:19 +08:00
|
|
|
if (Tok->Parent->is(tok::r_paren))
|
|
|
|
Tok->Type = TT_CtorInitializerColon;
|
2013-01-12 14:18:40 +08:00
|
|
|
if (ColonIsObjCMethodExpr)
|
|
|
|
Tok->Type = TT_ObjCMethodExpr;
|
|
|
|
break;
|
2013-01-18 01:17:19 +08:00
|
|
|
case tok::kw_if:
|
|
|
|
case tok::kw_while:
|
2013-01-23 18:09:28 +08:00
|
|
|
if (CurrentToken != NULL && CurrentToken->is(tok::l_paren)) {
|
2013-01-18 01:17:19 +08:00
|
|
|
next();
|
|
|
|
if (!parseParens(/*LookForDecls=*/true))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2013-01-18 13:50:57 +08:00
|
|
|
case tok::l_paren:
|
2013-01-05 02:52:56 +08:00
|
|
|
if (!parseParens())
|
|
|
|
return false;
|
2013-01-18 13:50:57 +08:00
|
|
|
break;
|
2012-12-04 02:12:45 +08:00
|
|
|
case tok::l_square:
|
2013-01-05 02:52:56 +08:00
|
|
|
if (!parseSquare())
|
|
|
|
return false;
|
2012-12-04 02:12:45 +08:00
|
|
|
break;
|
2013-01-10 17:26:47 +08:00
|
|
|
case tok::l_brace:
|
|
|
|
if (!parseBrace())
|
|
|
|
return false;
|
|
|
|
break;
|
2012-12-04 02:12:45 +08:00
|
|
|
case tok::less:
|
2012-12-04 22:54:30 +08:00
|
|
|
if (parseAngle())
|
2013-01-08 22:56:18 +08:00
|
|
|
Tok->Type = TT_TemplateOpener;
|
2012-12-04 02:12:45 +08:00
|
|
|
else {
|
2013-01-08 22:56:18 +08:00
|
|
|
Tok->Type = TT_BinaryOperator;
|
|
|
|
CurrentToken = Tok;
|
|
|
|
next();
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
break;
|
2013-01-05 02:52:56 +08:00
|
|
|
case tok::r_paren:
|
|
|
|
case tok::r_square:
|
|
|
|
return false;
|
2013-01-10 17:26:47 +08:00
|
|
|
case tok::r_brace:
|
|
|
|
// Lines can start with '}'.
|
|
|
|
if (Tok->Parent != NULL)
|
|
|
|
return false;
|
|
|
|
break;
|
2012-12-04 02:12:45 +08:00
|
|
|
case tok::greater:
|
2013-01-08 22:56:18 +08:00
|
|
|
Tok->Type = TT_BinaryOperator;
|
2012-12-04 02:12:45 +08:00
|
|
|
break;
|
|
|
|
case tok::kw_operator:
|
2013-01-23 18:09:28 +08:00
|
|
|
if (CurrentToken != NULL && CurrentToken->is(tok::l_paren)) {
|
2013-01-08 22:56:18 +08:00
|
|
|
CurrentToken->Type = TT_OverloadedOperator;
|
2012-12-24 18:56:04 +08:00
|
|
|
next();
|
2013-01-08 22:56:18 +08:00
|
|
|
if (CurrentToken != NULL && CurrentToken->is(tok::r_paren)) {
|
|
|
|
CurrentToken->Type = TT_OverloadedOperator;
|
2012-12-24 18:56:04 +08:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
} else {
|
2013-01-08 22:56:18 +08:00
|
|
|
while (CurrentToken != NULL && CurrentToken->isNot(tok::l_paren)) {
|
|
|
|
CurrentToken->Type = TT_OverloadedOperator;
|
2012-12-24 18:56:04 +08:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
break;
|
|
|
|
case tok::question:
|
2012-12-04 22:54:30 +08:00
|
|
|
parseConditional();
|
2012-12-04 02:12:45 +08:00
|
|
|
break;
|
2013-01-02 23:08:56 +08:00
|
|
|
case tok::kw_template:
|
|
|
|
parseTemplateDeclaration();
|
|
|
|
break;
|
2012-12-04 02:12:45 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-01-05 02:52:56 +08:00
|
|
|
return true;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2012-12-22 01:58:39 +08:00
|
|
|
void parseIncludeDirective() {
|
2013-01-15 23:50:27 +08:00
|
|
|
next();
|
|
|
|
if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
|
|
|
|
next();
|
|
|
|
while (CurrentToken != NULL) {
|
2013-01-18 17:19:33 +08:00
|
|
|
if (CurrentToken->isNot(tok::comment) ||
|
|
|
|
!CurrentToken->Children.empty())
|
|
|
|
CurrentToken->Type = TT_ImplicitStringLiteral;
|
2013-01-15 23:50:27 +08:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (CurrentToken != NULL) {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void parseWarningOrError() {
|
|
|
|
next();
|
|
|
|
// We still want to format the whitespace left of the first token of the
|
|
|
|
// warning or error.
|
|
|
|
next();
|
2013-01-08 22:56:18 +08:00
|
|
|
while (CurrentToken != NULL) {
|
2013-01-15 23:50:27 +08:00
|
|
|
CurrentToken->Type = TT_ImplicitStringLiteral;
|
2012-12-22 01:58:39 +08:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void parsePreprocessorDirective() {
|
|
|
|
next();
|
2013-01-08 22:56:18 +08:00
|
|
|
if (CurrentToken == NULL)
|
2012-12-22 01:58:39 +08:00
|
|
|
return;
|
2013-01-06 06:56:06 +08:00
|
|
|
// Hashes in the middle of a line can lead to any strange token
|
|
|
|
// sequence.
|
2013-01-08 22:56:18 +08:00
|
|
|
if (CurrentToken->FormatTok.Tok.getIdentifierInfo() == NULL)
|
2013-01-06 06:56:06 +08:00
|
|
|
return;
|
2013-01-08 22:56:18 +08:00
|
|
|
switch (
|
|
|
|
CurrentToken->FormatTok.Tok.getIdentifierInfo()->getPPKeywordID()) {
|
2012-12-22 01:58:39 +08:00
|
|
|
case tok::pp_include:
|
2012-12-22 02:21:56 +08:00
|
|
|
case tok::pp_import:
|
2012-12-22 01:58:39 +08:00
|
|
|
parseIncludeDirective();
|
|
|
|
break;
|
2013-01-15 23:50:27 +08:00
|
|
|
case tok::pp_error:
|
|
|
|
case tok::pp_warning:
|
|
|
|
parseWarningOrError();
|
|
|
|
break;
|
2012-12-22 01:58:39 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-07 18:48:50 +08:00
|
|
|
LineType parseLine() {
|
2013-01-22 22:28:24 +08:00
|
|
|
int PeriodsAndArrows = 0;
|
2013-01-28 17:35:24 +08:00
|
|
|
bool CanBeBuilderTypeStmt = true;
|
2013-01-08 22:56:18 +08:00
|
|
|
if (CurrentToken->is(tok::hash)) {
|
2012-12-22 01:58:39 +08:00
|
|
|
parsePreprocessorDirective();
|
2013-01-07 18:48:50 +08:00
|
|
|
return LT_PreprocessorDirective;
|
2012-12-22 01:58:39 +08:00
|
|
|
}
|
2013-01-08 22:56:18 +08:00
|
|
|
while (CurrentToken != NULL) {
|
2013-01-22 22:28:24 +08:00
|
|
|
|
2013-01-08 22:56:18 +08:00
|
|
|
if (CurrentToken->is(tok::kw_virtual))
|
2013-01-07 18:48:50 +08:00
|
|
|
KeywordVirtualFound = true;
|
2013-01-22 22:28:24 +08:00
|
|
|
if (CurrentToken->is(tok::period) || CurrentToken->is(tok::arrow))
|
|
|
|
++PeriodsAndArrows;
|
2013-01-28 17:35:24 +08:00
|
|
|
if (getPrecedence(*CurrentToken) > prec::Assignment &&
|
|
|
|
CurrentToken->isNot(tok::less) && CurrentToken->isNot(tok::greater))
|
|
|
|
CanBeBuilderTypeStmt = false;
|
2013-01-05 02:52:56 +08:00
|
|
|
if (!consumeToken())
|
2013-01-07 18:48:50 +08:00
|
|
|
return LT_Invalid;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
2013-01-07 18:48:50 +08:00
|
|
|
if (KeywordVirtualFound)
|
|
|
|
return LT_VirtualFunctionDecl;
|
2013-01-22 22:28:24 +08:00
|
|
|
|
|
|
|
// Assume a builder-type call if there are 2 or more "." and "->".
|
2013-01-28 17:35:24 +08:00
|
|
|
if (PeriodsAndArrows >= 2 && CanBeBuilderTypeStmt)
|
2013-01-22 22:28:24 +08:00
|
|
|
return LT_BuilderTypeCall;
|
|
|
|
|
2013-01-07 18:48:50 +08:00
|
|
|
return LT_Other;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void next() {
|
2013-01-08 22:56:18 +08:00
|
|
|
if (CurrentToken != NULL && !CurrentToken->Children.empty())
|
|
|
|
CurrentToken = &CurrentToken->Children[0];
|
|
|
|
else
|
|
|
|
CurrentToken = NULL;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-01-08 22:56:18 +08:00
|
|
|
AnnotatedToken *CurrentToken;
|
2013-01-07 18:48:50 +08:00
|
|
|
bool KeywordVirtualFound;
|
2013-01-12 14:18:40 +08:00
|
|
|
bool ColonIsObjCMethodExpr;
|
2012-12-04 02:12:45 +08:00
|
|
|
};
|
|
|
|
|
2013-01-08 22:56:18 +08:00
|
|
|
void calculateExtraInformation(AnnotatedToken &Current) {
|
|
|
|
Current.SpaceRequiredBefore = spaceRequiredBefore(Current);
|
|
|
|
|
2013-01-09 23:25:02 +08:00
|
|
|
if (Current.FormatTok.MustBreakBefore) {
|
2013-01-08 22:56:18 +08:00
|
|
|
Current.MustBreakBefore = true;
|
|
|
|
} else {
|
2013-01-14 00:10:20 +08:00
|
|
|
if (Current.Type == TT_LineComment) {
|
|
|
|
Current.MustBreakBefore = Current.FormatTok.NewlinesBefore > 0;
|
2013-01-17 20:53:34 +08:00
|
|
|
} else if ((Current.Parent->is(tok::comment) &&
|
|
|
|
Current.FormatTok.NewlinesBefore > 0) ||
|
2013-01-14 00:10:20 +08:00
|
|
|
(Current.is(tok::string_literal) &&
|
|
|
|
Current.Parent->is(tok::string_literal))) {
|
2013-01-09 23:25:02 +08:00
|
|
|
Current.MustBreakBefore = true;
|
|
|
|
} else {
|
|
|
|
Current.MustBreakBefore = false;
|
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
2013-01-08 22:56:18 +08:00
|
|
|
Current.CanBreakBefore = Current.MustBreakBefore || canBreakBefore(Current);
|
2013-01-16 18:41:46 +08:00
|
|
|
if (Current.MustBreakBefore)
|
|
|
|
Current.TotalLength = Current.Parent->TotalLength + Style.ColumnLimit;
|
|
|
|
else
|
|
|
|
Current.TotalLength = Current.Parent->TotalLength +
|
|
|
|
Current.FormatTok.TokenLength +
|
|
|
|
(Current.SpaceRequiredBefore ? 1 : 0);
|
2013-01-08 22:56:18 +08:00
|
|
|
if (!Current.Children.empty())
|
|
|
|
calculateExtraInformation(Current.Children[0]);
|
|
|
|
}
|
|
|
|
|
2013-01-14 21:08:07 +08:00
|
|
|
void annotate() {
|
|
|
|
AnnotatingParser Parser(Line.First);
|
|
|
|
Line.Type = Parser.parseLine();
|
|
|
|
if (Line.Type == LT_Invalid)
|
|
|
|
return;
|
2013-01-07 18:48:50 +08:00
|
|
|
|
2013-01-23 20:10:53 +08:00
|
|
|
determineTokenTypes(Line.First, /*IsExpression=*/ false);
|
2013-01-07 18:48:50 +08:00
|
|
|
|
2013-01-14 21:08:07 +08:00
|
|
|
if (Line.First.Type == TT_ObjCMethodSpecifier)
|
|
|
|
Line.Type = LT_ObjCMethodDecl;
|
|
|
|
else if (Line.First.Type == TT_ObjCDecl)
|
|
|
|
Line.Type = LT_ObjCDecl;
|
|
|
|
else if (Line.First.Type == TT_ObjCProperty)
|
|
|
|
Line.Type = LT_ObjCProperty;
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-01-14 21:08:07 +08:00
|
|
|
Line.First.SpaceRequiredBefore = true;
|
|
|
|
Line.First.MustBreakBefore = Line.First.FormatTok.MustBreakBefore;
|
|
|
|
Line.First.CanBreakBefore = Line.First.MustBreakBefore;
|
2013-01-07 18:48:50 +08:00
|
|
|
|
2013-01-16 18:41:46 +08:00
|
|
|
Line.First.TotalLength = Line.First.FormatTok.TokenLength;
|
2013-01-14 21:08:07 +08:00
|
|
|
if (!Line.First.Children.empty())
|
|
|
|
calculateExtraInformation(Line.First.Children[0]);
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-01-23 20:10:53 +08:00
|
|
|
void determineTokenTypes(AnnotatedToken &Current, bool IsExpression) {
|
|
|
|
if (getPrecedence(Current) == prec::Assignment) {
|
|
|
|
IsExpression = true;
|
|
|
|
AnnotatedToken *Previous = Current.Parent;
|
|
|
|
while (Previous != NULL) {
|
2013-01-23 22:08:21 +08:00
|
|
|
if (Previous->Type == TT_BinaryOperator &&
|
|
|
|
(Previous->is(tok::star) || Previous->is(tok::amp))) {
|
2013-01-23 20:10:53 +08:00
|
|
|
Previous->Type = TT_PointerOrReference;
|
2013-01-23 22:08:21 +08:00
|
|
|
}
|
2013-01-23 20:10:53 +08:00
|
|
|
Previous = Previous->Parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Current.is(tok::kw_return) || Current.is(tok::kw_throw) ||
|
2013-01-23 20:58:14 +08:00
|
|
|
(Current.is(tok::l_paren) && !Line.MustBeDeclaration &&
|
|
|
|
(Current.Parent == NULL || Current.Parent->isNot(tok::kw_for))))
|
2013-01-23 20:10:53 +08:00
|
|
|
IsExpression = true;
|
2013-01-08 22:56:18 +08:00
|
|
|
|
|
|
|
if (Current.Type == TT_Unknown) {
|
|
|
|
if (Current.is(tok::star) || Current.is(tok::amp)) {
|
2013-01-23 20:10:53 +08:00
|
|
|
Current.Type = determineStarAmpUsage(Current, IsExpression);
|
2013-01-09 16:36:49 +08:00
|
|
|
} else if (Current.is(tok::minus) || Current.is(tok::plus) ||
|
|
|
|
Current.is(tok::caret)) {
|
|
|
|
Current.Type = determinePlusMinusCaretUsage(Current);
|
2013-01-08 22:56:18 +08:00
|
|
|
} else if (Current.is(tok::minusminus) || Current.is(tok::plusplus)) {
|
|
|
|
Current.Type = determineIncrementUsage(Current);
|
|
|
|
} else if (Current.is(tok::exclaim)) {
|
|
|
|
Current.Type = TT_UnaryOperator;
|
|
|
|
} else if (isBinaryOperator(Current)) {
|
|
|
|
Current.Type = TT_BinaryOperator;
|
|
|
|
} else if (Current.is(tok::comment)) {
|
|
|
|
std::string Data(Lexer::getSpelling(Current.FormatTok.Tok, SourceMgr,
|
|
|
|
Lex.getLangOpts()));
|
2013-01-07 16:54:53 +08:00
|
|
|
if (StringRef(Data).startswith("//"))
|
2013-01-08 22:56:18 +08:00
|
|
|
Current.Type = TT_LineComment;
|
2012-12-04 02:12:45 +08:00
|
|
|
else
|
2013-01-08 22:56:18 +08:00
|
|
|
Current.Type = TT_BlockComment;
|
2013-01-10 19:14:08 +08:00
|
|
|
} else if (Current.is(tok::r_paren) &&
|
|
|
|
(Current.Parent->Type == TT_PointerOrReference ||
|
2013-01-13 16:01:36 +08:00
|
|
|
Current.Parent->Type == TT_TemplateCloser) &&
|
|
|
|
(Current.Children.empty() ||
|
|
|
|
(Current.Children[0].isNot(tok::equal) &&
|
|
|
|
Current.Children[0].isNot(tok::semi) &&
|
|
|
|
Current.Children[0].isNot(tok::l_brace)))) {
|
2013-01-10 19:14:08 +08:00
|
|
|
// FIXME: We need to get smarter and understand more cases of casts.
|
|
|
|
Current.Type = TT_CastRParen;
|
2013-01-11 03:19:14 +08:00
|
|
|
} else if (Current.is(tok::at) && Current.Children.size()) {
|
|
|
|
switch (Current.Children[0].FormatTok.Tok.getObjCKeywordID()) {
|
|
|
|
case tok::objc_interface:
|
|
|
|
case tok::objc_implementation:
|
|
|
|
case tok::objc_protocol:
|
|
|
|
Current.Type = TT_ObjCDecl;
|
2013-01-11 05:30:42 +08:00
|
|
|
break;
|
|
|
|
case tok::objc_property:
|
|
|
|
Current.Type = TT_ObjCProperty;
|
|
|
|
break;
|
2013-01-11 03:19:14 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
}
|
2013-01-08 22:56:18 +08:00
|
|
|
|
|
|
|
if (!Current.Children.empty())
|
2013-01-23 20:10:53 +08:00
|
|
|
determineTokenTypes(Current.Children[0], IsExpression);
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2013-01-15 22:27:39 +08:00
|
|
|
/// \brief Returns the previous token ignoring comments.
|
|
|
|
const AnnotatedToken *getPreviousToken(const AnnotatedToken &Tok) {
|
|
|
|
const AnnotatedToken *PrevToken = Tok.Parent;
|
|
|
|
while (PrevToken != NULL && PrevToken->is(tok::comment))
|
|
|
|
PrevToken = PrevToken->Parent;
|
|
|
|
return PrevToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Returns the next token ignoring comments.
|
|
|
|
const AnnotatedToken *getNextToken(const AnnotatedToken &Tok) {
|
|
|
|
if (Tok.Children.empty())
|
|
|
|
return NULL;
|
|
|
|
const AnnotatedToken *NextToken = &Tok.Children[0];
|
|
|
|
while (NextToken->is(tok::comment)) {
|
|
|
|
if (NextToken->Children.empty())
|
|
|
|
return NULL;
|
|
|
|
NextToken = &NextToken->Children[0];
|
|
|
|
}
|
|
|
|
return NextToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Return the type of the given token assuming it is * or &.
|
2013-01-23 20:10:53 +08:00
|
|
|
TokenType determineStarAmpUsage(const AnnotatedToken &Tok,
|
|
|
|
bool IsExpression) {
|
2013-01-15 22:27:39 +08:00
|
|
|
const AnnotatedToken *PrevToken = getPreviousToken(Tok);
|
|
|
|
if (PrevToken == NULL)
|
2013-01-07 18:48:50 +08:00
|
|
|
return TT_UnaryOperator;
|
2013-01-15 22:27:39 +08:00
|
|
|
|
|
|
|
const AnnotatedToken *NextToken = getNextToken(Tok);
|
|
|
|
if (NextToken == NULL)
|
2013-01-07 18:48:50 +08:00
|
|
|
return TT_Unknown;
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-01-22 19:46:26 +08:00
|
|
|
if (NextToken->is(tok::l_square) && NextToken->Type != TT_ObjCMethodExpr)
|
|
|
|
return TT_PointerOrReference;
|
|
|
|
|
2013-01-15 22:27:39 +08:00
|
|
|
if (PrevToken->is(tok::l_paren) || PrevToken->is(tok::l_square) ||
|
|
|
|
PrevToken->is(tok::l_brace) || PrevToken->is(tok::comma) ||
|
|
|
|
PrevToken->is(tok::kw_return) || PrevToken->is(tok::colon) ||
|
|
|
|
PrevToken->Type == TT_BinaryOperator ||
|
2013-01-17 00:04:06 +08:00
|
|
|
PrevToken->Type == TT_UnaryOperator || PrevToken->Type == TT_CastRParen)
|
2013-01-07 18:48:50 +08:00
|
|
|
return TT_UnaryOperator;
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-01-15 22:27:39 +08:00
|
|
|
if (PrevToken->FormatTok.Tok.isLiteral() || PrevToken->is(tok::r_paren) ||
|
|
|
|
PrevToken->is(tok::r_square) || NextToken->FormatTok.Tok.isLiteral() ||
|
|
|
|
NextToken->is(tok::plus) || NextToken->is(tok::minus) ||
|
|
|
|
NextToken->is(tok::plusplus) || NextToken->is(tok::minusminus) ||
|
|
|
|
NextToken->is(tok::tilde) || NextToken->is(tok::exclaim) ||
|
|
|
|
NextToken->is(tok::l_paren) || NextToken->is(tok::l_square) ||
|
|
|
|
NextToken->is(tok::kw_alignof) || NextToken->is(tok::kw_sizeof))
|
2013-01-07 18:48:50 +08:00
|
|
|
return TT_BinaryOperator;
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-01-15 22:27:39 +08:00
|
|
|
if (NextToken->is(tok::comma) || NextToken->is(tok::r_paren) ||
|
|
|
|
NextToken->is(tok::greater))
|
2013-01-07 18:48:50 +08:00
|
|
|
return TT_PointerOrReference;
|
2013-01-02 23:46:59 +08:00
|
|
|
|
2012-12-05 15:51:39 +08:00
|
|
|
// It is very unlikely that we are going to find a pointer or reference type
|
|
|
|
// definition on the RHS of an assignment.
|
2013-01-23 20:10:53 +08:00
|
|
|
if (IsExpression)
|
2013-01-07 18:48:50 +08:00
|
|
|
return TT_BinaryOperator;
|
2012-12-05 15:51:39 +08:00
|
|
|
|
2013-01-07 18:48:50 +08:00
|
|
|
return TT_PointerOrReference;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2013-01-09 16:36:49 +08:00
|
|
|
TokenType determinePlusMinusCaretUsage(const AnnotatedToken &Tok) {
|
2013-01-15 22:27:39 +08:00
|
|
|
const AnnotatedToken *PrevToken = getPreviousToken(Tok);
|
|
|
|
if (PrevToken == NULL)
|
|
|
|
return TT_UnaryOperator;
|
|
|
|
|
2012-12-21 17:41:31 +08:00
|
|
|
// Use heuristics to recognize unary operators.
|
2013-01-15 22:27:39 +08:00
|
|
|
if (PrevToken->is(tok::equal) || PrevToken->is(tok::l_paren) ||
|
|
|
|
PrevToken->is(tok::comma) || PrevToken->is(tok::l_square) ||
|
|
|
|
PrevToken->is(tok::question) || PrevToken->is(tok::colon) ||
|
|
|
|
PrevToken->is(tok::kw_return) || PrevToken->is(tok::kw_case) ||
|
|
|
|
PrevToken->is(tok::at) || PrevToken->is(tok::l_brace))
|
2013-01-07 18:48:50 +08:00
|
|
|
return TT_UnaryOperator;
|
2012-12-21 17:41:31 +08:00
|
|
|
|
|
|
|
// There can't be to consecutive binary operators.
|
2013-01-15 22:27:39 +08:00
|
|
|
if (PrevToken->Type == TT_BinaryOperator)
|
2013-01-07 18:48:50 +08:00
|
|
|
return TT_UnaryOperator;
|
2012-12-21 17:41:31 +08:00
|
|
|
|
|
|
|
// Fall back to marking the token as binary operator.
|
2013-01-07 18:48:50 +08:00
|
|
|
return TT_BinaryOperator;
|
2012-12-21 17:41:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Determine whether ++/-- are pre- or post-increments/-decrements.
|
2013-01-08 22:56:18 +08:00
|
|
|
TokenType determineIncrementUsage(const AnnotatedToken &Tok) {
|
2013-01-15 22:27:39 +08:00
|
|
|
const AnnotatedToken *PrevToken = getPreviousToken(Tok);
|
|
|
|
if (PrevToken == NULL)
|
2013-01-14 20:18:19 +08:00
|
|
|
return TT_UnaryOperator;
|
2013-01-15 22:27:39 +08:00
|
|
|
if (PrevToken->is(tok::r_paren) || PrevToken->is(tok::r_square) ||
|
|
|
|
PrevToken->is(tok::identifier))
|
2013-01-07 18:48:50 +08:00
|
|
|
return TT_TrailingUnaryOperator;
|
2012-12-21 17:41:31 +08:00
|
|
|
|
2013-01-07 18:48:50 +08:00
|
|
|
return TT_UnaryOperator;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2013-01-08 22:56:18 +08:00
|
|
|
bool spaceRequiredBetween(const AnnotatedToken &Left,
|
|
|
|
const AnnotatedToken &Right) {
|
2013-01-09 00:17:54 +08:00
|
|
|
if (Right.is(tok::hashhash))
|
|
|
|
return Left.is(tok::hash);
|
|
|
|
if (Left.is(tok::hashhash) || Left.is(tok::hash))
|
|
|
|
return Right.is(tok::hash);
|
2012-12-11 02:59:13 +08:00
|
|
|
if (Right.is(tok::r_paren) || Right.is(tok::semi) || Right.is(tok::comma))
|
|
|
|
return false;
|
2013-01-11 04:12:55 +08:00
|
|
|
if (Right.is(tok::less) &&
|
|
|
|
(Left.is(tok::kw_template) ||
|
2013-01-14 21:08:07 +08:00
|
|
|
(Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList)))
|
2012-12-04 02:12:45 +08:00
|
|
|
return true;
|
|
|
|
if (Left.is(tok::arrow) || Right.is(tok::arrow))
|
|
|
|
return false;
|
|
|
|
if (Left.is(tok::exclaim) || Left.is(tok::tilde))
|
|
|
|
return false;
|
2013-01-09 03:40:21 +08:00
|
|
|
if (Left.is(tok::at) &&
|
|
|
|
(Right.is(tok::identifier) || Right.is(tok::string_literal) ||
|
|
|
|
Right.is(tok::char_constant) || Right.is(tok::numeric_constant) ||
|
2013-01-10 21:08:12 +08:00
|
|
|
Right.is(tok::l_paren) || Right.is(tok::l_brace) ||
|
|
|
|
Right.is(tok::kw_true) || Right.is(tok::kw_false)))
|
2012-12-21 03:54:13 +08:00
|
|
|
return false;
|
2013-01-16 15:19:28 +08:00
|
|
|
if (Left.is(tok::coloncolon))
|
|
|
|
return false;
|
|
|
|
if (Right.is(tok::coloncolon))
|
|
|
|
return Left.isNot(tok::identifier) && Left.isNot(tok::greater);
|
2012-12-04 02:12:45 +08:00
|
|
|
if (Left.is(tok::less) || Right.is(tok::greater) || Right.is(tok::less))
|
|
|
|
return false;
|
2012-12-07 17:52:15 +08:00
|
|
|
if (Right.is(tok::amp) || Right.is(tok::star))
|
2013-01-08 22:56:18 +08:00
|
|
|
return Left.FormatTok.Tok.isLiteral() ||
|
2012-12-25 00:51:15 +08:00
|
|
|
(Left.isNot(tok::star) && Left.isNot(tok::amp) &&
|
|
|
|
!Style.PointerAndReferenceBindToType);
|
2012-12-04 02:12:45 +08:00
|
|
|
if (Left.is(tok::amp) || Left.is(tok::star))
|
2013-01-08 22:56:18 +08:00
|
|
|
return Right.FormatTok.Tok.isLiteral() ||
|
|
|
|
Style.PointerAndReferenceBindToType;
|
2012-12-04 02:12:45 +08:00
|
|
|
if (Right.is(tok::star) && Left.is(tok::l_paren))
|
|
|
|
return false;
|
2013-01-12 14:18:40 +08:00
|
|
|
if (Left.is(tok::l_square) || Right.is(tok::r_square))
|
|
|
|
return false;
|
|
|
|
if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr)
|
2012-12-04 02:12:45 +08:00
|
|
|
return false;
|
|
|
|
if (Left.is(tok::period) || Right.is(tok::period))
|
|
|
|
return false;
|
2013-01-12 14:18:40 +08:00
|
|
|
if (Left.is(tok::colon))
|
|
|
|
return Left.Type != TT_ObjCMethodExpr;
|
|
|
|
if (Right.is(tok::colon))
|
|
|
|
return Right.Type != TT_ObjCMethodExpr;
|
2012-12-04 02:12:45 +08:00
|
|
|
if (Left.is(tok::l_paren))
|
|
|
|
return false;
|
|
|
|
if (Right.is(tok::l_paren)) {
|
2013-01-14 21:08:07 +08:00
|
|
|
return Line.Type == LT_ObjCDecl || Left.is(tok::kw_if) ||
|
2013-01-11 03:19:14 +08:00
|
|
|
Left.is(tok::kw_for) || Left.is(tok::kw_while) ||
|
2013-01-11 22:23:32 +08:00
|
|
|
Left.is(tok::kw_switch) || Left.is(tok::kw_return) ||
|
2013-01-12 00:09:04 +08:00
|
|
|
Left.is(tok::kw_catch) || Left.is(tok::kw_new) ||
|
|
|
|
Left.is(tok::kw_delete);
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
2013-01-08 22:56:18 +08:00
|
|
|
if (Left.is(tok::at) &&
|
|
|
|
Right.FormatTok.Tok.getObjCKeywordID() != tok::objc_not_keyword)
|
2013-01-08 00:14:28 +08:00
|
|
|
return false;
|
2013-01-10 21:24:24 +08:00
|
|
|
if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
|
|
|
|
return false;
|
2012-12-04 02:12:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-08 22:56:18 +08:00
|
|
|
bool spaceRequiredBefore(const AnnotatedToken &Tok) {
|
2013-01-14 21:08:07 +08:00
|
|
|
if (Line.Type == LT_ObjCMethodDecl) {
|
2013-01-08 22:56:18 +08:00
|
|
|
if (Tok.is(tok::identifier) && !Tok.Children.empty() &&
|
|
|
|
Tok.Children[0].is(tok::colon) && Tok.Parent->is(tok::identifier))
|
2013-01-07 23:36:15 +08:00
|
|
|
return true;
|
2013-01-08 22:56:18 +08:00
|
|
|
if (Tok.is(tok::colon))
|
2013-01-07 23:36:15 +08:00
|
|
|
return false;
|
2013-01-08 22:56:18 +08:00
|
|
|
if (Tok.Parent->Type == TT_ObjCMethodSpecifier)
|
2013-01-17 14:14:50 +08:00
|
|
|
return true;
|
2013-01-08 22:56:18 +08:00
|
|
|
if (Tok.Parent->is(tok::r_paren) && Tok.is(tok::identifier))
|
2013-01-07 23:36:15 +08:00
|
|
|
// Don't space between ')' and <id>
|
|
|
|
return false;
|
2013-01-08 22:56:18 +08:00
|
|
|
if (Tok.Parent->is(tok::colon) && Tok.is(tok::l_paren))
|
2013-01-07 23:36:15 +08:00
|
|
|
// Don't space between ':' and '('
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-14 21:08:07 +08:00
|
|
|
if (Line.Type == LT_ObjCProperty &&
|
2013-01-11 05:30:42 +08:00
|
|
|
(Tok.is(tok::equal) || Tok.Parent->is(tok::equal)))
|
|
|
|
return false;
|
2013-01-07 23:36:15 +08:00
|
|
|
|
2013-01-13 16:19:51 +08:00
|
|
|
if (Tok.Parent->is(tok::comma))
|
|
|
|
return true;
|
2013-01-10 21:08:12 +08:00
|
|
|
if (Tok.Type == TT_CtorInitializerColon || Tok.Type == TT_ObjCBlockLParen)
|
2013-01-07 23:36:15 +08:00
|
|
|
return true;
|
2013-01-08 22:56:18 +08:00
|
|
|
if (Tok.Type == TT_OverloadedOperator)
|
|
|
|
return Tok.is(tok::identifier) || Tok.is(tok::kw_new) ||
|
2013-01-10 21:08:12 +08:00
|
|
|
Tok.is(tok::kw_delete) || Tok.is(tok::kw_bool);
|
2013-01-08 22:56:18 +08:00
|
|
|
if (Tok.Parent->Type == TT_OverloadedOperator)
|
2013-01-07 23:36:15 +08:00
|
|
|
return false;
|
2013-01-08 22:56:18 +08:00
|
|
|
if (Tok.is(tok::colon))
|
2013-01-14 21:08:07 +08:00
|
|
|
return Line.First.isNot(tok::kw_case) && !Tok.Children.empty() &&
|
2013-01-12 14:18:40 +08:00
|
|
|
Tok.Type != TT_ObjCMethodExpr;
|
2013-01-10 19:14:08 +08:00
|
|
|
if (Tok.Parent->Type == TT_UnaryOperator ||
|
|
|
|
Tok.Parent->Type == TT_CastRParen)
|
2013-01-07 23:36:15 +08:00
|
|
|
return false;
|
2013-01-08 22:56:18 +08:00
|
|
|
if (Tok.Type == TT_UnaryOperator)
|
|
|
|
return Tok.Parent->isNot(tok::l_paren) &&
|
2013-01-13 07:48:49 +08:00
|
|
|
Tok.Parent->isNot(tok::l_square) && Tok.Parent->isNot(tok::at) &&
|
|
|
|
(Tok.Parent->isNot(tok::colon) ||
|
|
|
|
Tok.Parent->Type != TT_ObjCMethodExpr);
|
2013-01-08 22:56:18 +08:00
|
|
|
if (Tok.Parent->is(tok::greater) && Tok.is(tok::greater)) {
|
|
|
|
return Tok.Type == TT_TemplateCloser && Tok.Parent->Type ==
|
2013-01-07 23:36:15 +08:00
|
|
|
TT_TemplateCloser && Style.SplitTemplateClosingGreater;
|
|
|
|
}
|
2013-01-08 22:56:18 +08:00
|
|
|
if (Tok.Type == TT_BinaryOperator || Tok.Parent->Type == TT_BinaryOperator)
|
2013-01-07 23:36:15 +08:00
|
|
|
return true;
|
2013-01-08 22:56:18 +08:00
|
|
|
if (Tok.Parent->Type == TT_TemplateCloser && Tok.is(tok::l_paren))
|
2013-01-07 23:36:15 +08:00
|
|
|
return false;
|
2013-01-14 21:08:07 +08:00
|
|
|
if (Tok.is(tok::less) && Line.First.is(tok::hash))
|
2013-01-07 23:36:15 +08:00
|
|
|
return true;
|
2013-01-08 22:56:18 +08:00
|
|
|
if (Tok.Type == TT_TrailingUnaryOperator)
|
2013-01-07 23:36:15 +08:00
|
|
|
return false;
|
2013-01-08 22:56:18 +08:00
|
|
|
return spaceRequiredBetween(*Tok.Parent, Tok);
|
2013-01-07 23:36:15 +08:00
|
|
|
}
|
|
|
|
|
2013-01-08 22:56:18 +08:00
|
|
|
bool canBreakBefore(const AnnotatedToken &Right) {
|
|
|
|
const AnnotatedToken &Left = *Right.Parent;
|
2013-01-14 21:08:07 +08:00
|
|
|
if (Line.Type == LT_ObjCMethodDecl) {
|
2013-01-08 22:56:18 +08:00
|
|
|
if (Right.is(tok::identifier) && !Right.Children.empty() &&
|
|
|
|
Right.Children[0].is(tok::colon) && Left.is(tok::identifier))
|
2013-01-07 23:36:15 +08:00
|
|
|
return true;
|
2013-01-12 15:00:16 +08:00
|
|
|
if (Right.is(tok::identifier) && Left.is(tok::l_paren) &&
|
|
|
|
Left.Parent->is(tok::colon))
|
2013-01-07 23:36:15 +08:00
|
|
|
// Don't break this identifier as ':' or identifier
|
|
|
|
// before it will break.
|
|
|
|
return false;
|
2013-01-08 22:56:18 +08:00
|
|
|
if (Right.is(tok::colon) && Left.is(tok::identifier) &&
|
|
|
|
Left.CanBreakBefore)
|
2013-01-07 23:36:15 +08:00
|
|
|
// Don't break at ':' if identifier before it can beak.
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-12 14:18:40 +08:00
|
|
|
if (Right.is(tok::colon) && Right.Type == TT_ObjCMethodExpr)
|
|
|
|
return false;
|
|
|
|
if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr)
|
|
|
|
return true;
|
2013-01-13 06:48:47 +08:00
|
|
|
if (isObjCSelectorName(Right))
|
|
|
|
return true;
|
2013-01-08 22:56:18 +08:00
|
|
|
if (Left.ClosesTemplateDeclaration)
|
2013-01-03 02:30:06 +08:00
|
|
|
return true;
|
2013-01-08 22:56:18 +08:00
|
|
|
if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser ||
|
2013-01-09 04:03:18 +08:00
|
|
|
Left.Type == TT_UnaryOperator || Right.Type == TT_ConditionalExpr)
|
2013-01-02 16:44:14 +08:00
|
|
|
return false;
|
2013-01-14 21:08:07 +08:00
|
|
|
if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl)
|
2013-01-07 18:48:50 +08:00
|
|
|
return false;
|
|
|
|
|
2013-01-17 20:53:34 +08:00
|
|
|
if (Right.Type == TT_LineComment)
|
2013-01-14 00:10:20 +08:00
|
|
|
// We rely on MustBreakBefore being set correctly here as we should not
|
|
|
|
// change the "binding" behavior of a comment.
|
|
|
|
return false;
|
|
|
|
|
2013-01-17 21:31:52 +08:00
|
|
|
// Allow breaking after a trailing 'const', e.g. after a method declaration,
|
|
|
|
// unless it is follow by ';', '{' or '='.
|
|
|
|
if (Left.is(tok::kw_const) && Left.Parent != NULL &&
|
|
|
|
Left.Parent->is(tok::r_paren))
|
|
|
|
return Right.isNot(tok::l_brace) && Right.isNot(tok::semi) &&
|
|
|
|
Right.isNot(tok::equal);
|
|
|
|
|
Fixes formatting of nested brace initializers.
We now format this correctly:
Status::Rep Status::global_reps[3] = {
{ kGlobalRef, OK_CODE, NULL, NULL, NULL },
{ kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },
{ kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }
};
- fixed a bug where BreakBeforeClosingBrace would be set on the wrong
state
- added penalties for breaking between = and {, and between { and any
other non-{ token
llvm-svn: 172433
2013-01-15 00:41:43 +08:00
|
|
|
// We only break before r_brace if there was a corresponding break before
|
|
|
|
// the l_brace, which is tracked by BreakBeforeClosingBrace.
|
|
|
|
if (Right.is(tok::r_brace))
|
|
|
|
return false;
|
|
|
|
|
2013-01-15 22:27:39 +08:00
|
|
|
if (Right.is(tok::r_paren) || Right.is(tok::greater))
|
2012-12-04 02:12:45 +08:00
|
|
|
return false;
|
2013-01-08 22:56:18 +08:00
|
|
|
return (isBinaryOperator(Left) && Left.isNot(tok::lessless)) ||
|
|
|
|
Left.is(tok::comma) || Right.is(tok::lessless) ||
|
|
|
|
Right.is(tok::arrow) || Right.is(tok::period) ||
|
2013-01-25 18:57:27 +08:00
|
|
|
Right.is(tok::colon) || Left.is(tok::coloncolon) ||
|
|
|
|
Left.is(tok::semi) || Left.is(tok::l_brace) ||
|
|
|
|
Left.is(tok::question) || Left.Type == TT_ConditionalExpr ||
|
|
|
|
(Left.is(tok::r_paren) && Left.Type != TT_CastRParen &&
|
|
|
|
Right.is(tok::identifier)) ||
|
2013-01-28 15:35:34 +08:00
|
|
|
(Left.is(tok::l_paren) && !Right.is(tok::r_paren)) ||
|
|
|
|
(Left.is(tok::l_square) && !Right.is(tok::r_square));
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FormatStyle Style;
|
|
|
|
SourceManager &SourceMgr;
|
2013-01-07 16:54:53 +08:00
|
|
|
Lexer &Lex;
|
2013-01-14 21:08:07 +08:00
|
|
|
AnnotatedLine &Line;
|
2012-12-04 02:12:45 +08:00
|
|
|
};
|
|
|
|
|
2012-12-08 00:15:44 +08:00
|
|
|
class LexerBasedFormatTokenSource : public FormatTokenSource {
|
|
|
|
public:
|
|
|
|
LexerBasedFormatTokenSource(Lexer &Lex, SourceManager &SourceMgr)
|
2012-12-19 05:05:13 +08:00
|
|
|
: GreaterStashed(false), Lex(Lex), SourceMgr(SourceMgr),
|
2012-12-08 00:15:44 +08:00
|
|
|
IdentTable(Lex.getLangOpts()) {
|
|
|
|
Lex.SetKeepWhitespaceMode(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual FormatToken getNextToken() {
|
|
|
|
if (GreaterStashed) {
|
|
|
|
FormatTok.NewlinesBefore = 0;
|
|
|
|
FormatTok.WhiteSpaceStart =
|
|
|
|
FormatTok.Tok.getLocation().getLocWithOffset(1);
|
|
|
|
FormatTok.WhiteSpaceLength = 0;
|
|
|
|
GreaterStashed = false;
|
|
|
|
return FormatTok;
|
|
|
|
}
|
|
|
|
|
|
|
|
FormatTok = FormatToken();
|
|
|
|
Lex.LexFromRawLexer(FormatTok.Tok);
|
2013-01-07 15:56:50 +08:00
|
|
|
StringRef Text = rawTokenText(FormatTok.Tok);
|
2012-12-08 00:15:44 +08:00
|
|
|
FormatTok.WhiteSpaceStart = FormatTok.Tok.getLocation();
|
2013-01-06 06:56:06 +08:00
|
|
|
if (SourceMgr.getFileOffset(FormatTok.WhiteSpaceStart) == 0)
|
|
|
|
FormatTok.IsFirst = true;
|
2012-12-08 00:15:44 +08:00
|
|
|
|
|
|
|
// Consume and record whitespace until we find a significant token.
|
|
|
|
while (FormatTok.Tok.is(tok::unknown)) {
|
2013-01-03 00:30:12 +08:00
|
|
|
FormatTok.NewlinesBefore += Text.count('\n');
|
2013-01-07 21:26:07 +08:00
|
|
|
FormatTok.HasUnescapedNewline = Text.count("\\\n") !=
|
|
|
|
FormatTok.NewlinesBefore;
|
2012-12-08 00:15:44 +08:00
|
|
|
FormatTok.WhiteSpaceLength += FormatTok.Tok.getLength();
|
|
|
|
|
|
|
|
if (FormatTok.Tok.is(tok::eof))
|
|
|
|
return FormatTok;
|
|
|
|
Lex.LexFromRawLexer(FormatTok.Tok);
|
2013-01-07 15:56:50 +08:00
|
|
|
Text = rawTokenText(FormatTok.Tok);
|
2013-01-05 07:34:14 +08:00
|
|
|
}
|
2013-01-07 15:56:50 +08:00
|
|
|
|
|
|
|
// Now FormatTok is the next non-whitespace token.
|
|
|
|
FormatTok.TokenLength = Text.size();
|
|
|
|
|
2013-01-05 07:34:14 +08:00
|
|
|
// In case the token starts with escaped newlines, we want to
|
|
|
|
// take them into account as whitespace - this pattern is quite frequent
|
|
|
|
// in macro definitions.
|
|
|
|
// FIXME: What do we want to do with other escaped spaces, and escaped
|
|
|
|
// spaces or newlines in the middle of tokens?
|
|
|
|
// FIXME: Add a more explicit test.
|
|
|
|
unsigned i = 0;
|
2013-01-07 18:48:50 +08:00
|
|
|
while (i + 1 < Text.size() && Text[i] == '\\' && Text[i + 1] == '\n') {
|
2013-01-23 00:31:55 +08:00
|
|
|
// FIXME: ++FormatTok.NewlinesBefore is missing...
|
2013-01-05 07:34:14 +08:00
|
|
|
FormatTok.WhiteSpaceLength += 2;
|
2013-01-07 15:56:50 +08:00
|
|
|
FormatTok.TokenLength -= 2;
|
2013-01-05 07:34:14 +08:00
|
|
|
i += 2;
|
2012-12-08 00:15:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (FormatTok.Tok.is(tok::raw_identifier)) {
|
2013-01-05 07:34:14 +08:00
|
|
|
IdentifierInfo &Info = IdentTable.get(Text);
|
2012-12-22 01:58:39 +08:00
|
|
|
FormatTok.Tok.setIdentifierInfo(&Info);
|
2012-12-08 00:15:44 +08:00
|
|
|
FormatTok.Tok.setKind(Info.getTokenID());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FormatTok.Tok.is(tok::greatergreater)) {
|
|
|
|
FormatTok.Tok.setKind(tok::greater);
|
|
|
|
GreaterStashed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FormatTok;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
FormatToken FormatTok;
|
|
|
|
bool GreaterStashed;
|
|
|
|
Lexer &Lex;
|
|
|
|
SourceManager &SourceMgr;
|
|
|
|
IdentifierTable IdentTable;
|
|
|
|
|
|
|
|
/// Returns the text of \c FormatTok.
|
2013-01-07 15:56:50 +08:00
|
|
|
StringRef rawTokenText(Token &Tok) {
|
2012-12-08 00:15:44 +08:00
|
|
|
return StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
|
|
|
|
Tok.getLength());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
class Formatter : public UnwrappedLineConsumer {
|
|
|
|
public:
|
2013-01-14 22:14:23 +08:00
|
|
|
Formatter(DiagnosticsEngine &Diag, const FormatStyle &Style, Lexer &Lex,
|
|
|
|
SourceManager &SourceMgr,
|
2012-12-04 02:12:45 +08:00
|
|
|
const std::vector<CharSourceRange> &Ranges)
|
2013-01-10 23:05:09 +08:00
|
|
|
: Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr),
|
2013-01-18 16:44:07 +08:00
|
|
|
Whitespaces(SourceMgr), Ranges(Ranges) {}
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-01-11 22:23:32 +08:00
|
|
|
virtual ~Formatter() {}
|
2012-12-05 05:05:31 +08:00
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
tooling::Replacements format() {
|
2012-12-08 00:15:44 +08:00
|
|
|
LexerBasedFormatTokenSource Tokens(Lex, SourceMgr);
|
2013-01-10 23:05:09 +08:00
|
|
|
UnwrappedLineParser Parser(Diag, Style, Tokens, *this);
|
2012-12-05 01:27:50 +08:00
|
|
|
StructuralError = Parser.parse();
|
2013-01-05 07:34:14 +08:00
|
|
|
unsigned PreviousEndOfLineColumn = 0;
|
2013-01-14 21:08:07 +08:00
|
|
|
for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
|
|
|
|
TokenAnnotator Annotator(Style, SourceMgr, Lex, AnnotatedLines[i]);
|
|
|
|
Annotator.annotate();
|
|
|
|
}
|
|
|
|
for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(),
|
|
|
|
E = AnnotatedLines.end();
|
2013-01-11 03:49:59 +08:00
|
|
|
I != E; ++I) {
|
2013-01-14 21:08:07 +08:00
|
|
|
const AnnotatedLine &TheLine = *I;
|
|
|
|
if (touchesRanges(TheLine) && TheLine.Type != LT_Invalid) {
|
|
|
|
unsigned Indent = formatFirstToken(TheLine.First, TheLine.Level,
|
|
|
|
TheLine.InPPDirective,
|
2013-01-11 03:49:59 +08:00
|
|
|
PreviousEndOfLineColumn);
|
2013-01-16 18:41:46 +08:00
|
|
|
tryFitMultipleLinesInOne(Indent, I, E);
|
2013-01-14 21:08:07 +08:00
|
|
|
UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent,
|
2013-01-18 16:44:07 +08:00
|
|
|
TheLine.First, Whitespaces,
|
2013-01-14 21:08:07 +08:00
|
|
|
StructuralError);
|
2013-01-11 03:49:59 +08:00
|
|
|
PreviousEndOfLineColumn = Formatter.format();
|
|
|
|
} else {
|
|
|
|
// If we did not reformat this unwrapped line, the column at the end of
|
|
|
|
// the last token is unchanged - thus, we can calculate the end of the
|
|
|
|
// last token, and return the result.
|
|
|
|
PreviousEndOfLineColumn =
|
2013-01-14 21:08:07 +08:00
|
|
|
SourceMgr.getSpellingColumnNumber(
|
|
|
|
TheLine.Last->FormatTok.Tok.getLocation()) +
|
|
|
|
Lex.MeasureTokenLength(TheLine.Last->FormatTok.Tok.getLocation(),
|
|
|
|
SourceMgr, Lex.getLangOpts()) -
|
2013-01-11 03:49:59 +08:00
|
|
|
1;
|
|
|
|
}
|
|
|
|
}
|
2013-01-18 16:44:07 +08:00
|
|
|
return Whitespaces.generateReplacements();
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-01-12 01:54:10 +08:00
|
|
|
/// \brief Tries to merge lines into one.
|
|
|
|
///
|
|
|
|
/// This will change \c Line and \c AnnotatedLine to contain the merged line,
|
|
|
|
/// if possible; note that \c I will be incremented when lines are merged.
|
|
|
|
///
|
|
|
|
/// Returns whether the resulting \c Line can fit in a single line.
|
2013-01-16 18:41:46 +08:00
|
|
|
void tryFitMultipleLinesInOne(unsigned Indent,
|
2013-01-14 21:08:07 +08:00
|
|
|
std::vector<AnnotatedLine>::iterator &I,
|
|
|
|
std::vector<AnnotatedLine>::iterator E) {
|
2013-01-12 01:54:10 +08:00
|
|
|
unsigned Limit = Style.ColumnLimit - (I->InPPDirective ? 1 : 0) - Indent;
|
|
|
|
|
2013-01-16 18:41:46 +08:00
|
|
|
// We can never merge stuff if there are trailing line comments.
|
|
|
|
if (I->Last->Type == TT_LineComment)
|
|
|
|
return;
|
|
|
|
|
2013-01-12 01:54:10 +08:00
|
|
|
// Check whether the UnwrappedLine can be put onto a single line. If
|
|
|
|
// so, this is bound to be the optimal solution (by definition) and we
|
|
|
|
// don't need to analyze the entire solution space.
|
2013-01-22 00:42:44 +08:00
|
|
|
if (I->Last->TotalLength > Limit)
|
2013-01-16 18:41:46 +08:00
|
|
|
return;
|
2013-01-22 00:42:44 +08:00
|
|
|
Limit -= I->Last->TotalLength;
|
2013-01-16 15:02:34 +08:00
|
|
|
|
2013-01-21 22:18:28 +08:00
|
|
|
if (I + 1 == E || (I + 1)->Type == LT_Invalid)
|
2013-01-16 18:41:46 +08:00
|
|
|
return;
|
2013-01-14 22:14:23 +08:00
|
|
|
|
|
|
|
if (I->Last->is(tok::l_brace)) {
|
|
|
|
tryMergeSimpleBlock(I, E, Limit);
|
|
|
|
} else if (I->First.is(tok::kw_if)) {
|
|
|
|
tryMergeSimpleIf(I, E, Limit);
|
2013-01-14 23:40:57 +08:00
|
|
|
} else if (I->InPPDirective && (I->First.FormatTok.HasUnescapedNewline ||
|
|
|
|
I->First.FormatTok.IsFirst)) {
|
|
|
|
tryMergeSimplePPDirective(I, E, Limit);
|
2013-01-14 22:14:23 +08:00
|
|
|
}
|
2013-01-16 18:41:46 +08:00
|
|
|
return;
|
2013-01-14 22:14:23 +08:00
|
|
|
}
|
2013-01-12 01:54:10 +08:00
|
|
|
|
2013-01-14 23:40:57 +08:00
|
|
|
void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I,
|
|
|
|
std::vector<AnnotatedLine>::iterator E,
|
|
|
|
unsigned Limit) {
|
|
|
|
AnnotatedLine &Line = *I;
|
2013-01-14 23:52:06 +08:00
|
|
|
if (!(I + 1)->InPPDirective || (I + 1)->First.FormatTok.HasUnescapedNewline)
|
|
|
|
return;
|
2013-01-14 23:40:57 +08:00
|
|
|
if (I + 2 != E && (I + 2)->InPPDirective &&
|
|
|
|
!(I + 2)->First.FormatTok.HasUnescapedNewline)
|
|
|
|
return;
|
2013-01-22 00:42:44 +08:00
|
|
|
if (1 + (I + 1)->Last->TotalLength > Limit)
|
2013-01-16 18:41:46 +08:00
|
|
|
return;
|
2013-01-14 23:40:57 +08:00
|
|
|
join(Line, *(++I));
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:14:23 +08:00
|
|
|
void tryMergeSimpleIf(std::vector<AnnotatedLine>::iterator &I,
|
|
|
|
std::vector<AnnotatedLine>::iterator E,
|
|
|
|
unsigned Limit) {
|
2013-01-15 00:24:39 +08:00
|
|
|
if (!Style.AllowShortIfStatementsOnASingleLine)
|
|
|
|
return;
|
2013-01-18 22:46:43 +08:00
|
|
|
if ((I + 1)->InPPDirective != I->InPPDirective ||
|
|
|
|
((I + 1)->InPPDirective &&
|
|
|
|
(I + 1)->First.FormatTok.HasUnescapedNewline))
|
|
|
|
return;
|
2013-01-14 22:14:23 +08:00
|
|
|
AnnotatedLine &Line = *I;
|
2013-01-16 15:02:34 +08:00
|
|
|
if (Line.Last->isNot(tok::r_paren))
|
|
|
|
return;
|
2013-01-22 00:42:44 +08:00
|
|
|
if (1 + (I + 1)->Last->TotalLength > Limit)
|
2013-01-14 22:14:23 +08:00
|
|
|
return;
|
|
|
|
if ((I + 1)->First.is(tok::kw_if) || (I + 1)->First.Type == TT_LineComment)
|
|
|
|
return;
|
|
|
|
// Only inline simple if's (no nested if or else).
|
|
|
|
if (I + 2 != E && (I + 2)->First.is(tok::kw_else))
|
|
|
|
return;
|
|
|
|
join(Line, *(++I));
|
|
|
|
}
|
|
|
|
|
|
|
|
void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I,
|
|
|
|
std::vector<AnnotatedLine>::iterator E,
|
|
|
|
unsigned Limit){
|
2013-01-12 01:54:10 +08:00
|
|
|
// First, check that the current line allows merging. This is the case if
|
|
|
|
// we're not in a control flow statement and the last token is an opening
|
|
|
|
// brace.
|
2013-01-14 22:14:23 +08:00
|
|
|
AnnotatedLine &Line = *I;
|
2013-01-12 01:54:10 +08:00
|
|
|
bool AllowedTokens =
|
2013-01-14 21:08:07 +08:00
|
|
|
Line.First.isNot(tok::kw_if) && Line.First.isNot(tok::kw_while) &&
|
|
|
|
Line.First.isNot(tok::kw_do) && Line.First.isNot(tok::r_brace) &&
|
|
|
|
Line.First.isNot(tok::kw_else) && Line.First.isNot(tok::kw_try) &&
|
|
|
|
Line.First.isNot(tok::kw_catch) && Line.First.isNot(tok::kw_for) &&
|
2013-01-12 05:14:08 +08:00
|
|
|
// This gets rid of all ObjC @ keywords and methods.
|
2013-01-14 21:08:07 +08:00
|
|
|
Line.First.isNot(tok::at) && Line.First.isNot(tok::minus) &&
|
|
|
|
Line.First.isNot(tok::plus);
|
2013-01-14 22:14:23 +08:00
|
|
|
if (!AllowedTokens)
|
|
|
|
return;
|
2013-01-12 01:54:10 +08:00
|
|
|
|
2013-01-22 00:42:44 +08:00
|
|
|
AnnotatedToken *Tok = &(I + 1)->First;
|
|
|
|
if (Tok->Children.empty() && Tok->is(tok::r_brace) &&
|
|
|
|
!Tok->MustBreakBefore && Tok->TotalLength <= Limit) {
|
|
|
|
Tok->SpaceRequiredBefore = false;
|
|
|
|
join(Line, *(I + 1));
|
|
|
|
I += 1;
|
|
|
|
} else {
|
|
|
|
// Check that we still have three lines and they fit into the limit.
|
|
|
|
if (I + 2 == E || (I + 2)->Type == LT_Invalid ||
|
|
|
|
!nextTwoLinesFitInto(I, Limit))
|
2013-01-14 22:14:23 +08:00
|
|
|
return;
|
2013-01-12 01:54:10 +08:00
|
|
|
|
2013-01-22 00:42:44 +08:00
|
|
|
// Second, check that the next line does not contain any braces - if it
|
|
|
|
// does, readability declines when putting it into a single line.
|
|
|
|
if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore)
|
|
|
|
return;
|
|
|
|
do {
|
|
|
|
if (Tok->is(tok::l_brace) || Tok->is(tok::r_brace))
|
|
|
|
return;
|
|
|
|
Tok = Tok->Children.empty() ? NULL : &Tok->Children.back();
|
|
|
|
} while (Tok != NULL);
|
|
|
|
|
|
|
|
// Last, check that the third line contains a single closing brace.
|
|
|
|
Tok = &(I + 2)->First;
|
|
|
|
if (!Tok->Children.empty() || Tok->isNot(tok::r_brace) ||
|
|
|
|
Tok->MustBreakBefore)
|
|
|
|
return;
|
2013-01-14 21:08:07 +08:00
|
|
|
|
2013-01-22 00:42:44 +08:00
|
|
|
join(Line, *(I + 1));
|
|
|
|
join(Line, *(I + 2));
|
|
|
|
I += 2;
|
2013-01-12 01:54:10 +08:00
|
|
|
}
|
2013-01-14 22:14:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I,
|
|
|
|
unsigned Limit) {
|
2013-01-22 00:42:44 +08:00
|
|
|
return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <=
|
|
|
|
Limit;
|
2013-01-12 01:54:10 +08:00
|
|
|
}
|
|
|
|
|
2013-01-14 21:08:07 +08:00
|
|
|
void join(AnnotatedLine &A, const AnnotatedLine &B) {
|
|
|
|
A.Last->Children.push_back(B.First);
|
|
|
|
while (!A.Last->Children.empty()) {
|
|
|
|
A.Last->Children[0].Parent = A.Last;
|
|
|
|
A.Last = &A.Last->Children[0];
|
|
|
|
}
|
2012-12-05 01:27:50 +08:00
|
|
|
}
|
|
|
|
|
2013-01-14 21:08:07 +08:00
|
|
|
bool touchesRanges(const AnnotatedLine &TheLine) {
|
|
|
|
const FormatToken *First = &TheLine.First.FormatTok;
|
|
|
|
const FormatToken *Last = &TheLine.Last->FormatTok;
|
2013-01-07 21:26:07 +08:00
|
|
|
CharSourceRange LineRange = CharSourceRange::getTokenRange(
|
2013-01-08 22:56:18 +08:00
|
|
|
First->Tok.getLocation(),
|
|
|
|
Last->Tok.getLocation());
|
2012-12-04 02:12:45 +08:00
|
|
|
for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
|
2013-01-11 03:49:59 +08:00
|
|
|
if (!SourceMgr.isBeforeInTranslationUnit(LineRange.getEnd(),
|
|
|
|
Ranges[i].getBegin()) &&
|
|
|
|
!SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
|
|
|
|
LineRange.getBegin()))
|
|
|
|
return true;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
2013-01-11 03:49:59 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
|
2013-01-16 17:10:19 +08:00
|
|
|
AnnotatedLines.push_back(AnnotatedLine(TheLine));
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2013-01-11 02:45:26 +08:00
|
|
|
/// \brief Add a new line and the required indent before the first Token
|
|
|
|
/// of the \c UnwrappedLine if there was no structural parsing error.
|
|
|
|
/// Returns the indent level of the \c UnwrappedLine.
|
|
|
|
unsigned formatFirstToken(const AnnotatedToken &RootToken, unsigned Level,
|
|
|
|
bool InPPDirective,
|
|
|
|
unsigned PreviousEndOfLineColumn) {
|
2013-01-11 22:23:32 +08:00
|
|
|
const FormatToken &Tok = RootToken.FormatTok;
|
2013-01-11 02:45:26 +08:00
|
|
|
if (!Tok.WhiteSpaceStart.isValid() || StructuralError)
|
|
|
|
return SourceMgr.getSpellingColumnNumber(Tok.Tok.getLocation()) - 1;
|
|
|
|
|
|
|
|
unsigned Newlines = std::min(Tok.NewlinesBefore,
|
|
|
|
Style.MaxEmptyLinesToKeep + 1);
|
|
|
|
if (Newlines == 0 && !Tok.IsFirst)
|
|
|
|
Newlines = 1;
|
|
|
|
unsigned Indent = Level * 2;
|
|
|
|
|
|
|
|
bool IsAccessModifier = false;
|
|
|
|
if (RootToken.is(tok::kw_public) || RootToken.is(tok::kw_protected) ||
|
|
|
|
RootToken.is(tok::kw_private))
|
|
|
|
IsAccessModifier = true;
|
|
|
|
else if (RootToken.is(tok::at) && !RootToken.Children.empty() &&
|
|
|
|
(RootToken.Children[0].isObjCAtKeyword(tok::objc_public) ||
|
|
|
|
RootToken.Children[0].isObjCAtKeyword(tok::objc_protected) ||
|
|
|
|
RootToken.Children[0].isObjCAtKeyword(tok::objc_package) ||
|
|
|
|
RootToken.Children[0].isObjCAtKeyword(tok::objc_private)))
|
|
|
|
IsAccessModifier = true;
|
|
|
|
|
|
|
|
if (IsAccessModifier &&
|
|
|
|
static_cast<int>(Indent) + Style.AccessModifierOffset >= 0)
|
|
|
|
Indent += Style.AccessModifierOffset;
|
|
|
|
if (!InPPDirective || Tok.HasUnescapedNewline) {
|
2013-01-18 16:44:07 +08:00
|
|
|
Whitespaces.replaceWhitespace(RootToken, Newlines, Indent, 0, Style);
|
2013-01-11 02:45:26 +08:00
|
|
|
} else {
|
2013-01-18 16:44:07 +08:00
|
|
|
Whitespaces.replacePPWhitespace(RootToken, Newlines, Indent,
|
|
|
|
PreviousEndOfLineColumn, Style);
|
2013-01-11 02:45:26 +08:00
|
|
|
}
|
|
|
|
return Indent;
|
|
|
|
}
|
|
|
|
|
2013-01-14 19:34:14 +08:00
|
|
|
DiagnosticsEngine &Diag;
|
2012-12-04 02:12:45 +08:00
|
|
|
FormatStyle Style;
|
|
|
|
Lexer &Lex;
|
|
|
|
SourceManager &SourceMgr;
|
2013-01-18 16:44:07 +08:00
|
|
|
WhitespaceManager Whitespaces;
|
2012-12-04 02:12:45 +08:00
|
|
|
std::vector<CharSourceRange> Ranges;
|
2013-01-14 21:08:07 +08:00
|
|
|
std::vector<AnnotatedLine> AnnotatedLines;
|
2012-12-05 01:27:50 +08:00
|
|
|
bool StructuralError;
|
2012-12-04 02:12:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
|
|
|
|
SourceManager &SourceMgr,
|
2013-01-14 19:34:14 +08:00
|
|
|
std::vector<CharSourceRange> Ranges,
|
|
|
|
DiagnosticConsumer *DiagClient) {
|
2013-01-10 23:05:09 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
|
2013-01-14 19:34:14 +08:00
|
|
|
OwningPtr<DiagnosticConsumer> DiagPrinter;
|
|
|
|
if (DiagClient == 0) {
|
|
|
|
DiagPrinter.reset(new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts));
|
|
|
|
DiagPrinter->BeginSourceFile(Lex.getLangOpts(), Lex.getPP());
|
|
|
|
DiagClient = DiagPrinter.get();
|
|
|
|
}
|
2013-01-10 23:05:09 +08:00
|
|
|
DiagnosticsEngine Diagnostics(
|
2013-01-13 03:30:44 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
|
2013-01-14 19:34:14 +08:00
|
|
|
DiagClient, false);
|
2013-01-10 23:05:09 +08:00
|
|
|
Diagnostics.setSourceManager(&SourceMgr);
|
|
|
|
Formatter formatter(Diagnostics, Style, Lex, SourceMgr, Ranges);
|
2012-12-04 02:12:45 +08:00
|
|
|
return formatter.format();
|
|
|
|
}
|
|
|
|
|
2013-01-10 21:08:12 +08:00
|
|
|
LangOptions getFormattingLangOpts() {
|
|
|
|
LangOptions LangOpts;
|
|
|
|
LangOpts.CPlusPlus = 1;
|
|
|
|
LangOpts.CPlusPlus11 = 1;
|
|
|
|
LangOpts.Bool = 1;
|
|
|
|
LangOpts.ObjC1 = 1;
|
|
|
|
LangOpts.ObjC2 = 1;
|
|
|
|
return LangOpts;
|
|
|
|
}
|
|
|
|
|
2013-01-07 21:26:07 +08:00
|
|
|
} // namespace format
|
|
|
|
} // namespace clang
|