Coverage Report

Created: 2019-07-24 05:18

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