1//===--- AffectedRangeManager.cpp - Format C++ code -----------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file implements AffectRangeManager class.
11///
12//===----------------------------------------------------------------------===//
13
14#include "AffectedRangeManager.h"
15
16#include "FormatToken.h"
17#include "TokenAnnotator.h"
18
19namespace clang {
20namespace format {
21
22bool AffectedRangeManager::computeAffectedLines(
23 SmallVectorImpl<AnnotatedLine *> &Lines) {
24 ArrayRef<AnnotatedLine *>::iterator I = Lines.begin();
25 ArrayRef<AnnotatedLine *>::iterator E = Lines.end();
26 bool SomeLineAffected = false;
27 const AnnotatedLine *PreviousLine = nullptr;
28 while (I != E) {
29 AnnotatedLine *Line = *I;
30 assert(Line->First);
31 Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(Tok: *Line->First);
32
33 // If a line is part of a preprocessor directive, it needs to be formatted
34 // if any token within the directive is affected.
35 if (Line->InPPDirective) {
36 FormatToken *Last = Line->Last;
37 const auto *PPEnd = I + 1;
38 while (PPEnd != E && !(*PPEnd)->First->HasUnescapedNewline &&
39 (*PPEnd)->First->isNot(Kind: tok::eof)) {
40 Last = (*PPEnd)->Last;
41 ++PPEnd;
42 }
43
44 if (affectsTokenRange(First: *Line->First, Last: *Last,
45 /*IncludeLeadingNewlines=*/false)) {
46 SomeLineAffected = true;
47 markAllAsAffected(I, E: PPEnd);
48 }
49 I = PPEnd;
50 continue;
51 }
52
53 if (nonPPLineAffected(Line, PreviousLine, Lines))
54 SomeLineAffected = true;
55
56 PreviousLine = Line;
57 ++I;
58 }
59 return SomeLineAffected;
60}
61
62bool AffectedRangeManager::affectsCharSourceRange(
63 const CharSourceRange &Range) {
64 for (const CharSourceRange &R : Ranges) {
65 if (!SourceMgr.isBeforeInTranslationUnit(LHS: Range.getEnd(), RHS: R.getBegin()) &&
66 !SourceMgr.isBeforeInTranslationUnit(LHS: R.getEnd(), RHS: Range.getBegin())) {
67 return true;
68 }
69 }
70 return false;
71}
72
73bool AffectedRangeManager::affectsTokenRange(const FormatToken &First,
74 const FormatToken &Last,
75 bool IncludeLeadingNewlines) {
76 SourceLocation Start = First.WhitespaceRange.getBegin();
77 if (!IncludeLeadingNewlines)
78 Start = Start.getLocWithOffset(Offset: First.LastNewlineOffset);
79 SourceLocation End = Last.getStartOfNonWhitespace();
80 End = End.getLocWithOffset(Offset: Last.TokenText.size());
81 CharSourceRange Range = CharSourceRange::getCharRange(B: Start, E: End);
82 return affectsCharSourceRange(Range);
83}
84
85bool AffectedRangeManager::affectsLeadingEmptyLines(const FormatToken &Tok) {
86 CharSourceRange EmptyLineRange = CharSourceRange::getCharRange(
87 B: Tok.WhitespaceRange.getBegin(),
88 E: Tok.WhitespaceRange.getBegin().getLocWithOffset(Offset: Tok.LastNewlineOffset));
89 return affectsCharSourceRange(Range: EmptyLineRange);
90}
91
92void AffectedRangeManager::markAllAsAffected(
93 ArrayRef<AnnotatedLine *>::iterator I,
94 ArrayRef<AnnotatedLine *>::iterator E) {
95 while (I != E) {
96 (*I)->Affected = true;
97 markAllAsAffected(I: (*I)->Children.begin(), E: (*I)->Children.end());
98 ++I;
99 }
100}
101
102bool AffectedRangeManager::nonPPLineAffected(
103 AnnotatedLine *Line, const AnnotatedLine *PreviousLine,
104 SmallVectorImpl<AnnotatedLine *> &Lines) {
105 bool SomeLineAffected = false;
106 Line->ChildrenAffected = computeAffectedLines(Lines&: Line->Children);
107 if (Line->ChildrenAffected)
108 SomeLineAffected = true;
109
110 // Stores whether one of the line's tokens is directly affected.
111 bool SomeTokenAffected = false;
112 // Stores whether we need to look at the leading newlines of the next token
113 // in order to determine whether it was affected.
114 bool IncludeLeadingNewlines = false;
115
116 // Stores whether the first child line of any of this line's tokens is
117 // affected.
118 bool SomeFirstChildAffected = false;
119
120 assert(Line->First);
121 for (FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
122 // Determine whether 'Tok' was affected.
123 if (affectsTokenRange(First: *Tok, Last: *Tok, IncludeLeadingNewlines))
124 SomeTokenAffected = true;
125
126 // Determine whether the first child of 'Tok' was affected.
127 if (!Tok->Children.empty() && Tok->Children.front()->Affected)
128 SomeFirstChildAffected = true;
129
130 IncludeLeadingNewlines = Tok->Children.empty();
131 }
132
133 // Was this line moved, i.e. has it previously been on the same line as an
134 // affected line?
135 bool LineMoved = PreviousLine && PreviousLine->Affected &&
136 Line->First->NewlinesBefore == 0;
137
138 bool IsContinuedComment =
139 Line->First->is(Kind: tok::comment) && !Line->First->Next &&
140 Line->First->NewlinesBefore < 2 && PreviousLine &&
141 PreviousLine->Affected && PreviousLine->Last->is(Kind: tok::comment);
142
143 bool IsAffectedClosingBrace =
144 Line->First->is(Kind: tok::r_brace) &&
145 Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex &&
146 Lines[Line->MatchingOpeningBlockLineIndex]->Affected;
147
148 if (SomeTokenAffected || SomeFirstChildAffected || LineMoved ||
149 IsContinuedComment || IsAffectedClosingBrace) {
150 Line->Affected = true;
151 SomeLineAffected = true;
152 }
153 return SomeLineAffected;
154}
155
156} // namespace format
157} // namespace clang
158