clang 24.0.0git
AffectedRangeManager.cpp
Go to the documentation of this file.
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
15
16#include "FormatToken.h"
17#include "TokenAnnotator.h"
18
19namespace clang {
20namespace format {
21
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 const auto *PPEnd = I + 1;
38 while (PPEnd != E && !(*PPEnd)->First->HasUnescapedNewline &&
39 (*PPEnd)->First->isNot(tok::eof)) {
40 Last = (*PPEnd)->Last;
41 ++PPEnd;
42 }
43
44 if (affectsTokenRange(*Line->First, *Last,
45 /*IncludeLeadingNewlines=*/false)) {
46 SomeLineAffected = true;
47 markAllAsAffected(I, PPEnd);
48 }
49 I = PPEnd;
50 continue;
51 }
52
53 if (nonPPLineAffected(Line, PreviousLine, Lines))
54 SomeLineAffected = true;
55
56 PreviousLine = Line;
57 ++I;
58 }
59 return SomeLineAffected;
60}
61
63 const CharSourceRange &Range) {
64 for (const CharSourceRange &R : Ranges) {
65 if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), R.getBegin()) &&
66 !SourceMgr.isBeforeInTranslationUnit(R.getEnd(), Range.getBegin())) {
67 return true;
68 }
69 }
70 return false;
71}
72
73bool 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());
82 return affectsCharSourceRange(Range);
83}
84
85bool AffectedRangeManager::affectsLeadingEmptyLines(const FormatToken &Tok) {
87 Tok.WhitespaceRange.getBegin(),
88 Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset));
89 return affectsCharSourceRange(EmptyLineRange);
90}
91
92void AffectedRangeManager::markAllAsAffected(
95 while (I != E) {
96 (*I)->Affected = true;
97 markAllAsAffected((*I)->Children.begin(), (*I)->Children.end());
98 ++I;
99 }
100}
101
102bool AffectedRangeManager::nonPPLineAffected(
103 AnnotatedLine *Line, const AnnotatedLine *PreviousLine,
105 bool SomeLineAffected = false;
106 Line->ChildrenAffected = computeAffectedLines(Line->Children);
107 if (Line->ChildrenAffected)
108 SomeLineAffected = true;
109
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;
115
116 // Stores whether the first child line of any of this line's tokens is
117 // affected.
118 bool SomeFirstChildAffected = false;
119
120 assert(Line->First);
121 for (FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
122 // Determine whether 'Tok' was affected.
123 if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines))
124 SomeTokenAffected = true;
125
126 // Determine whether the first child of 'Tok' was affected.
127 if (!Tok->Children.empty() && Tok->Children.front()->Affected)
128 SomeFirstChildAffected = true;
129
130 IncludeLeadingNewlines = Tok->Children.empty();
131 }
132
133 // Was this line moved, i.e. has it previously been on the same line as an
134 // affected line?
135 bool LineMoved = PreviousLine && PreviousLine->Affected &&
136 Line->First->NewlinesBefore == 0;
137
138 bool IsContinuedComment =
139 Line->First->is(tok::comment) && !Line->First->Next &&
140 Line->First->NewlinesBefore < 2 && PreviousLine &&
141 PreviousLine->Affected && PreviousLine->Last->is(tok::comment);
142
143 bool IsAffectedClosingBrace =
144 Line->First->is(tok::r_brace) &&
145 Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex &&
146 Lines[Line->MatchingOpeningBlockLineIndex]->Affected;
147
148 if (SomeTokenAffected || SomeFirstChildAffected || LineMoved ||
149 IsContinuedComment || IsAffectedClosingBrace) {
150 Line->Affected = true;
151 SomeLineAffected = true;
152 }
153 return SomeLineAffected;
154}
155
156} // namespace format
157} // namespace clang
AffectedRangeManager class manages affected ranges in the code.
This file contains the declaration of the FormatToken, a wrapper around Token with additional informa...
FormatToken()
Token Tok
The Token.
This file implements a token annotator, i.e.
Represents a byte-granular source range.
static CharSourceRange getCharRange(SourceRange R)
Encodes a location in the source.
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
bool computeAffectedLines(SmallVectorImpl< AnnotatedLine * > &Lines)
bool affectsCharSourceRange(const CharSourceRange &Range)
Top level wrappers for InstallAPI frontend operations.
A wrapper around a Token storing information about the whitespace characters preceding it.