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 
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) {
29     AnnotatedLine *Line = *I;
30     assert(Line->First);
31     Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*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       SmallVectorImpl<AnnotatedLine *>::iterator 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                             /*IncludeLeadingNewlines=*/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 
61 bool AffectedRangeManager::affectsCharSourceRange(
62     const CharSourceRange &Range) {
63   for (const CharSourceRange &R : Ranges)
64     if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), R.getBegin()) &&
65         !SourceMgr.isBeforeInTranslationUnit(R.getEnd(), Range.getBegin()))
66       return true;
67   return false;
68 }
69 
70 bool AffectedRangeManager::affectsTokenRange(const FormatToken &First,
71                                              const FormatToken &Last,
72                                              bool IncludeLeadingNewlines) {
73   SourceLocation Start = First.WhitespaceRange.getBegin();
74   if (!IncludeLeadingNewlines)
75     Start = Start.getLocWithOffset(First.LastNewlineOffset);
76   SourceLocation End = Last.getStartOfNonWhitespace();
77   End = End.getLocWithOffset(Last.TokenText.size());
78   CharSourceRange Range = CharSourceRange::getCharRange(Start, End);
79   return affectsCharSourceRange(Range);
80 }
81 
82 bool AffectedRangeManager::affectsLeadingEmptyLines(const FormatToken &Tok) {
83   CharSourceRange EmptyLineRange = CharSourceRange::getCharRange(
84       Tok.WhitespaceRange.getBegin(),
85       Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset));
86   return affectsCharSourceRange(EmptyLineRange);
87 }
88 
89 void AffectedRangeManager::markAllAsAffected(
90     SmallVectorImpl<AnnotatedLine *>::iterator I,
91     SmallVectorImpl<AnnotatedLine *>::iterator E) {
92   while (I != E) {
93     (*I)->Affected = true;
94     markAllAsAffected((*I)->Children.begin(), (*I)->Children.end());
95     ++I;
96   }
97 }
98 
99 bool AffectedRangeManager::nonPPLineAffected(
100     AnnotatedLine *Line, const AnnotatedLine *PreviousLine,
101     SmallVectorImpl<AnnotatedLine *> &Lines) {
102   bool SomeLineAffected = false;
103   Line->ChildrenAffected = computeAffectedLines(Line->Children);
104   if (Line->ChildrenAffected)
105     SomeLineAffected = true;
106 
107   // Stores whether one of the line's tokens is directly affected.
108   bool SomeTokenAffected = false;
109   // Stores whether we need to look at the leading newlines of the next token
110   // in order to determine whether it was affected.
111   bool IncludeLeadingNewlines = false;
112 
113   // Stores whether the first child line of any of this line's tokens is
114   // affected.
115   bool SomeFirstChildAffected = false;
116 
117   assert(Line->First);
118   for (FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
119     // Determine whether 'Tok' was affected.
120     if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines))
121       SomeTokenAffected = true;
122 
123     // Determine whether the first child of 'Tok' was affected.
124     if (!Tok->Children.empty() && Tok->Children.front()->Affected)
125       SomeFirstChildAffected = true;
126 
127     IncludeLeadingNewlines = Tok->Children.empty();
128   }
129 
130   // Was this line moved, i.e. has it previously been on the same line as an
131   // affected line?
132   bool LineMoved = PreviousLine && PreviousLine->Affected &&
133                    Line->First->NewlinesBefore == 0;
134 
135   bool IsContinuedComment =
136       Line->First->is(tok::comment) && Line->First->Next == nullptr &&
137       Line->First->NewlinesBefore < 2 && PreviousLine &&
138       PreviousLine->Affected && PreviousLine->Last->is(tok::comment);
139 
140   bool IsAffectedClosingBrace =
141       Line->First->is(tok::r_brace) &&
142       Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex &&
143       Lines[Line->MatchingOpeningBlockLineIndex]->Affected;
144 
145   if (SomeTokenAffected || SomeFirstChildAffected || LineMoved ||
146       IsContinuedComment || IsAffectedClosingBrace) {
147     Line->Affected = true;
148     SomeLineAffected = true;
149   }
150   return SomeLineAffected;
151 }
152 
153 } // namespace format
154 } // namespace clang
155