1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | #include "AffectedRangeManager.h" |
15 | |
16 | #include "FormatToken.h" |
17 | #include "TokenAnnotator.h" |
18 | |
19 | namespace clang { |
20 | namespace format { |
21 | |
22 | bool AffectedRangeManager::computeAffectedLines( |
23 | SmallVectorImpl<AnnotatedLine *> &Lines) { |
24 | SmallVectorImpl<AnnotatedLine *>::iterator I = Lines.begin(); |
25 | SmallVectorImpl<AnnotatedLine *>::iterator E = Lines.end(); |
26 | bool SomeLineAffected = false; |
27 | const AnnotatedLine *PreviousLine = nullptr; |
28 | while (I != E) { |
| 1 | Assuming 'I' is not equal to 'E' | |
|
| 2 | | Loop condition is true. Entering loop body | |
|
29 | AnnotatedLine *Line = *I; |
30 | Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*Line->First); |
31 | |
32 | |
33 | |
34 | if (Line->InPPDirective) { |
| 3 | | Assuming field 'InPPDirective' is false | |
|
| |
35 | FormatToken *Last = Line->Last; |
36 | SmallVectorImpl<AnnotatedLine *>::iterator PPEnd = I + 1; |
37 | while (PPEnd != E && !(*PPEnd)->First->HasUnescapedNewline) { |
38 | Last = (*PPEnd)->Last; |
39 | ++PPEnd; |
40 | } |
41 | |
42 | if (affectsTokenRange(*Line->First, *Last, |
43 | false)) { |
44 | SomeLineAffected = true; |
45 | markAllAsAffected(I, PPEnd); |
46 | } |
47 | I = PPEnd; |
48 | continue; |
49 | } |
50 | |
51 | if (nonPPLineAffected(Line, PreviousLine, Lines)) |
| 5 | | Calling 'AffectedRangeManager::nonPPLineAffected' | |
|
52 | SomeLineAffected = true; |
53 | |
54 | PreviousLine = Line; |
55 | ++I; |
56 | } |
57 | return SomeLineAffected; |
58 | } |
59 | |
60 | bool AffectedRangeManager::affectsCharSourceRange( |
61 | const CharSourceRange &Range) { |
62 | for (const CharSourceRange &R : Ranges) |
63 | if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), R.getBegin()) && |
64 | !SourceMgr.isBeforeInTranslationUnit(R.getEnd(), Range.getBegin())) |
65 | return true; |
66 | return false; |
67 | } |
68 | |
69 | bool AffectedRangeManager::affectsTokenRange(const FormatToken &First, |
70 | const FormatToken &Last, |
71 | bool IncludeLeadingNewlines) { |
72 | SourceLocation Start = First.WhitespaceRange.getBegin(); |
73 | if (!IncludeLeadingNewlines) |
74 | Start = Start.getLocWithOffset(First.LastNewlineOffset); |
75 | SourceLocation End = Last.getStartOfNonWhitespace(); |
76 | End = End.getLocWithOffset(Last.TokenText.size()); |
77 | CharSourceRange Range = CharSourceRange::getCharRange(Start, End); |
78 | return affectsCharSourceRange(Range); |
79 | } |
80 | |
81 | bool AffectedRangeManager::affectsLeadingEmptyLines(const FormatToken &Tok) { |
82 | CharSourceRange EmptyLineRange = CharSourceRange::getCharRange( |
83 | Tok.WhitespaceRange.getBegin(), |
84 | Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset)); |
85 | return affectsCharSourceRange(EmptyLineRange); |
86 | } |
87 | |
88 | void AffectedRangeManager::markAllAsAffected( |
89 | SmallVectorImpl<AnnotatedLine *>::iterator I, |
90 | SmallVectorImpl<AnnotatedLine *>::iterator E) { |
91 | while (I != E) { |
92 | (*I)->Affected = true; |
93 | markAllAsAffected((*I)->Children.begin(), (*I)->Children.end()); |
94 | ++I; |
95 | } |
96 | } |
97 | |
98 | bool AffectedRangeManager::nonPPLineAffected( |
99 | AnnotatedLine *Line, const AnnotatedLine *PreviousLine, |
100 | SmallVectorImpl<AnnotatedLine *> &Lines) { |
101 | bool SomeLineAffected = false; |
102 | Line->ChildrenAffected = computeAffectedLines(Line->Children); |
| 6 | | Value assigned to field 'First' | |
|
103 | if (Line->ChildrenAffected) |
| 7 | | Assuming field 'ChildrenAffected' is false | |
|
| |
104 | SomeLineAffected = true; |
105 | |
106 | |
107 | bool SomeTokenAffected = false; |
108 | |
109 | |
110 | bool IncludeLeadingNewlines = false; |
111 | |
112 | |
113 | |
114 | bool SomeFirstChildAffected = false; |
115 | |
116 | for (FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) { |
| 9 | | Assuming pointer value is null | |
|
117 | |
118 | if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines)) |
119 | SomeTokenAffected = true; |
120 | |
121 | |
122 | if (!Tok->Children.empty() && Tok->Children.front()->Affected) |
123 | SomeFirstChildAffected = true; |
124 | |
125 | IncludeLeadingNewlines = Tok->Children.empty(); |
126 | } |
127 | |
128 | |
129 | |
130 | bool LineMoved = PreviousLine && PreviousLine->Affected && |
131 | Line->First->NewlinesBefore == 0; |
132 | |
133 | bool IsContinuedComment = |
134 | Line->First->is(tok::comment) && Line->First->Next == nullptr && |
| 10 | | Called C++ object pointer is null |
|
135 | Line->First->NewlinesBefore < 2 && PreviousLine && |
136 | PreviousLine->Affected && PreviousLine->Last->is(tok::comment); |
137 | |
138 | bool IsAffectedClosingBrace = |
139 | Line->First->is(tok::r_brace) && |
140 | Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex && |
141 | Lines[Line->MatchingOpeningBlockLineIndex]->Affected; |
142 | |
143 | if (SomeTokenAffected || SomeFirstChildAffected || LineMoved || |
144 | IsContinuedComment || IsAffectedClosingBrace) { |
145 | Line->Affected = true; |
146 | SomeLineAffected = true; |
147 | } |
148 | return SomeLineAffected; |
149 | } |
150 | |
151 | } |
152 | } |