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.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Format/Format.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "UnwrappedLineParser.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"
|
2012-12-04 02:12:45 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2012-12-04 21:02:32 +08:00
|
|
|
#include <string>
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
|
|
|
|
// FIXME: Move somewhere sane.
|
|
|
|
struct TokenAnnotation {
|
2012-12-05 22:57:28 +08:00
|
|
|
enum TokenType {
|
|
|
|
TT_Unknown,
|
|
|
|
TT_TemplateOpener,
|
|
|
|
TT_TemplateCloser,
|
|
|
|
TT_BinaryOperator,
|
|
|
|
TT_UnaryOperator,
|
2012-12-21 17:41:31 +08:00
|
|
|
TT_TrailingUnaryOperator,
|
2012-12-05 22:57:28 +08:00
|
|
|
TT_OverloadedOperator,
|
|
|
|
TT_PointerOrReference,
|
|
|
|
TT_ConditionalExpr,
|
2012-12-19 05:05:13 +08:00
|
|
|
TT_CtorInitializerColon,
|
2012-12-05 22:57:28 +08:00
|
|
|
TT_LineComment,
|
2012-12-21 03:54:13 +08:00
|
|
|
TT_BlockComment,
|
2012-12-22 01:58:39 +08:00
|
|
|
TT_DirectorySeparator,
|
2012-12-21 03:54:13 +08:00
|
|
|
TT_ObjCMethodSpecifier
|
2012-12-05 22:57:28 +08:00
|
|
|
};
|
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;
|
2012-12-04 02:12:45 +08:00
|
|
|
};
|
|
|
|
|
2012-12-24 21:43:52 +08:00
|
|
|
static prec::Level getPrecedence(const FormatToken &Tok) {
|
|
|
|
return getBinOpPrecedence(Tok.Tok.getKind(), true, true);
|
|
|
|
}
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
using llvm::MutableArrayRef;
|
|
|
|
|
|
|
|
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;
|
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;
|
2012-12-04 02:12:45 +08:00
|
|
|
return GoogleStyle;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct OptimizationParameters {
|
|
|
|
unsigned PenaltyIndentLevel;
|
2012-12-25 00:43:00 +08:00
|
|
|
unsigned PenaltyLevelDecrease;
|
2012-12-04 02:12:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class UnwrappedLineFormatter {
|
|
|
|
public:
|
|
|
|
UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr,
|
|
|
|
const UnwrappedLine &Line,
|
2013-01-05 07:34:14 +08:00
|
|
|
unsigned PreviousEndOfLineColumn,
|
2012-12-04 02:12:45 +08:00
|
|
|
const std::vector<TokenAnnotation> &Annotations,
|
2012-12-05 01:27:50 +08:00
|
|
|
tooling::Replacements &Replaces, bool StructuralError)
|
2012-12-19 05:05:13 +08:00
|
|
|
: Style(Style), SourceMgr(SourceMgr), Line(Line),
|
2013-01-05 07:34:14 +08:00
|
|
|
PreviousEndOfLineColumn(PreviousEndOfLineColumn),
|
2012-12-19 05:05:13 +08:00
|
|
|
Annotations(Annotations), Replaces(Replaces),
|
2012-12-05 01:27:50 +08:00
|
|
|
StructuralError(StructuralError) {
|
2012-12-24 08:13:23 +08:00
|
|
|
Parameters.PenaltyIndentLevel = 15;
|
2013-01-07 15:13:20 +08:00
|
|
|
Parameters.PenaltyLevelDecrease = 30;
|
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
|
|
|
// Format first token and initialize indent.
|
2012-12-05 01:27:50 +08:00
|
|
|
unsigned Indent = formatFirstToken();
|
2012-12-06 17:56:08 +08:00
|
|
|
|
|
|
|
// Initialize state dependent on indent.
|
2012-12-04 02:12:45 +08:00
|
|
|
IndentState State;
|
2012-12-06 17:56:08 +08:00
|
|
|
State.Column = Indent;
|
|
|
|
State.ConsumedTokens = 0;
|
2012-12-05 01:27:50 +08:00
|
|
|
State.Indent.push_back(Indent + 4);
|
|
|
|
State.LastSpace.push_back(Indent);
|
2012-12-06 17:56:08 +08:00
|
|
|
State.FirstLessLess.push_back(0);
|
2012-12-21 22:37:20 +08:00
|
|
|
State.ForLoopVariablePos = 0;
|
|
|
|
State.LineContainsContinuedForLoopSection = false;
|
2012-12-25 00:43:00 +08:00
|
|
|
State.StartOfLineLevel = 1;
|
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
|
|
|
|
2012-12-19 05:05:13 +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
|
2013-01-05 02:52:56 +08:00
|
|
|
// need to analyze the entire solution space.
|
2012-12-19 05:05:13 +08:00
|
|
|
unsigned Columns = State.Column;
|
|
|
|
bool FitsOnALine = true;
|
|
|
|
for (unsigned i = 1, n = Line.Tokens.size(); i != n; ++i) {
|
|
|
|
Columns += (Annotations[i].SpaceRequiredBefore ? 1 : 0) +
|
2013-01-07 15:56:50 +08:00
|
|
|
Line.Tokens[i].TokenLength;
|
2012-12-19 05:05:13 +08:00
|
|
|
// A special case for the colon of a constructor initializer as this only
|
|
|
|
// needs to be put on a new line if the line needs to be split.
|
2013-01-07 15:56:50 +08:00
|
|
|
// FIXME: We need to check whether we're in a preprocessor directive, even
|
|
|
|
// if all tokens fit - the next line might be a preprocessor directive,
|
|
|
|
// too, in which case we need to account for the possible escaped newline.
|
2012-12-19 05:05:13 +08:00
|
|
|
if (Columns > Style.ColumnLimit ||
|
|
|
|
(Annotations[i].MustBreakBefore &&
|
|
|
|
Annotations[i].Type != TokenAnnotation::TT_CtorInitializerColon)) {
|
|
|
|
FitsOnALine = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
// Start iterating at 1 as we have correctly formatted of Token #0 above.
|
|
|
|
for (unsigned i = 1, n = Line.Tokens.size(); i != n; ++i) {
|
2012-12-19 05:05:13 +08:00
|
|
|
if (FitsOnALine) {
|
|
|
|
addTokenToState(false, false, State);
|
|
|
|
} else {
|
|
|
|
unsigned NoBreak = calcPenalty(State, false, UINT_MAX);
|
|
|
|
unsigned Break = calcPenalty(State, true, NoBreak);
|
|
|
|
addTokenToState(Break < NoBreak, false, State);
|
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
2013-01-05 07:34:14 +08:00
|
|
|
return State.Column;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// \brief The current state when indenting a unwrapped line.
|
|
|
|
///
|
|
|
|
/// As the indenting tries different combinations this is copied by value.
|
|
|
|
struct IndentState {
|
|
|
|
/// \brief The number of used columns in the current line.
|
|
|
|
unsigned Column;
|
|
|
|
|
|
|
|
/// \brief The number of tokens already consumed.
|
|
|
|
unsigned ConsumedTokens;
|
|
|
|
|
2012-12-25 00:43:00 +08:00
|
|
|
/// \brief The parenthesis level of the first token on the current line.
|
|
|
|
unsigned StartOfLineLevel;
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
/// \brief The position to which a specific parenthesis level needs to be
|
|
|
|
/// indented.
|
|
|
|
std::vector<unsigned> Indent;
|
|
|
|
|
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));
|
2012-12-04 02:12:45 +08:00
|
|
|
std::vector<unsigned> LastSpace;
|
|
|
|
|
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.
|
|
|
|
std::vector<unsigned> FirstLessLess;
|
|
|
|
|
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;
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
/// \brief Comparison operator to be able to used \c IndentState in \c map.
|
|
|
|
bool operator<(const IndentState &Other) const {
|
|
|
|
if (Other.ConsumedTokens != ConsumedTokens)
|
|
|
|
return Other.ConsumedTokens > ConsumedTokens;
|
|
|
|
if (Other.Column != Column)
|
|
|
|
return Other.Column > Column;
|
2012-12-25 00:43:00 +08:00
|
|
|
if (Other.StartOfLineLevel != StartOfLineLevel)
|
|
|
|
return Other.StartOfLineLevel > StartOfLineLevel;
|
2012-12-04 02:12:45 +08:00
|
|
|
if (Other.Indent.size() != Indent.size())
|
|
|
|
return Other.Indent.size() > Indent.size();
|
|
|
|
for (int i = 0, e = Indent.size(); i != e; ++i) {
|
|
|
|
if (Other.Indent[i] != Indent[i])
|
|
|
|
return Other.Indent[i] > Indent[i];
|
|
|
|
}
|
|
|
|
if (Other.LastSpace.size() != LastSpace.size())
|
|
|
|
return Other.LastSpace.size() > LastSpace.size();
|
|
|
|
for (int i = 0, e = LastSpace.size(); i != e; ++i) {
|
|
|
|
if (Other.LastSpace[i] != LastSpace[i])
|
|
|
|
return Other.LastSpace[i] > LastSpace[i];
|
|
|
|
}
|
2012-12-06 17:56:08 +08:00
|
|
|
if (Other.FirstLessLess.size() != FirstLessLess.size())
|
|
|
|
return Other.FirstLessLess.size() > FirstLessLess.size();
|
|
|
|
for (int i = 0, e = FirstLessLess.size(); i != e; ++i) {
|
|
|
|
if (Other.FirstLessLess[i] != FirstLessLess[i])
|
|
|
|
return Other.FirstLessLess[i] > FirstLessLess[i];
|
|
|
|
}
|
2012-12-21 22:37:20 +08:00
|
|
|
if (Other.ForLoopVariablePos != ForLoopVariablePos)
|
|
|
|
return Other.ForLoopVariablePos < ForLoopVariablePos;
|
|
|
|
if (Other.LineContainsContinuedForLoopSection !=
|
|
|
|
LineContainsContinuedForLoopSection)
|
|
|
|
return LineContainsContinuedForLoopSection;
|
2012-12-04 02:12:45 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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.
|
|
|
|
void addTokenToState(bool Newline, bool DryRun, IndentState &State) {
|
2012-12-04 02:12:45 +08:00
|
|
|
unsigned Index = State.ConsumedTokens;
|
|
|
|
const FormatToken &Current = Line.Tokens[Index];
|
|
|
|
const FormatToken &Previous = Line.Tokens[Index - 1];
|
2012-12-04 22:54:30 +08:00
|
|
|
unsigned ParenLevel = State.Indent.size() - 1;
|
2012-12-04 02:12:45 +08:00
|
|
|
|
|
|
|
if (Newline) {
|
2013-01-03 02:33:23 +08:00
|
|
|
unsigned WhitespaceStartColumn = State.Column;
|
2012-12-04 02:12:45 +08:00
|
|
|
if (Current.Tok.is(tok::string_literal) &&
|
2012-12-21 22:37:20 +08:00
|
|
|
Previous.Tok.is(tok::string_literal)) {
|
2013-01-07 15:56:50 +08:00
|
|
|
State.Column = State.Column - Previous.TokenLength;
|
2012-12-21 22:37:20 +08:00
|
|
|
} else if (Current.Tok.is(tok::lessless) &&
|
|
|
|
State.FirstLessLess[ParenLevel] != 0) {
|
2012-12-06 17:56:08 +08:00
|
|
|
State.Column = State.FirstLessLess[ParenLevel];
|
2012-12-21 22:37:20 +08:00
|
|
|
} else if (ParenLevel != 0 &&
|
|
|
|
(Previous.Tok.is(tok::equal) || Current.Tok.is(tok::arrow) ||
|
|
|
|
Current.Tok.is(tok::period))) {
|
2012-12-04 22:54:30 +08:00
|
|
|
// Indent and extra 4 spaces after '=' as it continues an expression.
|
|
|
|
// Don't do that on the top level, as we already indent 4 there.
|
2012-12-04 02:12:45 +08:00
|
|
|
State.Column = State.Indent[ParenLevel] + 4;
|
2012-12-25 00:51:15 +08:00
|
|
|
} else if (
|
|
|
|
Line.Tokens[0].Tok.is(tok::kw_for) && Previous.Tok.is(tok::comma)) {
|
2012-12-21 22:37:20 +08:00
|
|
|
State.Column = State.ForLoopVariablePos;
|
2013-01-02 23:08:56 +08:00
|
|
|
} else if (Annotations[Index - 1].ClosesTemplateDeclaration) {
|
|
|
|
State.Column = State.Indent[ParenLevel] - 4;
|
2012-12-21 22:37:20 +08:00
|
|
|
} else {
|
2012-12-04 02:12:45 +08:00
|
|
|
State.Column = State.Indent[ParenLevel];
|
2012-12-21 22:37:20 +08:00
|
|
|
}
|
|
|
|
|
2012-12-25 00:43:00 +08:00
|
|
|
State.StartOfLineLevel = ParenLevel + 1;
|
|
|
|
|
2012-12-21 22:37:20 +08:00
|
|
|
if (Line.Tokens[0].Tok.is(tok::kw_for))
|
|
|
|
State.LineContainsContinuedForLoopSection =
|
|
|
|
Previous.Tok.isNot(tok::semi);
|
2012-12-04 22:54:30 +08:00
|
|
|
|
2013-01-03 02:33:23 +08:00
|
|
|
if (!DryRun) {
|
|
|
|
if (!Line.InPPDirective)
|
|
|
|
replaceWhitespace(Current, 1, State.Column);
|
|
|
|
else
|
|
|
|
replacePPWhitespace(Current, 1, State.Column, WhitespaceStartColumn);
|
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2012-12-04 18:50:12 +08:00
|
|
|
State.LastSpace[ParenLevel] = State.Indent[ParenLevel];
|
2012-12-04 02:12:45 +08:00
|
|
|
if (Current.Tok.is(tok::colon) &&
|
2012-12-21 03:54:13 +08:00
|
|
|
Annotations[Index].Type != TokenAnnotation::TT_ConditionalExpr &&
|
|
|
|
Annotations[0].Type != TokenAnnotation::TT_ObjCMethodSpecifier)
|
2012-12-04 02:12:45 +08:00
|
|
|
State.Indent[ParenLevel] += 2;
|
|
|
|
} else {
|
2012-12-21 22:37:20 +08:00
|
|
|
if (Current.Tok.is(tok::equal) && Line.Tokens[0].Tok.is(tok::kw_for))
|
2013-01-07 15:56:50 +08:00
|
|
|
State.ForLoopVariablePos = State.Column - Previous.TokenLength;
|
2012-12-21 22:37:20 +08:00
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
unsigned Spaces = Annotations[Index].SpaceRequiredBefore ? 1 : 0;
|
|
|
|
if (Annotations[Index].Type == TokenAnnotation::TT_LineComment)
|
|
|
|
Spaces = 2;
|
2012-12-04 22:54:30 +08:00
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
if (!DryRun)
|
|
|
|
replaceWhitespace(Current, 0, Spaces);
|
2012-12-04 22:54:30 +08:00
|
|
|
|
2012-12-24 21:43:52 +08:00
|
|
|
// FIXME: Look into using this alignment at other ParenLevels.
|
|
|
|
if (ParenLevel == 0 && (getPrecedence(Previous) == prec::Assignment ||
|
|
|
|
Previous.Tok.is(tok::kw_return)))
|
|
|
|
State.Indent[ParenLevel] = State.Column + Spaces;
|
2012-12-04 22:54:30 +08:00
|
|
|
if (Previous.Tok.is(tok::l_paren) ||
|
|
|
|
Annotations[Index - 1].Type == TokenAnnotation::TT_TemplateOpener)
|
2012-12-04 02:12:45 +08:00
|
|
|
State.Indent[ParenLevel] = State.Column;
|
2012-12-19 05:05:13 +08:00
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
// Top-level spaces are exempt as that mostly leads to better results.
|
2012-12-06 17:56:08 +08:00
|
|
|
State.Column += Spaces;
|
2012-12-04 18:50:12 +08:00
|
|
|
if (Spaces > 0 && ParenLevel != 0)
|
2012-12-06 17:56:08 +08:00
|
|
|
State.LastSpace[ParenLevel] = State.Column;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
2012-12-04 22:54:30 +08:00
|
|
|
moveStateToNextToken(State);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Mark the next token as consumed in \p State and modify its stacks
|
|
|
|
/// accordingly.
|
|
|
|
void moveStateToNextToken(IndentState &State) {
|
|
|
|
unsigned Index = State.ConsumedTokens;
|
|
|
|
const FormatToken &Current = Line.Tokens[Index];
|
2012-12-06 17:56:08 +08:00
|
|
|
unsigned ParenLevel = State.Indent.size() - 1;
|
|
|
|
|
|
|
|
if (Current.Tok.is(tok::lessless) && State.FirstLessLess[ParenLevel] == 0)
|
|
|
|
State.FirstLessLess[ParenLevel] = State.Column;
|
|
|
|
|
2013-01-07 15:56:50 +08:00
|
|
|
State.Column += Current.TokenLength;
|
2012-12-04 22:54:30 +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.
|
|
|
|
if (Current.Tok.is(tok::l_paren) || Current.Tok.is(tok::l_square) ||
|
2012-12-24 21:43:52 +08:00
|
|
|
Current.Tok.is(tok::l_brace) ||
|
2012-12-04 22:54:30 +08:00
|
|
|
Annotations[Index].Type == TokenAnnotation::TT_TemplateOpener) {
|
|
|
|
State.Indent.push_back(4 + State.LastSpace.back());
|
|
|
|
State.LastSpace.push_back(State.LastSpace.back());
|
2012-12-06 17:56:08 +08:00
|
|
|
State.FirstLessLess.push_back(0);
|
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.
|
2012-12-04 18:50:12 +08:00
|
|
|
if (Current.Tok.is(tok::r_paren) || Current.Tok.is(tok::r_square) ||
|
2012-12-24 21:43:52 +08:00
|
|
|
(Current.Tok.is(tok::r_brace) && State.ConsumedTokens > 0) ||
|
2012-12-04 18:50:12 +08:00
|
|
|
Annotations[Index].Type == TokenAnnotation::TT_TemplateCloser) {
|
2012-12-04 02:12:45 +08:00
|
|
|
State.Indent.pop_back();
|
|
|
|
State.LastSpace.pop_back();
|
2012-12-06 17:56:08 +08:00
|
|
|
State.FirstLessLess.pop_back();
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
++State.ConsumedTokens;
|
|
|
|
}
|
|
|
|
|
2012-12-17 22:34:14 +08:00
|
|
|
/// \brief Calculate the panelty for splitting after the token at \p Index.
|
|
|
|
unsigned splitPenalty(unsigned Index) {
|
|
|
|
assert(Index < Line.Tokens.size() &&
|
|
|
|
"Tried to calculate penalty for splitting after the last token");
|
|
|
|
const FormatToken &Left = Line.Tokens[Index];
|
|
|
|
const FormatToken &Right = Line.Tokens[Index + 1];
|
2012-12-21 22:37:20 +08:00
|
|
|
|
|
|
|
// In for-loops, prefer breaking at ',' and ';'.
|
|
|
|
if (Line.Tokens[0].Tok.is(tok::kw_for) &&
|
|
|
|
(Left.Tok.isNot(tok::comma) && Left.Tok.isNot(tok::semi)))
|
|
|
|
return 20;
|
|
|
|
|
2013-01-02 23:08:56 +08:00
|
|
|
if (Left.Tok.is(tok::semi) || Left.Tok.is(tok::comma) ||
|
|
|
|
Annotations[Index].ClosesTemplateDeclaration)
|
2012-12-04 02:12:45 +08:00
|
|
|
return 0;
|
2012-12-24 08:13:23 +08:00
|
|
|
if (Left.Tok.is(tok::l_paren))
|
2013-01-02 22:40:02 +08:00
|
|
|
return 20;
|
2012-12-17 22:34:14 +08:00
|
|
|
|
2012-12-24 21:43:52 +08:00
|
|
|
prec::Level Level = getPrecedence(Line.Tokens[Index]);
|
2012-12-24 08:13:23 +08:00
|
|
|
if (Level != prec::Unknown)
|
|
|
|
return Level;
|
|
|
|
|
2012-12-17 22:34:14 +08:00
|
|
|
if (Right.Tok.is(tok::arrow) || Right.Tok.is(tok::period))
|
2013-01-07 15:13:20 +08:00
|
|
|
return 150;
|
2012-12-17 22:34:14 +08:00
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Calculate the number of lines needed to format the remaining part
|
|
|
|
/// of the unwrapped line.
|
|
|
|
///
|
|
|
|
/// Assumes the formatting so far has led to
|
|
|
|
/// the \c IndentState \p State. If \p NewLine is set, a new line will be
|
|
|
|
/// 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.
|
|
|
|
unsigned calcPenalty(IndentState State, bool NewLine, unsigned StopAt) {
|
|
|
|
// We are at the end of the unwrapped line, so we don't need any more lines.
|
|
|
|
if (State.ConsumedTokens >= Line.Tokens.size())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!NewLine && Annotations[State.ConsumedTokens].MustBreakBefore)
|
|
|
|
return UINT_MAX;
|
|
|
|
if (NewLine && !Annotations[State.ConsumedTokens].CanBreakBefore)
|
|
|
|
return UINT_MAX;
|
2012-12-21 22:37:20 +08:00
|
|
|
if (!NewLine && Line.Tokens[State.ConsumedTokens - 1].Tok.is(tok::semi) &&
|
|
|
|
State.LineContainsContinuedForLoopSection)
|
|
|
|
return UINT_MAX;
|
2012-12-04 02:12:45 +08:00
|
|
|
|
|
|
|
unsigned CurrentPenalty = 0;
|
|
|
|
if (NewLine) {
|
2012-12-04 22:54:30 +08:00
|
|
|
CurrentPenalty += Parameters.PenaltyIndentLevel * State.Indent.size() +
|
2012-12-25 00:51:15 +08:00
|
|
|
splitPenalty(State.ConsumedTokens - 1);
|
2012-12-25 00:43:00 +08:00
|
|
|
} else {
|
|
|
|
if (State.Indent.size() < State.StartOfLineLevel)
|
|
|
|
CurrentPenalty += Parameters.PenaltyLevelDecrease *
|
|
|
|
(State.StartOfLineLevel - State.Indent.size());
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2012-12-05 22:57:28 +08:00
|
|
|
addTokenToState(NewLine, true, State);
|
|
|
|
|
|
|
|
// Exceeding column limit is bad.
|
2013-01-03 02:33:23 +08:00
|
|
|
if (State.Column > Style.ColumnLimit - (Line.InPPDirective ? 1 : 0))
|
2012-12-05 22:57:28 +08:00
|
|
|
return UINT_MAX;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Replaces the whitespace in front of \p Tok. Only call once for
|
|
|
|
/// each \c FormatToken.
|
|
|
|
void replaceWhitespace(const FormatToken &Tok, unsigned NewLines,
|
|
|
|
unsigned Spaces) {
|
2013-01-03 02:33:23 +08:00
|
|
|
Replaces.insert(tooling::Replacement(
|
|
|
|
SourceMgr, Tok.WhiteSpaceStart, Tok.WhiteSpaceLength,
|
|
|
|
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 FormatToken &Tok, unsigned NewLines,
|
|
|
|
unsigned Spaces, unsigned WhitespaceStartColumn) {
|
2013-01-03 00:30:12 +08:00
|
|
|
std::string NewLineText;
|
2013-01-03 02:33:23 +08:00
|
|
|
if (NewLines > 0) {
|
2013-01-03 00:30:12 +08:00
|
|
|
unsigned Offset =
|
2013-01-03 02:33:23 +08:00
|
|
|
std::min<int>(Style.ColumnLimit - 1, WhitespaceStartColumn);
|
2013-01-03 00:30:12 +08:00
|
|
|
for (unsigned i = 0; i < NewLines; ++i) {
|
|
|
|
NewLineText += std::string(Style.ColumnLimit - Offset - 1, ' ');
|
|
|
|
NewLineText += "\\\n";
|
|
|
|
Offset = 0;
|
|
|
|
}
|
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
Replaces.insert(tooling::Replacement(
|
|
|
|
SourceMgr, Tok.WhiteSpaceStart, Tok.WhiteSpaceLength,
|
2013-01-03 00:30:12 +08:00
|
|
|
NewLineText + std::string(Spaces, ' ')));
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Add a new line and the required indent before the first Token
|
2012-12-05 21:56:52 +08:00
|
|
|
/// of the \c UnwrappedLine if there was no structural parsing error.
|
|
|
|
/// Returns the indent level of the \c UnwrappedLine.
|
2012-12-05 01:27:50 +08:00
|
|
|
unsigned formatFirstToken() {
|
2012-12-04 02:12:45 +08:00
|
|
|
const FormatToken &Token = Line.Tokens[0];
|
2012-12-05 01:27:50 +08:00
|
|
|
if (!Token.WhiteSpaceStart.isValid() || StructuralError)
|
|
|
|
return SourceMgr.getSpellingColumnNumber(Token.Tok.getLocation()) - 1;
|
|
|
|
|
|
|
|
unsigned Newlines =
|
|
|
|
std::min(Token.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
|
2013-01-06 06:56:06 +08:00
|
|
|
if (Newlines == 0 && !Token.IsFirst)
|
2012-12-05 01:27:50 +08:00
|
|
|
Newlines = 1;
|
|
|
|
unsigned Indent = Line.Level * 2;
|
2012-12-11 00:34:48 +08:00
|
|
|
if ((Token.Tok.is(tok::kw_public) || Token.Tok.is(tok::kw_protected) ||
|
|
|
|
Token.Tok.is(tok::kw_private)) &&
|
|
|
|
static_cast<int>(Indent) + Style.AccessModifierOffset >= 0)
|
2012-12-05 01:27:50 +08:00
|
|
|
Indent += Style.AccessModifierOffset;
|
2013-01-03 02:33:23 +08:00
|
|
|
if (!Line.InPPDirective || Token.HasUnescapedNewline)
|
|
|
|
replaceWhitespace(Token, Newlines, Indent);
|
|
|
|
else
|
2013-01-05 07:34:14 +08:00
|
|
|
replacePPWhitespace(Token, Newlines, Indent, PreviousEndOfLineColumn);
|
2012-12-05 01:27:50 +08:00
|
|
|
return Indent;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FormatStyle Style;
|
|
|
|
SourceManager &SourceMgr;
|
|
|
|
const UnwrappedLine &Line;
|
2013-01-05 07:34:14 +08:00
|
|
|
const unsigned PreviousEndOfLineColumn;
|
2012-12-04 02:12:45 +08:00
|
|
|
const std::vector<TokenAnnotation> &Annotations;
|
|
|
|
tooling::Replacements &Replaces;
|
2012-12-05 01:27:50 +08:00
|
|
|
bool StructuralError;
|
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).
|
|
|
|
typedef std::map<IndentState, std::pair<unsigned, unsigned> > StateMap;
|
|
|
|
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:
|
|
|
|
TokenAnnotator(const UnwrappedLine &Line, const FormatStyle &Style,
|
|
|
|
SourceManager &SourceMgr)
|
2012-12-19 05:05:13 +08:00
|
|
|
: Line(Line), Style(Style), SourceMgr(SourceMgr) {
|
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:
|
2012-12-04 04:55:42 +08:00
|
|
|
AnnotatingParser(const SmallVector<FormatToken, 16> &Tokens,
|
2012-12-04 02:12:45 +08:00
|
|
|
std::vector<TokenAnnotation> &Annotations)
|
2012-12-19 05:05:13 +08:00
|
|
|
: Tokens(Tokens), Annotations(Annotations), Index(0) {
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2012-12-04 22:54:30 +08:00
|
|
|
bool parseAngle() {
|
2012-12-04 02:12:45 +08:00
|
|
|
while (Index < Tokens.size()) {
|
|
|
|
if (Tokens[Index].Tok.is(tok::greater)) {
|
2012-12-04 18:50:12 +08:00
|
|
|
Annotations[Index].Type = TokenAnnotation::TT_TemplateCloser;
|
2012-12-04 02:12:45 +08:00
|
|
|
next();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (Tokens[Index].Tok.is(tok::r_paren) ||
|
2013-01-05 02:52:56 +08:00
|
|
|
Tokens[Index].Tok.is(tok::r_square) ||
|
|
|
|
Tokens[Index].Tok.is(tok::r_brace))
|
2012-12-04 02:12:45 +08:00
|
|
|
return false;
|
|
|
|
if (Tokens[Index].Tok.is(tok::pipepipe) ||
|
|
|
|
Tokens[Index].Tok.is(tok::ampamp) ||
|
|
|
|
Tokens[Index].Tok.is(tok::question) ||
|
|
|
|
Tokens[Index].Tok.is(tok::colon))
|
|
|
|
return false;
|
2013-01-05 02:52:56 +08:00
|
|
|
if (!consumeToken())
|
|
|
|
return false;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-04 22:54:30 +08:00
|
|
|
bool parseParens() {
|
2012-12-04 02:12:45 +08:00
|
|
|
while (Index < Tokens.size()) {
|
|
|
|
if (Tokens[Index].Tok.is(tok::r_paren)) {
|
|
|
|
next();
|
|
|
|
return true;
|
|
|
|
}
|
2013-01-05 02:52:56 +08:00
|
|
|
if (Tokens[Index].Tok.is(tok::r_square) ||
|
|
|
|
Tokens[Index].Tok.is(tok::r_brace))
|
|
|
|
return false;
|
|
|
|
if (!consumeToken())
|
2012-12-04 02:12:45 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-04 22:54:30 +08:00
|
|
|
bool parseSquare() {
|
2012-12-04 02:12:45 +08:00
|
|
|
while (Index < Tokens.size()) {
|
|
|
|
if (Tokens[Index].Tok.is(tok::r_square)) {
|
|
|
|
next();
|
|
|
|
return true;
|
|
|
|
}
|
2013-01-05 02:52:56 +08:00
|
|
|
if (Tokens[Index].Tok.is(tok::r_paren) ||
|
|
|
|
Tokens[Index].Tok.is(tok::r_brace))
|
|
|
|
return false;
|
|
|
|
if (!consumeToken())
|
2012-12-04 02:12:45 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-04 22:54:30 +08:00
|
|
|
bool parseConditional() {
|
2012-12-04 02:12:45 +08:00
|
|
|
while (Index < Tokens.size()) {
|
|
|
|
if (Tokens[Index].Tok.is(tok::colon)) {
|
|
|
|
Annotations[Index].Type = TokenAnnotation::TT_ConditionalExpr;
|
|
|
|
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() {
|
|
|
|
if (Index < Tokens.size() && Tokens[Index].Tok.is(tok::less)) {
|
|
|
|
Annotations[Index].Type = TokenAnnotation::TT_TemplateOpener;
|
|
|
|
next();
|
|
|
|
if (!parseAngle())
|
|
|
|
return false;
|
|
|
|
Annotations[Index - 1].ClosesTemplateDeclaration = true;
|
|
|
|
parseLine();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-05 02:52:56 +08:00
|
|
|
bool consumeToken() {
|
2012-12-04 02:12:45 +08:00
|
|
|
unsigned CurrentIndex = Index;
|
|
|
|
next();
|
|
|
|
switch (Tokens[CurrentIndex].Tok.getKind()) {
|
|
|
|
case tok::l_paren:
|
2013-01-05 02:52:56 +08:00
|
|
|
if (!parseParens())
|
|
|
|
return false;
|
2012-12-19 05:05:13 +08:00
|
|
|
if (Index < Tokens.size() && Tokens[Index].Tok.is(tok::colon)) {
|
|
|
|
Annotations[Index].Type = TokenAnnotation::TT_CtorInitializerColon;
|
|
|
|
next();
|
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
break;
|
|
|
|
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;
|
|
|
|
case tok::less:
|
2012-12-04 22:54:30 +08:00
|
|
|
if (parseAngle())
|
2012-12-04 02:12:45 +08:00
|
|
|
Annotations[CurrentIndex].Type = TokenAnnotation::TT_TemplateOpener;
|
|
|
|
else {
|
|
|
|
Annotations[CurrentIndex].Type = TokenAnnotation::TT_BinaryOperator;
|
|
|
|
Index = CurrentIndex + 1;
|
|
|
|
}
|
|
|
|
break;
|
2013-01-05 02:52:56 +08:00
|
|
|
case tok::r_paren:
|
|
|
|
case tok::r_square:
|
|
|
|
return false;
|
2012-12-04 02:12:45 +08:00
|
|
|
case tok::greater:
|
|
|
|
Annotations[CurrentIndex].Type = TokenAnnotation::TT_BinaryOperator;
|
|
|
|
break;
|
|
|
|
case tok::kw_operator:
|
2012-12-24 18:56:04 +08:00
|
|
|
if (Tokens[Index].Tok.is(tok::l_paren)) {
|
2012-12-04 02:12:45 +08:00
|
|
|
Annotations[Index].Type = TokenAnnotation::TT_OverloadedOperator;
|
2012-12-24 18:56:04 +08:00
|
|
|
next();
|
|
|
|
if (Index < Tokens.size() && Tokens[Index].Tok.is(tok::r_paren)) {
|
|
|
|
Annotations[Index].Type = TokenAnnotation::TT_OverloadedOperator;
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (Index < Tokens.size() && !Tokens[Index].Tok.is(tok::l_paren)) {
|
|
|
|
Annotations[Index].Type = TokenAnnotation::TT_OverloadedOperator;
|
|
|
|
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() {
|
|
|
|
while (Index < Tokens.size()) {
|
|
|
|
if (Tokens[Index].Tok.is(tok::slash))
|
|
|
|
Annotations[Index].Type = TokenAnnotation::TT_DirectorySeparator;
|
|
|
|
else if (Tokens[Index].Tok.is(tok::less))
|
|
|
|
Annotations[Index].Type = TokenAnnotation::TT_TemplateOpener;
|
|
|
|
else if (Tokens[Index].Tok.is(tok::greater))
|
|
|
|
Annotations[Index].Type = TokenAnnotation::TT_TemplateCloser;
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void parsePreprocessorDirective() {
|
|
|
|
next();
|
|
|
|
if (Index >= Tokens.size())
|
|
|
|
return;
|
2013-01-06 06:56:06 +08:00
|
|
|
// Hashes in the middle of a line can lead to any strange token
|
|
|
|
// sequence.
|
|
|
|
if (Tokens[Index].Tok.getIdentifierInfo() == NULL)
|
|
|
|
return;
|
2012-12-22 01:58:39 +08:00
|
|
|
switch (Tokens[Index].Tok.getIdentifierInfo()->getPPKeywordID()) {
|
|
|
|
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;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-05 02:52:56 +08:00
|
|
|
bool parseLine() {
|
2012-12-22 01:58:39 +08:00
|
|
|
if (Tokens[Index].Tok.is(tok::hash)) {
|
|
|
|
parsePreprocessorDirective();
|
2013-01-05 02:52:56 +08:00
|
|
|
return true;
|
2012-12-22 01:58:39 +08:00
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
while (Index < Tokens.size()) {
|
2013-01-05 02:52:56 +08:00
|
|
|
if (!consumeToken())
|
|
|
|
return false;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
2013-01-05 02:52:56 +08:00
|
|
|
return true;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void next() {
|
|
|
|
++Index;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const SmallVector<FormatToken, 16> &Tokens;
|
|
|
|
std::vector<TokenAnnotation> &Annotations;
|
|
|
|
unsigned Index;
|
|
|
|
};
|
|
|
|
|
2013-01-05 02:52:56 +08:00
|
|
|
bool annotate() {
|
2012-12-04 02:12:45 +08:00
|
|
|
Annotations.clear();
|
|
|
|
for (int i = 0, e = Line.Tokens.size(); i != e; ++i) {
|
|
|
|
Annotations.push_back(TokenAnnotation());
|
|
|
|
}
|
|
|
|
|
2012-12-04 04:55:42 +08:00
|
|
|
AnnotatingParser Parser(Line.Tokens, Annotations);
|
2013-01-05 02:52:56 +08:00
|
|
|
if (!Parser.parseLine())
|
|
|
|
return false;
|
2012-12-04 02:12:45 +08:00
|
|
|
|
|
|
|
determineTokenTypes();
|
2012-12-21 03:54:13 +08:00
|
|
|
bool IsObjCMethodDecl =
|
2012-12-21 17:41:31 +08:00
|
|
|
(Line.Tokens.size() > 0 &&
|
|
|
|
(Annotations[0].Type == TokenAnnotation::TT_ObjCMethodSpecifier));
|
2012-12-04 02:12:45 +08:00
|
|
|
for (int i = 1, e = Line.Tokens.size(); i != e; ++i) {
|
|
|
|
TokenAnnotation &Annotation = Annotations[i];
|
|
|
|
|
2013-01-02 16:44:14 +08:00
|
|
|
Annotation.CanBreakBefore = canBreakBefore(i);
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2012-12-19 05:05:13 +08:00
|
|
|
if (Annotation.Type == TokenAnnotation::TT_CtorInitializerColon) {
|
|
|
|
Annotation.MustBreakBefore = true;
|
|
|
|
Annotation.SpaceRequiredBefore = true;
|
2012-12-24 18:56:04 +08:00
|
|
|
} else if (Annotation.Type == TokenAnnotation::TT_OverloadedOperator) {
|
|
|
|
Annotation.SpaceRequiredBefore =
|
2012-12-25 00:51:15 +08:00
|
|
|
Line.Tokens[i].Tok.is(tok::identifier) ||
|
|
|
|
Line.Tokens[i].Tok.is(tok::kw_new) ||
|
|
|
|
Line.Tokens[i].Tok.is(tok::kw_delete);
|
2012-12-24 18:56:04 +08:00
|
|
|
} else if (
|
|
|
|
Annotations[i - 1].Type == TokenAnnotation::TT_OverloadedOperator) {
|
|
|
|
Annotation.SpaceRequiredBefore = false;
|
2012-12-21 17:41:31 +08:00
|
|
|
} else if (IsObjCMethodDecl && Line.Tokens[i].Tok.is(tok::identifier) &&
|
|
|
|
(i != e - 1) && Line.Tokens[i + 1].Tok.is(tok::colon) &&
|
|
|
|
Line.Tokens[i - 1].Tok.is(tok::identifier)) {
|
2012-12-21 03:54:13 +08:00
|
|
|
Annotation.CanBreakBefore = true;
|
|
|
|
Annotation.SpaceRequiredBefore = true;
|
2012-12-21 17:41:31 +08:00
|
|
|
} else if (IsObjCMethodDecl && Line.Tokens[i].Tok.is(tok::identifier) &&
|
|
|
|
Line.Tokens[i - 1].Tok.is(tok::l_paren) &&
|
|
|
|
Line.Tokens[i - 2].Tok.is(tok::colon)) {
|
2012-12-21 03:54:13 +08:00
|
|
|
// Don't break this identifier as ':' or identifier
|
|
|
|
// before it will break.
|
|
|
|
Annotation.CanBreakBefore = false;
|
|
|
|
} else if (Line.Tokens[i].Tok.is(tok::at) &&
|
2012-12-21 17:41:31 +08:00
|
|
|
Line.Tokens[i - 2].Tok.is(tok::at)) {
|
2012-12-21 03:54:13 +08:00
|
|
|
// Don't put two objc's '@' on the same line. This could happen,
|
2012-12-22 01:14:23 +08:00
|
|
|
// as in, @optional @property ...
|
2012-12-21 03:54:13 +08:00
|
|
|
Annotation.MustBreakBefore = true;
|
2012-12-19 05:05:13 +08:00
|
|
|
} else if (Line.Tokens[i].Tok.is(tok::colon)) {
|
2012-12-06 00:24:48 +08:00
|
|
|
Annotation.SpaceRequiredBefore =
|
2012-12-25 00:51:15 +08:00
|
|
|
Line.Tokens[0].Tok.isNot(tok::kw_case) && !IsObjCMethodDecl &&
|
|
|
|
(i != e - 1);
|
2012-12-21 03:54:13 +08:00
|
|
|
// Don't break at ':' if identifier before it can beak.
|
2012-12-21 17:41:31 +08:00
|
|
|
if (IsObjCMethodDecl && Line.Tokens[i - 1].Tok.is(tok::identifier) &&
|
|
|
|
Annotations[i - 1].CanBreakBefore)
|
2012-12-21 03:54:13 +08:00
|
|
|
Annotation.CanBreakBefore = false;
|
2012-12-21 17:41:31 +08:00
|
|
|
} else if (
|
|
|
|
Annotations[i - 1].Type == TokenAnnotation::TT_ObjCMethodSpecifier) {
|
2012-12-21 03:54:13 +08:00
|
|
|
Annotation.SpaceRequiredBefore = true;
|
2012-12-21 17:41:31 +08:00
|
|
|
} else if (Annotations[i - 1].Type == TokenAnnotation::TT_UnaryOperator) {
|
2012-12-04 02:12:45 +08:00
|
|
|
Annotation.SpaceRequiredBefore = false;
|
|
|
|
} else if (Annotation.Type == TokenAnnotation::TT_UnaryOperator) {
|
|
|
|
Annotation.SpaceRequiredBefore =
|
2012-12-04 21:02:32 +08:00
|
|
|
Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&
|
|
|
|
Line.Tokens[i - 1].Tok.isNot(tok::l_square);
|
2012-12-04 02:12:45 +08:00
|
|
|
} else if (Line.Tokens[i - 1].Tok.is(tok::greater) &&
|
|
|
|
Line.Tokens[i].Tok.is(tok::greater)) {
|
2012-12-04 21:02:32 +08:00
|
|
|
if (Annotation.Type == TokenAnnotation::TT_TemplateCloser &&
|
2012-12-04 22:54:30 +08:00
|
|
|
Annotations[i - 1].Type == TokenAnnotation::TT_TemplateCloser)
|
2012-12-04 18:50:12 +08:00
|
|
|
Annotation.SpaceRequiredBefore = Style.SplitTemplateClosingGreater;
|
2012-12-04 02:12:45 +08:00
|
|
|
else
|
|
|
|
Annotation.SpaceRequiredBefore = false;
|
2012-12-22 01:58:39 +08:00
|
|
|
} else if (
|
|
|
|
Annotation.Type == TokenAnnotation::TT_DirectorySeparator ||
|
|
|
|
Annotations[i - 1].Type == TokenAnnotation::TT_DirectorySeparator) {
|
|
|
|
Annotation.SpaceRequiredBefore = false;
|
2012-12-04 02:12:45 +08:00
|
|
|
} else if (
|
|
|
|
Annotation.Type == TokenAnnotation::TT_BinaryOperator ||
|
|
|
|
Annotations[i - 1].Type == TokenAnnotation::TT_BinaryOperator) {
|
|
|
|
Annotation.SpaceRequiredBefore = true;
|
|
|
|
} else if (
|
2012-12-04 18:50:12 +08:00
|
|
|
Annotations[i - 1].Type == TokenAnnotation::TT_TemplateCloser &&
|
2012-12-04 02:12:45 +08:00
|
|
|
Line.Tokens[i].Tok.is(tok::l_paren)) {
|
|
|
|
Annotation.SpaceRequiredBefore = false;
|
2012-12-04 21:02:32 +08:00
|
|
|
} else if (Line.Tokens[i].Tok.is(tok::less) &&
|
|
|
|
Line.Tokens[0].Tok.is(tok::hash)) {
|
|
|
|
Annotation.SpaceRequiredBefore = true;
|
2012-12-21 17:41:31 +08:00
|
|
|
} else if (IsObjCMethodDecl && Line.Tokens[i - 1].Tok.is(tok::r_paren) &&
|
|
|
|
Line.Tokens[i].Tok.is(tok::identifier)) {
|
2012-12-21 03:54:13 +08:00
|
|
|
// Don't space between ')' and <id>
|
|
|
|
Annotation.SpaceRequiredBefore = false;
|
2012-12-21 17:41:31 +08:00
|
|
|
} else if (IsObjCMethodDecl && Line.Tokens[i - 1].Tok.is(tok::colon) &&
|
|
|
|
Line.Tokens[i].Tok.is(tok::l_paren)) {
|
2012-12-21 03:54:13 +08:00
|
|
|
// Don't space between ':' and '('
|
|
|
|
Annotation.SpaceRequiredBefore = false;
|
2012-12-21 17:41:31 +08:00
|
|
|
} else if (Annotation.Type == TokenAnnotation::TT_TrailingUnaryOperator) {
|
|
|
|
Annotation.SpaceRequiredBefore = false;
|
|
|
|
} else {
|
2012-12-04 02:12:45 +08:00
|
|
|
Annotation.SpaceRequiredBefore =
|
|
|
|
spaceRequiredBetween(Line.Tokens[i - 1].Tok, Line.Tokens[i].Tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Annotations[i - 1].Type == TokenAnnotation::TT_LineComment ||
|
|
|
|
(Line.Tokens[i].Tok.is(tok::string_literal) &&
|
|
|
|
Line.Tokens[i - 1].Tok.is(tok::string_literal))) {
|
|
|
|
Annotation.MustBreakBefore = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Annotation.MustBreakBefore)
|
|
|
|
Annotation.CanBreakBefore = true;
|
|
|
|
}
|
2013-01-05 02:52:56 +08:00
|
|
|
return true;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<TokenAnnotation> &getAnnotations() {
|
|
|
|
return Annotations;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void determineTokenTypes() {
|
2012-12-23 09:07:46 +08:00
|
|
|
bool IsRHS = false;
|
2012-12-04 02:12:45 +08:00
|
|
|
for (int i = 0, e = Line.Tokens.size(); i != e; ++i) {
|
|
|
|
TokenAnnotation &Annotation = Annotations[i];
|
|
|
|
const FormatToken &Tok = Line.Tokens[i];
|
|
|
|
|
2012-12-24 21:43:52 +08:00
|
|
|
if (getPrecedence(Tok) == prec::Assignment)
|
2012-12-23 09:07:46 +08:00
|
|
|
IsRHS = true;
|
|
|
|
else if (Tok.Tok.is(tok::kw_return))
|
|
|
|
IsRHS = true;
|
2012-12-05 15:51:39 +08:00
|
|
|
|
2012-12-21 18:20:02 +08:00
|
|
|
if (Annotation.Type != TokenAnnotation::TT_Unknown)
|
|
|
|
continue;
|
|
|
|
|
2012-12-21 17:41:31 +08:00
|
|
|
if (Tok.Tok.is(tok::star) || Tok.Tok.is(tok::amp)) {
|
2012-12-23 09:07:46 +08:00
|
|
|
Annotation.Type = determineStarAmpUsage(i, IsRHS);
|
2012-12-21 17:41:31 +08:00
|
|
|
} else if (Tok.Tok.is(tok::minus) || Tok.Tok.is(tok::plus)) {
|
|
|
|
Annotation.Type = determinePlusMinusUsage(i);
|
|
|
|
} else if (Tok.Tok.is(tok::minusminus) || Tok.Tok.is(tok::plusplus)) {
|
|
|
|
Annotation.Type = determineIncrementUsage(i);
|
|
|
|
} else if (Tok.Tok.is(tok::exclaim)) {
|
2012-12-04 02:12:45 +08:00
|
|
|
Annotation.Type = TokenAnnotation::TT_UnaryOperator;
|
2012-12-21 18:20:02 +08:00
|
|
|
} else if (isBinaryOperator(Line.Tokens[i])) {
|
2012-12-04 02:12:45 +08:00
|
|
|
Annotation.Type = TokenAnnotation::TT_BinaryOperator;
|
2012-12-21 18:20:02 +08:00
|
|
|
} else if (Tok.Tok.is(tok::comment)) {
|
2013-01-07 15:56:50 +08:00
|
|
|
// FIXME: Use Lexer::getSpelling(Tok, SourceMgr, LangOpts, bool*);
|
2012-12-04 02:12:45 +08:00
|
|
|
StringRef Data(SourceMgr.getCharacterData(Tok.Tok.getLocation()),
|
|
|
|
Tok.Tok.getLength());
|
|
|
|
if (Data.startswith("//"))
|
|
|
|
Annotation.Type = TokenAnnotation::TT_LineComment;
|
|
|
|
else
|
|
|
|
Annotation.Type = TokenAnnotation::TT_BlockComment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isBinaryOperator(const FormatToken &Tok) {
|
2012-12-22 01:58:39 +08:00
|
|
|
// Comma is a binary operator, but does not behave as such wrt. formatting.
|
2012-12-24 21:43:52 +08:00
|
|
|
return getPrecedence(Tok) > prec::Comma;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2012-12-25 00:51:15 +08:00
|
|
|
TokenAnnotation::TokenType determineStarAmpUsage(unsigned Index, bool IsRHS) {
|
2013-01-03 01:21:36 +08:00
|
|
|
if (Index == 0)
|
|
|
|
return TokenAnnotation::TT_UnaryOperator;
|
2012-12-04 02:12:45 +08:00
|
|
|
if (Index == Annotations.size())
|
|
|
|
return TokenAnnotation::TT_Unknown;
|
2013-01-02 23:46:59 +08:00
|
|
|
const FormatToken &PrevToken = Line.Tokens[Index - 1];
|
|
|
|
const FormatToken &NextToken = Line.Tokens[Index + 1];
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-01-05 04:46:38 +08:00
|
|
|
if (PrevToken.Tok.is(tok::l_paren) || PrevToken.Tok.is(tok::l_square) ||
|
|
|
|
PrevToken.Tok.is(tok::comma) || PrevToken.Tok.is(tok::kw_return) ||
|
|
|
|
PrevToken.Tok.is(tok::colon) ||
|
2012-12-04 02:12:45 +08:00
|
|
|
Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator)
|
|
|
|
return TokenAnnotation::TT_UnaryOperator;
|
|
|
|
|
2013-01-02 23:46:59 +08:00
|
|
|
if (PrevToken.Tok.isLiteral() || NextToken.Tok.isLiteral() ||
|
2013-01-03 01:21:36 +08:00
|
|
|
NextToken.Tok.is(tok::plus) || NextToken.Tok.is(tok::minus) ||
|
|
|
|
NextToken.Tok.is(tok::plusplus) || NextToken.Tok.is(tok::minusminus) ||
|
|
|
|
NextToken.Tok.is(tok::tilde) || NextToken.Tok.is(tok::exclaim) ||
|
|
|
|
NextToken.Tok.is(tok::kw_alignof) || NextToken.Tok.is(tok::kw_sizeof))
|
2012-12-04 02:12:45 +08:00
|
|
|
return TokenAnnotation::TT_BinaryOperator;
|
|
|
|
|
2013-01-02 23:46:59 +08:00
|
|
|
if (NextToken.Tok.is(tok::comma) || NextToken.Tok.is(tok::r_paren) ||
|
|
|
|
NextToken.Tok.is(tok::greater))
|
|
|
|
return TokenAnnotation::TT_PointerOrReference;
|
|
|
|
|
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.
|
2012-12-23 09:07:46 +08:00
|
|
|
if (IsRHS)
|
2012-12-05 15:51:39 +08:00
|
|
|
return TokenAnnotation::TT_BinaryOperator;
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
return TokenAnnotation::TT_PointerOrReference;
|
|
|
|
}
|
|
|
|
|
2012-12-21 17:41:31 +08:00
|
|
|
TokenAnnotation::TokenType determinePlusMinusUsage(unsigned Index) {
|
|
|
|
// At the start of the line, +/- specific ObjectiveC method declarations.
|
|
|
|
if (Index == 0)
|
|
|
|
return TokenAnnotation::TT_ObjCMethodSpecifier;
|
|
|
|
|
|
|
|
// Use heuristics to recognize unary operators.
|
|
|
|
const Token &PreviousTok = Line.Tokens[Index - 1].Tok;
|
|
|
|
if (PreviousTok.is(tok::equal) || PreviousTok.is(tok::l_paren) ||
|
|
|
|
PreviousTok.is(tok::comma) || PreviousTok.is(tok::l_square) ||
|
2013-01-02 23:26:16 +08:00
|
|
|
PreviousTok.is(tok::question) || PreviousTok.is(tok::colon) ||
|
|
|
|
PreviousTok.is(tok::kw_return) || PreviousTok.is(tok::kw_case))
|
2012-12-21 17:41:31 +08:00
|
|
|
return TokenAnnotation::TT_UnaryOperator;
|
|
|
|
|
|
|
|
// There can't be to consecutive binary operators.
|
|
|
|
if (Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator)
|
|
|
|
return TokenAnnotation::TT_UnaryOperator;
|
|
|
|
|
|
|
|
// Fall back to marking the token as binary operator.
|
|
|
|
return TokenAnnotation::TT_BinaryOperator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Determine whether ++/-- are pre- or post-increments/-decrements.
|
|
|
|
TokenAnnotation::TokenType determineIncrementUsage(unsigned Index) {
|
|
|
|
if (Index != 0 && Line.Tokens[Index - 1].Tok.is(tok::identifier))
|
|
|
|
return TokenAnnotation::TT_TrailingUnaryOperator;
|
|
|
|
|
|
|
|
return TokenAnnotation::TT_UnaryOperator;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool spaceRequiredBetween(Token Left, Token Right) {
|
2012-12-11 02:59:13 +08:00
|
|
|
if (Right.is(tok::r_paren) || Right.is(tok::semi) || Right.is(tok::comma))
|
|
|
|
return false;
|
2012-12-04 02:12:45 +08:00
|
|
|
if (Left.is(tok::kw_template) && Right.is(tok::less))
|
|
|
|
return true;
|
|
|
|
if (Left.is(tok::arrow) || Right.is(tok::arrow))
|
|
|
|
return false;
|
|
|
|
if (Left.is(tok::exclaim) || Left.is(tok::tilde))
|
|
|
|
return false;
|
2012-12-21 03:54:13 +08:00
|
|
|
if (Left.is(tok::at) && Right.is(tok::identifier))
|
|
|
|
return false;
|
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))
|
|
|
|
return Left.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))
|
|
|
|
return Right.isLiteral() || Style.PointerAndReferenceBindToType;
|
|
|
|
if (Right.is(tok::star) && Left.is(tok::l_paren))
|
|
|
|
return false;
|
|
|
|
if (Left.is(tok::l_square) || Right.is(tok::l_square) ||
|
|
|
|
Right.is(tok::r_square))
|
|
|
|
return false;
|
2012-12-07 17:52:15 +08:00
|
|
|
if (Left.is(tok::coloncolon) ||
|
|
|
|
(Right.is(tok::coloncolon) &&
|
|
|
|
(Left.is(tok::identifier) || Left.is(tok::greater))))
|
2012-12-04 02:12:45 +08:00
|
|
|
return false;
|
|
|
|
if (Left.is(tok::period) || Right.is(tok::period))
|
|
|
|
return false;
|
|
|
|
if (Left.is(tok::colon) || Right.is(tok::colon))
|
|
|
|
return true;
|
|
|
|
if (Left.is(tok::l_paren))
|
|
|
|
return false;
|
|
|
|
if (Left.is(tok::hash))
|
|
|
|
return false;
|
|
|
|
if (Right.is(tok::l_paren)) {
|
2012-12-21 17:41:31 +08:00
|
|
|
return Left.is(tok::kw_if) || Left.is(tok::kw_for) ||
|
2012-12-25 00:51:15 +08:00
|
|
|
Left.is(tok::kw_while) || Left.is(tok::kw_switch) ||
|
|
|
|
(Left.isNot(tok::identifier) && Left.isNot(tok::kw_sizeof) &&
|
2013-01-03 01:21:36 +08:00
|
|
|
Left.isNot(tok::kw_typeof) && Left.isNot(tok::kw_alignof));
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-02 16:44:14 +08:00
|
|
|
bool canBreakBefore(unsigned i) {
|
2013-01-03 02:30:06 +08:00
|
|
|
if (Annotations[i - 1].ClosesTemplateDeclaration)
|
|
|
|
return true;
|
2013-01-02 16:44:14 +08:00
|
|
|
if (Annotations[i - 1].Type == TokenAnnotation::TT_PointerOrReference ||
|
2013-01-03 02:30:06 +08:00
|
|
|
Annotations[i - 1].Type == TokenAnnotation::TT_TemplateCloser ||
|
2013-01-02 16:44:14 +08:00
|
|
|
Annotations[i].Type == TokenAnnotation::TT_ConditionalExpr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const FormatToken &Left = Line.Tokens[i - 1];
|
|
|
|
const FormatToken &Right = Line.Tokens[i];
|
2012-12-17 19:29:41 +08:00
|
|
|
if (Right.Tok.is(tok::r_paren) || Right.Tok.is(tok::l_brace) ||
|
|
|
|
Right.Tok.is(tok::comment) || Right.Tok.is(tok::greater))
|
2012-12-04 02:12:45 +08:00
|
|
|
return false;
|
2012-12-21 18:20:02 +08:00
|
|
|
return (isBinaryOperator(Left) && Left.Tok.isNot(tok::lessless)) ||
|
2012-12-25 00:51:15 +08:00
|
|
|
Left.Tok.is(tok::comma) || Right.Tok.is(tok::lessless) ||
|
|
|
|
Right.Tok.is(tok::arrow) || Right.Tok.is(tok::period) ||
|
|
|
|
Right.Tok.is(tok::colon) || Left.Tok.is(tok::semi) ||
|
|
|
|
Left.Tok.is(tok::l_brace) ||
|
|
|
|
(Left.Tok.is(tok::l_paren) && !Right.Tok.is(tok::r_paren));
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const UnwrappedLine &Line;
|
|
|
|
FormatStyle Style;
|
|
|
|
SourceManager &SourceMgr;
|
|
|
|
std::vector<TokenAnnotation> Annotations;
|
|
|
|
};
|
|
|
|
|
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');
|
|
|
|
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;
|
|
|
|
while (i + 1 < Text.size() && Text[i] == '\\' && Text[i+1] == '\n') {
|
|
|
|
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:
|
|
|
|
Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr,
|
|
|
|
const std::vector<CharSourceRange> &Ranges)
|
2012-12-19 05:05:13 +08:00
|
|
|
: Style(Style), Lex(Lex), SourceMgr(SourceMgr), Ranges(Ranges),
|
2012-12-05 01:27:50 +08:00
|
|
|
StructuralError(false) {
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2012-12-05 05:05:31 +08:00
|
|
|
virtual ~Formatter() {
|
|
|
|
}
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
tooling::Replacements format() {
|
2012-12-08 00:15:44 +08:00
|
|
|
LexerBasedFormatTokenSource Tokens(Lex, SourceMgr);
|
|
|
|
UnwrappedLineParser Parser(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;
|
2012-12-05 01:27:50 +08:00
|
|
|
for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),
|
|
|
|
E = UnwrappedLines.end();
|
|
|
|
I != E; ++I)
|
2013-01-05 07:34:14 +08:00
|
|
|
PreviousEndOfLineColumn =
|
|
|
|
formatUnwrappedLine(*I, PreviousEndOfLineColumn);
|
2012-12-04 02:12:45 +08:00
|
|
|
return Replaces;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-12-05 21:56:52 +08:00
|
|
|
virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
|
2012-12-05 01:27:50 +08:00
|
|
|
UnwrappedLines.push_back(TheLine);
|
|
|
|
}
|
|
|
|
|
2013-01-05 07:34:14 +08:00
|
|
|
unsigned formatUnwrappedLine(const UnwrappedLine &TheLine,
|
|
|
|
unsigned PreviousEndOfLineColumn) {
|
|
|
|
if (TheLine.Tokens.empty())
|
|
|
|
return 0; // FIXME: Find out how this can ever happen.
|
2012-12-04 02:12:45 +08:00
|
|
|
|
|
|
|
CharSourceRange LineRange =
|
|
|
|
CharSourceRange::getTokenRange(TheLine.Tokens.front().Tok.getLocation(),
|
|
|
|
TheLine.Tokens.back().Tok.getLocation());
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
|
|
|
|
if (SourceMgr.isBeforeInTranslationUnit(LineRange.getEnd(),
|
|
|
|
Ranges[i].getBegin()) ||
|
|
|
|
SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
|
|
|
|
LineRange.getBegin()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
TokenAnnotator Annotator(TheLine, Style, SourceMgr);
|
2013-01-05 02:52:56 +08:00
|
|
|
if (!Annotator.annotate())
|
2013-01-05 07:34:14 +08:00
|
|
|
break;
|
|
|
|
UnwrappedLineFormatter Formatter(
|
|
|
|
Style, SourceMgr, TheLine, PreviousEndOfLineColumn,
|
|
|
|
Annotator.getAnnotations(), Replaces, StructuralError);
|
|
|
|
return Formatter.format();
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
2013-01-05 07:34:14 +08:00
|
|
|
// 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.
|
|
|
|
const FormatToken &Token = TheLine.Tokens.back();
|
|
|
|
return SourceMgr.getSpellingColumnNumber(Token.Tok.getLocation()) +
|
|
|
|
Lex.MeasureTokenLength(Token.Tok.getLocation(), SourceMgr,
|
|
|
|
Lex.getLangOpts()) -
|
|
|
|
1;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FormatStyle Style;
|
|
|
|
Lexer &Lex;
|
|
|
|
SourceManager &SourceMgr;
|
|
|
|
tooling::Replacements Replaces;
|
|
|
|
std::vector<CharSourceRange> Ranges;
|
2012-12-05 01:27:50 +08:00
|
|
|
std::vector<UnwrappedLine> UnwrappedLines;
|
|
|
|
bool StructuralError;
|
2012-12-04 02:12:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
|
|
|
|
SourceManager &SourceMgr,
|
|
|
|
std::vector<CharSourceRange> Ranges) {
|
|
|
|
Formatter formatter(Style, Lex, SourceMgr, Ranges);
|
|
|
|
return formatter.format();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace format
|
|
|
|
} // namespace clang
|