Coverage Report

Created: 2021-09-21 08:58

/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
614
std::string computeName(const FormatToken *NamespaceTok) {
28
614
  assert(NamespaceTok &&
29
614
         NamespaceTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) &&
30
614
         "expecting a namespace token");
31
0
  std::string name = "";
32
614
  const FormatToken *Tok = NamespaceTok->getNextNonComment();
33
614
  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
546
  } 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
546
    const FormatToken *FirstNSTok = Tok;
48
1.08k
    while (Tok && 
!Tok->is(tok::l_brace)1.04k
&&
!Tok->is(tok::coloncolon)551
) {
49
542
      FirstNSTok = Tok;
50
542
      Tok = Tok->getNextNonComment();
51
542
    }
52
53
546
    Tok = FirstNSTok;
54
1.15k
    while (Tok && 
!Tok->is(tok::l_brace)1.10k
) {
55
606
      name += Tok->TokenText;
56
606
      if (Tok->is(tok::kw_inline))
57
2
        name += " ";
58
606
      Tok = Tok->getNextNonComment();
59
606
    }
60
546
  }
61
0
  return name;
62
614
}
63
64
std::string computeEndCommentText(StringRef NamespaceName, bool AddNewline,
65
                                  const FormatToken *NamespaceTok,
66
586
                                  unsigned SpacesToAdd) {
67
586
  std::string text = "//";
68
586
  text.append(SpacesToAdd, ' ');
69
586
  text += NamespaceTok->TokenText;
70
586
  if (NamespaceTok->is(TT_NamespaceMacro))
71
61
    text += "(";
72
525
  else if (!NamespaceName.empty())
73
502
    text += ' ';
74
586
  text += NamespaceName;
75
586
  if (NamespaceTok->is(TT_NamespaceMacro))
76
61
    text += ")";
77
586
  if (AddNewline)
78
57
    text += '\n';
79
586
  return text;
80
586
}
81
82
932
bool hasEndComment(const FormatToken *RBraceTok) {
83
932
  return RBraceTok->Next && 
RBraceTok->Next->is(tok::comment)586
;
84
932
}
85
86
bool validEndComment(const FormatToken *RBraceTok, StringRef NamespaceName,
87
252
                     const FormatToken *NamespaceTok) {
88
252
  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
252
  static const llvm::Regex NamespaceCommentPattern =
94
252
      llvm::Regex("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
95
252
                  "namespace( +([a-zA-Z0-9:_]+))?\\.? *(\\*/)?$",
96
252
                  llvm::Regex::IgnoreCase);
97
252
  static const llvm::Regex NamespaceMacroCommentPattern =
98
252
      llvm::Regex("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
99
252
                  "([a-zA-Z0-9_]+)\\(([a-zA-Z0-9:_]*)\\)\\.? *(\\*/)?$",
100
252
                  llvm::Regex::IgnoreCase);
101
102
252
  SmallVector<StringRef, 8> Groups;
103
252
  if (NamespaceTok->is(TT_NamespaceMacro) &&
104
252
      
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
206
  } else if (NamespaceTok->isNot(tok::kw_namespace) ||
110
206
             
!NamespaceCommentPattern.match(Comment->TokenText, &Groups)199
) {
111
    // Comment does not match regex.
112
16
    return false;
113
16
  }
114
232
  StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] : 
""0
;
115
  // Anonymous namespace comments must not mention a namespace name.
116
232
  if (NamespaceName.empty() && 
!NamespaceNameInComment.empty()13
)
117
4
    return false;
118
228
  StringRef AnonymousInComment = Groups.size() > 3 ? Groups[3] : 
""0
;
119
  // Named namespace comments must not mention anonymous namespace.
120
228
  if (!NamespaceName.empty() && 
!AnonymousInComment.empty()219
)
121
2
    return false;
122
226
  if (NamespaceNameInComment == NamespaceName)
123
200
    return true;
124
125
  // Has namespace comment flowed onto the next line.
126
  // } // namespace
127
  //   // verylongnamespacenamethatdidnotfitonthepreviouscommentline
128
26
  if (!(Comment->Next && 
Comment->Next->is(TT_LineComment)1
))
129
25
    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
206
                   tooling::Replacements *Fixes) {
146
206
  auto EndLoc = RBraceTok->Tok.getEndLoc();
147
206
  auto Range = CharSourceRange::getCharRange(EndLoc, EndLoc);
148
206
  auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, EndCommentText));
149
206
  if (Err) {
150
0
    llvm::errs() << "Error while adding namespace end comment: "
151
0
                 << llvm::toString(std::move(Err)) << "\n";
152
0
  }
153
206
}
154
155
void updateEndComment(const FormatToken *RBraceTok, StringRef EndCommentText,
156
                      const SourceManager &SourceMgr,
157
66
                      tooling::Replacements *Fixes) {
158
66
  assert(hasEndComment(RBraceTok));
159
0
  const FormatToken *Comment = RBraceTok->Next;
160
66
  auto Range = CharSourceRange::getCharRange(Comment->getStartOfNonWhitespace(),
161
66
                                             Comment->Tok.getEndLoc());
162
66
  auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, EndCommentText));
163
66
  if (Err) {
164
0
    llvm::errs() << "Error while updating namespace end comment: "
165
0
                 << llvm::toString(std::move(Err)) << "\n";
166
0
  }
167
66
}
168
} // namespace
169
170
const FormatToken *
171
getNamespaceToken(const AnnotatedLine *Line,
172
48.7k
                  const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
173
48.7k
  if (!Line->Affected || 
Line->InPPDirective41.9k
||
!Line->startsWith(tok::r_brace)38.8k
)
174
38.4k
    return nullptr;
175
10.2k
  size_t StartLineIndex = Line->MatchingOpeningBlockLineIndex;
176
10.2k
  if (StartLineIndex == UnwrappedLine::kInvalidIndex)
177
122
    return nullptr;
178
10.1k
  assert(StartLineIndex < AnnotatedLines.size());
179
0
  const FormatToken *NamespaceTok = AnnotatedLines[StartLineIndex]->First;
180
10.1k
  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.83k
    if (StartLineIndex > 0)
184
1.61k
      NamespaceTok = AnnotatedLines[StartLineIndex - 1]->First;
185
1.83k
  }
186
10.1k
  return NamespaceTok->getNamespaceToken();
187
10.2k
}
188
189
StringRef
190
getNamespaceTokenText(const AnnotatedLine *Line,
191
65
                      const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
192
65
  const FormatToken *NamespaceTok = getNamespaceToken(Line, AnnotatedLines);
193
65
  return NamespaceTok ? 
NamespaceTok->TokenText32
:
StringRef()33
;
194
65
}
195
196
NamespaceEndCommentsFixer::NamespaceEndCommentsFixer(const Environment &Env,
197
                                                     const FormatStyle &Style)
198
9.99k
    : TokenAnalyzer(Env, Style) {}
199
200
std::pair<tooling::Replacements, unsigned> NamespaceEndCommentsFixer::analyze(
201
    TokenAnnotator &Annotator, SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
202
10.0k
    FormatTokenLexer &Tokens) {
203
10.0k
  const SourceManager &SourceMgr = Env.getSourceManager();
204
10.0k
  AffectedRangeMgr.computeAffectedLines(AnnotatedLines);
205
10.0k
  tooling::Replacements Fixes;
206
207
  // Spin through the lines and ensure we have balanced braces.
208
10.0k
  int Braces = 0;
209
54.3k
  for (size_t I = 0, E = AnnotatedLines.size(); I != E; 
++I44.2k
) {
210
44.2k
    FormatToken *Tok = AnnotatedLines[I]->First;
211
230k
    while (Tok) {
212
185k
      Braces += Tok->is(tok::l_brace) ? 
19.80k
:
Tok->is(tok::r_brace)176k
?
-19.78k
:
0166k
;
213
185k
      Tok = Tok->Next;
214
185k
    }
215
44.2k
  }
216
  // Don't attempt to comment unbalanced braces or this can
217
  // lead to comments being placed on the closing brace which isn't
218
  // the matching brace of the namespace. (occurs during incomplete editing).
219
10.0k
  if (Braces != 0) {
220
61
    return {Fixes, 0};
221
61
  }
222
223
10.0k
  std::string AllNamespaceNames = "";
224
10.0k
  size_t StartLineIndex = SIZE_MAX;
225
10.0k
  StringRef NamespaceTokenText;
226
10.0k
  unsigned int CompactedNamespacesCount = 0;
227
53.9k
  for (size_t I = 0, E = AnnotatedLines.size(); I != E; 
++I43.9k
) {
228
43.9k
    const AnnotatedLine *EndLine = AnnotatedLines[I];
229
43.9k
    const FormatToken *NamespaceTok =
230
43.9k
        getNamespaceToken(EndLine, AnnotatedLines);
231
43.9k
    if (!NamespaceTok)
232
43.3k
      continue;
233
615
    FormatToken *RBraceTok = EndLine->First;
234
615
    if (RBraceTok->Finalized)
235
1
      continue;
236
614
    RBraceTok->Finalized = true;
237
614
    const FormatToken *EndCommentPrevTok = RBraceTok;
238
    // Namespaces often end with '};'. In that case, attach namespace end
239
    // comments to the semicolon tokens.
240
614
    if (RBraceTok->Next && 
RBraceTok->Next->is(tok::semi)290
) {
241
39
      EndCommentPrevTok = RBraceTok->Next;
242
39
    }
243
614
    if (StartLineIndex == SIZE_MAX)
244
586
      StartLineIndex = EndLine->MatchingOpeningBlockLineIndex;
245
614
    std::string NamespaceName = computeName(NamespaceTok);
246
614
    if (Style.CompactNamespaces) {
247
65
      if (CompactedNamespacesCount == 0)
248
37
        NamespaceTokenText = NamespaceTok->TokenText;
249
65
      if ((I + 1 < E) &&
250
65
          NamespaceTokenText ==
251
65
              getNamespaceTokenText(AnnotatedLines[I + 1], AnnotatedLines) &&
252
65
          StartLineIndex - CompactedNamespacesCount - 1 ==
253
29
              AnnotatedLines[I + 1]->MatchingOpeningBlockLineIndex &&
254
65
          
!AnnotatedLines[I + 1]->First->Finalized28
) {
255
28
        if (hasEndComment(EndCommentPrevTok)) {
256
          // remove end comment, it will be merged in next one
257
15
          updateEndComment(EndCommentPrevTok, std::string(), SourceMgr, &Fixes);
258
15
        }
259
28
        CompactedNamespacesCount++;
260
28
        AllNamespaceNames = "::" + NamespaceName + AllNamespaceNames;
261
28
        continue;
262
28
      }
263
37
      NamespaceName += AllNamespaceNames;
264
37
      CompactedNamespacesCount = 0;
265
37
      AllNamespaceNames = std::string();
266
37
    }
267
    // The next token in the token stream after the place where the end comment
268
    // token must be. This is either the next token on the current line or the
269
    // first token on the next line.
270
586
    const FormatToken *EndCommentNextTok = EndCommentPrevTok->Next;
271
586
    if (EndCommentNextTok && 
EndCommentNextTok->is(tok::comment)253
)
272
252
      EndCommentNextTok = EndCommentNextTok->Next;
273
586
    if (!EndCommentNextTok && 
I + 1 < E584
)
274
584
      EndCommentNextTok = AnnotatedLines[I + 1]->First;
275
586
    bool AddNewline = EndCommentNextTok &&
276
586
                      EndCommentNextTok->NewlinesBefore == 0 &&
277
586
                      
EndCommentNextTok->isNot(tok::eof)411
;
278
586
    const std::string EndCommentText =
279
586
        computeEndCommentText(NamespaceName, AddNewline, NamespaceTok,
280
586
                              Style.SpacesInLineCommentPrefix.Minimum);
281
586
    if (!hasEndComment(EndCommentPrevTok)) {
282
334
      bool isShort = I - StartLineIndex <= Style.ShortNamespaceLines + 1;
283
334
      if (!isShort)
284
206
        addEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);
285
334
    } else 
if (252
!validEndComment(EndCommentPrevTok, NamespaceName,
286
252
                                NamespaceTok)) {
287
51
      updateEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);
288
51
    }
289
586
    StartLineIndex = SIZE_MAX;
290
586
  }
291
10.0k
  return {Fixes, 0};
292
10.0k
}
293
294
} // namespace format
295
} // namespace clang