Coverage Report

Created: 2020-03-31 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Format/NamespaceEndCommentsFixer.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- NamespaceEndCommentsFixer.cpp --------------------------*- C++ -*-===//
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 NamespaceEndCommentsFixer, a TokenAnalyzer that
11
/// fixes namespace end comments.
12
///
13
//===----------------------------------------------------------------------===//
14
15
#include "NamespaceEndCommentsFixer.h"
16
#include "llvm/Support/Debug.h"
17
#include "llvm/Support/Regex.h"
18
19
#define DEBUG_TYPE "namespace-end-comments-fixer"
20
21
namespace clang {
22
namespace format {
23
24
namespace {
25
// The maximal number of unwrapped lines that a short namespace spans.
26
// Short namespaces don't need an end comment.
27
static const int kShortNamespaceMaxLines = 1;
28
29
// Computes the name of a namespace given the namespace token.
30
// Returns "" for anonymous namespace.
31
556
std::string computeName(const FormatToken *NamespaceTok) {
32
556
  assert(NamespaceTok &&
33
556
         NamespaceTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) &&
34
556
         "expecting a namespace token");
35
556
  std::string name = "";
36
556
  const FormatToken *Tok = NamespaceTok->getNextNonComment();
37
556
  if (NamespaceTok->is(TT_NamespaceMacro)) {
38
68
    // Collects all the non-comment tokens between opening parenthesis
39
68
    // and closing parenthesis or comma.
40
68
    assert(Tok && Tok->is(tok::l_paren) && "expected an opening parenthesis");
41
68
    Tok = Tok->getNextNonComment();
42
139
    while (Tok && !Tok->isOneOf(tok::r_paren, tok::comma)) {
43
71
      name += Tok->TokenText;
44
71
      Tok = Tok->getNextNonComment();
45
71
    }
46
488
  } else {
47
488
    // For `namespace [[foo]] A::B::inline C {` or
48
488
    // `namespace MACRO1 MACRO2 A::B::inline C {`, returns "A::B::inline C".
49
488
    // Peek for the first '::' (or '{') and then return all tokens from one
50
488
    // token before that up until the '{'.
51
488
    const FormatToken *FirstNSTok = Tok;
52
973
    while (Tok && 
!Tok->is(tok::l_brace)950
&&
!Tok->is(tok::coloncolon)492
) {
53
485
      FirstNSTok = Tok;
54
485
      Tok = Tok->getNextNonComment();
55
485
    }
56
488
57
488
    Tok = FirstNSTok;
58
973
    while (Tok && 
!Tok->is(tok::l_brace)950
) {
59
485
      name += Tok->TokenText;
60
485
      if (Tok->is(tok::kw_inline))
61
2
        name += " ";
62
485
      Tok = Tok->getNextNonComment();
63
485
    }
64
488
  }
65
556
  return name;
66
556
}
67
68
std::string computeEndCommentText(StringRef NamespaceName, bool AddNewline,
69
528
                                  const FormatToken *NamespaceTok) {
70
528
  std::string text = "// ";
71
528
  text += NamespaceTok->TokenText;
72
528
  if (NamespaceTok->is(TT_NamespaceMacro))
73
61
    text += "(";
74
467
  else if (!NamespaceName.empty())
75
445
    text += ' ';
76
528
  text += NamespaceName;
77
528
  if (NamespaceTok->is(TT_NamespaceMacro))
78
61
    text += ")";
79
528
  if (AddNewline)
80
57
    text += '\n';
81
528
  return text;
82
528
}
83
84
849
bool hasEndComment(const FormatToken *RBraceTok) {
85
849
  return RBraceTok->Next && 
RBraceTok->Next->is(tok::comment)536
;
86
849
}
87
88
bool validEndComment(const FormatToken *RBraceTok, StringRef NamespaceName,
89
227
                     const FormatToken *NamespaceTok) {
90
227
  assert(hasEndComment(RBraceTok));
91
227
  const FormatToken *Comment = RBraceTok->Next;
92
227
93
227
  // Matches a valid namespace end comment.
94
227
  // Valid namespace end comments don't need to be edited.
95
227
  static const llvm::Regex NamespaceCommentPattern =
96
227
      llvm::Regex("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
97
227
                  "namespace( +([a-zA-Z0-9:_]+))?\\.? *(\\*/)?$",
98
227
                  llvm::Regex::IgnoreCase);
99
227
  static const llvm::Regex NamespaceMacroCommentPattern =
100
227
      llvm::Regex("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
101
227
                  "([a-zA-Z0-9_]+)\\(([a-zA-Z0-9:_]*)\\)\\.? *(\\*/)?$",
102
227
                  llvm::Regex::IgnoreCase);
103
227
104
227
  SmallVector<StringRef, 8> Groups;
105
227
  if (NamespaceTok->is(TT_NamespaceMacro) &&
106
227
      
NamespaceMacroCommentPattern.match(Comment->TokenText, &Groups)53
) {
107
46
    StringRef NamespaceTokenText = Groups.size() > 4 ? Groups[4] : 
""0
;
108
46
    // The name of the macro must be used.
109
46
    if (NamespaceTokenText != NamespaceTok->TokenText)
110
4
      return false;
111
181
  } else if (NamespaceTok->isNot(tok::kw_namespace) ||
112
181
             
!NamespaceCommentPattern.match(Comment->TokenText, &Groups)174
) {
113
16
    // Comment does not match regex.
114
16
    return false;
115
16
  }
116
207
  StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] : 
""0
;
117
207
  // Anonymous namespace comments must not mention a namespace name.
118
207
  if (NamespaceName.empty() && 
!NamespaceNameInComment.empty()12
)
119
4
    return false;
120
203
  StringRef AnonymousInComment = Groups.size() > 3 ? Groups[3] : 
""0
;
121
203
  // Named namespace comments must not mention anonymous namespace.
122
203
  if (!NamespaceName.empty() && 
!AnonymousInComment.empty()195
)
123
2
    return false;
124
201
  return NamespaceNameInComment == NamespaceName;
125
201
}
126
127
void addEndComment(const FormatToken *RBraceTok, StringRef EndCommentText,
128
                   const SourceManager &SourceMgr,
129
188
                   tooling::Replacements *Fixes) {
130
188
  auto EndLoc = RBraceTok->Tok.getEndLoc();
131
188
  auto Range = CharSourceRange::getCharRange(EndLoc, EndLoc);
132
188
  auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, EndCommentText));
133
188
  if (Err) {
134
0
    llvm::errs() << "Error while adding namespace end comment: "
135
0
                 << llvm::toString(std::move(Err)) << "\n";
136
0
  }
137
188
}
138
139
void updateEndComment(const FormatToken *RBraceTok, StringRef EndCommentText,
140
                      const SourceManager &SourceMgr,
141
66
                      tooling::Replacements *Fixes) {
142
66
  assert(hasEndComment(RBraceTok));
143
66
  const FormatToken *Comment = RBraceTok->Next;
144
66
  auto Range = CharSourceRange::getCharRange(Comment->getStartOfNonWhitespace(),
145
66
                                             Comment->Tok.getEndLoc());
146
66
  auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, EndCommentText));
147
66
  if (Err) {
148
0
    llvm::errs() << "Error while updating namespace end comment: "
149
0
                 << llvm::toString(std::move(Err)) << "\n";
150
0
  }
151
66
}
152
} // namespace
153
154
const FormatToken *
155
getNamespaceToken(const AnnotatedLine *Line,
156
37.9k
                  const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
157
37.9k
  if (!Line->Affected || 
Line->InPPDirective31.1k
||
!Line->startsWith(tok::r_brace)28.3k
)
158
30.5k
    return nullptr;
159
7.44k
  size_t StartLineIndex = Line->MatchingOpeningBlockLineIndex;
160
7.44k
  if (StartLineIndex == UnwrappedLine::kInvalidIndex)
161
60
    return nullptr;
162
7.38k
  assert(StartLineIndex < AnnotatedLines.size());
163
7.38k
  const FormatToken *NamespaceTok = AnnotatedLines[StartLineIndex]->First;
164
7.38k
  if (NamespaceTok->is(tok::l_brace)) {
165
1.27k
    // "namespace" keyword can be on the line preceding '{', e.g. in styles
166
1.27k
    // where BraceWrapping.AfterNamespace is true.
167
1.27k
    if (StartLineIndex > 0)
168
1.10k
      NamespaceTok = AnnotatedLines[StartLineIndex - 1]->First;
169
1.27k
  }
170
7.38k
  return NamespaceTok->getNamespaceToken();
171
7.38k
}
172
173
StringRef
174
getNamespaceTokenText(const AnnotatedLine *Line,
175
65
                      const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
176
65
  const FormatToken *NamespaceTok = getNamespaceToken(Line, AnnotatedLines);
177
65
  return NamespaceTok ? 
NamespaceTok->TokenText32
:
StringRef()33
;
178
65
}
179
180
NamespaceEndCommentsFixer::NamespaceEndCommentsFixer(const Environment &Env,
181
                                                     const FormatStyle &Style)
182
7.75k
    : TokenAnalyzer(Env, Style) {}
183
184
std::pair<tooling::Replacements, unsigned> NamespaceEndCommentsFixer::analyze(
185
    TokenAnnotator &Annotator, SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
186
7.84k
    FormatTokenLexer &Tokens) {
187
7.84k
  const SourceManager &SourceMgr = Env.getSourceManager();
188
7.84k
  AffectedRangeMgr.computeAffectedLines(AnnotatedLines);
189
7.84k
  tooling::Replacements Fixes;
190
7.84k
  std::string AllNamespaceNames = "";
191
7.84k
  size_t StartLineIndex = SIZE_MAX;
192
7.84k
  StringRef NamespaceTokenText;
193
7.84k
  unsigned int CompactedNamespacesCount = 0;
194
42.4k
  for (size_t I = 0, E = AnnotatedLines.size(); I != E; 
++I34.6k
) {
195
34.6k
    const AnnotatedLine *EndLine = AnnotatedLines[I];
196
34.6k
    const FormatToken *NamespaceTok =
197
34.6k
        getNamespaceToken(EndLine, AnnotatedLines);
198
34.6k
    if (!NamespaceTok)
199
34.0k
      continue;
200
557
    FormatToken *RBraceTok = EndLine->First;
201
557
    if (RBraceTok->Finalized)
202
1
      continue;
203
556
    RBraceTok->Finalized = true;
204
556
    const FormatToken *EndCommentPrevTok = RBraceTok;
205
556
    // Namespaces often end with '};'. In that case, attach namespace end
206
556
    // comments to the semicolon tokens.
207
556
    if (RBraceTok->Next && 
RBraceTok->Next->is(tok::semi)265
) {
208
39
      EndCommentPrevTok = RBraceTok->Next;
209
39
    }
210
556
    if (StartLineIndex == SIZE_MAX)
211
556
      
StartLineIndex = EndLine->MatchingOpeningBlockLineIndex528
;
212
556
    std::string NamespaceName = computeName(NamespaceTok);
213
556
    if (Style.CompactNamespaces) {
214
65
      if (CompactedNamespacesCount == 0)
215
37
        NamespaceTokenText = NamespaceTok->TokenText;
216
65
      if ((I + 1 < E) &&
217
65
          NamespaceTokenText ==
218
65
              getNamespaceTokenText(AnnotatedLines[I + 1], AnnotatedLines) &&
219
65
          StartLineIndex - CompactedNamespacesCount - 1 ==
220
29
              AnnotatedLines[I + 1]->MatchingOpeningBlockLineIndex &&
221
65
          
!AnnotatedLines[I + 1]->First->Finalized28
) {
222
28
        if (hasEndComment(EndCommentPrevTok)) {
223
15
          // remove end comment, it will be merged in next one
224
15
          updateEndComment(EndCommentPrevTok, std::string(), SourceMgr, &Fixes);
225
15
        }
226
28
        CompactedNamespacesCount++;
227
28
        AllNamespaceNames = "::" + NamespaceName + AllNamespaceNames;
228
28
        continue;
229
28
      }
230
37
      NamespaceName += AllNamespaceNames;
231
37
      CompactedNamespacesCount = 0;
232
37
      AllNamespaceNames = std::string();
233
37
    }
234
556
    // The next token in the token stream after the place where the end comment
235
556
    // token must be. This is either the next token on the current line or the
236
556
    // first token on the next line.
237
556
    const FormatToken *EndCommentNextTok = EndCommentPrevTok->Next;
238
528
    if (EndCommentNextTok && 
EndCommentNextTok->is(tok::comment)228
)
239
227
      EndCommentNextTok = EndCommentNextTok->Next;
240
528
    if (!EndCommentNextTok && 
I + 1 < E527
)
241
527
      EndCommentNextTok = AnnotatedLines[I + 1]->First;
242
528
    bool AddNewline = EndCommentNextTok &&
243
528
                      EndCommentNextTok->NewlinesBefore == 0 &&
244
528
                      
EndCommentNextTok->isNot(tok::eof)382
;
245
528
    const std::string EndCommentText =
246
528
        computeEndCommentText(NamespaceName, AddNewline, NamespaceTok);
247
528
    if (!hasEndComment(EndCommentPrevTok)) {
248
301
      bool isShort = I - StartLineIndex <= kShortNamespaceMaxLines + 1;
249
301
      if (!isShort)
250
188
        addEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);
251
301
    } else 
if (227
!validEndComment(EndCommentPrevTok, NamespaceName,
252
227
                                NamespaceTok)) {
253
51
      updateEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);
254
51
    }
255
528
    StartLineIndex = SIZE_MAX;
256
528
  }
257
7.84k
  return {Fixes, 0};
258
7.84k
}
259
260
} // namespace format
261
} // namespace clang