Coverage Report

Created: 2021-01-19 06:58

/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
173k
    SmallVectorImpl<AnnotatedLine *> &Lines) {
24
173k
  SmallVectorImpl<AnnotatedLine *>::iterator I = Lines.begin();
25
173k
  SmallVectorImpl<AnnotatedLine *>::iterator E = Lines.end();
26
173k
  bool SomeLineAffected = false;
27
173k
  const AnnotatedLine *PreviousLine = nullptr;
28
318k
  while (I != E) {
29
144k
    AnnotatedLine *Line = *I;
30
144k
    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
144k
    if (Line->InPPDirective) {
35
8.53k
      FormatToken *Last = Line->Last;
36
8.53k
      SmallVectorImpl<AnnotatedLine *>::iterator PPEnd = I + 1;
37
12.7k
      while (PPEnd != E && 
!(*PPEnd)->First->HasUnescapedNewline11.2k
) {
38
4.19k
        Last = (*PPEnd)->Last;
39
4.19k
        ++PPEnd;
40
4.19k
      }
41
42
8.53k
      if (affectsTokenRange(*Line->First, *Last,
43
7.22k
                            /*IncludeLeadingNewlines=*/false)) {
44
7.22k
        SomeLineAffected = true;
45
7.22k
        markAllAsAffected(I, PPEnd);
46
7.22k
      }
47
8.53k
      I = PPEnd;
48
8.53k
      continue;
49
8.53k
    }
50
51
136k
    if (nonPPLineAffected(Line, PreviousLine, Lines))
52
117k
      SomeLineAffected = true;
53
54
136k
    PreviousLine = Line;
55
136k
    ++I;
56
136k
  }
57
173k
  return SomeLineAffected;
58
173k
}
59
60
bool AffectedRangeManager::affectsCharSourceRange(
61
752k
    const CharSourceRange &Range) {
62
752k
  for (SmallVectorImpl<CharSourceRange>::const_iterator I = Ranges.begin(),
63
752k
                                                        E = Ranges.end();
64
908k
       I != E; 
++I155k
) {
65
798k
    if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), I->getBegin()) &&
66
731k
        !SourceMgr.isBeforeInTranslationUnit(I->getEnd(), Range.getBegin()))
67
642k
      return true;
68
798k
  }
69
110k
  return false;
70
752k
}
71
72
bool AffectedRangeManager::affectsTokenRange(const FormatToken &First,
73
                                             const FormatToken &Last,
74
608k
                                             bool IncludeLeadingNewlines) {
75
608k
  SourceLocation Start = First.WhitespaceRange.getBegin();
76
608k
  if (!IncludeLeadingNewlines)
77
146k
    Start = Start.getLocWithOffset(First.LastNewlineOffset);
78
608k
  SourceLocation End = Last.getStartOfNonWhitespace();
79
608k
  End = End.getLocWithOffset(Last.TokenText.size());
80
608k
  CharSourceRange Range = CharSourceRange::getCharRange(Start, End);
81
608k
  return affectsCharSourceRange(Range);
82
608k
}
83
84
144k
bool AffectedRangeManager::affectsLeadingEmptyLines(const FormatToken &Tok) {
85
144k
  CharSourceRange EmptyLineRange = CharSourceRange::getCharRange(
86
144k
      Tok.WhitespaceRange.getBegin(),
87
144k
      Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset));
88
144k
  return affectsCharSourceRange(EmptyLineRange);
89
144k
}
90
91
void AffectedRangeManager::markAllAsAffected(
92
    SmallVectorImpl<AnnotatedLine *>::iterator I,
93
18.2k
    SmallVectorImpl<AnnotatedLine *>::iterator E) {
94
29.1k
  while (I != E) {
95
10.9k
    (*I)->Affected = true;
96
10.9k
    markAllAsAffected((*I)->Children.begin(), (*I)->Children.end());
97
10.9k
    ++I;
98
10.9k
  }
99
18.2k
}
100
101
bool AffectedRangeManager::nonPPLineAffected(
102
    AnnotatedLine *Line, const AnnotatedLine *PreviousLine,
103
136k
    SmallVectorImpl<AnnotatedLine *> &Lines) {
104
136k
  bool SomeLineAffected = false;
105
136k
  Line->ChildrenAffected = computeAffectedLines(Line->Children);
106
136k
  if (Line->ChildrenAffected)
107
2.01k
    SomeLineAffected = true;
108
109
  // Stores whether one of the line's tokens is directly affected.
110
136k
  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
136k
  bool IncludeLeadingNewlines = false;
114
115
  // Stores whether the first child line of any of this line's tokens is
116
  // affected.
117
136k
  bool SomeFirstChildAffected = false;
118
119
735k
  for (FormatToken *Tok = Line->First; Tok; 
Tok = Tok->Next599k
) {
120
    // Determine whether 'Tok' was affected.
121
599k
    if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines))
122
514k
      SomeTokenAffected = true;
123
124
    // Determine whether the first child of 'Tok' was affected.
125
599k
    if (!Tok->Children.empty() && 
Tok->Children.front()->Affected2.13k
)
126
2.10k
      SomeFirstChildAffected = true;
127
128
599k
    IncludeLeadingNewlines = Tok->Children.empty();
129
599k
  }
130
131
  // Was this line moved, i.e. has it previously been on the same line as an
132
  // affected line?
133
136k
  bool LineMoved = PreviousLine && 
PreviousLine->Affected97.7k
&&
134
79.9k
                   Line->First->NewlinesBefore == 0;
135
136
136k
  bool IsContinuedComment =
137
136k
      Line->First->is(tok::comment) && 
Line->First->Next == nullptr2.61k
&&
138
2.04k
      Line->First->NewlinesBefore < 2 && 
PreviousLine1.98k
&&
139
1.05k
      PreviousLine->Affected && 
PreviousLine->Last->is(tok::comment)1.04k
;
140
141
136k
  bool IsAffectedClosingBrace =
142
136k
      Line->First->is(tok::r_brace) &&
143
23.6k
      Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex &&
144
23.4k
      Lines[Line->MatchingOpeningBlockLineIndex]->Affected;
145
146
136k
  if (SomeTokenAffected || 
SomeFirstChildAffected20.8k
||
LineMoved20.8k
||
147
117k
      
IsContinuedComment19.1k
||
IsAffectedClosingBrace19.1k
) {
148
117k
    Line->Affected = true;
149
117k
    SomeLineAffected = true;
150
117k
  }
151
136k
  return SomeLineAffected;
152
136k
}
153
154
} // namespace format
155
} // namespace clang