blob: 7ad1f7070d0a293c524e9a89ec5ca6500946c1ce [file] [log] [blame]
Eric Liu4cfb88a2016-04-25 15:09:22 +00001//===--- AffectedRangeManager.cpp - Format C++ code -----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Eric Liu4cfb88a2016-04-25 15:09:22 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010/// This file implements AffectRangeManager class.
Eric Liu4cfb88a2016-04-25 15:09:22 +000011///
12//===----------------------------------------------------------------------===//
13
14#include "AffectedRangeManager.h"
15
16#include "FormatToken.h"
17#include "TokenAnnotator.h"
18
19namespace clang {
20namespace format {
21
22bool AffectedRangeManager::computeAffectedLines(
Manuel Klimek0dddcf72018-04-23 09:34:26 +000023 SmallVectorImpl<AnnotatedLine *> &Lines) {
24 SmallVectorImpl<AnnotatedLine *>::iterator I = Lines.begin();
25 SmallVectorImpl<AnnotatedLine *>::iterator E = Lines.end();
Eric Liu4cfb88a2016-04-25 15:09:22 +000026 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
Manuel Klimek0dddcf72018-04-23 09:34:26 +000051 if (nonPPLineAffected(Line, PreviousLine, Lines))
Eric Liu4cfb88a2016-04-25 15:09:22 +000052 SomeLineAffected = true;
53
54 PreviousLine = Line;
55 ++I;
56 }
57 return SomeLineAffected;
58}
59
60bool AffectedRangeManager::affectsCharSourceRange(
61 const CharSourceRange &Range) {
62 for (SmallVectorImpl<CharSourceRange>::const_iterator I = Ranges.begin(),
63 E = Ranges.end();
64 I != E; ++I) {
65 if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), I->getBegin()) &&
66 !SourceMgr.isBeforeInTranslationUnit(I->getEnd(), Range.getBegin()))
67 return true;
68 }
69 return false;
70}
71
72bool 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);
82}
83
84bool 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);
89}
90
91void AffectedRangeManager::markAllAsAffected(
92 SmallVectorImpl<AnnotatedLine *>::iterator I,
93 SmallVectorImpl<AnnotatedLine *>::iterator E) {
94 while (I != E) {
95 (*I)->Affected = true;
96 markAllAsAffected((*I)->Children.begin(), (*I)->Children.end());
97 ++I;
98 }
99}
100
101bool AffectedRangeManager::nonPPLineAffected(
Manuel Klimek0dddcf72018-04-23 09:34:26 +0000102 AnnotatedLine *Line, const AnnotatedLine *PreviousLine,
103 SmallVectorImpl<AnnotatedLine *> &Lines) {
Eric Liu4cfb88a2016-04-25 15:09:22 +0000104 bool SomeLineAffected = false;
Manuel Klimek0dddcf72018-04-23 09:34:26 +0000105 Line->ChildrenAffected = computeAffectedLines(Line->Children);
Eric Liu4cfb88a2016-04-25 15:09:22 +0000106 if (Line->ChildrenAffected)
107 SomeLineAffected = true;
108
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;
114
115 // Stores whether the first child line of any of this line's tokens is
116 // affected.
117 bool SomeFirstChildAffected = false;
118
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;
123
124 // Determine whether the first child of 'Tok' was affected.
125 if (!Tok->Children.empty() && Tok->Children.front()->Affected)
126 SomeFirstChildAffected = true;
127
128 IncludeLeadingNewlines = Tok->Children.empty();
129 }
130
131 // Was this line moved, i.e. has it previously been on the same line as an
132 // affected line?
133 bool LineMoved = PreviousLine && PreviousLine->Affected &&
134 Line->First->NewlinesBefore == 0;
135
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);
140
Manuel Klimek0dddcf72018-04-23 09:34:26 +0000141 bool IsAffectedClosingBrace =
142 Line->First->is(tok::r_brace) &&
143 Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex &&
144 Lines[Line->MatchingOpeningBlockLineIndex]->Affected;
145
Eric Liu4cfb88a2016-04-25 15:09:22 +0000146 if (SomeTokenAffected || SomeFirstChildAffected || LineMoved ||
Manuel Klimek0dddcf72018-04-23 09:34:26 +0000147 IsContinuedComment || IsAffectedClosingBrace) {
Eric Liu4cfb88a2016-04-25 15:09:22 +0000148 Line->Affected = true;
149 SomeLineAffected = true;
150 }
151 return SomeLineAffected;
152}
153
154} // namespace format
155} // namespace clang