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