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