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