Coverage Report

Created: 2020-11-24 06:42

/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
564
std::string computeName(const FormatToken *NamespaceTok) {
32
564
  assert(NamespaceTok &&
33
564
         NamespaceTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) &&
34
564
         "expecting a namespace token");
35
564
  std::string name = "";
36
564
  const FormatToken *Tok = NamespaceTok->getNextNonComment();
37
564
  if (NamespaceTok->is(TT_NamespaceMacro)) {
38
    // Collects all the non-comment tokens between opening parenthesis
39
    // 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
496
  } else {
47
    // For `namespace [[foo]] A::B::inline C {` or
48
    // `namespace MACRO1 MACRO2 A::B::inline C {`, returns "A::B::inline C".
49
    // Peek for the first '::' (or '{') and then return all tokens from one
50
    // token before that up until the '{'.
51
496
    const FormatToken *FirstNSTok = Tok;
52
988
    while (Tok && 
!Tok->is(tok::l_brace)965
&&
!Tok->is(tok::coloncolon)501
) {
53
492
      FirstNSTok = Tok;
54
492
      Tok = Tok->getNextNonComment();
55
492
    }
56
57
496
    Tok = FirstNSTok;
58
1.05k
    while (Tok && 
!Tok->is(tok::l_brace)1.02k
) {
59
556
      name += Tok->TokenText;
60
556
      if (Tok->is(tok::kw_inline))
61
2
        name += " ";
62
556
      Tok = Tok->getNextNonComment();
63
556
    }
64
496
  }
65
564
  return name;
66
564
}
67
68
std::string computeEndCommentText(StringRef NamespaceName, bool AddNewline,
69
536
                                  const FormatToken *NamespaceTok) {
70
536
  std::string text = "// ";
71
536
  text += NamespaceTok->TokenText;
72
536
  if (NamespaceTok->is(TT_NamespaceMacro))
73
61
    text += "(";
74
475
  else if (!NamespaceName.empty())
75
452
    text += ' ';
76
536
  text += NamespaceName;
77
536
  if (NamespaceTok->is(TT_NamespaceMacro))
78
61
    text += ")";
79
536
  if (AddNewline)
80
57
    text += '\n';
81
536
  return text;
82
536
}
83
84
862
bool hasEndComment(const FormatToken *RBraceTok) {
85
862
  return RBraceTok->Next && 
RBraceTok->Next->is(tok::comment)546
;
86
862
}
87
88
bool validEndComment(const FormatToken *RBraceTok, StringRef NamespaceName,
89
232
                     const FormatToken *NamespaceTok) {
90
232
  assert(hasEndComment(RBraceTok));
91
232
  const FormatToken *Comment = RBraceTok->Next;
92
93
  // Matches a valid namespace end comment.
94
  // Valid namespace end comments don't need to be edited.
95
232
  static const llvm::Regex NamespaceCommentPattern =
96
232
      llvm::Regex("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
97
232
                  "namespace( +([a-zA-Z0-9:_]+))?\\.? *(\\*/)?$",
98
232
                  llvm::Regex::IgnoreCase);
99
232
  static const llvm::Regex NamespaceMacroCommentPattern =
100
232
      llvm::Regex("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
101
232
                  "([a-zA-Z0-9_]+)\\(([a-zA-Z0-9:_]*)\\)\\.? *(\\*/)?$",
102
232
                  llvm::Regex::IgnoreCase);
103
104
232
  SmallVector<StringRef, 8> Groups;
105
232
  if (NamespaceTok->is(TT_NamespaceMacro) &&
106
53
      NamespaceMacroCommentPattern.match(Comment->TokenText, &Groups)) {
107
46
    StringRef NamespaceTokenText = Groups.size() > 4 ? Groups[4] : 
""0
;
108
    // The name of the macro must be used.
109
46
    if (NamespaceTokenText != NamespaceTok->TokenText)
110
4
      return false;
111
186
  } else if (NamespaceTok->isNot(tok::kw_namespace) ||
112
179
             !NamespaceCommentPattern.match(Comment->TokenText, &Groups)) {
113
    // Comment does not match regex.
114
16
    return false;
115
16
  }
116
212
  StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] : 
""0
;
117
  // Anonymous namespace comments must not mention a namespace name.
118
212
  if (NamespaceName.empty() && 
!NamespaceNameInComment.empty()13
)
119
4
    return false;
120
208
  StringRef AnonymousInComment = Groups.size() > 3 ? Groups[3] : 
""0
;
121
  // Named namespace comments must not mention anonymous namespace.
122
208
  if (!NamespaceName.empty() && 
!AnonymousInComment.empty()199
)
123
2
    return false;
124
206
  if (NamespaceNameInComment == NamespaceName)
125
180
    return true;
126
127
  // Has namespace comment flowed onto the next line.
128
  // } // namespace
129
  //   // verylongnamespacenamethatdidnotfitonthepreviouscommentline
130
26
  if (!(Comment->Next && 
Comment->Next->is(TT_LineComment)1
))
131
25
    return false;
132
133
1
  static const llvm::Regex CommentPattern = llvm::Regex(
134
1
      "^/[/*] *( +([a-zA-Z0-9:_]+))?\\.? *(\\*/)?$", llvm::Regex::IgnoreCase);
135
136
  // Pull out just the comment text.
137
1
  if (!CommentPattern.match(Comment->Next->TokenText, &Groups)) {
138
0
    return false;
139
0
  }
140
1
  NamespaceNameInComment = Groups.size() > 2 ? Groups[2] : 
""0
;
141
142
1
  return (NamespaceNameInComment == NamespaceName);
143
1
}
144
145
void addEndComment(const FormatToken *RBraceTok, StringRef EndCommentText,
146
                   const SourceManager &SourceMgr,
147
191
                   tooling::Replacements *Fixes) {
148
191
  auto EndLoc = RBraceTok->Tok.getEndLoc();
149
191
  auto Range = CharSourceRange::getCharRange(EndLoc, EndLoc);
150
191
  auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, EndCommentText));
151
191
  if (Err) {
152
0
    llvm::errs() << "Error while adding namespace end comment: "
153
0
                 << llvm::toString(std::move(Err)) << "\n";
154
0
  }
155
191
}
156
157
void updateEndComment(const FormatToken *RBraceTok, StringRef EndCommentText,
158
                      const SourceManager &SourceMgr,
159
66
                      tooling::Replacements *Fixes) {
160
66
  assert(hasEndComment(RBraceTok));
161
66
  const FormatToken *Comment = RBraceTok->Next;
162
66
  auto Range = CharSourceRange::getCharRange(Comment->getStartOfNonWhitespace(),
163
66
                                             Comment->Tok.getEndLoc());
164
66
  auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, EndCommentText));
165
66
  if (Err) {
166
0
    llvm::errs() << "Error while updating namespace end comment: "
167
0
                 << llvm::toString(std::move(Err)) << "\n";
168
0
  }
169
66
}
170
} // namespace
171
172
const FormatToken *
173
getNamespaceToken(const AnnotatedLine *Line,
174
41.3k
                  const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
175
41.3k
  if (!Line->Affected || 
Line->InPPDirective34.5k
||
!Line->startsWith(tok::r_brace)31.7k
)
176
33.0k
    return nullptr;
177
8.27k
  size_t StartLineIndex = Line->MatchingOpeningBlockLineIndex;
178
8.27k
  if (StartLineIndex == UnwrappedLine::kInvalidIndex)
179
60
    return nullptr;
180
8.21k
  assert(StartLineIndex < AnnotatedLines.size());
181
8.21k
  const FormatToken *NamespaceTok = AnnotatedLines[StartLineIndex]->First;
182
8.21k
  if (NamespaceTok->is(tok::l_brace)) {
183
    // "namespace" keyword can be on the line preceding '{', e.g. in styles
184
    // where BraceWrapping.AfterNamespace is true.
185
1.45k
    if (StartLineIndex > 0)
186
1.25k
      NamespaceTok = AnnotatedLines[StartLineIndex - 1]->First;
187
1.45k
  }
188
8.21k
  return NamespaceTok->getNamespaceToken();
189
8.21k
}
190
191
StringRef
192
getNamespaceTokenText(const AnnotatedLine *Line,
193
65
                      const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
194
65
  const FormatToken *NamespaceTok = getNamespaceToken(Line, AnnotatedLines);
195
33
  return NamespaceTok ? 
NamespaceTok->TokenText32
: StringRef();
196
65
}
197
198
NamespaceEndCommentsFixer::NamespaceEndCommentsFixer(const Environment &Env,
199
                                                     const FormatStyle &Style)
200
8.77k
    : TokenAnalyzer(Env, Style) {}
201
202
std::pair<tooling::Replacements, unsigned> NamespaceEndCommentsFixer::analyze(
203
    TokenAnnotator &Annotator, SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
204
8.86k
    FormatTokenLexer &Tokens) {
205
8.86k
  const SourceManager &SourceMgr = Env.getSourceManager();
206
8.86k
  AffectedRangeMgr.computeAffectedLines(AnnotatedLines);
207
8.86k
  tooling::Replacements Fixes;
208
209
  // Spin through the lines and ensure we have balanced braces.
210
8.86k
  int Braces = 0;
211
46.7k
  for (size_t I = 0, E = AnnotatedLines.size(); I != E; 
++I37.8k
) {
212
37.8k
    FormatToken *Tok = AnnotatedLines[I]->First;
213
197k
    while (Tok) {
214
151k
      Braces += Tok->is(tok::l_brace) ? 
18.41k
: Tok->is(tok::r_brace) ?
-18.39k
:
0143k
;
215
159k
      Tok = Tok->Next;
216
159k
    }
217
37.8k
  }
218
  // Don't attempt to comment unbalanced braces or this can
219
  // lead to comments being placed on the closing brace which isn't
220
  // the matching brace of the namespace. (occurs during incomplete editing).
221
8.86k
  if (Braces != 0) {
222
59
    return {Fixes, 0};
223
59
  }
224
225
8.80k
  std::string AllNamespaceNames = "";
226
8.80k
  size_t StartLineIndex = SIZE_MAX;
227
8.80k
  StringRef NamespaceTokenText;
228
8.80k
  unsigned int CompactedNamespacesCount = 0;
229
46.3k
  for (size_t I = 0, E = AnnotatedLines.size(); I != E; 
++I37.5k
) {
230
37.5k
    const AnnotatedLine *EndLine = AnnotatedLines[I];
231
37.5k
    const FormatToken *NamespaceTok =
232
37.5k
        getNamespaceToken(EndLine, AnnotatedLines);
233
37.5k
    if (!NamespaceTok)
234
36.9k
      continue;
235
565
    FormatToken *RBraceTok = EndLine->First;
236
565
    if (RBraceTok->Finalized)
237
1
      continue;
238
564
    RBraceTok->Finalized = true;
239
564
    const FormatToken *EndCommentPrevTok = RBraceTok;
240
    // Namespaces often end with '};'. In that case, attach namespace end
241
    // comments to the semicolon tokens.
242
564
    if (RBraceTok->Next && 
RBraceTok->Next->is(tok::semi)270
) {
243
39
      EndCommentPrevTok = RBraceTok->Next;
244
39
    }
245
564
    if (StartLineIndex == SIZE_MAX)
246
536
      StartLineIndex = EndLine->MatchingOpeningBlockLineIndex;
247
564
    std::string NamespaceName = computeName(NamespaceTok);
248
564
    if (Style.CompactNamespaces) {
249
65
      if (CompactedNamespacesCount == 0)
250
37
        NamespaceTokenText = NamespaceTok->TokenText;
251
65
      if ((I + 1 < E) &&
252
65
          NamespaceTokenText ==
253
65
              getNamespaceTokenText(AnnotatedLines[I + 1], AnnotatedLines) &&
254
29
          StartLineIndex - CompactedNamespacesCount - 1 ==
255
29
              AnnotatedLines[I + 1]->MatchingOpeningBlockLineIndex &&
256
28
          !AnnotatedLines[I + 1]->First->Finalized) {
257
28
        if (hasEndComment(EndCommentPrevTok)) {
258
          // remove end comment, it will be merged in next one
259
15
          updateEndComment(EndCommentPrevTok, std::string(), SourceMgr, &Fixes);
260
15
        }
261
28
        CompactedNamespacesCount++;
262
28
        AllNamespaceNames = "::" + NamespaceName + AllNamespaceNames;
263
28
        continue;
264
28
      }
265
37
      NamespaceName += AllNamespaceNames;
266
37
      CompactedNamespacesCount = 0;
267
37
      AllNamespaceNames = std::string();
268
37
    }
269
    // The next token in the token stream after the place where the end comment
270
    // token must be. This is either the next token on the current line or the
271
    // first token on the next line.
272
536
    const FormatToken *EndCommentNextTok = EndCommentPrevTok->Next;
273
536
    if (EndCommentNextTok && 
EndCommentNextTok->is(tok::comment)233
)
274
232
      EndCommentNextTok = EndCommentNextTok->Next;
275
536
    if (!EndCommentNextTok && 
I + 1 < E534
)
276
534
      EndCommentNextTok = AnnotatedLines[I + 1]->First;
277
536
    bool AddNewline = EndCommentNextTok &&
278
536
                      EndCommentNextTok->NewlinesBefore == 0 &&
279
388
                      EndCommentNextTok->isNot(tok::eof);
280
536
    const std::string EndCommentText =
281
536
        computeEndCommentText(NamespaceName, AddNewline, NamespaceTok);
282
536
    if (!hasEndComment(EndCommentPrevTok)) {
283
304
      bool isShort = I - StartLineIndex <= kShortNamespaceMaxLines + 1;
284
304
      if (!isShort)
285
191
        addEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);
286
232
    } else if (!validEndComment(EndCommentPrevTok, NamespaceName,
287
51
                                NamespaceTok)) {
288
51
      updateEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);
289
51
    }
290
536
    StartLineIndex = SIZE_MAX;
291
536
  }
292
8.80k
  return {Fixes, 0};
293
8.80k
}
294
295
} // namespace format
296
} // namespace clang