2013-08-16 19:20:30 +08:00
|
|
|
//===--- ContinuationIndenter.h - Format C++ code ---------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-08-16 19:20:30 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-09 09:00:01 +08:00
|
|
|
/// This file implements an indenter that manages the indentation of
|
2013-08-16 19:20:30 +08:00
|
|
|
/// continuations.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:25:19 +08:00
|
|
|
#ifndef LLVM_CLANG_LIB_FORMAT_CONTINUATIONINDENTER_H
|
|
|
|
#define LLVM_CLANG_LIB_FORMAT_CONTINUATIONINDENTER_H
|
2013-08-16 19:20:30 +08:00
|
|
|
|
|
|
|
#include "Encoding.h"
|
2014-11-04 20:41:02 +08:00
|
|
|
#include "FormatToken.h"
|
2013-08-16 19:20:30 +08:00
|
|
|
#include "clang/Format/Format.h"
|
2014-01-02 23:13:14 +08:00
|
|
|
#include "llvm/Support/Regex.h"
|
2017-10-30 22:01:50 +08:00
|
|
|
#include <map>
|
|
|
|
#include <tuple>
|
2013-08-16 19:20:30 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
class SourceManager;
|
|
|
|
|
|
|
|
namespace format {
|
|
|
|
|
|
|
|
class AnnotatedLine;
|
2017-11-14 17:19:53 +08:00
|
|
|
class BreakableToken;
|
2013-08-16 19:20:30 +08:00
|
|
|
struct FormatToken;
|
|
|
|
struct LineState;
|
|
|
|
struct ParenState;
|
2017-10-30 22:01:50 +08:00
|
|
|
struct RawStringFormatStyleManager;
|
2013-08-16 19:20:30 +08:00
|
|
|
class WhitespaceManager;
|
|
|
|
|
2017-10-30 22:01:50 +08:00
|
|
|
struct RawStringFormatStyleManager {
|
|
|
|
llvm::StringMap<FormatStyle> DelimiterStyle;
|
2018-01-18 00:17:26 +08:00
|
|
|
llvm::StringMap<FormatStyle> EnclosingFunctionStyle;
|
2017-10-30 22:01:50 +08:00
|
|
|
|
|
|
|
RawStringFormatStyleManager(const FormatStyle &CodeStyle);
|
|
|
|
|
2018-01-18 00:17:26 +08:00
|
|
|
llvm::Optional<FormatStyle> getDelimiterStyle(StringRef Delimiter) const;
|
|
|
|
|
|
|
|
llvm::Optional<FormatStyle>
|
|
|
|
getEnclosingFunctionStyle(StringRef EnclosingFunction) const;
|
2017-10-30 22:01:50 +08:00
|
|
|
};
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
class ContinuationIndenter {
|
|
|
|
public:
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Constructs a \c ContinuationIndenter to format \p Line starting in
|
2013-08-16 19:20:30 +08:00
|
|
|
/// column \p FirstIndent.
|
2014-11-04 20:41:02 +08:00
|
|
|
ContinuationIndenter(const FormatStyle &Style,
|
|
|
|
const AdditionalKeywords &Keywords,
|
2016-04-28 15:52:03 +08:00
|
|
|
const SourceManager &SourceMgr,
|
|
|
|
WhitespaceManager &Whitespaces,
|
2013-08-16 19:20:30 +08:00
|
|
|
encoding::Encoding Encoding,
|
|
|
|
bool BinPackInconclusiveFunctions);
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Get the initial state, i.e. the state after placing \p Line's
|
2017-10-30 22:01:50 +08:00
|
|
|
/// first token at \p FirstIndent. When reformatting a fragment of code, as in
|
|
|
|
/// the case of formatting inside raw string literals, \p FirstStartColumn is
|
|
|
|
/// the column at which the state of the parent formatter is.
|
|
|
|
LineState getInitialState(unsigned FirstIndent, unsigned FirstStartColumn,
|
|
|
|
const AnnotatedLine *Line, bool DryRun);
|
2013-08-16 19:20:30 +08:00
|
|
|
|
|
|
|
// FIXME: canBreak and mustBreak aren't strictly indentation-related. Find a
|
|
|
|
// better home.
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns \c true, if a line break after \p State is allowed.
|
2013-08-16 19:20:30 +08:00
|
|
|
bool canBreak(const LineState &State);
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns \c true, if a line break after \p State is mandatory.
|
2013-08-16 19:20:30 +08:00
|
|
|
bool mustBreak(const LineState &State);
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Appends the next token to \p State and updates information
|
2013-08-16 19:20:30 +08:00
|
|
|
/// necessary for indentation.
|
|
|
|
///
|
|
|
|
/// Puts the token on the current line if \p Newline is \c false and adds a
|
|
|
|
/// line break and necessary indentation otherwise.
|
|
|
|
///
|
|
|
|
/// If \p DryRun is \c false, also creates and stores the required
|
|
|
|
/// \c Replacement.
|
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
|
|
|
unsigned addTokenToState(LineState &State, bool Newline, bool DryRun,
|
|
|
|
unsigned ExtraSpaces = 0);
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Get the column limit for this line. This is the style's column
|
2013-08-16 19:20:30 +08:00
|
|
|
/// limit, potentially reduced for preprocessor definitions.
|
2013-09-05 17:29:45 +08:00
|
|
|
unsigned getColumnLimit(const LineState &State) const;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
|
|
|
private:
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Mark the next token as consumed in \p State and modify its stacks
|
2013-08-16 19:20:30 +08:00
|
|
|
/// accordingly.
|
|
|
|
unsigned moveStateToNextToken(LineState &State, bool DryRun, bool Newline);
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Update 'State' according to the next token's fake left parentheses.
|
2014-05-26 21:10:39 +08:00
|
|
|
void moveStatePastFakeLParens(LineState &State, bool Newline);
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Update 'State' according to the next token's fake r_parens.
|
2014-05-26 21:10:39 +08:00
|
|
|
void moveStatePastFakeRParens(LineState &State);
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Update 'State' according to the next token being one of "(<{[".
|
2014-05-26 21:10:39 +08:00
|
|
|
void moveStatePastScopeOpener(LineState &State, bool Newline);
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Update 'State' according to the next token being one of ")>}]".
|
2014-05-26 21:10:39 +08:00
|
|
|
void moveStatePastScopeCloser(LineState &State);
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Update 'State' with the next token opening a nested block.
|
2014-05-26 21:10:39 +08:00
|
|
|
void moveStateToNewBlock(LineState &State);
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Reformats a raw string literal.
|
2018-07-31 03:24:48 +08:00
|
|
|
///
|
2017-10-30 22:01:50 +08:00
|
|
|
/// \returns An extra penalty induced by reformatting the token.
|
|
|
|
unsigned reformatRawStringLiteral(const FormatToken &Current,
|
2017-11-14 17:19:53 +08:00
|
|
|
LineState &State,
|
2017-10-30 22:01:50 +08:00
|
|
|
const FormatStyle &RawStringStyle,
|
2019-04-19 01:14:05 +08:00
|
|
|
bool DryRun, bool Newline);
|
2017-10-30 22:01:50 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// If the current token is at the end of the current line, handle
|
2017-11-14 17:19:53 +08:00
|
|
|
/// the transition to the next line.
|
|
|
|
unsigned handleEndOfLine(const FormatToken &Current, LineState &State,
|
2019-04-19 01:14:05 +08:00
|
|
|
bool DryRun, bool AllowBreak, bool Newline);
|
2017-11-14 17:19:53 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// If \p Current is a raw string that is configured to be reformatted,
|
2017-11-14 17:19:53 +08:00
|
|
|
/// return the style to be used.
|
|
|
|
llvm::Optional<FormatStyle> getRawStringStyle(const FormatToken &Current,
|
|
|
|
const LineState &State);
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// If the current token sticks out over the end of the line, break
|
2013-08-16 19:20:30 +08:00
|
|
|
/// it if possible.
|
|
|
|
///
|
2017-12-01 21:28:08 +08:00
|
|
|
/// \returns A pair (penalty, exceeded), where penalty is the extra penalty
|
|
|
|
/// when tokens are broken or lines exceed the column limit, and exceeded
|
|
|
|
/// indicates whether the algorithm purposefully left lines exceeding the
|
|
|
|
/// column limit.
|
2013-08-16 19:20:30 +08:00
|
|
|
///
|
2017-10-30 22:01:50 +08:00
|
|
|
/// The returned penalty will cover the cost of the additional line breaks
|
|
|
|
/// and column limit violation in all lines except for the last one. The
|
|
|
|
/// penalty for the column limit violation in the last line (and in single
|
|
|
|
/// line tokens) is handled in \c addNextStateToQueue.
|
2017-12-01 21:28:08 +08:00
|
|
|
///
|
|
|
|
/// \p Strict indicates whether reflowing is allowed to leave characters
|
|
|
|
/// protruding the column limit; if true, lines will be split strictly within
|
|
|
|
/// the column limit where possible; if false, words are allowed to protrude
|
|
|
|
/// over the column limit as long as the penalty is less than the penalty
|
|
|
|
/// of a break.
|
|
|
|
std::pair<unsigned, bool> breakProtrudingToken(const FormatToken &Current,
|
|
|
|
LineState &State,
|
|
|
|
bool AllowBreak, bool DryRun,
|
|
|
|
bool Strict);
|
2017-11-14 17:19:53 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns the \c BreakableToken starting at \p Current, or nullptr
|
2017-11-14 17:19:53 +08:00
|
|
|
/// if the current token cannot be broken.
|
|
|
|
std::unique_ptr<BreakableToken>
|
|
|
|
createBreakableToken(const FormatToken &Current, LineState &State,
|
|
|
|
bool AllowBreak);
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Appends the next token to \p State and updates information
|
2013-10-01 22:41:18 +08:00
|
|
|
/// necessary for indentation.
|
|
|
|
///
|
|
|
|
/// Puts the token on the current line.
|
|
|
|
///
|
|
|
|
/// If \p DryRun is \c false, also creates and stores the required
|
|
|
|
/// \c Replacement.
|
2013-11-20 22:54:39 +08:00
|
|
|
void addTokenOnCurrentLine(LineState &State, bool DryRun,
|
|
|
|
unsigned ExtraSpaces);
|
2013-10-01 22:41:18 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Appends the next token to \p State and updates information
|
2013-10-01 22:41:18 +08:00
|
|
|
/// necessary for indentation.
|
|
|
|
///
|
|
|
|
/// Adds a line break and necessary indentation.
|
|
|
|
///
|
|
|
|
/// If \p DryRun is \c false, also creates and stores the required
|
|
|
|
/// \c Replacement.
|
|
|
|
unsigned addTokenOnNewLine(LineState &State, bool DryRun);
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Calculate the new column for a line wrap before the next token.
|
2014-03-27 22:33:30 +08:00
|
|
|
unsigned getNewLineColumn(const LineState &State);
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Adds a multiline token to the \p State.
|
2013-08-30 01:32:57 +08:00
|
|
|
///
|
|
|
|
/// \returns Extra penalty for the first line of the literal: last line is
|
|
|
|
/// handled in \c addNextStateToQueue, and the penalty for other lines doesn't
|
|
|
|
/// matter, as we don't change them.
|
2013-09-10 20:29:48 +08:00
|
|
|
unsigned addMultilineToken(const FormatToken &Current, LineState &State);
|
2013-08-30 01:32:57 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns \c true if the next token starts a multiline string
|
2013-08-23 19:57:34 +08:00
|
|
|
/// literal.
|
|
|
|
///
|
|
|
|
/// This includes implicitly concatenated strings, strings that will be broken
|
|
|
|
/// by clang-format and string literals with escaped newlines.
|
2013-12-16 15:23:08 +08:00
|
|
|
bool nextIsMultilineString(const LineState &State);
|
2013-08-23 19:57:34 +08:00
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
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-01-02 23:13:14 +08:00
|
|
|
llvm::Regex CommentPragmasRegex;
|
2017-10-30 22:01:50 +08:00
|
|
|
const RawStringFormatStyleManager RawStringFormats;
|
2013-08-16 19:20:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ParenState {
|
2018-05-09 17:02:11 +08:00
|
|
|
ParenState(const FormatToken *Tok, unsigned Indent, unsigned LastSpace,
|
|
|
|
bool AvoidBinPacking, bool NoLineBreak)
|
|
|
|
: Tok(Tok), Indent(Indent), LastSpace(LastSpace),
|
2020-04-23 15:25:01 +08:00
|
|
|
NestedBlockIndent(Indent), IsAligned(false),
|
|
|
|
BreakBeforeClosingBrace(false), AvoidBinPacking(AvoidBinPacking),
|
|
|
|
BreakBeforeParameter(false), NoLineBreak(NoLineBreak),
|
|
|
|
NoLineBreakInOperand(false), LastOperatorWrapped(true),
|
|
|
|
ContainsLineBreak(false), ContainsUnwrappedBuilder(false),
|
|
|
|
AlignColons(true), ObjCSelectorNameFound(false),
|
|
|
|
HasMultipleNestedBlocks(false), NestedBlockInlined(false),
|
clang-format: support aligned nested conditionals formatting
Summary:
When multiple ternary operators are chained, e.g. like an if/else-if/
else-if/.../else sequence, clang-format will keep aligning the colon
with the question mark, which increases the indent for each
conditionals:
int a = condition1 ? result1
: condition2 ? result2
: condition3 ? result3
: result4;
This patch detects the situation (e.g. conditionals used in false branch
of another conditional), to avoid indenting in that case:
int a = condition1 ? result1
: condition2 ? result2
: condition3 ? result3
: result4;
When BreakBeforeTernaryOperators is false, this will format like this:
int a = condition1 ? result1 :
condition2 ? result2 :
conditino3 ? result3 :
result4;
This formatting style is referenced here:
https://www.fluentcpp.com/2018/02/27/replace-else-if-ternary-operator/
and here:
https://marcmutz.wordpress.com/2010/10/14/top-5-reasons-you-should-love-your-ternary-operator/
Reviewers: krasimir, djasper, klimek, MyDeveloperDay
Reviewed By: MyDeveloperDay
Subscribers: hokein, dyung, MyDeveloperDay, acoomans, cfe-commits
Tags: #clang, #clang-format
Differential Revision: https://reviews.llvm.org/D50078
2020-04-23 21:56:47 +08:00
|
|
|
IsInsideObjCArrayLiteral(false), IsCSharpGenericTypeConstraint(false),
|
[clang-format] Fix AlignOperands when BreakBeforeBinaryOperators is set
Summary:
Even when BreakBeforeBinaryOperators is set, AlignOperands kept
aligning the beginning of the line, even when it could align the
actual operands (e.g. after an assignment).
With this patch, the operands are actually aligned, and the operator
gets aligned with the equal sign:
int aaaaa = bbbbbb
+ cccccc;
This not happen in tests, to avoid 'breaking' the indentation:
if (aaaaa
&& bbbbb)
return;
Reviewers: krasimir, djasper, klimek, MyDeveloperDay
Reviewed By: MyDeveloperDay
Subscribers: MyDeveloperDay, acoomans, cfe-commits, klimek
Tags: #clang, #clang-format
Differential Revision: https://reviews.llvm.org/D32478
2017-05-22 17:23:08 +08:00
|
|
|
IsChainedConditional(false), IsWrappedConditional(false),
|
|
|
|
UnindentOperator(false) {}
|
2018-05-09 17:02:11 +08:00
|
|
|
|
|
|
|
/// \brief The token opening this parenthesis level, or nullptr if this level
|
|
|
|
/// is opened by fake parenthesis.
|
|
|
|
///
|
|
|
|
/// Not considered for memoization as it will always have the same value at
|
|
|
|
/// the same token.
|
|
|
|
const FormatToken *Tok;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The position to which a specific parenthesis level needs to be
|
2013-08-16 19:20:30 +08:00
|
|
|
/// indented.
|
|
|
|
unsigned Indent;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The position of the last space on each level.
|
2013-08-16 19:20:30 +08:00
|
|
|
///
|
|
|
|
/// Used e.g. to break like:
|
|
|
|
/// functionCall(Parameter, otherCall(
|
|
|
|
/// OtherParameter));
|
|
|
|
unsigned LastSpace;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// If a block relative to this parenthesis level gets wrapped, indent
|
2014-12-12 17:40:58 +08:00
|
|
|
/// it this much.
|
|
|
|
unsigned NestedBlockIndent;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The position the first "<<" operator encountered on each level.
|
2013-08-16 19:20:30 +08:00
|
|
|
///
|
|
|
|
/// Used to align "<<" operators. 0 if no such operator has been encountered
|
|
|
|
/// on a level.
|
2015-06-12 22:39:08 +08:00
|
|
|
unsigned FirstLessLess = 0;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The column of a \c ? in a conditional expression;
|
2015-06-12 22:39:08 +08:00
|
|
|
unsigned QuestionColumn = 0;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The position of the colon in an ObjC method declaration/call.
|
2015-06-12 22:39:08 +08:00
|
|
|
unsigned ColonPos = 0;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The start of the most recent function in a builder-type call.
|
2015-06-12 22:39:08 +08:00
|
|
|
unsigned StartOfFunctionCall = 0;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Contains the start of array subscript expressions, so that they
|
2013-08-16 19:20:30 +08:00
|
|
|
/// can be aligned.
|
2015-06-12 22:39:08 +08:00
|
|
|
unsigned StartOfArraySubscripts = 0;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// If a nested name specifier was broken over multiple lines, this
|
2013-08-16 19:20:30 +08:00
|
|
|
/// contains the start column of the second line. Otherwise 0.
|
2015-06-12 22:39:08 +08:00
|
|
|
unsigned NestedNameSpecifierContinuation = 0;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// If a call expression was broken over multiple lines, this
|
2013-08-16 19:20:30 +08:00
|
|
|
/// contains the start column of the second line. Otherwise 0.
|
2015-06-12 22:39:08 +08:00
|
|
|
unsigned CallContinuation = 0;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The column of the first variable name in a variable declaration.
|
2013-08-16 19:20:30 +08:00
|
|
|
///
|
|
|
|
/// Used to align further variables if necessary.
|
2015-06-12 22:39:08 +08:00
|
|
|
unsigned VariablePos = 0;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2020-04-13 22:14:26 +08:00
|
|
|
/// Whether this block's indentation is used for alignment.
|
|
|
|
bool IsAligned : 1;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Whether a newline needs to be inserted before the block's closing
|
2015-06-12 21:07:03 +08:00
|
|
|
/// brace.
|
|
|
|
///
|
|
|
|
/// We only want to insert a newline before the closing brace if there also
|
|
|
|
/// was a newline after the beginning left brace.
|
|
|
|
bool BreakBeforeClosingBrace : 1;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Avoid bin packing, i.e. multiple parameters/elements on multiple
|
2015-06-12 21:07:03 +08:00
|
|
|
/// lines, in this context.
|
|
|
|
bool AvoidBinPacking : 1;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Break after the next comma (or all the commas in this context if
|
2015-06-12 21:07:03 +08:00
|
|
|
/// \c AvoidBinPacking is \c true).
|
|
|
|
bool BreakBeforeParameter : 1;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Line breaking in this context would break a formatting rule.
|
2015-06-12 21:07:03 +08:00
|
|
|
bool NoLineBreak : 1;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Same as \c NoLineBreak, but is restricted until the end of the
|
2017-01-16 21:13:15 +08:00
|
|
|
/// operand (including the next ",").
|
|
|
|
bool NoLineBreakInOperand : 1;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// True if the last binary operator on this level was wrapped to the
|
2015-06-12 21:07:03 +08:00
|
|
|
/// next line.
|
|
|
|
bool LastOperatorWrapped : 1;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// \c true if this \c ParenState already contains a line-break.
|
2013-08-16 19:20:30 +08:00
|
|
|
///
|
|
|
|
/// The first line break in a certain \c ParenState causes extra penalty so
|
|
|
|
/// that clang-format prefers similar breaks, i.e. breaks in the same
|
|
|
|
/// parenthesis.
|
2015-06-12 21:07:03 +08:00
|
|
|
bool ContainsLineBreak : 1;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// \c true if this \c ParenState contains multiple segments of a
|
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
|
|
|
/// builder-type call on one line.
|
2015-06-12 21:07:03 +08:00
|
|
|
bool ContainsUnwrappedBuilder : 1;
|
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
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// \c true if the colons of the curren ObjC method expression should
|
2013-12-23 15:29:06 +08:00
|
|
|
/// be aligned.
|
|
|
|
///
|
|
|
|
/// Not considered for memoization as it will always have the same value at
|
|
|
|
/// the same token.
|
2015-06-12 21:07:03 +08:00
|
|
|
bool AlignColons : 1;
|
2013-12-23 15:29:06 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// \c true if at least one selector name was found in the current
|
2013-12-23 15:29:06 +08:00
|
|
|
/// ObjC method expression.
|
|
|
|
///
|
|
|
|
/// Not considered for memoization as it will always have the same value at
|
|
|
|
/// the same token.
|
2015-06-12 21:07:03 +08:00
|
|
|
bool ObjCSelectorNameFound : 1;
|
2013-12-23 15:29:06 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// \c true if there are multiple nested blocks inside these parens.
|
2014-04-09 20:08:39 +08:00
|
|
|
///
|
|
|
|
/// Not considered for memoization as it will always have the same value at
|
|
|
|
/// the same token.
|
2015-06-12 21:07:03 +08:00
|
|
|
bool HasMultipleNestedBlocks : 1;
|
2014-04-09 20:08:39 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The start of a nested block (e.g. lambda introducer in C++ or
|
2018-02-06 20:12:00 +08:00
|
|
|
/// "function" in JavaScript) is not wrapped to a new line.
|
2015-06-12 21:07:03 +08:00
|
|
|
bool NestedBlockInlined : 1;
|
2014-05-21 20:51:23 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// \c true if the current \c ParenState represents an Objective-C
|
2018-02-09 00:07:25 +08:00
|
|
|
/// array literal.
|
|
|
|
bool IsInsideObjCArrayLiteral : 1;
|
|
|
|
|
2020-03-23 20:50:44 +08:00
|
|
|
bool IsCSharpGenericTypeConstraint : 1;
|
|
|
|
|
clang-format: support aligned nested conditionals formatting
Summary:
When multiple ternary operators are chained, e.g. like an if/else-if/
else-if/.../else sequence, clang-format will keep aligning the colon
with the question mark, which increases the indent for each
conditionals:
int a = condition1 ? result1
: condition2 ? result2
: condition3 ? result3
: result4;
This patch detects the situation (e.g. conditionals used in false branch
of another conditional), to avoid indenting in that case:
int a = condition1 ? result1
: condition2 ? result2
: condition3 ? result3
: result4;
When BreakBeforeTernaryOperators is false, this will format like this:
int a = condition1 ? result1 :
condition2 ? result2 :
conditino3 ? result3 :
result4;
This formatting style is referenced here:
https://www.fluentcpp.com/2018/02/27/replace-else-if-ternary-operator/
and here:
https://marcmutz.wordpress.com/2010/10/14/top-5-reasons-you-should-love-your-ternary-operator/
Reviewers: krasimir, djasper, klimek, MyDeveloperDay
Reviewed By: MyDeveloperDay
Subscribers: hokein, dyung, MyDeveloperDay, acoomans, cfe-commits
Tags: #clang, #clang-format
Differential Revision: https://reviews.llvm.org/D50078
2020-04-23 21:56:47 +08:00
|
|
|
/// \brief true if the current \c ParenState represents the false branch of
|
|
|
|
/// a chained conditional expression (e.g. else-if)
|
|
|
|
bool IsChainedConditional : 1;
|
|
|
|
|
|
|
|
/// \brief true if there conditionnal was wrapped on the first operator (the
|
|
|
|
/// question mark)
|
|
|
|
bool IsWrappedConditional : 1;
|
|
|
|
|
[clang-format] Fix AlignOperands when BreakBeforeBinaryOperators is set
Summary:
Even when BreakBeforeBinaryOperators is set, AlignOperands kept
aligning the beginning of the line, even when it could align the
actual operands (e.g. after an assignment).
With this patch, the operands are actually aligned, and the operator
gets aligned with the equal sign:
int aaaaa = bbbbbb
+ cccccc;
This not happen in tests, to avoid 'breaking' the indentation:
if (aaaaa
&& bbbbb)
return;
Reviewers: krasimir, djasper, klimek, MyDeveloperDay
Reviewed By: MyDeveloperDay
Subscribers: MyDeveloperDay, acoomans, cfe-commits, klimek
Tags: #clang, #clang-format
Differential Revision: https://reviews.llvm.org/D32478
2017-05-22 17:23:08 +08:00
|
|
|
/// \brief Indicates the indent should be reduced by the length of the
|
|
|
|
/// operator.
|
|
|
|
bool UnindentOperator : 1;
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
bool operator<(const ParenState &Other) const {
|
|
|
|
if (Indent != Other.Indent)
|
|
|
|
return Indent < Other.Indent;
|
|
|
|
if (LastSpace != Other.LastSpace)
|
|
|
|
return LastSpace < Other.LastSpace;
|
2014-12-12 17:40:58 +08:00
|
|
|
if (NestedBlockIndent != Other.NestedBlockIndent)
|
|
|
|
return NestedBlockIndent < Other.NestedBlockIndent;
|
2013-08-16 19:20:30 +08:00
|
|
|
if (FirstLessLess != Other.FirstLessLess)
|
|
|
|
return FirstLessLess < Other.FirstLessLess;
|
2020-04-13 22:14:26 +08:00
|
|
|
if (IsAligned != Other.IsAligned)
|
|
|
|
return IsAligned;
|
2013-08-16 19:20:30 +08:00
|
|
|
if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace)
|
|
|
|
return BreakBeforeClosingBrace;
|
|
|
|
if (QuestionColumn != Other.QuestionColumn)
|
|
|
|
return QuestionColumn < Other.QuestionColumn;
|
|
|
|
if (AvoidBinPacking != Other.AvoidBinPacking)
|
|
|
|
return AvoidBinPacking;
|
|
|
|
if (BreakBeforeParameter != Other.BreakBeforeParameter)
|
|
|
|
return BreakBeforeParameter;
|
|
|
|
if (NoLineBreak != Other.NoLineBreak)
|
|
|
|
return NoLineBreak;
|
2014-04-14 19:08:45 +08:00
|
|
|
if (LastOperatorWrapped != Other.LastOperatorWrapped)
|
|
|
|
return LastOperatorWrapped;
|
2013-08-16 19:20:30 +08:00
|
|
|
if (ColonPos != Other.ColonPos)
|
|
|
|
return ColonPos < Other.ColonPos;
|
|
|
|
if (StartOfFunctionCall != Other.StartOfFunctionCall)
|
|
|
|
return StartOfFunctionCall < Other.StartOfFunctionCall;
|
|
|
|
if (StartOfArraySubscripts != Other.StartOfArraySubscripts)
|
|
|
|
return StartOfArraySubscripts < Other.StartOfArraySubscripts;
|
|
|
|
if (CallContinuation != Other.CallContinuation)
|
|
|
|
return CallContinuation < Other.CallContinuation;
|
|
|
|
if (VariablePos != Other.VariablePos)
|
|
|
|
return VariablePos < Other.VariablePos;
|
|
|
|
if (ContainsLineBreak != Other.ContainsLineBreak)
|
2015-06-09 19:39:22 +08:00
|
|
|
return ContainsLineBreak;
|
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 (ContainsUnwrappedBuilder != Other.ContainsUnwrappedBuilder)
|
2015-06-09 19:39:22 +08:00
|
|
|
return ContainsUnwrappedBuilder;
|
2014-11-21 21:38:53 +08:00
|
|
|
if (NestedBlockInlined != Other.NestedBlockInlined)
|
2015-06-09 19:39:22 +08:00
|
|
|
return NestedBlockInlined;
|
2020-03-23 20:50:44 +08:00
|
|
|
if (IsCSharpGenericTypeConstraint != Other.IsCSharpGenericTypeConstraint)
|
|
|
|
return IsCSharpGenericTypeConstraint;
|
clang-format: support aligned nested conditionals formatting
Summary:
When multiple ternary operators are chained, e.g. like an if/else-if/
else-if/.../else sequence, clang-format will keep aligning the colon
with the question mark, which increases the indent for each
conditionals:
int a = condition1 ? result1
: condition2 ? result2
: condition3 ? result3
: result4;
This patch detects the situation (e.g. conditionals used in false branch
of another conditional), to avoid indenting in that case:
int a = condition1 ? result1
: condition2 ? result2
: condition3 ? result3
: result4;
When BreakBeforeTernaryOperators is false, this will format like this:
int a = condition1 ? result1 :
condition2 ? result2 :
conditino3 ? result3 :
result4;
This formatting style is referenced here:
https://www.fluentcpp.com/2018/02/27/replace-else-if-ternary-operator/
and here:
https://marcmutz.wordpress.com/2010/10/14/top-5-reasons-you-should-love-your-ternary-operator/
Reviewers: krasimir, djasper, klimek, MyDeveloperDay
Reviewed By: MyDeveloperDay
Subscribers: hokein, dyung, MyDeveloperDay, acoomans, cfe-commits
Tags: #clang, #clang-format
Differential Revision: https://reviews.llvm.org/D50078
2020-04-23 21:56:47 +08:00
|
|
|
if (IsChainedConditional != Other.IsChainedConditional)
|
|
|
|
return IsChainedConditional;
|
|
|
|
if (IsWrappedConditional != Other.IsWrappedConditional)
|
|
|
|
return IsWrappedConditional;
|
[clang-format] Fix AlignOperands when BreakBeforeBinaryOperators is set
Summary:
Even when BreakBeforeBinaryOperators is set, AlignOperands kept
aligning the beginning of the line, even when it could align the
actual operands (e.g. after an assignment).
With this patch, the operands are actually aligned, and the operator
gets aligned with the equal sign:
int aaaaa = bbbbbb
+ cccccc;
This not happen in tests, to avoid 'breaking' the indentation:
if (aaaaa
&& bbbbb)
return;
Reviewers: krasimir, djasper, klimek, MyDeveloperDay
Reviewed By: MyDeveloperDay
Subscribers: MyDeveloperDay, acoomans, cfe-commits, klimek
Tags: #clang, #clang-format
Differential Revision: https://reviews.llvm.org/D32478
2017-05-22 17:23:08 +08:00
|
|
|
if (UnindentOperator != Other.UnindentOperator)
|
|
|
|
return UnindentOperator;
|
2013-08-16 19:20:30 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The current state when indenting a unwrapped line.
|
2013-08-16 19:20:30 +08:00
|
|
|
///
|
|
|
|
/// As the indenting tries different combinations this is copied by value.
|
|
|
|
struct LineState {
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The number of used columns in the current line.
|
2013-08-16 19:20:30 +08:00
|
|
|
unsigned Column;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The token that needs to be next formatted.
|
2013-10-12 05:25:45 +08:00
|
|
|
FormatToken *NextToken;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// \c true if this line contains a continued for-loop section.
|
2013-08-16 19:20:30 +08:00
|
|
|
bool LineContainsContinuedForLoopSection;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// \c true if \p NextToken should not continue this line.
|
2017-10-16 17:08:53 +08:00
|
|
|
bool NoContinuation;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The \c NestingLevel at the start of this line.
|
2013-08-16 19:20:30 +08:00
|
|
|
unsigned StartOfLineLevel;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The lowest \c NestingLevel on the current line.
|
2013-08-16 19:20:30 +08:00
|
|
|
unsigned LowestLevelOnLine;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The start column of the string literal, if we're in a string
|
2013-08-16 19:20:30 +08:00
|
|
|
/// literal sequence, 0 otherwise.
|
|
|
|
unsigned StartOfStringLiteral;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// A stack keeping track of properties applying to parenthesis
|
2013-08-16 19:20:30 +08:00
|
|
|
/// levels.
|
|
|
|
std::vector<ParenState> Stack;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Ignore the stack of \c ParenStates for state comparison.
|
2013-08-16 19:20:30 +08:00
|
|
|
///
|
|
|
|
/// In long and deeply nested unwrapped lines, the current algorithm can
|
|
|
|
/// be insufficient for finding the best formatting with a reasonable amount
|
|
|
|
/// of time and memory. Setting this flag will effectively lead to the
|
|
|
|
/// algorithm not analyzing some combinations. However, these combinations
|
|
|
|
/// rarely contain the optimal solution: In short, accepting a higher
|
|
|
|
/// penalty early would need to lead to different values in the \c
|
|
|
|
/// ParenState stack (in an otherwise identical state) and these different
|
|
|
|
/// values would need to lead to a significant amount of avoided penalty
|
|
|
|
/// later.
|
|
|
|
///
|
|
|
|
/// FIXME: Come up with a better algorithm instead.
|
|
|
|
bool IgnoreStackForComparison;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The indent of the first token.
|
2013-09-05 17:29:45 +08:00
|
|
|
unsigned FirstIndent;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The line that is being formatted.
|
2013-09-05 17:29:45 +08:00
|
|
|
///
|
|
|
|
/// Does not need to be considered for memoization because it doesn't change.
|
|
|
|
const AnnotatedLine *Line;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Comparison operator to be able to used \c LineState in \c map.
|
2013-08-16 19:20:30 +08:00
|
|
|
bool operator<(const LineState &Other) const {
|
|
|
|
if (NextToken != Other.NextToken)
|
|
|
|
return NextToken < Other.NextToken;
|
|
|
|
if (Column != Other.Column)
|
|
|
|
return Column < Other.Column;
|
|
|
|
if (LineContainsContinuedForLoopSection !=
|
|
|
|
Other.LineContainsContinuedForLoopSection)
|
|
|
|
return LineContainsContinuedForLoopSection;
|
2017-10-16 17:08:53 +08:00
|
|
|
if (NoContinuation != Other.NoContinuation)
|
|
|
|
return NoContinuation;
|
2013-08-16 19:20:30 +08:00
|
|
|
if (StartOfLineLevel != Other.StartOfLineLevel)
|
|
|
|
return StartOfLineLevel < Other.StartOfLineLevel;
|
|
|
|
if (LowestLevelOnLine != Other.LowestLevelOnLine)
|
|
|
|
return LowestLevelOnLine < Other.LowestLevelOnLine;
|
|
|
|
if (StartOfStringLiteral != Other.StartOfStringLiteral)
|
|
|
|
return StartOfStringLiteral < Other.StartOfStringLiteral;
|
|
|
|
if (IgnoreStackForComparison || Other.IgnoreStackForComparison)
|
|
|
|
return false;
|
|
|
|
return Stack < Other.Stack;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace format
|
|
|
|
} // end namespace clang
|
|
|
|
|
2014-08-14 00:25:19 +08:00
|
|
|
#endif
|