Coverage Report

Created: 2021-08-24 07:12

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Format/AffectedRangeManager.cpp
Line
Count
Source
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
14
#include "AffectedRangeManager.h"
15
16
#include "FormatToken.h"
17
#include "TokenAnnotator.h"
18
19
namespace clang {
20
namespace format {
21
22
bool AffectedRangeManager::computeAffectedLines(
23
195k
    SmallVectorImpl<AnnotatedLine *> &Lines) {
24
195k
  SmallVectorImpl<AnnotatedLine *>::iterator I = Lines.begin();
25
195k
  SmallVectorImpl<AnnotatedLine *>::iterator E = Lines.end();
26
195k
  bool SomeLineAffected = false;
27
195k
  const AnnotatedLine *PreviousLine = nullptr;
28
359k
  while (I != E) {
29
163k
    AnnotatedLine *Line = *I;
30
163k
    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
163k
    if (Line->InPPDirective) {
35
9.02k
      FormatToken *Last = Line->Last;
36
9.02k
      SmallVectorImpl<AnnotatedLine *>::iterator PPEnd = I + 1;
37
13.5k
      while (PPEnd != E && 
!(*PPEnd)->First->HasUnescapedNewline11.9k
) {
38
4.49k
        Last = (*PPEnd)->Last;
39
4.49k
        ++PPEnd;
40
4.49k
      }
41
42
9.02k
      if (affectsTokenRange(*Line->First, *Last,
43
9.02k
                            /*IncludeLeadingNewlines=*/false)) {
44
7.70k
        SomeLineAffected = true;
45
7.70k
        markAllAsAffected(I, PPEnd);
46
7.70k
      }
47
9.02k
      I = PPEnd;
48
9.02k
      continue;
49
9.02k
    }
50
51
154k
    if (nonPPLineAffected(Line, PreviousLine, Lines))
52
135k
      SomeLineAffected = true;
53
54
154k
    PreviousLine = Line;
55
154k
    ++I;
56
154k
  }
57
195k
  return SomeLineAffected;
58
195k
}
59
60
bool AffectedRangeManager::affectsCharSourceRange(
61
847k
    const CharSourceRange &Range) {
62
847k
  for (SmallVectorImpl<CharSourceRange>::const_iterator I = Ranges.begin(),
63
847k
                                                        E = Ranges.end();
64
1.00M
       I != E; 
++I156k
) {
65
893k
    if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), I->getBegin()) &&
66
893k
        
!SourceMgr.isBeforeInTranslationUnit(I->getEnd(), Range.getBegin())825k
)
67
736k
      return true;
68
893k
  }
69
110k
  return false;
70
847k
}
71
72
bool AffectedRangeManager::affectsTokenRange(const FormatToken &First,
73
                                             const FormatToken &Last,
74
683k
                                             bool IncludeLeadingNewlines) {
75
683k
  SourceLocation Start = First.WhitespaceRange.getBegin();
76
683k
  if (!IncludeLeadingNewlines)
77
165k
    Start = Start.getLocWithOffset(First.LastNewlineOffset);
78
683k
  SourceLocation End = Last.getStartOfNonWhitespace();
79
683k
  End = End.getLocWithOffset(Last.TokenText.size());
80
683k
  CharSourceRange Range = CharSourceRange::getCharRange(Start, End);
81
683k
  return affectsCharSourceRange(Range);
82
683k
}
83
84
163k
bool AffectedRangeManager::affectsLeadingEmptyLines(const FormatToken &Tok) {
85
163k
  CharSourceRange EmptyLineRange = CharSourceRange::getCharRange(
86
163k
      Tok.WhitespaceRange.getBegin(),
87
163k
      Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset));
88
163k
  return affectsCharSourceRange(EmptyLineRange);
89
163k
}
90
91
void AffectedRangeManager::markAllAsAffected(
92
    SmallVectorImpl<AnnotatedLine *>::iterator I,
93
19.4k
    SmallVectorImpl<AnnotatedLine *>::iterator E) {
94
31.2k
  while (I != E) {
95
11.7k
    (*I)->Affected = true;
96
11.7k
    markAllAsAffected((*I)->Children.begin(), (*I)->Children.end());
97
11.7k
    ++I;
98
11.7k
  }
99
19.4k
}
100
101
bool AffectedRangeManager::nonPPLineAffected(
102
    AnnotatedLine *Line, const AnnotatedLine *PreviousLine,
103
154k
    SmallVectorImpl<AnnotatedLine *> &Lines) {
104
154k
  bool SomeLineAffected = false;
105
154k
  Line->ChildrenAffected = computeAffectedLines(Line->Children);
106
154k
  if (Line->ChildrenAffected)
107
2.17k
    SomeLineAffected = true;
108
109
  // Stores whether one of the line's tokens is directly affected.
110
154k
  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
154k
  bool IncludeLeadingNewlines = false;
114
115
  // Stores whether the first child line of any of this line's tokens is
116
  // affected.
117
154k
  bool SomeFirstChildAffected = false;
118
119
829k
  for (FormatToken *Tok = Line->First; Tok; 
Tok = Tok->Next674k
) {
120
    // Determine whether 'Tok' was affected.
121
674k
    if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines))
122
589k
      SomeTokenAffected = true;
123
124
    // Determine whether the first child of 'Tok' was affected.
125
674k
    if (!Tok->Children.empty() && 
Tok->Children.front()->Affected2.29k
)
126
2.26k
      SomeFirstChildAffected = true;
127
128
674k
    IncludeLeadingNewlines = Tok->Children.empty();
129
674k
  }
130
131
  // Was this line moved, i.e. has it previously been on the same line as an
132
  // affected line?
133
154k
  bool LineMoved = PreviousLine && 
PreviousLine->Affected112k
&&
134
154k
                   
Line->First->NewlinesBefore == 094.5k
;
135
136
154k
  bool IsContinuedComment =
137
154k
      Line->First->is(tok::comment) && 
Line->First->Next == nullptr3.26k
&&
138
154k
      
Line->First->NewlinesBefore < 22.52k
&&
PreviousLine2.26k
&&
139
154k
      
PreviousLine->Affected1.25k
&&
PreviousLine->Last->is(tok::comment)1.23k
;
140
141
154k
  bool IsAffectedClosingBrace =
142
154k
      Line->First->is(tok::r_brace) &&
143
154k
      
Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex26.3k
&&
144
154k
      
Lines[Line->MatchingOpeningBlockLineIndex]->Affected26.1k
;
145
146
154k
  if (SomeTokenAffected || 
SomeFirstChildAffected21.0k
||
LineMoved21.0k
||
147
154k
      
IsContinuedComment19.2k
||
IsAffectedClosingBrace19.2k
) {
148
135k
    Line->Affected = true;
149
135k
    SomeLineAffected = true;
150
135k
  }
151
154k
  return SomeLineAffected;
152
154k
}
153
154
} // namespace format
155
} // namespace clang