Coverage Report

Created: 2020-02-15 09:57

/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
584
const char *Comment::getCommentKindName() const {
36
584
  switch (getCommentKind()) {
37
0
  case NoCommentKind: return "NoCommentKind";
38
0
#define ABSTRACT_COMMENT(COMMENT)
39
0
#define COMMENT(CLASS, PARENT) \
40
584
  case CLASS##Kind: \
41
584
    return #CLASS;
42
0
#include "clang/AST/CommentNodes.inc"
43
584
#undef COMMENT
44
584
#undef ABSTRACT_COMMENT
45
584
  }
46
584
  
llvm_unreachable0
("Unknown comment kind!");
47
584
}
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.3k
Comment::child_iterator Comment::child_begin() const {
83
18.3k
  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.3k
  case CLASS##Kind: \
88
18.3k
    return static_cast<const CLASS *>(this)->child_begin();
89
0
#include "clang/AST/CommentNodes.inc"
90
18.3k
#undef COMMENT
91
18.3k
#undef ABSTRACT_COMMENT
92
18.3k
  }
93
18.3k
  
llvm_unreachable0
("Unknown comment kind!");
94
18.3k
}
95
96
12.7k
Comment::child_iterator Comment::child_end() const {
97
12.7k
  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.7k
  case CLASS##Kind: \
102
12.7k
    return static_cast<const CLASS *>(this)->child_end();
103
0
#include "clang/AST/CommentNodes.inc"
104
12.7k
#undef COMMENT
105
12.7k
#undef ABSTRACT_COMMENT
106
12.7k
  }
107
12.7k
  
llvm_unreachable0
("Unknown comment kind!");
108
12.7k
}
109
110
2.75k
bool TextComment::isWhitespaceNoCache() const {
111
2.75k
  for (StringRef::const_iterator I = Text.begin(), E = Text.end();
112
5.53k
       I != E; 
++I2.77k
) {
113
4.66k
    if (!clang::isWhitespace(*I))
114
1.89k
      return false;
115
4.66k
  }
116
2.75k
  
return true864
;
117
2.75k
}
118
119
2.31k
bool ParagraphComment::isWhitespaceNoCache() const {
120
2.92k
  for (child_iterator I = child_begin(), E = child_end(); I != E; 
++I608
) {
121
2.39k
    if (const TextComment *TC = dyn_cast<TextComment>(*I)) {
122
2.32k
      if (!TC->isWhitespace())
123
1.71k
        return false;
124
69
    } else
125
69
      return false;
126
2.39k
  }
127
2.31k
  
return true531
;
128
2.31k
}
129
130
665
static TypeLoc lookThroughTypedefOrTypeAliasLocs(TypeLoc &SrcTL) {
131
665
  TypeLoc TL = SrcTL.IgnoreParens();
132
665
133
665
  // Look through attribute types.
134
665
  if (AttributedTypeLoc AttributeTL = TL.getAs<AttributedTypeLoc>())
135
2
    return AttributeTL.getModifiedLoc();
136
663
  // Look through qualified types.
137
663
  if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>())
138
0
    return QualifiedTL.getUnqualifiedLoc();
139
663
  // Look through pointer types.
140
663
  if (PointerTypeLoc PointerTL = TL.getAs<PointerTypeLoc>())
141
84
    return PointerTL.getPointeeLoc().getUnqualifiedLoc();
142
579
  // Look through reference types.
143
579
  if (ReferenceTypeLoc ReferenceTL = TL.getAs<ReferenceTypeLoc>())
144
18
    return ReferenceTL.getPointeeLoc().getUnqualifiedLoc();
145
561
  // Look through adjusted types.
146
561
  if (AdjustedTypeLoc ATL = TL.getAs<AdjustedTypeLoc>())
147
0
    return ATL.getOriginalLoc();
148
561
  if (BlockPointerTypeLoc BlockPointerTL = TL.getAs<BlockPointerTypeLoc>())
149
11
    return BlockPointerTL.getPointeeLoc().getUnqualifiedLoc();
150
550
  if (MemberPointerTypeLoc MemberPointerTL = TL.getAs<MemberPointerTypeLoc>())
151
6
    return MemberPointerTL.getPointeeLoc().getUnqualifiedLoc();
152
544
  if (ElaboratedTypeLoc ETL = TL.getAs<ElaboratedTypeLoc>())
153
75
    return ETL.getNamedTypeLoc();
154
469
155
469
  return TL;
156
469
}
157
158
415
static bool getFunctionTypeLoc(TypeLoc TL, FunctionTypeLoc &ResFTL) {
159
415
  TypeLoc PrevTL;
160
1.08k
  while (PrevTL != TL) {
161
665
    PrevTL = TL;
162
665
    TL = lookThroughTypedefOrTypeAliasLocs(TL);
163
665
  }
164
415
165
415
  if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
166
64
    ResFTL = FTL;
167
64
    return true;
168
64
  }
169
351
170
351
  if (TemplateSpecializationTypeLoc STL =
171
58
          TL.getAs<TemplateSpecializationTypeLoc>()) {
172
58
    // If we have a typedef to a template specialization with exactly one
173
58
    // template argument of a function type, this looks like std::function,
174
58
    // boost::function, or other function wrapper.  Treat these typedefs as
175
58
    // 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
297
  }
188
297
189
297
  return false;
190
297
}
191
192
35
const char *ParamCommandComment::getDirectionAsString(PassDirection D) {
193
35
  switch (D) {
194
18
  case ParamCommandComment::In:
195
18
    return "[in]";
196
5
  case ParamCommandComment::Out:
197
5
    return "[out]";
198
12
  case ParamCommandComment::InOut:
199
12
    return "[in,out]";
200
0
  }
201
0
  llvm_unreachable("unknown PassDirection");
202
0
}
203
204
1.63k
void DeclInfo::fill() {
205
1.63k
  assert(!IsFilled);
206
1.63k
207
1.63k
  // Set defaults.
208
1.63k
  Kind = OtherKind;
209
1.63k
  TemplateKind = NotTemplate;
210
1.63k
  IsObjCMethod = false;
211
1.63k
  IsInstanceMethod = false;
212
1.63k
  IsClassMethod = false;
213
1.63k
  ParamVars = None;
214
1.63k
  TemplateParameters = nullptr;
215
1.63k
216
1.63k
  if (!CommentDecl) {
217
0
    // If there is no declaration, the defaults is our only guess.
218
0
    IsFilled = true;
219
0
    return;
220
0
  }
221
1.63k
  CurrentDecl = CommentDecl;
222
1.63k
223
1.63k
  Decl::Kind K = CommentDecl->getKind();
224
1.63k
  switch (K) {
225
77
  default:
226
77
    // Defaults are should be good for declarations we don't handle explicitly.
227
77
    break;
228
708
  case Decl::Function:
229
708
  case Decl::CXXMethod:
230
708
  case Decl::CXXConstructor:
231
708
  case Decl::CXXDestructor:
232
708
  case Decl::CXXConversion: {
233
708
    const FunctionDecl *FD = cast<FunctionDecl>(CommentDecl);
234
708
    Kind = FunctionKind;
235
708
    ParamVars = FD->parameters();
236
708
    ReturnType = FD->getReturnType();
237
708
    unsigned NumLists = FD->getNumTemplateParameterLists();
238
708
    if (NumLists != 0) {
239
24
      TemplateKind = TemplateSpecialization;
240
24
      TemplateParameters =
241
24
          FD->getTemplateParameterList(NumLists - 1);
242
24
    }
243
708
244
708
    if (K == Decl::CXXMethod || 
K == Decl::CXXConstructor625
||
245
708
        
K == Decl::CXXDestructor612
||
K == Decl::CXXConversion603
) {
246
107
      const CXXMethodDecl *MD = cast<CXXMethodDecl>(CommentDecl);
247
107
      IsInstanceMethod = MD->isInstance();
248
107
      IsClassMethod = !IsInstanceMethod;
249
107
    }
250
708
    break;
251
708
  }
252
708
  case Decl::ObjCMethod: {
253
81
    const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(CommentDecl);
254
81
    Kind = FunctionKind;
255
81
    ParamVars = MD->parameters();
256
81
    ReturnType = MD->getReturnType();
257
81
    IsObjCMethod = true;
258
81
    IsInstanceMethod = MD->isInstanceMethod();
259
81
    IsClassMethod = !IsInstanceMethod;
260
81
    break;
261
708
  }
262
708
  case Decl::FunctionTemplate: {
263
119
    const FunctionTemplateDecl *FTD = cast<FunctionTemplateDecl>(CommentDecl);
264
119
    Kind = FunctionKind;
265
119
    TemplateKind = Template;
266
119
    const FunctionDecl *FD = FTD->getTemplatedDecl();
267
119
    ParamVars = FD->parameters();
268
119
    ReturnType = FD->getReturnType();
269
119
    TemplateParameters = FTD->getTemplateParameters();
270
119
    break;
271
708
  }
272
708
  case Decl::ClassTemplate: {
273
43
    const ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(CommentDecl);
274
43
    Kind = ClassKind;
275
43
    TemplateKind = Template;
276
43
    TemplateParameters = CTD->getTemplateParameters();
277
43
    break;
278
708
  }
279
708
  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
708
  }
287
708
  case Decl::ClassTemplateSpecialization:
288
11
    Kind = ClassKind;
289
11
    TemplateKind = TemplateSpecialization;
290
11
    break;
291
708
  case Decl::Record:
292
100
  case Decl::CXXRecord:
293
100
    Kind = ClassKind;
294
100
    break;
295
283
  case Decl::Var:
296
283
  case Decl::Field:
297
283
  case Decl::EnumConstant:
298
283
  case Decl::ObjCIvar:
299
283
  case Decl::ObjCAtDefsField:
300
283
  case Decl::ObjCProperty: {
301
283
    const TypeSourceInfo *TSI;
302
283
    if (const auto *VD = dyn_cast<DeclaratorDecl>(CommentDecl))
303
226
      TSI = VD->getTypeSourceInfo();
304
57
    else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(CommentDecl))
305
28
      TSI = PD->getTypeSourceInfo();
306
29
    else
307
29
      TSI = nullptr;
308
283
    if (TSI) {
309
254
      TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
310
254
      FunctionTypeLoc FTL;
311
254
      if (getFunctionTypeLoc(TL, FTL)) {
312
18
        ParamVars = FTL.getParams();
313
18
        ReturnType = FTL.getReturnLoc().getType();
314
18
      }
315
254
    }
316
283
    Kind = VariableKind;
317
283
    break;
318
283
  }
319
283
  case Decl::Namespace:
320
15
    Kind = NamespaceKind;
321
15
    break;
322
283
  case Decl::TypeAlias:
323
138
  case Decl::Typedef: {
324
138
    Kind = TypedefKind;
325
138
    // If this is a typedef / using to something we consider a function, extract
326
138
    // arguments and return type.
327
138
    const TypeSourceInfo *TSI =
328
138
        K == Decl::Typedef
329
138
            ? 
cast<TypedefDecl>(CommentDecl)->getTypeSourceInfo()83
330
138
            : 
cast<TypeAliasDecl>(CommentDecl)->getTypeSourceInfo()55
;
331
138
    if (!TSI)
332
0
      break;
333
138
    TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
334
138
    FunctionTypeLoc FTL;
335
138
    if (getFunctionTypeLoc(TL, FTL)) {
336
66
      Kind = FunctionKind;
337
66
      ParamVars = FTL.getParams();
338
66
      ReturnType = FTL.getReturnLoc().getType();
339
66
    }
340
138
    break;
341
138
  }
342
138
  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
23
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.63k
  }
367
1.63k
368
1.63k
  IsFilled = true;
369
1.63k
}
370
371
246
StringRef ParamCommandComment::getParamName(const FullComment *FC) const {
372
246
  assert(isParamIndexValid());
373
246
  if (isVarArgParam())
374
12
    return "...";
375
234
  return FC->getDeclInfo()->ParamVars[getParamIndex()]->getName();
376
234
}
377
378
162
StringRef TParamCommandComment::getParamName(const FullComment *FC) const {
379
162
  assert(isPositionValid());
380
162
  const TemplateParameterList *TPL = FC->getDeclInfo()->TemplateParameters;
381
226
  for (unsigned i = 0, e = getDepth(); i != e; 
++i64
) {
382
226
    assert(TPL && "Unknown TemplateParameterList");
383
226
    if (i == e - 1)
384
162
      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
162
  
return ""0
;
390
162
}
391
392
} // end namespace comments
393
} // end namespace clang
394