clang 19.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 "TokenAnnotator.h"
17
18namespace clang {
19namespace format {
20
25 bool SomeLineAffected = false;
26 const AnnotatedLine *PreviousLine = nullptr;
27 while (I != E) {
28 AnnotatedLine *Line = *I;
29 assert(Line->First);
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;
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
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 }
67 }
68 return false;
69}
70
71bool AffectedRangeManager::affectsTokenRange(const FormatToken &First,
72 const FormatToken &Last,
73 bool IncludeLeadingNewlines) {
74 SourceLocation Start = First.WhitespaceRange.getBegin();
75 if (!IncludeLeadingNewlines)
76 Start = Start.getLocWithOffset(First.LastNewlineOffset);
77 SourceLocation End = Last.getStartOfNonWhitespace();
78 End = End.getLocWithOffset(Last.TokenText.size());
80 return affectsCharSourceRange(Range);
81}
82
83bool AffectedRangeManager::affectsLeadingEmptyLines(const FormatToken &Tok) {
85 Tok.WhitespaceRange.getBegin(),
86 Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset));
87 return affectsCharSourceRange(EmptyLineRange);
88}
89
90void AffectedRangeManager::markAllAsAffected(
91 SmallVectorImpl<AnnotatedLine *>::iterator I,
92 SmallVectorImpl<AnnotatedLine *>::iterator E) {
93 while (I != E) {
94 (*I)->Affected = true;
95 markAllAsAffected((*I)->Children.begin(), (*I)->Children.end());
96 ++I;
97 }
98}
99
100bool AffectedRangeManager::nonPPLineAffected(
101 AnnotatedLine *Line, const AnnotatedLine *PreviousLine,
102 SmallVectorImpl<AnnotatedLine *> &Lines) {
103 bool SomeLineAffected = false;
104 Line->ChildrenAffected = computeAffectedLines(Line->Children);
105 if (Line->ChildrenAffected)
106 SomeLineAffected = true;
107
108 // Stores whether one of the line's tokens is directly affected.
109 bool SomeTokenAffected = false;
110 // Stores whether we need to look at the leading newlines of the next token
111 // in order to determine whether it was affected.
112 bool IncludeLeadingNewlines = false;
113
114 // Stores whether the first child line of any of this line's tokens is
115 // affected.
116 bool SomeFirstChildAffected = false;
117
118 assert(Line->First);
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 &&
138 Line->First->NewlinesBefore < 2 && PreviousLine &&
139 PreviousLine->Affected && PreviousLine->Last->is(tok::comment);
140
141 bool IsAffectedClosingBrace =
142 Line->First->is(tok::r_brace) &&
143 Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex &&
144 Lines[Line->MatchingOpeningBlockLineIndex]->Affected;
145
146 if (SomeTokenAffected || SomeFirstChildAffected || LineMoved ||
147 IsContinuedComment || IsAffectedClosingBrace) {
148 Line->Affected = true;
149 SomeLineAffected = true;
150 }
151 return SomeLineAffected;
152}
153
154} // namespace format
155} // namespace clang
AffectedRangeManager class manages affected ranges in the code.
This file implements a token annotator, i.e.
Represents a character-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 isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const
Determines the order of 2 source locations in the translation unit.
bool computeAffectedLines(SmallVectorImpl< AnnotatedLine * > &Lines)
bool affectsCharSourceRange(const CharSourceRange &Range)
The JSON file list parser is used to communicate input to InstallAPI.
A wrapper around a Token storing information about the whitespace characters preceding it.
Definition: FormatToken.h:283
static const size_t kInvalidIndex