/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/CommentSema.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- CommentSema.cpp - Doxygen comment semantic analysis --------------===// |
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/CommentSema.h" |
10 | | #include "clang/AST/Attr.h" |
11 | | #include "clang/AST/CommentCommandTraits.h" |
12 | | #include "clang/AST/CommentDiagnostic.h" |
13 | | #include "clang/AST/Decl.h" |
14 | | #include "clang/AST/DeclTemplate.h" |
15 | | #include "clang/Basic/LLVM.h" |
16 | | #include "clang/Basic/SourceManager.h" |
17 | | #include "clang/Lex/Preprocessor.h" |
18 | | #include "llvm/ADT/SmallString.h" |
19 | | #include "llvm/ADT/StringSwitch.h" |
20 | | |
21 | | namespace clang { |
22 | | namespace comments { |
23 | | |
24 | | namespace { |
25 | | #include "clang/AST/CommentHTMLTagsProperties.inc" |
26 | | } // end anonymous namespace |
27 | | |
28 | | Sema::Sema(llvm::BumpPtrAllocator &Allocator, const SourceManager &SourceMgr, |
29 | | DiagnosticsEngine &Diags, CommandTraits &Traits, |
30 | | const Preprocessor *PP) : |
31 | | Allocator(Allocator), SourceMgr(SourceMgr), Diags(Diags), Traits(Traits), |
32 | | PP(PP), ThisDeclInfo(nullptr), BriefCommand(nullptr), |
33 | 3.29k | HeaderfileCommand(nullptr) { |
34 | 3.29k | } |
35 | | |
36 | 3.21k | void Sema::setDecl(const Decl *D) { |
37 | 3.21k | if (!D) |
38 | 0 | return; |
39 | | |
40 | 3.21k | ThisDeclInfo = new (Allocator) DeclInfo; |
41 | 3.21k | ThisDeclInfo->CommentDecl = D; |
42 | 3.21k | ThisDeclInfo->IsFilled = false; |
43 | 3.21k | } |
44 | | |
45 | | ParagraphComment *Sema::actOnParagraphComment( |
46 | 16.0k | ArrayRef<InlineContentComment *> Content) { |
47 | 16.0k | return new (Allocator) ParagraphComment(Content); |
48 | 16.0k | } |
49 | | |
50 | | BlockCommandComment *Sema::actOnBlockCommandStart( |
51 | | SourceLocation LocBegin, |
52 | | SourceLocation LocEnd, |
53 | | unsigned CommandID, |
54 | 3.94k | CommandMarkerKind CommandMarker) { |
55 | 3.94k | BlockCommandComment *BC = new (Allocator) BlockCommandComment(LocBegin, LocEnd, |
56 | 3.94k | CommandID, |
57 | 3.94k | CommandMarker); |
58 | 3.94k | checkContainerDecl(BC); |
59 | 3.94k | return BC; |
60 | 3.94k | } |
61 | | |
62 | | void Sema::actOnBlockCommandArgs(BlockCommandComment *Command, |
63 | 16 | ArrayRef<BlockCommandComment::Argument> Args) { |
64 | 16 | Command->setArgs(Args); |
65 | 16 | } |
66 | | |
67 | | void Sema::actOnBlockCommandFinish(BlockCommandComment *Command, |
68 | 3.94k | ParagraphComment *Paragraph) { |
69 | 3.94k | Command->setParagraph(Paragraph); |
70 | 3.94k | checkBlockCommandEmptyParagraph(Command); |
71 | 3.94k | checkBlockCommandDuplicate(Command); |
72 | 3.94k | if (ThisDeclInfo) { |
73 | | // These checks only make sense if the comment is attached to a |
74 | | // declaration. |
75 | 3.93k | checkReturnsCommand(Command); |
76 | 3.93k | checkDeprecatedCommand(Command); |
77 | 3.93k | } |
78 | 3.94k | } |
79 | | |
80 | | ParamCommandComment *Sema::actOnParamCommandStart( |
81 | | SourceLocation LocBegin, |
82 | | SourceLocation LocEnd, |
83 | | unsigned CommandID, |
84 | 3.49k | CommandMarkerKind CommandMarker) { |
85 | 3.49k | ParamCommandComment *Command = |
86 | 3.49k | new (Allocator) ParamCommandComment(LocBegin, LocEnd, CommandID, |
87 | 3.49k | CommandMarker); |
88 | | |
89 | 3.49k | if (!involvesFunctionType()) |
90 | 53 | Diag(Command->getLocation(), |
91 | 53 | diag::warn_doc_param_not_attached_to_a_function_decl) |
92 | 53 | << CommandMarker |
93 | 53 | << Command->getCommandNameRange(Traits); |
94 | | |
95 | 3.49k | return Command; |
96 | 3.49k | } |
97 | | |
98 | 111 | void Sema::checkFunctionDeclVerbatimLine(const BlockCommandComment *Comment) { |
99 | 111 | const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID()); |
100 | 111 | if (!Info->IsFunctionDeclarationCommand) |
101 | 89 | return; |
102 | | |
103 | 22 | unsigned DiagSelect; |
104 | 22 | switch (Comment->getCommandID()) { |
105 | 11 | case CommandTraits::KCI_function: |
106 | 11 | DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl()6 )? 13 : 08 ; |
107 | 11 | break; |
108 | 0 | case CommandTraits::KCI_functiongroup: |
109 | 0 | DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 2 : 0; |
110 | 0 | break; |
111 | 6 | case CommandTraits::KCI_method: |
112 | 6 | DiagSelect = !isObjCMethodDecl() ? 3 : 00 ; |
113 | 6 | break; |
114 | 2 | case CommandTraits::KCI_methodgroup: |
115 | 2 | DiagSelect = !isObjCMethodDecl() ? 4 : 00 ; |
116 | 2 | break; |
117 | 3 | case CommandTraits::KCI_callback: |
118 | 3 | DiagSelect = !isFunctionPointerVarDecl() ? 5 : 00 ; |
119 | 3 | break; |
120 | 0 | default: |
121 | 0 | DiagSelect = 0; |
122 | 0 | break; |
123 | 22 | } |
124 | 22 | if (DiagSelect) |
125 | 14 | Diag(Comment->getLocation(), diag::warn_doc_function_method_decl_mismatch) |
126 | 14 | << Comment->getCommandMarker() |
127 | 14 | << (DiagSelect-1) << (DiagSelect-1) |
128 | 14 | << Comment->getSourceRange(); |
129 | 22 | } |
130 | | |
131 | 111 | void Sema::checkContainerDeclVerbatimLine(const BlockCommandComment *Comment) { |
132 | 111 | const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID()); |
133 | 111 | if (!Info->IsRecordLikeDeclarationCommand) |
134 | 61 | return; |
135 | 50 | unsigned DiagSelect; |
136 | 50 | switch (Comment->getCommandID()) { |
137 | 18 | case CommandTraits::KCI_class: |
138 | 18 | DiagSelect = |
139 | 18 | (!isClassOrStructOrTagTypedefDecl() && !isClassTemplateDecl()7 ) ? 14 |
140 | 18 | : 014 ; |
141 | | // Allow @class command on @interface declarations. |
142 | | // FIXME. Currently, \class and @class are indistinguishable. So, |
143 | | // \class is also allowed on an @interface declaration |
144 | 18 | if (DiagSelect && Comment->getCommandMarker()4 && isObjCInterfaceDecl()4 ) |
145 | 2 | DiagSelect = 0; |
146 | 18 | break; |
147 | 9 | case CommandTraits::KCI_interface: |
148 | 9 | DiagSelect = !isObjCInterfaceDecl() ? 24 : 05 ; |
149 | 9 | break; |
150 | 6 | case CommandTraits::KCI_protocol: |
151 | 6 | DiagSelect = !isObjCProtocolDecl() ? 34 : 02 ; |
152 | 6 | break; |
153 | 11 | case CommandTraits::KCI_struct: |
154 | 11 | DiagSelect = !isClassOrStructOrTagTypedefDecl() ? 45 : 06 ; |
155 | 11 | break; |
156 | 6 | case CommandTraits::KCI_union: |
157 | 6 | DiagSelect = !isUnionDecl() ? 53 : 03 ; |
158 | 6 | break; |
159 | 0 | default: |
160 | 0 | DiagSelect = 0; |
161 | 0 | break; |
162 | 50 | } |
163 | 50 | if (DiagSelect) |
164 | 18 | Diag(Comment->getLocation(), diag::warn_doc_api_container_decl_mismatch) |
165 | 18 | << Comment->getCommandMarker() |
166 | 18 | << (DiagSelect-1) << (DiagSelect-1) |
167 | 18 | << Comment->getSourceRange(); |
168 | 50 | } |
169 | | |
170 | 3.94k | void Sema::checkContainerDecl(const BlockCommandComment *Comment) { |
171 | 3.94k | const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID()); |
172 | 3.94k | if (!Info->IsRecordLikeDetailCommand || isRecordLikeDecl()4 ) |
173 | 3.94k | return; |
174 | 4 | unsigned DiagSelect; |
175 | 4 | switch (Comment->getCommandID()) { |
176 | 2 | case CommandTraits::KCI_classdesign: |
177 | 2 | DiagSelect = 1; |
178 | 2 | break; |
179 | 2 | case CommandTraits::KCI_coclass: |
180 | 2 | DiagSelect = 2; |
181 | 2 | break; |
182 | 0 | case CommandTraits::KCI_dependency: |
183 | 0 | DiagSelect = 3; |
184 | 0 | break; |
185 | 0 | case CommandTraits::KCI_helper: |
186 | 0 | DiagSelect = 4; |
187 | 0 | break; |
188 | 0 | case CommandTraits::KCI_helperclass: |
189 | 0 | DiagSelect = 5; |
190 | 0 | break; |
191 | 0 | case CommandTraits::KCI_helps: |
192 | 0 | DiagSelect = 6; |
193 | 0 | break; |
194 | 0 | case CommandTraits::KCI_instancesize: |
195 | 0 | DiagSelect = 7; |
196 | 0 | break; |
197 | 0 | case CommandTraits::KCI_ownership: |
198 | 0 | DiagSelect = 8; |
199 | 0 | break; |
200 | 0 | case CommandTraits::KCI_performance: |
201 | 0 | DiagSelect = 9; |
202 | 0 | break; |
203 | 0 | case CommandTraits::KCI_security: |
204 | 0 | DiagSelect = 10; |
205 | 0 | break; |
206 | 0 | case CommandTraits::KCI_superclass: |
207 | 0 | DiagSelect = 11; |
208 | 0 | break; |
209 | 0 | default: |
210 | 0 | DiagSelect = 0; |
211 | 0 | break; |
212 | 4 | } |
213 | 4 | if (DiagSelect) |
214 | 4 | Diag(Comment->getLocation(), diag::warn_doc_container_decl_mismatch) |
215 | 4 | << Comment->getCommandMarker() |
216 | 4 | << (DiagSelect-1) |
217 | 4 | << Comment->getSourceRange(); |
218 | 4 | } |
219 | | |
220 | | /// Turn a string into the corresponding PassDirection or -1 if it's not |
221 | | /// valid. |
222 | 65 | static int getParamPassDirection(StringRef Arg) { |
223 | 65 | return llvm::StringSwitch<int>(Arg) |
224 | 65 | .Case("[in]", ParamCommandComment::In) |
225 | 65 | .Case("[out]", ParamCommandComment::Out) |
226 | 65 | .Cases("[in,out]", "[out,in]", ParamCommandComment::InOut) |
227 | 65 | .Default(-1); |
228 | 65 | } |
229 | | |
230 | | void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command, |
231 | | SourceLocation ArgLocBegin, |
232 | | SourceLocation ArgLocEnd, |
233 | 54 | StringRef Arg) { |
234 | 54 | std::string ArgLower = Arg.lower(); |
235 | 54 | int Direction = getParamPassDirection(ArgLower); |
236 | | |
237 | 54 | if (Direction == -1) { |
238 | | // Try again with whitespace removed. |
239 | 11 | llvm::erase_if(ArgLower, clang::isWhitespace); |
240 | 11 | Direction = getParamPassDirection(ArgLower); |
241 | | |
242 | 11 | SourceRange ArgRange(ArgLocBegin, ArgLocEnd); |
243 | 11 | if (Direction != -1) { |
244 | 8 | const char *FixedName = ParamCommandComment::getDirectionAsString( |
245 | 8 | (ParamCommandComment::PassDirection)Direction); |
246 | 8 | Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction) |
247 | 8 | << ArgRange << FixItHint::CreateReplacement(ArgRange, FixedName); |
248 | 8 | } else { |
249 | 3 | Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction) << ArgRange; |
250 | 3 | Direction = ParamCommandComment::In; // Sane fall back. |
251 | 3 | } |
252 | 11 | } |
253 | 54 | Command->setDirection((ParamCommandComment::PassDirection)Direction, |
254 | 54 | /*Explicit=*/true); |
255 | 54 | } |
256 | | |
257 | | void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command, |
258 | | SourceLocation ArgLocBegin, |
259 | | SourceLocation ArgLocEnd, |
260 | 3.48k | StringRef Arg) { |
261 | | // Parser will not feed us more arguments than needed. |
262 | 3.48k | assert(Command->getNumArgs() == 0); |
263 | | |
264 | 3.48k | if (!Command->isDirectionExplicit()) { |
265 | | // User didn't provide a direction argument. |
266 | 3.43k | Command->setDirection(ParamCommandComment::In, /* Explicit = */ false); |
267 | 3.43k | } |
268 | 3.48k | auto *A = new (Allocator) |
269 | 3.48k | Comment::Argument{SourceRange(ArgLocBegin, ArgLocEnd), Arg}; |
270 | 3.48k | Command->setArgs(llvm::makeArrayRef(A, 1)); |
271 | 3.48k | } |
272 | | |
273 | | void Sema::actOnParamCommandFinish(ParamCommandComment *Command, |
274 | 3.49k | ParagraphComment *Paragraph) { |
275 | 3.49k | Command->setParagraph(Paragraph); |
276 | 3.49k | checkBlockCommandEmptyParagraph(Command); |
277 | 3.49k | } |
278 | | |
279 | | TParamCommandComment *Sema::actOnTParamCommandStart( |
280 | | SourceLocation LocBegin, |
281 | | SourceLocation LocEnd, |
282 | | unsigned CommandID, |
283 | 207 | CommandMarkerKind CommandMarker) { |
284 | 207 | TParamCommandComment *Command = |
285 | 207 | new (Allocator) TParamCommandComment(LocBegin, LocEnd, CommandID, |
286 | 207 | CommandMarker); |
287 | | |
288 | 207 | if (!isTemplateOrSpecialization()) |
289 | 28 | Diag(Command->getLocation(), |
290 | 28 | diag::warn_doc_tparam_not_attached_to_a_template_decl) |
291 | 28 | << CommandMarker |
292 | 28 | << Command->getCommandNameRange(Traits); |
293 | | |
294 | 207 | return Command; |
295 | 207 | } |
296 | | |
297 | | void Sema::actOnTParamCommandParamNameArg(TParamCommandComment *Command, |
298 | | SourceLocation ArgLocBegin, |
299 | | SourceLocation ArgLocEnd, |
300 | 199 | StringRef Arg) { |
301 | | // Parser will not feed us more arguments than needed. |
302 | 199 | assert(Command->getNumArgs() == 0); |
303 | | |
304 | 0 | auto *A = new (Allocator) |
305 | 199 | Comment::Argument{SourceRange(ArgLocBegin, ArgLocEnd), Arg}; |
306 | 199 | Command->setArgs(llvm::makeArrayRef(A, 1)); |
307 | | |
308 | 199 | if (!isTemplateOrSpecialization()) { |
309 | | // We already warned that this \\tparam is not attached to a template decl. |
310 | 27 | return; |
311 | 27 | } |
312 | | |
313 | 172 | const TemplateParameterList *TemplateParameters = |
314 | 172 | ThisDeclInfo->TemplateParameters; |
315 | 172 | SmallVector<unsigned, 2> Position; |
316 | 172 | if (resolveTParamReference(Arg, TemplateParameters, &Position)) { |
317 | 121 | Command->setPosition(copyArray(llvm::makeArrayRef(Position))); |
318 | 121 | TParamCommandComment *&PrevCommand = TemplateParameterDocs[Arg]; |
319 | 121 | if (PrevCommand) { |
320 | 3 | SourceRange ArgRange(ArgLocBegin, ArgLocEnd); |
321 | 3 | Diag(ArgLocBegin, diag::warn_doc_tparam_duplicate) |
322 | 3 | << Arg << ArgRange; |
323 | 3 | Diag(PrevCommand->getLocation(), diag::note_doc_tparam_previous) |
324 | 3 | << PrevCommand->getParamNameRange(); |
325 | 3 | } |
326 | 121 | PrevCommand = Command; |
327 | 121 | return; |
328 | 121 | } |
329 | | |
330 | 51 | SourceRange ArgRange(ArgLocBegin, ArgLocEnd); |
331 | 51 | Diag(ArgLocBegin, diag::warn_doc_tparam_not_found) |
332 | 51 | << Arg << ArgRange; |
333 | | |
334 | 51 | if (!TemplateParameters || TemplateParameters->size() == 048 ) |
335 | 12 | return; |
336 | | |
337 | 39 | StringRef CorrectedName; |
338 | 39 | if (TemplateParameters->size() == 1) { |
339 | 28 | const NamedDecl *Param = TemplateParameters->getParam(0); |
340 | 28 | const IdentifierInfo *II = Param->getIdentifier(); |
341 | 28 | if (II) |
342 | 28 | CorrectedName = II->getName(); |
343 | 28 | } else { |
344 | 11 | CorrectedName = correctTypoInTParamReference(Arg, TemplateParameters); |
345 | 11 | } |
346 | | |
347 | 39 | if (!CorrectedName.empty()) { |
348 | 35 | Diag(ArgLocBegin, diag::note_doc_tparam_name_suggestion) |
349 | 35 | << CorrectedName |
350 | 35 | << FixItHint::CreateReplacement(ArgRange, CorrectedName); |
351 | 35 | } |
352 | 39 | } |
353 | | |
354 | | void Sema::actOnTParamCommandFinish(TParamCommandComment *Command, |
355 | 207 | ParagraphComment *Paragraph) { |
356 | 207 | Command->setParagraph(Paragraph); |
357 | 207 | checkBlockCommandEmptyParagraph(Command); |
358 | 207 | } |
359 | | |
360 | | InlineCommandComment * |
361 | | Sema::actOnInlineCommand(SourceLocation CommandLocBegin, |
362 | | SourceLocation CommandLocEnd, unsigned CommandID, |
363 | 1.30k | ArrayRef<Comment::Argument> Args) { |
364 | 1.30k | StringRef CommandName = Traits.getCommandInfo(CommandID)->Name; |
365 | | |
366 | 1.30k | return new (Allocator) |
367 | 1.30k | InlineCommandComment(CommandLocBegin, CommandLocEnd, CommandID, |
368 | 1.30k | getInlineCommandRenderKind(CommandName), Args); |
369 | 1.30k | } |
370 | | |
371 | | InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin, |
372 | | SourceLocation LocEnd, |
373 | 150 | StringRef CommandName) { |
374 | 150 | unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID(); |
375 | 150 | return actOnUnknownCommand(LocBegin, LocEnd, CommandID); |
376 | 150 | } |
377 | | |
378 | | InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin, |
379 | | SourceLocation LocEnd, |
380 | 154 | unsigned CommandID) { |
381 | 154 | ArrayRef<InlineCommandComment::Argument> Args; |
382 | 154 | return new (Allocator) InlineCommandComment( |
383 | 154 | LocBegin, LocEnd, CommandID, |
384 | 154 | InlineCommandComment::RenderNormal, |
385 | 154 | Args); |
386 | 154 | } |
387 | | |
388 | | TextComment *Sema::actOnText(SourceLocation LocBegin, |
389 | | SourceLocation LocEnd, |
390 | 34.2k | StringRef Text) { |
391 | 34.2k | return new (Allocator) TextComment(LocBegin, LocEnd, Text); |
392 | 34.2k | } |
393 | | |
394 | | VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc, |
395 | 103 | unsigned CommandID) { |
396 | 103 | StringRef CommandName = Traits.getCommandInfo(CommandID)->Name; |
397 | 103 | return new (Allocator) VerbatimBlockComment( |
398 | 103 | Loc, |
399 | 103 | Loc.getLocWithOffset(1 + CommandName.size()), |
400 | 103 | CommandID); |
401 | 103 | } |
402 | | |
403 | | VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc, |
404 | 896 | StringRef Text) { |
405 | 896 | return new (Allocator) VerbatimBlockLineComment(Loc, Text); |
406 | 896 | } |
407 | | |
408 | | void Sema::actOnVerbatimBlockFinish( |
409 | | VerbatimBlockComment *Block, |
410 | | SourceLocation CloseNameLocBegin, |
411 | | StringRef CloseName, |
412 | 103 | ArrayRef<VerbatimBlockLineComment *> Lines) { |
413 | 103 | Block->setCloseName(CloseName, CloseNameLocBegin); |
414 | 103 | Block->setLines(Lines); |
415 | 103 | } |
416 | | |
417 | | VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin, |
418 | | unsigned CommandID, |
419 | | SourceLocation TextBegin, |
420 | 111 | StringRef Text) { |
421 | 111 | VerbatimLineComment *VL = new (Allocator) VerbatimLineComment( |
422 | 111 | LocBegin, |
423 | 111 | TextBegin.getLocWithOffset(Text.size()), |
424 | 111 | CommandID, |
425 | 111 | TextBegin, |
426 | 111 | Text); |
427 | 111 | checkFunctionDeclVerbatimLine(VL); |
428 | 111 | checkContainerDeclVerbatimLine(VL); |
429 | 111 | return VL; |
430 | 111 | } |
431 | | |
432 | | HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin, |
433 | 147 | StringRef TagName) { |
434 | 147 | return new (Allocator) HTMLStartTagComment(LocBegin, TagName); |
435 | 147 | } |
436 | | |
437 | | void Sema::actOnHTMLStartTagFinish( |
438 | | HTMLStartTagComment *Tag, |
439 | | ArrayRef<HTMLStartTagComment::Attribute> Attrs, |
440 | | SourceLocation GreaterLoc, |
441 | 147 | bool IsSelfClosing) { |
442 | 147 | Tag->setAttrs(Attrs); |
443 | 147 | Tag->setGreaterLoc(GreaterLoc); |
444 | 147 | if (IsSelfClosing) |
445 | 12 | Tag->setSelfClosing(); |
446 | 135 | else if (!isHTMLEndTagForbidden(Tag->getTagName())) |
447 | 121 | HTMLOpenTags.push_back(Tag); |
448 | 147 | } |
449 | | |
450 | | HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin, |
451 | | SourceLocation LocEnd, |
452 | 88 | StringRef TagName) { |
453 | 88 | HTMLEndTagComment *HET = |
454 | 88 | new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName); |
455 | 88 | if (isHTMLEndTagForbidden(TagName)) { |
456 | 3 | Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden) |
457 | 3 | << TagName << HET->getSourceRange(); |
458 | 3 | HET->setIsMalformed(); |
459 | 3 | return HET; |
460 | 3 | } |
461 | | |
462 | 85 | bool FoundOpen = false; |
463 | 85 | for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator |
464 | 85 | I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend(); |
465 | 99 | I != E; ++I14 ) { |
466 | 81 | if ((*I)->getTagName() == TagName) { |
467 | 67 | FoundOpen = true; |
468 | 67 | break; |
469 | 67 | } |
470 | 81 | } |
471 | 85 | if (!FoundOpen) { |
472 | 18 | Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced) |
473 | 18 | << HET->getSourceRange(); |
474 | 18 | HET->setIsMalformed(); |
475 | 18 | return HET; |
476 | 18 | } |
477 | | |
478 | 73 | while (67 !HTMLOpenTags.empty()) { |
479 | 73 | HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val(); |
480 | 73 | StringRef LastNotClosedTagName = HST->getTagName(); |
481 | 73 | if (LastNotClosedTagName == TagName) { |
482 | | // If the start tag is malformed, end tag is malformed as well. |
483 | 67 | if (HST->isMalformed()) |
484 | 0 | HET->setIsMalformed(); |
485 | 67 | break; |
486 | 67 | } |
487 | | |
488 | 6 | if (isHTMLEndTagOptional(LastNotClosedTagName)) |
489 | 0 | continue; |
490 | | |
491 | 6 | bool OpenLineInvalid; |
492 | 6 | const unsigned OpenLine = SourceMgr.getPresumedLineNumber( |
493 | 6 | HST->getLocation(), |
494 | 6 | &OpenLineInvalid); |
495 | 6 | bool CloseLineInvalid; |
496 | 6 | const unsigned CloseLine = SourceMgr.getPresumedLineNumber( |
497 | 6 | HET->getLocation(), |
498 | 6 | &CloseLineInvalid); |
499 | | |
500 | 6 | if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine) { |
501 | 6 | Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch) |
502 | 6 | << HST->getTagName() << HET->getTagName() |
503 | 6 | << HST->getSourceRange() << HET->getSourceRange(); |
504 | 6 | HST->setIsMalformed(); |
505 | 6 | } else { |
506 | 0 | Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch) |
507 | 0 | << HST->getTagName() << HET->getTagName() |
508 | 0 | << HST->getSourceRange(); |
509 | 0 | Diag(HET->getLocation(), diag::note_doc_html_end_tag) |
510 | 0 | << HET->getSourceRange(); |
511 | 0 | HST->setIsMalformed(); |
512 | 0 | } |
513 | 6 | } |
514 | | |
515 | 67 | return HET; |
516 | 85 | } |
517 | | |
518 | | FullComment *Sema::actOnFullComment( |
519 | 3.29k | ArrayRef<BlockContentComment *> Blocks) { |
520 | 3.29k | FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo); |
521 | 3.29k | resolveParamCommandIndexes(FC); |
522 | | |
523 | | // Complain about HTML tags that are not closed. |
524 | 3.34k | while (!HTMLOpenTags.empty()) { |
525 | 48 | HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val(); |
526 | 48 | if (isHTMLEndTagOptional(HST->getTagName())) |
527 | 0 | continue; |
528 | | |
529 | 48 | Diag(HST->getLocation(), diag::warn_doc_html_missing_end_tag) |
530 | 48 | << HST->getTagName() << HST->getSourceRange(); |
531 | 48 | HST->setIsMalformed(); |
532 | 48 | } |
533 | | |
534 | 3.29k | return FC; |
535 | 3.29k | } |
536 | | |
537 | 7.64k | void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) { |
538 | 7.64k | if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed) |
539 | 76 | return; |
540 | | |
541 | 7.56k | ParagraphComment *Paragraph = Command->getParagraph(); |
542 | 7.56k | if (Paragraph->isWhitespace()) { |
543 | 264 | SourceLocation DiagLoc; |
544 | 264 | if (Command->getNumArgs() > 0) |
545 | 39 | DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd(); |
546 | 264 | if (!DiagLoc.isValid()) |
547 | 225 | DiagLoc = Command->getCommandNameRange(Traits).getEnd(); |
548 | 264 | Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph) |
549 | 264 | << Command->getCommandMarker() |
550 | 264 | << Command->getCommandName(Traits) |
551 | 264 | << Command->getSourceRange(); |
552 | 264 | } |
553 | 7.56k | } |
554 | | |
555 | 3.93k | void Sema::checkReturnsCommand(const BlockCommandComment *Command) { |
556 | 3.93k | if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand) |
557 | 2.32k | return; |
558 | | |
559 | 1.60k | assert(ThisDeclInfo && "should not call this check on a bare comment"); |
560 | | |
561 | | // We allow the return command for all @properties because it can be used |
562 | | // to document the value that the property getter returns. |
563 | 1.60k | if (isObjCPropertyDecl()) |
564 | 8 | return; |
565 | 1.60k | if (involvesFunctionType()) { |
566 | 1.53k | assert(!ThisDeclInfo->ReturnType.isNull() && |
567 | 1.53k | "should have a valid return type"); |
568 | 1.53k | if (ThisDeclInfo->ReturnType->isVoidType()) { |
569 | 44 | unsigned DiagKind; |
570 | 44 | switch (ThisDeclInfo->CommentDecl->getKind()) { |
571 | 38 | default: |
572 | 38 | if (ThisDeclInfo->IsObjCMethod) |
573 | 2 | DiagKind = 3; |
574 | 36 | else |
575 | 36 | DiagKind = 0; |
576 | 38 | break; |
577 | 3 | case Decl::CXXConstructor: |
578 | 3 | DiagKind = 1; |
579 | 3 | break; |
580 | 3 | case Decl::CXXDestructor: |
581 | 3 | DiagKind = 2; |
582 | 3 | break; |
583 | 44 | } |
584 | 44 | Diag(Command->getLocation(), |
585 | 44 | diag::warn_doc_returns_attached_to_a_void_function) |
586 | 44 | << Command->getCommandMarker() |
587 | 44 | << Command->getCommandName(Traits) |
588 | 44 | << DiagKind |
589 | 44 | << Command->getSourceRange(); |
590 | 44 | } |
591 | 1.53k | return; |
592 | 1.53k | } |
593 | | |
594 | 63 | Diag(Command->getLocation(), |
595 | 63 | diag::warn_doc_returns_not_attached_to_a_function_decl) |
596 | 63 | << Command->getCommandMarker() |
597 | 63 | << Command->getCommandName(Traits) |
598 | 63 | << Command->getSourceRange(); |
599 | 63 | } |
600 | | |
601 | 3.94k | void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) { |
602 | 3.94k | const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID()); |
603 | 3.94k | const BlockCommandComment *PrevCommand = nullptr; |
604 | 3.94k | if (Info->IsBriefCommand) { |
605 | 440 | if (!BriefCommand) { |
606 | 426 | BriefCommand = Command; |
607 | 426 | return; |
608 | 426 | } |
609 | 14 | PrevCommand = BriefCommand; |
610 | 3.50k | } else if (Info->IsHeaderfileCommand) { |
611 | 1.55k | if (!HeaderfileCommand) { |
612 | 1.55k | HeaderfileCommand = Command; |
613 | 1.55k | return; |
614 | 1.55k | } |
615 | 3 | PrevCommand = HeaderfileCommand; |
616 | 1.94k | } else { |
617 | | // We don't want to check this command for duplicates. |
618 | 1.94k | return; |
619 | 1.94k | } |
620 | 17 | StringRef CommandName = Command->getCommandName(Traits); |
621 | 17 | StringRef PrevCommandName = PrevCommand->getCommandName(Traits); |
622 | 17 | Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate) |
623 | 17 | << Command->getCommandMarker() |
624 | 17 | << CommandName |
625 | 17 | << Command->getSourceRange(); |
626 | 17 | if (CommandName == PrevCommandName) |
627 | 13 | Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous) |
628 | 13 | << PrevCommand->getCommandMarker() |
629 | 13 | << PrevCommandName |
630 | 13 | << PrevCommand->getSourceRange(); |
631 | 4 | else |
632 | 4 | Diag(PrevCommand->getLocation(), |
633 | 4 | diag::note_doc_block_command_previous_alias) |
634 | 4 | << PrevCommand->getCommandMarker() |
635 | 4 | << PrevCommandName |
636 | 4 | << CommandName; |
637 | 17 | } |
638 | | |
639 | 3.93k | void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) { |
640 | 3.93k | if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand) |
641 | 3.86k | return; |
642 | | |
643 | 74 | assert(ThisDeclInfo && "should not call this check on a bare comment"); |
644 | | |
645 | 0 | const Decl *D = ThisDeclInfo->CommentDecl; |
646 | 74 | if (!D) |
647 | 0 | return; |
648 | | |
649 | 74 | if (D->hasAttr<DeprecatedAttr>() || |
650 | 74 | D->hasAttr<AvailabilityAttr>()66 || |
651 | 74 | D->hasAttr<UnavailableAttr>()63 ) |
652 | 14 | return; |
653 | | |
654 | 60 | Diag(Command->getLocation(), diag::warn_doc_deprecated_not_sync) |
655 | 60 | << Command->getSourceRange() << Command->getCommandMarker(); |
656 | | |
657 | | // Try to emit a fixit with a deprecation attribute. |
658 | 60 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
659 | | // Don't emit a Fix-It for non-member function definitions. GCC does not |
660 | | // accept attributes on them. |
661 | 57 | const DeclContext *Ctx = FD->getDeclContext(); |
662 | 57 | if ((!Ctx || !Ctx->isRecord()) && |
663 | 57 | FD->doesThisDeclarationHaveABody()21 ) |
664 | 1 | return; |
665 | | |
666 | 56 | const LangOptions &LO = FD->getLangOpts(); |
667 | 56 | const bool DoubleSquareBracket = LO.CPlusPlus14 || LO.C2x13 ; |
668 | 56 | StringRef AttributeSpelling = |
669 | 56 | DoubleSquareBracket ? "[[deprecated]]"47 : "__attribute__((deprecated))"9 ; |
670 | 56 | if (PP) { |
671 | | // Try to find a replacement macro: |
672 | | // - In C2x/C++14 we prefer [[deprecated]]. |
673 | | // - If not found or an older C/C++ look for __attribute__((deprecated)). |
674 | 49 | StringRef MacroName; |
675 | 49 | if (DoubleSquareBracket) { |
676 | 40 | TokenValue Tokens[] = {tok::l_square, tok::l_square, |
677 | 40 | PP->getIdentifierInfo("deprecated"), |
678 | 40 | tok::r_square, tok::r_square}; |
679 | 40 | MacroName = PP->getLastMacroWithSpelling(FD->getLocation(), Tokens); |
680 | 40 | if (!MacroName.empty()) |
681 | 3 | AttributeSpelling = MacroName; |
682 | 40 | } |
683 | | |
684 | 49 | if (MacroName.empty()) { |
685 | 46 | TokenValue Tokens[] = { |
686 | 46 | tok::kw___attribute, tok::l_paren, |
687 | 46 | tok::l_paren, PP->getIdentifierInfo("deprecated"), |
688 | 46 | tok::r_paren, tok::r_paren}; |
689 | 46 | StringRef MacroName = |
690 | 46 | PP->getLastMacroWithSpelling(FD->getLocation(), Tokens); |
691 | 46 | if (!MacroName.empty()) |
692 | 4 | AttributeSpelling = MacroName; |
693 | 46 | } |
694 | 49 | } |
695 | | |
696 | 56 | SmallString<64> TextToInsert = AttributeSpelling; |
697 | 56 | TextToInsert += " "; |
698 | 56 | SourceLocation Loc = FD->getSourceRange().getBegin(); |
699 | 56 | Diag(Loc, diag::note_add_deprecation_attr) |
700 | 56 | << FixItHint::CreateInsertion(Loc, TextToInsert); |
701 | 56 | } |
702 | 60 | } |
703 | | |
704 | 3.29k | void Sema::resolveParamCommandIndexes(const FullComment *FC) { |
705 | 3.29k | if (!involvesFunctionType()) { |
706 | | // We already warned that \\param commands are not attached to a function |
707 | | // decl. |
708 | 737 | return; |
709 | 737 | } |
710 | | |
711 | 2.55k | SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands; |
712 | | |
713 | | // Comment AST nodes that correspond to \c ParamVars for which we have |
714 | | // found a \\param command or NULL if no documentation was found so far. |
715 | 2.55k | SmallVector<ParamCommandComment *, 8> ParamVarDocs; |
716 | | |
717 | 2.55k | ArrayRef<const ParmVarDecl *> ParamVars = getParamVars(); |
718 | 2.55k | ParamVarDocs.resize(ParamVars.size(), nullptr); |
719 | | |
720 | | // First pass over all \\param commands: resolve all parameter names. |
721 | 2.55k | for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end(); |
722 | 17.3k | I != E; ++I14.7k ) { |
723 | 14.7k | ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I); |
724 | 14.7k | if (!PCC || !PCC->hasParamName()3.44k ) |
725 | 11.3k | continue; |
726 | 3.43k | StringRef ParamName = PCC->getParamNameAsWritten(); |
727 | | |
728 | | // Check that referenced parameter name is in the function decl. |
729 | 3.43k | const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName, |
730 | 3.43k | ParamVars); |
731 | 3.43k | if (ResolvedParamIndex == ParamCommandComment::VarArgParamIndex) { |
732 | 38 | PCC->setIsVarArgParam(); |
733 | 38 | continue; |
734 | 38 | } |
735 | 3.39k | if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) { |
736 | 147 | UnresolvedParamCommands.push_back(PCC); |
737 | 147 | continue; |
738 | 147 | } |
739 | 3.24k | PCC->setParamIndex(ResolvedParamIndex); |
740 | 3.24k | if (ParamVarDocs[ResolvedParamIndex]) { |
741 | 6 | SourceRange ArgRange = PCC->getParamNameRange(); |
742 | 6 | Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate) |
743 | 6 | << ParamName << ArgRange; |
744 | 6 | ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex]; |
745 | 6 | Diag(PrevCommand->getLocation(), diag::note_doc_param_previous) |
746 | 6 | << PrevCommand->getParamNameRange(); |
747 | 6 | } |
748 | 3.24k | ParamVarDocs[ResolvedParamIndex] = PCC; |
749 | 3.24k | } |
750 | | |
751 | | // Find parameter declarations that have no corresponding \\param. |
752 | 2.55k | SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls; |
753 | 6.50k | for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i3.94k ) { |
754 | 3.94k | if (!ParamVarDocs[i]) |
755 | 707 | OrphanedParamDecls.push_back(ParamVars[i]); |
756 | 3.94k | } |
757 | | |
758 | | // Second pass over unresolved \\param commands: do typo correction. |
759 | | // Suggest corrections from a set of parameter declarations that have no |
760 | | // corresponding \\param. |
761 | 2.70k | for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i147 ) { |
762 | 147 | const ParamCommandComment *PCC = UnresolvedParamCommands[i]; |
763 | | |
764 | 147 | SourceRange ArgRange = PCC->getParamNameRange(); |
765 | 147 | StringRef ParamName = PCC->getParamNameAsWritten(); |
766 | 147 | Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found) |
767 | 147 | << ParamName << ArgRange; |
768 | | |
769 | | // All parameters documented -- can't suggest a correction. |
770 | 147 | if (OrphanedParamDecls.size() == 0) |
771 | 39 | continue; |
772 | | |
773 | 108 | unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex; |
774 | 108 | if (OrphanedParamDecls.size() == 1) { |
775 | | // If one parameter is not documented then that parameter is the only |
776 | | // possible suggestion. |
777 | 91 | CorrectedParamIndex = 0; |
778 | 91 | } else { |
779 | | // Do typo correction. |
780 | 17 | CorrectedParamIndex = correctTypoInParmVarReference(ParamName, |
781 | 17 | OrphanedParamDecls); |
782 | 17 | } |
783 | 108 | if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) { |
784 | 96 | const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex]; |
785 | 96 | if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier()) |
786 | 92 | Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion) |
787 | 92 | << CorrectedII->getName() |
788 | 92 | << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName()); |
789 | 96 | } |
790 | 108 | } |
791 | 2.55k | } |
792 | | |
793 | 8.38k | bool Sema::involvesFunctionType() { |
794 | 8.38k | if (!ThisDeclInfo) |
795 | 102 | return false; |
796 | 8.28k | if (!ThisDeclInfo->IsFilled) |
797 | 2.83k | inspectThisDecl(); |
798 | 8.28k | return ThisDeclInfo->involvesFunctionType(); |
799 | 8.38k | } |
800 | | |
801 | 19 | bool Sema::isFunctionDecl() { |
802 | 19 | if (!ThisDeclInfo) |
803 | 0 | return false; |
804 | 19 | if (!ThisDeclInfo->IsFilled) |
805 | 15 | inspectThisDecl(); |
806 | 19 | return ThisDeclInfo->getKind() == DeclInfo::FunctionKind; |
807 | 19 | } |
808 | | |
809 | 11 | bool Sema::isAnyFunctionDecl() { |
810 | 11 | return isFunctionDecl() && ThisDeclInfo->CurrentDecl8 && |
811 | 11 | isa<FunctionDecl>(ThisDeclInfo->CurrentDecl)8 ; |
812 | 11 | } |
813 | | |
814 | 50 | bool Sema::isFunctionOrMethodVariadic() { |
815 | 50 | if (!ThisDeclInfo) |
816 | 0 | return false; |
817 | 50 | if (!ThisDeclInfo->IsFilled) |
818 | 0 | inspectThisDecl(); |
819 | 50 | return ThisDeclInfo->IsVariadic; |
820 | 50 | } |
821 | | |
822 | 8 | bool Sema::isObjCMethodDecl() { |
823 | 8 | return isFunctionDecl() && ThisDeclInfo->CurrentDecl2 && |
824 | 8 | isa<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl)2 ; |
825 | 8 | } |
826 | | |
827 | 3 | bool Sema::isFunctionPointerVarDecl() { |
828 | 3 | if (!ThisDeclInfo) |
829 | 0 | return false; |
830 | 3 | if (!ThisDeclInfo->IsFilled) |
831 | 3 | inspectThisDecl(); |
832 | 3 | if (ThisDeclInfo->getKind() == DeclInfo::VariableKind) { |
833 | 0 | if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDeclInfo->CurrentDecl)) { |
834 | 0 | QualType QT = VD->getType(); |
835 | 0 | return QT->isFunctionPointerType(); |
836 | 0 | } |
837 | 0 | } |
838 | 3 | return false; |
839 | 3 | } |
840 | | |
841 | 1.60k | bool Sema::isObjCPropertyDecl() { |
842 | 1.60k | if (!ThisDeclInfo) |
843 | 0 | return false; |
844 | 1.60k | if (!ThisDeclInfo->IsFilled) |
845 | 182 | inspectThisDecl(); |
846 | 1.60k | return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty; |
847 | 1.60k | } |
848 | | |
849 | 406 | bool Sema::isTemplateOrSpecialization() { |
850 | 406 | if (!ThisDeclInfo) |
851 | 9 | return false; |
852 | 397 | if (!ThisDeclInfo->IsFilled) |
853 | 137 | inspectThisDecl(); |
854 | 397 | return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate; |
855 | 406 | } |
856 | | |
857 | 4 | bool Sema::isRecordLikeDecl() { |
858 | 4 | if (!ThisDeclInfo) |
859 | 0 | return false; |
860 | 4 | if (!ThisDeclInfo->IsFilled) |
861 | 0 | inspectThisDecl(); |
862 | 4 | return isUnionDecl() || isClassOrStructDecl() || isObjCInterfaceDecl() || |
863 | 4 | isObjCProtocolDecl(); |
864 | 4 | } |
865 | | |
866 | 10 | bool Sema::isUnionDecl() { |
867 | 10 | if (!ThisDeclInfo) |
868 | 0 | return false; |
869 | 10 | if (!ThisDeclInfo->IsFilled) |
870 | 6 | inspectThisDecl(); |
871 | 10 | if (const RecordDecl *RD = |
872 | 10 | dyn_cast_or_null<RecordDecl>(ThisDeclInfo->CurrentDecl)) |
873 | 6 | return RD->isUnion(); |
874 | 4 | return false; |
875 | 10 | } |
876 | 35 | static bool isClassOrStructDeclImpl(const Decl *D) { |
877 | 35 | if (auto *record = dyn_cast_or_null<RecordDecl>(D)) |
878 | 20 | return !record->isUnion(); |
879 | | |
880 | 15 | return false; |
881 | 35 | } |
882 | | |
883 | 4 | bool Sema::isClassOrStructDecl() { |
884 | 4 | if (!ThisDeclInfo) |
885 | 0 | return false; |
886 | 4 | if (!ThisDeclInfo->IsFilled) |
887 | 0 | inspectThisDecl(); |
888 | | |
889 | 4 | if (!ThisDeclInfo->CurrentDecl) |
890 | 0 | return false; |
891 | | |
892 | 4 | return isClassOrStructDeclImpl(ThisDeclInfo->CurrentDecl); |
893 | 4 | } |
894 | | |
895 | 29 | bool Sema::isClassOrStructOrTagTypedefDecl() { |
896 | 29 | if (!ThisDeclInfo) |
897 | 0 | return false; |
898 | 29 | if (!ThisDeclInfo->IsFilled) |
899 | 27 | inspectThisDecl(); |
900 | | |
901 | 29 | if (!ThisDeclInfo->CurrentDecl) |
902 | 0 | return false; |
903 | | |
904 | 29 | if (isClassOrStructDeclImpl(ThisDeclInfo->CurrentDecl)) |
905 | 15 | return true; |
906 | | |
907 | 14 | if (auto *ThisTypedefDecl = dyn_cast<TypedefDecl>(ThisDeclInfo->CurrentDecl)) { |
908 | 2 | auto UnderlyingType = ThisTypedefDecl->getUnderlyingType(); |
909 | 2 | if (auto ThisElaboratedType = dyn_cast<ElaboratedType>(UnderlyingType)) { |
910 | 2 | auto DesugaredType = ThisElaboratedType->desugar(); |
911 | 2 | if (auto *DesugaredTypePtr = DesugaredType.getTypePtrOrNull()) { |
912 | 2 | if (auto *ThisRecordType = dyn_cast<RecordType>(DesugaredTypePtr)) { |
913 | 2 | return isClassOrStructDeclImpl(ThisRecordType->getAsRecordDecl()); |
914 | 2 | } |
915 | 2 | } |
916 | 2 | } |
917 | 2 | } |
918 | | |
919 | 12 | return false; |
920 | 14 | } |
921 | | |
922 | 7 | bool Sema::isClassTemplateDecl() { |
923 | 7 | if (!ThisDeclInfo) |
924 | 0 | return false; |
925 | 7 | if (!ThisDeclInfo->IsFilled) |
926 | 0 | inspectThisDecl(); |
927 | 7 | return ThisDeclInfo->CurrentDecl && |
928 | 7 | (isa<ClassTemplateDecl>(ThisDeclInfo->CurrentDecl)); |
929 | 7 | } |
930 | | |
931 | 6 | bool Sema::isFunctionTemplateDecl() { |
932 | 6 | if (!ThisDeclInfo) |
933 | 0 | return false; |
934 | 6 | if (!ThisDeclInfo->IsFilled) |
935 | 0 | inspectThisDecl(); |
936 | 6 | return ThisDeclInfo->CurrentDecl && |
937 | 6 | (isa<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl)); |
938 | 6 | } |
939 | | |
940 | 17 | bool Sema::isObjCInterfaceDecl() { |
941 | 17 | if (!ThisDeclInfo) |
942 | 0 | return false; |
943 | 17 | if (!ThisDeclInfo->IsFilled) |
944 | 7 | inspectThisDecl(); |
945 | 17 | return ThisDeclInfo->CurrentDecl && |
946 | 17 | isa<ObjCInterfaceDecl>(ThisDeclInfo->CurrentDecl); |
947 | 17 | } |
948 | | |
949 | 10 | bool Sema::isObjCProtocolDecl() { |
950 | 10 | if (!ThisDeclInfo) |
951 | 0 | return false; |
952 | 10 | if (!ThisDeclInfo->IsFilled) |
953 | 6 | inspectThisDecl(); |
954 | 10 | return ThisDeclInfo->CurrentDecl && |
955 | 10 | isa<ObjCProtocolDecl>(ThisDeclInfo->CurrentDecl); |
956 | 10 | } |
957 | | |
958 | 2.55k | ArrayRef<const ParmVarDecl *> Sema::getParamVars() { |
959 | 2.55k | if (!ThisDeclInfo->IsFilled) |
960 | 0 | inspectThisDecl(); |
961 | 2.55k | return ThisDeclInfo->ParamVars; |
962 | 2.55k | } |
963 | | |
964 | 3.21k | void Sema::inspectThisDecl() { |
965 | 3.21k | ThisDeclInfo->fill(); |
966 | 3.21k | } |
967 | | |
968 | | unsigned Sema::resolveParmVarReference(StringRef Name, |
969 | 3.43k | ArrayRef<const ParmVarDecl *> ParamVars) { |
970 | 8.48k | for (unsigned i = 0, e = ParamVars.size(); i != e; ++i5.05k ) { |
971 | 8.30k | const IdentifierInfo *II = ParamVars[i]->getIdentifier(); |
972 | 8.30k | if (II && II->getName() == Name8.29k ) |
973 | 3.24k | return i; |
974 | 8.30k | } |
975 | 185 | if (Name == "..." && isFunctionOrMethodVariadic()50 ) |
976 | 38 | return ParamCommandComment::VarArgParamIndex; |
977 | 147 | return ParamCommandComment::InvalidParamIndex; |
978 | 185 | } |
979 | | |
980 | | namespace { |
981 | | class SimpleTypoCorrector { |
982 | | const NamedDecl *BestDecl; |
983 | | |
984 | | StringRef Typo; |
985 | | const unsigned MaxEditDistance; |
986 | | |
987 | | unsigned BestEditDistance; |
988 | | unsigned BestIndex; |
989 | | unsigned NextIndex; |
990 | | |
991 | | public: |
992 | | explicit SimpleTypoCorrector(StringRef Typo) |
993 | | : BestDecl(nullptr), Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3), |
994 | 28 | BestEditDistance(MaxEditDistance + 1), BestIndex(0), NextIndex(0) {} |
995 | | |
996 | | void addDecl(const NamedDecl *ND); |
997 | | |
998 | 33 | const NamedDecl *getBestDecl() const { |
999 | 33 | if (BestEditDistance > MaxEditDistance) |
1000 | 16 | return nullptr; |
1001 | | |
1002 | 17 | return BestDecl; |
1003 | 33 | } |
1004 | | |
1005 | 5 | unsigned getBestDeclIndex() const { |
1006 | 5 | assert(getBestDecl()); |
1007 | 0 | return BestIndex; |
1008 | 5 | } |
1009 | | }; |
1010 | | |
1011 | 59 | void SimpleTypoCorrector::addDecl(const NamedDecl *ND) { |
1012 | 59 | unsigned CurrIndex = NextIndex++; |
1013 | | |
1014 | 59 | const IdentifierInfo *II = ND->getIdentifier(); |
1015 | 59 | if (!II) |
1016 | 0 | return; |
1017 | | |
1018 | 59 | StringRef Name = II->getName(); |
1019 | 59 | unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size()); |
1020 | 59 | if (MinPossibleEditDistance > 0 && |
1021 | 59 | Typo.size() / MinPossibleEditDistance < 320 ) |
1022 | 15 | return; |
1023 | | |
1024 | 44 | unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance); |
1025 | 44 | if (EditDistance < BestEditDistance) { |
1026 | 12 | BestEditDistance = EditDistance; |
1027 | 12 | BestDecl = ND; |
1028 | 12 | BestIndex = CurrIndex; |
1029 | 12 | } |
1030 | 44 | } |
1031 | | } // end anonymous namespace |
1032 | | |
1033 | | unsigned Sema::correctTypoInParmVarReference( |
1034 | | StringRef Typo, |
1035 | 17 | ArrayRef<const ParmVarDecl *> ParamVars) { |
1036 | 17 | SimpleTypoCorrector Corrector(Typo); |
1037 | 51 | for (unsigned i = 0, e = ParamVars.size(); i != e; ++i34 ) |
1038 | 34 | Corrector.addDecl(ParamVars[i]); |
1039 | 17 | if (Corrector.getBestDecl()) |
1040 | 5 | return Corrector.getBestDeclIndex(); |
1041 | 12 | else |
1042 | 12 | return ParamCommandComment::InvalidParamIndex; |
1043 | 17 | } |
1044 | | |
1045 | | namespace { |
1046 | | bool ResolveTParamReferenceHelper( |
1047 | | StringRef Name, |
1048 | | const TemplateParameterList *TemplateParameters, |
1049 | 202 | SmallVectorImpl<unsigned> *Position) { |
1050 | 291 | for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i89 ) { |
1051 | 240 | const NamedDecl *Param = TemplateParameters->getParam(i); |
1052 | 240 | const IdentifierInfo *II = Param->getIdentifier(); |
1053 | 240 | if (II && II->getName() == Name) { |
1054 | 121 | Position->push_back(i); |
1055 | 121 | return true; |
1056 | 121 | } |
1057 | | |
1058 | 119 | if (const TemplateTemplateParmDecl *TTP = |
1059 | 119 | dyn_cast<TemplateTemplateParmDecl>(Param)) { |
1060 | 33 | Position->push_back(i); |
1061 | 33 | if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(), |
1062 | 33 | Position)) |
1063 | 30 | return true; |
1064 | 3 | Position->pop_back(); |
1065 | 3 | } |
1066 | 119 | } |
1067 | 51 | return false; |
1068 | 202 | } |
1069 | | } // end anonymous namespace |
1070 | | |
1071 | | bool Sema::resolveTParamReference( |
1072 | | StringRef Name, |
1073 | | const TemplateParameterList *TemplateParameters, |
1074 | 172 | SmallVectorImpl<unsigned> *Position) { |
1075 | 172 | Position->clear(); |
1076 | 172 | if (!TemplateParameters) |
1077 | 3 | return false; |
1078 | | |
1079 | 169 | return ResolveTParamReferenceHelper(Name, TemplateParameters, Position); |
1080 | 172 | } |
1081 | | |
1082 | | namespace { |
1083 | | void CorrectTypoInTParamReferenceHelper( |
1084 | | const TemplateParameterList *TemplateParameters, |
1085 | 11 | SimpleTypoCorrector &Corrector) { |
1086 | 36 | for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i25 ) { |
1087 | 25 | const NamedDecl *Param = TemplateParameters->getParam(i); |
1088 | 25 | Corrector.addDecl(Param); |
1089 | | |
1090 | 25 | if (const TemplateTemplateParmDecl *TTP = |
1091 | 25 | dyn_cast<TemplateTemplateParmDecl>(Param)) |
1092 | 0 | CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(), |
1093 | 0 | Corrector); |
1094 | 25 | } |
1095 | 11 | } |
1096 | | } // end anonymous namespace |
1097 | | |
1098 | | StringRef Sema::correctTypoInTParamReference( |
1099 | | StringRef Typo, |
1100 | 11 | const TemplateParameterList *TemplateParameters) { |
1101 | 11 | SimpleTypoCorrector Corrector(Typo); |
1102 | 11 | CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector); |
1103 | 11 | if (const NamedDecl *ND = Corrector.getBestDecl()) { |
1104 | 7 | const IdentifierInfo *II = ND->getIdentifier(); |
1105 | 7 | assert(II && "SimpleTypoCorrector should not return this decl"); |
1106 | 0 | return II->getName(); |
1107 | 7 | } |
1108 | 4 | return StringRef(); |
1109 | 11 | } |
1110 | | |
1111 | | InlineCommandComment::RenderKind |
1112 | 1.30k | Sema::getInlineCommandRenderKind(StringRef Name) const { |
1113 | 1.30k | assert(Traits.getCommandInfo(Name)->IsInlineCommand); |
1114 | | |
1115 | 0 | return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name) |
1116 | 1.30k | .Case("b", InlineCommandComment::RenderBold) |
1117 | 1.30k | .Cases("c", "p", InlineCommandComment::RenderMonospaced) |
1118 | 1.30k | .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized) |
1119 | 1.30k | .Case("anchor", InlineCommandComment::RenderAnchor) |
1120 | 1.30k | .Default(InlineCommandComment::RenderNormal); |
1121 | 1.30k | } |
1122 | | |
1123 | | } // end namespace comments |
1124 | | } // end namespace clang |