19#include "llvm/ADT/STLExtras.h"
20#include "llvm/Support/Debug.h"
23#define DEBUG_TYPE "format-token-breaker"
28static constexpr StringRef
Blanks =
" \t\v\f\r";
44 static constexpr StringRef KnownCStylePrefixes[] = {
"///<",
"//!<",
"///",
46 static constexpr StringRef KnownTextProtoPrefixes[] = {
"####",
"###",
"##",
50 KnownPrefixes = KnownTextProtoPrefixes;
53 llvm::is_sorted(KnownPrefixes, [](StringRef Lhs, StringRef Rhs)
noexcept {
54 return Lhs.size() > Rhs.size();
57 for (StringRef KnownPrefix : KnownPrefixes) {
58 if (Comment.starts_with(KnownPrefix)) {
59 const auto PrefixLength =
60 Comment.find_first_not_of(
' ', KnownPrefix.size());
61 return Comment.substr(0, PrefixLength);
69 unsigned ColumnLimit,
unsigned TabWidth,
71 bool DecorationEndsWithStar =
false) {
72 LLVM_DEBUG(llvm::dbgs() <<
"Comment split: \"" <<
Text
73 <<
"\", Column limit: " << ColumnLimit
74 <<
", Content start: " << ContentStartColumn <<
"\n");
75 if (ColumnLimit <= ContentStartColumn + 1)
78 unsigned MaxSplit = ColumnLimit - ContentStartColumn + 1;
79 unsigned MaxSplitBytes = 0;
81 for (
unsigned NumChars = 0;
82 NumChars < MaxSplit && MaxSplitBytes <
Text.size();) {
83 unsigned BytesInChar =
86 Text.substr(MaxSplitBytes, BytesInChar), ContentStartColumn + NumChars,
88 MaxSplitBytes += BytesInChar;
95 StringRef::size_type SpaceOffset =
97 if (SpaceOffset != StringRef::npos && SpaceOffset + 1 <
Text.size() &&
98 Text[SpaceOffset + 1] ==
'{') {
99 MaxSplitBytes = SpaceOffset + 1;
103 StringRef::size_type SpaceOffset =
Text.find_last_of(
Blanks, MaxSplitBytes);
105 static const auto kNumberedListRegexp = llvm::Regex(
"^[1-9][0-9]?\\.");
107 while (SpaceOffset != StringRef::npos) {
114 StringRef::size_type LastNonBlank =
116 if (LastNonBlank != StringRef::npos &&
Text[LastNonBlank] ==
'\\') {
117 SpaceOffset =
Text.find_last_of(
Blanks, LastNonBlank);
124 if (kNumberedListRegexp.match(
Text.substr(SpaceOffset).ltrim(
Blanks))) {
125 SpaceOffset =
Text.find_last_of(
Blanks, SpaceOffset);
131 (
Text[SpaceOffset + 1] ==
'{' ||
Text[SpaceOffset + 1] ==
'@')) {
132 SpaceOffset =
Text.find_last_of(
Blanks, SpaceOffset);
139 if (SpaceOffset == StringRef::npos ||
141 Text.find_last_not_of(
Blanks, SpaceOffset) == StringRef::npos) {
144 StringRef::size_type FirstNonWhitespace =
Text.find_first_not_of(
Blanks);
145 if (FirstNonWhitespace == StringRef::npos) {
149 SpaceOffset =
Text.find_first_of(
150 Blanks, std::max<unsigned>(MaxSplitBytes, FirstNonWhitespace));
152 if (SpaceOffset != StringRef::npos && SpaceOffset != 0) {
157 if (SpaceOffset == 1 &&
Text[SpaceOffset - 1] ==
'*')
159 StringRef BeforeCut =
Text.substr(0, SpaceOffset).rtrim(
Blanks);
160 StringRef AfterCut =
Text.substr(SpaceOffset);
162 if (!DecorationEndsWithStar || AfterCut.size() <= 1 || AfterCut[1] !=
'/')
163 AfterCut = AfterCut.ltrim(
Blanks);
165 AfterCut.begin() - BeforeCut.end());
176 if (ColumnLimit <= UsedColumns)
178 unsigned MaxSplit = ColumnLimit - UsedColumns;
179 StringRef::size_type SpaceOffset = 0;
180 StringRef::size_type SlashOffset = 0;
181 StringRef::size_type WordStartOffset = 0;
182 StringRef::size_type SplitPoint = 0;
183 for (
unsigned Chars = 0;;) {
185 if (
Text[0] ==
'\\') {
191 Text.substr(0, Advance), UsedColumns + Chars, TabWidth, Encoding);
194 if (Chars > MaxSplit ||
Text.size() <= Advance)
198 SpaceOffset = SplitPoint;
200 SlashOffset = SplitPoint;
202 WordStartOffset = SplitPoint;
204 SplitPoint += Advance;
208 if (SpaceOffset != 0)
210 if (SlashOffset != 0)
212 if (WordStartOffset != 0)
221 "formatting regions are switched by comment tokens");
222 StringRef Content =
Token.TokenText.substr(2).ltrim();
223 return Content.starts_with(
"clang-format on") ||
224 Content.starts_with(
"clang-format off");
241 return RemainingTokenColumns + 1 -
Split.second;
248 StringRef::size_type Length,
249 unsigned StartColumn)
const {
250 llvm_unreachable(
"Getting the length of a part of the string literal "
251 "indicates that the code tries to reflow it.");
256 unsigned StartColumn)
const {
268 const FormatToken &Tok,
unsigned StartColumn, StringRef Prefix,
269 StringRef Postfix,
unsigned UnbreakableTailLength,
bool InPPDirective,
272 StartColumn(StartColumn), Prefix(Prefix), Postfix(Postfix),
273 UnbreakableTailLength(UnbreakableTailLength) {
280 unsigned LineIndex,
unsigned TailOffset,
unsigned ColumnLimit,
281 unsigned ContentStartColumn,
const llvm::Regex &CommentPragmasRegex)
const {
288 unsigned ContentIndent,
290 Whitespaces.replaceWhitespaceInToken(
297 unsigned StartColumn,
unsigned UnbreakableTailLength,
bool InPPDirective,
300 Tok, StartColumn, QuoteStyle == SingleQuotes ?
"'"
301 : QuoteStyle == AtDoubleQuotes ?
"@\""
303 QuoteStyle == SingleQuotes ?
"'" :
"\"",
304 UnbreakableTailLength, InPPDirective, Encoding, Style),
305 BracesNeeded(Tok.isNot(TT_StringInConcatenation)),
306 QuoteStyle(QuoteStyle) {
332 Postfix = SignOnNewLine ?
"'" :
"' +";
333 Prefix = SignOnNewLine ?
"+ '" :
"'";
337 Prefix = SignOnNewLine ?
"+ @\"" :
"@\"";
340 Prefix = SignOnNewLine ?
"+ \"" :
"\"";
343 Postfix = SignOnNewLine ?
"\"" :
"\" +";
358 unsigned LineIndex,
unsigned Offset,
unsigned StartColumn)
const {
377 unsigned LineIndex,
unsigned TailOffset,
Split Split,
379 Whitespaces.replaceWhitespaceInToken(
399 Whitespaces.replaceWhitespaceInToken(
403 Whitespaces.replaceWhitespaceInToken(
410 unsigned StartColumn,
bool InPPDirective,
414 StartColumn(StartColumn) {}
420 unsigned ColumnLimit,
unsigned ContentStartColumn,
421 const llvm::Regex &CommentPragmasRegex)
const {
423 if (CommentPragmasRegex.match(
Content[LineIndex]))
424 return Split(StringRef::npos, 0);
431 unsigned LineIndex,
unsigned TailOffset,
Split Split,
433 StringRef
Text =
Content[LineIndex].substr(TailOffset);
438 unsigned BreakOffsetInToken =
440 unsigned CharsToRemove =
Split.second;
441 Whitespaces.replaceWhitespaceInToken(
442 tokenAt(LineIndex), BreakOffsetInToken, CharsToRemove,
"",
"",
451 Content = Content.trim(
Blanks);
454 bool hasSpecialMeaningPrefix =
false;
455 for (StringRef Prefix :
456 {
"@",
"\\",
"TODO",
"FIXME",
"XXX",
"-# ",
"- ",
"+ ",
"* "}) {
457 if (Content.starts_with(Prefix)) {
458 hasSpecialMeaningPrefix =
true;
466 static const auto kNumberedListRegexp = llvm::Regex(
"^[1-9][0-9]?\\. ");
467 hasSpecialMeaningPrefix =
468 hasSpecialMeaningPrefix || kNumberedListRegexp.match(Content);
473 return Content.size() >= 2 && !hasSpecialMeaningPrefix &&
474 !Content.ends_with(
"\\") &&
482 unsigned OriginalStartColumn,
bool FirstInLine,
bool InPPDirective,
485 DelimitersOnNewline(
false),
486 UnbreakableTailLength(
Token.UnbreakableTailLength) {
487 assert(
Tok.
is(TT_BlockComment) &&
488 "block comment section must start with a block comment");
491 assert(TokenText.starts_with(
"/*") && TokenText.ends_with(
"*/"));
492 TokenText.substr(2, TokenText.size() - 4)
493 .split(
Lines, UseCRLF ?
"\r\n" :
"\n");
495 int IndentDelta =
StartColumn - OriginalStartColumn;
502 for (
size_t i = 1; i <
Lines.size(); ++i)
503 adjustWhitespace(i, IndentDelta);
514 if (
Lines.size() >= 2 &&
Content[1].starts_with(
"**") &&
520 if (
Lines.size() == 1 && !FirstInLine) {
528 for (
size_t i = 1, e =
Content.size(); i < e && !Decoration.empty(); ++i) {
534 }
else if (!
Text.empty() && Decoration.starts_with(
Text)) {
537 while (!
Text.starts_with(Decoration))
538 Decoration = Decoration.drop_back(1);
541 LastLineNeedsDecoration =
true;
543 for (
size_t i = 1, e =
Lines.size(); i < e; ++i) {
549 LastLineNeedsDecoration =
false;
551 if (e >= 2 && !Decoration.empty())
553 }
else if (Decoration.empty()) {
565 unsigned DecorationSize = Decoration.starts_with(
Content[i])
571 if (!Decoration.starts_with(
Content[i])) {
573 std::min<int>(IndentAtLineBreak, std::max(0,
ContentColumn[i]));
576 IndentAtLineBreak = std::max<unsigned>(IndentAtLineBreak, Decoration.size());
580 if ((
Lines[0] ==
"*" ||
Lines[0].starts_with(
"* ")) &&
Lines.size() > 1) {
582 DelimitersOnNewline =
true;
583 }
else if (
Lines[0].starts_with(
"* ") &&
Lines.size() == 1) {
597 llvm::dbgs() <<
"IndentAtLineBreak " << IndentAtLineBreak <<
"\n";
598 llvm::dbgs() <<
"DelimitersOnNewline " << DelimitersOnNewline <<
"\n";
599 for (
size_t i = 0; i <
Lines.size(); ++i) {
600 llvm::dbgs() << i <<
" |" <<
Content[i] <<
"| "
602 <<
"IN=" << (
Content[i].data() -
Lines[i].data()) <<
"\n";
608 unsigned LineIndex,
unsigned TailOffset,
unsigned ColumnLimit,
609 unsigned ContentStartColumn,
const llvm::Regex &CommentPragmasRegex)
const {
611 if (CommentPragmasRegex.match(
Content[LineIndex]))
612 return Split(StringRef::npos, 0);
618void BreakableBlockComment::adjustWhitespace(
unsigned LineIndex,
625 size_t EndOfPreviousLine =
Lines[LineIndex - 1].size();
631 Lines[LineIndex - 1].find_last_not_of(
Blanks, EndOfPreviousLine);
632 if (EndOfPreviousLine == StringRef::npos)
633 EndOfPreviousLine = 0;
637 size_t StartOfLine =
Lines[LineIndex].find_first_not_of(
Blanks);
638 if (StartOfLine == StringRef::npos)
639 StartOfLine =
Lines[LineIndex].size();
641 StringRef Whitespace =
Lines[LineIndex].substr(0, StartOfLine);
643 size_t PreviousContentOffset =
644 Content[LineIndex - 1].data() -
Lines[LineIndex - 1].data();
646 PreviousContentOffset, EndOfPreviousLine - PreviousContentOffset);
647 Content[LineIndex] =
Lines[LineIndex].substr(StartOfLine);
657 StringRef::size_type Length,
658 unsigned StartColumn)
const {
666 unsigned StartColumn)
const {
667 unsigned LineLength =
668 UnbreakableTailLength +
670 if (LineIndex + 1 ==
Lines.size()) {
673 bool HasRemainingText = Offset <
Content[LineIndex].size();
674 if (!HasRemainingText) {
675 bool HasDecoration =
Lines[LineIndex].ltrim().starts_with(Decoration);
677 LineLength -= Decoration.size();
686 return IndentAtLineBreak;
690const llvm::StringSet<>
692 "@param",
"@return",
"@returns",
"@throws",
"@type",
"@template",
693 "@see",
"@deprecated",
"@define",
"@exports",
"@mods",
"@private",
702 StringRef ContentWithNoDecoration =
Content[LineIndex];
703 if (LineIndex == 0 && ContentWithNoDecoration.starts_with(
"*"))
704 ContentWithNoDecoration = ContentWithNoDecoration.substr(1).ltrim(
Blanks);
705 StringRef FirstWord = ContentWithNoDecoration.substr(
706 0, ContentWithNoDecoration.find_first_of(
Blanks));
715 StringRef
Text =
Content[LineIndex].substr(TailOffset);
716 StringRef Prefix = Decoration;
720 unsigned LocalIndentAtLineBreak = IndentAtLineBreak;
721 if (LineIndex + 1 ==
Lines.size() &&
725 if (LocalIndentAtLineBreak >= 2)
726 LocalIndentAtLineBreak -= 2;
730 unsigned BreakOffsetInToken =
732 unsigned CharsToRemove =
Split.second;
733 assert(LocalIndentAtLineBreak >= Prefix.size());
734 std::string PrefixWithTrailingIndent = std::string(Prefix);
735 PrefixWithTrailingIndent.append(ContentIndent,
' ');
736 Whitespaces.replaceWhitespaceInToken(
737 tokenAt(LineIndex), BreakOffsetInToken, CharsToRemove,
"",
739 LocalIndentAtLineBreak + ContentIndent -
740 PrefixWithTrailingIndent.size());
744 unsigned LineIndex,
const llvm::Regex &CommentPragmasRegex)
const {
745 if (!
mayReflow(LineIndex, CommentPragmasRegex))
746 return Split(StringRef::npos, 0);
750 size_t Trimmed =
Content[LineIndex].find_first_not_of(
Blanks);
753 if (PreviousContentIndent && Trimmed != StringRef::npos &&
754 Trimmed != PreviousContentIndent) {
755 return Split(StringRef::npos, 0);
759 return Split(0, Trimmed != StringRef::npos ? Trimmed : 0);
764 return DelimitersOnNewline &&
765 Lines[0].substr(1).find_first_not_of(
Blanks) != StringRef::npos;
773 "Reflowing whitespace within a token");
776 unsigned WhitespaceOffsetInToken =
Content[LineIndex - 1].data() +
777 Content[LineIndex - 1].size() -
779 unsigned WhitespaceLength = TrimmedContent.data() -
781 WhitespaceOffsetInToken;
782 Whitespaces.replaceWhitespaceInToken(
783 tokenAt(LineIndex), WhitespaceOffsetInToken,
784 WhitespaceLength,
"",
791 if (LineIndex == 0) {
792 if (DelimitersOnNewline) {
797 size_t BreakLength =
Lines[0].substr(1).find_first_not_of(
Blanks);
798 if (BreakLength != StringRef::npos) {
807 StringRef Prefix = Decoration;
808 if (
Content[LineIndex].empty()) {
809 if (LineIndex + 1 ==
Lines.size()) {
810 if (!LastLineNeedsDecoration) {
815 }
else if (!Decoration.empty()) {
818 Prefix = Prefix.substr(0, 1);
822 Prefix = Prefix.substr(0, 1);
826 unsigned WhitespaceOffsetInToken =
Content[LineIndex - 1].data() +
827 Content[LineIndex - 1].size() -
829 unsigned WhitespaceLength =
Content[LineIndex].data() -
831 WhitespaceOffsetInToken;
832 Whitespaces.replaceWhitespaceInToken(
833 tokenAt(LineIndex), WhitespaceOffsetInToken, WhitespaceLength,
"", Prefix,
839 if (DelimitersOnNewline) {
843 StringRef
Line =
Content.back().substr(TailOffset);
845 if (!TrimmedLine.empty())
846 return Split(TrimmedLine.size(),
Line.size() - TrimmedLine.size());
848 return Split(StringRef::npos, 0);
852 unsigned LineIndex,
const llvm::Regex &CommentPragmasRegex)
const {
855 StringRef IndentContent =
Content[LineIndex];
856 if (
Lines[LineIndex].ltrim(
Blanks).starts_with(
"*"))
857 IndentContent =
Lines[LineIndex].ltrim(
Blanks).substr(1);
858 return LineIndex > 0 && !CommentPragmasRegex.match(IndentContent) &&
867 assert(
Tok.
is(TT_LineComment) &&
868 "line comment section must start with a line comment");
873 int FirstLineSpaceChange = 0;
875 CurrentTok && CurrentTok->
is(TT_LineComment);
876 CurrentTok = CurrentTok->Next) {
877 LastLineTok = LineTok;
878 StringRef TokenText(CurrentTok->TokenText);
879 assert((TokenText.starts_with(
"//") || TokenText.starts_with(
"#")) &&
880 "unsupported line comment prefix, '//' and '#' are supported");
881 size_t FirstLineIndex =
Lines.size();
882 TokenText.split(
Lines,
"\n");
885 PrefixSpaceChange.resize(
Lines.size());
887 Prefix.resize(
Lines.size());
888 OriginalPrefix.resize(
Lines.size());
889 for (
size_t i = FirstLineIndex, e =
Lines.size(); i < e; ++i) {
892 OriginalPrefix[i] = IndentPrefix;
893 const int SpacesInPrefix = llvm::count(IndentPrefix,
' ');
897 const auto NoSpaceBeforeFirstCommentChar = [&]() {
898 assert(
Lines[i].size() > IndentPrefix.size());
899 const char FirstCommentChar =
Lines[i][IndentPrefix.size()];
900 const unsigned FirstCharByteSize =
903 Lines[i].substr(IndentPrefix.size(), FirstCharByteSize),
916 if (FirstCommentChar ==
'#' && !TokenText.starts_with(
"#"))
918 return FirstCommentChar ==
'\\' ||
isPunctuation(FirstCommentChar) ||
927 if (i == 0 || OriginalPrefix[i].rtrim(
Blanks) !=
928 OriginalPrefix[i - 1].rtrim(
Blanks)) {
929 if (SpacesInPrefix < Minimum &&
Lines[i].size() > IndentPrefix.size() &&
930 !NoSpaceBeforeFirstCommentChar()) {
931 FirstLineSpaceChange = Minimum - SpacesInPrefix;
932 }
else if (
static_cast<unsigned>(SpacesInPrefix) >
934 FirstLineSpaceChange =
937 FirstLineSpaceChange = 0;
941 if (
Lines[i].size() != IndentPrefix.size()) {
942 PrefixSpaceChange[i] = FirstLineSpaceChange;
944 if (SpacesInPrefix + PrefixSpaceChange[i] < Minimum) {
945 PrefixSpaceChange[i] +=
946 Minimum - (SpacesInPrefix + PrefixSpaceChange[i]);
949 assert(
Lines[i].size() > IndentPrefix.size());
950 const auto FirstNonSpace =
Lines[i][IndentPrefix.size()];
952 const bool LineRequiresLeadingSpace =
953 !NoSpaceBeforeFirstCommentChar() ||
954 (FirstNonSpace ==
'}' && FirstLineSpaceChange != 0);
955 const bool AllowsSpaceChange =
957 (SpacesInPrefix != 0 || LineRequiresLeadingSpace);
959 if (PrefixSpaceChange[i] > 0 && AllowsSpaceChange) {
960 Prefix[i] = IndentPrefix.str();
961 Prefix[i].append(PrefixSpaceChange[i],
' ');
962 }
else if (PrefixSpaceChange[i] < 0 && AllowsSpaceChange) {
963 Prefix[i] = IndentPrefix
964 .drop_back(std::min<std::size_t>(
965 -PrefixSpaceChange[i], SpacesInPrefix))
968 Prefix[i] = IndentPrefix.str();
973 Prefix[i] = IndentPrefix.drop_back(SpacesInPrefix).str();
984 if (EndOfLine == StringRef::npos)
990 LineTok = CurrentTok->
Next;
991 if (CurrentTok->Next && !CurrentTok->Next->ContinuesLineCommentSection) {
1013 StringRef::size_type Length,
1014 unsigned StartColumn)
const {
1027 unsigned LineIndex,
unsigned TailOffset,
Split Split,
1029 StringRef
Text =
Content[LineIndex].substr(TailOffset);
1032 unsigned BreakOffsetInToken =
1034 unsigned CharsToRemove =
Split.second;
1035 Whitespaces.replaceWhitespaceInToken(
1036 tokenAt(LineIndex), BreakOffsetInToken, CharsToRemove,
"",
1042 unsigned LineIndex,
const llvm::Regex &CommentPragmasRegex)
const {
1043 if (!
mayReflow(LineIndex, CommentPragmasRegex))
1044 return Split(StringRef::npos, 0);
1046 size_t Trimmed =
Content[LineIndex].find_first_not_of(
Blanks);
1052 return Split(0, Trimmed != StringRef::npos ? Trimmed : 0);
1057 if (LineIndex > 0 &&
Tokens[LineIndex] !=
Tokens[LineIndex - 1]) {
1060 Whitespaces.replaceWhitespace(
1061 *
Tokens[LineIndex], 0, 0,
1064 }
else if (LineIndex > 0) {
1075 unsigned Offset =
Lines[LineIndex - 1].data() +
1076 Lines[LineIndex - 1].size() -
1080 unsigned WhitespaceLength =
1082 Whitespaces.replaceWhitespaceInToken(*
Tokens[LineIndex], Offset,
1093 unsigned WhitespaceLength =
1095 Whitespaces.replaceWhitespaceInToken(*
Tokens[LineIndex], Offset,
1112 if (LineIndex > 0 &&
Tokens[LineIndex] !=
Tokens[LineIndex - 1]) {
1118 unsigned LineColumn =
1120 (
Content[LineIndex].data() -
Lines[LineIndex].data()) +
1121 (OriginalPrefix[LineIndex].size() - Prefix[LineIndex].size());
1127 Whitespaces.replaceWhitespace(*
Tokens[LineIndex],
1134 if (OriginalPrefix[LineIndex] != Prefix[LineIndex]) {
1136 const auto SpacesToRemove = -std::min(PrefixSpaceChange[LineIndex], 0);
1137 const auto SpacesToAdd = std::max(PrefixSpaceChange[LineIndex], 0);
1138 Whitespaces.replaceWhitespaceInToken(
1139 tokenAt(LineIndex), OriginalPrefix[LineIndex].size() - SpacesToRemove,
1140 SpacesToRemove,
"",
"",
false,
1147 State.NextToken = LastLineTok->
Next;
1151 unsigned LineIndex,
const llvm::Regex &CommentPragmasRegex)
const {
1154 StringRef IndentContent =
Content[LineIndex];
1155 if (
Lines[LineIndex].starts_with(
"//"))
1156 IndentContent =
Lines[LineIndex].substr(2);
1163 return LineIndex > 0 && !CommentPragmasRegex.match(IndentContent) &&
1166 OriginalPrefix[LineIndex] == OriginalPrefix[LineIndex - 1];
Declares BreakableToken, BreakableStringLiteral, BreakableComment, BreakableBlockComment and Breakabl...
This file implements an indenter that manages the indentation of continuations.
Token - This structure provides full information about a lexed token.
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {....
The JSON file list parser is used to communicate input to InstallAPI.
LLVM_READONLY bool isAlphanumeric(unsigned char c)
Return true if this character is an ASCII letter or digit: [a-zA-Z0-9].
LLVM_READONLY bool isHorizontalWhitespace(unsigned char c)
Returns true if this character is horizontal ASCII whitespace: ' ', '\t', '\f', '\v'.
LLVM_READONLY bool isPunctuation(unsigned char c)
Return true if this character is an ASCII punctuation character.