Coverage Report

Created: 2024-01-17 10:31

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