1 //===--- AffectedRangeManager.cpp - Format C++ code -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file implements AffectRangeManager class.
13 //===----------------------------------------------------------------------===//
15 #include "AffectedRangeManager.h"
17 #include "FormatToken.h"
18 #include "TokenAnnotator.h"
23 bool AffectedRangeManager::computeAffectedLines(
24 SmallVectorImpl<AnnotatedLine *> &Lines) {
25 SmallVectorImpl<AnnotatedLine *>::iterator I = Lines.begin();
26 SmallVectorImpl<AnnotatedLine *>::iterator E = Lines.end();
27 bool SomeLineAffected = false;
28 const AnnotatedLine *PreviousLine = nullptr;
30 AnnotatedLine *Line = *I;
31 Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*Line->First);
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;
43 if (affectsTokenRange(*Line->First, *Last,
44 /*IncludeLeadingNewlines=*/false)) {
45 SomeLineAffected = true;
46 markAllAsAffected(I, PPEnd);
52 if (nonPPLineAffected(Line, PreviousLine, Lines))
53 SomeLineAffected = true;
58 return SomeLineAffected;
61 bool AffectedRangeManager::affectsCharSourceRange(
62 const CharSourceRange &Range) {
63 for (SmallVectorImpl<CharSourceRange>::const_iterator I = Ranges.begin(),
66 if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), I->getBegin()) &&
67 !SourceMgr.isBeforeInTranslationUnit(I->getEnd(), Range.getBegin()))
73 bool 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(First.LastNewlineOffset);
79 SourceLocation End = Last.getStartOfNonWhitespace();
80 End = End.getLocWithOffset(Last.TokenText.size());
81 CharSourceRange Range = CharSourceRange::getCharRange(Start, End);
82 return affectsCharSourceRange(Range);
85 bool AffectedRangeManager::affectsLeadingEmptyLines(const FormatToken &Tok) {
86 CharSourceRange EmptyLineRange = CharSourceRange::getCharRange(
87 Tok.WhitespaceRange.getBegin(),
88 Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset));
89 return affectsCharSourceRange(EmptyLineRange);
92 void AffectedRangeManager::markAllAsAffected(
93 SmallVectorImpl<AnnotatedLine *>::iterator I,
94 SmallVectorImpl<AnnotatedLine *>::iterator E) {
96 (*I)->Affected = true;
97 markAllAsAffected((*I)->Children.begin(), (*I)->Children.end());
102 bool AffectedRangeManager::nonPPLineAffected(
103 AnnotatedLine *Line, const AnnotatedLine *PreviousLine,
104 SmallVectorImpl<AnnotatedLine *> &Lines) {
105 bool SomeLineAffected = false;
106 Line->ChildrenAffected = computeAffectedLines(Line->Children);
107 if (Line->ChildrenAffected)
108 SomeLineAffected = true;
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;
116 // Stores whether the first child line of any of this line's tokens is
118 bool SomeFirstChildAffected = false;
120 for (FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
121 // Determine whether 'Tok' was affected.
122 if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines))
123 SomeTokenAffected = true;
125 // Determine whether the first child of 'Tok' was affected.
126 if (!Tok->Children.empty() && Tok->Children.front()->Affected)
127 SomeFirstChildAffected = true;
129 IncludeLeadingNewlines = Tok->Children.empty();
132 // Was this line moved, i.e. has it previously been on the same line as an
134 bool LineMoved = PreviousLine && PreviousLine->Affected &&
135 Line->First->NewlinesBefore == 0;
137 bool IsContinuedComment =
138 Line->First->is(tok::comment) && Line->First->Next == nullptr &&
139 Line->First->NewlinesBefore < 2 && PreviousLine &&
140 PreviousLine->Affected && PreviousLine->Last->is(tok::comment);
142 bool IsAffectedClosingBrace =
143 Line->First->is(tok::r_brace) &&
144 Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex &&
145 Lines[Line->MatchingOpeningBlockLineIndex]->Affected;
147 if (SomeTokenAffected || SomeFirstChildAffected || LineMoved ||
148 IsContinuedComment || IsAffectedClosingBrace) {
149 Line->Affected = true;
150 SomeLineAffected = true;
152 return SomeLineAffected;
155 } // namespace format