Coverage Report

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