2013-04-15 22:28:00 +08:00
|
|
|
//===--- WhitespaceManager.cpp - Format C++ code --------------------------===//
|
|
|
|
//
|
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-04-15 22:28:00 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-09 09:00:01 +08:00
|
|
|
/// This file implements WhitespaceManager class.
|
2013-04-15 22:28:00 +08:00
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "WhitespaceManager.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
|
2019-07-29 21:26:48 +08:00
|
|
|
bool WhitespaceManager::Change::IsBeforeInFile::operator()(
|
|
|
|
const Change &C1, const Change &C2) const {
|
2013-05-22 20:51:29 +08:00
|
|
|
return SourceMgr.isBeforeInTranslationUnit(
|
|
|
|
C1.OriginalWhitespaceRange.getBegin(),
|
|
|
|
C2.OriginalWhitespaceRange.getBegin());
|
|
|
|
}
|
2013-04-25 16:56:26 +08:00
|
|
|
|
2017-01-31 19:25:01 +08:00
|
|
|
WhitespaceManager::Change::Change(const FormatToken &Tok,
|
|
|
|
bool CreateReplacement,
|
|
|
|
SourceRange OriginalWhitespaceRange,
|
|
|
|
int Spaces, unsigned StartOfTokenColumn,
|
|
|
|
unsigned NewlinesBefore,
|
|
|
|
StringRef PreviousLinePostfix,
|
2020-04-13 22:14:26 +08:00
|
|
|
StringRef CurrentLinePrefix, bool IsAligned,
|
2017-01-31 19:25:01 +08:00
|
|
|
bool ContinuesPPDirective, bool IsInsideToken)
|
|
|
|
: Tok(&Tok), CreateReplacement(CreateReplacement),
|
2013-05-22 20:51:29 +08:00
|
|
|
OriginalWhitespaceRange(OriginalWhitespaceRange),
|
|
|
|
StartOfTokenColumn(StartOfTokenColumn), NewlinesBefore(NewlinesBefore),
|
|
|
|
PreviousLinePostfix(PreviousLinePostfix),
|
2020-04-13 22:14:26 +08:00
|
|
|
CurrentLinePrefix(CurrentLinePrefix), IsAligned(IsAligned),
|
2017-01-31 19:25:01 +08:00
|
|
|
ContinuesPPDirective(ContinuesPPDirective), Spaces(Spaces),
|
|
|
|
IsInsideToken(IsInsideToken), IsTrailingComment(false), TokenLength(0),
|
|
|
|
PreviousEndOfTokenColumn(0), EscapedNewlineColumn(0),
|
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
|
|
|
StartOfBlockComment(nullptr), IndentationOffset(0), ConditionalsLevel(0) {
|
|
|
|
}
|
2013-05-22 20:51:29 +08:00
|
|
|
|
2013-10-12 05:25:45 +08:00
|
|
|
void WhitespaceManager::replaceWhitespace(FormatToken &Tok, unsigned Newlines,
|
2017-01-31 19:25:01 +08:00
|
|
|
unsigned Spaces,
|
2013-05-22 20:51:29 +08:00
|
|
|
unsigned StartOfTokenColumn,
|
2020-04-13 22:14:26 +08:00
|
|
|
bool IsAligned, bool InPPDirective) {
|
2013-10-12 05:25:45 +08:00
|
|
|
if (Tok.Finalized)
|
|
|
|
return;
|
2020-07-28 06:19:02 +08:00
|
|
|
Tok.setDecision((Newlines > 0) ? FD_Break : FD_Continue);
|
2017-03-23 10:51:25 +08:00
|
|
|
Changes.push_back(Change(Tok, /*CreateReplacement=*/true, Tok.WhitespaceRange,
|
|
|
|
Spaces, StartOfTokenColumn, Newlines, "", "",
|
2020-04-13 22:14:26 +08:00
|
|
|
IsAligned, InPPDirective && !Tok.IsFirst,
|
2017-01-31 19:25:01 +08:00
|
|
|
/*IsInsideToken=*/false));
|
2013-05-22 20:51:29 +08:00
|
|
|
}
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
void WhitespaceManager::addUntouchableToken(const FormatToken &Tok,
|
|
|
|
bool InPPDirective) {
|
2013-10-12 05:25:45 +08:00
|
|
|
if (Tok.Finalized)
|
|
|
|
return;
|
2017-01-31 19:25:01 +08:00
|
|
|
Changes.push_back(Change(Tok, /*CreateReplacement=*/false,
|
|
|
|
Tok.WhitespaceRange, /*Spaces=*/0,
|
|
|
|
Tok.OriginalColumn, Tok.NewlinesBefore, "", "",
|
2020-04-13 22:14:26 +08:00
|
|
|
/*IsAligned=*/false, InPPDirective && !Tok.IsFirst,
|
2017-01-31 19:25:01 +08:00
|
|
|
/*IsInsideToken=*/false));
|
2013-04-15 22:28:00 +08:00
|
|
|
}
|
|
|
|
|
2017-10-30 22:01:50 +08:00
|
|
|
llvm::Error
|
|
|
|
WhitespaceManager::addReplacement(const tooling::Replacement &Replacement) {
|
|
|
|
return Replaces.add(Replacement);
|
|
|
|
}
|
|
|
|
|
2013-06-12 00:01:49 +08:00
|
|
|
void WhitespaceManager::replaceWhitespaceInToken(
|
|
|
|
const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars,
|
|
|
|
StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective,
|
2017-01-31 19:25:01 +08:00
|
|
|
unsigned Newlines, int Spaces) {
|
2013-10-12 05:25:45 +08:00
|
|
|
if (Tok.Finalized)
|
|
|
|
return;
|
2014-04-18 00:12:46 +08:00
|
|
|
SourceLocation Start = Tok.getStartOfNonWhitespace().getLocWithOffset(Offset);
|
2017-01-31 19:25:01 +08:00
|
|
|
Changes.push_back(
|
|
|
|
Change(Tok, /*CreateReplacement=*/true,
|
|
|
|
SourceRange(Start, Start.getLocWithOffset(ReplaceChars)), Spaces,
|
|
|
|
std::max(0, Spaces), Newlines, PreviousPostfix, CurrentPrefix,
|
2020-04-13 22:14:26 +08:00
|
|
|
/*IsAligned=*/true, InPPDirective && !Tok.IsFirst,
|
|
|
|
/*IsInsideToken=*/true));
|
2013-05-22 20:51:29 +08:00
|
|
|
}
|
|
|
|
|
2013-04-15 22:28:00 +08:00
|
|
|
const tooling::Replacements &WhitespaceManager::generateReplacements() {
|
2013-05-22 20:51:29 +08:00
|
|
|
if (Changes.empty())
|
|
|
|
return Replaces;
|
|
|
|
|
2018-09-27 06:16:28 +08:00
|
|
|
llvm::sort(Changes, Change::IsBeforeInFile(SourceMgr));
|
2013-05-22 20:51:29 +08:00
|
|
|
calculateLineBreakInformation();
|
2019-07-02 23:53:14 +08:00
|
|
|
alignConsecutiveMacros();
|
2015-10-01 18:06:54 +08:00
|
|
|
alignConsecutiveDeclarations();
|
2020-05-20 14:42:07 +08:00
|
|
|
alignConsecutiveBitFields();
|
2015-04-29 21:06:49 +08:00
|
|
|
alignConsecutiveAssignments();
|
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
|
|
|
alignChainedConditionals();
|
2013-05-22 20:51:29 +08:00
|
|
|
alignTrailingComments();
|
2013-04-25 16:56:26 +08:00
|
|
|
alignEscapedNewlines();
|
2013-05-22 20:51:29 +08:00
|
|
|
generateChanges();
|
|
|
|
|
2013-04-15 22:28:00 +08:00
|
|
|
return Replaces;
|
|
|
|
}
|
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
void WhitespaceManager::calculateLineBreakInformation() {
|
|
|
|
Changes[0].PreviousEndOfTokenColumn = 0;
|
2016-01-12 00:27:16 +08:00
|
|
|
Change *LastOutsideTokenChange = &Changes[0];
|
2013-05-22 20:51:29 +08:00
|
|
|
for (unsigned i = 1, e = Changes.size(); i != e; ++i) {
|
2017-06-07 22:05:06 +08:00
|
|
|
SourceLocation OriginalWhitespaceStart =
|
|
|
|
Changes[i].OriginalWhitespaceRange.getBegin();
|
|
|
|
SourceLocation PreviousOriginalWhitespaceEnd =
|
|
|
|
Changes[i - 1].OriginalWhitespaceRange.getEnd();
|
|
|
|
unsigned OriginalWhitespaceStartOffset =
|
|
|
|
SourceMgr.getFileOffset(OriginalWhitespaceStart);
|
|
|
|
unsigned PreviousOriginalWhitespaceEndOffset =
|
|
|
|
SourceMgr.getFileOffset(PreviousOriginalWhitespaceEnd);
|
|
|
|
assert(PreviousOriginalWhitespaceEndOffset <=
|
|
|
|
OriginalWhitespaceStartOffset);
|
|
|
|
const char *const PreviousOriginalWhitespaceEndData =
|
|
|
|
SourceMgr.getCharacterData(PreviousOriginalWhitespaceEnd);
|
|
|
|
StringRef Text(PreviousOriginalWhitespaceEndData,
|
|
|
|
SourceMgr.getCharacterData(OriginalWhitespaceStart) -
|
|
|
|
PreviousOriginalWhitespaceEndData);
|
|
|
|
// Usually consecutive changes would occur in consecutive tokens. This is
|
|
|
|
// not the case however when analyzing some preprocessor runs of the
|
|
|
|
// annotated lines. For example, in this code:
|
|
|
|
//
|
|
|
|
// #if A // line 1
|
|
|
|
// int i = 1;
|
|
|
|
// #else B // line 2
|
|
|
|
// int i = 2;
|
|
|
|
// #endif // line 3
|
|
|
|
//
|
|
|
|
// one of the runs will produce the sequence of lines marked with line 1, 2
|
|
|
|
// and 3. So the two consecutive whitespace changes just before '// line 2'
|
|
|
|
// and before '#endif // line 3' span multiple lines and tokens:
|
|
|
|
//
|
|
|
|
// #else B{change X}[// line 2
|
|
|
|
// int i = 2;
|
|
|
|
// ]{change Y}#endif // line 3
|
|
|
|
//
|
|
|
|
// For this reason, if the text between consecutive changes spans multiple
|
|
|
|
// newlines, the token length must be adjusted to the end of the original
|
|
|
|
// line of the token.
|
|
|
|
auto NewlinePos = Text.find_first_of('\n');
|
|
|
|
if (NewlinePos == StringRef::npos) {
|
|
|
|
Changes[i - 1].TokenLength = OriginalWhitespaceStartOffset -
|
|
|
|
PreviousOriginalWhitespaceEndOffset +
|
|
|
|
Changes[i].PreviousLinePostfix.size() +
|
|
|
|
Changes[i - 1].CurrentLinePrefix.size();
|
|
|
|
} else {
|
|
|
|
Changes[i - 1].TokenLength =
|
|
|
|
NewlinePos + Changes[i - 1].CurrentLinePrefix.size();
|
|
|
|
}
|
2013-05-22 20:51:29 +08:00
|
|
|
|
2016-01-12 00:27:16 +08:00
|
|
|
// If there are multiple changes in this token, sum up all the changes until
|
|
|
|
// the end of the line.
|
2017-06-05 03:27:02 +08:00
|
|
|
if (Changes[i - 1].IsInsideToken && Changes[i - 1].NewlinesBefore == 0)
|
2016-01-12 00:27:16 +08:00
|
|
|
LastOutsideTokenChange->TokenLength +=
|
|
|
|
Changes[i - 1].TokenLength + Changes[i - 1].Spaces;
|
|
|
|
else
|
|
|
|
LastOutsideTokenChange = &Changes[i - 1];
|
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
Changes[i].PreviousEndOfTokenColumn =
|
|
|
|
Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength;
|
|
|
|
|
|
|
|
Changes[i - 1].IsTrailingComment =
|
2017-01-31 19:25:01 +08:00
|
|
|
(Changes[i].NewlinesBefore > 0 || Changes[i].Tok->is(tok::eof) ||
|
|
|
|
(Changes[i].IsInsideToken && Changes[i].Tok->is(tok::comment))) &&
|
|
|
|
Changes[i - 1].Tok->is(tok::comment) &&
|
2017-01-25 21:58:58 +08:00
|
|
|
// FIXME: This is a dirty hack. The problem is that
|
|
|
|
// BreakableLineCommentSection does comment reflow changes and here is
|
|
|
|
// the aligning of trailing comments. Consider the case where we reflow
|
|
|
|
// the second line up in this example:
|
2017-09-20 17:51:03 +08:00
|
|
|
//
|
2017-01-25 21:58:58 +08:00
|
|
|
// // line 1
|
|
|
|
// // line 2
|
2017-09-20 17:51:03 +08:00
|
|
|
//
|
2017-01-25 21:58:58 +08:00
|
|
|
// That amounts to 2 changes by BreakableLineCommentSection:
|
|
|
|
// - the first, delimited by (), for the whitespace between the tokens,
|
|
|
|
// - and second, delimited by [], for the whitespace at the beginning
|
|
|
|
// of the second token:
|
2017-09-20 17:51:03 +08:00
|
|
|
//
|
2017-01-25 21:58:58 +08:00
|
|
|
// // line 1(
|
|
|
|
// )[// ]line 2
|
|
|
|
//
|
|
|
|
// So in the end we have two changes like this:
|
|
|
|
//
|
|
|
|
// // line1()[ ]line 2
|
|
|
|
//
|
|
|
|
// Note that the OriginalWhitespaceStart of the second change is the
|
|
|
|
// same as the PreviousOriginalWhitespaceEnd of the first change.
|
|
|
|
// In this case, the below check ensures that the second change doesn't
|
|
|
|
// get treated as a trailing comment change here, since this might
|
|
|
|
// trigger additional whitespace to be wrongly inserted before "line 2"
|
|
|
|
// by the comment aligner here.
|
|
|
|
//
|
|
|
|
// For a proper solution we need a mechanism to say to WhitespaceManager
|
|
|
|
// that a particular change breaks the current sequence of trailing
|
|
|
|
// comments.
|
|
|
|
OriginalWhitespaceStart != PreviousOriginalWhitespaceEnd;
|
2013-05-22 20:51:29 +08:00
|
|
|
}
|
2013-05-22 22:01:08 +08:00
|
|
|
// FIXME: The last token is currently not always an eof token; in those
|
|
|
|
// cases, setting TokenLength of the last token to 0 is wrong.
|
|
|
|
Changes.back().TokenLength = 0;
|
2017-01-31 19:25:01 +08:00
|
|
|
Changes.back().IsTrailingComment = Changes.back().Tok->is(tok::comment);
|
2014-04-18 00:12:46 +08:00
|
|
|
|
|
|
|
const WhitespaceManager::Change *LastBlockComment = nullptr;
|
|
|
|
for (auto &Change : Changes) {
|
2016-01-12 00:27:16 +08:00
|
|
|
// Reset the IsTrailingComment flag for changes inside of trailing comments
|
2017-02-03 18:18:25 +08:00
|
|
|
// so they don't get realigned later. Comment line breaks however still need
|
|
|
|
// to be aligned.
|
|
|
|
if (Change.IsInsideToken && Change.NewlinesBefore == 0)
|
2016-01-12 00:27:16 +08:00
|
|
|
Change.IsTrailingComment = false;
|
2014-04-18 00:12:46 +08:00
|
|
|
Change.StartOfBlockComment = nullptr;
|
|
|
|
Change.IndentationOffset = 0;
|
2017-01-31 19:25:01 +08:00
|
|
|
if (Change.Tok->is(tok::comment)) {
|
|
|
|
if (Change.Tok->is(TT_LineComment) || !Change.IsInsideToken)
|
|
|
|
LastBlockComment = &Change;
|
|
|
|
else {
|
|
|
|
if ((Change.StartOfBlockComment = LastBlockComment))
|
|
|
|
Change.IndentationOffset =
|
|
|
|
Change.StartOfTokenColumn -
|
|
|
|
Change.StartOfBlockComment->StartOfTokenColumn;
|
|
|
|
}
|
2014-04-18 00:12:46 +08:00
|
|
|
} else {
|
|
|
|
LastBlockComment = nullptr;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
|
|
|
// Compute conditional nesting level
|
|
|
|
// Level is increased for each conditional, unless this conditional continues
|
|
|
|
// a chain of conditional, i.e. starts immediately after the colon of another
|
|
|
|
// conditional.
|
|
|
|
SmallVector<bool, 16> ScopeStack;
|
|
|
|
int ConditionalsLevel = 0;
|
|
|
|
for (auto &Change : Changes) {
|
|
|
|
for (unsigned i = 0, e = Change.Tok->FakeLParens.size(); i != e; ++i) {
|
|
|
|
bool isNestedConditional =
|
|
|
|
Change.Tok->FakeLParens[e - 1 - i] == prec::Conditional &&
|
|
|
|
!(i == 0 && Change.Tok->Previous &&
|
|
|
|
Change.Tok->Previous->is(TT_ConditionalExpr) &&
|
|
|
|
Change.Tok->Previous->is(tok::colon));
|
|
|
|
if (isNestedConditional)
|
|
|
|
++ConditionalsLevel;
|
|
|
|
ScopeStack.push_back(isNestedConditional);
|
|
|
|
}
|
|
|
|
|
|
|
|
Change.ConditionalsLevel = ConditionalsLevel;
|
|
|
|
|
|
|
|
for (unsigned i = Change.Tok->FakeRParens; i > 0 && ScopeStack.size();
|
|
|
|
--i) {
|
|
|
|
if (ScopeStack.pop_back_val())
|
|
|
|
--ConditionalsLevel;
|
|
|
|
}
|
|
|
|
}
|
2013-05-22 20:51:29 +08:00
|
|
|
}
|
|
|
|
|
2015-12-01 20:00:43 +08:00
|
|
|
// Align a single sequence of tokens, see AlignTokens below.
|
|
|
|
template <typename F>
|
|
|
|
static void
|
|
|
|
AlignTokenSequence(unsigned Start, unsigned End, unsigned Column, F &&Matches,
|
|
|
|
SmallVector<WhitespaceManager::Change, 16> &Changes) {
|
|
|
|
bool FoundMatchOnLine = false;
|
2015-09-22 17:32:00 +08:00
|
|
|
int Shift = 0;
|
2017-03-23 10:51:25 +08:00
|
|
|
|
|
|
|
// ScopeStack keeps track of the current scope depth. It contains indices of
|
|
|
|
// the first token on each scope.
|
|
|
|
// We only run the "Matches" function on tokens from the outer-most scope.
|
|
|
|
// However, we do need to pay special attention to one class of tokens
|
|
|
|
// that are not in the outer-most scope, and that is function parameters
|
|
|
|
// which are split across multiple lines, as illustrated by this example:
|
|
|
|
// double a(int x);
|
|
|
|
// int b(int y,
|
|
|
|
// double z);
|
|
|
|
// In the above example, we need to take special care to ensure that
|
|
|
|
// 'double z' is indented along with it's owning function 'b'.
|
2021-03-11 05:08:07 +08:00
|
|
|
// The same holds for calling a function:
|
|
|
|
// double a = foo(x);
|
|
|
|
// int b = bar(foo(y),
|
|
|
|
// foor(z));
|
|
|
|
// Similar for broken string literals:
|
|
|
|
// double x = 3.14;
|
|
|
|
// auto s = "Hello"
|
|
|
|
// "World";
|
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
|
|
|
// Special handling is required for 'nested' ternary operators.
|
2017-03-23 10:51:25 +08:00
|
|
|
SmallVector<unsigned, 16> ScopeStack;
|
|
|
|
|
2015-04-29 21:06:49 +08:00
|
|
|
for (unsigned i = Start; i != End; ++i) {
|
2017-03-23 10:51:25 +08:00
|
|
|
if (ScopeStack.size() != 0 &&
|
2017-08-26 03:14:53 +08:00
|
|
|
Changes[i].indentAndNestingLevel() <
|
|
|
|
Changes[ScopeStack.back()].indentAndNestingLevel())
|
2017-03-23 10:51:25 +08:00
|
|
|
ScopeStack.pop_back();
|
|
|
|
|
2018-08-01 23:32:56 +08:00
|
|
|
// Compare current token to previous non-comment token to ensure whether
|
|
|
|
// it is in a deeper scope or not.
|
|
|
|
unsigned PreviousNonComment = i - 1;
|
|
|
|
while (PreviousNonComment > Start &&
|
|
|
|
Changes[PreviousNonComment].Tok->is(tok::comment))
|
|
|
|
PreviousNonComment--;
|
2017-08-26 03:14:53 +08:00
|
|
|
if (i != Start && Changes[i].indentAndNestingLevel() >
|
2018-08-01 23:32:56 +08:00
|
|
|
Changes[PreviousNonComment].indentAndNestingLevel())
|
2017-03-23 10:51:25 +08:00
|
|
|
ScopeStack.push_back(i);
|
|
|
|
|
|
|
|
bool InsideNestedScope = ScopeStack.size() != 0;
|
2021-03-11 05:08:07 +08:00
|
|
|
bool ContinuedStringLiteral = i > Start &&
|
|
|
|
Changes[i].Tok->is(tok::string_literal) &&
|
|
|
|
Changes[i - 1].Tok->is(tok::string_literal);
|
|
|
|
bool SkipMatchCheck = InsideNestedScope || ContinuedStringLiteral;
|
2017-03-23 10:51:25 +08:00
|
|
|
|
2021-03-11 05:08:07 +08:00
|
|
|
if (Changes[i].NewlinesBefore > 0 && !SkipMatchCheck) {
|
2015-09-22 17:32:00 +08:00
|
|
|
Shift = 0;
|
2017-03-23 10:51:25 +08:00
|
|
|
FoundMatchOnLine = false;
|
2015-09-22 17:32:00 +08:00
|
|
|
}
|
|
|
|
|
2015-12-01 20:00:43 +08:00
|
|
|
// If this is the first matching token to be aligned, remember by how many
|
2015-09-22 17:32:00 +08:00
|
|
|
// spaces it has to be shifted, so the rest of the changes on the line are
|
|
|
|
// shifted by the same amount
|
2021-03-11 05:08:07 +08:00
|
|
|
if (!FoundMatchOnLine && !SkipMatchCheck && Matches(Changes[i])) {
|
2015-12-01 20:00:43 +08:00
|
|
|
FoundMatchOnLine = true;
|
2015-04-29 21:06:49 +08:00
|
|
|
Shift = Column - Changes[i].StartOfTokenColumn;
|
2015-09-22 17:32:00 +08:00
|
|
|
Changes[i].Spaces += Shift;
|
2015-04-29 21:06:49 +08:00
|
|
|
}
|
2015-09-22 17:32:00 +08:00
|
|
|
|
2017-03-23 10:51:25 +08:00
|
|
|
// This is for function parameters that are split across multiple lines,
|
|
|
|
// as mentioned in the ScopeStack comment.
|
|
|
|
if (InsideNestedScope && Changes[i].NewlinesBefore > 0) {
|
|
|
|
unsigned ScopeStart = ScopeStack.back();
|
2021-03-11 05:08:07 +08:00
|
|
|
auto ShouldShiftBeAdded = [&] {
|
|
|
|
// Function declaration
|
|
|
|
if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Continued function declaration
|
|
|
|
if (ScopeStart > Start + 1 &&
|
|
|
|
Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Continued function call
|
|
|
|
if (ScopeStart > Start + 1 &&
|
|
|
|
Changes[ScopeStart - 2].Tok->is(tok::identifier) &&
|
|
|
|
Changes[ScopeStart - 1].Tok->is(tok::l_paren))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Ternary operator
|
|
|
|
if (Changes[i].Tok->is(TT_ConditionalExpr))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Continued ternary operator
|
|
|
|
if (Changes[i].Tok->Previous &&
|
|
|
|
Changes[i].Tok->Previous->is(TT_ConditionalExpr))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (ShouldShiftBeAdded())
|
2017-03-23 10:51:25 +08:00
|
|
|
Changes[i].Spaces += Shift;
|
|
|
|
}
|
|
|
|
|
2021-03-11 05:08:07 +08:00
|
|
|
if (ContinuedStringLiteral)
|
|
|
|
Changes[i].Spaces += Shift;
|
|
|
|
|
2015-04-29 21:06:49 +08:00
|
|
|
assert(Shift >= 0);
|
2015-09-22 17:32:00 +08:00
|
|
|
Changes[i].StartOfTokenColumn += Shift;
|
2015-04-29 21:06:49 +08:00
|
|
|
if (i + 1 != Changes.size())
|
|
|
|
Changes[i + 1].PreviousEndOfTokenColumn += Shift;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-23 10:51:25 +08:00
|
|
|
// Walk through a subset of the changes, starting at StartAt, and find
|
|
|
|
// sequences of matching tokens to align. To do so, keep track of the lines and
|
|
|
|
// whether or not a matching token was found on a line. If a matching token is
|
|
|
|
// found, extend the current sequence. If the current line cannot be part of a
|
|
|
|
// sequence, e.g. because there is an empty line before it or it contains only
|
|
|
|
// non-matching tokens, finalize the previous sequence.
|
|
|
|
// The value returned is the token on which we stopped, either because we
|
|
|
|
// exhausted all items inside Changes, or because we hit a scope level higher
|
|
|
|
// than our initial scope.
|
|
|
|
// This function is recursive. Each invocation processes only the scope level
|
|
|
|
// equal to the initial level, which is the level of Changes[StartAt].
|
|
|
|
// If we encounter a scope level greater than the initial level, then we call
|
|
|
|
// ourselves recursively, thereby avoiding the pollution of the current state
|
|
|
|
// with the alignment requirements of the nested sub-level. This recursive
|
|
|
|
// behavior is necessary for aligning function prototypes that have one or more
|
|
|
|
// arguments.
|
|
|
|
// If this function encounters a scope level less than the initial level,
|
|
|
|
// it returns the current position.
|
|
|
|
// There is a non-obvious subtlety in the recursive behavior: Even though we
|
|
|
|
// defer processing of nested levels to recursive invocations of this
|
|
|
|
// function, when it comes time to align a sequence of tokens, we run the
|
|
|
|
// alignment on the entire sequence, including the nested levels.
|
|
|
|
// When doing so, most of the nested tokens are skipped, because their
|
|
|
|
// alignment was already handled by the recursive invocations of this function.
|
|
|
|
// However, the special exception is that we do NOT skip function parameters
|
|
|
|
// that are split across multiple lines. See the test case in FormatTest.cpp
|
|
|
|
// that mentions "split function parameter alignment" for an example of this.
|
2015-12-01 20:00:43 +08:00
|
|
|
template <typename F>
|
2021-01-25 16:30:02 +08:00
|
|
|
static unsigned AlignTokens(
|
|
|
|
const FormatStyle &Style, F &&Matches,
|
|
|
|
SmallVector<WhitespaceManager::Change, 16> &Changes, unsigned StartAt,
|
|
|
|
const FormatStyle::AlignConsecutiveStyle &ACS = FormatStyle::ACS_None) {
|
2015-10-01 18:06:54 +08:00
|
|
|
unsigned MinColumn = 0;
|
|
|
|
unsigned MaxColumn = UINT_MAX;
|
2015-12-01 20:00:43 +08:00
|
|
|
|
|
|
|
// Line number of the start and the end of the current token sequence.
|
2015-10-01 18:06:54 +08:00
|
|
|
unsigned StartOfSequence = 0;
|
|
|
|
unsigned EndOfSequence = 0;
|
|
|
|
|
2017-03-23 10:51:25 +08:00
|
|
|
// Measure the scope level (i.e. depth of (), [], {}) of the first token, and
|
|
|
|
// abort when we hit any token in a higher scope than the starting one.
|
2017-08-26 03:14:53 +08:00
|
|
|
auto IndentAndNestingLevel = StartAt < Changes.size()
|
|
|
|
? Changes[StartAt].indentAndNestingLevel()
|
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
|
|
|
: std::tuple<unsigned, unsigned, unsigned>();
|
2015-12-01 20:00:43 +08:00
|
|
|
|
|
|
|
// Keep track of the number of commas before the matching tokens, we will only
|
|
|
|
// align a sequence of matching tokens if they are preceded by the same number
|
|
|
|
// of commas.
|
|
|
|
unsigned CommasBeforeLastMatch = 0;
|
|
|
|
unsigned CommasBeforeMatch = 0;
|
|
|
|
|
|
|
|
// Whether a matching token has been found on the current line.
|
|
|
|
bool FoundMatchOnLine = false;
|
|
|
|
|
2021-01-25 16:30:02 +08:00
|
|
|
// Whether the current line consists purely of comments.
|
|
|
|
bool LineIsComment = true;
|
|
|
|
|
2015-12-01 20:00:43 +08:00
|
|
|
// Aligns a sequence of matching tokens, on the MinColumn column.
|
|
|
|
//
|
|
|
|
// Sequences start from the first matching token to align, and end at the
|
|
|
|
// first token of the first line that doesn't need to be aligned.
|
|
|
|
//
|
|
|
|
// We need to adjust the StartOfTokenColumn of each Change that is on a line
|
|
|
|
// containing any matching token to be aligned and located after such token.
|
|
|
|
auto AlignCurrentSequence = [&] {
|
2015-10-01 18:06:54 +08:00
|
|
|
if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
|
2015-12-01 20:00:43 +08:00
|
|
|
AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
|
|
|
|
Changes);
|
2015-10-01 18:06:54 +08:00
|
|
|
MinColumn = 0;
|
|
|
|
MaxColumn = UINT_MAX;
|
|
|
|
StartOfSequence = 0;
|
|
|
|
EndOfSequence = 0;
|
|
|
|
};
|
|
|
|
|
2017-03-23 10:51:25 +08:00
|
|
|
unsigned i = StartAt;
|
|
|
|
for (unsigned e = Changes.size(); i != e; ++i) {
|
2017-08-26 03:14:53 +08:00
|
|
|
if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
|
2017-03-23 10:51:25 +08:00
|
|
|
break;
|
|
|
|
|
2015-10-01 18:06:54 +08:00
|
|
|
if (Changes[i].NewlinesBefore != 0) {
|
2015-12-01 20:00:43 +08:00
|
|
|
CommasBeforeMatch = 0;
|
2015-10-01 18:06:54 +08:00
|
|
|
EndOfSequence = i;
|
2021-01-25 16:30:02 +08:00
|
|
|
|
|
|
|
// Whether to break the alignment sequence because of an empty line.
|
|
|
|
bool EmptyLineBreak =
|
|
|
|
(Changes[i].NewlinesBefore > 1) &&
|
|
|
|
(ACS != FormatStyle::ACS_AcrossEmptyLines) &&
|
|
|
|
(ACS != FormatStyle::ACS_AcrossEmptyLinesAndComments);
|
|
|
|
|
|
|
|
// Whether to break the alignment sequence because of a line without a
|
|
|
|
// match.
|
|
|
|
bool NoMatchBreak =
|
|
|
|
!FoundMatchOnLine &&
|
|
|
|
!(LineIsComment &&
|
|
|
|
((ACS == FormatStyle::ACS_AcrossComments) ||
|
|
|
|
(ACS == FormatStyle::ACS_AcrossEmptyLinesAndComments)));
|
|
|
|
|
|
|
|
if (EmptyLineBreak || NoMatchBreak)
|
2015-12-01 20:00:43 +08:00
|
|
|
AlignCurrentSequence();
|
|
|
|
|
2021-01-25 16:30:02 +08:00
|
|
|
// A new line starts, re-initialize line status tracking bools.
|
2021-03-11 05:08:07 +08:00
|
|
|
// Keep the match state if a string literal is continued on this line.
|
|
|
|
if (i == 0 || !Changes[i].Tok->is(tok::string_literal) ||
|
|
|
|
!Changes[i - 1].Tok->is(tok::string_literal))
|
|
|
|
FoundMatchOnLine = false;
|
2021-01-25 16:30:02 +08:00
|
|
|
LineIsComment = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Changes[i].Tok->is(tok::comment)) {
|
|
|
|
LineIsComment = false;
|
2015-10-01 18:06:54 +08:00
|
|
|
}
|
|
|
|
|
2017-01-31 19:25:01 +08:00
|
|
|
if (Changes[i].Tok->is(tok::comma)) {
|
2015-12-01 20:00:43 +08:00
|
|
|
++CommasBeforeMatch;
|
2017-08-26 03:14:53 +08:00
|
|
|
} else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
|
2017-03-23 10:51:25 +08:00
|
|
|
// Call AlignTokens recursively, skipping over this scope block.
|
2021-01-25 16:30:02 +08:00
|
|
|
unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i, ACS);
|
2017-03-23 10:51:25 +08:00
|
|
|
i = StoppedAt - 1;
|
|
|
|
continue;
|
2015-10-01 18:06:54 +08:00
|
|
|
}
|
2015-12-01 20:00:43 +08:00
|
|
|
|
|
|
|
if (!Matches(Changes[i]))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If there is more than one matching token per line, or if the number of
|
2017-03-23 10:51:25 +08:00
|
|
|
// preceding commas, do not match anymore, end the sequence.
|
|
|
|
if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
|
2015-12-01 20:00:43 +08:00
|
|
|
AlignCurrentSequence();
|
|
|
|
|
|
|
|
CommasBeforeLastMatch = CommasBeforeMatch;
|
|
|
|
FoundMatchOnLine = true;
|
|
|
|
|
|
|
|
if (StartOfSequence == 0)
|
|
|
|
StartOfSequence = i;
|
|
|
|
|
|
|
|
unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
|
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
|
|
|
int LineLengthAfter = Changes[i].TokenLength;
|
2020-05-20 14:22:01 +08:00
|
|
|
for (unsigned j = i + 1; j != e && Changes[j].NewlinesBefore == 0; ++j) {
|
|
|
|
LineLengthAfter += Changes[j].Spaces;
|
|
|
|
// Changes are generally 1:1 with the tokens, but a change could also be
|
|
|
|
// inside of a token, in which case it's counted more than once: once for
|
|
|
|
// the whitespace surrounding the token (!IsInsideToken) and once for
|
|
|
|
// each whitespace change within it (IsInsideToken).
|
|
|
|
// Therefore, changes inside of a token should only count the space.
|
|
|
|
if (!Changes[j].IsInsideToken)
|
|
|
|
LineLengthAfter += Changes[j].TokenLength;
|
|
|
|
}
|
2015-12-01 20:00:43 +08:00
|
|
|
unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
|
|
|
|
|
|
|
|
// If we are restricted by the maximum column width, end the sequence.
|
|
|
|
if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
|
|
|
|
CommasBeforeLastMatch != CommasBeforeMatch) {
|
|
|
|
AlignCurrentSequence();
|
|
|
|
StartOfSequence = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
MinColumn = std::max(MinColumn, ChangeMinColumn);
|
|
|
|
MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
|
2015-10-01 18:06:54 +08:00
|
|
|
}
|
|
|
|
|
2017-03-23 10:51:25 +08:00
|
|
|
EndOfSequence = i;
|
2015-12-01 20:00:43 +08:00
|
|
|
AlignCurrentSequence();
|
2017-03-23 10:51:25 +08:00
|
|
|
return i;
|
2015-10-01 18:06:54 +08:00
|
|
|
}
|
|
|
|
|
2019-07-02 23:53:14 +08:00
|
|
|
// Aligns a sequence of matching tokens, on the MinColumn column.
|
|
|
|
//
|
|
|
|
// Sequences start from the first matching token to align, and end at the
|
|
|
|
// first token of the first line that doesn't need to be aligned.
|
|
|
|
//
|
|
|
|
// We need to adjust the StartOfTokenColumn of each Change that is on a line
|
|
|
|
// containing any matching token to be aligned and located after such token.
|
|
|
|
static void AlignMacroSequence(
|
|
|
|
unsigned &StartOfSequence, unsigned &EndOfSequence, unsigned &MinColumn,
|
|
|
|
unsigned &MaxColumn, bool &FoundMatchOnLine,
|
|
|
|
std::function<bool(const WhitespaceManager::Change &C)> AlignMacrosMatches,
|
|
|
|
SmallVector<WhitespaceManager::Change, 16> &Changes) {
|
|
|
|
if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) {
|
|
|
|
|
|
|
|
FoundMatchOnLine = false;
|
|
|
|
int Shift = 0;
|
|
|
|
|
|
|
|
for (unsigned I = StartOfSequence; I != EndOfSequence; ++I) {
|
|
|
|
if (Changes[I].NewlinesBefore > 0) {
|
|
|
|
Shift = 0;
|
|
|
|
FoundMatchOnLine = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is the first matching token to be aligned, remember by how many
|
|
|
|
// spaces it has to be shifted, so the rest of the changes on the line are
|
|
|
|
// shifted by the same amount
|
|
|
|
if (!FoundMatchOnLine && AlignMacrosMatches(Changes[I])) {
|
|
|
|
FoundMatchOnLine = true;
|
|
|
|
Shift = MinColumn - Changes[I].StartOfTokenColumn;
|
|
|
|
Changes[I].Spaces += Shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(Shift >= 0);
|
|
|
|
Changes[I].StartOfTokenColumn += Shift;
|
|
|
|
if (I + 1 != Changes.size())
|
|
|
|
Changes[I + 1].PreviousEndOfTokenColumn += Shift;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MinColumn = 0;
|
|
|
|
MaxColumn = UINT_MAX;
|
|
|
|
StartOfSequence = 0;
|
|
|
|
EndOfSequence = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WhitespaceManager::alignConsecutiveMacros() {
|
2021-01-25 16:30:02 +08:00
|
|
|
if (Style.AlignConsecutiveMacros == FormatStyle::ACS_None)
|
2019-07-02 23:53:14 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
auto AlignMacrosMatches = [](const Change &C) {
|
|
|
|
const FormatToken *Current = C.Tok;
|
|
|
|
unsigned SpacesRequiredBefore = 1;
|
|
|
|
|
|
|
|
if (Current->SpacesRequiredBefore == 0 || !Current->Previous)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Current = Current->Previous;
|
|
|
|
|
|
|
|
// If token is a ")", skip over the parameter list, to the
|
|
|
|
// token that precedes the "("
|
|
|
|
if (Current->is(tok::r_paren) && Current->MatchingParen) {
|
|
|
|
Current = Current->MatchingParen->Previous;
|
|
|
|
SpacesRequiredBefore = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Current || !Current->is(tok::identifier))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!Current->Previous || !Current->Previous->is(tok::pp_define))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// For a macro function, 0 spaces are required between the
|
|
|
|
// identifier and the lparen that opens the parameter list.
|
|
|
|
// For a simple macro, 1 space is required between the
|
|
|
|
// identifier and the first token of the defined value.
|
|
|
|
return Current->Next->SpacesRequiredBefore == SpacesRequiredBefore;
|
|
|
|
};
|
|
|
|
|
|
|
|
unsigned MinColumn = 0;
|
|
|
|
unsigned MaxColumn = UINT_MAX;
|
|
|
|
|
|
|
|
// Start and end of the token sequence we're processing.
|
|
|
|
unsigned StartOfSequence = 0;
|
|
|
|
unsigned EndOfSequence = 0;
|
|
|
|
|
|
|
|
// Whether a matching token has been found on the current line.
|
|
|
|
bool FoundMatchOnLine = false;
|
|
|
|
|
2021-01-25 16:30:02 +08:00
|
|
|
// Whether the current line consists only of comments
|
|
|
|
bool LineIsComment = true;
|
|
|
|
|
2019-07-02 23:53:14 +08:00
|
|
|
unsigned I = 0;
|
|
|
|
for (unsigned E = Changes.size(); I != E; ++I) {
|
|
|
|
if (Changes[I].NewlinesBefore != 0) {
|
|
|
|
EndOfSequence = I;
|
2021-01-25 16:30:02 +08:00
|
|
|
|
|
|
|
// Whether to break the alignment sequence because of an empty line.
|
|
|
|
bool EmptyLineBreak =
|
|
|
|
(Changes[I].NewlinesBefore > 1) &&
|
|
|
|
(Style.AlignConsecutiveMacros != FormatStyle::ACS_AcrossEmptyLines) &&
|
|
|
|
(Style.AlignConsecutiveMacros !=
|
|
|
|
FormatStyle::ACS_AcrossEmptyLinesAndComments);
|
|
|
|
|
|
|
|
// Whether to break the alignment sequence because of a line without a
|
|
|
|
// match.
|
|
|
|
bool NoMatchBreak =
|
|
|
|
!FoundMatchOnLine &&
|
|
|
|
!(LineIsComment && ((Style.AlignConsecutiveMacros ==
|
|
|
|
FormatStyle::ACS_AcrossComments) ||
|
|
|
|
(Style.AlignConsecutiveMacros ==
|
|
|
|
FormatStyle::ACS_AcrossEmptyLinesAndComments)));
|
|
|
|
|
|
|
|
if (EmptyLineBreak || NoMatchBreak)
|
2019-07-02 23:53:14 +08:00
|
|
|
AlignMacroSequence(StartOfSequence, EndOfSequence, MinColumn, MaxColumn,
|
|
|
|
FoundMatchOnLine, AlignMacrosMatches, Changes);
|
|
|
|
|
2021-01-25 16:30:02 +08:00
|
|
|
// A new line starts, re-initialize line status tracking bools.
|
2019-07-02 23:53:14 +08:00
|
|
|
FoundMatchOnLine = false;
|
2021-01-25 16:30:02 +08:00
|
|
|
LineIsComment = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Changes[I].Tok->is(tok::comment)) {
|
|
|
|
LineIsComment = false;
|
2019-07-02 23:53:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!AlignMacrosMatches(Changes[I]))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
FoundMatchOnLine = true;
|
|
|
|
|
|
|
|
if (StartOfSequence == 0)
|
|
|
|
StartOfSequence = I;
|
|
|
|
|
|
|
|
unsigned ChangeMinColumn = Changes[I].StartOfTokenColumn;
|
|
|
|
int LineLengthAfter = -Changes[I].Spaces;
|
|
|
|
for (unsigned j = I; j != E && Changes[j].NewlinesBefore == 0; ++j)
|
|
|
|
LineLengthAfter += Changes[j].Spaces + Changes[j].TokenLength;
|
|
|
|
unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
|
|
|
|
|
|
|
|
MinColumn = std::max(MinColumn, ChangeMinColumn);
|
|
|
|
MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
|
|
|
|
}
|
|
|
|
|
|
|
|
EndOfSequence = I;
|
|
|
|
AlignMacroSequence(StartOfSequence, EndOfSequence, MinColumn, MaxColumn,
|
|
|
|
FoundMatchOnLine, AlignMacrosMatches, Changes);
|
|
|
|
}
|
|
|
|
|
2015-12-01 20:00:43 +08:00
|
|
|
void WhitespaceManager::alignConsecutiveAssignments() {
|
2021-01-25 16:30:02 +08:00
|
|
|
if (Style.AlignConsecutiveAssignments == FormatStyle::ACS_None)
|
2015-12-01 20:00:43 +08:00
|
|
|
return;
|
2015-10-01 18:06:54 +08:00
|
|
|
|
2019-03-01 17:09:54 +08:00
|
|
|
AlignTokens(
|
|
|
|
Style,
|
|
|
|
[&](const Change &C) {
|
|
|
|
// Do not align on equal signs that are first on a line.
|
|
|
|
if (C.NewlinesBefore > 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Do not align on equal signs that are last on a line.
|
|
|
|
if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return C.Tok->is(tok::equal);
|
|
|
|
},
|
2021-01-25 16:30:02 +08:00
|
|
|
Changes, /*StartAt=*/0, Style.AlignConsecutiveAssignments);
|
2015-12-01 20:00:43 +08:00
|
|
|
}
|
|
|
|
|
2020-05-20 14:42:07 +08:00
|
|
|
void WhitespaceManager::alignConsecutiveBitFields() {
|
2021-01-25 16:30:02 +08:00
|
|
|
if (Style.AlignConsecutiveBitFields == FormatStyle::ACS_None)
|
2020-05-20 14:42:07 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
AlignTokens(
|
|
|
|
Style,
|
|
|
|
[&](Change const &C) {
|
|
|
|
// Do not align on ':' that is first on a line.
|
|
|
|
if (C.NewlinesBefore > 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Do not align on ':' that is last on a line.
|
|
|
|
if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return C.Tok->is(TT_BitFieldColon);
|
|
|
|
},
|
2021-01-25 16:30:02 +08:00
|
|
|
Changes, /*StartAt=*/0, Style.AlignConsecutiveBitFields);
|
2020-05-20 14:42:07 +08:00
|
|
|
}
|
|
|
|
|
2015-12-01 20:00:43 +08:00
|
|
|
void WhitespaceManager::alignConsecutiveDeclarations() {
|
2021-01-25 16:30:02 +08:00
|
|
|
if (Style.AlignConsecutiveDeclarations == FormatStyle::ACS_None)
|
2015-12-01 20:00:43 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// FIXME: Currently we don't handle properly the PointerAlignment: Right
|
|
|
|
// The * and & are not aligned and are left dangling. Something has to be done
|
|
|
|
// about it, but it raises the question of alignment of code like:
|
|
|
|
// const char* const* v1;
|
|
|
|
// float const* v2;
|
|
|
|
// SomeVeryLongType const& v3;
|
2019-03-01 17:09:54 +08:00
|
|
|
AlignTokens(
|
|
|
|
Style,
|
|
|
|
[](Change const &C) {
|
|
|
|
// tok::kw_operator is necessary for aligning operator overload
|
|
|
|
// definitions.
|
2019-05-02 02:23:44 +08:00
|
|
|
if (C.Tok->isOneOf(TT_FunctionDeclarationName, tok::kw_operator))
|
|
|
|
return true;
|
|
|
|
if (C.Tok->isNot(TT_StartOfName))
|
|
|
|
return false;
|
2020-12-24 05:03:39 +08:00
|
|
|
if (C.Tok->Previous &&
|
|
|
|
C.Tok->Previous->is(TT_StatementAttributeLikeMacro))
|
|
|
|
return false;
|
2019-05-02 02:23:44 +08:00
|
|
|
// Check if there is a subsequent name that starts the same declaration.
|
|
|
|
for (FormatToken *Next = C.Tok->Next; Next; Next = Next->Next) {
|
|
|
|
if (Next->is(tok::comment))
|
|
|
|
continue;
|
2021-02-28 05:40:57 +08:00
|
|
|
if (Next->is(TT_PointerOrReference))
|
|
|
|
return false;
|
2019-05-02 02:23:44 +08:00
|
|
|
if (!Next->Tok.getIdentifierInfo())
|
|
|
|
break;
|
|
|
|
if (Next->isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
|
|
|
|
tok::kw_operator))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2019-03-01 17:09:54 +08:00
|
|
|
},
|
2021-01-25 16:30:02 +08:00
|
|
|
Changes, /*StartAt=*/0, Style.AlignConsecutiveDeclarations);
|
2015-10-01 18:06:54 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
void WhitespaceManager::alignChainedConditionals() {
|
|
|
|
if (Style.BreakBeforeTernaryOperators) {
|
|
|
|
AlignTokens(
|
|
|
|
Style,
|
|
|
|
[](Change const &C) {
|
|
|
|
// Align question operators and last colon
|
|
|
|
return C.Tok->is(TT_ConditionalExpr) &&
|
|
|
|
((C.Tok->is(tok::question) && !C.NewlinesBefore) ||
|
|
|
|
(C.Tok->is(tok::colon) && C.Tok->Next &&
|
|
|
|
(C.Tok->Next->FakeLParens.size() == 0 ||
|
|
|
|
C.Tok->Next->FakeLParens.back() != prec::Conditional)));
|
|
|
|
},
|
|
|
|
Changes, /*StartAt=*/0);
|
|
|
|
} else {
|
|
|
|
static auto AlignWrappedOperand = [](Change const &C) {
|
|
|
|
auto Previous = C.Tok->getPreviousNonComment(); // Previous;
|
|
|
|
return C.NewlinesBefore && Previous && Previous->is(TT_ConditionalExpr) &&
|
|
|
|
(Previous->is(tok::question) ||
|
|
|
|
(Previous->is(tok::colon) &&
|
|
|
|
(C.Tok->FakeLParens.size() == 0 ||
|
|
|
|
C.Tok->FakeLParens.back() != prec::Conditional)));
|
|
|
|
};
|
|
|
|
// Ensure we keep alignment of wrapped operands with non-wrapped operands
|
|
|
|
// Since we actually align the operators, the wrapped operands need the
|
|
|
|
// extra offset to be properly aligned.
|
|
|
|
for (Change &C : Changes) {
|
|
|
|
if (AlignWrappedOperand(C))
|
|
|
|
C.StartOfTokenColumn -= 2;
|
|
|
|
}
|
|
|
|
AlignTokens(
|
|
|
|
Style,
|
|
|
|
[this](Change const &C) {
|
|
|
|
// Align question operators if next operand is not wrapped, as
|
|
|
|
// well as wrapped operands after question operator or last
|
|
|
|
// colon in conditional sequence
|
|
|
|
return (C.Tok->is(TT_ConditionalExpr) && C.Tok->is(tok::question) &&
|
|
|
|
&C != &Changes.back() && (&C + 1)->NewlinesBefore == 0 &&
|
|
|
|
!(&C + 1)->IsTrailingComment) ||
|
|
|
|
AlignWrappedOperand(C);
|
|
|
|
},
|
|
|
|
Changes, /*StartAt=*/0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
void WhitespaceManager::alignTrailingComments() {
|
|
|
|
unsigned MinColumn = 0;
|
|
|
|
unsigned MaxColumn = UINT_MAX;
|
|
|
|
unsigned StartOfSequence = 0;
|
|
|
|
bool BreakBeforeNext = false;
|
|
|
|
unsigned Newlines = 0;
|
|
|
|
for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
|
2014-04-18 00:12:46 +08:00
|
|
|
if (Changes[i].StartOfBlockComment)
|
|
|
|
continue;
|
|
|
|
Newlines += Changes[i].NewlinesBefore;
|
|
|
|
if (!Changes[i].IsTrailingComment)
|
|
|
|
continue;
|
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
|
2017-08-23 15:18:36 +08:00
|
|
|
unsigned ChangeMaxColumn;
|
|
|
|
|
|
|
|
if (Style.ColumnLimit == 0)
|
|
|
|
ChangeMaxColumn = UINT_MAX;
|
|
|
|
else if (Style.ColumnLimit >= Changes[i].TokenLength)
|
|
|
|
ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength;
|
|
|
|
else
|
|
|
|
ChangeMaxColumn = ChangeMinColumn;
|
2016-01-09 23:56:53 +08:00
|
|
|
|
|
|
|
// If we don't create a replacement for this change, we have to consider
|
|
|
|
// it to be immovable.
|
|
|
|
if (!Changes[i].CreateReplacement)
|
|
|
|
ChangeMaxColumn = ChangeMinColumn;
|
|
|
|
|
2014-04-27 18:03:19 +08:00
|
|
|
if (i + 1 != e && Changes[i + 1].ContinuesPPDirective)
|
|
|
|
ChangeMaxColumn -= 2;
|
2014-04-18 00:12:46 +08:00
|
|
|
// If this comment follows an } in column 0, it probably documents the
|
|
|
|
// closing of a namespace and we don't want to align it.
|
|
|
|
bool FollowsRBraceInColumn0 = i > 0 && Changes[i].NewlinesBefore == 0 &&
|
2017-01-31 19:25:01 +08:00
|
|
|
Changes[i - 1].Tok->is(tok::r_brace) &&
|
2014-04-18 00:12:46 +08:00
|
|
|
Changes[i - 1].StartOfTokenColumn == 0;
|
|
|
|
bool WasAlignedWithStartOfNextLine = false;
|
|
|
|
if (Changes[i].NewlinesBefore == 1) { // A comment on its own line.
|
2015-01-07 22:00:11 +08:00
|
|
|
unsigned CommentColumn = SourceMgr.getSpellingColumnNumber(
|
|
|
|
Changes[i].OriginalWhitespaceRange.getEnd());
|
2014-04-18 00:12:46 +08:00
|
|
|
for (unsigned j = i + 1; j != e; ++j) {
|
2017-01-31 19:25:01 +08:00
|
|
|
if (Changes[j].Tok->is(tok::comment))
|
2016-02-01 19:20:55 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
unsigned NextColumn = SourceMgr.getSpellingColumnNumber(
|
|
|
|
Changes[j].OriginalWhitespaceRange.getEnd());
|
|
|
|
// The start of the next token was previously aligned with the
|
|
|
|
// start of this comment.
|
|
|
|
WasAlignedWithStartOfNextLine =
|
|
|
|
CommentColumn == NextColumn ||
|
|
|
|
CommentColumn == NextColumn + Style.IndentWidth;
|
|
|
|
break;
|
2013-11-09 07:31:14 +08:00
|
|
|
}
|
2013-05-22 20:51:29 +08:00
|
|
|
}
|
2014-04-18 00:12:46 +08:00
|
|
|
if (!Style.AlignTrailingComments || FollowsRBraceInColumn0) {
|
|
|
|
alignTrailingComments(StartOfSequence, i, MinColumn);
|
|
|
|
MinColumn = ChangeMinColumn;
|
|
|
|
MaxColumn = ChangeMinColumn;
|
|
|
|
StartOfSequence = i;
|
|
|
|
} else if (BreakBeforeNext || Newlines > 1 ||
|
|
|
|
(ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
|
|
|
|
// Break the comment sequence if the previous line did not end
|
|
|
|
// in a trailing comment.
|
|
|
|
(Changes[i].NewlinesBefore == 1 && i > 0 &&
|
|
|
|
!Changes[i - 1].IsTrailingComment) ||
|
|
|
|
WasAlignedWithStartOfNextLine) {
|
|
|
|
alignTrailingComments(StartOfSequence, i, MinColumn);
|
|
|
|
MinColumn = ChangeMinColumn;
|
|
|
|
MaxColumn = ChangeMaxColumn;
|
|
|
|
StartOfSequence = i;
|
|
|
|
} else {
|
|
|
|
MinColumn = std::max(MinColumn, ChangeMinColumn);
|
|
|
|
MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
|
|
|
|
}
|
2019-03-01 17:09:54 +08:00
|
|
|
BreakBeforeNext = (i == 0) || (Changes[i].NewlinesBefore > 1) ||
|
|
|
|
// Never start a sequence with a comment at the beginning
|
|
|
|
// of the line.
|
|
|
|
(Changes[i].NewlinesBefore == 1 && StartOfSequence == i);
|
2014-04-18 00:12:46 +08:00
|
|
|
Newlines = 0;
|
2013-05-22 20:51:29 +08:00
|
|
|
}
|
|
|
|
alignTrailingComments(StartOfSequence, Changes.size(), MinColumn);
|
2013-04-15 22:28:00 +08:00
|
|
|
}
|
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End,
|
|
|
|
unsigned Column) {
|
|
|
|
for (unsigned i = Start; i != End; ++i) {
|
2014-04-18 00:12:46 +08:00
|
|
|
int Shift = 0;
|
2013-05-22 20:51:29 +08:00
|
|
|
if (Changes[i].IsTrailingComment) {
|
2014-04-18 00:12:46 +08:00
|
|
|
Shift = Column - Changes[i].StartOfTokenColumn;
|
|
|
|
}
|
|
|
|
if (Changes[i].StartOfBlockComment) {
|
|
|
|
Shift = Changes[i].IndentationOffset +
|
|
|
|
Changes[i].StartOfBlockComment->StartOfTokenColumn -
|
|
|
|
Changes[i].StartOfTokenColumn;
|
2013-05-22 20:51:29 +08:00
|
|
|
}
|
2014-04-18 00:12:46 +08:00
|
|
|
assert(Shift >= 0);
|
|
|
|
Changes[i].Spaces += Shift;
|
2016-10-26 15:44:51 +08:00
|
|
|
if (i + 1 != Changes.size())
|
2014-04-18 00:12:46 +08:00
|
|
|
Changes[i + 1].PreviousEndOfTokenColumn += Shift;
|
|
|
|
Changes[i].StartOfTokenColumn += Shift;
|
2013-05-22 20:51:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WhitespaceManager::alignEscapedNewlines() {
|
2017-05-08 23:08:00 +08:00
|
|
|
if (Style.AlignEscapedNewlines == FormatStyle::ENAS_DontAlign)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool AlignLeft = Style.AlignEscapedNewlines == FormatStyle::ENAS_Left;
|
|
|
|
unsigned MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
|
2013-05-22 20:51:29 +08:00
|
|
|
unsigned StartOfMacro = 0;
|
|
|
|
for (unsigned i = 1, e = Changes.size(); i < e; ++i) {
|
|
|
|
Change &C = Changes[i];
|
|
|
|
if (C.NewlinesBefore > 0) {
|
|
|
|
if (C.ContinuesPPDirective) {
|
2013-08-28 17:07:32 +08:00
|
|
|
MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
|
2013-05-22 20:51:29 +08:00
|
|
|
} else {
|
|
|
|
alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
|
2017-05-08 23:08:00 +08:00
|
|
|
MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
|
2013-05-22 20:51:29 +08:00
|
|
|
StartOfMacro = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End,
|
|
|
|
unsigned Column) {
|
|
|
|
for (unsigned i = Start; i < End; ++i) {
|
|
|
|
Change &C = Changes[i];
|
|
|
|
if (C.NewlinesBefore > 0) {
|
|
|
|
assert(C.ContinuesPPDirective);
|
|
|
|
if (C.PreviousEndOfTokenColumn + 1 > Column)
|
|
|
|
C.EscapedNewlineColumn = 0;
|
|
|
|
else
|
|
|
|
C.EscapedNewlineColumn = Column;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WhitespaceManager::generateChanges() {
|
|
|
|
for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
|
|
|
|
const Change &C = Changes[i];
|
2015-01-29 18:47:14 +08:00
|
|
|
if (i > 0) {
|
|
|
|
assert(Changes[i - 1].OriginalWhitespaceRange.getBegin() !=
|
|
|
|
C.OriginalWhitespaceRange.getBegin() &&
|
|
|
|
"Generating two replacements for the same location");
|
|
|
|
}
|
2013-05-22 20:51:29 +08:00
|
|
|
if (C.CreateReplacement) {
|
2013-09-11 20:25:57 +08:00
|
|
|
std::string ReplacementText = C.PreviousLinePostfix;
|
|
|
|
if (C.ContinuesPPDirective)
|
2017-08-10 08:15:31 +08:00
|
|
|
appendEscapedNewlineText(ReplacementText, C.NewlinesBefore,
|
|
|
|
C.PreviousEndOfTokenColumn,
|
|
|
|
C.EscapedNewlineColumn);
|
2013-09-11 20:25:57 +08:00
|
|
|
else
|
|
|
|
appendNewlineText(ReplacementText, C.NewlinesBefore);
|
2020-04-13 22:14:26 +08:00
|
|
|
appendIndentText(
|
|
|
|
ReplacementText, C.Tok->IndentLevel, std::max(0, C.Spaces),
|
|
|
|
C.StartOfTokenColumn - std::max(0, C.Spaces), C.IsAligned);
|
2013-09-11 20:25:57 +08:00
|
|
|
ReplacementText.append(C.CurrentLinePrefix);
|
2013-05-22 20:51:29 +08:00
|
|
|
storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-20 17:51:03 +08:00
|
|
|
void WhitespaceManager::storeReplacement(SourceRange Range, StringRef Text) {
|
2013-05-22 20:51:29 +08:00
|
|
|
unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) -
|
|
|
|
SourceMgr.getFileOffset(Range.getBegin());
|
|
|
|
// Don't create a replacement, if it does not change anything.
|
|
|
|
if (StringRef(SourceMgr.getCharacterData(Range.getBegin()),
|
2013-07-08 22:34:09 +08:00
|
|
|
WhitespaceLength) == Text)
|
2013-05-22 20:51:29 +08:00
|
|
|
return;
|
2016-08-01 18:16:37 +08:00
|
|
|
auto Err = Replaces.add(tooling::Replacement(
|
2013-05-22 20:51:29 +08:00
|
|
|
SourceMgr, CharSourceRange::getCharRange(Range), Text));
|
2016-08-01 18:16:37 +08:00
|
|
|
// FIXME: better error handling. For now, just print an error message in the
|
|
|
|
// release version.
|
2016-12-23 19:40:44 +08:00
|
|
|
if (Err) {
|
2016-08-01 18:16:37 +08:00
|
|
|
llvm::errs() << llvm::toString(std::move(Err)) << "\n";
|
2016-12-23 19:40:44 +08:00
|
|
|
assert(false);
|
|
|
|
}
|
2013-04-15 22:28:00 +08:00
|
|
|
}
|
|
|
|
|
2013-09-11 20:25:57 +08:00
|
|
|
void WhitespaceManager::appendNewlineText(std::string &Text,
|
|
|
|
unsigned Newlines) {
|
|
|
|
for (unsigned i = 0; i < Newlines; ++i)
|
|
|
|
Text.append(UseCRLF ? "\r\n" : "\n");
|
2013-04-15 22:28:00 +08:00
|
|
|
}
|
|
|
|
|
2017-09-20 17:51:03 +08:00
|
|
|
void WhitespaceManager::appendEscapedNewlineText(
|
|
|
|
std::string &Text, unsigned Newlines, unsigned PreviousEndOfTokenColumn,
|
|
|
|
unsigned EscapedNewlineColumn) {
|
2013-06-12 00:01:49 +08:00
|
|
|
if (Newlines > 0) {
|
2017-08-10 08:15:31 +08:00
|
|
|
unsigned Spaces =
|
|
|
|
std::max<int>(1, EscapedNewlineColumn - PreviousEndOfTokenColumn - 1);
|
2013-06-12 00:01:49 +08:00
|
|
|
for (unsigned i = 0; i < Newlines; ++i) {
|
2017-08-10 08:15:31 +08:00
|
|
|
Text.append(Spaces, ' ');
|
2013-09-11 20:25:57 +08:00
|
|
|
Text.append(UseCRLF ? "\\\r\n" : "\\\n");
|
2017-08-10 08:15:31 +08:00
|
|
|
Spaces = std::max<int>(0, EscapedNewlineColumn - 1);
|
2013-04-15 22:28:00 +08:00
|
|
|
}
|
|
|
|
}
|
2013-05-13 17:22:11 +08:00
|
|
|
}
|
|
|
|
|
2013-09-28 00:14:22 +08:00
|
|
|
void WhitespaceManager::appendIndentText(std::string &Text,
|
|
|
|
unsigned IndentLevel, unsigned Spaces,
|
2020-04-13 22:14:26 +08:00
|
|
|
unsigned WhitespaceStartColumn,
|
|
|
|
bool IsAligned) {
|
2013-09-28 00:14:22 +08:00
|
|
|
switch (Style.UseTab) {
|
|
|
|
case FormatStyle::UT_Never:
|
2015-05-29 03:55:49 +08:00
|
|
|
Text.append(Spaces, ' ');
|
2013-09-28 00:14:22 +08:00
|
|
|
break;
|
|
|
|
case FormatStyle::UT_Always: {
|
2019-09-19 02:57:09 +08:00
|
|
|
if (Style.TabWidth) {
|
|
|
|
unsigned FirstTabWidth =
|
|
|
|
Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
|
|
|
|
|
|
|
|
// Insert only spaces when we want to end up before the next tab.
|
|
|
|
if (Spaces < FirstTabWidth || Spaces == 1) {
|
|
|
|
Text.append(Spaces, ' ');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Align to the next tab.
|
|
|
|
Spaces -= FirstTabWidth;
|
|
|
|
Text.append("\t");
|
|
|
|
|
|
|
|
Text.append(Spaces / Style.TabWidth, '\t');
|
|
|
|
Text.append(Spaces % Style.TabWidth, ' ');
|
|
|
|
} else if (Spaces == 1) {
|
2019-02-16 07:07:43 +08:00
|
|
|
Text.append(Spaces, ' ');
|
2013-09-27 17:45:40 +08:00
|
|
|
}
|
2013-09-28 00:14:22 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FormatStyle::UT_ForIndentation:
|
|
|
|
if (WhitespaceStartColumn == 0) {
|
|
|
|
unsigned Indentation = IndentLevel * Style.IndentWidth;
|
2020-04-13 22:14:26 +08:00
|
|
|
Spaces = appendTabIndent(Text, Spaces, Indentation);
|
2013-09-28 00:14:22 +08:00
|
|
|
}
|
2015-05-29 03:55:49 +08:00
|
|
|
Text.append(Spaces, ' ');
|
2013-09-28 00:14:22 +08:00
|
|
|
break;
|
2016-04-14 22:52:26 +08:00
|
|
|
case FormatStyle::UT_ForContinuationAndIndentation:
|
2020-04-13 22:14:26 +08:00
|
|
|
if (WhitespaceStartColumn == 0)
|
|
|
|
Spaces = appendTabIndent(Text, Spaces, Spaces);
|
|
|
|
Text.append(Spaces, ' ');
|
|
|
|
break;
|
|
|
|
case FormatStyle::UT_AlignWithSpaces:
|
|
|
|
if (WhitespaceStartColumn == 0) {
|
|
|
|
unsigned Indentation =
|
|
|
|
IsAligned ? IndentLevel * Style.IndentWidth : Spaces;
|
|
|
|
Spaces = appendTabIndent(Text, Spaces, Indentation);
|
2016-04-14 22:52:26 +08:00
|
|
|
}
|
|
|
|
Text.append(Spaces, ' ');
|
|
|
|
break;
|
2013-09-11 20:25:57 +08:00
|
|
|
}
|
2013-04-15 22:28:00 +08:00
|
|
|
}
|
|
|
|
|
2020-04-13 22:14:26 +08:00
|
|
|
unsigned WhitespaceManager::appendTabIndent(std::string &Text, unsigned Spaces,
|
|
|
|
unsigned Indentation) {
|
|
|
|
// This happens, e.g. when a line in a block comment is indented less than the
|
|
|
|
// first one.
|
|
|
|
if (Indentation > Spaces)
|
|
|
|
Indentation = Spaces;
|
|
|
|
if (Style.TabWidth) {
|
|
|
|
unsigned Tabs = Indentation / Style.TabWidth;
|
|
|
|
Text.append(Tabs, '\t');
|
|
|
|
Spaces -= Tabs * Style.TabWidth;
|
|
|
|
}
|
|
|
|
return Spaces;
|
|
|
|
}
|
|
|
|
|
2013-04-15 22:28:00 +08:00
|
|
|
} // namespace format
|
|
|
|
} // namespace clang
|