1 //===--- CommentSema.cpp - Doxygen comment semantic analysis --------------===//
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
7 //===----------------------------------------------------------------------===//
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/StringSwitch.h"
24 #include "clang/AST/CommentHTMLTagsProperties.inc"
25 } // end anonymous namespace
27 Sema::Sema(llvm::BumpPtrAllocator
&Allocator
, const SourceManager
&SourceMgr
,
28 DiagnosticsEngine
&Diags
, CommandTraits
&Traits
,
29 const Preprocessor
*PP
) :
30 Allocator(Allocator
), SourceMgr(SourceMgr
), Diags(Diags
), Traits(Traits
),
31 PP(PP
), ThisDeclInfo(nullptr), BriefCommand(nullptr),
32 HeaderfileCommand(nullptr) {
35 void Sema::setDecl(const Decl
*D
) {
39 ThisDeclInfo
= new (Allocator
) DeclInfo
;
40 ThisDeclInfo
->CommentDecl
= D
;
41 ThisDeclInfo
->IsFilled
= false;
44 ParagraphComment
*Sema::actOnParagraphComment(
45 ArrayRef
<InlineContentComment
*> Content
) {
46 return new (Allocator
) ParagraphComment(Content
);
49 BlockCommandComment
*Sema::actOnBlockCommandStart(
50 SourceLocation LocBegin
,
51 SourceLocation LocEnd
,
53 CommandMarkerKind CommandMarker
) {
54 BlockCommandComment
*BC
= new (Allocator
) BlockCommandComment(LocBegin
, LocEnd
,
57 checkContainerDecl(BC
);
61 void Sema::actOnBlockCommandArgs(BlockCommandComment
*Command
,
62 ArrayRef
<BlockCommandComment::Argument
> Args
) {
63 Command
->setArgs(Args
);
66 void Sema::actOnBlockCommandFinish(BlockCommandComment
*Command
,
67 ParagraphComment
*Paragraph
) {
68 Command
->setParagraph(Paragraph
);
69 checkBlockCommandEmptyParagraph(Command
);
70 checkBlockCommandDuplicate(Command
);
72 // These checks only make sense if the comment is attached to a
74 checkReturnsCommand(Command
);
75 checkDeprecatedCommand(Command
);
79 ParamCommandComment
*Sema::actOnParamCommandStart(
80 SourceLocation LocBegin
,
81 SourceLocation LocEnd
,
83 CommandMarkerKind CommandMarker
) {
84 ParamCommandComment
*Command
=
85 new (Allocator
) ParamCommandComment(LocBegin
, LocEnd
, CommandID
,
88 if (!involvesFunctionType())
89 Diag(Command
->getLocation(),
90 diag::warn_doc_param_not_attached_to_a_function_decl
)
92 << Command
->getCommandNameRange(Traits
);
97 void Sema::checkFunctionDeclVerbatimLine(const BlockCommandComment
*Comment
) {
98 const CommandInfo
*Info
= Traits
.getCommandInfo(Comment
->getCommandID());
99 if (!Info
->IsFunctionDeclarationCommand
)
103 switch (Comment
->getCommandID()) {
104 case CommandTraits::KCI_function
:
105 DiagSelect
= (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 1 : 0;
107 case CommandTraits::KCI_functiongroup
:
108 DiagSelect
= (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 2 : 0;
110 case CommandTraits::KCI_method
:
111 DiagSelect
= !isObjCMethodDecl() ? 3 : 0;
113 case CommandTraits::KCI_methodgroup
:
114 DiagSelect
= !isObjCMethodDecl() ? 4 : 0;
116 case CommandTraits::KCI_callback
:
117 DiagSelect
= !isFunctionPointerVarDecl() ? 5 : 0;
124 Diag(Comment
->getLocation(), diag::warn_doc_function_method_decl_mismatch
)
125 << Comment
->getCommandMarker()
126 << (DiagSelect
-1) << (DiagSelect
-1)
127 << Comment
->getSourceRange();
130 void Sema::checkContainerDeclVerbatimLine(const BlockCommandComment
*Comment
) {
131 const CommandInfo
*Info
= Traits
.getCommandInfo(Comment
->getCommandID());
132 if (!Info
->IsRecordLikeDeclarationCommand
)
135 switch (Comment
->getCommandID()) {
136 case CommandTraits::KCI_class
:
138 (!isClassOrStructOrTagTypedefDecl() && !isClassTemplateDecl()) ? 1
140 // Allow @class command on @interface declarations.
141 // FIXME. Currently, \class and @class are indistinguishable. So,
142 // \class is also allowed on an @interface declaration
143 if (DiagSelect
&& Comment
->getCommandMarker() && isObjCInterfaceDecl())
146 case CommandTraits::KCI_interface
:
147 DiagSelect
= !isObjCInterfaceDecl() ? 2 : 0;
149 case CommandTraits::KCI_protocol
:
150 DiagSelect
= !isObjCProtocolDecl() ? 3 : 0;
152 case CommandTraits::KCI_struct
:
153 DiagSelect
= !isClassOrStructOrTagTypedefDecl() ? 4 : 0;
155 case CommandTraits::KCI_union
:
156 DiagSelect
= !isUnionDecl() ? 5 : 0;
163 Diag(Comment
->getLocation(), diag::warn_doc_api_container_decl_mismatch
)
164 << Comment
->getCommandMarker()
165 << (DiagSelect
-1) << (DiagSelect
-1)
166 << Comment
->getSourceRange();
169 void Sema::checkContainerDecl(const BlockCommandComment
*Comment
) {
170 const CommandInfo
*Info
= Traits
.getCommandInfo(Comment
->getCommandID());
171 if (!Info
->IsRecordLikeDetailCommand
|| isRecordLikeDecl())
174 switch (Comment
->getCommandID()) {
175 case CommandTraits::KCI_classdesign
:
178 case CommandTraits::KCI_coclass
:
181 case CommandTraits::KCI_dependency
:
184 case CommandTraits::KCI_helper
:
187 case CommandTraits::KCI_helperclass
:
190 case CommandTraits::KCI_helps
:
193 case CommandTraits::KCI_instancesize
:
196 case CommandTraits::KCI_ownership
:
199 case CommandTraits::KCI_performance
:
202 case CommandTraits::KCI_security
:
205 case CommandTraits::KCI_superclass
:
213 Diag(Comment
->getLocation(), diag::warn_doc_container_decl_mismatch
)
214 << Comment
->getCommandMarker()
216 << Comment
->getSourceRange();
219 /// Turn a string into the corresponding PassDirection or -1 if it's not
221 static ParamCommandPassDirection
getParamPassDirection(StringRef Arg
) {
222 return llvm::StringSwitch
<ParamCommandPassDirection
>(Arg
)
223 .Case("[in]", ParamCommandPassDirection::In
)
224 .Case("[out]", ParamCommandPassDirection::Out
)
225 .Cases("[in,out]", "[out,in]", ParamCommandPassDirection::InOut
)
226 .Default(static_cast<ParamCommandPassDirection
>(-1));
229 void Sema::actOnParamCommandDirectionArg(ParamCommandComment
*Command
,
230 SourceLocation ArgLocBegin
,
231 SourceLocation ArgLocEnd
,
233 std::string ArgLower
= Arg
.lower();
234 ParamCommandPassDirection Direction
= getParamPassDirection(ArgLower
);
236 if (Direction
== static_cast<ParamCommandPassDirection
>(-1)) {
237 // Try again with whitespace removed.
238 llvm::erase_if(ArgLower
, clang::isWhitespace
);
239 Direction
= getParamPassDirection(ArgLower
);
241 SourceRange
ArgRange(ArgLocBegin
, ArgLocEnd
);
242 if (Direction
!= static_cast<ParamCommandPassDirection
>(-1)) {
243 const char *FixedName
=
244 ParamCommandComment::getDirectionAsString(Direction
);
245 Diag(ArgLocBegin
, diag::warn_doc_param_spaces_in_direction
)
246 << ArgRange
<< FixItHint::CreateReplacement(ArgRange
, FixedName
);
248 Diag(ArgLocBegin
, diag::warn_doc_param_invalid_direction
) << ArgRange
;
249 Direction
= ParamCommandPassDirection::In
; // Sane fall back.
252 Command
->setDirection(Direction
,
256 void Sema::actOnParamCommandParamNameArg(ParamCommandComment
*Command
,
257 SourceLocation ArgLocBegin
,
258 SourceLocation ArgLocEnd
,
260 // Parser will not feed us more arguments than needed.
261 assert(Command
->getNumArgs() == 0);
263 if (!Command
->isDirectionExplicit()) {
264 // User didn't provide a direction argument.
265 Command
->setDirection(ParamCommandPassDirection::In
,
266 /* Explicit = */ false);
268 auto *A
= new (Allocator
)
269 Comment::Argument
{SourceRange(ArgLocBegin
, ArgLocEnd
), Arg
};
270 Command
->setArgs(llvm::ArrayRef(A
, 1));
273 void Sema::actOnParamCommandFinish(ParamCommandComment
*Command
,
274 ParagraphComment
*Paragraph
) {
275 Command
->setParagraph(Paragraph
);
276 checkBlockCommandEmptyParagraph(Command
);
279 TParamCommandComment
*Sema::actOnTParamCommandStart(
280 SourceLocation LocBegin
,
281 SourceLocation LocEnd
,
283 CommandMarkerKind CommandMarker
) {
284 TParamCommandComment
*Command
=
285 new (Allocator
) TParamCommandComment(LocBegin
, LocEnd
, CommandID
,
288 if (!isTemplateOrSpecialization())
289 Diag(Command
->getLocation(),
290 diag::warn_doc_tparam_not_attached_to_a_template_decl
)
292 << Command
->getCommandNameRange(Traits
);
297 void Sema::actOnTParamCommandParamNameArg(TParamCommandComment
*Command
,
298 SourceLocation ArgLocBegin
,
299 SourceLocation ArgLocEnd
,
301 // Parser will not feed us more arguments than needed.
302 assert(Command
->getNumArgs() == 0);
304 auto *A
= new (Allocator
)
305 Comment::Argument
{SourceRange(ArgLocBegin
, ArgLocEnd
), Arg
};
306 Command
->setArgs(llvm::ArrayRef(A
, 1));
308 if (!isTemplateOrSpecialization()) {
309 // We already warned that this \\tparam is not attached to a template decl.
313 const TemplateParameterList
*TemplateParameters
=
314 ThisDeclInfo
->TemplateParameters
;
315 SmallVector
<unsigned, 2> Position
;
316 if (resolveTParamReference(Arg
, TemplateParameters
, &Position
)) {
317 Command
->setPosition(copyArray(llvm::ArrayRef(Position
)));
318 TParamCommandComment
*&PrevCommand
= TemplateParameterDocs
[Arg
];
320 SourceRange
ArgRange(ArgLocBegin
, ArgLocEnd
);
321 Diag(ArgLocBegin
, diag::warn_doc_tparam_duplicate
)
323 Diag(PrevCommand
->getLocation(), diag::note_doc_tparam_previous
)
324 << PrevCommand
->getParamNameRange();
326 PrevCommand
= Command
;
330 SourceRange
ArgRange(ArgLocBegin
, ArgLocEnd
);
331 Diag(ArgLocBegin
, diag::warn_doc_tparam_not_found
)
334 if (!TemplateParameters
|| TemplateParameters
->size() == 0)
337 StringRef CorrectedName
;
338 if (TemplateParameters
->size() == 1) {
339 const NamedDecl
*Param
= TemplateParameters
->getParam(0);
340 const IdentifierInfo
*II
= Param
->getIdentifier();
342 CorrectedName
= II
->getName();
344 CorrectedName
= correctTypoInTParamReference(Arg
, TemplateParameters
);
347 if (!CorrectedName
.empty()) {
348 Diag(ArgLocBegin
, diag::note_doc_tparam_name_suggestion
)
350 << FixItHint::CreateReplacement(ArgRange
, CorrectedName
);
354 void Sema::actOnTParamCommandFinish(TParamCommandComment
*Command
,
355 ParagraphComment
*Paragraph
) {
356 Command
->setParagraph(Paragraph
);
357 checkBlockCommandEmptyParagraph(Command
);
360 InlineCommandComment
*
361 Sema::actOnInlineCommand(SourceLocation CommandLocBegin
,
362 SourceLocation CommandLocEnd
, unsigned CommandID
,
363 ArrayRef
<Comment::Argument
> Args
) {
364 StringRef CommandName
= Traits
.getCommandInfo(CommandID
)->Name
;
366 return new (Allocator
)
367 InlineCommandComment(CommandLocBegin
, CommandLocEnd
, CommandID
,
368 getInlineCommandRenderKind(CommandName
), Args
);
371 InlineContentComment
*Sema::actOnUnknownCommand(SourceLocation LocBegin
,
372 SourceLocation LocEnd
,
373 StringRef CommandName
) {
374 unsigned CommandID
= Traits
.registerUnknownCommand(CommandName
)->getID();
375 return actOnUnknownCommand(LocBegin
, LocEnd
, CommandID
);
378 InlineContentComment
*Sema::actOnUnknownCommand(SourceLocation LocBegin
,
379 SourceLocation LocEnd
,
380 unsigned CommandID
) {
381 ArrayRef
<InlineCommandComment::Argument
> Args
;
382 return new (Allocator
) InlineCommandComment(
383 LocBegin
, LocEnd
, CommandID
, InlineCommandRenderKind::Normal
, Args
);
386 TextComment
*Sema::actOnText(SourceLocation LocBegin
,
387 SourceLocation LocEnd
,
389 return new (Allocator
) TextComment(LocBegin
, LocEnd
, Text
);
392 VerbatimBlockComment
*Sema::actOnVerbatimBlockStart(SourceLocation Loc
,
393 unsigned CommandID
) {
394 StringRef CommandName
= Traits
.getCommandInfo(CommandID
)->Name
;
395 return new (Allocator
) VerbatimBlockComment(
397 Loc
.getLocWithOffset(1 + CommandName
.size()),
401 VerbatimBlockLineComment
*Sema::actOnVerbatimBlockLine(SourceLocation Loc
,
403 return new (Allocator
) VerbatimBlockLineComment(Loc
, Text
);
406 void Sema::actOnVerbatimBlockFinish(
407 VerbatimBlockComment
*Block
,
408 SourceLocation CloseNameLocBegin
,
410 ArrayRef
<VerbatimBlockLineComment
*> Lines
) {
411 Block
->setCloseName(CloseName
, CloseNameLocBegin
);
412 Block
->setLines(Lines
);
415 VerbatimLineComment
*Sema::actOnVerbatimLine(SourceLocation LocBegin
,
417 SourceLocation TextBegin
,
419 VerbatimLineComment
*VL
= new (Allocator
) VerbatimLineComment(
421 TextBegin
.getLocWithOffset(Text
.size()),
425 checkFunctionDeclVerbatimLine(VL
);
426 checkContainerDeclVerbatimLine(VL
);
430 HTMLStartTagComment
*Sema::actOnHTMLStartTagStart(SourceLocation LocBegin
,
432 return new (Allocator
) HTMLStartTagComment(LocBegin
, TagName
);
435 void Sema::actOnHTMLStartTagFinish(
436 HTMLStartTagComment
*Tag
,
437 ArrayRef
<HTMLStartTagComment::Attribute
> Attrs
,
438 SourceLocation GreaterLoc
,
439 bool IsSelfClosing
) {
440 Tag
->setAttrs(Attrs
);
441 Tag
->setGreaterLoc(GreaterLoc
);
443 Tag
->setSelfClosing();
444 else if (!isHTMLEndTagForbidden(Tag
->getTagName()))
445 HTMLOpenTags
.push_back(Tag
);
448 HTMLEndTagComment
*Sema::actOnHTMLEndTag(SourceLocation LocBegin
,
449 SourceLocation LocEnd
,
451 HTMLEndTagComment
*HET
=
452 new (Allocator
) HTMLEndTagComment(LocBegin
, LocEnd
, TagName
);
453 if (isHTMLEndTagForbidden(TagName
)) {
454 Diag(HET
->getLocation(), diag::warn_doc_html_end_forbidden
)
455 << TagName
<< HET
->getSourceRange();
456 HET
->setIsMalformed();
460 bool FoundOpen
= false;
461 for (SmallVectorImpl
<HTMLStartTagComment
*>::const_reverse_iterator
462 I
= HTMLOpenTags
.rbegin(), E
= HTMLOpenTags
.rend();
464 if ((*I
)->getTagName() == TagName
) {
470 Diag(HET
->getLocation(), diag::warn_doc_html_end_unbalanced
)
471 << HET
->getSourceRange();
472 HET
->setIsMalformed();
476 while (!HTMLOpenTags
.empty()) {
477 HTMLStartTagComment
*HST
= HTMLOpenTags
.pop_back_val();
478 StringRef LastNotClosedTagName
= HST
->getTagName();
479 if (LastNotClosedTagName
== TagName
) {
480 // If the start tag is malformed, end tag is malformed as well.
481 if (HST
->isMalformed())
482 HET
->setIsMalformed();
486 if (isHTMLEndTagOptional(LastNotClosedTagName
))
489 bool OpenLineInvalid
;
490 const unsigned OpenLine
= SourceMgr
.getPresumedLineNumber(
493 bool CloseLineInvalid
;
494 const unsigned CloseLine
= SourceMgr
.getPresumedLineNumber(
498 if (OpenLineInvalid
|| CloseLineInvalid
|| OpenLine
== CloseLine
) {
499 Diag(HST
->getLocation(), diag::warn_doc_html_start_end_mismatch
)
500 << HST
->getTagName() << HET
->getTagName()
501 << HST
->getSourceRange() << HET
->getSourceRange();
502 HST
->setIsMalformed();
504 Diag(HST
->getLocation(), diag::warn_doc_html_start_end_mismatch
)
505 << HST
->getTagName() << HET
->getTagName()
506 << HST
->getSourceRange();
507 Diag(HET
->getLocation(), diag::note_doc_html_end_tag
)
508 << HET
->getSourceRange();
509 HST
->setIsMalformed();
516 FullComment
*Sema::actOnFullComment(
517 ArrayRef
<BlockContentComment
*> Blocks
) {
518 FullComment
*FC
= new (Allocator
) FullComment(Blocks
, ThisDeclInfo
);
519 resolveParamCommandIndexes(FC
);
521 // Complain about HTML tags that are not closed.
522 while (!HTMLOpenTags
.empty()) {
523 HTMLStartTagComment
*HST
= HTMLOpenTags
.pop_back_val();
524 if (isHTMLEndTagOptional(HST
->getTagName()))
527 Diag(HST
->getLocation(), diag::warn_doc_html_missing_end_tag
)
528 << HST
->getTagName() << HST
->getSourceRange();
529 HST
->setIsMalformed();
535 void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment
*Command
) {
536 if (Traits
.getCommandInfo(Command
->getCommandID())->IsEmptyParagraphAllowed
)
539 ParagraphComment
*Paragraph
= Command
->getParagraph();
540 if (Paragraph
->isWhitespace()) {
541 SourceLocation DiagLoc
;
542 if (Command
->getNumArgs() > 0)
543 DiagLoc
= Command
->getArgRange(Command
->getNumArgs() - 1).getEnd();
544 if (!DiagLoc
.isValid())
545 DiagLoc
= Command
->getCommandNameRange(Traits
).getEnd();
546 Diag(DiagLoc
, diag::warn_doc_block_command_empty_paragraph
)
547 << Command
->getCommandMarker()
548 << Command
->getCommandName(Traits
)
549 << Command
->getSourceRange();
553 void Sema::checkReturnsCommand(const BlockCommandComment
*Command
) {
554 if (!Traits
.getCommandInfo(Command
->getCommandID())->IsReturnsCommand
)
557 assert(ThisDeclInfo
&& "should not call this check on a bare comment");
559 // We allow the return command for all @properties because it can be used
560 // to document the value that the property getter returns.
561 if (isObjCPropertyDecl())
563 if (involvesFunctionType()) {
564 assert(!ThisDeclInfo
->ReturnType
.isNull() &&
565 "should have a valid return type");
566 if (ThisDeclInfo
->ReturnType
->isVoidType()) {
568 switch (ThisDeclInfo
->CommentDecl
->getKind()) {
570 if (ThisDeclInfo
->IsObjCMethod
)
575 case Decl::CXXConstructor
:
578 case Decl::CXXDestructor
:
582 Diag(Command
->getLocation(),
583 diag::warn_doc_returns_attached_to_a_void_function
)
584 << Command
->getCommandMarker()
585 << Command
->getCommandName(Traits
)
587 << Command
->getSourceRange();
592 Diag(Command
->getLocation(),
593 diag::warn_doc_returns_not_attached_to_a_function_decl
)
594 << Command
->getCommandMarker()
595 << Command
->getCommandName(Traits
)
596 << Command
->getSourceRange();
599 void Sema::checkBlockCommandDuplicate(const BlockCommandComment
*Command
) {
600 const CommandInfo
*Info
= Traits
.getCommandInfo(Command
->getCommandID());
601 const BlockCommandComment
*PrevCommand
= nullptr;
602 if (Info
->IsBriefCommand
) {
604 BriefCommand
= Command
;
607 PrevCommand
= BriefCommand
;
608 } else if (Info
->IsHeaderfileCommand
) {
609 if (!HeaderfileCommand
) {
610 HeaderfileCommand
= Command
;
613 PrevCommand
= HeaderfileCommand
;
615 // We don't want to check this command for duplicates.
618 StringRef CommandName
= Command
->getCommandName(Traits
);
619 StringRef PrevCommandName
= PrevCommand
->getCommandName(Traits
);
620 Diag(Command
->getLocation(), diag::warn_doc_block_command_duplicate
)
621 << Command
->getCommandMarker()
623 << Command
->getSourceRange();
624 if (CommandName
== PrevCommandName
)
625 Diag(PrevCommand
->getLocation(), diag::note_doc_block_command_previous
)
626 << PrevCommand
->getCommandMarker()
628 << PrevCommand
->getSourceRange();
630 Diag(PrevCommand
->getLocation(),
631 diag::note_doc_block_command_previous_alias
)
632 << PrevCommand
->getCommandMarker()
637 void Sema::checkDeprecatedCommand(const BlockCommandComment
*Command
) {
638 if (!Traits
.getCommandInfo(Command
->getCommandID())->IsDeprecatedCommand
)
641 assert(ThisDeclInfo
&& "should not call this check on a bare comment");
643 const Decl
*D
= ThisDeclInfo
->CommentDecl
;
647 if (D
->hasAttr
<DeprecatedAttr
>() ||
648 D
->hasAttr
<AvailabilityAttr
>() ||
649 D
->hasAttr
<UnavailableAttr
>())
652 Diag(Command
->getLocation(), diag::warn_doc_deprecated_not_sync
)
653 << Command
->getSourceRange() << Command
->getCommandMarker();
655 // Try to emit a fixit with a deprecation attribute.
656 if (const FunctionDecl
*FD
= dyn_cast
<FunctionDecl
>(D
)) {
657 // Don't emit a Fix-It for non-member function definitions. GCC does not
658 // accept attributes on them.
659 const DeclContext
*Ctx
= FD
->getDeclContext();
660 if ((!Ctx
|| !Ctx
->isRecord()) &&
661 FD
->doesThisDeclarationHaveABody())
664 const LangOptions
&LO
= FD
->getLangOpts();
665 const bool DoubleSquareBracket
= LO
.CPlusPlus14
|| LO
.C23
;
666 StringRef AttributeSpelling
=
667 DoubleSquareBracket
? "[[deprecated]]" : "__attribute__((deprecated))";
669 // Try to find a replacement macro:
670 // - In C23/C++14 we prefer [[deprecated]].
671 // - If not found or an older C/C++ look for __attribute__((deprecated)).
673 if (DoubleSquareBracket
) {
674 TokenValue Tokens
[] = {tok::l_square
, tok::l_square
,
675 PP
->getIdentifierInfo("deprecated"),
676 tok::r_square
, tok::r_square
};
677 MacroName
= PP
->getLastMacroWithSpelling(FD
->getLocation(), Tokens
);
678 if (!MacroName
.empty())
679 AttributeSpelling
= MacroName
;
682 if (MacroName
.empty()) {
683 TokenValue Tokens
[] = {
684 tok::kw___attribute
, tok::l_paren
,
685 tok::l_paren
, PP
->getIdentifierInfo("deprecated"),
686 tok::r_paren
, tok::r_paren
};
687 StringRef MacroName
=
688 PP
->getLastMacroWithSpelling(FD
->getLocation(), Tokens
);
689 if (!MacroName
.empty())
690 AttributeSpelling
= MacroName
;
694 SmallString
<64> TextToInsert
= AttributeSpelling
;
696 SourceLocation Loc
= FD
->getSourceRange().getBegin();
697 Diag(Loc
, diag::note_add_deprecation_attr
)
698 << FixItHint::CreateInsertion(Loc
, TextToInsert
);
702 void Sema::resolveParamCommandIndexes(const FullComment
*FC
) {
703 if (!involvesFunctionType()) {
704 // We already warned that \\param commands are not attached to a function
709 SmallVector
<ParamCommandComment
*, 8> UnresolvedParamCommands
;
711 // Comment AST nodes that correspond to \c ParamVars for which we have
712 // found a \\param command or NULL if no documentation was found so far.
713 SmallVector
<ParamCommandComment
*, 8> ParamVarDocs
;
715 ArrayRef
<const ParmVarDecl
*> ParamVars
= getParamVars();
716 ParamVarDocs
.resize(ParamVars
.size(), nullptr);
718 // First pass over all \\param commands: resolve all parameter names.
719 for (Comment::child_iterator I
= FC
->child_begin(), E
= FC
->child_end();
721 ParamCommandComment
*PCC
= dyn_cast
<ParamCommandComment
>(*I
);
722 if (!PCC
|| !PCC
->hasParamName())
724 StringRef ParamName
= PCC
->getParamNameAsWritten();
726 // Check that referenced parameter name is in the function decl.
727 const unsigned ResolvedParamIndex
= resolveParmVarReference(ParamName
,
729 if (ResolvedParamIndex
== ParamCommandComment::VarArgParamIndex
) {
730 PCC
->setIsVarArgParam();
733 if (ResolvedParamIndex
== ParamCommandComment::InvalidParamIndex
) {
734 UnresolvedParamCommands
.push_back(PCC
);
737 PCC
->setParamIndex(ResolvedParamIndex
);
738 if (ParamVarDocs
[ResolvedParamIndex
]) {
739 SourceRange ArgRange
= PCC
->getParamNameRange();
740 Diag(ArgRange
.getBegin(), diag::warn_doc_param_duplicate
)
741 << ParamName
<< ArgRange
;
742 ParamCommandComment
*PrevCommand
= ParamVarDocs
[ResolvedParamIndex
];
743 Diag(PrevCommand
->getLocation(), diag::note_doc_param_previous
)
744 << PrevCommand
->getParamNameRange();
746 ParamVarDocs
[ResolvedParamIndex
] = PCC
;
749 // Find parameter declarations that have no corresponding \\param.
750 SmallVector
<const ParmVarDecl
*, 8> OrphanedParamDecls
;
751 for (unsigned i
= 0, e
= ParamVarDocs
.size(); i
!= e
; ++i
) {
752 if (!ParamVarDocs
[i
])
753 OrphanedParamDecls
.push_back(ParamVars
[i
]);
756 // Second pass over unresolved \\param commands: do typo correction.
757 // Suggest corrections from a set of parameter declarations that have no
758 // corresponding \\param.
759 for (unsigned i
= 0, e
= UnresolvedParamCommands
.size(); i
!= e
; ++i
) {
760 const ParamCommandComment
*PCC
= UnresolvedParamCommands
[i
];
762 SourceRange ArgRange
= PCC
->getParamNameRange();
763 StringRef ParamName
= PCC
->getParamNameAsWritten();
764 Diag(ArgRange
.getBegin(), diag::warn_doc_param_not_found
)
765 << ParamName
<< ArgRange
;
767 // All parameters documented -- can't suggest a correction.
768 if (OrphanedParamDecls
.size() == 0)
771 unsigned CorrectedParamIndex
= ParamCommandComment::InvalidParamIndex
;
772 if (OrphanedParamDecls
.size() == 1) {
773 // If one parameter is not documented then that parameter is the only
774 // possible suggestion.
775 CorrectedParamIndex
= 0;
777 // Do typo correction.
778 CorrectedParamIndex
= correctTypoInParmVarReference(ParamName
,
781 if (CorrectedParamIndex
!= ParamCommandComment::InvalidParamIndex
) {
782 const ParmVarDecl
*CorrectedPVD
= OrphanedParamDecls
[CorrectedParamIndex
];
783 if (const IdentifierInfo
*CorrectedII
= CorrectedPVD
->getIdentifier())
784 Diag(ArgRange
.getBegin(), diag::note_doc_param_name_suggestion
)
785 << CorrectedII
->getName()
786 << FixItHint::CreateReplacement(ArgRange
, CorrectedII
->getName());
791 bool Sema::involvesFunctionType() {
794 if (!ThisDeclInfo
->IsFilled
)
796 return ThisDeclInfo
->involvesFunctionType();
799 bool Sema::isFunctionDecl() {
802 if (!ThisDeclInfo
->IsFilled
)
804 return ThisDeclInfo
->getKind() == DeclInfo::FunctionKind
;
807 bool Sema::isAnyFunctionDecl() {
808 return isFunctionDecl() && ThisDeclInfo
->CurrentDecl
&&
809 isa
<FunctionDecl
>(ThisDeclInfo
->CurrentDecl
);
812 bool Sema::isFunctionOrMethodVariadic() {
815 if (!ThisDeclInfo
->IsFilled
)
817 return ThisDeclInfo
->IsVariadic
;
820 bool Sema::isObjCMethodDecl() {
821 return isFunctionDecl() && ThisDeclInfo
->CurrentDecl
&&
822 isa
<ObjCMethodDecl
>(ThisDeclInfo
->CurrentDecl
);
825 bool Sema::isFunctionPointerVarDecl() {
828 if (!ThisDeclInfo
->IsFilled
)
830 if (ThisDeclInfo
->getKind() == DeclInfo::VariableKind
) {
831 if (const VarDecl
*VD
= dyn_cast_or_null
<VarDecl
>(ThisDeclInfo
->CurrentDecl
)) {
832 QualType QT
= VD
->getType();
833 return QT
->isFunctionPointerType();
839 bool Sema::isObjCPropertyDecl() {
842 if (!ThisDeclInfo
->IsFilled
)
844 return ThisDeclInfo
->CurrentDecl
->getKind() == Decl::ObjCProperty
;
847 bool Sema::isTemplateOrSpecialization() {
850 if (!ThisDeclInfo
->IsFilled
)
852 return ThisDeclInfo
->getTemplateKind() != DeclInfo::NotTemplate
;
855 bool Sema::isRecordLikeDecl() {
858 if (!ThisDeclInfo
->IsFilled
)
860 return isUnionDecl() || isClassOrStructDecl() || isObjCInterfaceDecl() ||
861 isObjCProtocolDecl();
864 bool Sema::isUnionDecl() {
867 if (!ThisDeclInfo
->IsFilled
)
869 if (const RecordDecl
*RD
=
870 dyn_cast_or_null
<RecordDecl
>(ThisDeclInfo
->CurrentDecl
))
871 return RD
->isUnion();
874 static bool isClassOrStructDeclImpl(const Decl
*D
) {
875 if (auto *record
= dyn_cast_or_null
<RecordDecl
>(D
))
876 return !record
->isUnion();
881 bool Sema::isClassOrStructDecl() {
884 if (!ThisDeclInfo
->IsFilled
)
887 if (!ThisDeclInfo
->CurrentDecl
)
890 return isClassOrStructDeclImpl(ThisDeclInfo
->CurrentDecl
);
893 bool Sema::isClassOrStructOrTagTypedefDecl() {
896 if (!ThisDeclInfo
->IsFilled
)
899 if (!ThisDeclInfo
->CurrentDecl
)
902 if (isClassOrStructDeclImpl(ThisDeclInfo
->CurrentDecl
))
905 if (auto *ThisTypedefDecl
= dyn_cast
<TypedefDecl
>(ThisDeclInfo
->CurrentDecl
)) {
906 auto UnderlyingType
= ThisTypedefDecl
->getUnderlyingType();
907 if (auto ThisElaboratedType
= dyn_cast
<ElaboratedType
>(UnderlyingType
)) {
908 auto DesugaredType
= ThisElaboratedType
->desugar();
909 if (auto *DesugaredTypePtr
= DesugaredType
.getTypePtrOrNull()) {
910 if (auto *ThisRecordType
= dyn_cast
<RecordType
>(DesugaredTypePtr
)) {
911 return isClassOrStructDeclImpl(ThisRecordType
->getAsRecordDecl());
920 bool Sema::isClassTemplateDecl() {
923 if (!ThisDeclInfo
->IsFilled
)
925 return ThisDeclInfo
->CurrentDecl
&&
926 (isa
<ClassTemplateDecl
>(ThisDeclInfo
->CurrentDecl
));
929 bool Sema::isFunctionTemplateDecl() {
932 if (!ThisDeclInfo
->IsFilled
)
934 return ThisDeclInfo
->CurrentDecl
&&
935 (isa
<FunctionTemplateDecl
>(ThisDeclInfo
->CurrentDecl
));
938 bool Sema::isObjCInterfaceDecl() {
941 if (!ThisDeclInfo
->IsFilled
)
943 return ThisDeclInfo
->CurrentDecl
&&
944 isa
<ObjCInterfaceDecl
>(ThisDeclInfo
->CurrentDecl
);
947 bool Sema::isObjCProtocolDecl() {
950 if (!ThisDeclInfo
->IsFilled
)
952 return ThisDeclInfo
->CurrentDecl
&&
953 isa
<ObjCProtocolDecl
>(ThisDeclInfo
->CurrentDecl
);
956 ArrayRef
<const ParmVarDecl
*> Sema::getParamVars() {
957 if (!ThisDeclInfo
->IsFilled
)
959 return ThisDeclInfo
->ParamVars
;
962 void Sema::inspectThisDecl() {
963 ThisDeclInfo
->fill();
966 unsigned Sema::resolveParmVarReference(StringRef Name
,
967 ArrayRef
<const ParmVarDecl
*> ParamVars
) {
968 for (unsigned i
= 0, e
= ParamVars
.size(); i
!= e
; ++i
) {
969 const IdentifierInfo
*II
= ParamVars
[i
]->getIdentifier();
970 if (II
&& II
->getName() == Name
)
973 if (Name
== "..." && isFunctionOrMethodVariadic())
974 return ParamCommandComment::VarArgParamIndex
;
975 return ParamCommandComment::InvalidParamIndex
;
979 class SimpleTypoCorrector
{
980 const NamedDecl
*BestDecl
;
983 const unsigned MaxEditDistance
;
985 unsigned BestEditDistance
;
990 explicit SimpleTypoCorrector(StringRef Typo
)
991 : BestDecl(nullptr), Typo(Typo
), MaxEditDistance((Typo
.size() + 2) / 3),
992 BestEditDistance(MaxEditDistance
+ 1), BestIndex(0), NextIndex(0) {}
994 void addDecl(const NamedDecl
*ND
);
996 const NamedDecl
*getBestDecl() const {
997 if (BestEditDistance
> MaxEditDistance
)
1003 unsigned getBestDeclIndex() const {
1004 assert(getBestDecl());
1009 void SimpleTypoCorrector::addDecl(const NamedDecl
*ND
) {
1010 unsigned CurrIndex
= NextIndex
++;
1012 const IdentifierInfo
*II
= ND
->getIdentifier();
1016 StringRef Name
= II
->getName();
1017 unsigned MinPossibleEditDistance
= abs((int)Name
.size() - (int)Typo
.size());
1018 if (MinPossibleEditDistance
> 0 &&
1019 Typo
.size() / MinPossibleEditDistance
< 3)
1022 unsigned EditDistance
= Typo
.edit_distance(Name
, true, MaxEditDistance
);
1023 if (EditDistance
< BestEditDistance
) {
1024 BestEditDistance
= EditDistance
;
1026 BestIndex
= CurrIndex
;
1029 } // end anonymous namespace
1031 unsigned Sema::correctTypoInParmVarReference(
1033 ArrayRef
<const ParmVarDecl
*> ParamVars
) {
1034 SimpleTypoCorrector
Corrector(Typo
);
1035 for (unsigned i
= 0, e
= ParamVars
.size(); i
!= e
; ++i
)
1036 Corrector
.addDecl(ParamVars
[i
]);
1037 if (Corrector
.getBestDecl())
1038 return Corrector
.getBestDeclIndex();
1040 return ParamCommandComment::InvalidParamIndex
;
1044 bool ResolveTParamReferenceHelper(
1046 const TemplateParameterList
*TemplateParameters
,
1047 SmallVectorImpl
<unsigned> *Position
) {
1048 for (unsigned i
= 0, e
= TemplateParameters
->size(); i
!= e
; ++i
) {
1049 const NamedDecl
*Param
= TemplateParameters
->getParam(i
);
1050 const IdentifierInfo
*II
= Param
->getIdentifier();
1051 if (II
&& II
->getName() == Name
) {
1052 Position
->push_back(i
);
1056 if (const TemplateTemplateParmDecl
*TTP
=
1057 dyn_cast
<TemplateTemplateParmDecl
>(Param
)) {
1058 Position
->push_back(i
);
1059 if (ResolveTParamReferenceHelper(Name
, TTP
->getTemplateParameters(),
1062 Position
->pop_back();
1067 } // end anonymous namespace
1069 bool Sema::resolveTParamReference(
1071 const TemplateParameterList
*TemplateParameters
,
1072 SmallVectorImpl
<unsigned> *Position
) {
1074 if (!TemplateParameters
)
1077 return ResolveTParamReferenceHelper(Name
, TemplateParameters
, Position
);
1081 void CorrectTypoInTParamReferenceHelper(
1082 const TemplateParameterList
*TemplateParameters
,
1083 SimpleTypoCorrector
&Corrector
) {
1084 for (unsigned i
= 0, e
= TemplateParameters
->size(); i
!= e
; ++i
) {
1085 const NamedDecl
*Param
= TemplateParameters
->getParam(i
);
1086 Corrector
.addDecl(Param
);
1088 if (const TemplateTemplateParmDecl
*TTP
=
1089 dyn_cast
<TemplateTemplateParmDecl
>(Param
))
1090 CorrectTypoInTParamReferenceHelper(TTP
->getTemplateParameters(),
1094 } // end anonymous namespace
1096 StringRef
Sema::correctTypoInTParamReference(
1098 const TemplateParameterList
*TemplateParameters
) {
1099 SimpleTypoCorrector
Corrector(Typo
);
1100 CorrectTypoInTParamReferenceHelper(TemplateParameters
, Corrector
);
1101 if (const NamedDecl
*ND
= Corrector
.getBestDecl()) {
1102 const IdentifierInfo
*II
= ND
->getIdentifier();
1103 assert(II
&& "SimpleTypoCorrector should not return this decl");
1104 return II
->getName();
1109 InlineCommandRenderKind
Sema::getInlineCommandRenderKind(StringRef Name
) const {
1110 assert(Traits
.getCommandInfo(Name
)->IsInlineCommand
);
1112 return llvm::StringSwitch
<InlineCommandRenderKind
>(Name
)
1113 .Case("b", InlineCommandRenderKind::Bold
)
1114 .Cases("c", "p", InlineCommandRenderKind::Monospaced
)
1115 .Cases("a", "e", "em", InlineCommandRenderKind::Emphasized
)
1116 .Case("anchor", InlineCommandRenderKind::Anchor
)
1117 .Default(InlineCommandRenderKind::Normal
);
1120 } // end namespace comments
1121 } // end namespace clang