Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Format/AffectedRangeManager.cpp
Line
Count
Source (jump to first uncovered line)
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
295k
    SmallVectorImpl<AnnotatedLine *> &Lines) {
24
295k
  SmallVectorImpl<AnnotatedLine *>::iterator I = Lines.begin();
25
295k
  SmallVectorImpl<AnnotatedLine *>::iterator E = Lines.end();
26
295k
  bool SomeLineAffected = false;
27
295k
  const AnnotatedLine *PreviousLine = nullptr;
28
540k
  while (I != E) {
29
244k
    AnnotatedLine *Line = *I;
30
244k
    assert(Line->First);
31
0
    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
244k
    if (Line->InPPDirective) {
36
13.2k
      FormatToken *Last = Line->Last;
37
13.2k
      SmallVectorImpl<AnnotatedLine *>::iterator PPEnd = I + 1;
38
20.1k
      while (PPEnd != E && 
!(*PPEnd)->First->HasUnescapedNewline17.3k
) {
39
6.86k
        Last = (*PPEnd)->Last;
40
6.86k
        ++PPEnd;
41
6.86k
      }
42
43
13.2k
      if (affectsTokenRange(*Line->First, *Last,
44
13.2k
                            /*IncludeLeadingNewlines=*/false)) {
45
11.9k
        SomeLineAffected = true;
46
11.9k
        markAllAsAffected(I, PPEnd);
47
11.9k
      }
48
13.2k
      I = PPEnd;
49
13.2k
      continue;
50
13.2k
    }
51
52
231k
    if (nonPPLineAffected(Line, PreviousLine, Lines))
53
212k
      SomeLineAffected = true;
54
55
231k
    PreviousLine = Line;
56
231k
    ++I;
57
231k
  }
58
295k
  return SomeLineAffected;
59
295k
}
60
61
bool AffectedRangeManager::affectsCharSourceRange(
62
1.29M
    const CharSourceRange &Range) {
63
1.34M
  for (const CharSourceRange &R : Ranges) {
64
1.34M
    if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), R.getBegin()) &&
65
1.34M
        
!SourceMgr.isBeforeInTranslationUnit(R.getEnd(), Range.getBegin())1.27M
) {
66
1.18M
      return true;
67
1.18M
    }
68
1.34M
  }
69
110k
  return false;
70
1.29M
}
71
72
bool AffectedRangeManager::affectsTokenRange(const FormatToken &First,
73
                                             const FormatToken &Last,
74
1.05M
                                             bool IncludeLeadingNewlines) {
75
1.05M
  SourceLocation Start = First.WhitespaceRange.getBegin();
76
1.05M
  if (!IncludeLeadingNewlines)
77
248k
    Start = Start.getLocWithOffset(First.LastNewlineOffset);
78
1.05M
  SourceLocation End = Last.getStartOfNonWhitespace();
79
1.05M
  End = End.getLocWithOffset(Last.TokenText.size());
80
1.05M
  CharSourceRange Range = CharSourceRange::getCharRange(Start, End);
81
1.05M
  return affectsCharSourceRange(Range);
82
1.05M
}
83
84
244k
bool AffectedRangeManager::affectsLeadingEmptyLines(const FormatToken &Tok) {
85
244k
  CharSourceRange EmptyLineRange = CharSourceRange::getCharRange(
86
244k
      Tok.WhitespaceRange.getBegin(),
87
244k
      Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset));
88
244k
  return affectsCharSourceRange(EmptyLineRange);
89
244k
}
90
91
void AffectedRangeManager::markAllAsAffected(
92
    SmallVectorImpl<AnnotatedLine *>::iterator I,
93
30.3k
    SmallVectorImpl<AnnotatedLine *>::iterator E) {
94
48.7k
  while (I != E) {
95
18.4k
    (*I)->Affected = true;
96
18.4k
    markAllAsAffected((*I)->Children.begin(), (*I)->Children.end());
97
18.4k
    ++I;
98
18.4k
  }
99
30.3k
}
100
101
bool AffectedRangeManager::nonPPLineAffected(
102
    AnnotatedLine *Line, const AnnotatedLine *PreviousLine,
103
231k
    SmallVectorImpl<AnnotatedLine *> &Lines) {
104
231k
  bool SomeLineAffected = false;
105
231k
  Line->ChildrenAffected = computeAffectedLines(Line->Children);
106
231k
  if (Line->ChildrenAffected)
107
3.73k
    SomeLineAffected = true;
108
109
  // Stores whether one of the line's tokens is directly affected.
110
231k
  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
231k
  bool IncludeLeadingNewlines = false;
114
115
  // Stores whether the first child line of any of this line's tokens is
116
  // affected.
117
231k
  bool SomeFirstChildAffected = false;
118
119
231k
  assert(Line->First);
120
1.27M
  for (FormatToken *Tok = Line->First; Tok; 
Tok = Tok->Next1.04M
) {
121
    // Determine whether 'Tok' was affected.
122
1.04M
    if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines))
123
955k
      SomeTokenAffected = true;
124
125
    // Determine whether the first child of 'Tok' was affected.
126
1.04M
    if (!Tok->Children.empty() && 
Tok->Children.front()->Affected3.99k
)
127
3.95k
      SomeFirstChildAffected = true;
128
129
1.04M
    IncludeLeadingNewlines = Tok->Children.empty();
130
1.04M
  }
131
132
  // Was this line moved, i.e. has it previously been on the same line as an
133
  // affected line?
134
231k
  bool LineMoved = PreviousLine && 
PreviousLine->Affected164k
&&
135
231k
                   
Line->First->NewlinesBefore == 0146k
;
136
137
231k
  bool IsContinuedComment =
138
231k
      Line->First->is(tok::comment) && 
Line->First->Next == nullptr4.92k
&&
139
231k
      
Line->First->NewlinesBefore < 23.83k
&&
PreviousLine3.23k
&&
140
231k
      
PreviousLine->Affected1.95k
&&
PreviousLine->Last->is(tok::comment)1.93k
;
141
142
231k
  bool IsAffectedClosingBrace =
143
231k
      Line->First->is(tok::r_brace) &&
144
231k
      
Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex36.7k
&&
145
231k
      
Lines[Line->MatchingOpeningBlockLineIndex]->Affected36.4k
;
146
147
231k
  if (SomeTokenAffected || 
SomeFirstChildAffected21.0k
||
LineMoved21.0k
||
148
231k
      
IsContinuedComment19.3k
||
IsAffectedClosingBrace19.3k
) {
149
212k
    Line->Affected = true;
150
212k
    SomeLineAffected = true;
151
212k
  }
152
231k
  return SomeLineAffected;
153
231k
}
154
155
} // namespace format
156
} // namespace clang