Coverage Report

Created: 2020-09-22 08:39

/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
660
const char *Comment::getCommentKindName() const {
36
660
  switch (getCommentKind()) {
37
0
  case NoCommentKind: return "NoCommentKind";
38
0
#define ABSTRACT_COMMENT(COMMENT)
39
0
#define COMMENT(CLASS, PARENT) \
40
660
  case CLASS##Kind: \
41
660
    return #CLASS;
42
0
#include "clang/AST/CommentNodes.inc"
43
660
#undef COMMENT
44
660
#undef ABSTRACT_COMMENT
45
660
  }
46
660
  
llvm_unreachable0
("Unknown comment kind!");
47
660
}
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.4k
Comment::child_iterator Comment::child_begin() const {
83
18.4k
  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.4k
  case CLASS##Kind: \
88
18.4k
    return static_cast<const CLASS *>(this)->child_begin();
89
0
#include "clang/AST/CommentNodes.inc"
90
18.4k
#undef COMMENT
91
18.4k
#undef ABSTRACT_COMMENT
92
18.4k
  }
93
18.4k
  
llvm_unreachable0
("Unknown comment kind!");
94
18.4k
}
95
96
12.8k
Comment::child_iterator Comment::child_end() const {
97
12.8k
  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
12.8k
  case CLASS##Kind: \
102
12.8k
    return static_cast<const CLASS *>(this)->child_end();
103
0
#include "clang/AST/CommentNodes.inc"
104
12.8k
#undef COMMENT
105
12.8k
#undef ABSTRACT_COMMENT
106
12.8k
  }
107
12.8k
  
llvm_unreachable0
("Unknown comment kind!");
108
12.8k
}
109
110
2.81k
bool TextComment::isWhitespaceNoCache() const {
111
2.81k
  for (StringRef::const_iterator I = Text.begin(), E = Text.end();
112
5.65k
       I != E; 
++I2.83k
) {
113
4.78k
    if (!clang::isWhitespace(*I))
114
1.94k
      return false;
115
4.78k
  }
116
868
  return true;
117
2.81k
}
118
119
2.37k
bool ParagraphComment::isWhitespaceNoCache() const {
120
2.98k
  for (child_iterator I = child_begin(), E = child_end(); I != E; 
++I612
) {
121
2.45k
    if (const TextComment *TC = dyn_cast<TextComment>(*I)) {
122
2.38k
      if (!TC->isWhitespace())
123
1.77k
        return false;
124
72
    } else
125
72
      return false;
126
2.45k
  }
127
532
  return true;
128
2.37k
}
129
130
722
static TypeLoc lookThroughTypedefOrTypeAliasLocs(TypeLoc &SrcTL) {
131
722
  TypeLoc TL = SrcTL.IgnoreParens();
132
133
  // Look through attribute types.
134
722
  if (AttributedTypeLoc AttributeTL = TL.getAs<AttributedTypeLoc>())
135
3
    return AttributeTL.getModifiedLoc();
136
  // Look through qualified types.
137
719
  if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>())
138
0
    return QualifiedTL.getUnqualifiedLoc();
139
  // Look through pointer types.
140
719
  if (PointerTypeLoc PointerTL = TL.getAs<PointerTypeLoc>())
141
86
    return PointerTL.getPointeeLoc().getUnqualifiedLoc();
142
  // Look through reference types.
143
633
  if (ReferenceTypeLoc ReferenceTL = TL.getAs<ReferenceTypeLoc>())
144
18
    return ReferenceTL.getPointeeLoc().getUnqualifiedLoc();
145
  // Look through adjusted types.
146
615
  if (AdjustedTypeLoc ATL = TL.getAs<AdjustedTypeLoc>())
147
0
    return ATL.getOriginalLoc();
148
615
  if (BlockPointerTypeLoc BlockPointerTL = TL.getAs<BlockPointerTypeLoc>())
149
19
    return BlockPointerTL.getPointeeLoc().getUnqualifiedLoc();
150
596
  if (MemberPointerTypeLoc MemberPointerTL = TL.getAs<MemberPointerTypeLoc>())
151
6
    return MemberPointerTL.getPointeeLoc().getUnqualifiedLoc();
152
590
  if (ElaboratedTypeLoc ETL = TL.getAs<ElaboratedTypeLoc>())
153
77
    return ETL.getNamedTypeLoc();
154
155
513
  return TL;
156
513
}
157
158
451
static bool getFunctionTypeLoc(TypeLoc TL, FunctionTypeLoc &ResFTL) {
159
451
  TypeLoc PrevTL;
160
1.17k
  while (PrevTL != TL) {
161
722
    PrevTL = TL;
162
722
    TL = lookThroughTypedefOrTypeAliasLocs(TL);
163
722
  }
164
165
451
  if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
166
72
    ResFTL = FTL;
167
72
    return true;
168
72
  }
169
170
379
  if (TemplateSpecializationTypeLoc STL =
171
58
          TL.getAs<TemplateSpecializationTypeLoc>()) {
172
    // If we have a typedef to a template specialization with exactly one
173
    // template argument of a function type, this looks like std::function,
174
    // boost::function, or other function wrapper.  Treat these typedefs as
175
    // functions.
176
58
    if (STL.getNumArgs() != 1)
177
16
      return false;
178
42
    TemplateArgumentLoc MaybeFunction = STL.getArgLoc(0);
179
42
    if (MaybeFunction.getArgument().getKind() != TemplateArgument::Type)
180
6
      return false;
181
36
    TypeSourceInfo *MaybeFunctionTSI = MaybeFunction.getTypeSourceInfo();
182
36
    TypeLoc TL = MaybeFunctionTSI->getTypeLoc().getUnqualifiedLoc();
183
36
    if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
184
32
      ResFTL = FTL;
185
32
      return true;
186
32
    }
187
325
  }
188
189
325
  return false;
190
325
}
191
192
39
const char *ParamCommandComment::getDirectionAsString(PassDirection D) {
193
39
  switch (D) {
194
21
  case ParamCommandComment::In:
195
21
    return "[in]";
196
5
  case ParamCommandComment::Out:
197
5
    return "[out]";
198
13
  case ParamCommandComment::InOut:
199
13
    return "[in,out]";
200
0
  }
201
0
  llvm_unreachable("unknown PassDirection");
202
0
}
203
204
1.71k
void DeclInfo::fill() {
205
1.71k
  assert(!IsFilled);
206
207
  // Set defaults.
208
1.71k
  Kind = OtherKind;
209
1.71k
  TemplateKind = NotTemplate;
210
1.71k
  IsObjCMethod = false;
211
1.71k
  IsInstanceMethod = false;
212
1.71k
  IsClassMethod = false;
213
1.71k
  ParamVars = None;
214
1.71k
  TemplateParameters = nullptr;
215
216
1.71k
  if (!CommentDecl) {
217
    // If there is no declaration, the defaults is our only guess.
218
0
    IsFilled = true;
219
0
    return;
220
0
  }
221
1.71k
  CurrentDecl = CommentDecl;
222
223
1.71k
  Decl::Kind K = CommentDecl->getKind();
224
1.71k
  switch (K) {
225
91
  default:
226
    // Defaults are should be good for declarations we don't handle explicitly.
227
91
    break;
228
715
  case Decl::Function:
229
715
  case Decl::CXXMethod:
230
715
  case Decl::CXXConstructor:
231
715
  case Decl::CXXDestructor:
232
715
  case Decl::CXXConversion: {
233
715
    const FunctionDecl *FD = cast<FunctionDecl>(CommentDecl);
234
715
    Kind = FunctionKind;
235
715
    ParamVars = FD->parameters();
236
715
    ReturnType = FD->getReturnType();
237
715
    unsigned NumLists = FD->getNumTemplateParameterLists();
238
715
    if (NumLists != 0) {
239
24
      TemplateKind = TemplateSpecialization;
240
24
      TemplateParameters =
241
24
          FD->getTemplateParameterList(NumLists - 1);
242
24
    }
243
244
715
    if (K == Decl::CXXMethod || 
K == Decl::CXXConstructor632
||
245
619
        K == Decl::CXXDestructor || 
K == Decl::CXXConversion610
) {
246
107
      const CXXMethodDecl *MD = cast<CXXMethodDecl>(CommentDecl);
247
107
      IsInstanceMethod = MD->isInstance();
248
107
      IsClassMethod = !IsInstanceMethod;
249
107
    }
250
715
    break;
251
715
  }
252
89
  case Decl::ObjCMethod: {
253
89
    const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(CommentDecl);
254
89
    Kind = FunctionKind;
255
89
    ParamVars = MD->parameters();
256
89
    ReturnType = MD->getReturnType();
257
89
    IsObjCMethod = true;
258
89
    IsInstanceMethod = MD->isInstanceMethod();
259
89
    IsClassMethod = !IsInstanceMethod;
260
89
    break;
261
715
  }
262
121
  case Decl::FunctionTemplate: {
263
121
    const FunctionTemplateDecl *FTD = cast<FunctionTemplateDecl>(CommentDecl);
264
121
    Kind = FunctionKind;
265
121
    TemplateKind = Template;
266
121
    const FunctionDecl *FD = FTD->getTemplatedDecl();
267
121
    ParamVars = FD->parameters();
268
121
    ReturnType = FD->getReturnType();
269
121
    TemplateParameters = FTD->getTemplateParameters();
270
121
    break;
271
715
  }
272
45
  case Decl::ClassTemplate: {
273
45
    const ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(CommentDecl);
274
45
    Kind = ClassKind;
275
45
    TemplateKind = Template;
276
45
    TemplateParameters = CTD->getTemplateParameters();
277
45
    break;
278
715
  }
279
14
  case Decl::ClassTemplatePartialSpecialization: {
280
14
    const ClassTemplatePartialSpecializationDecl *CTPSD =
281
14
        cast<ClassTemplatePartialSpecializationDecl>(CommentDecl);
282
14
    Kind = ClassKind;
283
14
    TemplateKind = TemplatePartialSpecialization;
284
14
    TemplateParameters = CTPSD->getTemplateParameters();
285
14
    break;
286
715
  }
287
11
  case Decl::ClassTemplateSpecialization:
288
11
    Kind = ClassKind;
289
11
    TemplateKind = TemplateSpecialization;
290
11
    break;
291
104
  case Decl::Record:
292
104
  case Decl::CXXRecord:
293
104
    Kind = ClassKind;
294
104
    break;
295
312
  case Decl::Var:
296
312
  case Decl::Field:
297
312
  case Decl::EnumConstant:
298
312
  case Decl::ObjCIvar:
299
312
  case Decl::ObjCAtDefsField:
300
312
  case Decl::ObjCProperty: {
301
312
    const TypeSourceInfo *TSI;
302
312
    if (const auto *VD = dyn_cast<DeclaratorDecl>(CommentDecl))
303
250
      TSI = VD->getTypeSourceInfo();
304
62
    else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(CommentDecl))
305
33
      TSI = PD->getTypeSourceInfo();
306
29
    else
307
29
      TSI = nullptr;
308
312
    if (TSI) {
309
283
      TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
310
283
      FunctionTypeLoc FTL;
311
283
      if (getFunctionTypeLoc(TL, FTL)) {
312
24
        ParamVars = FTL.getParams();
313
24
        ReturnType = FTL.getReturnLoc().getType();
314
24
      }
315
283
    }
316
312
    Kind = VariableKind;
317
312
    break;
318
312
  }
319
15
  case Decl::Namespace:
320
15
    Kind = NamespaceKind;
321
15
    break;
322
145
  case Decl::TypeAlias:
323
145
  case Decl::Typedef: {
324
145
    Kind = TypedefKind;
325
    // If this is a typedef / using to something we consider a function, extract
326
    // arguments and return type.
327
145
    const TypeSourceInfo *TSI =
328
145
        K == Decl::Typedef
329
90
            ? cast<TypedefDecl>(CommentDecl)->getTypeSourceInfo()
330
55
            : cast<TypeAliasDecl>(CommentDecl)->getTypeSourceInfo();
331
145
    if (!TSI)
332
0
      break;
333
145
    TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
334
145
    FunctionTypeLoc FTL;
335
145
    if (getFunctionTypeLoc(TL, FTL)) {
336
68
      Kind = FunctionKind;
337
68
      ParamVars = FTL.getParams();
338
68
      ReturnType = FTL.getReturnLoc().getType();
339
68
    }
340
145
    break;
341
145
  }
342
23
  case Decl::TypeAliasTemplate: {
343
23
    const TypeAliasTemplateDecl *TAT = cast<TypeAliasTemplateDecl>(CommentDecl);
344
23
    Kind = TypedefKind;
345
23
    TemplateKind = Template;
346
23
    TemplateParameters = TAT->getTemplateParameters();
347
23
    TypeAliasDecl *TAD = TAT->getTemplatedDecl();
348
23
    if (!TAD)
349
0
      break;
350
351
23
    const TypeSourceInfo *TSI = TAD->getTypeSourceInfo();
352
23
    if (!TSI)
353
0
      break;
354
23
    TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
355
23
    FunctionTypeLoc FTL;
356
23
    if (getFunctionTypeLoc(TL, FTL)) {
357
12
      Kind = FunctionKind;
358
12
      ParamVars = FTL.getParams();
359
12
      ReturnType = FTL.getReturnLoc().getType();
360
12
    }
361
23
    break;
362
23
  }
363
25
  case Decl::Enum:
364
25
    Kind = EnumKind;
365
25
    break;
366
1.71k
  }
367
368
1.71k
  IsFilled = true;
369
1.71k
}
370
371
252
StringRef ParamCommandComment::getParamName(const FullComment *FC) const {
372
252
  assert(isParamIndexValid());
373
252
  if (isVarArgParam())
374
14
    return "...";
375
238
  return FC->getDeclInfo()->ParamVars[getParamIndex()]->getName();
376
238
}
377
378
163
StringRef TParamCommandComment::getParamName(const FullComment *FC) const {
379
163
  assert(isPositionValid());
380
163
  const TemplateParameterList *TPL = FC->getDeclInfo()->TemplateParameters;
381
227
  for (unsigned i = 0, e = getDepth(); i != e; 
++i64
) {
382
227
    assert(TPL && "Unknown TemplateParameterList");
383
227
    if (i == e - 1)
384
163
      return TPL->getParam(getIndex(i))->getName();
385
64
    const NamedDecl *Param = TPL->getParam(getIndex(i));
386
64
    if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param))
387
64
      TPL = TTP->getTemplateParameters();
388
64
  }
389
0
  return "";
390
163
}
391
392
} // end namespace comments
393
} // end namespace clang
394