Coverage Report

Created: 2022-01-25 06:29

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/Comment.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- Comment.cpp - Comment AST node implementation --------------------===//
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
#include "clang/AST/Comment.h"
10
#include "clang/AST/ASTContext.h"
11
#include "clang/AST/Decl.h"
12
#include "clang/AST/DeclObjC.h"
13
#include "clang/AST/DeclTemplate.h"
14
#include "clang/Basic/CharInfo.h"
15
#include "llvm/Support/ErrorHandling.h"
16
#include <type_traits>
17
18
namespace clang {
19
namespace comments {
20
21
// Check that no comment class has a non-trival destructor. They are allocated
22
// with a BumpPtrAllocator and therefore their destructor is not executed.
23
#define ABSTRACT_COMMENT(COMMENT)
24
#define COMMENT(CLASS, PARENT)                                                 \
25
  static_assert(std::is_trivially_destructible<CLASS>::value,                  \
26
                #CLASS " should be trivially destructible!");
27
#include "clang/AST/CommentNodes.inc"
28
#undef COMMENT
29
#undef ABSTRACT_COMMENT
30
31
// DeclInfo is also allocated with a BumpPtrAllocator.
32
static_assert(std::is_trivially_destructible<DeclInfo>::value,
33
              "DeclInfo should be trivially destructible!");
34
35
692
const char *Comment::getCommentKindName() const {
36
692
  switch (getCommentKind()) {
37
0
  case NoCommentKind: return "NoCommentKind";
38
0
#define ABSTRACT_COMMENT(COMMENT)
39
0
#define COMMENT(CLASS, PARENT) \
40
692
  case CLASS##Kind: \
41
692
    return #CLASS;
42
692
#include 
"clang/AST/CommentNodes.inc"0
43
692
#undef COMMENT
44
692
#undef ABSTRACT_COMMENT
45
692
  }
46
0
  llvm_unreachable("Unknown comment kind!");
47
0
}
48
49
namespace {
50
struct good {};
51
struct bad {};
52
53
template <typename T>
54
0
good implements_child_begin_end(Comment::child_iterator (T::*)() const) {
55
0
  return good();
56
0
}
Unexecuted instantiation: Comment.cpp:clang::comments::(anonymous namespace)::good clang::comments::(anonymous namespace)::implements_child_begin_end<clang::comments::BlockCommandComment>(clang::comments::Comment* const* (clang::comments::BlockCommandComment::*)() const)
Unexecuted instantiation: Comment.cpp:clang::comments::(anonymous namespace)::good clang::comments::(anonymous namespace)::implements_child_begin_end<clang::comments::VerbatimBlockComment>(clang::comments::Comment* const* (clang::comments::VerbatimBlockComment::*)() const)
Unexecuted instantiation: Comment.cpp:clang::comments::(anonymous namespace)::good clang::comments::(anonymous namespace)::implements_child_begin_end<clang::comments::VerbatimLineComment>(clang::comments::Comment* const* (clang::comments::VerbatimLineComment::*)() const)
Unexecuted instantiation: Comment.cpp:clang::comments::(anonymous namespace)::good clang::comments::(anonymous namespace)::implements_child_begin_end<clang::comments::ParagraphComment>(clang::comments::Comment* const* (clang::comments::ParagraphComment::*)() const)
Unexecuted instantiation: Comment.cpp:clang::comments::(anonymous namespace)::good clang::comments::(anonymous namespace)::implements_child_begin_end<clang::comments::FullComment>(clang::comments::Comment* const* (clang::comments::FullComment::*)() const)
Unexecuted instantiation: Comment.cpp:clang::comments::(anonymous namespace)::good clang::comments::(anonymous namespace)::implements_child_begin_end<clang::comments::HTMLEndTagComment>(clang::comments::Comment* const* (clang::comments::HTMLEndTagComment::*)() const)
Unexecuted instantiation: Comment.cpp:clang::comments::(anonymous namespace)::good clang::comments::(anonymous namespace)::implements_child_begin_end<clang::comments::HTMLStartTagComment>(clang::comments::Comment* const* (clang::comments::HTMLStartTagComment::*)() const)
Unexecuted instantiation: Comment.cpp:clang::comments::(anonymous namespace)::good clang::comments::(anonymous namespace)::implements_child_begin_end<clang::comments::InlineCommandComment>(clang::comments::Comment* const* (clang::comments::InlineCommandComment::*)() const)
Unexecuted instantiation: Comment.cpp:clang::comments::(anonymous namespace)::good clang::comments::(anonymous namespace)::implements_child_begin_end<clang::comments::TextComment>(clang::comments::Comment* const* (clang::comments::TextComment::*)() const)
Unexecuted instantiation: Comment.cpp:clang::comments::(anonymous namespace)::good clang::comments::(anonymous namespace)::implements_child_begin_end<clang::comments::VerbatimBlockLineComment>(clang::comments::Comment* const* (clang::comments::VerbatimBlockLineComment::*)() const)
57
58
LLVM_ATTRIBUTE_UNUSED
59
static inline bad implements_child_begin_end(
60
0
                      Comment::child_iterator (Comment::*)() const) {
61
0
  return bad();
62
0
}
63
64
#define ASSERT_IMPLEMENTS_child_begin(function) \
65
  (void) good(implements_child_begin_end(function))
66
67
LLVM_ATTRIBUTE_UNUSED
68
0
static inline void CheckCommentASTNodes() {
69
0
#define ABSTRACT_COMMENT(COMMENT)
70
0
#define COMMENT(CLASS, PARENT) \
71
0
  ASSERT_IMPLEMENTS_child_begin(&CLASS::child_begin); \
72
0
  ASSERT_IMPLEMENTS_child_begin(&CLASS::child_end);
73
0
#include "clang/AST/CommentNodes.inc"
74
0
#undef COMMENT
75
0
#undef ABSTRACT_COMMENT
76
0
}
77
78
#undef ASSERT_IMPLEMENTS_child_begin
79
80
} // end unnamed namespace
81
82
18.7k
Comment::child_iterator Comment::child_begin() const {
83
18.7k
  switch (getCommentKind()) {
84
0
  case NoCommentKind: llvm_unreachable("comment without a kind");
85
0
#define ABSTRACT_COMMENT(COMMENT)
86
0
#define COMMENT(CLASS, PARENT) \
87
18.7k
  case CLASS##Kind: \
88
18.7k
    return static_cast<const CLASS *>(this)->child_begin();
89
18.7k
#include 
"clang/AST/CommentNodes.inc"0
90
18.7k
#undef COMMENT
91
18.7k
#undef ABSTRACT_COMMENT
92
18.7k
  }
93
0
  llvm_unreachable("Unknown comment kind!");
94
0
}
95
96
13.0k
Comment::child_iterator Comment::child_end() const {
97
13.0k
  switch (getCommentKind()) {
98
0
  case NoCommentKind: llvm_unreachable("comment without a kind");
99
0
#define ABSTRACT_COMMENT(COMMENT)
100
0
#define COMMENT(CLASS, PARENT) \
101
13.0k
  case CLASS##Kind: \
102
13.0k
    return static_cast<const CLASS *>(this)->child_end();
103
13.0k
#include 
"clang/AST/CommentNodes.inc"0
104
13.0k
#undef COMMENT
105
13.0k
#undef ABSTRACT_COMMENT
106
13.0k
  }
107
0
  llvm_unreachable("Unknown comment kind!");
108
0
}
109
110
2.86k
bool TextComment::isWhitespaceNoCache() const {
111
2.86k
  return llvm::all_of(Text, clang::isWhitespace);
112
2.86k
}
113
114
2.42k
bool ParagraphComment::isWhitespaceNoCache() const {
115
3.04k
  for (child_iterator I = child_begin(), E = child_end(); I != E; 
++I620
) {
116
2.50k
    if (const TextComment *TC = dyn_cast<TextComment>(*I)) {
117
2.42k
      if (!TC->isWhitespace())
118
1.80k
        return false;
119
2.42k
    } else
120
75
      return false;
121
2.50k
  }
122
540
  return true;
123
2.42k
}
124
125
752
static TypeLoc lookThroughTypedefOrTypeAliasLocs(TypeLoc &SrcTL) {
126
752
  TypeLoc TL = SrcTL.IgnoreParens();
127
128
  // Look through attribute types.
129
752
  if (AttributedTypeLoc AttributeTL = TL.getAs<AttributedTypeLoc>())
130
3
    return AttributeTL.getModifiedLoc();
131
  // Look through qualified types.
132
749
  if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>())
133
0
    return QualifiedTL.getUnqualifiedLoc();
134
  // Look through pointer types.
135
749
  if (PointerTypeLoc PointerTL = TL.getAs<PointerTypeLoc>())
136
96
    return PointerTL.getPointeeLoc().getUnqualifiedLoc();
137
  // Look through reference types.
138
653
  if (ReferenceTypeLoc ReferenceTL = TL.getAs<ReferenceTypeLoc>())
139
18
    return ReferenceTL.getPointeeLoc().getUnqualifiedLoc();
140
  // Look through adjusted types.
141
635
  if (AdjustedTypeLoc ATL = TL.getAs<AdjustedTypeLoc>())
142
0
    return ATL.getOriginalLoc();
143
635
  if (BlockPointerTypeLoc BlockPointerTL = TL.getAs<BlockPointerTypeLoc>())
144
19
    return BlockPointerTL.getPointeeLoc().getUnqualifiedLoc();
145
616
  if (MemberPointerTypeLoc MemberPointerTL = TL.getAs<MemberPointerTypeLoc>())
146
6
    return MemberPointerTL.getPointeeLoc().getUnqualifiedLoc();
147
610
  if (ElaboratedTypeLoc ETL = TL.getAs<ElaboratedTypeLoc>())
148
77
    return ETL.getNamedTypeLoc();
149
150
533
  return TL;
151
610
}
152
153
461
static bool getFunctionTypeLoc(TypeLoc TL, FunctionTypeLoc &ResFTL) {
154
461
  TypeLoc PrevTL;
155
1.21k
  while (PrevTL != TL) {
156
752
    PrevTL = TL;
157
752
    TL = lookThroughTypedefOrTypeAliasLocs(TL);
158
752
  }
159
160
461
  if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
161
82
    ResFTL = FTL;
162
82
    return true;
163
82
  }
164
165
379
  if (TemplateSpecializationTypeLoc STL =
166
379
          TL.getAs<TemplateSpecializationTypeLoc>()) {
167
    // If we have a typedef to a template specialization with exactly one
168
    // template argument of a function type, this looks like std::function,
169
    // boost::function, or other function wrapper.  Treat these typedefs as
170
    // functions.
171
58
    if (STL.getNumArgs() != 1)
172
16
      return false;
173
42
    TemplateArgumentLoc MaybeFunction = STL.getArgLoc(0);
174
42
    if (MaybeFunction.getArgument().getKind() != TemplateArgument::Type)
175
6
      return false;
176
36
    TypeSourceInfo *MaybeFunctionTSI = MaybeFunction.getTypeSourceInfo();
177
36
    TypeLoc TL = MaybeFunctionTSI->getTypeLoc().getUnqualifiedLoc();
178
36
    if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
179
32
      ResFTL = FTL;
180
32
      return true;
181
32
    }
182
36
  }
183
184
325
  return false;
185
379
}
186
187
39
const char *ParamCommandComment::getDirectionAsString(PassDirection D) {
188
39
  switch (D) {
189
21
  case ParamCommandComment::In:
190
21
    return "[in]";
191
5
  case ParamCommandComment::Out:
192
5
    return "[out]";
193
13
  case ParamCommandComment::InOut:
194
13
    return "[in,out]";
195
39
  }
196
0
  llvm_unreachable("unknown PassDirection");
197
0
}
198
199
1.73k
void DeclInfo::fill() {
200
1.73k
  assert(!IsFilled);
201
202
  // Set defaults.
203
0
  Kind = OtherKind;
204
1.73k
  TemplateKind = NotTemplate;
205
1.73k
  IsObjCMethod = false;
206
1.73k
  IsInstanceMethod = false;
207
1.73k
  IsClassMethod = false;
208
1.73k
  IsVariadic = false;
209
1.73k
  ParamVars = None;
210
1.73k
  TemplateParameters = nullptr;
211
212
1.73k
  if (!CommentDecl) {
213
    // If there is no declaration, the defaults is our only guess.
214
0
    IsFilled = true;
215
0
    return;
216
0
  }
217
1.73k
  CurrentDecl = CommentDecl;
218
219
1.73k
  Decl::Kind K = CommentDecl->getKind();
220
1.73k
  const TypeSourceInfo *TSI = nullptr;
221
1.73k
  switch (K) {
222
91
  default:
223
    // Defaults are should be good for declarations we don't handle explicitly.
224
91
    break;
225
618
  case Decl::Function:
226
701
  case Decl::CXXMethod:
227
714
  case Decl::CXXConstructor:
228
723
  case Decl::CXXDestructor:
229
725
  case Decl::CXXConversion: {
230
725
    const FunctionDecl *FD = cast<FunctionDecl>(CommentDecl);
231
725
    Kind = FunctionKind;
232
725
    ParamVars = FD->parameters();
233
725
    ReturnType = FD->getReturnType();
234
725
    unsigned NumLists = FD->getNumTemplateParameterLists();
235
725
    if (NumLists != 0) {
236
24
      TemplateKind = TemplateSpecialization;
237
24
      TemplateParameters =
238
24
          FD->getTemplateParameterList(NumLists - 1);
239
24
    }
240
241
725
    if (K == Decl::CXXMethod || 
K == Decl::CXXConstructor642
||
242
725
        
K == Decl::CXXDestructor629
||
K == Decl::CXXConversion620
) {
243
107
      const CXXMethodDecl *MD = cast<CXXMethodDecl>(CommentDecl);
244
107
      IsInstanceMethod = MD->isInstance();
245
107
      IsClassMethod = !IsInstanceMethod;
246
107
    }
247
725
    IsVariadic = FD->isVariadic();
248
725
    assert(involvesFunctionType());
249
0
    break;
250
723
  }
251
89
  case Decl::ObjCMethod: {
252
89
    const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(CommentDecl);
253
89
    Kind = FunctionKind;
254
89
    ParamVars = MD->parameters();
255
89
    ReturnType = MD->getReturnType();
256
89
    IsObjCMethod = true;
257
89
    IsInstanceMethod = MD->isInstanceMethod();
258
89
    IsClassMethod = !IsInstanceMethod;
259
89
    IsVariadic = MD->isVariadic();
260
89
    assert(involvesFunctionType());
261
0
    break;
262
723
  }
263
121
  case Decl::FunctionTemplate: {
264
121
    const FunctionTemplateDecl *FTD = cast<FunctionTemplateDecl>(CommentDecl);
265
121
    Kind = FunctionKind;
266
121
    TemplateKind = Template;
267
121
    const FunctionDecl *FD = FTD->getTemplatedDecl();
268
121
    ParamVars = FD->parameters();
269
121
    ReturnType = FD->getReturnType();
270
121
    TemplateParameters = FTD->getTemplateParameters();
271
121
    IsVariadic = FD->isVariadic();
272
121
    assert(involvesFunctionType());
273
0
    break;
274
723
  }
275
45
  case Decl::ClassTemplate: {
276
45
    const ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(CommentDecl);
277
45
    Kind = ClassKind;
278
45
    TemplateKind = Template;
279
45
    TemplateParameters = CTD->getTemplateParameters();
280
45
    break;
281
723
  }
282
14
  case Decl::ClassTemplatePartialSpecialization: {
283
14
    const ClassTemplatePartialSpecializationDecl *CTPSD =
284
14
        cast<ClassTemplatePartialSpecializationDecl>(CommentDecl);
285
14
    Kind = ClassKind;
286
14
    TemplateKind = TemplatePartialSpecialization;
287
14
    TemplateParameters = CTPSD->getTemplateParameters();
288
14
    break;
289
723
  }
290
11
  case Decl::ClassTemplateSpecialization:
291
11
    Kind = ClassKind;
292
11
    TemplateKind = TemplateSpecialization;
293
11
    break;
294
15
  case Decl::Record:
295
104
  case Decl::CXXRecord:
296
104
    Kind = ClassKind;
297
104
    break;
298
195
  case Decl::Var:
299
195
    if (const VarTemplateDecl *VTD =
300
195
            cast<VarDecl>(CommentDecl)->getDescribedVarTemplate()) {
301
1
      TemplateKind = TemplateSpecialization;
302
1
      TemplateParameters = VTD->getTemplateParameters();
303
1
    }
304
195
    LLVM_FALLTHROUGH;
305
248
  case Decl::Field:
306
277
  case Decl::EnumConstant:
307
283
  case Decl::ObjCIvar:
308
283
  case Decl::ObjCAtDefsField:
309
316
  case Decl::ObjCProperty:
310
316
    if (const auto *VD = dyn_cast<DeclaratorDecl>(CommentDecl))
311
254
      TSI = VD->getTypeSourceInfo();
312
62
    else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(CommentDecl))
313
33
      TSI = PD->getTypeSourceInfo();
314
316
    Kind = VariableKind;
315
316
    break;
316
6
  case Decl::VarTemplate: {
317
6
    const VarTemplateDecl *VTD = cast<VarTemplateDecl>(CommentDecl);
318
6
    Kind = VariableKind;
319
6
    TemplateKind = Template;
320
6
    TemplateParameters = VTD->getTemplateParameters();
321
6
    if (const VarDecl *VD = VTD->getTemplatedDecl())
322
6
      TSI = VD->getTypeSourceInfo();
323
6
    break;
324
283
  }
325
15
  case Decl::Namespace:
326
15
    Kind = NamespaceKind;
327
15
    break;
328
55
  case Decl::TypeAlias:
329
145
  case Decl::Typedef:
330
145
    Kind = TypedefKind;
331
145
    TSI = cast<TypedefNameDecl>(CommentDecl)->getTypeSourceInfo();
332
145
    break;
333
23
  case Decl::TypeAliasTemplate: {
334
23
    const TypeAliasTemplateDecl *TAT = cast<TypeAliasTemplateDecl>(CommentDecl);
335
23
    Kind = TypedefKind;
336
23
    TemplateKind = Template;
337
23
    TemplateParameters = TAT->getTemplateParameters();
338
23
    if (TypeAliasDecl *TAD = TAT->getTemplatedDecl())
339
23
      TSI = TAD->getTypeSourceInfo();
340
23
    break;
341
55
  }
342
25
  case Decl::Enum:
343
25
    Kind = EnumKind;
344
25
    break;
345
1.73k
  }
346
347
  // If the type is a typedef / using to something we consider a function,
348
  // extract arguments and return type.
349
1.73k
  if (TSI) {
350
461
    TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
351
461
    FunctionTypeLoc FTL;
352
461
    if (getFunctionTypeLoc(TL, FTL)) {
353
114
      ParamVars = FTL.getParams();
354
114
      ReturnType = FTL.getReturnLoc().getType();
355
114
      if (const auto *FPT = dyn_cast<FunctionProtoType>(FTL.getTypePtr()))
356
112
        IsVariadic = FPT->isVariadic();
357
114
      assert(involvesFunctionType());
358
114
    }
359
461
  }
360
361
0
  IsFilled = true;
362
1.73k
}
363
364
263
StringRef ParamCommandComment::getParamName(const FullComment *FC) const {
365
263
  assert(isParamIndexValid());
366
263
  if (isVarArgParam())
367
15
    return "...";
368
248
  return FC->getDeclInfo()->ParamVars[getParamIndex()]->getName();
369
263
}
370
371
165
StringRef TParamCommandComment::getParamName(const FullComment *FC) const {
372
165
  assert(isPositionValid());
373
0
  const TemplateParameterList *TPL = FC->getDeclInfo()->TemplateParameters;
374
229
  for (unsigned i = 0, e = getDepth(); i != e; 
++i64
) {
375
229
    assert(TPL && "Unknown TemplateParameterList");
376
229
    if (i == e - 1)
377
165
      return TPL->getParam(getIndex(i))->getName();
378
64
    const NamedDecl *Param = TPL->getParam(getIndex(i));
379
64
    if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param))
380
64
      TPL = TTP->getTemplateParameters();
381
64
  }
382
0
  return "";
383
165
}
384
385
} // end namespace comments
386
} // end namespace clang
387