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 /// \brief 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 *>::iterator I,
25 SmallVectorImpl<AnnotatedLine *>::iterator E) {
26 bool SomeLineAffected = false;
27 const AnnotatedLine *PreviousLine = nullptr;
29 AnnotatedLine *Line = *I;
30 Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*Line->First);
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;
42 if (affectsTokenRange(*Line->First, *Last,
43 /*IncludeLeadingNewlines=*/false)) {
44 SomeLineAffected = true;
45 markAllAsAffected(I, PPEnd);
51 if (nonPPLineAffected(Line, PreviousLine))
52 SomeLineAffected = true;
57 return SomeLineAffected;
60 bool AffectedRangeManager::affectsCharSourceRange(
61 const CharSourceRange &Range) {
62 for (SmallVectorImpl<CharSourceRange>::const_iterator I = Ranges.begin(),
65 if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), I->getBegin()) &&
66 !SourceMgr.isBeforeInTranslationUnit(I->getEnd(), Range.getBegin()))
72 bool AffectedRangeManager::affectsTokenRange(const FormatToken &First,
73 const FormatToken &Last,
74 bool IncludeLeadingNewlines) {
75 SourceLocation Start = First.WhitespaceRange.getBegin();
76 if (!IncludeLeadingNewlines)
77 Start = Start.getLocWithOffset(First.LastNewlineOffset);
78 SourceLocation End = Last.getStartOfNonWhitespace();
79 End = End.getLocWithOffset(Last.TokenText.size());
80 CharSourceRange Range = CharSourceRange::getCharRange(Start, End);
81 return affectsCharSourceRange(Range);
84 bool AffectedRangeManager::affectsLeadingEmptyLines(const FormatToken &Tok) {
85 CharSourceRange EmptyLineRange = CharSourceRange::getCharRange(
86 Tok.WhitespaceRange.getBegin(),
87 Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset));
88 return affectsCharSourceRange(EmptyLineRange);
91 void AffectedRangeManager::markAllAsAffected(
92 SmallVectorImpl<AnnotatedLine *>::iterator I,
93 SmallVectorImpl<AnnotatedLine *>::iterator E) {
95 (*I)->Affected = true;
96 markAllAsAffected((*I)->Children.begin(), (*I)->Children.end());
101 bool AffectedRangeManager::nonPPLineAffected(
102 AnnotatedLine *Line, const AnnotatedLine *PreviousLine) {
103 bool SomeLineAffected = false;
104 Line->ChildrenAffected =
105 computeAffectedLines(Line->Children.begin(), Line->Children.end());
106 if (Line->ChildrenAffected)
107 SomeLineAffected = true;
109 // Stores whether one of the line's tokens is directly affected.
110 bool SomeTokenAffected = false;
111 // Stores whether we need to look at the leading newlines of the next token
112 // in order to determine whether it was affected.
113 bool IncludeLeadingNewlines = false;
115 // Stores whether the first child line of any of this line's tokens is
117 bool SomeFirstChildAffected = false;
119 for (FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
120 // Determine whether 'Tok' was affected.
121 if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines))
122 SomeTokenAffected = true;
124 // Determine whether the first child of 'Tok' was affected.
125 if (!Tok->Children.empty() && Tok->Children.front()->Affected)
126 SomeFirstChildAffected = true;
128 IncludeLeadingNewlines = Tok->Children.empty();
131 // Was this line moved, i.e. has it previously been on the same line as an
133 bool LineMoved = PreviousLine && PreviousLine->Affected &&
134 Line->First->NewlinesBefore == 0;
136 bool IsContinuedComment =
137 Line->First->is(tok::comment) && Line->First->Next == nullptr &&
138 Line->First->NewlinesBefore < 2 && PreviousLine &&
139 PreviousLine->Affected && PreviousLine->Last->is(tok::comment);
141 if (SomeTokenAffected || SomeFirstChildAffected || LineMoved ||
142 IsContinuedComment) {
143 Line->Affected = true;
144 SomeLineAffected = true;
146 return SomeLineAffected;
149 } // namespace format