2013-04-25 16:56:26 +08:00
|
|
|
//===--- WhitespaceManager.h - Format C++ code ------------------*- C++ -*-===//
|
2013-04-15 22:28:00 +08:00
|
|
|
//
|
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
|
|
|
/// WhitespaceManager class manages whitespace around tokens and their
|
2013-04-15 22:28:00 +08:00
|
|
|
/// replacements.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:25:19 +08:00
|
|
|
#ifndef LLVM_CLANG_LIB_FORMAT_WHITESPACEMANAGER_H
|
|
|
|
#define LLVM_CLANG_LIB_FORMAT_WHITESPACEMANAGER_H
|
2013-04-15 22:28:00 +08:00
|
|
|
|
|
|
|
#include "TokenAnnotator.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Format/Format.h"
|
|
|
|
#include <string>
|
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
|
|
|
#include <tuple>
|
2013-04-15 22:28:00 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Manages the whitespaces around tokens and their replacements.
|
2013-04-15 22:28:00 +08:00
|
|
|
///
|
|
|
|
/// This includes special handling for certain constructs, e.g. the alignment of
|
|
|
|
/// trailing line comments.
|
2013-05-22 20:51:29 +08:00
|
|
|
///
|
|
|
|
/// To guarantee correctness of alignment operations, the \c WhitespaceManager
|
|
|
|
/// must be informed about every token in the source file; for each token, there
|
|
|
|
/// must be exactly one call to either \c replaceWhitespace or
|
|
|
|
/// \c addUntouchableToken.
|
|
|
|
///
|
|
|
|
/// There may be multiple calls to \c breakToken for a given token.
|
2013-04-15 22:28:00 +08:00
|
|
|
class WhitespaceManager {
|
|
|
|
public:
|
2016-04-28 15:52:03 +08:00
|
|
|
WhitespaceManager(const SourceManager &SourceMgr, const FormatStyle &Style,
|
2013-09-11 20:25:57 +08:00
|
|
|
bool UseCRLF)
|
|
|
|
: SourceMgr(SourceMgr), Style(Style), UseCRLF(UseCRLF) {}
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2019-04-24 04:29:46 +08:00
|
|
|
bool useCRLF() const { return UseCRLF; }
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Replaces the whitespace in front of \p Tok. Only call once for
|
2013-04-15 22:28:00 +08:00
|
|
|
/// each \c AnnotatedToken.
|
2017-06-13 22:58:55 +08:00
|
|
|
///
|
|
|
|
/// \p StartOfTokenColumn is the column at which the token will start after
|
|
|
|
/// this replacement. It is needed for determining how \p Spaces is turned
|
|
|
|
/// into tabs and spaces for some format styles.
|
2017-01-31 19:25:01 +08:00
|
|
|
void replaceWhitespace(FormatToken &Tok, unsigned Newlines, unsigned Spaces,
|
2020-04-13 22:14:26 +08:00
|
|
|
unsigned StartOfTokenColumn, bool isAligned = false,
|
2013-05-22 20:51:29 +08:00
|
|
|
bool InPPDirective = false);
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Adds information about an unchangeable token's whitespace.
|
2013-04-15 22:28:00 +08:00
|
|
|
///
|
2013-05-22 20:51:29 +08:00
|
|
|
/// Needs to be called for every token for which \c replaceWhitespace
|
|
|
|
/// was not called.
|
|
|
|
void addUntouchableToken(const FormatToken &Tok, bool InPPDirective);
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2017-10-30 22:01:50 +08:00
|
|
|
llvm::Error addReplacement(const tooling::Replacement &Replacement);
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Inserts or replaces whitespace in the middle of a token.
|
2013-04-15 22:28:00 +08:00
|
|
|
///
|
2013-06-12 00:01:49 +08:00
|
|
|
/// Inserts \p PreviousPostfix, \p Newlines, \p Spaces and \p CurrentPrefix
|
|
|
|
/// (in this order) at \p Offset inside \p Tok, replacing \p ReplaceChars
|
|
|
|
/// characters.
|
2013-04-15 22:28:00 +08:00
|
|
|
///
|
2014-04-18 00:12:46 +08:00
|
|
|
/// Note: \p Spaces can be negative to retain information about initial
|
|
|
|
/// relative column offset between a line of a block comment and the start of
|
|
|
|
/// the comment. This negative offset may be compensated by trailing comment
|
|
|
|
/// alignment here. In all other cases negative \p Spaces will be truncated to
|
|
|
|
/// 0.
|
|
|
|
///
|
2013-06-12 00:01:49 +08:00
|
|
|
/// When \p InPPDirective is true, escaped newlines are inserted. \p Spaces is
|
|
|
|
/// used to align backslashes correctly.
|
|
|
|
void 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-04-15 22:28:00 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns all the \c Replacements created during formatting.
|
2013-04-15 22:28:00 +08:00
|
|
|
const tooling::Replacements &generateReplacements();
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Represents a change before a token, a break inside a token,
|
2013-05-22 20:51:29 +08:00
|
|
|
/// or the layout of an unchanged token (or whitespace within).
|
|
|
|
struct Change {
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Functor to sort changes in original source order.
|
2013-05-22 20:51:29 +08:00
|
|
|
class IsBeforeInFile {
|
2013-07-08 22:34:09 +08:00
|
|
|
public:
|
2013-05-22 20:51:29 +08:00
|
|
|
IsBeforeInFile(const SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
|
|
|
|
bool operator()(const Change &C1, const Change &C2) const;
|
|
|
|
|
2013-07-08 22:34:09 +08:00
|
|
|
private:
|
2013-05-22 20:51:29 +08:00
|
|
|
const SourceManager &SourceMgr;
|
|
|
|
};
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Creates a \c Change.
|
2013-05-22 20:51:29 +08:00
|
|
|
///
|
|
|
|
/// The generated \c Change will replace the characters at
|
|
|
|
/// \p OriginalWhitespaceRange with a concatenation of
|
|
|
|
/// \p PreviousLinePostfix, \p NewlinesBefore line breaks, \p Spaces spaces
|
|
|
|
/// and \p CurrentLinePrefix.
|
|
|
|
///
|
|
|
|
/// \p StartOfTokenColumn and \p InPPDirective will be used to lay out
|
|
|
|
/// trailing comments and escaped newlines.
|
2017-01-31 19:25:01 +08:00
|
|
|
Change(const FormatToken &Tok, bool CreateReplacement,
|
|
|
|
SourceRange OriginalWhitespaceRange, int Spaces,
|
|
|
|
unsigned StartOfTokenColumn, unsigned NewlinesBefore,
|
|
|
|
StringRef PreviousLinePostfix, StringRef CurrentLinePrefix,
|
2020-04-13 22:14:26 +08:00
|
|
|
bool IsAligned, bool ContinuesPPDirective, bool IsInsideToken);
|
2017-01-31 19:25:01 +08:00
|
|
|
|
|
|
|
// The kind of the token whose whitespace this change replaces, or in which
|
|
|
|
// this change inserts whitespace.
|
|
|
|
// FIXME: Currently this is not set correctly for breaks inside comments, as
|
|
|
|
// the \c BreakableToken is still doing its own alignment.
|
|
|
|
const FormatToken *Tok;
|
2013-05-22 20:51:29 +08:00
|
|
|
|
|
|
|
bool CreateReplacement;
|
|
|
|
// Changes might be in the middle of a token, so we cannot just keep the
|
|
|
|
// FormatToken around to query its information.
|
|
|
|
SourceRange OriginalWhitespaceRange;
|
|
|
|
unsigned StartOfTokenColumn;
|
|
|
|
unsigned NewlinesBefore;
|
|
|
|
std::string PreviousLinePostfix;
|
|
|
|
std::string CurrentLinePrefix;
|
2020-04-13 22:14:26 +08:00
|
|
|
bool IsAligned;
|
2013-05-22 20:51:29 +08:00
|
|
|
bool ContinuesPPDirective;
|
2013-09-28 00:14:22 +08:00
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
// The number of spaces in front of the token or broken part of the token.
|
|
|
|
// This will be adapted when aligning tokens.
|
2014-04-18 00:12:46 +08:00
|
|
|
// Can be negative to retain information about the initial relative offset
|
|
|
|
// of the lines in a block comment. This is used when aligning trailing
|
|
|
|
// comments. Uncompensated negative offset is truncated to 0.
|
|
|
|
int Spaces;
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2016-01-12 00:27:16 +08:00
|
|
|
// If this change is inside of a token but not at the start of the token or
|
|
|
|
// directly after a newline.
|
|
|
|
bool IsInsideToken;
|
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
// \c IsTrailingComment, \c TokenLength, \c PreviousEndOfTokenColumn and
|
|
|
|
// \c EscapedNewlineColumn will be calculated in
|
|
|
|
// \c calculateLineBreakInformation.
|
|
|
|
bool IsTrailingComment;
|
|
|
|
unsigned TokenLength;
|
|
|
|
unsigned PreviousEndOfTokenColumn;
|
|
|
|
unsigned EscapedNewlineColumn;
|
2014-04-18 00:12:46 +08:00
|
|
|
|
|
|
|
// These fields are used to retain correct relative line indentation in a
|
|
|
|
// block comment when aligning trailing comments.
|
|
|
|
//
|
|
|
|
// If this Change represents a continuation of a block comment,
|
|
|
|
// \c StartOfBlockComment is pointer to the first Change in the block
|
|
|
|
// comment. \c IndentationOffset is a relative column offset to this
|
|
|
|
// change, so that the correct column can be reconstructed at the end of
|
|
|
|
// the alignment process.
|
|
|
|
const Change *StartOfBlockComment;
|
|
|
|
int IndentationOffset;
|
2017-03-23 10:51:25 +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
|
|
|
// Depth of conditionals. Computed from tracking fake parenthesis, except
|
|
|
|
// it does not increase the indent for "chained" conditionals.
|
|
|
|
int ConditionalsLevel;
|
|
|
|
|
|
|
|
// A combination of indent, nesting and conditionals levels, which are used
|
|
|
|
// in tandem to compute lexical scope, for the purposes of deciding
|
2017-03-23 10:51:25 +08:00
|
|
|
// when to stop consecutive alignment runs.
|
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> indentAndNestingLevel() const {
|
|
|
|
return std::make_tuple(Tok->IndentLevel, Tok->NestingLevel,
|
|
|
|
ConditionalsLevel);
|
2017-03-23 10:51:25 +08:00
|
|
|
}
|
2013-05-22 20:51:29 +08:00
|
|
|
};
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2015-12-01 20:00:43 +08:00
|
|
|
private:
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Calculate \c IsTrailingComment, \c TokenLength for the last tokens
|
2013-05-22 20:51:29 +08:00
|
|
|
/// or token parts in a line and \c PreviousEndOfTokenColumn and
|
|
|
|
/// \c EscapedNewlineColumn for the first tokens or token parts in a line.
|
|
|
|
void calculateLineBreakInformation();
|
2013-04-25 16:56:26 +08:00
|
|
|
|
2019-07-02 23:53:14 +08:00
|
|
|
/// \brief Align consecutive C/C++ preprocessor macros over all \c Changes.
|
|
|
|
void alignConsecutiveMacros();
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Align consecutive assignments over all \c Changes.
|
2015-04-29 21:06:49 +08:00
|
|
|
void alignConsecutiveAssignments();
|
|
|
|
|
2020-05-20 14:42:07 +08:00
|
|
|
/// Align consecutive bitfields over all \c Changes.
|
|
|
|
void alignConsecutiveBitFields();
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Align consecutive declarations over all \c Changes.
|
2015-10-01 18:06:54 +08:00
|
|
|
void alignConsecutiveDeclarations();
|
|
|
|
|
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
|
|
|
/// Align consecutive declarations over all \c Changes.
|
|
|
|
void alignChainedConditionals();
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Align trailing comments over all \c Changes.
|
2013-05-22 20:51:29 +08:00
|
|
|
void alignTrailingComments();
|
2013-05-13 17:22:11 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Align trailing comments from change \p Start to change \p End at
|
2013-05-22 20:51:29 +08:00
|
|
|
/// the specified \p Column.
|
|
|
|
void alignTrailingComments(unsigned Start, unsigned End, unsigned Column);
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Align escaped newlines over all \c Changes.
|
2013-05-22 20:51:29 +08:00
|
|
|
void alignEscapedNewlines();
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Align escaped newlines from change \p Start to change \p End at
|
2013-05-22 20:51:29 +08:00
|
|
|
/// the specified \p Column.
|
|
|
|
void alignEscapedNewlines(unsigned Start, unsigned End, unsigned Column);
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Fill \c Replaces with the replacements for all effective changes.
|
2013-05-22 20:51:29 +08:00
|
|
|
void generateChanges();
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Stores \p Text as the replacement for the whitespace in \p Range.
|
2015-10-04 12:53:55 +08:00
|
|
|
void storeReplacement(SourceRange Range, StringRef Text);
|
2013-09-11 20:25:57 +08:00
|
|
|
void appendNewlineText(std::string &Text, unsigned Newlines);
|
2017-08-10 08:15:31 +08:00
|
|
|
void appendEscapedNewlineText(std::string &Text, unsigned Newlines,
|
|
|
|
unsigned PreviousEndOfTokenColumn,
|
|
|
|
unsigned EscapedNewlineColumn);
|
2013-09-28 00:14:22 +08:00
|
|
|
void appendIndentText(std::string &Text, unsigned IndentLevel,
|
2020-04-13 22:14:26 +08:00
|
|
|
unsigned Spaces, unsigned WhitespaceStartColumn,
|
|
|
|
bool IsAligned);
|
|
|
|
unsigned appendTabIndent(std::string &Text, unsigned Spaces,
|
|
|
|
unsigned Indentation);
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
SmallVector<Change, 16> Changes;
|
2016-04-28 15:52:03 +08:00
|
|
|
const SourceManager &SourceMgr;
|
2013-04-15 22:28:00 +08:00
|
|
|
tooling::Replacements Replaces;
|
|
|
|
const FormatStyle &Style;
|
2013-09-11 20:25:57 +08:00
|
|
|
bool UseCRLF;
|
2013-04-15 22:28:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace format
|
|
|
|
} // namespace clang
|
|
|
|
|
2014-08-14 00:25:19 +08:00
|
|
|
#endif
|