xref: /llvm-project/clang/lib/Format/AffectedRangeManager.cpp (revision 01f355fe95f6b45db8bb239239b7ed978e094a13)
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     Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*Line->First);
31 
32     // If a line is part of a preprocessor directive, it needs to be formatted
33     // if any token within the directive is affected.
34     if (Line->InPPDirective) {
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                             /*IncludeLeadingNewlines=*/false)) {
44         SomeLineAffected = true;
45         markAllAsAffected(I, PPEnd);
46       }
47       I = PPEnd;
48       continue;
49     }
50 
51     if (nonPPLineAffected(Line, PreviousLine, Lines))
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);
103   if (Line->ChildrenAffected)
104     SomeLineAffected = true;
105 
106   // Stores whether one of the line's tokens is directly affected.
107   bool SomeTokenAffected = false;
108   // Stores whether we need to look at the leading newlines of the next token
109   // in order to determine whether it was affected.
110   bool IncludeLeadingNewlines = false;
111 
112   // Stores whether the first child line of any of this line's tokens is
113   // affected.
114   bool SomeFirstChildAffected = false;
115 
116   for (FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
117     // Determine whether 'Tok' was affected.
118     if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines))
119       SomeTokenAffected = true;
120 
121     // Determine whether the first child of 'Tok' was affected.
122     if (!Tok->Children.empty() && Tok->Children.front()->Affected)
123       SomeFirstChildAffected = true;
124 
125     IncludeLeadingNewlines = Tok->Children.empty();
126   }
127 
128   // Was this line moved, i.e. has it previously been on the same line as an
129   // affected line?
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 &&
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 } // namespace format
152 } // namespace clang
153