1 //===--- CommentSema.cpp - Doxygen comment semantic analysis --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/AST/CommentSema.h"
11 #include "clang/AST/Attr.h"
12 #include "clang/AST/CommentCommandTraits.h"
13 #include "clang/AST/CommentDiagnostic.h"
14 #include "clang/AST/Decl.h"
15 #include "clang/AST/DeclTemplate.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"
25 #include "clang/AST/CommentHTMLTagsProperties.inc"
26 } // end anonymous namespace
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 HeaderfileCommand(nullptr) {
36 void Sema::setDecl(const Decl *D) {
40 ThisDeclInfo = new (Allocator) DeclInfo;
41 ThisDeclInfo->CommentDecl = D;
42 ThisDeclInfo->IsFilled = false;
45 ParagraphComment *Sema::actOnParagraphComment(
46 ArrayRef<InlineContentComment *> Content) {
47 return new (Allocator) ParagraphComment(Content);
50 BlockCommandComment *Sema::actOnBlockCommandStart(
51 SourceLocation LocBegin,
52 SourceLocation LocEnd,
54 CommandMarkerKind CommandMarker) {
55 BlockCommandComment *BC = new (Allocator) BlockCommandComment(LocBegin, LocEnd,
58 checkContainerDecl(BC);
62 void Sema::actOnBlockCommandArgs(BlockCommandComment *Command,
63 ArrayRef<BlockCommandComment::Argument> Args) {
64 Command->setArgs(Args);
67 void Sema::actOnBlockCommandFinish(BlockCommandComment *Command,
68 ParagraphComment *Paragraph) {
69 Command->setParagraph(Paragraph);
70 checkBlockCommandEmptyParagraph(Command);
71 checkBlockCommandDuplicate(Command);
73 // These checks only make sense if the comment is attached to a
75 checkReturnsCommand(Command);
76 checkDeprecatedCommand(Command);
80 ParamCommandComment *Sema::actOnParamCommandStart(
81 SourceLocation LocBegin,
82 SourceLocation LocEnd,
84 CommandMarkerKind CommandMarker) {
85 ParamCommandComment *Command =
86 new (Allocator) ParamCommandComment(LocBegin, LocEnd, CommandID,
89 if (!isFunctionDecl())
90 Diag(Command->getLocation(),
91 diag::warn_doc_param_not_attached_to_a_function_decl)
93 << Command->getCommandNameRange(Traits);
98 void Sema::checkFunctionDeclVerbatimLine(const BlockCommandComment *Comment) {
99 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
100 if (!Info->IsFunctionDeclarationCommand)
104 switch (Comment->getCommandID()) {
105 case CommandTraits::KCI_function:
106 DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 1 : 0;
108 case CommandTraits::KCI_functiongroup:
109 DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 2 : 0;
111 case CommandTraits::KCI_method:
112 DiagSelect = !isObjCMethodDecl() ? 3 : 0;
114 case CommandTraits::KCI_methodgroup:
115 DiagSelect = !isObjCMethodDecl() ? 4 : 0;
117 case CommandTraits::KCI_callback:
118 DiagSelect = !isFunctionPointerVarDecl() ? 5 : 0;
125 Diag(Comment->getLocation(), diag::warn_doc_function_method_decl_mismatch)
126 << Comment->getCommandMarker()
127 << (DiagSelect-1) << (DiagSelect-1)
128 << Comment->getSourceRange();
131 void Sema::checkContainerDeclVerbatimLine(const BlockCommandComment *Comment) {
132 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
133 if (!Info->IsRecordLikeDeclarationCommand)
136 switch (Comment->getCommandID()) {
137 case CommandTraits::KCI_class:
138 DiagSelect = (!isClassOrStructDecl() && !isClassTemplateDecl()) ? 1 : 0;
139 // Allow @class command on @interface declarations.
140 // FIXME. Currently, \class and @class are indistinguishable. So,
141 // \class is also allowed on an @interface declaration
142 if (DiagSelect && Comment->getCommandMarker() && isObjCInterfaceDecl())
145 case CommandTraits::KCI_interface:
146 DiagSelect = !isObjCInterfaceDecl() ? 2 : 0;
148 case CommandTraits::KCI_protocol:
149 DiagSelect = !isObjCProtocolDecl() ? 3 : 0;
151 case CommandTraits::KCI_struct:
152 DiagSelect = !isClassOrStructDecl() ? 4 : 0;
154 case CommandTraits::KCI_union:
155 DiagSelect = !isUnionDecl() ? 5 : 0;
162 Diag(Comment->getLocation(), diag::warn_doc_api_container_decl_mismatch)
163 << Comment->getCommandMarker()
164 << (DiagSelect-1) << (DiagSelect-1)
165 << Comment->getSourceRange();
168 void Sema::checkContainerDecl(const BlockCommandComment *Comment) {
169 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
170 if (!Info->IsRecordLikeDetailCommand || isRecordLikeDecl())
173 switch (Comment->getCommandID()) {
174 case CommandTraits::KCI_classdesign:
177 case CommandTraits::KCI_coclass:
180 case CommandTraits::KCI_dependency:
183 case CommandTraits::KCI_helper:
186 case CommandTraits::KCI_helperclass:
189 case CommandTraits::KCI_helps:
192 case CommandTraits::KCI_instancesize:
195 case CommandTraits::KCI_ownership:
198 case CommandTraits::KCI_performance:
201 case CommandTraits::KCI_security:
204 case CommandTraits::KCI_superclass:
212 Diag(Comment->getLocation(), diag::warn_doc_container_decl_mismatch)
213 << Comment->getCommandMarker()
215 << Comment->getSourceRange();
218 /// \brief Turn a string into the corresponding PassDirection or -1 if it's not
220 static int getParamPassDirection(StringRef Arg) {
221 return llvm::StringSwitch<int>(Arg)
222 .Case("[in]", ParamCommandComment::In)
223 .Case("[out]", ParamCommandComment::Out)
224 .Cases("[in,out]", "[out,in]", ParamCommandComment::InOut)
228 void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command,
229 SourceLocation ArgLocBegin,
230 SourceLocation ArgLocEnd,
232 std::string ArgLower = Arg.lower();
233 int Direction = getParamPassDirection(ArgLower);
235 if (Direction == -1) {
236 // Try again with whitespace removed.
238 std::remove_if(ArgLower.begin(), ArgLower.end(), clang::isWhitespace),
240 Direction = getParamPassDirection(ArgLower);
242 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
243 if (Direction != -1) {
244 const char *FixedName = ParamCommandComment::getDirectionAsString(
245 (ParamCommandComment::PassDirection)Direction);
246 Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction)
247 << ArgRange << FixItHint::CreateReplacement(ArgRange, FixedName);
249 Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction) << ArgRange;
250 Direction = ParamCommandComment::In; // Sane fall back.
253 Command->setDirection((ParamCommandComment::PassDirection)Direction,
257 void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command,
258 SourceLocation ArgLocBegin,
259 SourceLocation ArgLocEnd,
261 // Parser will not feed us more arguments than needed.
262 assert(Command->getNumArgs() == 0);
264 if (!Command->isDirectionExplicit()) {
265 // User didn't provide a direction argument.
266 Command->setDirection(ParamCommandComment::In, /* Explicit = */ false);
268 typedef BlockCommandComment::Argument Argument;
269 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
272 Command->setArgs(llvm::makeArrayRef(A, 1));
275 void Sema::actOnParamCommandFinish(ParamCommandComment *Command,
276 ParagraphComment *Paragraph) {
277 Command->setParagraph(Paragraph);
278 checkBlockCommandEmptyParagraph(Command);
281 TParamCommandComment *Sema::actOnTParamCommandStart(
282 SourceLocation LocBegin,
283 SourceLocation LocEnd,
285 CommandMarkerKind CommandMarker) {
286 TParamCommandComment *Command =
287 new (Allocator) TParamCommandComment(LocBegin, LocEnd, CommandID,
290 if (!isTemplateOrSpecialization())
291 Diag(Command->getLocation(),
292 diag::warn_doc_tparam_not_attached_to_a_template_decl)
294 << Command->getCommandNameRange(Traits);
299 void Sema::actOnTParamCommandParamNameArg(TParamCommandComment *Command,
300 SourceLocation ArgLocBegin,
301 SourceLocation ArgLocEnd,
303 // Parser will not feed us more arguments than needed.
304 assert(Command->getNumArgs() == 0);
306 typedef BlockCommandComment::Argument Argument;
307 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
310 Command->setArgs(llvm::makeArrayRef(A, 1));
312 if (!isTemplateOrSpecialization()) {
313 // We already warned that this \\tparam is not attached to a template decl.
317 const TemplateParameterList *TemplateParameters =
318 ThisDeclInfo->TemplateParameters;
319 SmallVector<unsigned, 2> Position;
320 if (resolveTParamReference(Arg, TemplateParameters, &Position)) {
321 Command->setPosition(copyArray(llvm::makeArrayRef(Position)));
322 TParamCommandComment *&PrevCommand = TemplateParameterDocs[Arg];
324 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
325 Diag(ArgLocBegin, diag::warn_doc_tparam_duplicate)
327 Diag(PrevCommand->getLocation(), diag::note_doc_tparam_previous)
328 << PrevCommand->getParamNameRange();
330 PrevCommand = Command;
334 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
335 Diag(ArgLocBegin, diag::warn_doc_tparam_not_found)
338 if (!TemplateParameters || TemplateParameters->size() == 0)
341 StringRef CorrectedName;
342 if (TemplateParameters->size() == 1) {
343 const NamedDecl *Param = TemplateParameters->getParam(0);
344 const IdentifierInfo *II = Param->getIdentifier();
346 CorrectedName = II->getName();
348 CorrectedName = correctTypoInTParamReference(Arg, TemplateParameters);
351 if (!CorrectedName.empty()) {
352 Diag(ArgLocBegin, diag::note_doc_tparam_name_suggestion)
354 << FixItHint::CreateReplacement(ArgRange, CorrectedName);
358 void Sema::actOnTParamCommandFinish(TParamCommandComment *Command,
359 ParagraphComment *Paragraph) {
360 Command->setParagraph(Paragraph);
361 checkBlockCommandEmptyParagraph(Command);
364 InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
365 SourceLocation CommandLocEnd,
366 unsigned CommandID) {
367 ArrayRef<InlineCommandComment::Argument> Args;
368 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
369 return new (Allocator) InlineCommandComment(
373 getInlineCommandRenderKind(CommandName),
377 InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
378 SourceLocation CommandLocEnd,
380 SourceLocation ArgLocBegin,
381 SourceLocation ArgLocEnd,
383 typedef InlineCommandComment::Argument Argument;
384 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
387 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
389 return new (Allocator) InlineCommandComment(
393 getInlineCommandRenderKind(CommandName),
394 llvm::makeArrayRef(A, 1));
397 InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
398 SourceLocation LocEnd,
399 StringRef CommandName) {
400 unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID();
401 return actOnUnknownCommand(LocBegin, LocEnd, CommandID);
404 InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
405 SourceLocation LocEnd,
406 unsigned CommandID) {
407 ArrayRef<InlineCommandComment::Argument> Args;
408 return new (Allocator) InlineCommandComment(
409 LocBegin, LocEnd, CommandID,
410 InlineCommandComment::RenderNormal,
414 TextComment *Sema::actOnText(SourceLocation LocBegin,
415 SourceLocation LocEnd,
417 return new (Allocator) TextComment(LocBegin, LocEnd, Text);
420 VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc,
421 unsigned CommandID) {
422 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
423 return new (Allocator) VerbatimBlockComment(
425 Loc.getLocWithOffset(1 + CommandName.size()),
429 VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc,
431 return new (Allocator) VerbatimBlockLineComment(Loc, Text);
434 void Sema::actOnVerbatimBlockFinish(
435 VerbatimBlockComment *Block,
436 SourceLocation CloseNameLocBegin,
438 ArrayRef<VerbatimBlockLineComment *> Lines) {
439 Block->setCloseName(CloseName, CloseNameLocBegin);
440 Block->setLines(Lines);
443 VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin,
445 SourceLocation TextBegin,
447 VerbatimLineComment *VL = new (Allocator) VerbatimLineComment(
449 TextBegin.getLocWithOffset(Text.size()),
453 checkFunctionDeclVerbatimLine(VL);
454 checkContainerDeclVerbatimLine(VL);
458 HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin,
460 return new (Allocator) HTMLStartTagComment(LocBegin, TagName);
463 void Sema::actOnHTMLStartTagFinish(
464 HTMLStartTagComment *Tag,
465 ArrayRef<HTMLStartTagComment::Attribute> Attrs,
466 SourceLocation GreaterLoc,
467 bool IsSelfClosing) {
468 Tag->setAttrs(Attrs);
469 Tag->setGreaterLoc(GreaterLoc);
471 Tag->setSelfClosing();
472 else if (!isHTMLEndTagForbidden(Tag->getTagName()))
473 HTMLOpenTags.push_back(Tag);
476 HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin,
477 SourceLocation LocEnd,
479 HTMLEndTagComment *HET =
480 new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName);
481 if (isHTMLEndTagForbidden(TagName)) {
482 Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden)
483 << TagName << HET->getSourceRange();
484 HET->setIsMalformed();
488 bool FoundOpen = false;
489 for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator
490 I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend();
492 if ((*I)->getTagName() == TagName) {
498 Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced)
499 << HET->getSourceRange();
500 HET->setIsMalformed();
504 while (!HTMLOpenTags.empty()) {
505 HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val();
506 StringRef LastNotClosedTagName = HST->getTagName();
507 if (LastNotClosedTagName == TagName) {
508 // If the start tag is malformed, end tag is malformed as well.
509 if (HST->isMalformed())
510 HET->setIsMalformed();
514 if (isHTMLEndTagOptional(LastNotClosedTagName))
517 bool OpenLineInvalid;
518 const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
521 bool CloseLineInvalid;
522 const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
526 if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine) {
527 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
528 << HST->getTagName() << HET->getTagName()
529 << HST->getSourceRange() << HET->getSourceRange();
530 HST->setIsMalformed();
532 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
533 << HST->getTagName() << HET->getTagName()
534 << HST->getSourceRange();
535 Diag(HET->getLocation(), diag::note_doc_html_end_tag)
536 << HET->getSourceRange();
537 HST->setIsMalformed();
544 FullComment *Sema::actOnFullComment(
545 ArrayRef<BlockContentComment *> Blocks) {
546 FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo);
547 resolveParamCommandIndexes(FC);
549 // Complain about HTML tags that are not closed.
550 while (!HTMLOpenTags.empty()) {
551 HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val();
552 if (isHTMLEndTagOptional(HST->getTagName()))
555 Diag(HST->getLocation(), diag::warn_doc_html_missing_end_tag)
556 << HST->getTagName() << HST->getSourceRange();
557 HST->setIsMalformed();
563 void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) {
564 if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed)
567 ParagraphComment *Paragraph = Command->getParagraph();
568 if (Paragraph->isWhitespace()) {
569 SourceLocation DiagLoc;
570 if (Command->getNumArgs() > 0)
571 DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
572 if (!DiagLoc.isValid())
573 DiagLoc = Command->getCommandNameRange(Traits).getEnd();
574 Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
575 << Command->getCommandMarker()
576 << Command->getCommandName(Traits)
577 << Command->getSourceRange();
581 void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
582 if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
585 assert(ThisDeclInfo && "should not call this check on a bare comment");
587 if (isFunctionDecl()) {
588 if (ThisDeclInfo->ReturnType->isVoidType()) {
590 switch (ThisDeclInfo->CommentDecl->getKind()) {
592 if (ThisDeclInfo->IsObjCMethod)
597 case Decl::CXXConstructor:
600 case Decl::CXXDestructor:
604 Diag(Command->getLocation(),
605 diag::warn_doc_returns_attached_to_a_void_function)
606 << Command->getCommandMarker()
607 << Command->getCommandName(Traits)
609 << Command->getSourceRange();
613 else if (isObjCPropertyDecl())
616 Diag(Command->getLocation(),
617 diag::warn_doc_returns_not_attached_to_a_function_decl)
618 << Command->getCommandMarker()
619 << Command->getCommandName(Traits)
620 << Command->getSourceRange();
623 void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
624 const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
625 const BlockCommandComment *PrevCommand = nullptr;
626 if (Info->IsBriefCommand) {
628 BriefCommand = Command;
631 PrevCommand = BriefCommand;
632 } else if (Info->IsHeaderfileCommand) {
633 if (!HeaderfileCommand) {
634 HeaderfileCommand = Command;
637 PrevCommand = HeaderfileCommand;
639 // We don't want to check this command for duplicates.
642 StringRef CommandName = Command->getCommandName(Traits);
643 StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
644 Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
645 << Command->getCommandMarker()
647 << Command->getSourceRange();
648 if (CommandName == PrevCommandName)
649 Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
650 << PrevCommand->getCommandMarker()
652 << PrevCommand->getSourceRange();
654 Diag(PrevCommand->getLocation(),
655 diag::note_doc_block_command_previous_alias)
656 << PrevCommand->getCommandMarker()
661 void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) {
662 if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand)
665 assert(ThisDeclInfo && "should not call this check on a bare comment");
667 const Decl *D = ThisDeclInfo->CommentDecl;
671 if (D->hasAttr<DeprecatedAttr>() ||
672 D->hasAttr<AvailabilityAttr>() ||
673 D->hasAttr<UnavailableAttr>())
676 Diag(Command->getLocation(),
677 diag::warn_doc_deprecated_not_sync)
678 << Command->getSourceRange();
680 // Try to emit a fixit with a deprecation attribute.
681 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
682 // Don't emit a Fix-It for non-member function definitions. GCC does not
683 // accept attributes on them.
684 const DeclContext *Ctx = FD->getDeclContext();
685 if ((!Ctx || !Ctx->isRecord()) &&
686 FD->doesThisDeclarationHaveABody())
689 StringRef AttributeSpelling = "__attribute__((deprecated))";
691 TokenValue Tokens[] = {
692 tok::kw___attribute, tok::l_paren, tok::l_paren,
693 PP->getIdentifierInfo("deprecated"),
694 tok::r_paren, tok::r_paren
696 StringRef MacroName = PP->getLastMacroWithSpelling(FD->getLocation(),
698 if (!MacroName.empty())
699 AttributeSpelling = MacroName;
702 SmallString<64> TextToInsert(" ");
703 TextToInsert += AttributeSpelling;
704 Diag(FD->getLocEnd(),
705 diag::note_add_deprecation_attr)
706 << FixItHint::CreateInsertion(FD->getLocEnd().getLocWithOffset(1),
711 void Sema::resolveParamCommandIndexes(const FullComment *FC) {
712 if (!isFunctionDecl()) {
713 // We already warned that \\param commands are not attached to a function
718 SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
720 // Comment AST nodes that correspond to \c ParamVars for which we have
721 // found a \\param command or NULL if no documentation was found so far.
722 SmallVector<ParamCommandComment *, 8> ParamVarDocs;
724 ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
725 ParamVarDocs.resize(ParamVars.size(), nullptr);
727 // First pass over all \\param commands: resolve all parameter names.
728 for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end();
730 ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
731 if (!PCC || !PCC->hasParamName())
733 StringRef ParamName = PCC->getParamNameAsWritten();
735 // Check that referenced parameter name is in the function decl.
736 const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
738 if (ResolvedParamIndex == ParamCommandComment::VarArgParamIndex) {
739 PCC->setIsVarArgParam();
742 if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) {
743 UnresolvedParamCommands.push_back(PCC);
746 PCC->setParamIndex(ResolvedParamIndex);
747 if (ParamVarDocs[ResolvedParamIndex]) {
748 SourceRange ArgRange = PCC->getParamNameRange();
749 Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
750 << ParamName << ArgRange;
751 ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
752 Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
753 << PrevCommand->getParamNameRange();
755 ParamVarDocs[ResolvedParamIndex] = PCC;
758 // Find parameter declarations that have no corresponding \\param.
759 SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
760 for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) {
761 if (!ParamVarDocs[i])
762 OrphanedParamDecls.push_back(ParamVars[i]);
765 // Second pass over unresolved \\param commands: do typo correction.
766 // Suggest corrections from a set of parameter declarations that have no
767 // corresponding \\param.
768 for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) {
769 const ParamCommandComment *PCC = UnresolvedParamCommands[i];
771 SourceRange ArgRange = PCC->getParamNameRange();
772 StringRef ParamName = PCC->getParamNameAsWritten();
773 Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
774 << ParamName << ArgRange;
776 // All parameters documented -- can't suggest a correction.
777 if (OrphanedParamDecls.size() == 0)
780 unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
781 if (OrphanedParamDecls.size() == 1) {
782 // If one parameter is not documented then that parameter is the only
783 // possible suggestion.
784 CorrectedParamIndex = 0;
786 // Do typo correction.
787 CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
790 if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
791 const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
792 if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
793 Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
794 << CorrectedII->getName()
795 << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
800 bool Sema::isFunctionDecl() {
803 if (!ThisDeclInfo->IsFilled)
805 return ThisDeclInfo->getKind() == DeclInfo::FunctionKind;
808 bool Sema::isAnyFunctionDecl() {
809 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
810 isa<FunctionDecl>(ThisDeclInfo->CurrentDecl);
813 bool Sema::isFunctionOrMethodVariadic() {
814 if (!isAnyFunctionDecl() && !isObjCMethodDecl() && !isFunctionTemplateDecl())
816 if (const FunctionDecl *FD =
817 dyn_cast<FunctionDecl>(ThisDeclInfo->CurrentDecl))
818 return FD->isVariadic();
819 if (const FunctionTemplateDecl *FTD =
820 dyn_cast<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl))
821 return FTD->getTemplatedDecl()->isVariadic();
822 if (const ObjCMethodDecl *MD =
823 dyn_cast<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl))
824 return MD->isVariadic();
828 bool Sema::isObjCMethodDecl() {
829 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
830 isa<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl);
833 bool Sema::isFunctionPointerVarDecl() {
836 if (!ThisDeclInfo->IsFilled)
838 if (ThisDeclInfo->getKind() == DeclInfo::VariableKind) {
839 if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDeclInfo->CurrentDecl)) {
840 QualType QT = VD->getType();
841 return QT->isFunctionPointerType();
847 bool Sema::isObjCPropertyDecl() {
850 if (!ThisDeclInfo->IsFilled)
852 return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty;
855 bool Sema::isTemplateOrSpecialization() {
858 if (!ThisDeclInfo->IsFilled)
860 return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
863 bool Sema::isRecordLikeDecl() {
866 if (!ThisDeclInfo->IsFilled)
868 return isUnionDecl() || isClassOrStructDecl() || isObjCInterfaceDecl() ||
869 isObjCProtocolDecl();
872 bool Sema::isUnionDecl() {
875 if (!ThisDeclInfo->IsFilled)
877 if (const RecordDecl *RD =
878 dyn_cast_or_null<RecordDecl>(ThisDeclInfo->CurrentDecl))
879 return RD->isUnion();
883 bool Sema::isClassOrStructDecl() {
886 if (!ThisDeclInfo->IsFilled)
888 return ThisDeclInfo->CurrentDecl &&
889 isa<RecordDecl>(ThisDeclInfo->CurrentDecl) &&
893 bool Sema::isClassTemplateDecl() {
896 if (!ThisDeclInfo->IsFilled)
898 return ThisDeclInfo->CurrentDecl &&
899 (isa<ClassTemplateDecl>(ThisDeclInfo->CurrentDecl));
902 bool Sema::isFunctionTemplateDecl() {
905 if (!ThisDeclInfo->IsFilled)
907 return ThisDeclInfo->CurrentDecl &&
908 (isa<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl));
911 bool Sema::isObjCInterfaceDecl() {
914 if (!ThisDeclInfo->IsFilled)
916 return ThisDeclInfo->CurrentDecl &&
917 isa<ObjCInterfaceDecl>(ThisDeclInfo->CurrentDecl);
920 bool Sema::isObjCProtocolDecl() {
923 if (!ThisDeclInfo->IsFilled)
925 return ThisDeclInfo->CurrentDecl &&
926 isa<ObjCProtocolDecl>(ThisDeclInfo->CurrentDecl);
929 ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
930 if (!ThisDeclInfo->IsFilled)
932 return ThisDeclInfo->ParamVars;
935 void Sema::inspectThisDecl() {
936 ThisDeclInfo->fill();
939 unsigned Sema::resolveParmVarReference(StringRef Name,
940 ArrayRef<const ParmVarDecl *> ParamVars) {
941 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
942 const IdentifierInfo *II = ParamVars[i]->getIdentifier();
943 if (II && II->getName() == Name)
946 if (Name == "..." && isFunctionOrMethodVariadic())
947 return ParamCommandComment::VarArgParamIndex;
948 return ParamCommandComment::InvalidParamIndex;
952 class SimpleTypoCorrector {
954 const unsigned MaxEditDistance;
956 const NamedDecl *BestDecl;
957 unsigned BestEditDistance;
962 SimpleTypoCorrector(StringRef Typo) :
963 Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
964 BestDecl(nullptr), BestEditDistance(MaxEditDistance + 1),
965 BestIndex(0), NextIndex(0)
968 void addDecl(const NamedDecl *ND);
970 const NamedDecl *getBestDecl() const {
971 if (BestEditDistance > MaxEditDistance)
977 unsigned getBestDeclIndex() const {
978 assert(getBestDecl());
983 void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
984 unsigned CurrIndex = NextIndex++;
986 const IdentifierInfo *II = ND->getIdentifier();
990 StringRef Name = II->getName();
991 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
992 if (MinPossibleEditDistance > 0 &&
993 Typo.size() / MinPossibleEditDistance < 3)
996 unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
997 if (EditDistance < BestEditDistance) {
998 BestEditDistance = EditDistance;
1000 BestIndex = CurrIndex;
1003 } // end anonymous namespace
1005 unsigned Sema::correctTypoInParmVarReference(
1007 ArrayRef<const ParmVarDecl *> ParamVars) {
1008 SimpleTypoCorrector Corrector(Typo);
1009 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i)
1010 Corrector.addDecl(ParamVars[i]);
1011 if (Corrector.getBestDecl())
1012 return Corrector.getBestDeclIndex();
1014 return ParamCommandComment::InvalidParamIndex;
1018 bool ResolveTParamReferenceHelper(
1020 const TemplateParameterList *TemplateParameters,
1021 SmallVectorImpl<unsigned> *Position) {
1022 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1023 const NamedDecl *Param = TemplateParameters->getParam(i);
1024 const IdentifierInfo *II = Param->getIdentifier();
1025 if (II && II->getName() == Name) {
1026 Position->push_back(i);
1030 if (const TemplateTemplateParmDecl *TTP =
1031 dyn_cast<TemplateTemplateParmDecl>(Param)) {
1032 Position->push_back(i);
1033 if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
1036 Position->pop_back();
1041 } // end anonymous namespace
1043 bool Sema::resolveTParamReference(
1045 const TemplateParameterList *TemplateParameters,
1046 SmallVectorImpl<unsigned> *Position) {
1048 if (!TemplateParameters)
1051 return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
1055 void CorrectTypoInTParamReferenceHelper(
1056 const TemplateParameterList *TemplateParameters,
1057 SimpleTypoCorrector &Corrector) {
1058 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1059 const NamedDecl *Param = TemplateParameters->getParam(i);
1060 Corrector.addDecl(Param);
1062 if (const TemplateTemplateParmDecl *TTP =
1063 dyn_cast<TemplateTemplateParmDecl>(Param))
1064 CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
1068 } // end anonymous namespace
1070 StringRef Sema::correctTypoInTParamReference(
1072 const TemplateParameterList *TemplateParameters) {
1073 SimpleTypoCorrector Corrector(Typo);
1074 CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
1075 if (const NamedDecl *ND = Corrector.getBestDecl()) {
1076 const IdentifierInfo *II = ND->getIdentifier();
1077 assert(II && "SimpleTypoCorrector should not return this decl");
1078 return II->getName();
1083 InlineCommandComment::RenderKind
1084 Sema::getInlineCommandRenderKind(StringRef Name) const {
1085 assert(Traits.getCommandInfo(Name)->IsInlineCommand);
1087 return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
1088 .Case("b", InlineCommandComment::RenderBold)
1089 .Cases("c", "p", InlineCommandComment::RenderMonospaced)
1090 .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
1091 .Default(InlineCommandComment::RenderNormal);
1094 } // end namespace comments
1095 } // end namespace clang