clang: lib/Format/AffectedRangeManager.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
15
18
20namespace format {
21
26 bool SomeLineAffected = false;
28 while (I != E) {
30 assert(Line->First);
31 Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*Line->First);
32
33
34
35 if (Line->InPPDirective) {
37 const auto *PPEnd = I + 1;
38 while (PPEnd != E && !(*PPEnd)->First->HasUnescapedNewline) {
39 Last = (*PPEnd)->Last;
40 ++PPEnd;
41 }
42
43 if (affectsTokenRange(*Line->First, *Last,
44 false)) {
45 SomeLineAffected = true;
46 markAllAsAffected(I, PPEnd);
47 }
48 I = PPEnd;
49 continue;
50 }
51
52 if (nonPPLineAffected(Line, PreviousLine, Lines))
53 SomeLineAffected = true;
54
55 PreviousLine = Line;
56 ++I;
57 }
58 return SomeLineAffected;
59}
60
66 return true;
67 }
68 }
69 return false;
70}
71
72bool AffectedRangeManager::affectsTokenRange(const FormatToken &First,
74 bool IncludeLeadingNewlines) {
76 if (!IncludeLeadingNewlines)
79 End = End.getLocWithOffset(Last.TokenText.size());
82}
83
84bool AffectedRangeManager::affectsLeadingEmptyLines(const FormatToken &Tok) {
86 Tok.WhitespaceRange.getBegin(),
87 Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset));
89}
90
91void AffectedRangeManager::markAllAsAffected(
92 ArrayRef<AnnotatedLine *>::iterator I,
93 ArrayRef<AnnotatedLine *>::iterator E) {
94 while (I != E) {
95 (*I)->Affected = true;
96 markAllAsAffected((*I)->Children.begin(), (*I)->Children.end());
97 ++I;
98 }
99}
100
101bool AffectedRangeManager::nonPPLineAffected(
102 AnnotatedLine *Line, const AnnotatedLine *PreviousLine,
103 SmallVectorImpl<AnnotatedLine *> &Lines) {
104 bool SomeLineAffected = false;
106 if (Line->ChildrenAffected)
107 SomeLineAffected = true;
108
109
110 bool SomeTokenAffected = false;
111
112
113 bool IncludeLeadingNewlines = false;
114
115
116
117 bool SomeFirstChildAffected = false;
118
119 assert(Line->First);
120 for (FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
121
122 if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines))
123 SomeTokenAffected = true;
124
125
126 if (!Tok->Children.empty() && Tok->Children.front()->Affected)
127 SomeFirstChildAffected = true;
128
129 IncludeLeadingNewlines = Tok->Children.empty();
130 }
131
132
133
134 bool LineMoved = PreviousLine && PreviousLine->Affected &&
135 Line->First->NewlinesBefore == 0;
136
137 bool IsContinuedComment =
138 Line->First->is(tok::comment) && ->First->Next &&
139 Line->First->NewlinesBefore < 2 && PreviousLine &&
140 PreviousLine->Affected && PreviousLine->Last->is(tok::comment);
141
142 bool IsAffectedClosingBrace =
143 Line->First->is(tok::r_brace) &&
145 Lines[Line->MatchingOpeningBlockLineIndex]->Affected;
146
147 if (SomeTokenAffected || SomeFirstChildAffected || LineMoved ||
148 IsContinuedComment || IsAffectedClosingBrace) {
149 Line->Affected = true;
150 SomeLineAffected = true;
151 }
152 return SomeLineAffected;
153}
154
155}
156}
AffectedRangeManager class manages affected ranges in the code.
This file contains the declaration of the FormatToken, a wrapper around Token with additional informa...
This file implements a token annotator, i.e.
Represents a character-granular source range.
static CharSourceRange getCharRange(SourceRange R)
Encodes a location in the source.
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const
Determines the order of 2 source locations in the translation unit.
bool computeAffectedLines(SmallVectorImpl< AnnotatedLine * > &Lines)
bool affectsCharSourceRange(const CharSourceRange &Range)
The JSON file list parser is used to communicate input to InstallAPI.
A wrapper around a Token storing information about the whitespace characters preceding it.
static const size_t kInvalidIndex