2013-08-16 19:20:30 +08:00
|
|
|
//===--- ContinuationIndenter.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 the continuation indenter.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ContinuationIndenter.h"
|
2017-09-20 17:51:03 +08:00
|
|
|
#include "BreakableToken.h"
|
2017-10-30 22:01:50 +08:00
|
|
|
#include "FormatInternal.h"
|
2013-08-16 19:20:30 +08:00
|
|
|
#include "WhitespaceManager.h"
|
|
|
|
#include "clang/Basic/OperatorPrecedence.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Format/Format.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
|
2017-01-25 21:58:58 +08:00
|
|
|
#define DEBUG_TYPE "format-indenter"
|
2014-04-22 11:17:02 +08:00
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
|
|
|
|
// Returns the length of everything up to the first possible line break after
|
|
|
|
// the ), ], } or > matching \c Tok.
|
|
|
|
static unsigned getLengthToMatchingParen(const FormatToken &Tok) {
|
2014-05-09 16:15:10 +08:00
|
|
|
if (!Tok.MatchingParen)
|
2013-08-16 19:20:30 +08:00
|
|
|
return 0;
|
|
|
|
FormatToken *End = Tok.MatchingParen;
|
|
|
|
while (End->Next && !End->Next->CanBreakBefore) {
|
|
|
|
End = End->Next;
|
|
|
|
}
|
|
|
|
return End->TotalLength - Tok.TotalLength + 1;
|
|
|
|
}
|
|
|
|
|
2016-01-05 21:03:59 +08:00
|
|
|
static unsigned getLengthToNextOperator(const FormatToken &Tok) {
|
|
|
|
if (!Tok.NextOperator)
|
|
|
|
return 0;
|
|
|
|
return Tok.NextOperator->TotalLength - Tok.TotalLength;
|
|
|
|
}
|
|
|
|
|
clang-format: Format segments of builder-type calls one per line.
This fixes llvm.org/PR14818.
Before:
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:
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);
llvm-svn: 189353
2013-08-27 22:24:43 +08:00
|
|
|
// Returns \c true if \c Tok is the "." or "->" of a call and starts the next
|
|
|
|
// segment of a builder type call.
|
|
|
|
static bool startsSegmentOfBuilderTypeCall(const FormatToken &Tok) {
|
|
|
|
return Tok.isMemberAccess() && Tok.Previous && Tok.Previous->closesScope();
|
|
|
|
}
|
|
|
|
|
2013-10-08 13:11:18 +08:00
|
|
|
// Returns \c true if \c Current starts a new parameter.
|
|
|
|
static bool startsNextParameter(const FormatToken &Current,
|
|
|
|
const FormatStyle &Style) {
|
|
|
|
const FormatToken &Previous = *Current.Previous;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current.is(TT_CtorInitializerComma) &&
|
2017-05-24 19:36:58 +08:00
|
|
|
Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma)
|
2013-10-08 13:11:18 +08:00
|
|
|
return true;
|
2017-06-27 21:43:07 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_Proto && Current.is(TT_SelectorName))
|
|
|
|
return true;
|
2013-10-08 13:11:18 +08:00
|
|
|
return Previous.is(tok::comma) && !Current.isTrailingComment() &&
|
2017-03-10 23:10:37 +08:00
|
|
|
((Previous.isNot(TT_CtorInitializerComma) ||
|
2017-05-24 19:36:58 +08:00
|
|
|
Style.BreakConstructorInitializers !=
|
|
|
|
FormatStyle::BCIS_BeforeComma) &&
|
2017-03-10 23:10:37 +08:00
|
|
|
(Previous.isNot(TT_InheritanceComma) ||
|
2017-05-24 19:36:58 +08:00
|
|
|
!Style.BreakBeforeInheritanceComma));
|
2013-10-08 13:11:18 +08:00
|
|
|
}
|
|
|
|
|
2017-07-03 23:05:14 +08:00
|
|
|
static bool opensProtoMessageField(const FormatToken &LessTok,
|
|
|
|
const FormatStyle &Style) {
|
|
|
|
if (LessTok.isNot(tok::less))
|
|
|
|
return false;
|
|
|
|
return Style.Language == FormatStyle::LK_TextProto ||
|
|
|
|
(Style.Language == FormatStyle::LK_Proto &&
|
|
|
|
(LessTok.NestingLevel > 0 ||
|
|
|
|
(LessTok.Previous && LessTok.Previous->is(tok::equal))));
|
|
|
|
}
|
|
|
|
|
2017-10-30 22:01:50 +08:00
|
|
|
// Returns the delimiter of a raw string literal, or None if TokenText is not
|
|
|
|
// the text of a raw string literal. The delimiter could be the empty string.
|
|
|
|
// For example, the delimiter of R"deli(cont)deli" is deli.
|
|
|
|
static llvm::Optional<StringRef> getRawStringDelimiter(StringRef TokenText) {
|
|
|
|
if (TokenText.size() < 5 // The smallest raw string possible is 'R"()"'.
|
|
|
|
|| !TokenText.startswith("R\"") || !TokenText.endswith("\""))
|
|
|
|
return None;
|
|
|
|
|
|
|
|
// A raw string starts with 'R"<delimiter>(' and delimiter is ascii and has
|
|
|
|
// size at most 16 by the standard, so the first '(' must be among the first
|
|
|
|
// 19 bytes.
|
|
|
|
size_t LParenPos = TokenText.substr(0, 19).find_first_of('(');
|
|
|
|
if (LParenPos == StringRef::npos)
|
|
|
|
return None;
|
|
|
|
StringRef Delimiter = TokenText.substr(2, LParenPos - 2);
|
|
|
|
|
|
|
|
// Check that the string ends in ')Delimiter"'.
|
|
|
|
size_t RParenPos = TokenText.size() - Delimiter.size() - 2;
|
|
|
|
if (TokenText[RParenPos] != ')')
|
|
|
|
return None;
|
|
|
|
if (!TokenText.substr(RParenPos + 1).startswith(Delimiter))
|
|
|
|
return None;
|
|
|
|
return Delimiter;
|
|
|
|
}
|
|
|
|
|
|
|
|
RawStringFormatStyleManager::RawStringFormatStyleManager(
|
|
|
|
const FormatStyle &CodeStyle) {
|
|
|
|
for (const auto &RawStringFormat : CodeStyle.RawStringFormats) {
|
|
|
|
FormatStyle Style;
|
|
|
|
if (!getPredefinedStyle(RawStringFormat.BasedOnStyle,
|
|
|
|
RawStringFormat.Language, &Style)) {
|
|
|
|
Style = getLLVMStyle();
|
|
|
|
Style.Language = RawStringFormat.Language;
|
|
|
|
}
|
|
|
|
Style.ColumnLimit = CodeStyle.ColumnLimit;
|
|
|
|
DelimiterStyle.insert({RawStringFormat.Delimiter, Style});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Optional<FormatStyle>
|
|
|
|
RawStringFormatStyleManager::get(StringRef Delimiter) const {
|
|
|
|
auto It = DelimiterStyle.find(Delimiter);
|
|
|
|
if (It == DelimiterStyle.end())
|
|
|
|
return None;
|
|
|
|
return It->second;
|
|
|
|
}
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style,
|
2014-11-04 20:41:02 +08:00
|
|
|
const AdditionalKeywords &Keywords,
|
2016-04-28 15:52:03 +08:00
|
|
|
const SourceManager &SourceMgr,
|
2013-08-16 19:20:30 +08:00
|
|
|
WhitespaceManager &Whitespaces,
|
|
|
|
encoding::Encoding Encoding,
|
|
|
|
bool BinPackInconclusiveFunctions)
|
2014-11-04 20:41:02 +08:00
|
|
|
: Style(Style), Keywords(Keywords), SourceMgr(SourceMgr),
|
|
|
|
Whitespaces(Whitespaces), Encoding(Encoding),
|
2014-01-02 23:13:14 +08:00
|
|
|
BinPackInconclusiveFunctions(BinPackInconclusiveFunctions),
|
2017-10-30 22:01:50 +08:00
|
|
|
CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) {}
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2013-09-05 17:29:45 +08:00
|
|
|
LineState ContinuationIndenter::getInitialState(unsigned FirstIndent,
|
2017-10-30 22:01:50 +08:00
|
|
|
unsigned FirstStartColumn,
|
2013-09-06 15:54:20 +08:00
|
|
|
const AnnotatedLine *Line,
|
|
|
|
bool DryRun) {
|
2013-08-16 19:20:30 +08:00
|
|
|
LineState State;
|
2013-09-05 17:29:45 +08:00
|
|
|
State.FirstIndent = FirstIndent;
|
2017-10-30 22:01:50 +08:00
|
|
|
if (FirstStartColumn && Line->First->NewlinesBefore == 0)
|
|
|
|
State.Column = FirstStartColumn;
|
|
|
|
else
|
|
|
|
State.Column = FirstIndent;
|
clang-format: Add preprocessor directive indentation
Summary:
This is an implementation for [bug 17362](https://bugs.llvm.org/attachment.cgi?bugid=17362) which adds support for indenting preprocessor statements inside if/ifdef/endif. This takes previous work from fmauch (https://github.com/fmauch/clang/tree/preprocessor_indent) and makes it into a full feature.
The context of this patch is that I'm a VMware intern, and I implemented this because VMware needs the feature. As such, some decisions were made based on what VMware wants, and I would appreciate suggestions on expanding this if necessary to use-cases other people may want.
This adds a new enum config option, `IndentPPDirectives`. Values are:
* `PPDIS_None` (in config: `None`):
```
#if FOO
#if BAR
#include <foo>
#endif
#endif
```
* `PPDIS_AfterHash` (in config: `AfterHash`):
```
#if FOO
# if BAR
# include <foo>
# endif
#endif
```
This is meant to work whether spaces or tabs are used for indentation. Preprocessor indentation is independent of indentation for non-preprocessor lines.
Preprocessor indentation also attempts to ignore include guards with the checks:
1. Include guards cover the entire file
2. Include guards don't have `#else`
3. Include guards begin with
```
#ifndef <var>
#define <var>
```
This patch allows `UnwrappedLineParser::PPBranchLevel` to be decremented to -1 (the initial value is -1) so the variable can be used for indent tracking.
Defects:
* This patch does not handle the case where there's code between the `#ifndef` and `#define` but all other conditions hold. This is because when the #define line is parsed, `UnwrappedLineParser::Lines` doesn't hold the previous code line yet, so we can't detect it. This is out of the scope of this patch.
* This patch does not handle cases where legitimate lines may be outside an include guard. Examples are `#pragma once` and `#pragma GCC diagnostic`, or anything else that does not change the meaning of the file if it's included multiple times.
* This does not detect when there is a single non-preprocessor line in front of an include-guard-like structure where other conditions hold because `ScopedLineState` hides the line.
* Preprocessor indentation throws off `TokenAnnotator::setCommentLineLevels` so the indentation of comments immediately before indented preprocessor lines is toggled on each run. Fixing this issue appears to be a major change and too much complexity for this patch.
Contributed by @euhlmann!
Reviewers: djasper, klimek, krasimir
Reviewed By: djasper, krasimir
Subscribers: krasimir, mzeren-vmw, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D35955
llvm-svn: 312125
2017-08-30 22:34:57 +08:00
|
|
|
// With preprocessor directive indentation, the line starts on column 0
|
|
|
|
// since it's indented after the hash, but FirstIndent is set to the
|
|
|
|
// preprocessor indent.
|
|
|
|
if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash &&
|
|
|
|
(Line->Type == LT_PreprocessorDirective ||
|
|
|
|
Line->Type == LT_ImportStatement))
|
|
|
|
State.Column = 0;
|
2013-09-05 17:29:45 +08:00
|
|
|
State.Line = Line;
|
|
|
|
State.NextToken = Line->First;
|
2017-01-31 19:25:01 +08:00
|
|
|
State.Stack.push_back(ParenState(FirstIndent, FirstIndent,
|
2013-08-16 19:20:30 +08:00
|
|
|
/*AvoidBinPacking=*/false,
|
|
|
|
/*NoLineBreak=*/false));
|
|
|
|
State.LineContainsContinuedForLoopSection = false;
|
2017-10-16 17:08:53 +08:00
|
|
|
State.NoContinuation = false;
|
2013-08-16 19:20:30 +08:00
|
|
|
State.StartOfStringLiteral = 0;
|
2014-05-08 20:21:30 +08:00
|
|
|
State.StartOfLineLevel = 0;
|
|
|
|
State.LowestLevelOnLine = 0;
|
2013-08-16 19:20:30 +08:00
|
|
|
State.IgnoreStackForComparison = false;
|
|
|
|
|
2017-07-03 23:05:14 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_TextProto) {
|
|
|
|
// We need this in order to deal with the bin packing of text fields at
|
|
|
|
// global scope.
|
|
|
|
State.Stack.back().AvoidBinPacking = true;
|
|
|
|
State.Stack.back().BreakBeforeParameter = true;
|
|
|
|
}
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
// The first token has already been indented and thus consumed.
|
2013-09-06 15:54:20 +08:00
|
|
|
moveStateToNextToken(State, DryRun, /*Newline=*/false);
|
2013-08-16 19:20:30 +08:00
|
|
|
return State;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContinuationIndenter::canBreak(const LineState &State) {
|
|
|
|
const FormatToken &Current = *State.NextToken;
|
|
|
|
const FormatToken &Previous = *Current.Previous;
|
|
|
|
assert(&Previous == Current.Previous);
|
2017-09-20 17:51:03 +08:00
|
|
|
if (!Current.CanBreakBefore && !(State.Stack.back().BreakBeforeClosingBrace &&
|
|
|
|
Current.closesBlockOrBlockTypeList(Style)))
|
2013-08-16 19:20:30 +08:00
|
|
|
return false;
|
|
|
|
// The opening "{" of a braced list has to be on the same line as the first
|
|
|
|
// element if it is nested in another braced init list or function call.
|
|
|
|
if (!Current.MustBreakBefore && Previous.is(tok::l_brace) &&
|
2014-11-25 18:05:17 +08:00
|
|
|
Previous.isNot(TT_DictLiteral) && Previous.BlockKind == BK_BracedInit &&
|
2014-05-09 21:11:16 +08:00
|
|
|
Previous.Previous &&
|
2013-08-16 19:20:30 +08:00
|
|
|
Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma))
|
|
|
|
return false;
|
|
|
|
// This prevents breaks like:
|
|
|
|
// ...
|
|
|
|
// SomeParameter, OtherParameter).DoSomething(
|
|
|
|
// ...
|
|
|
|
// As they hide "DoSomething" and are generally bad for readability.
|
2014-03-11 19:03:26 +08:00
|
|
|
if (Previous.opensScope() && Previous.isNot(tok::l_brace) &&
|
2014-07-15 17:00:34 +08:00
|
|
|
State.LowestLevelOnLine < State.StartOfLineLevel &&
|
|
|
|
State.LowestLevelOnLine < Current.NestingLevel)
|
2013-08-16 19:20:30 +08:00
|
|
|
return false;
|
clang-format: Format segments of builder-type calls one per line.
This fixes llvm.org/PR14818.
Before:
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:
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);
llvm-svn: 189353
2013-08-27 22:24:43 +08:00
|
|
|
if (Current.isMemberAccess() && State.Stack.back().ContainsUnwrappedBuilder)
|
|
|
|
return false;
|
2014-06-03 20:02:45 +08:00
|
|
|
|
|
|
|
// Don't create a 'hanging' indent if there are multiple blocks in a single
|
|
|
|
// statement.
|
2014-11-21 21:38:53 +08:00
|
|
|
if (Previous.is(tok::l_brace) && State.Stack.size() > 1 &&
|
|
|
|
State.Stack[State.Stack.size() - 2].NestedBlockInlined &&
|
2014-06-03 20:02:45 +08:00
|
|
|
State.Stack[State.Stack.size() - 2].HasMultipleNestedBlocks)
|
|
|
|
return false;
|
|
|
|
|
2014-10-28 01:13:59 +08:00
|
|
|
// Don't break after very short return types (e.g. "void") as that is often
|
|
|
|
// unexpected.
|
2015-12-19 06:20:15 +08:00
|
|
|
if (Current.is(TT_FunctionDeclarationName) && State.Column < 6) {
|
|
|
|
if (Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None)
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-28 01:13:59 +08:00
|
|
|
|
2017-01-16 21:13:15 +08:00
|
|
|
// If binary operators are moved to the next line (including commas for some
|
|
|
|
// styles of constructor initializers), that's always ok.
|
|
|
|
if (!Current.isOneOf(TT_BinaryOperator, tok::comma) &&
|
|
|
|
State.Stack.back().NoLineBreakInOperand)
|
|
|
|
return false;
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
return !State.Stack.back().NoLineBreak;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContinuationIndenter::mustBreak(const LineState &State) {
|
|
|
|
const FormatToken &Current = *State.NextToken;
|
|
|
|
const FormatToken &Previous = *Current.Previous;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current.MustBreakBefore || Current.is(TT_InlineASMColon))
|
2013-08-16 19:20:30 +08:00
|
|
|
return true;
|
2013-10-22 23:30:28 +08:00
|
|
|
if (State.Stack.back().BreakBeforeClosingBrace &&
|
2015-10-27 21:42:08 +08:00
|
|
|
Current.closesBlockOrBlockTypeList(Style))
|
2013-08-16 19:20:30 +08:00
|
|
|
return true;
|
|
|
|
if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection)
|
|
|
|
return true;
|
2013-10-08 13:11:18 +08:00
|
|
|
if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) ||
|
2016-01-09 23:56:47 +08:00
|
|
|
(Previous.is(TT_TemplateCloser) && Current.is(TT_StartOfName) &&
|
2017-03-31 21:30:24 +08:00
|
|
|
Style.isCpp() &&
|
2016-01-11 19:01:05 +08:00
|
|
|
// FIXME: This is a temporary workaround for the case where clang-format
|
|
|
|
// sets BreakBeforeParameter to avoid bin packing and this creates a
|
|
|
|
// completely unnecessary line break after a template type that isn't
|
|
|
|
// line-wrapped.
|
|
|
|
(Previous.NestingLevel == 1 || Style.BinPackParameters)) ||
|
2015-05-27 13:37:40 +08:00
|
|
|
(Style.BreakBeforeTernaryOperators && Current.is(TT_ConditionalExpr) &&
|
|
|
|
Previous.isNot(tok::question)) ||
|
2013-11-08 08:57:11 +08:00
|
|
|
(!Style.BreakBeforeTernaryOperators &&
|
2015-05-27 13:37:40 +08:00
|
|
|
Previous.is(TT_ConditionalExpr))) &&
|
2013-08-16 19:20:30 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter && !Current.isTrailingComment() &&
|
|
|
|
!Current.isOneOf(tok::r_paren, tok::r_brace))
|
|
|
|
return true;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (((Previous.is(TT_DictLiteral) && Previous.is(tok::l_brace)) ||
|
2016-01-04 15:27:33 +08:00
|
|
|
(Previous.is(TT_ArrayInitializerLSquare) &&
|
2017-07-03 23:05:14 +08:00
|
|
|
Previous.ParameterCount > 1) ||
|
|
|
|
opensProtoMessageField(Previous, Style)) &&
|
2014-04-14 20:11:07 +08:00
|
|
|
Style.ColumnLimit > 0 &&
|
2015-06-02 23:14:21 +08:00
|
|
|
getLengthToMatchingParen(Previous) + State.Column - 1 >
|
|
|
|
getColumnLimit(State))
|
2013-10-21 00:45:46 +08:00
|
|
|
return true;
|
2017-05-24 19:36:58 +08:00
|
|
|
|
|
|
|
const FormatToken &BreakConstructorInitializersToken =
|
|
|
|
Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon
|
|
|
|
? Previous
|
|
|
|
: Current;
|
|
|
|
if (BreakConstructorInitializersToken.is(TT_CtorInitializerColon) &&
|
|
|
|
(State.Column + State.Line->Last->TotalLength - Previous.TotalLength >
|
2015-08-27 19:59:31 +08:00
|
|
|
getColumnLimit(State) ||
|
|
|
|
State.Stack.back().BreakBeforeParameter) &&
|
2017-05-24 19:36:58 +08:00
|
|
|
(Style.AllowShortFunctionsOnASingleLine != FormatStyle::SFS_All ||
|
|
|
|
Style.BreakConstructorInitializers != FormatStyle::BCIS_BeforeColon ||
|
|
|
|
Style.ColumnLimit != 0))
|
2014-03-28 00:14:13 +08:00
|
|
|
return true;
|
2017-05-24 19:36:58 +08:00
|
|
|
|
2016-11-12 15:38:22 +08:00
|
|
|
if (Current.is(TT_ObjCMethodExpr) && !Previous.is(TT_SelectorName) &&
|
|
|
|
State.Line->startsWith(TT_ObjCMethodSpecifier))
|
|
|
|
return true;
|
2015-05-06 21:13:03 +08:00
|
|
|
if (Current.is(TT_SelectorName) && State.Stack.back().ObjCSelectorNameFound &&
|
|
|
|
State.Stack.back().BreakBeforeParameter)
|
|
|
|
return true;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2015-06-18 17:12:47 +08:00
|
|
|
unsigned NewLineColumn = getNewLineColumn(State);
|
2016-01-14 21:36:46 +08:00
|
|
|
if (Current.isMemberAccess() && Style.ColumnLimit != 0 &&
|
2016-01-11 19:00:58 +08:00
|
|
|
State.Column + getLengthToNextOperator(Current) > Style.ColumnLimit &&
|
|
|
|
(State.Column > NewLineColumn ||
|
|
|
|
Current.NestingLevel < State.StartOfLineLevel))
|
2016-01-05 21:03:59 +08:00
|
|
|
return true;
|
|
|
|
|
2017-01-14 07:18:16 +08:00
|
|
|
if (startsSegmentOfBuilderTypeCall(Current) &&
|
|
|
|
(State.Stack.back().CallContinuation != 0 ||
|
2017-01-30 15:08:40 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter) &&
|
|
|
|
// JavaScript is treated different here as there is a frequent pattern:
|
|
|
|
// SomeFunction(function() {
|
|
|
|
// ...
|
|
|
|
// }.bind(...));
|
|
|
|
// FIXME: We should find a more generic solution to this problem.
|
2017-05-29 15:50:52 +08:00
|
|
|
!(State.Column <= NewLineColumn &&
|
2017-01-30 15:08:40 +08:00
|
|
|
Style.Language == FormatStyle::LK_JavaScript))
|
2017-01-14 07:18:16 +08:00
|
|
|
return true;
|
|
|
|
|
2016-01-11 19:00:58 +08:00
|
|
|
if (State.Column <= NewLineColumn)
|
|
|
|
return false;
|
|
|
|
|
2015-06-18 17:12:47 +08:00
|
|
|
if (Style.AlwaysBreakBeforeMultilineStrings &&
|
2015-06-19 00:05:17 +08:00
|
|
|
(NewLineColumn == State.FirstIndent + Style.ContinuationIndentWidth ||
|
2015-06-19 18:32:28 +08:00
|
|
|
Previous.is(tok::comma) || Current.NestingLevel < 2) &&
|
2015-06-18 17:12:47 +08:00
|
|
|
!Previous.isOneOf(tok::kw_return, tok::lessless, tok::at) &&
|
|
|
|
!Previous.isOneOf(TT_InlineASMColon, TT_ConditionalExpr) &&
|
|
|
|
nextIsMultilineString(State))
|
|
|
|
return true;
|
|
|
|
|
2015-05-11 05:15:07 +08:00
|
|
|
// Using CanBreakBefore here and below takes care of the decision whether the
|
|
|
|
// current style uses wrapping before or after operators for the given
|
|
|
|
// operator.
|
|
|
|
if (Previous.is(TT_BinaryOperator) && Current.CanBreakBefore) {
|
2013-08-16 19:20:30 +08:00
|
|
|
// If we need to break somewhere inside the LHS of a binary expression, we
|
|
|
|
// should also break after the operator. Otherwise, the formatting would
|
|
|
|
// hide the operator precedence, e.g. in:
|
|
|
|
// if (aaaaaaaaaaaaaa ==
|
|
|
|
// bbbbbbbbbbbbbb && c) {..
|
|
|
|
// For comparisons, we only apply this rule, if the LHS is a binary
|
|
|
|
// expression itself as otherwise, the line breaks seem superfluous.
|
|
|
|
// We need special cases for ">>" which we have split into two ">" while
|
|
|
|
// lexing in order to make template parsing easier.
|
|
|
|
bool IsComparison = (Previous.getPrecedence() == prec::Relational ||
|
|
|
|
Previous.getPrecedence() == prec::Equality) &&
|
|
|
|
Previous.Previous &&
|
2014-11-25 18:05:17 +08:00
|
|
|
Previous.Previous->isNot(TT_BinaryOperator); // For >>.
|
2013-08-16 19:20:30 +08:00
|
|
|
bool LHSIsBinaryExpr =
|
2013-09-06 16:08:14 +08:00
|
|
|
Previous.Previous && Previous.Previous->EndsBinaryExpression;
|
2015-05-11 05:15:07 +08:00
|
|
|
if ((!IsComparison || LHSIsBinaryExpr) && !Current.isTrailingComment() &&
|
2013-08-16 19:20:30 +08:00
|
|
|
Previous.getPrecedence() != prec::Assignment &&
|
|
|
|
State.Stack.back().BreakBeforeParameter)
|
|
|
|
return true;
|
2015-05-11 05:15:07 +08:00
|
|
|
} else if (Current.is(TT_BinaryOperator) && Current.CanBreakBefore &&
|
|
|
|
State.Stack.back().BreakBeforeParameter) {
|
|
|
|
return true;
|
2013-08-16 19:20:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Same as above, but for the first "<<" operator.
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current.is(tok::lessless) && Current.isNot(TT_OverloadedOperator) &&
|
2014-03-06 23:13:08 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter &&
|
2013-08-16 19:20:30 +08:00
|
|
|
State.Stack.back().FirstLessLess == 0)
|
|
|
|
return true;
|
|
|
|
|
2014-12-09 04:08:04 +08:00
|
|
|
if (Current.NestingLevel == 0 && !Current.isTrailingComment()) {
|
2015-05-18 17:47:22 +08:00
|
|
|
// Always break after "template <...>" and leading annotations. This is only
|
|
|
|
// for cases where the entire line does not fit on a single line as a
|
|
|
|
// different LineFormatter would be used otherwise.
|
2014-12-09 04:08:04 +08:00
|
|
|
if (Previous.ClosesTemplateDeclaration)
|
|
|
|
return true;
|
2015-05-18 21:47:23 +08:00
|
|
|
if (Previous.is(TT_FunctionAnnotationRParen))
|
2015-05-18 17:47:22 +08:00
|
|
|
return true;
|
2015-01-10 07:25:06 +08:00
|
|
|
if (Previous.is(TT_LeadingJavaAnnotation) && Current.isNot(tok::l_paren) &&
|
|
|
|
Current.isNot(TT_LeadingJavaAnnotation))
|
2014-12-09 04:08:04 +08:00
|
|
|
return true;
|
|
|
|
}
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2014-07-09 15:50:33 +08:00
|
|
|
// If the return type spans multiple lines, wrap before the function name.
|
2015-07-16 03:11:58 +08:00
|
|
|
if ((Current.is(TT_FunctionDeclarationName) ||
|
|
|
|
(Current.is(tok::kw_operator) && !Previous.is(tok::coloncolon))) &&
|
2016-02-05 22:17:16 +08:00
|
|
|
!Previous.is(tok::kw_template) && State.Stack.back().BreakBeforeParameter)
|
2013-08-16 19:20:30 +08:00
|
|
|
return true;
|
2014-07-09 15:50:33 +08:00
|
|
|
|
2014-01-05 20:38:10 +08:00
|
|
|
// The following could be precomputed as they do not depend on the state.
|
|
|
|
// However, as they should take effect only if the UnwrappedLine does not fit
|
|
|
|
// into the ColumnLimit, they are checked here in the ContinuationIndenter.
|
2014-04-29 22:05:20 +08:00
|
|
|
if (Style.ColumnLimit != 0 && Previous.BlockKind == BK_Block &&
|
|
|
|
Previous.is(tok::l_brace) && !Current.isOneOf(tok::r_brace, tok::comment))
|
2014-01-05 20:38:10 +08:00
|
|
|
return true;
|
|
|
|
|
2016-01-05 21:06:27 +08:00
|
|
|
if (Current.is(tok::lessless) &&
|
|
|
|
((Previous.is(tok::identifier) && Previous.TokenText == "endl") ||
|
|
|
|
(Previous.Tok.isLiteral() && (Previous.TokenText.endswith("\\n\"") ||
|
|
|
|
Previous.TokenText == "\'\\n\'"))))
|
2015-02-17 18:05:15 +08:00
|
|
|
return true;
|
|
|
|
|
2017-10-16 17:08:53 +08:00
|
|
|
if (Previous.is(TT_BlockComment) && Previous.IsMultiline)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (State.NoContinuation)
|
|
|
|
return true;
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline,
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
bool DryRun,
|
|
|
|
unsigned ExtraSpaces) {
|
2013-08-16 19:20:30 +08:00
|
|
|
const FormatToken &Current = *State.NextToken;
|
|
|
|
|
2014-03-18 19:22:45 +08:00
|
|
|
assert(!State.Stack.empty());
|
2017-10-16 17:08:53 +08:00
|
|
|
State.NoContinuation = false;
|
|
|
|
|
2014-11-25 18:05:17 +08:00
|
|
|
if ((Current.is(TT_ImplicitStringLiteral) &&
|
2014-05-09 16:15:10 +08:00
|
|
|
(Current.Previous->Tok.getIdentifierInfo() == nullptr ||
|
2013-10-30 21:54:53 +08:00
|
|
|
Current.Previous->Tok.getIdentifierInfo()->getPPKeywordID() ==
|
|
|
|
tok::pp_not_keyword))) {
|
2015-01-31 15:05:46 +08:00
|
|
|
unsigned EndColumn =
|
|
|
|
SourceMgr.getSpellingColumnNumber(Current.WhitespaceRange.getEnd());
|
|
|
|
if (Current.LastNewlineOffset != 0) {
|
|
|
|
// If there is a newline within this token, the final column will solely
|
|
|
|
// determined by the current end column.
|
|
|
|
State.Column = EndColumn;
|
|
|
|
} else {
|
|
|
|
unsigned StartColumn =
|
|
|
|
SourceMgr.getSpellingColumnNumber(Current.WhitespaceRange.getBegin());
|
|
|
|
assert(EndColumn >= StartColumn);
|
|
|
|
State.Column += EndColumn - StartColumn;
|
|
|
|
}
|
2014-03-31 22:23:49 +08:00
|
|
|
moveStateToNextToken(State, DryRun, /*Newline=*/false);
|
2013-08-16 19:20:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-01 22:41:18 +08:00
|
|
|
unsigned Penalty = 0;
|
|
|
|
if (Newline)
|
|
|
|
Penalty = addTokenOnNewLine(State, DryRun);
|
|
|
|
else
|
2013-11-20 22:54:39 +08:00
|
|
|
addTokenOnCurrentLine(State, DryRun, ExtraSpaces);
|
2013-10-01 22:41:18 +08:00
|
|
|
|
|
|
|
return moveStateToNextToken(State, DryRun, Newline) + Penalty;
|
|
|
|
}
|
|
|
|
|
2013-11-20 22:54:39 +08:00
|
|
|
void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
|
|
|
|
unsigned ExtraSpaces) {
|
2013-10-12 05:25:45 +08:00
|
|
|
FormatToken &Current = *State.NextToken;
|
2013-10-01 22:41:18 +08:00
|
|
|
const FormatToken &Previous = *State.NextToken->Previous;
|
|
|
|
if (Current.is(tok::equal) &&
|
2014-05-08 20:21:30 +08:00
|
|
|
(State.Line->First->is(tok::kw_for) || Current.NestingLevel == 0) &&
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().VariablePos == 0) {
|
|
|
|
State.Stack.back().VariablePos = State.Column;
|
|
|
|
// Move over * and & if they are bound to the variable name.
|
|
|
|
const FormatToken *Tok = &Previous;
|
|
|
|
while (Tok && State.Stack.back().VariablePos >= Tok->ColumnWidth) {
|
|
|
|
State.Stack.back().VariablePos -= Tok->ColumnWidth;
|
|
|
|
if (Tok->SpacesRequiredBefore != 0)
|
|
|
|
break;
|
|
|
|
Tok = Tok->Previous;
|
|
|
|
}
|
|
|
|
if (Previous.PartOfMultiVariableDeclStmt)
|
|
|
|
State.Stack.back().LastSpace = State.Stack.back().VariablePos;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces;
|
|
|
|
|
clang-format: Add preprocessor directive indentation
Summary:
This is an implementation for [bug 17362](https://bugs.llvm.org/attachment.cgi?bugid=17362) which adds support for indenting preprocessor statements inside if/ifdef/endif. This takes previous work from fmauch (https://github.com/fmauch/clang/tree/preprocessor_indent) and makes it into a full feature.
The context of this patch is that I'm a VMware intern, and I implemented this because VMware needs the feature. As such, some decisions were made based on what VMware wants, and I would appreciate suggestions on expanding this if necessary to use-cases other people may want.
This adds a new enum config option, `IndentPPDirectives`. Values are:
* `PPDIS_None` (in config: `None`):
```
#if FOO
#if BAR
#include <foo>
#endif
#endif
```
* `PPDIS_AfterHash` (in config: `AfterHash`):
```
#if FOO
# if BAR
# include <foo>
# endif
#endif
```
This is meant to work whether spaces or tabs are used for indentation. Preprocessor indentation is independent of indentation for non-preprocessor lines.
Preprocessor indentation also attempts to ignore include guards with the checks:
1. Include guards cover the entire file
2. Include guards don't have `#else`
3. Include guards begin with
```
#ifndef <var>
#define <var>
```
This patch allows `UnwrappedLineParser::PPBranchLevel` to be decremented to -1 (the initial value is -1) so the variable can be used for indent tracking.
Defects:
* This patch does not handle the case where there's code between the `#ifndef` and `#define` but all other conditions hold. This is because when the #define line is parsed, `UnwrappedLineParser::Lines` doesn't hold the previous code line yet, so we can't detect it. This is out of the scope of this patch.
* This patch does not handle cases where legitimate lines may be outside an include guard. Examples are `#pragma once` and `#pragma GCC diagnostic`, or anything else that does not change the meaning of the file if it's included multiple times.
* This does not detect when there is a single non-preprocessor line in front of an include-guard-like structure where other conditions hold because `ScopedLineState` hides the line.
* Preprocessor indentation throws off `TokenAnnotator::setCommentLineLevels` so the indentation of comments immediately before indented preprocessor lines is toggled on each run. Fixing this issue appears to be a major change and too much complexity for this patch.
Contributed by @euhlmann!
Reviewers: djasper, klimek, krasimir
Reviewed By: djasper, krasimir
Subscribers: krasimir, mzeren-vmw, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D35955
llvm-svn: 312125
2017-08-30 22:34:57 +08:00
|
|
|
// Indent preprocessor directives after the hash if required.
|
|
|
|
int PPColumnCorrection = 0;
|
|
|
|
if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash &&
|
|
|
|
Previous.is(tok::hash) && State.FirstIndent > 0 &&
|
|
|
|
(State.Line->Type == LT_PreprocessorDirective ||
|
|
|
|
State.Line->Type == LT_ImportStatement)) {
|
|
|
|
Spaces += State.FirstIndent;
|
|
|
|
|
|
|
|
// For preprocessor indent with tabs, State.Column will be 1 because of the
|
|
|
|
// hash. This causes second-level indents onward to have an extra space
|
|
|
|
// after the tabs. We avoid this misalignment by subtracting 1 from the
|
|
|
|
// column value passed to replaceWhitespace().
|
|
|
|
if (Style.UseTab != FormatStyle::UT_Never)
|
|
|
|
PPColumnCorrection = -1;
|
|
|
|
}
|
|
|
|
|
2013-10-01 22:41:18 +08:00
|
|
|
if (!DryRun)
|
2017-01-31 19:25:01 +08:00
|
|
|
Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, Spaces,
|
clang-format: Add preprocessor directive indentation
Summary:
This is an implementation for [bug 17362](https://bugs.llvm.org/attachment.cgi?bugid=17362) which adds support for indenting preprocessor statements inside if/ifdef/endif. This takes previous work from fmauch (https://github.com/fmauch/clang/tree/preprocessor_indent) and makes it into a full feature.
The context of this patch is that I'm a VMware intern, and I implemented this because VMware needs the feature. As such, some decisions were made based on what VMware wants, and I would appreciate suggestions on expanding this if necessary to use-cases other people may want.
This adds a new enum config option, `IndentPPDirectives`. Values are:
* `PPDIS_None` (in config: `None`):
```
#if FOO
#if BAR
#include <foo>
#endif
#endif
```
* `PPDIS_AfterHash` (in config: `AfterHash`):
```
#if FOO
# if BAR
# include <foo>
# endif
#endif
```
This is meant to work whether spaces or tabs are used for indentation. Preprocessor indentation is independent of indentation for non-preprocessor lines.
Preprocessor indentation also attempts to ignore include guards with the checks:
1. Include guards cover the entire file
2. Include guards don't have `#else`
3. Include guards begin with
```
#ifndef <var>
#define <var>
```
This patch allows `UnwrappedLineParser::PPBranchLevel` to be decremented to -1 (the initial value is -1) so the variable can be used for indent tracking.
Defects:
* This patch does not handle the case where there's code between the `#ifndef` and `#define` but all other conditions hold. This is because when the #define line is parsed, `UnwrappedLineParser::Lines` doesn't hold the previous code line yet, so we can't detect it. This is out of the scope of this patch.
* This patch does not handle cases where legitimate lines may be outside an include guard. Examples are `#pragma once` and `#pragma GCC diagnostic`, or anything else that does not change the meaning of the file if it's included multiple times.
* This does not detect when there is a single non-preprocessor line in front of an include-guard-like structure where other conditions hold because `ScopedLineState` hides the line.
* Preprocessor indentation throws off `TokenAnnotator::setCommentLineLevels` so the indentation of comments immediately before indented preprocessor lines is toggled on each run. Fixing this issue appears to be a major change and too much complexity for this patch.
Contributed by @euhlmann!
Reviewers: djasper, klimek, krasimir
Reviewed By: djasper, krasimir
Subscribers: krasimir, mzeren-vmw, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D35955
llvm-svn: 312125
2017-08-30 22:34:57 +08:00
|
|
|
State.Column + Spaces + PPColumnCorrection);
|
2013-10-01 22:41:18 +08:00
|
|
|
|
2017-03-10 23:10:37 +08:00
|
|
|
// If "BreakBeforeInheritanceComma" mode, don't break within the inheritance
|
|
|
|
// declaration unless there is multiple inheritance.
|
|
|
|
if (Style.BreakBeforeInheritanceComma && Current.is(TT_InheritanceColon))
|
|
|
|
State.Stack.back().NoLineBreak = true;
|
|
|
|
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current.is(TT_SelectorName) &&
|
2013-12-23 15:29:06 +08:00
|
|
|
!State.Stack.back().ObjCSelectorNameFound) {
|
2016-01-04 15:29:07 +08:00
|
|
|
unsigned MinIndent =
|
|
|
|
std::max(State.FirstIndent + Style.ContinuationIndentWidth,
|
|
|
|
State.Stack.back().Indent);
|
|
|
|
unsigned FirstColonPos = State.Column + Spaces + Current.ColumnWidth;
|
2013-12-23 15:29:06 +08:00
|
|
|
if (Current.LongestObjCSelectorName == 0)
|
|
|
|
State.Stack.back().AlignColons = false;
|
2016-01-04 15:29:07 +08:00
|
|
|
else if (MinIndent + Current.LongestObjCSelectorName > FirstColonPos)
|
|
|
|
State.Stack.back().ColonPos = MinIndent + Current.LongestObjCSelectorName;
|
2013-10-01 22:41:18 +08:00
|
|
|
else
|
2016-01-04 15:29:07 +08:00
|
|
|
State.Stack.back().ColonPos = FirstColonPos;
|
2013-10-01 22:41:18 +08:00
|
|
|
}
|
|
|
|
|
2015-10-27 20:38:37 +08:00
|
|
|
// In "AlwaysBreak" mode, enforce wrapping directly after the parenthesis by
|
|
|
|
// disallowing any further line breaks if there is no line break after the
|
|
|
|
// opening parenthesis. Don't break if it doesn't conserve columns.
|
|
|
|
if (Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak &&
|
2016-02-02 18:28:11 +08:00
|
|
|
Previous.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) &&
|
|
|
|
State.Column > getNewLineColumn(State) &&
|
2017-09-20 17:51:03 +08:00
|
|
|
(!Previous.Previous || !Previous.Previous->isOneOf(
|
|
|
|
tok::kw_for, tok::kw_while, tok::kw_switch)) &&
|
2016-03-17 20:00:22 +08:00
|
|
|
// Don't do this for simple (no expressions) one-argument function calls
|
|
|
|
// as that feels like needlessly wasting whitespace, e.g.:
|
|
|
|
//
|
|
|
|
// caaaaaaaaaaaall(
|
|
|
|
// caaaaaaaaaaaall(
|
|
|
|
// caaaaaaaaaaaall(
|
|
|
|
// caaaaaaaaaaaaaaaaaaaaaaall(aaaaaaaaaaaaaa, aaaaaaaaa))));
|
|
|
|
Current.FakeLParens.size() > 0 &&
|
|
|
|
Current.FakeLParens.back() > prec::Unknown)
|
2015-10-27 20:38:37 +08:00
|
|
|
State.Stack.back().NoLineBreak = true;
|
2017-02-20 22:51:16 +08:00
|
|
|
if (Previous.is(TT_TemplateString) && Previous.opensScope())
|
|
|
|
State.Stack.back().NoLineBreak = true;
|
2015-10-27 20:38:37 +08:00
|
|
|
|
|
|
|
if (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign &&
|
|
|
|
Previous.opensScope() && Previous.isNot(TT_ObjCMethodExpr) &&
|
2014-11-25 18:05:17 +08:00
|
|
|
(Current.isNot(TT_LineComment) || Previous.BlockKind == BK_BracedInit))
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().Indent = State.Column + Spaces;
|
2013-10-08 13:11:18 +08:00
|
|
|
if (State.Stack.back().AvoidBinPacking && startsNextParameter(Current, Style))
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().NoLineBreak = true;
|
2015-04-24 18:08:09 +08:00
|
|
|
if (startsSegmentOfBuilderTypeCall(Current) &&
|
|
|
|
State.Column > getNewLineColumn(State))
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().ContainsUnwrappedBuilder = true;
|
|
|
|
|
2015-06-05 21:18:09 +08:00
|
|
|
if (Current.is(TT_LambdaArrow) && Style.Language == FormatStyle::LK_Java)
|
2015-03-27 02:46:28 +08:00
|
|
|
State.Stack.back().NoLineBreak = true;
|
2014-09-13 00:35:28 +08:00
|
|
|
if (Current.isMemberAccess() && Previous.is(tok::r_paren) &&
|
|
|
|
(Previous.MatchingParen &&
|
2017-02-20 22:51:16 +08:00
|
|
|
(Previous.TotalLength - Previous.MatchingParen->TotalLength > 10)))
|
2014-09-13 00:35:28 +08:00
|
|
|
// If there is a function call with long parameters, break before trailing
|
|
|
|
// calls. This prevents things like:
|
|
|
|
// EXPECT_CALL(SomeLongParameter).Times(
|
|
|
|
// 2);
|
|
|
|
// We don't want to do this for short parameters as they can just be
|
|
|
|
// indexes.
|
|
|
|
State.Stack.back().NoLineBreak = true;
|
|
|
|
|
2017-01-16 21:13:15 +08:00
|
|
|
// Don't allow the RHS of an operator to be split over multiple lines unless
|
|
|
|
// there is a line-break right after the operator.
|
|
|
|
// Exclude relational operators, as there, it is always more desirable to
|
|
|
|
// have the LHS 'left' of the RHS.
|
|
|
|
const FormatToken *P = Current.getPreviousNonComment();
|
|
|
|
if (!Current.is(tok::comment) && P &&
|
|
|
|
(P->isOneOf(TT_BinaryOperator, tok::comma) ||
|
|
|
|
(P->is(TT_ConditionalExpr) && P->is(tok::colon))) &&
|
|
|
|
!P->isOneOf(TT_OverloadedOperator, TT_CtorInitializerComma) &&
|
|
|
|
P->getPrecedence() != prec::Assignment &&
|
|
|
|
P->getPrecedence() != prec::Relational) {
|
|
|
|
bool BreakBeforeOperator =
|
2017-02-02 07:27:37 +08:00
|
|
|
P->MustBreakBefore || P->is(tok::lessless) ||
|
2017-01-16 21:13:15 +08:00
|
|
|
(P->is(TT_BinaryOperator) &&
|
|
|
|
Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None) ||
|
|
|
|
(P->is(TT_ConditionalExpr) && Style.BreakBeforeTernaryOperators);
|
2017-02-01 17:23:39 +08:00
|
|
|
// Don't do this if there are only two operands. In these cases, there is
|
|
|
|
// always a nice vertical separation between them and the extra line break
|
|
|
|
// does not help.
|
|
|
|
bool HasTwoOperands =
|
|
|
|
P->OperatorIndex == 0 && !P->NextOperator && !P->is(TT_ConditionalExpr);
|
2017-02-02 16:30:21 +08:00
|
|
|
if ((!BreakBeforeOperator && !(HasTwoOperands && Style.AlignOperands)) ||
|
2017-01-16 21:13:15 +08:00
|
|
|
(!State.Stack.back().LastOperatorWrapped && BreakBeforeOperator))
|
|
|
|
State.Stack.back().NoLineBreakInOperand = true;
|
|
|
|
}
|
|
|
|
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Column += Spaces;
|
2014-05-07 17:23:05 +08:00
|
|
|
if (Current.isNot(tok::comment) && Previous.is(tok::l_paren) &&
|
2014-12-12 17:40:58 +08:00
|
|
|
Previous.Previous &&
|
2017-06-19 15:40:49 +08:00
|
|
|
(Previous.Previous->isOneOf(tok::kw_if, tok::kw_for) ||
|
|
|
|
Previous.Previous->endsSequence(tok::kw_constexpr, tok::kw_if))) {
|
2013-10-01 22:41:18 +08:00
|
|
|
// Treat the condition inside an if as if it was a second function
|
2013-10-18 18:38:14 +08:00
|
|
|
// parameter, i.e. let nested calls have a continuation indent.
|
2014-05-07 17:23:05 +08:00
|
|
|
State.Stack.back().LastSpace = State.Column;
|
2014-12-12 17:40:58 +08:00
|
|
|
State.Stack.back().NestedBlockIndent = State.Column;
|
|
|
|
} else if (!Current.isOneOf(tok::comment, tok::caret) &&
|
2016-01-09 23:56:40 +08:00
|
|
|
((Previous.is(tok::comma) &&
|
|
|
|
!Previous.is(TT_OverloadedOperator)) ||
|
2014-12-12 17:40:58 +08:00
|
|
|
(Previous.is(tok::colon) && Previous.is(TT_ObjCMethodExpr)))) {
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().LastSpace = State.Column;
|
2017-05-24 19:36:58 +08:00
|
|
|
} else if (Previous.is(TT_CtorInitializerColon) &&
|
|
|
|
Style.BreakConstructorInitializers ==
|
|
|
|
FormatStyle::BCIS_AfterColon) {
|
|
|
|
State.Stack.back().Indent = State.Column;
|
|
|
|
State.Stack.back().LastSpace = State.Column;
|
2014-12-12 17:40:58 +08:00
|
|
|
} else if ((Previous.isOneOf(TT_BinaryOperator, TT_ConditionalExpr,
|
|
|
|
TT_CtorInitializerColon)) &&
|
|
|
|
((Previous.getPrecedence() != prec::Assignment &&
|
|
|
|
(Previous.isNot(tok::lessless) || Previous.OperatorIndex != 0 ||
|
2016-01-05 21:03:50 +08:00
|
|
|
Previous.NextOperator)) ||
|
2014-12-12 17:40:58 +08:00
|
|
|
Current.StartsBinaryExpression)) {
|
2016-02-11 21:15:14 +08:00
|
|
|
// Indent relative to the RHS of the expression unless this is a simple
|
|
|
|
// assignment without binary expression on the RHS. Also indent relative to
|
|
|
|
// unary operators and the colons of constructor initializers.
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().LastSpace = State.Column;
|
2014-12-12 17:40:58 +08:00
|
|
|
} else if (Previous.is(TT_InheritanceColon)) {
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().Indent = State.Column;
|
2013-10-09 00:24:07 +08:00
|
|
|
State.Stack.back().LastSpace = State.Column;
|
|
|
|
} else if (Previous.opensScope()) {
|
2013-10-01 22:41:18 +08:00
|
|
|
// If a function has a trailing call, indent all parameters from the
|
|
|
|
// opening parenthesis. This avoids confusing indents like:
|
|
|
|
// OuterFunction(InnerFunctionCall( // break
|
|
|
|
// ParameterToInnerFunction)) // break
|
|
|
|
// .SecondInnerFunctionCall();
|
|
|
|
bool HasTrailingCall = false;
|
|
|
|
if (Previous.MatchingParen) {
|
|
|
|
const FormatToken *Next = Previous.MatchingParen->getNextNonComment();
|
|
|
|
HasTrailingCall = Next && Next->isMemberAccess();
|
|
|
|
}
|
2015-02-17 17:58:03 +08:00
|
|
|
if (HasTrailingCall && State.Stack.size() > 1 &&
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack[State.Stack.size() - 2].CallContinuation == 0)
|
|
|
|
State.Stack.back().LastSpace = State.Column;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
|
|
|
|
bool DryRun) {
|
2013-10-12 05:25:45 +08:00
|
|
|
FormatToken &Current = *State.NextToken;
|
2013-10-01 22:41:18 +08:00
|
|
|
const FormatToken &Previous = *State.NextToken->Previous;
|
2014-03-27 22:33:30 +08:00
|
|
|
|
2013-10-01 22:41:18 +08:00
|
|
|
// Extra penalty that needs to be added because of the way certain line
|
|
|
|
// breaks are chosen.
|
|
|
|
unsigned Penalty = 0;
|
|
|
|
|
2014-02-11 18:08:11 +08:00
|
|
|
const FormatToken *PreviousNonComment = Current.getPreviousNonComment();
|
|
|
|
const FormatToken *NextNonComment = Previous.getNextNonComment();
|
|
|
|
if (!NextNonComment)
|
|
|
|
NextNonComment = &Current;
|
2014-05-08 20:21:30 +08:00
|
|
|
// The first line break on any NestingLevel causes an extra penalty in order
|
2013-10-01 22:41:18 +08:00
|
|
|
// prefer similar line breaks.
|
|
|
|
if (!State.Stack.back().ContainsLineBreak)
|
|
|
|
Penalty += 15;
|
|
|
|
State.Stack.back().ContainsLineBreak = true;
|
|
|
|
|
|
|
|
Penalty += State.NextToken->SplitPenalty;
|
|
|
|
|
|
|
|
// Breaking before the first "<<" is generally not desirable if the LHS is
|
2016-12-19 19:14:23 +08:00
|
|
|
// short. Also always add the penalty if the LHS is split over multiple lines
|
2014-04-03 20:00:27 +08:00
|
|
|
// to avoid unnecessary line breaks that just work around this penalty.
|
2014-02-11 18:08:11 +08:00
|
|
|
if (NextNonComment->is(tok::lessless) &&
|
|
|
|
State.Stack.back().FirstLessLess == 0 &&
|
2013-12-20 00:06:40 +08:00
|
|
|
(State.Column <= Style.ColumnLimit / 3 ||
|
2013-11-20 22:54:39 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter))
|
2013-10-01 22:41:18 +08:00
|
|
|
Penalty += Style.PenaltyBreakFirstLessLess;
|
|
|
|
|
2014-03-27 22:33:30 +08:00
|
|
|
State.Column = getNewLineColumn(State);
|
2015-06-18 20:32:59 +08:00
|
|
|
|
|
|
|
// Indent nested blocks relative to this column, unless in a very specific
|
|
|
|
// JavaScript special case where:
|
|
|
|
//
|
|
|
|
// var loooooong_name =
|
|
|
|
// function() {
|
|
|
|
// // code
|
|
|
|
// }
|
|
|
|
//
|
2016-06-13 15:48:45 +08:00
|
|
|
// is common and should be formatted like a free-standing function. The same
|
|
|
|
// goes for wrapping before the lambda return type arrow.
|
|
|
|
if (!Current.is(TT_LambdaArrow) &&
|
|
|
|
(Style.Language != FormatStyle::LK_JavaScript ||
|
|
|
|
Current.NestingLevel != 0 || !PreviousNonComment ||
|
|
|
|
!PreviousNonComment->is(tok::equal) ||
|
|
|
|
!Current.isOneOf(Keywords.kw_async, Keywords.kw_function)))
|
2015-06-18 20:32:59 +08:00
|
|
|
State.Stack.back().NestedBlockIndent = State.Column;
|
|
|
|
|
2014-03-27 22:33:30 +08:00
|
|
|
if (NextNonComment->isMemberAccess()) {
|
|
|
|
if (State.Stack.back().CallContinuation == 0)
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().CallContinuation = State.Column;
|
2014-11-25 18:05:17 +08:00
|
|
|
} else if (NextNonComment->is(TT_SelectorName)) {
|
2013-12-23 15:29:06 +08:00
|
|
|
if (!State.Stack.back().ObjCSelectorNameFound) {
|
2014-02-11 18:08:11 +08:00
|
|
|
if (NextNonComment->LongestObjCSelectorName == 0) {
|
2013-12-23 15:29:06 +08:00
|
|
|
State.Stack.back().AlignColons = false;
|
|
|
|
} else {
|
|
|
|
State.Stack.back().ColonPos =
|
2015-05-13 17:38:25 +08:00
|
|
|
(Style.IndentWrappedFunctionNames
|
|
|
|
? std::max(State.Stack.back().Indent,
|
|
|
|
State.FirstIndent + Style.ContinuationIndentWidth)
|
|
|
|
: State.Stack.back().Indent) +
|
|
|
|
NextNonComment->LongestObjCSelectorName;
|
2013-12-23 15:29:06 +08:00
|
|
|
}
|
2014-03-27 22:33:30 +08:00
|
|
|
} else if (State.Stack.back().AlignColons &&
|
|
|
|
State.Stack.back().ColonPos <= NextNonComment->ColumnWidth) {
|
2014-02-11 18:08:11 +08:00
|
|
|
State.Stack.back().ColonPos = State.Column + NextNonComment->ColumnWidth;
|
2013-08-16 19:20:30 +08:00
|
|
|
}
|
2014-03-17 22:32:47 +08:00
|
|
|
} else if (PreviousNonComment && PreviousNonComment->is(tok::colon) &&
|
2014-11-25 18:05:17 +08:00
|
|
|
PreviousNonComment->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)) {
|
2013-12-23 15:29:06 +08:00
|
|
|
// FIXME: This is hacky, find a better way. The problem is that in an ObjC
|
|
|
|
// method expression, the block should be aligned to the line starting it,
|
|
|
|
// e.g.:
|
|
|
|
// [aaaaaaaaaaaaaaa aaaaaaaaa: \\ break for some reason
|
|
|
|
// ^(int *i) {
|
|
|
|
// // ...
|
|
|
|
// }];
|
2014-05-08 20:21:30 +08:00
|
|
|
// Thus, we set LastSpace of the next higher NestingLevel, to which we move
|
2013-12-23 15:29:06 +08:00
|
|
|
// when we consume all of the "}"'s FakeRParens at the "{".
|
2013-12-23 19:25:40 +08:00
|
|
|
if (State.Stack.size() > 1)
|
2014-03-27 22:33:30 +08:00
|
|
|
State.Stack[State.Stack.size() - 2].LastSpace =
|
|
|
|
std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) +
|
|
|
|
Style.ContinuationIndentWidth;
|
2013-10-01 22:41:18 +08:00
|
|
|
}
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2016-11-29 17:40:01 +08:00
|
|
|
if ((PreviousNonComment &&
|
|
|
|
PreviousNonComment->isOneOf(tok::comma, tok::semi) &&
|
2013-10-01 22:41:18 +08:00
|
|
|
!State.Stack.back().AvoidBinPacking) ||
|
2014-11-25 18:05:17 +08:00
|
|
|
Previous.is(TT_BinaryOperator))
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter = false;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Previous.isOneOf(TT_TemplateCloser, TT_JavaAnnotation) &&
|
2014-11-03 10:27:28 +08:00
|
|
|
Current.NestingLevel == 0)
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter = false;
|
2014-02-11 18:08:11 +08:00
|
|
|
if (NextNonComment->is(tok::question) ||
|
2013-11-08 08:57:11 +08:00
|
|
|
(PreviousNonComment && PreviousNonComment->is(tok::question)))
|
|
|
|
State.Stack.back().BreakBeforeParameter = true;
|
2015-06-03 17:26:03 +08:00
|
|
|
if (Current.is(TT_BinaryOperator) && Current.CanBreakBefore)
|
|
|
|
State.Stack.back().BreakBeforeParameter = false;
|
2013-10-01 22:41:18 +08:00
|
|
|
|
|
|
|
if (!DryRun) {
|
2017-11-18 02:06:33 +08:00
|
|
|
unsigned MaxEmptyLinesToKeep = Style.MaxEmptyLinesToKeep + 1;
|
|
|
|
if (Current.is(tok::r_brace) && Current.MatchingParen &&
|
|
|
|
// Only strip trailing empty lines for l_braces that have children, i.e.
|
|
|
|
// for function expressions (lambdas, arrows, etc).
|
|
|
|
!Current.MatchingParen->Children.empty()) {
|
|
|
|
// lambdas and arrow functions are expressions, thus their r_brace is not
|
|
|
|
// on its own line, and thus not covered by UnwrappedLineFormatter's logic
|
|
|
|
// about removing empty lines on closing blocks. Special case them here.
|
|
|
|
MaxEmptyLinesToKeep = 1;
|
|
|
|
}
|
2014-06-04 20:40:57 +08:00
|
|
|
unsigned Newlines = std::max(
|
2017-11-18 02:06:33 +08:00
|
|
|
1u, std::min(Current.NewlinesBefore, MaxEmptyLinesToKeep));
|
2017-05-19 18:34:57 +08:00
|
|
|
bool ContinuePPDirective =
|
|
|
|
State.Line->InPPDirective && State.Line->Type != LT_ImportStatement;
|
2017-01-31 19:25:01 +08:00
|
|
|
Whitespaces.replaceWhitespace(Current, Newlines, State.Column, State.Column,
|
2017-05-19 18:34:57 +08:00
|
|
|
ContinuePPDirective);
|
2013-10-01 22:41:18 +08:00
|
|
|
}
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2013-10-01 22:41:18 +08:00
|
|
|
if (!Current.isTrailingComment())
|
|
|
|
State.Stack.back().LastSpace = State.Column;
|
2016-02-11 21:15:14 +08:00
|
|
|
if (Current.is(tok::lessless))
|
|
|
|
// If we are breaking before a "<<", we always want to indent relative to
|
|
|
|
// RHS. This is necessary only for "<<", as we special-case it and don't
|
|
|
|
// always indent relative to the RHS.
|
|
|
|
State.Stack.back().LastSpace += 3; // 3 -> width of "<< ".
|
|
|
|
|
2014-05-08 20:21:30 +08:00
|
|
|
State.StartOfLineLevel = Current.NestingLevel;
|
|
|
|
State.LowestLevelOnLine = Current.NestingLevel;
|
2013-10-01 22:41:18 +08:00
|
|
|
|
|
|
|
// Any break on this level means that the parent level has been broken
|
|
|
|
// and we need to avoid bin packing there.
|
2014-11-21 21:38:53 +08:00
|
|
|
bool NestedBlockSpecialCase =
|
2017-03-31 21:30:24 +08:00
|
|
|
!Style.isCpp() && Current.is(tok::r_brace) && State.Stack.size() > 1 &&
|
2014-11-21 21:38:53 +08:00
|
|
|
State.Stack[State.Stack.size() - 2].NestedBlockInlined;
|
2015-06-01 17:56:32 +08:00
|
|
|
if (!NestedBlockSpecialCase)
|
|
|
|
for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i)
|
2014-05-21 20:51:23 +08:00
|
|
|
State.Stack[i].BreakBeforeParameter = true;
|
|
|
|
|
2013-11-09 03:56:28 +08:00
|
|
|
if (PreviousNonComment &&
|
2017-05-24 19:36:58 +08:00
|
|
|
!PreviousNonComment->isOneOf(tok::comma, tok::colon, tok::semi) &&
|
2014-11-25 18:05:17 +08:00
|
|
|
(PreviousNonComment->isNot(TT_TemplateCloser) ||
|
2014-11-14 21:14:45 +08:00
|
|
|
Current.NestingLevel != 0) &&
|
2015-05-18 21:47:23 +08:00
|
|
|
!PreviousNonComment->isOneOf(
|
|
|
|
TT_BinaryOperator, TT_FunctionAnnotationRParen, TT_JavaAnnotation,
|
|
|
|
TT_LeadingJavaAnnotation) &&
|
2014-11-25 18:05:17 +08:00
|
|
|
Current.isNot(TT_BinaryOperator) && !PreviousNonComment->opensScope())
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter = true;
|
|
|
|
|
2013-10-22 23:30:28 +08:00
|
|
|
// If we break after { or the [ of an array initializer, we should also break
|
|
|
|
// before the corresponding } or ].
|
2014-06-10 18:42:26 +08:00
|
|
|
if (PreviousNonComment &&
|
2017-02-20 22:51:16 +08:00
|
|
|
(PreviousNonComment->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
|
clang-format: [JS] simplify template string wrapping.
Summary:
Previously, clang-format would try to wrap template string substitutions
by indenting relative to the openening `${`. This helped with
indenting structured strings, such as strings containing HTML, as the
substitutions would be aligned according to the structure of the string.
However it turns out that the overwhelming majority of template string +
substitution usages are for substitutions into non-structured strings,
e.g. URLs or just plain messages. For these situations, clang-format
would often produce very ugly indents, in particular for strings
containing no line breaks:
return `<a href='http://google3/${file}?l=${row}'>${file}</a>(${
row
},${
col
}): `;
This change makes clang-format indent template string substitutions as
if they were string concatenation operations. It wraps +4 on overlong
lines and keeps all operands on the same line:
return `<a href='http://google3/${file}?l=${row}'>${file}</a>(${
row},${col}): `;
While this breaks some lexical continuity between the `${` and `row}`
here, the overall effects are still a huge improvement, and users can
still manually break the string using `+` if desired.
Reviewers: djasper
Subscribers: klimek, cfe-commits
Differential Revision: https://reviews.llvm.org/D37142
llvm-svn: 311988
2017-08-29 16:30:07 +08:00
|
|
|
opensProtoMessageField(*PreviousNonComment, Style)))
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().BreakBeforeClosingBrace = true;
|
|
|
|
|
|
|
|
if (State.Stack.back().AvoidBinPacking) {
|
|
|
|
// If we are breaking after '(', '{', '<', this is not bin packing
|
2014-05-21 21:26:58 +08:00
|
|
|
// unless AllowAllParametersOfDeclarationOnNextLine is false or this is a
|
|
|
|
// dict/object literal.
|
2014-11-25 18:05:17 +08:00
|
|
|
if (!Previous.isOneOf(tok::l_paren, tok::l_brace, TT_BinaryOperator) ||
|
2013-10-01 22:41:18 +08:00
|
|
|
(!Style.AllowAllParametersOfDeclarationOnNextLine &&
|
2014-05-21 21:26:58 +08:00
|
|
|
State.Line->MustBeDeclaration) ||
|
2014-11-25 18:05:17 +08:00
|
|
|
Previous.is(TT_DictLiteral))
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter = true;
|
2013-08-16 19:20:30 +08:00
|
|
|
}
|
|
|
|
|
2013-10-01 22:41:18 +08:00
|
|
|
return Penalty;
|
2013-08-16 19:20:30 +08:00
|
|
|
}
|
|
|
|
|
2014-03-27 22:33:30 +08:00
|
|
|
unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
|
2014-03-28 00:14:13 +08:00
|
|
|
if (!State.NextToken || !State.NextToken->Previous)
|
|
|
|
return 0;
|
2014-03-27 22:33:30 +08:00
|
|
|
FormatToken &Current = *State.NextToken;
|
2014-10-07 22:45:34 +08:00
|
|
|
const FormatToken &Previous = *Current.Previous;
|
2014-03-27 22:33:30 +08:00
|
|
|
// If we are continuing an expression, we want to use the continuation indent.
|
|
|
|
unsigned ContinuationIndent =
|
|
|
|
std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) +
|
|
|
|
Style.ContinuationIndentWidth;
|
|
|
|
const FormatToken *PreviousNonComment = Current.getPreviousNonComment();
|
|
|
|
const FormatToken *NextNonComment = Previous.getNextNonComment();
|
|
|
|
if (!NextNonComment)
|
|
|
|
NextNonComment = &Current;
|
2014-11-03 03:16:41 +08:00
|
|
|
|
|
|
|
// Java specific bits.
|
2014-11-04 20:41:02 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_Java &&
|
|
|
|
Current.isOneOf(Keywords.kw_implements, Keywords.kw_extends))
|
2014-11-03 03:16:41 +08:00
|
|
|
return std::max(State.Stack.back().LastSpace,
|
|
|
|
State.Stack.back().Indent + Style.ContinuationIndentWidth);
|
|
|
|
|
2014-05-09 21:11:16 +08:00
|
|
|
if (NextNonComment->is(tok::l_brace) && NextNonComment->BlockKind == BK_Block)
|
2014-05-08 20:21:30 +08:00
|
|
|
return Current.NestingLevel == 0 ? State.FirstIndent
|
|
|
|
: State.Stack.back().Indent;
|
2017-06-27 21:43:07 +08:00
|
|
|
if ((Current.isOneOf(tok::r_brace, tok::r_square) ||
|
2017-07-03 23:05:14 +08:00
|
|
|
(Current.is(tok::greater) &&
|
|
|
|
(Style.Language == FormatStyle::LK_Proto ||
|
|
|
|
Style.Language == FormatStyle::LK_TextProto))) &&
|
2017-06-27 21:43:07 +08:00
|
|
|
State.Stack.size() > 1) {
|
2015-10-27 21:42:08 +08:00
|
|
|
if (Current.closesBlockOrBlockTypeList(Style))
|
2014-12-12 17:40:58 +08:00
|
|
|
return State.Stack[State.Stack.size() - 2].NestedBlockIndent;
|
|
|
|
if (Current.MatchingParen &&
|
|
|
|
Current.MatchingParen->BlockKind == BK_BracedInit)
|
2014-03-27 22:33:30 +08:00
|
|
|
return State.Stack[State.Stack.size() - 2].LastSpace;
|
2014-12-11 01:24:34 +08:00
|
|
|
return State.FirstIndent;
|
2014-03-27 22:33:30 +08:00
|
|
|
}
|
2017-05-29 15:50:52 +08:00
|
|
|
// Indent a closing parenthesis at the previous level if followed by a semi or
|
|
|
|
// opening brace. This allows indentations such as:
|
|
|
|
// foo(
|
|
|
|
// a,
|
|
|
|
// );
|
|
|
|
// function foo(
|
|
|
|
// a,
|
|
|
|
// ) {
|
|
|
|
// code(); //
|
|
|
|
// }
|
|
|
|
if (Current.is(tok::r_paren) && State.Stack.size() > 1 &&
|
|
|
|
(!Current.Next || Current.Next->isOneOf(tok::semi, tok::l_brace)))
|
2017-05-15 19:15:29 +08:00
|
|
|
return State.Stack[State.Stack.size() - 2].LastSpace;
|
2017-02-20 22:51:16 +08:00
|
|
|
if (NextNonComment->is(TT_TemplateString) && NextNonComment->closesScope())
|
|
|
|
return State.Stack[State.Stack.size() - 2].LastSpace;
|
2014-04-15 17:54:30 +08:00
|
|
|
if (Current.is(tok::identifier) && Current.Next &&
|
2017-08-03 22:17:29 +08:00
|
|
|
(Current.Next->is(TT_DictLiteral) ||
|
|
|
|
((Style.Language == FormatStyle::LK_Proto ||
|
|
|
|
Style.Language == FormatStyle::LK_TextProto) &&
|
|
|
|
Current.Next->isOneOf(TT_TemplateOpener, tok::l_brace))))
|
2014-04-15 17:54:30 +08:00
|
|
|
return State.Stack.back().Indent;
|
2015-05-17 16:13:23 +08:00
|
|
|
if (NextNonComment->is(TT_ObjCStringLiteral) &&
|
|
|
|
State.StartOfStringLiteral != 0)
|
|
|
|
return State.StartOfStringLiteral - 1;
|
2017-04-11 17:55:00 +08:00
|
|
|
if (NextNonComment->isStringLiteral() && State.StartOfStringLiteral != 0)
|
|
|
|
return State.StartOfStringLiteral;
|
2014-03-27 22:33:30 +08:00
|
|
|
if (NextNonComment->is(tok::lessless) &&
|
|
|
|
State.Stack.back().FirstLessLess != 0)
|
|
|
|
return State.Stack.back().FirstLessLess;
|
|
|
|
if (NextNonComment->isMemberAccess()) {
|
2014-12-11 01:24:34 +08:00
|
|
|
if (State.Stack.back().CallContinuation == 0)
|
2014-03-27 22:33:30 +08:00
|
|
|
return ContinuationIndent;
|
2014-12-11 01:24:34 +08:00
|
|
|
return State.Stack.back().CallContinuation;
|
2014-03-27 22:33:30 +08:00
|
|
|
}
|
|
|
|
if (State.Stack.back().QuestionColumn != 0 &&
|
2014-04-14 19:08:45 +08:00
|
|
|
((NextNonComment->is(tok::colon) &&
|
2014-11-25 18:05:17 +08:00
|
|
|
NextNonComment->is(TT_ConditionalExpr)) ||
|
|
|
|
Previous.is(TT_ConditionalExpr)))
|
2014-03-27 22:33:30 +08:00
|
|
|
return State.Stack.back().QuestionColumn;
|
|
|
|
if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0)
|
|
|
|
return State.Stack.back().VariablePos;
|
2014-11-01 02:23:49 +08:00
|
|
|
if ((PreviousNonComment &&
|
|
|
|
(PreviousNonComment->ClosesTemplateDeclaration ||
|
2015-05-18 21:47:23 +08:00
|
|
|
PreviousNonComment->isOneOf(
|
|
|
|
TT_AttributeParen, TT_FunctionAnnotationRParen, TT_JavaAnnotation,
|
|
|
|
TT_LeadingJavaAnnotation))) ||
|
2014-07-09 16:42:42 +08:00
|
|
|
(!Style.IndentWrappedFunctionNames &&
|
2014-11-25 18:05:17 +08:00
|
|
|
NextNonComment->isOneOf(tok::kw_operator, TT_FunctionDeclarationName)))
|
2014-03-27 22:33:30 +08:00
|
|
|
return std::max(State.Stack.back().LastSpace, State.Stack.back().Indent);
|
2014-11-25 18:05:17 +08:00
|
|
|
if (NextNonComment->is(TT_SelectorName)) {
|
2014-03-27 22:33:30 +08:00
|
|
|
if (!State.Stack.back().ObjCSelectorNameFound) {
|
2014-12-11 01:24:34 +08:00
|
|
|
if (NextNonComment->LongestObjCSelectorName == 0)
|
2014-03-27 22:33:30 +08:00
|
|
|
return State.Stack.back().Indent;
|
2015-05-13 17:38:25 +08:00
|
|
|
return (Style.IndentWrappedFunctionNames
|
|
|
|
? std::max(State.Stack.back().Indent,
|
|
|
|
State.FirstIndent + Style.ContinuationIndentWidth)
|
|
|
|
: State.Stack.back().Indent) +
|
2014-12-11 01:24:34 +08:00
|
|
|
NextNonComment->LongestObjCSelectorName -
|
|
|
|
NextNonComment->ColumnWidth;
|
|
|
|
}
|
|
|
|
if (!State.Stack.back().AlignColons)
|
2014-03-27 22:33:30 +08:00
|
|
|
return State.Stack.back().Indent;
|
2014-12-11 01:24:34 +08:00
|
|
|
if (State.Stack.back().ColonPos > NextNonComment->ColumnWidth)
|
2014-03-27 22:33:30 +08:00
|
|
|
return State.Stack.back().ColonPos - NextNonComment->ColumnWidth;
|
2014-12-11 01:24:34 +08:00
|
|
|
return State.Stack.back().Indent;
|
2014-03-27 22:33:30 +08:00
|
|
|
}
|
2016-11-12 15:38:22 +08:00
|
|
|
if (NextNonComment->is(tok::colon) && NextNonComment->is(TT_ObjCMethodExpr))
|
|
|
|
return State.Stack.back().ColonPos;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (NextNonComment->is(TT_ArraySubscriptLSquare)) {
|
2014-03-27 22:33:30 +08:00
|
|
|
if (State.Stack.back().StartOfArraySubscripts != 0)
|
|
|
|
return State.Stack.back().StartOfArraySubscripts;
|
2014-12-11 01:24:34 +08:00
|
|
|
return ContinuationIndent;
|
2014-03-27 22:33:30 +08:00
|
|
|
}
|
2015-05-07 22:19:59 +08:00
|
|
|
|
|
|
|
// This ensure that we correctly format ObjC methods calls without inputs,
|
|
|
|
// i.e. where the last element isn't selector like: [callee method];
|
|
|
|
if (NextNonComment->is(tok::identifier) && NextNonComment->FakeRParens == 0 &&
|
|
|
|
NextNonComment->Next && NextNonComment->Next->is(TT_ObjCMethodExpr))
|
2015-05-06 20:48:06 +08:00
|
|
|
return State.Stack.back().Indent;
|
2015-05-07 22:19:59 +08:00
|
|
|
|
2015-03-12 23:04:53 +08:00
|
|
|
if (NextNonComment->isOneOf(TT_StartOfName, TT_PointerOrReference) ||
|
2015-07-06 22:07:51 +08:00
|
|
|
Previous.isOneOf(tok::coloncolon, tok::equal, TT_JsTypeColon))
|
2014-03-27 22:33:30 +08:00
|
|
|
return ContinuationIndent;
|
|
|
|
if (PreviousNonComment && PreviousNonComment->is(tok::colon) &&
|
2014-11-25 18:05:17 +08:00
|
|
|
PreviousNonComment->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral))
|
2014-03-27 22:33:30 +08:00
|
|
|
return ContinuationIndent;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (NextNonComment->is(TT_CtorInitializerComma))
|
2014-03-27 22:33:30 +08:00
|
|
|
return State.Stack.back().Indent;
|
2017-05-24 19:36:58 +08:00
|
|
|
if (PreviousNonComment && PreviousNonComment->is(TT_CtorInitializerColon) &&
|
|
|
|
Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon)
|
|
|
|
return State.Stack.back().Indent;
|
2017-03-10 23:10:37 +08:00
|
|
|
if (NextNonComment->isOneOf(TT_CtorInitializerColon, TT_InheritanceColon,
|
|
|
|
TT_InheritanceComma))
|
|
|
|
return State.FirstIndent + Style.ConstructorInitializerIndentWidth;
|
2014-08-06 21:14:58 +08:00
|
|
|
if (Previous.is(tok::r_paren) && !Current.isBinaryOperator() &&
|
2014-11-14 20:31:14 +08:00
|
|
|
!Current.isOneOf(tok::colon, tok::comment))
|
2014-08-06 21:14:58 +08:00
|
|
|
return ContinuationIndent;
|
2014-03-28 00:14:13 +08:00
|
|
|
if (State.Stack.back().Indent == State.FirstIndent && PreviousNonComment &&
|
2014-03-27 22:33:30 +08:00
|
|
|
PreviousNonComment->isNot(tok::r_brace))
|
|
|
|
// Ensure that we fall back to the continuation indent width instead of
|
|
|
|
// just flushing continuations left.
|
|
|
|
return State.Stack.back().Indent + Style.ContinuationIndentWidth;
|
|
|
|
return State.Stack.back().Indent;
|
|
|
|
}
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
|
|
|
|
bool DryRun, bool Newline) {
|
|
|
|
assert(State.Stack.size());
|
2014-05-26 21:10:39 +08:00
|
|
|
const FormatToken &Current = *State.NextToken;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2017-01-16 21:13:15 +08:00
|
|
|
if (Current.isOneOf(tok::comma, TT_BinaryOperator))
|
|
|
|
State.Stack.back().NoLineBreakInOperand = false;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current.is(TT_InheritanceColon))
|
2013-08-16 19:20:30 +08:00
|
|
|
State.Stack.back().AvoidBinPacking = true;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current.is(tok::lessless) && Current.isNot(TT_OverloadedOperator)) {
|
2014-04-14 19:08:45 +08:00
|
|
|
if (State.Stack.back().FirstLessLess == 0)
|
|
|
|
State.Stack.back().FirstLessLess = State.Column;
|
|
|
|
else
|
|
|
|
State.Stack.back().LastOperatorWrapped = Newline;
|
|
|
|
}
|
2017-01-16 21:13:15 +08:00
|
|
|
if (Current.is(TT_BinaryOperator) && Current.isNot(tok::lessless))
|
|
|
|
State.Stack.back().LastOperatorWrapped = Newline;
|
|
|
|
if (Current.is(TT_ConditionalExpr) && Current.Previous &&
|
|
|
|
!Current.Previous->is(TT_ConditionalExpr))
|
2014-04-14 19:08:45 +08:00
|
|
|
State.Stack.back().LastOperatorWrapped = Newline;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current.is(TT_ArraySubscriptLSquare) &&
|
2013-08-16 19:20:30 +08:00
|
|
|
State.Stack.back().StartOfArraySubscripts == 0)
|
|
|
|
State.Stack.back().StartOfArraySubscripts = State.Column;
|
2016-02-04 01:27:10 +08:00
|
|
|
if (Style.BreakBeforeTernaryOperators && Current.is(tok::question))
|
2013-08-16 19:20:30 +08:00
|
|
|
State.Stack.back().QuestionColumn = State.Column;
|
2016-02-04 01:27:10 +08:00
|
|
|
if (!Style.BreakBeforeTernaryOperators && Current.isNot(tok::colon)) {
|
|
|
|
const FormatToken *Previous = Current.Previous;
|
|
|
|
while (Previous && Previous->isTrailingComment())
|
|
|
|
Previous = Previous->Previous;
|
|
|
|
if (Previous && Previous->is(tok::question))
|
|
|
|
State.Stack.back().QuestionColumn = State.Column;
|
|
|
|
}
|
2017-04-24 22:28:49 +08:00
|
|
|
if (!Current.opensScope() && !Current.closesScope() &&
|
|
|
|
!Current.is(TT_PointerOrReference))
|
2013-08-16 19:20:30 +08:00
|
|
|
State.LowestLevelOnLine =
|
2014-05-08 20:21:30 +08:00
|
|
|
std::min(State.LowestLevelOnLine, Current.NestingLevel);
|
clang-format: Format segments of builder-type calls one per line.
This fixes llvm.org/PR14818.
Before:
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:
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);
llvm-svn: 189353
2013-08-27 22:24:43 +08:00
|
|
|
if (Current.isMemberAccess())
|
2013-08-16 19:20:30 +08:00
|
|
|
State.Stack.back().StartOfFunctionCall =
|
2016-01-05 21:03:50 +08:00
|
|
|
!Current.NextOperator ? 0 : State.Column;
|
2015-07-17 06:58:24 +08:00
|
|
|
if (Current.is(TT_SelectorName)) {
|
2013-12-23 15:29:06 +08:00
|
|
|
State.Stack.back().ObjCSelectorNameFound = true;
|
2015-07-17 06:58:24 +08:00
|
|
|
if (Style.IndentWrappedFunctionNames) {
|
|
|
|
State.Stack.back().Indent =
|
|
|
|
State.FirstIndent + Style.ContinuationIndentWidth;
|
|
|
|
}
|
|
|
|
}
|
2017-05-24 19:36:58 +08:00
|
|
|
if (Current.is(TT_CtorInitializerColon) &&
|
|
|
|
Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon) {
|
2013-08-16 19:20:30 +08:00
|
|
|
// Indent 2 from the column, so:
|
|
|
|
// SomeClass::SomeClass()
|
|
|
|
// : First(...), ...
|
|
|
|
// Next(...)
|
|
|
|
// ^ line up here.
|
|
|
|
State.Stack.back().Indent =
|
2017-09-20 17:51:03 +08:00
|
|
|
State.Column +
|
|
|
|
(Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma
|
|
|
|
? 0
|
|
|
|
: 2);
|
2015-01-12 18:23:24 +08:00
|
|
|
State.Stack.back().NestedBlockIndent = State.Stack.back().Indent;
|
2013-08-16 19:20:30 +08:00
|
|
|
if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
|
|
|
|
State.Stack.back().AvoidBinPacking = true;
|
|
|
|
State.Stack.back().BreakBeforeParameter = false;
|
|
|
|
}
|
2017-05-24 19:36:58 +08:00
|
|
|
if (Current.is(TT_CtorInitializerColon) &&
|
|
|
|
Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon) {
|
|
|
|
State.Stack.back().Indent =
|
|
|
|
State.FirstIndent + Style.ConstructorInitializerIndentWidth;
|
|
|
|
State.Stack.back().NestedBlockIndent = State.Stack.back().Indent;
|
|
|
|
if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
|
2017-09-20 17:51:03 +08:00
|
|
|
State.Stack.back().AvoidBinPacking = true;
|
2017-05-24 19:36:58 +08:00
|
|
|
}
|
2017-03-10 23:10:37 +08:00
|
|
|
if (Current.is(TT_InheritanceColon))
|
|
|
|
State.Stack.back().Indent =
|
|
|
|
State.FirstIndent + Style.ContinuationIndentWidth;
|
2015-05-14 00:09:21 +08:00
|
|
|
if (Current.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) && Newline)
|
|
|
|
State.Stack.back().NestedBlockIndent =
|
|
|
|
State.Column + Current.ColumnWidth + 1;
|
2017-02-20 20:43:48 +08:00
|
|
|
if (Current.isOneOf(TT_LambdaLSquare, TT_LambdaArrow))
|
|
|
|
State.Stack.back().LastSpace = State.Column;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
|
|
|
// Insert scopes created by fake parenthesis.
|
|
|
|
const FormatToken *Previous = Current.getPreviousNonComment();
|
2014-05-21 20:51:23 +08:00
|
|
|
|
|
|
|
// Add special behavior to support a format commonly used for JavaScript
|
|
|
|
// closures:
|
|
|
|
// SomeFunction(function() {
|
|
|
|
// foo();
|
|
|
|
// bar();
|
|
|
|
// }, a, b, c);
|
2015-06-15 17:23:17 +08:00
|
|
|
if (Current.isNot(tok::comment) && Previous &&
|
|
|
|
Previous->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) &&
|
2016-01-04 15:27:33 +08:00
|
|
|
!Previous->is(TT_DictLiteral) && State.Stack.size() > 1) {
|
2015-06-01 17:56:32 +08:00
|
|
|
if (State.Stack[State.Stack.size() - 2].NestedBlockInlined && Newline)
|
|
|
|
for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i)
|
2014-11-21 21:38:53 +08:00
|
|
|
State.Stack[i].NoLineBreak = true;
|
|
|
|
State.Stack[State.Stack.size() - 2].NestedBlockInlined = false;
|
|
|
|
}
|
2017-09-20 17:51:03 +08:00
|
|
|
if (Previous &&
|
|
|
|
(Previous->isOneOf(tok::l_paren, tok::comma, tok::colon) ||
|
|
|
|
Previous->isOneOf(TT_BinaryOperator, TT_ConditionalExpr)) &&
|
2014-11-21 21:38:53 +08:00
|
|
|
!Previous->isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
|
|
|
|
State.Stack.back().NestedBlockInlined =
|
|
|
|
!Newline &&
|
2014-12-12 17:40:58 +08:00
|
|
|
(Previous->isNot(tok::l_paren) || Previous->ParameterCount > 1);
|
2014-05-21 20:51:23 +08:00
|
|
|
}
|
|
|
|
|
2014-05-26 21:10:39 +08:00
|
|
|
moveStatePastFakeLParens(State, Newline);
|
|
|
|
moveStatePastScopeCloser(State);
|
2017-11-14 17:19:53 +08:00
|
|
|
bool AllowBreak = !State.Stack.back().NoLineBreak &&
|
|
|
|
!State.Stack.back().NoLineBreakInOperand;
|
2017-02-03 22:32:38 +08:00
|
|
|
moveStatePastScopeOpener(State, Newline);
|
2014-05-26 21:10:39 +08:00
|
|
|
moveStatePastFakeRParens(State);
|
|
|
|
|
2015-05-17 16:13:23 +08:00
|
|
|
if (Current.is(TT_ObjCStringLiteral) && State.StartOfStringLiteral == 0)
|
|
|
|
State.StartOfStringLiteral = State.Column + 1;
|
2017-04-11 17:55:00 +08:00
|
|
|
else if (Current.isStringLiteral() && State.StartOfStringLiteral == 0)
|
|
|
|
State.StartOfStringLiteral = State.Column;
|
2015-05-17 16:13:23 +08:00
|
|
|
else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash) &&
|
2015-06-17 21:08:06 +08:00
|
|
|
!Current.isStringLiteral())
|
2014-05-26 21:10:39 +08:00
|
|
|
State.StartOfStringLiteral = 0;
|
|
|
|
|
|
|
|
State.Column += Current.ColumnWidth;
|
|
|
|
State.NextToken = State.NextToken->Next;
|
2017-11-14 17:19:53 +08:00
|
|
|
|
|
|
|
unsigned Penalty =
|
|
|
|
handleEndOfLine(Current, State, DryRun, AllowBreak);
|
2014-05-26 21:10:39 +08:00
|
|
|
|
|
|
|
if (Current.Role)
|
|
|
|
Current.Role->formatFromToken(State, this, DryRun);
|
|
|
|
// If the previous has a special role, let it consume tokens as appropriate.
|
|
|
|
// It is necessary to start at the previous token for the only implemented
|
|
|
|
// role (comma separated list). That way, the decision whether or not to break
|
|
|
|
// after the "{" is already done and both options are tried and evaluated.
|
|
|
|
// FIXME: This is ugly, find a better way.
|
|
|
|
if (Previous && Previous->Role)
|
|
|
|
Penalty += Previous->Role->formatAfterToken(State, this, DryRun);
|
|
|
|
|
|
|
|
return Penalty;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
|
|
|
|
bool Newline) {
|
|
|
|
const FormatToken &Current = *State.NextToken;
|
|
|
|
const FormatToken *Previous = Current.getPreviousNonComment();
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
// Don't add extra indentation for the first fake parenthesis after
|
2014-05-02 01:19:34 +08:00
|
|
|
// 'return', assignments or opening <({[. The indentation for these cases
|
2013-08-16 19:20:30 +08:00
|
|
|
// is special cased.
|
|
|
|
bool SkipFirstExtraIndent =
|
2015-03-06 18:57:12 +08:00
|
|
|
(Previous && (Previous->opensScope() ||
|
|
|
|
Previous->isOneOf(tok::semi, tok::kw_return) ||
|
2014-12-02 21:24:51 +08:00
|
|
|
(Previous->getPrecedence() == prec::Assignment &&
|
|
|
|
Style.AlignOperands) ||
|
2014-11-25 18:05:17 +08:00
|
|
|
Previous->is(TT_ObjCMethodExpr)));
|
2013-08-16 19:20:30 +08:00
|
|
|
for (SmallVectorImpl<prec::Level>::const_reverse_iterator
|
|
|
|
I = Current.FakeLParens.rbegin(),
|
|
|
|
E = Current.FakeLParens.rend();
|
|
|
|
I != E; ++I) {
|
|
|
|
ParenState NewParenState = State.Stack.back();
|
|
|
|
NewParenState.ContainsLineBreak = false;
|
2017-03-16 15:54:11 +08:00
|
|
|
NewParenState.LastOperatorWrapped = true;
|
2017-01-16 21:13:15 +08:00
|
|
|
NewParenState.NoLineBreak =
|
|
|
|
NewParenState.NoLineBreak || State.Stack.back().NoLineBreakInOperand;
|
2013-09-30 16:29:03 +08:00
|
|
|
|
2017-05-08 23:07:52 +08:00
|
|
|
// Don't propagate AvoidBinPacking into subexpressions of arg/param lists.
|
|
|
|
if (*I > prec::Comma)
|
|
|
|
NewParenState.AvoidBinPacking = false;
|
|
|
|
|
2014-11-19 07:55:27 +08:00
|
|
|
// Indent from 'LastSpace' unless these are fake parentheses encapsulating
|
|
|
|
// a builder type call after 'return' or, if the alignment after opening
|
|
|
|
// brackets is disabled.
|
2014-10-07 22:45:34 +08:00
|
|
|
if (!Current.isTrailingComment() &&
|
2014-12-02 21:24:51 +08:00
|
|
|
(Style.AlignOperands || *I < prec::Assignment) &&
|
2014-11-20 17:54:49 +08:00
|
|
|
(!Previous || Previous->isNot(tok::kw_return) ||
|
|
|
|
(Style.Language != FormatStyle::LK_Java && *I > 0)) &&
|
2015-10-27 20:38:37 +08:00
|
|
|
(Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign ||
|
|
|
|
*I != prec::Comma || Current.NestingLevel == 0))
|
2013-09-30 16:29:03 +08:00
|
|
|
NewParenState.Indent =
|
|
|
|
std::max(std::max(State.Column, NewParenState.Indent),
|
|
|
|
State.Stack.back().LastSpace);
|
|
|
|
|
2013-09-05 17:29:45 +08:00
|
|
|
// Do not indent relative to the fake parentheses inserted for "." or "->".
|
|
|
|
// This is a special case to make the following to statements consistent:
|
|
|
|
// OuterFunction(InnerFunctionCall( // break
|
|
|
|
// ParameterToInnerFunction));
|
|
|
|
// OuterFunction(SomeObject.InnerFunctionCall( // break
|
|
|
|
// ParameterToInnerFunction));
|
|
|
|
if (*I > prec::Unknown)
|
|
|
|
NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column);
|
2016-02-08 17:52:54 +08:00
|
|
|
if (*I != prec::Conditional && !Current.is(TT_UnaryOperator) &&
|
|
|
|
Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign)
|
2014-12-09 05:28:31 +08:00
|
|
|
NewParenState.StartOfFunctionCall = State.Column;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
|
|
|
// Always indent conditional expressions. Never indent expression where
|
|
|
|
// the 'operator' is ',', ';' or an assignment (i.e. *I <=
|
|
|
|
// prec::Assignment) as those have different indentation rules. Indent
|
|
|
|
// other expression, unless the indentation needs to be skipped.
|
|
|
|
if (*I == prec::Conditional ||
|
|
|
|
(!SkipFirstExtraIndent && *I > prec::Assignment &&
|
2014-12-02 17:46:56 +08:00
|
|
|
!Current.isTrailingComment()))
|
2013-10-18 18:38:14 +08:00
|
|
|
NewParenState.Indent += Style.ContinuationIndentWidth;
|
2016-01-08 02:11:54 +08:00
|
|
|
if ((Previous && !Previous->opensScope()) || *I != prec::Comma)
|
2013-08-16 19:20:30 +08:00
|
|
|
NewParenState.BreakBeforeParameter = false;
|
|
|
|
State.Stack.push_back(NewParenState);
|
|
|
|
SkipFirstExtraIndent = false;
|
|
|
|
}
|
2014-05-26 21:10:39 +08:00
|
|
|
}
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2014-12-12 17:40:58 +08:00
|
|
|
void ContinuationIndenter::moveStatePastFakeRParens(LineState &State) {
|
|
|
|
for (unsigned i = 0, e = State.NextToken->FakeRParens; i != e; ++i) {
|
2014-05-28 17:11:53 +08:00
|
|
|
unsigned VariablePos = State.Stack.back().VariablePos;
|
|
|
|
if (State.Stack.size() == 1) {
|
|
|
|
// Do not pop the last element.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
State.Stack.pop_back();
|
|
|
|
State.Stack.back().VariablePos = VariablePos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-26 21:10:39 +08:00
|
|
|
void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
|
|
|
|
bool Newline) {
|
|
|
|
const FormatToken &Current = *State.NextToken;
|
|
|
|
if (!Current.opensScope())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Current.MatchingParen && Current.BlockKind == BK_Block) {
|
|
|
|
moveStateToNewBlock(State);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned NewIndent;
|
2015-05-04 15:39:00 +08:00
|
|
|
unsigned LastSpace = State.Stack.back().LastSpace;
|
2014-05-26 21:10:39 +08:00
|
|
|
bool AvoidBinPacking;
|
|
|
|
bool BreakBeforeParameter = false;
|
2015-07-14 19:26:14 +08:00
|
|
|
unsigned NestedBlockIndent = std::max(State.Stack.back().StartOfFunctionCall,
|
|
|
|
State.Stack.back().NestedBlockIndent);
|
2017-06-27 21:43:07 +08:00
|
|
|
if (Current.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
|
2017-07-03 23:05:14 +08:00
|
|
|
opensProtoMessageField(Current, Style)) {
|
2015-10-27 21:42:08 +08:00
|
|
|
if (Current.opensBlockOrBlockTypeList(Style)) {
|
2017-06-06 20:38:29 +08:00
|
|
|
NewIndent = Style.IndentWidth +
|
|
|
|
std::min(State.Column, State.Stack.back().NestedBlockIndent);
|
2014-05-26 21:10:39 +08:00
|
|
|
} else {
|
2014-12-12 17:40:58 +08:00
|
|
|
NewIndent = State.Stack.back().LastSpace + Style.ContinuationIndentWidth;
|
2014-05-26 21:10:39 +08:00
|
|
|
}
|
|
|
|
const FormatToken *NextNoComment = Current.getNextNonComment();
|
clang-format: [ObjC+JS] Allow bin-packing of array literals.
After reading the style guides again, they don't actually say how to
pack or not pack array literals. Based on some user reports, array
initializers can unnecessarily get quite long if they contain many
small elements. Array literals with trailing commas are still formatted
one per line so that users have a way to opt out of the packing.
Before:
var array = [
aaaaaa,
aaaaaa,
aaaaaa,
aaaaaa,
aaaaaa,
aaaaaa,
aaaaaa,
aaaaaa,
aaaaaa,
aaaaaa
];
After:
var array = [
aaaaaa, aaaaaa, aaaaaa, aaaaaa, aaaaaa, aaaaaa, aaaaaa, aaaaaa, aaaaaa,
aaaaaa, aaaaaa
];
llvm-svn: 257615
2016-01-14 00:41:34 +08:00
|
|
|
bool EndsInComma = Current.MatchingParen &&
|
|
|
|
Current.MatchingParen->Previous &&
|
|
|
|
Current.MatchingParen->Previous->is(tok::comma);
|
2017-09-20 17:51:03 +08:00
|
|
|
AvoidBinPacking = EndsInComma || Current.is(TT_DictLiteral) ||
|
|
|
|
Style.Language == FormatStyle::LK_Proto ||
|
|
|
|
Style.Language == FormatStyle::LK_TextProto ||
|
|
|
|
!Style.BinPackArguments ||
|
|
|
|
(NextNoComment &&
|
|
|
|
NextNoComment->isOneOf(TT_DesignatedInitializerPeriod,
|
|
|
|
TT_DesignatedInitializerLSquare));
|
clang-format: Do not binpack initialization lists
Summary:
This patch tries to avoid binpacking when initializing lists/arrays, to allow things like:
static int types[] = {
registerType1(),
registerType2(),
registerType3(),
};
std::map<int, std::string> x = {
{ 0, "foo fjakfjaklf kljj" },
{ 1, "bar fjakfjaklf kljj" },
{ 2, "stuff fjakfjaklf kljj" },
};
This is similar to how dictionnaries are formatted, and actually corresponds to the same conditions: when initializing a container (and not just 'calling' a constructor).
Such formatting involves 2 things:
* Line breaks around the content of the block. This can be forced by adding a comma or comment after the last element
* Elements should not be binpacked
This patch considers the block is an initializer list if it either ends with a comma, or follows an assignment, which seems to provide a sensible approximation.
Reviewers: krasimir, djasper
Reviewed By: djasper
Subscribers: malcolm.parsons, klimek, cfe-commits
Differential Revision: https://reviews.llvm.org/D34238
llvm-svn: 306868
2017-07-01 04:00:02 +08:00
|
|
|
BreakBeforeParameter = EndsInComma;
|
2015-07-14 19:26:14 +08:00
|
|
|
if (Current.ParameterCount > 1)
|
|
|
|
NestedBlockIndent = std::max(NestedBlockIndent, State.Column + 1);
|
2014-05-26 21:10:39 +08:00
|
|
|
} else {
|
|
|
|
NewIndent = Style.ContinuationIndentWidth +
|
|
|
|
std::max(State.Stack.back().LastSpace,
|
|
|
|
State.Stack.back().StartOfFunctionCall);
|
2015-05-04 15:39:00 +08:00
|
|
|
|
|
|
|
// Ensure that different different brackets force relative alignment, e.g.:
|
|
|
|
// void SomeFunction(vector< // break
|
|
|
|
// int> v);
|
|
|
|
// FIXME: We likely want to do this for more combinations of brackets.
|
|
|
|
// Verify that it is wanted for ObjC, too.
|
2017-01-31 22:39:33 +08:00
|
|
|
if (Current.is(tok::less) && Current.ParentBracket == tok::l_paren) {
|
2015-05-04 15:39:00 +08:00
|
|
|
NewIndent = std::max(NewIndent, State.Stack.back().Indent);
|
|
|
|
LastSpace = std::max(LastSpace, State.Stack.back().Indent);
|
|
|
|
}
|
|
|
|
|
2017-05-15 19:15:29 +08:00
|
|
|
bool EndsInComma =
|
|
|
|
Current.MatchingParen &&
|
|
|
|
Current.MatchingParen->getPreviousNonComment() &&
|
|
|
|
Current.MatchingParen->getPreviousNonComment()->is(tok::comma);
|
|
|
|
|
2014-10-09 17:52:05 +08:00
|
|
|
AvoidBinPacking =
|
2017-05-15 19:15:29 +08:00
|
|
|
(Style.Language == FormatStyle::LK_JavaScript && EndsInComma) ||
|
2014-10-09 17:52:05 +08:00
|
|
|
(State.Line->MustBeDeclaration && !Style.BinPackParameters) ||
|
|
|
|
(!State.Line->MustBeDeclaration && !Style.BinPackArguments) ||
|
|
|
|
(Style.ExperimentalAutoDetectBinPacking &&
|
|
|
|
(Current.PackingKind == PPK_OnePerLine ||
|
|
|
|
(!BinPackInconclusiveFunctions &&
|
|
|
|
Current.PackingKind == PPK_Inconclusive)));
|
2017-05-15 19:15:29 +08:00
|
|
|
|
2015-04-23 17:23:17 +08:00
|
|
|
if (Current.is(TT_ObjCMethodExpr) && Current.MatchingParen) {
|
|
|
|
if (Style.ColumnLimit) {
|
|
|
|
// If this '[' opens an ObjC call, determine whether all parameters fit
|
|
|
|
// into one line and put one per line if they don't.
|
|
|
|
if (getLengthToMatchingParen(Current) + State.Column >
|
2014-05-26 21:10:39 +08:00
|
|
|
getColumnLimit(State))
|
2015-04-23 17:23:17 +08:00
|
|
|
BreakBeforeParameter = true;
|
|
|
|
} else {
|
|
|
|
// For ColumnLimit = 0, we have to figure out whether there is or has to
|
|
|
|
// be a line break within this call.
|
|
|
|
for (const FormatToken *Tok = &Current;
|
|
|
|
Tok && Tok != Current.MatchingParen; Tok = Tok->Next) {
|
2015-06-17 21:08:06 +08:00
|
|
|
if (Tok->MustBreakBefore ||
|
2015-04-23 17:23:17 +08:00
|
|
|
(Tok->CanBreakBefore && Tok->NewlinesBefore > 0)) {
|
|
|
|
BreakBeforeParameter = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-15 19:15:29 +08:00
|
|
|
|
|
|
|
if (Style.Language == FormatStyle::LK_JavaScript && EndsInComma)
|
|
|
|
BreakBeforeParameter = true;
|
2013-08-16 19:20:30 +08:00
|
|
|
}
|
2015-10-27 21:42:08 +08:00
|
|
|
// Generally inherit NoLineBreak from the current scope to nested scope.
|
|
|
|
// However, don't do this for non-empty nested blocks, dict literals and
|
|
|
|
// array literals as these follow different indentation rules.
|
|
|
|
bool NoLineBreak =
|
|
|
|
Current.Children.empty() &&
|
|
|
|
!Current.isOneOf(TT_DictLiteral, TT_ArrayInitializerLSquare) &&
|
|
|
|
(State.Stack.back().NoLineBreak ||
|
2017-01-16 21:13:15 +08:00
|
|
|
State.Stack.back().NoLineBreakInOperand ||
|
2015-10-27 21:42:08 +08:00
|
|
|
(Current.is(TT_TemplateOpener) &&
|
2017-01-16 21:13:15 +08:00
|
|
|
State.Stack.back().ContainsUnwrappedBuilder));
|
2017-01-31 19:25:01 +08:00
|
|
|
State.Stack.push_back(
|
|
|
|
ParenState(NewIndent, LastSpace, AvoidBinPacking, NoLineBreak));
|
2014-12-12 17:40:58 +08:00
|
|
|
State.Stack.back().NestedBlockIndent = NestedBlockIndent;
|
2014-05-26 21:10:39 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter = BreakBeforeParameter;
|
2014-06-03 20:02:45 +08:00
|
|
|
State.Stack.back().HasMultipleNestedBlocks = Current.BlockParameterCount > 1;
|
2014-05-26 21:10:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ContinuationIndenter::moveStatePastScopeCloser(LineState &State) {
|
|
|
|
const FormatToken &Current = *State.NextToken;
|
|
|
|
if (!Current.closesScope())
|
|
|
|
return;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
|
|
|
// If we encounter a closing ), ], } or >, we can remove a level from our
|
|
|
|
// stacks.
|
2013-08-28 17:17:37 +08:00
|
|
|
if (State.Stack.size() > 1 &&
|
2017-02-03 22:32:38 +08:00
|
|
|
(Current.isOneOf(tok::r_paren, tok::r_square, TT_TemplateString) ||
|
2013-09-05 17:29:45 +08:00
|
|
|
(Current.is(tok::r_brace) && State.NextToken != State.Line->First) ||
|
2014-11-25 18:05:17 +08:00
|
|
|
State.NextToken->is(TT_TemplateCloser)))
|
2013-08-16 19:20:30 +08:00
|
|
|
State.Stack.pop_back();
|
2014-05-28 17:11:53 +08:00
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
if (Current.is(tok::r_square)) {
|
|
|
|
// If this ends the array subscript expr, reset the corresponding value.
|
|
|
|
const FormatToken *NextNonComment = Current.getNextNonComment();
|
|
|
|
if (NextNonComment && NextNonComment->isNot(tok::l_square))
|
|
|
|
State.Stack.back().StartOfArraySubscripts = 0;
|
|
|
|
}
|
2014-05-26 21:10:39 +08:00
|
|
|
}
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2014-05-26 21:10:39 +08:00
|
|
|
void ContinuationIndenter::moveStateToNewBlock(LineState &State) {
|
2014-12-12 17:40:58 +08:00
|
|
|
unsigned NestedBlockIndent = State.Stack.back().NestedBlockIndent;
|
2014-10-29 00:53:38 +08:00
|
|
|
// ObjC block sometimes follow special indentation rules.
|
2014-11-25 18:05:17 +08:00
|
|
|
unsigned NewIndent =
|
2014-12-12 17:40:58 +08:00
|
|
|
NestedBlockIndent + (State.NextToken->is(TT_ObjCBlockLBrace)
|
|
|
|
? Style.ObjCBlockIndentWidth
|
|
|
|
: Style.IndentWidth);
|
2017-01-31 19:25:01 +08:00
|
|
|
State.Stack.push_back(ParenState(NewIndent, State.Stack.back().LastSpace,
|
|
|
|
/*AvoidBinPacking=*/true,
|
|
|
|
/*NoLineBreak=*/false));
|
2014-12-12 17:40:58 +08:00
|
|
|
State.Stack.back().NestedBlockIndent = NestedBlockIndent;
|
2014-05-26 21:10:39 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter = true;
|
2013-08-16 19:20:30 +08:00
|
|
|
}
|
|
|
|
|
2017-10-30 22:01:50 +08:00
|
|
|
static unsigned getLastLineEndColumn(StringRef Text, unsigned StartColumn,
|
|
|
|
unsigned TabWidth,
|
|
|
|
encoding::Encoding Encoding) {
|
|
|
|
size_t LastNewlinePos = Text.find_last_of("\n");
|
|
|
|
if (LastNewlinePos == StringRef::npos) {
|
|
|
|
return StartColumn +
|
|
|
|
encoding::columnWidthWithTabs(Text, StartColumn, TabWidth, Encoding);
|
|
|
|
} else {
|
|
|
|
return encoding::columnWidthWithTabs(Text.substr(LastNewlinePos),
|
|
|
|
/*StartColumn=*/0, TabWidth, Encoding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned ContinuationIndenter::reformatRawStringLiteral(
|
2017-11-14 17:19:53 +08:00
|
|
|
const FormatToken &Current, LineState &State,
|
|
|
|
const FormatStyle &RawStringStyle, bool DryRun) {
|
|
|
|
unsigned StartColumn = State.Column - Current.ColumnWidth;
|
|
|
|
auto Delimiter = *getRawStringDelimiter(Current.TokenText);
|
2017-10-30 22:01:50 +08:00
|
|
|
// The text of a raw string is between the leading 'R"delimiter(' and the
|
|
|
|
// trailing 'delimiter)"'.
|
|
|
|
unsigned PrefixSize = 3 + Delimiter.size();
|
|
|
|
unsigned SuffixSize = 2 + Delimiter.size();
|
|
|
|
|
|
|
|
// The first start column is the column the raw text starts.
|
|
|
|
unsigned FirstStartColumn = StartColumn + PrefixSize;
|
|
|
|
|
|
|
|
// The next start column is the intended indentation a line break inside
|
|
|
|
// the raw string at level 0. It is determined by the following rules:
|
|
|
|
// - if the content starts on newline, it is one level more than the current
|
|
|
|
// indent, and
|
|
|
|
// - if the content does not start on a newline, it is the first start
|
|
|
|
// column.
|
|
|
|
// These rules have the advantage that the formatted content both does not
|
|
|
|
// violate the rectangle rule and visually flows within the surrounding
|
|
|
|
// source.
|
|
|
|
bool ContentStartsOnNewline = Current.TokenText[PrefixSize] == '\n';
|
|
|
|
unsigned NextStartColumn = ContentStartsOnNewline
|
|
|
|
? State.Stack.back().Indent + Style.IndentWidth
|
|
|
|
: FirstStartColumn;
|
|
|
|
|
|
|
|
// The last start column is the column the raw string suffix starts if it is
|
|
|
|
// put on a newline.
|
|
|
|
// The last start column is the intended indentation of the raw string postfix
|
|
|
|
// if it is put on a newline. It is determined by the following rules:
|
|
|
|
// - if the raw string prefix starts on a newline, it is the column where
|
|
|
|
// that raw string prefix starts, and
|
|
|
|
// - if the raw string prefix does not start on a newline, it is the current
|
|
|
|
// indent.
|
|
|
|
unsigned LastStartColumn = Current.NewlinesBefore
|
|
|
|
? FirstStartColumn - PrefixSize
|
|
|
|
: State.Stack.back().Indent;
|
|
|
|
|
|
|
|
std::string RawText =
|
|
|
|
Current.TokenText.substr(PrefixSize).drop_back(SuffixSize);
|
|
|
|
|
|
|
|
std::pair<tooling::Replacements, unsigned> Fixes = internal::reformat(
|
|
|
|
RawStringStyle, RawText, {tooling::Range(0, RawText.size())},
|
|
|
|
FirstStartColumn, NextStartColumn, LastStartColumn, "<stdin>",
|
2017-11-09 21:19:14 +08:00
|
|
|
/*Status=*/nullptr);
|
2017-10-30 22:01:50 +08:00
|
|
|
|
|
|
|
auto NewCode = applyAllReplacements(RawText, Fixes.first);
|
|
|
|
tooling::Replacements NoFixes;
|
|
|
|
if (!NewCode) {
|
|
|
|
State.Column += Current.ColumnWidth;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!DryRun) {
|
|
|
|
SourceLocation OriginLoc =
|
|
|
|
Current.Tok.getLocation().getLocWithOffset(PrefixSize);
|
|
|
|
for (const tooling::Replacement &Fix : Fixes.first) {
|
|
|
|
auto Err = Whitespaces.addReplacement(tooling::Replacement(
|
|
|
|
SourceMgr, OriginLoc.getLocWithOffset(Fix.getOffset()),
|
|
|
|
Fix.getLength(), Fix.getReplacementText()));
|
|
|
|
if (Err) {
|
|
|
|
llvm::errs() << "Failed to reformat raw string: "
|
|
|
|
<< llvm::toString(std::move(Err)) << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsigned RawLastLineEndColumn = getLastLineEndColumn(
|
|
|
|
*NewCode, FirstStartColumn, Style.TabWidth, Encoding);
|
|
|
|
State.Column = RawLastLineEndColumn + SuffixSize;
|
|
|
|
return Fixes.second;
|
|
|
|
}
|
|
|
|
|
2013-09-10 20:29:48 +08:00
|
|
|
unsigned ContinuationIndenter::addMultilineToken(const FormatToken &Current,
|
|
|
|
LineState &State) {
|
2013-08-30 01:32:57 +08:00
|
|
|
// Break before further function parameters on all levels.
|
|
|
|
for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
|
|
|
|
State.Stack[i].BreakBeforeParameter = true;
|
|
|
|
|
2013-09-10 17:38:25 +08:00
|
|
|
unsigned ColumnsUsed = State.Column;
|
2013-09-02 21:58:14 +08:00
|
|
|
// We can only affect layout of the first and the last line, so the penalty
|
|
|
|
// for all other lines is constant, and we ignore it.
|
2013-09-05 22:08:34 +08:00
|
|
|
State.Column = Current.LastLineColumnWidth;
|
2013-09-02 21:58:14 +08:00
|
|
|
|
2013-09-05 17:29:45 +08:00
|
|
|
if (ColumnsUsed > getColumnLimit(State))
|
|
|
|
return Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit(State));
|
2013-08-30 01:32:57 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-14 17:19:53 +08:00
|
|
|
unsigned ContinuationIndenter::handleEndOfLine(const FormatToken &Current,
|
|
|
|
LineState &State, bool DryRun,
|
|
|
|
bool AllowBreak) {
|
|
|
|
unsigned Penalty = 0;
|
2017-10-30 22:01:50 +08:00
|
|
|
// Compute the raw string style to use in case this is a raw string literal
|
|
|
|
// that can be reformatted.
|
2017-11-14 17:19:53 +08:00
|
|
|
auto RawStringStyle = getRawStringStyle(Current, State);
|
|
|
|
if (RawStringStyle) {
|
|
|
|
Penalty = reformatRawStringLiteral(Current, State, *RawStringStyle, DryRun);
|
|
|
|
} else if (Current.IsMultiline && Current.isNot(TT_BlockComment)) {
|
|
|
|
// Don't break multi-line tokens other than block comments and raw string
|
|
|
|
// literals. Instead, just update the state.
|
|
|
|
Penalty = addMultilineToken(Current, State);
|
|
|
|
} else if (State.Line->Type != LT_ImportStatement) {
|
|
|
|
// We generally don't break import statements.
|
|
|
|
Penalty = breakProtrudingToken(Current, State, AllowBreak, DryRun);
|
|
|
|
}
|
|
|
|
if (State.Column > getColumnLimit(State)) {
|
|
|
|
unsigned ExcessCharacters = State.Column - getColumnLimit(State);
|
|
|
|
Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
|
|
|
|
}
|
|
|
|
return Penalty;
|
|
|
|
}
|
2013-10-30 21:54:53 +08:00
|
|
|
|
2017-11-14 17:19:53 +08:00
|
|
|
llvm::Optional<FormatStyle>
|
|
|
|
ContinuationIndenter::getRawStringStyle(const FormatToken &Current,
|
|
|
|
const LineState &State) {
|
|
|
|
if (!Current.isStringLiteral())
|
|
|
|
return None;
|
|
|
|
auto Delimiter = getRawStringDelimiter(Current.TokenText);
|
|
|
|
if (!Delimiter)
|
|
|
|
return None;
|
|
|
|
auto RawStringStyle = RawStringFormats.get(*Delimiter);
|
|
|
|
if (!RawStringStyle)
|
|
|
|
return None;
|
|
|
|
RawStringStyle->ColumnLimit = getColumnLimit(State);
|
|
|
|
return RawStringStyle;
|
|
|
|
}
|
2013-08-23 18:05:49 +08:00
|
|
|
|
2017-11-14 17:19:53 +08:00
|
|
|
std::unique_ptr<BreakableToken> ContinuationIndenter::createBreakableToken(
|
|
|
|
const FormatToken &Current, LineState &State, bool AllowBreak) {
|
2013-09-10 17:38:25 +08:00
|
|
|
unsigned StartColumn = State.Column - Current.ColumnWidth;
|
2013-12-20 14:22:01 +08:00
|
|
|
if (Current.isStringLiteral()) {
|
2015-01-04 17:11:17 +08:00
|
|
|
// FIXME: String literal breaking is currently disabled for Java and JS, as
|
|
|
|
// it requires strings to be merged using "+" which we don't support.
|
|
|
|
if (Style.Language == FormatStyle::LK_Java ||
|
2016-02-01 19:21:02 +08:00
|
|
|
Style.Language == FormatStyle::LK_JavaScript ||
|
2017-11-14 17:19:53 +08:00
|
|
|
!Style.BreakStringLiterals ||
|
|
|
|
!AllowBreak)
|
|
|
|
return nullptr;
|
2015-01-04 17:11:17 +08:00
|
|
|
|
2013-10-12 05:43:05 +08:00
|
|
|
// Don't break string literals inside preprocessor directives (except for
|
|
|
|
// #define directives, as their contents are stored in separate lines and
|
|
|
|
// are not affected by this check).
|
|
|
|
// This way we avoid breaking code with line directives and unknown
|
|
|
|
// preprocessor directives that contain long string literals.
|
|
|
|
if (State.Line->Type == LT_PreprocessorDirective)
|
2017-11-14 17:19:53 +08:00
|
|
|
return nullptr;
|
2013-08-16 19:20:30 +08:00
|
|
|
// Exempts unterminated string literals from line breaking. The user will
|
|
|
|
// likely want to terminate the string before any line breaking is done.
|
|
|
|
if (Current.IsUnterminatedLiteral)
|
2017-11-14 17:19:53 +08:00
|
|
|
return nullptr;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2013-09-17 04:20:49 +08:00
|
|
|
StringRef Text = Current.TokenText;
|
|
|
|
StringRef Prefix;
|
|
|
|
StringRef Postfix;
|
|
|
|
// FIXME: Handle whitespace between '_T', '(', '"..."', and ')'.
|
|
|
|
// FIXME: Store Prefix and Suffix (or PrefixLength and SuffixLength to
|
|
|
|
// reduce the overhead) for each FormatToken, which is a string, so that we
|
|
|
|
// don't run multiple checks here on the hot path.
|
|
|
|
if ((Text.endswith(Postfix = "\"") &&
|
2017-04-11 17:55:00 +08:00
|
|
|
(Text.startswith(Prefix = "@\"") || Text.startswith(Prefix = "\"") ||
|
2014-01-09 22:18:12 +08:00
|
|
|
Text.startswith(Prefix = "u\"") || Text.startswith(Prefix = "U\"") ||
|
|
|
|
Text.startswith(Prefix = "u8\"") ||
|
2013-09-17 04:20:49 +08:00
|
|
|
Text.startswith(Prefix = "L\""))) ||
|
2014-12-15 04:47:11 +08:00
|
|
|
(Text.startswith(Prefix = "_T(\"") && Text.endswith(Postfix = "\")"))) {
|
2017-11-14 17:19:53 +08:00
|
|
|
return llvm::make_unique<BreakableStringLiteral>(
|
|
|
|
Current, StartColumn, Prefix, Postfix, State.Line->InPPDirective,
|
|
|
|
Encoding, Style);
|
2013-09-17 04:20:49 +08:00
|
|
|
}
|
2016-06-08 17:45:08 +08:00
|
|
|
} else if (Current.is(TT_BlockComment)) {
|
2017-10-16 17:08:53 +08:00
|
|
|
if (!Style.ReflowComments ||
|
2017-01-25 21:58:58 +08:00
|
|
|
// If a comment token switches formatting, like
|
|
|
|
// /* clang-format on */, we don't want to break it further,
|
|
|
|
// but we may still want to adjust its indentation.
|
2017-11-14 17:19:53 +08:00
|
|
|
switchesFormatting(Current)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return llvm::make_unique<BreakableBlockComment>(
|
2017-01-31 19:25:01 +08:00
|
|
|
Current, StartColumn, Current.OriginalColumn, !Current.Previous,
|
2017-11-14 17:19:53 +08:00
|
|
|
State.Line->InPPDirective, Encoding, Style);
|
2014-11-25 18:05:17 +08:00
|
|
|
} else if (Current.is(TT_LineComment) &&
|
2014-05-09 16:15:10 +08:00
|
|
|
(Current.Previous == nullptr ||
|
2014-11-25 18:05:17 +08:00
|
|
|
Current.Previous->isNot(TT_ImplicitStringLiteral))) {
|
2015-12-01 21:28:53 +08:00
|
|
|
if (!Style.ReflowComments ||
|
2017-01-25 21:58:58 +08:00
|
|
|
CommentPragmasRegex.match(Current.TokenText.substr(2)) ||
|
|
|
|
switchesFormatting(Current))
|
2017-11-14 17:19:53 +08:00
|
|
|
return nullptr;
|
|
|
|
return llvm::make_unique<BreakableLineCommentSection>(
|
2017-01-31 19:25:01 +08:00
|
|
|
Current, StartColumn, Current.OriginalColumn, !Current.Previous,
|
2017-11-14 17:19:53 +08:00
|
|
|
/*InPPDirective=*/false, Encoding, Style);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
|
|
|
|
LineState &State,
|
|
|
|
bool AllowBreak,
|
|
|
|
bool DryRun) {
|
|
|
|
std::unique_ptr<BreakableToken> Token =
|
|
|
|
createBreakableToken(Current, State, AllowBreak);
|
|
|
|
if (!Token)
|
|
|
|
return 0;
|
|
|
|
unsigned ColumnLimit = getColumnLimit(State);
|
|
|
|
unsigned StartColumn = State.Column - Current.ColumnWidth;
|
|
|
|
if (Current.is(TT_LineComment)) {
|
2013-11-13 01:30:49 +08:00
|
|
|
// We don't insert backslashes when breaking line comments.
|
|
|
|
ColumnLimit = Style.ColumnLimit;
|
2013-08-16 19:20:30 +08:00
|
|
|
}
|
2013-11-13 01:30:49 +08:00
|
|
|
if (Current.UnbreakableTailLength >= ColumnLimit)
|
2013-08-16 19:20:30 +08:00
|
|
|
return 0;
|
|
|
|
|
2017-11-17 19:17:15 +08:00
|
|
|
unsigned NewBreakPenalty = Current.isStringLiteral()
|
|
|
|
? Style.PenaltyBreakString
|
|
|
|
: Style.PenaltyBreakComment;
|
2013-11-13 01:30:49 +08:00
|
|
|
unsigned RemainingSpace = ColumnLimit - Current.UnbreakableTailLength;
|
2017-11-17 19:17:15 +08:00
|
|
|
bool BreakInserted = Token->introducesBreakBeforeToken();
|
|
|
|
// Store whether we inserted a new line break at the end of the previous
|
|
|
|
// logical line.
|
|
|
|
bool NewBreakBefore = false;
|
2017-01-25 21:58:58 +08:00
|
|
|
// We use a conservative reflowing strategy. Reflow starts after a line is
|
|
|
|
// broken or the corresponding whitespace compressed. Reflow ends as soon as a
|
|
|
|
// line that doesn't get reflown with the previous line is reached.
|
|
|
|
bool ReflowInProgress = false;
|
2013-08-16 19:20:30 +08:00
|
|
|
unsigned Penalty = 0;
|
|
|
|
unsigned RemainingTokenColumns = 0;
|
2017-07-21 06:29:39 +08:00
|
|
|
unsigned TailOffset = 0;
|
2017-11-17 19:17:15 +08:00
|
|
|
DEBUG(llvm::dbgs() << "Breaking protruding token at column " << StartColumn
|
|
|
|
<< ".\n");
|
2013-08-16 19:20:30 +08:00
|
|
|
for (unsigned LineIndex = 0, EndIndex = Token->getLineCount();
|
|
|
|
LineIndex != EndIndex; ++LineIndex) {
|
2017-11-17 19:17:15 +08:00
|
|
|
DEBUG(llvm::dbgs() << " Line: " << LineIndex
|
|
|
|
<< " (Reflow: " << ReflowInProgress << ")\n");
|
2017-01-25 21:58:58 +08:00
|
|
|
BreakableToken::Split SplitBefore(StringRef::npos, 0);
|
|
|
|
if (ReflowInProgress) {
|
|
|
|
SplitBefore = Token->getSplitBefore(LineIndex, RemainingTokenColumns,
|
2017-02-02 23:32:19 +08:00
|
|
|
RemainingSpace, CommentPragmasRegex);
|
2017-01-25 21:58:58 +08:00
|
|
|
}
|
|
|
|
ReflowInProgress = SplitBefore.first != StringRef::npos;
|
2017-11-17 19:17:15 +08:00
|
|
|
DEBUG({
|
|
|
|
if (ReflowInProgress)
|
|
|
|
llvm::dbgs() << " Reflowing.\n";
|
|
|
|
});
|
2017-07-21 06:29:39 +08:00
|
|
|
TailOffset =
|
2017-01-25 21:58:58 +08:00
|
|
|
ReflowInProgress ? (SplitBefore.first + SplitBefore.second) : 0;
|
2017-11-17 19:17:15 +08:00
|
|
|
// If we found a reflow split and have added a new break before this line,
|
|
|
|
// we are going to remove the line break at the start of the next logical
|
|
|
|
// line.
|
|
|
|
// For example, here we'll add a new line break after 'text', and
|
|
|
|
// subsequently delete the line break between 'that' and 'reflows'.
|
|
|
|
// // some text that
|
|
|
|
// // reflows
|
|
|
|
// ->
|
|
|
|
// // some text
|
|
|
|
// // that reflows
|
|
|
|
// When adding the line break, we also added the penalty for it, so we need
|
|
|
|
// to subtract that penalty again when we remove the line break due to
|
|
|
|
// reflowing.
|
|
|
|
if (ReflowInProgress && NewBreakBefore) {
|
|
|
|
assert(Penalty >= NewBreakPenalty);
|
|
|
|
Penalty -= NewBreakPenalty;
|
|
|
|
}
|
|
|
|
NewBreakBefore = false;
|
2013-08-16 19:20:30 +08:00
|
|
|
if (!DryRun)
|
2017-01-25 21:58:58 +08:00
|
|
|
Token->replaceWhitespaceBefore(LineIndex, RemainingTokenColumns,
|
|
|
|
RemainingSpace, SplitBefore, Whitespaces);
|
|
|
|
RemainingTokenColumns = Token->getLineLengthAfterSplitBefore(
|
|
|
|
LineIndex, TailOffset, RemainingTokenColumns, ColumnLimit, SplitBefore);
|
2013-08-16 19:20:30 +08:00
|
|
|
while (RemainingTokenColumns > RemainingSpace) {
|
2017-11-17 19:17:15 +08:00
|
|
|
DEBUG(llvm::dbgs() << " Over limit, need: " << RemainingTokenColumns
|
|
|
|
<< ", space: " << RemainingSpace << "\n");
|
2017-03-08 16:55:12 +08:00
|
|
|
BreakableToken::Split Split = Token->getSplit(
|
|
|
|
LineIndex, TailOffset, ColumnLimit, CommentPragmasRegex);
|
2013-08-16 19:20:30 +08:00
|
|
|
if (Split.first == StringRef::npos) {
|
|
|
|
// The last line's penalty is handled in addNextStateToQueue().
|
|
|
|
if (LineIndex < EndIndex - 1)
|
|
|
|
Penalty += Style.PenaltyExcessCharacter *
|
|
|
|
(RemainingTokenColumns - RemainingSpace);
|
2017-11-17 19:17:15 +08:00
|
|
|
DEBUG(llvm::dbgs() << " No break opportunity.\n");
|
2013-08-16 19:20:30 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
assert(Split.first != 0);
|
2013-11-13 01:50:13 +08:00
|
|
|
|
2017-01-25 21:58:58 +08:00
|
|
|
// Check if compressing the whitespace range will bring the line length
|
|
|
|
// under the limit. If that is the case, we perform whitespace compression
|
|
|
|
// instead of inserting a line break.
|
|
|
|
unsigned RemainingTokenColumnsAfterCompression =
|
|
|
|
Token->getLineLengthAfterCompression(RemainingTokenColumns, Split);
|
|
|
|
if (RemainingTokenColumnsAfterCompression <= RemainingSpace) {
|
|
|
|
RemainingTokenColumns = RemainingTokenColumnsAfterCompression;
|
|
|
|
ReflowInProgress = true;
|
2013-11-13 01:50:13 +08:00
|
|
|
if (!DryRun)
|
2017-01-25 21:58:58 +08:00
|
|
|
Token->compressWhitespace(LineIndex, TailOffset, Split, Whitespaces);
|
2017-11-17 19:17:15 +08:00
|
|
|
DEBUG(llvm::dbgs() << " Compressing below limit.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute both the penalties for:
|
|
|
|
// - not breaking, and leaving excess characters
|
|
|
|
// - adding a new line break
|
|
|
|
assert(RemainingTokenColumnsAfterCompression > RemainingSpace);
|
|
|
|
unsigned ExcessCharactersPenalty =
|
|
|
|
(RemainingTokenColumnsAfterCompression - RemainingSpace) *
|
|
|
|
Style.PenaltyExcessCharacter;
|
|
|
|
|
|
|
|
unsigned BreakPenalty = NewBreakPenalty;
|
|
|
|
unsigned ColumnsUsed =
|
|
|
|
Token->getLineLengthAfterSplit(LineIndex, TailOffset, Split.first);
|
|
|
|
if (ColumnsUsed > ColumnLimit)
|
|
|
|
BreakPenalty +=
|
|
|
|
Style.PenaltyExcessCharacter * (ColumnsUsed - ColumnLimit);
|
|
|
|
|
|
|
|
DEBUG(llvm::dbgs() << " Penalty excess: " << ExcessCharactersPenalty
|
|
|
|
<< "\n break : " << BreakPenalty << "\n");
|
|
|
|
// Only continue to add the line break if the penalty of the excess
|
|
|
|
// characters is larger than the penalty of the line break.
|
|
|
|
// FIXME: This does not take into account when we can later remove the
|
|
|
|
// line break again due to a reflow.
|
|
|
|
if (ExcessCharactersPenalty < BreakPenalty) {
|
|
|
|
if (!DryRun)
|
|
|
|
Token->compressWhitespace(LineIndex, TailOffset, Split, Whitespaces);
|
|
|
|
// Do not set ReflowInProgress: we do not have any space left to
|
|
|
|
// reflow into.
|
|
|
|
Penalty += ExcessCharactersPenalty;
|
2013-11-13 01:50:13 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-01-25 21:58:58 +08:00
|
|
|
unsigned NewRemainingTokenColumns = Token->getLineLengthAfterSplit(
|
|
|
|
LineIndex, TailOffset + Split.first + Split.second, StringRef::npos);
|
|
|
|
|
2014-04-15 22:52:43 +08:00
|
|
|
// When breaking before a tab character, it may be moved by a few columns,
|
|
|
|
// but will still be expanded to the next tab stop, so we don't save any
|
|
|
|
// columns.
|
|
|
|
if (NewRemainingTokenColumns == RemainingTokenColumns)
|
2017-11-17 19:17:15 +08:00
|
|
|
// FIXME: Do we need to adjust the penalty?
|
2014-04-15 22:52:43 +08:00
|
|
|
break;
|
2013-08-16 19:20:30 +08:00
|
|
|
assert(NewRemainingTokenColumns < RemainingTokenColumns);
|
2017-11-17 19:17:15 +08:00
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
if (!DryRun)
|
|
|
|
Token->insertBreak(LineIndex, TailOffset, Split, Whitespaces);
|
2017-11-17 19:17:15 +08:00
|
|
|
|
|
|
|
Penalty += BreakPenalty;
|
2013-08-16 19:20:30 +08:00
|
|
|
TailOffset += Split.first + Split.second;
|
|
|
|
RemainingTokenColumns = NewRemainingTokenColumns;
|
2017-01-25 21:58:58 +08:00
|
|
|
ReflowInProgress = true;
|
2013-08-16 19:20:30 +08:00
|
|
|
BreakInserted = true;
|
2017-11-17 19:17:15 +08:00
|
|
|
NewBreakBefore = true;
|
2013-08-16 19:20:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-09 17:42:32 +08:00
|
|
|
BreakableToken::Split SplitAfterLastLine =
|
|
|
|
Token->getSplitAfterLastLine(TailOffset, ColumnLimit);
|
2017-07-21 06:29:39 +08:00
|
|
|
if (SplitAfterLastLine.first != StringRef::npos) {
|
2017-11-17 19:17:15 +08:00
|
|
|
DEBUG(llvm::dbgs() << "Replacing whitespace after last line.\n");
|
2017-07-21 06:29:39 +08:00
|
|
|
if (!DryRun)
|
|
|
|
Token->replaceWhitespaceAfterLastLine(TailOffset, SplitAfterLastLine,
|
|
|
|
Whitespaces);
|
|
|
|
RemainingTokenColumns = Token->getLineLengthAfterSplitAfterLastLine(
|
|
|
|
TailOffset, SplitAfterLastLine);
|
|
|
|
}
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
State.Column = RemainingTokenColumns;
|
|
|
|
|
|
|
|
if (BreakInserted) {
|
|
|
|
// If we break the token inside a parameter list, we need to break before
|
|
|
|
// the next parameter on all levels, so that the next parameter is clearly
|
|
|
|
// visible. Line comments already introduce a break.
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current.isNot(TT_LineComment)) {
|
2013-08-16 19:20:30 +08:00
|
|
|
for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
|
|
|
|
State.Stack[i].BreakBeforeParameter = true;
|
|
|
|
}
|
|
|
|
|
2017-10-16 17:08:53 +08:00
|
|
|
if (Current.is(TT_BlockComment))
|
|
|
|
State.NoContinuation = true;
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
State.Stack.back().LastSpace = StartColumn;
|
|
|
|
}
|
2017-01-25 21:58:58 +08:00
|
|
|
|
|
|
|
Token->updateNextToken(State);
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
return Penalty;
|
|
|
|
}
|
|
|
|
|
2013-09-05 17:29:45 +08:00
|
|
|
unsigned ContinuationIndenter::getColumnLimit(const LineState &State) const {
|
2013-08-16 19:20:30 +08:00
|
|
|
// In preprocessor directives reserve two chars for trailing " \"
|
2013-09-05 17:29:45 +08:00
|
|
|
return Style.ColumnLimit - (State.Line->InPPDirective ? 2 : 0);
|
2013-08-16 19:20:30 +08:00
|
|
|
}
|
|
|
|
|
2013-12-16 15:23:08 +08:00
|
|
|
bool ContinuationIndenter::nextIsMultilineString(const LineState &State) {
|
2013-08-23 19:57:34 +08:00
|
|
|
const FormatToken &Current = *State.NextToken;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (!Current.isStringLiteral() || Current.is(TT_ImplicitStringLiteral))
|
2013-08-23 19:57:34 +08:00
|
|
|
return false;
|
2013-08-30 01:32:57 +08:00
|
|
|
// We never consider raw string literals "multiline" for the purpose of
|
2013-12-16 15:23:08 +08:00
|
|
|
// AlwaysBreakBeforeMultilineStrings implementation as they are special-cased
|
|
|
|
// (see TokenAnnotator::mustBreakBefore().
|
2013-08-30 01:32:57 +08:00
|
|
|
if (Current.TokenText.startswith("R\""))
|
|
|
|
return false;
|
2013-09-10 17:38:25 +08:00
|
|
|
if (Current.IsMultiline)
|
2013-08-30 01:32:57 +08:00
|
|
|
return true;
|
2013-08-23 19:57:34 +08:00
|
|
|
if (Current.getNextNonComment() &&
|
2013-12-20 14:22:01 +08:00
|
|
|
Current.getNextNonComment()->isStringLiteral())
|
2013-08-23 19:57:34 +08:00
|
|
|
return true; // Implicit concatenation.
|
2015-01-20 20:59:20 +08:00
|
|
|
if (Style.ColumnLimit != 0 &&
|
|
|
|
State.Column + Current.ColumnWidth + Current.UnbreakableTailLength >
|
|
|
|
Style.ColumnLimit)
|
2013-08-23 19:57:34 +08:00
|
|
|
return true; // String will be split.
|
2013-08-30 01:32:57 +08:00
|
|
|
return false;
|
2013-08-23 19:57:34 +08:00
|
|
|
}
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
} // namespace format
|
|
|
|
} // namespace clang
|