Marek Kurdej 2022-01-24 08:48:14 +01:00
parent 3519dcfec2
commit 670a721de2
4 changed files with 17 additions and 11 deletions

View File

@ -27,6 +27,7 @@ bool AffectedRangeManager::computeAffectedLines(
const AnnotatedLine *PreviousLine = nullptr;
while (I != E) {
AnnotatedLine *Line = *I;
assert(Line->First);
Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*Line->First);
// If a line is part of a preprocessor directive, it needs to be formatted
@ -113,6 +114,7 @@ bool AffectedRangeManager::nonPPLineAffected(
// affected.
bool SomeFirstChildAffected = false;
assert(Line->First);
for (FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
// Determine whether 'Tok' was affected.
if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines))

View File

@ -361,6 +361,7 @@ private:
bool AnyImportAffected = false;
bool FormattingOff = false;
for (auto *Line : AnnotatedLines) {
assert(Line->First);
Current = Line->First;
LineEnd = Line->Last;
// clang-format comments toggle formatting on/off.

View File

@ -2353,9 +2353,10 @@ private:
void TokenAnnotator::setCommentLineLevels(
SmallVectorImpl<AnnotatedLine *> &Lines) {
const AnnotatedLine *NextNonCommentLine = nullptr;
for (AnnotatedLine *AL : llvm::reverse(Lines)) {
for (AnnotatedLine *Line : llvm::reverse(Lines)) {
assert(Line->First);
bool CommentLine = true;
for (const FormatToken *Tok = AL->First; Tok; Tok = Tok->Next) {
for (const FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
if (!Tok->is(tok::comment)) {
CommentLine = false;
break;
@ -2367,20 +2368,21 @@ void TokenAnnotator::setCommentLineLevels(
if (NextNonCommentLine && CommentLine &&
NextNonCommentLine->First->NewlinesBefore <= 1 &&
NextNonCommentLine->First->OriginalColumn ==
AL->First->OriginalColumn) {
Line->First->OriginalColumn) {
// Align comments for preprocessor lines with the # in column 0 if
// preprocessor lines are not indented. Otherwise, align with the next
// line.
AL->Level = (Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash &&
(NextNonCommentLine->Type == LT_PreprocessorDirective ||
NextNonCommentLine->Type == LT_ImportStatement))
? 0
: NextNonCommentLine->Level;
Line->Level =
(Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash &&
(NextNonCommentLine->Type == LT_PreprocessorDirective ||
NextNonCommentLine->Type == LT_ImportStatement))
? 0
: NextNonCommentLine->Level;
} else {
NextNonCommentLine = AL->First->isNot(tok::r_brace) ? AL : nullptr;
NextNonCommentLine = Line->First->isNot(tok::r_brace) ? Line : nullptr;
}
setCommentLineLevels(AL->Children);
setCommentLineLevels(Line->Children);
}
}

View File

@ -1213,6 +1213,7 @@ unsigned UnwrappedLineFormatter::format(
Joiner.getNextMergedLine(DryRun, IndentTracker);
Line; PrevPrevLine = PreviousLine, PreviousLine = Line, Line = NextLine,
FirstLine = false) {
assert(Line->First);
const AnnotatedLine &TheLine = *Line;
unsigned Indent = IndentTracker.getIndent();
@ -1240,7 +1241,7 @@ unsigned UnwrappedLineFormatter::format(
if (ShouldFormat && TheLine.Type != LT_Invalid) {
if (!DryRun) {
bool LastLine = Line->First->is(tok::eof);
bool LastLine = TheLine.First->is(tok::eof);
formatFirstToken(TheLine, PreviousLine, PrevPrevLine, Lines, Indent,
LastLine ? LastStartColumn : NextStartColumn + Indent);
}