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