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 } // unnamed 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(NULL), BriefCommand(NULL), HeaderfileCommand(NULL) {
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);
71 checkReturnsCommand(Command);
72 checkDeprecatedCommand(Command);
75 ParamCommandComment *Sema::actOnParamCommandStart(
76 SourceLocation LocBegin,
77 SourceLocation LocEnd,
79 CommandMarkerKind CommandMarker) {
80 ParamCommandComment *Command =
81 new (Allocator) ParamCommandComment(LocBegin, LocEnd, CommandID,
84 if (!isFunctionDecl())
85 Diag(Command->getLocation(),
86 diag::warn_doc_param_not_attached_to_a_function_decl)
88 << Command->getCommandNameRange(Traits);
93 void Sema::checkFunctionDeclVerbatimLine(const BlockCommandComment *Comment) {
94 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
95 if (!Info->IsFunctionDeclarationCommand)
99 switch (Comment->getCommandID()) {
100 case CommandTraits::KCI_function:
101 DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 1 : 0;
103 case CommandTraits::KCI_functiongroup:
104 DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 2 : 0;
106 case CommandTraits::KCI_method:
107 DiagSelect = !isObjCMethodDecl() ? 3 : 0;
109 case CommandTraits::KCI_methodgroup:
110 DiagSelect = !isObjCMethodDecl() ? 4 : 0;
112 case CommandTraits::KCI_callback:
113 DiagSelect = !isFunctionPointerVarDecl() ? 5 : 0;
120 Diag(Comment->getLocation(), diag::warn_doc_function_method_decl_mismatch)
121 << Comment->getCommandMarker()
122 << (DiagSelect-1) << (DiagSelect-1)
123 << Comment->getSourceRange();
126 void Sema::checkContainerDeclVerbatimLine(const BlockCommandComment *Comment) {
127 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
128 if (!Info->IsRecordLikeDeclarationCommand)
131 switch (Comment->getCommandID()) {
132 case CommandTraits::KCI_class:
133 DiagSelect = (!isClassOrStructDecl() && !isClassTemplateDecl()) ? 1 : 0;
134 // Allow @class command on @interface declarations.
135 // FIXME. Currently, \class and @class are indistinguishable. So,
136 // \class is also allowed on an @interface declaration
137 if (DiagSelect && Comment->getCommandMarker() && isObjCInterfaceDecl())
140 case CommandTraits::KCI_interface:
141 DiagSelect = !isObjCInterfaceDecl() ? 2 : 0;
143 case CommandTraits::KCI_protocol:
144 DiagSelect = !isObjCProtocolDecl() ? 3 : 0;
146 case CommandTraits::KCI_struct:
147 DiagSelect = !isClassOrStructDecl() ? 4 : 0;
149 case CommandTraits::KCI_union:
150 DiagSelect = !isUnionDecl() ? 5 : 0;
157 Diag(Comment->getLocation(), diag::warn_doc_api_container_decl_mismatch)
158 << Comment->getCommandMarker()
159 << (DiagSelect-1) << (DiagSelect-1)
160 << Comment->getSourceRange();
163 void Sema::checkContainerDecl(const BlockCommandComment *Comment) {
164 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
165 if (!Info->IsRecordLikeDetailCommand || isRecordLikeDecl())
168 switch (Comment->getCommandID()) {
169 case CommandTraits::KCI_classdesign:
172 case CommandTraits::KCI_coclass:
175 case CommandTraits::KCI_dependency:
178 case CommandTraits::KCI_helper:
181 case CommandTraits::KCI_helperclass:
184 case CommandTraits::KCI_helps:
187 case CommandTraits::KCI_instancesize:
190 case CommandTraits::KCI_ownership:
193 case CommandTraits::KCI_performance:
196 case CommandTraits::KCI_security:
199 case CommandTraits::KCI_superclass:
207 Diag(Comment->getLocation(), diag::warn_doc_container_decl_mismatch)
208 << Comment->getCommandMarker()
210 << Comment->getSourceRange();
213 /// \brief Turn a string into the corresponding PassDirection or -1 if it's not
215 static int getParamPassDirection(StringRef Arg) {
216 return llvm::StringSwitch<int>(Arg)
217 .Case("[in]", ParamCommandComment::In)
218 .Case("[out]", ParamCommandComment::Out)
219 .Cases("[in,out]", "[out,in]", ParamCommandComment::InOut)
223 void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command,
224 SourceLocation ArgLocBegin,
225 SourceLocation ArgLocEnd,
227 std::string ArgLower = Arg.lower();
228 int Direction = getParamPassDirection(ArgLower);
230 if (Direction == -1) {
231 // Try again with whitespace removed.
233 std::remove_if(ArgLower.begin(), ArgLower.end(), clang::isWhitespace),
235 Direction = getParamPassDirection(ArgLower);
237 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
238 if (Direction != -1) {
239 const char *FixedName = ParamCommandComment::getDirectionAsString(
240 (ParamCommandComment::PassDirection)Direction);
241 Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction)
242 << ArgRange << FixItHint::CreateReplacement(ArgRange, FixedName);
244 Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction) << ArgRange;
245 Direction = ParamCommandComment::In; // Sane fall back.
248 Command->setDirection((ParamCommandComment::PassDirection)Direction,
252 void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command,
253 SourceLocation ArgLocBegin,
254 SourceLocation ArgLocEnd,
256 // Parser will not feed us more arguments than needed.
257 assert(Command->getNumArgs() == 0);
259 if (!Command->isDirectionExplicit()) {
260 // User didn't provide a direction argument.
261 Command->setDirection(ParamCommandComment::In, /* Explicit = */ false);
263 typedef BlockCommandComment::Argument Argument;
264 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
267 Command->setArgs(llvm::makeArrayRef(A, 1));
270 void Sema::actOnParamCommandFinish(ParamCommandComment *Command,
271 ParagraphComment *Paragraph) {
272 Command->setParagraph(Paragraph);
273 checkBlockCommandEmptyParagraph(Command);
276 TParamCommandComment *Sema::actOnTParamCommandStart(
277 SourceLocation LocBegin,
278 SourceLocation LocEnd,
280 CommandMarkerKind CommandMarker) {
281 TParamCommandComment *Command =
282 new (Allocator) TParamCommandComment(LocBegin, LocEnd, CommandID,
285 if (!isTemplateOrSpecialization())
286 Diag(Command->getLocation(),
287 diag::warn_doc_tparam_not_attached_to_a_template_decl)
289 << Command->getCommandNameRange(Traits);
294 void Sema::actOnTParamCommandParamNameArg(TParamCommandComment *Command,
295 SourceLocation ArgLocBegin,
296 SourceLocation ArgLocEnd,
298 // Parser will not feed us more arguments than needed.
299 assert(Command->getNumArgs() == 0);
301 typedef BlockCommandComment::Argument Argument;
302 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
305 Command->setArgs(llvm::makeArrayRef(A, 1));
307 if (!isTemplateOrSpecialization()) {
308 // We already warned that this \\tparam is not attached to a template decl.
312 const TemplateParameterList *TemplateParameters =
313 ThisDeclInfo->TemplateParameters;
314 SmallVector<unsigned, 2> Position;
315 if (resolveTParamReference(Arg, TemplateParameters, &Position)) {
316 Command->setPosition(copyArray(llvm::makeArrayRef(Position)));
317 TParamCommandComment *&PrevCommand = TemplateParameterDocs[Arg];
319 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
320 Diag(ArgLocBegin, diag::warn_doc_tparam_duplicate)
322 Diag(PrevCommand->getLocation(), diag::note_doc_tparam_previous)
323 << PrevCommand->getParamNameRange();
325 PrevCommand = Command;
329 SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
330 Diag(ArgLocBegin, diag::warn_doc_tparam_not_found)
333 if (!TemplateParameters || TemplateParameters->size() == 0)
336 StringRef CorrectedName;
337 if (TemplateParameters->size() == 1) {
338 const NamedDecl *Param = TemplateParameters->getParam(0);
339 const IdentifierInfo *II = Param->getIdentifier();
341 CorrectedName = II->getName();
343 CorrectedName = correctTypoInTParamReference(Arg, TemplateParameters);
346 if (!CorrectedName.empty()) {
347 Diag(ArgLocBegin, diag::note_doc_tparam_name_suggestion)
349 << FixItHint::CreateReplacement(ArgRange, CorrectedName);
355 void Sema::actOnTParamCommandFinish(TParamCommandComment *Command,
356 ParagraphComment *Paragraph) {
357 Command->setParagraph(Paragraph);
358 checkBlockCommandEmptyParagraph(Command);
361 InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
362 SourceLocation CommandLocEnd,
363 unsigned CommandID) {
364 ArrayRef<InlineCommandComment::Argument> Args;
365 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
366 return new (Allocator) InlineCommandComment(
370 getInlineCommandRenderKind(CommandName),
374 InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
375 SourceLocation CommandLocEnd,
377 SourceLocation ArgLocBegin,
378 SourceLocation ArgLocEnd,
380 typedef InlineCommandComment::Argument Argument;
381 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
384 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
386 return new (Allocator) InlineCommandComment(
390 getInlineCommandRenderKind(CommandName),
391 llvm::makeArrayRef(A, 1));
394 InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
395 SourceLocation LocEnd,
396 StringRef CommandName) {
397 unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID();
398 return actOnUnknownCommand(LocBegin, LocEnd, CommandID);
401 InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
402 SourceLocation LocEnd,
403 unsigned CommandID) {
404 ArrayRef<InlineCommandComment::Argument> Args;
405 return new (Allocator) InlineCommandComment(
406 LocBegin, LocEnd, CommandID,
407 InlineCommandComment::RenderNormal,
411 TextComment *Sema::actOnText(SourceLocation LocBegin,
412 SourceLocation LocEnd,
414 return new (Allocator) TextComment(LocBegin, LocEnd, Text);
417 VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc,
418 unsigned CommandID) {
419 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
420 return new (Allocator) VerbatimBlockComment(
422 Loc.getLocWithOffset(1 + CommandName.size()),
426 VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc,
428 return new (Allocator) VerbatimBlockLineComment(Loc, Text);
431 void Sema::actOnVerbatimBlockFinish(
432 VerbatimBlockComment *Block,
433 SourceLocation CloseNameLocBegin,
435 ArrayRef<VerbatimBlockLineComment *> Lines) {
436 Block->setCloseName(CloseName, CloseNameLocBegin);
437 Block->setLines(Lines);
440 VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin,
442 SourceLocation TextBegin,
444 VerbatimLineComment *VL = new (Allocator) VerbatimLineComment(
446 TextBegin.getLocWithOffset(Text.size()),
450 checkFunctionDeclVerbatimLine(VL);
451 checkContainerDeclVerbatimLine(VL);
455 HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin,
457 return new (Allocator) HTMLStartTagComment(LocBegin, TagName);
460 void Sema::actOnHTMLStartTagFinish(
461 HTMLStartTagComment *Tag,
462 ArrayRef<HTMLStartTagComment::Attribute> Attrs,
463 SourceLocation GreaterLoc,
464 bool IsSelfClosing) {
465 Tag->setAttrs(Attrs);
466 Tag->setGreaterLoc(GreaterLoc);
468 Tag->setSelfClosing();
469 else if (!isHTMLEndTagForbidden(Tag->getTagName()))
470 HTMLOpenTags.push_back(Tag);
473 HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin,
474 SourceLocation LocEnd,
476 HTMLEndTagComment *HET =
477 new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName);
478 if (isHTMLEndTagForbidden(TagName)) {
479 Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden)
480 << TagName << HET->getSourceRange();
484 bool FoundOpen = false;
485 for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator
486 I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend();
488 if ((*I)->getTagName() == TagName) {
494 Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced)
495 << HET->getSourceRange();
499 while (!HTMLOpenTags.empty()) {
500 const HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val();
501 StringRef LastNotClosedTagName = HST->getTagName();
502 if (LastNotClosedTagName == TagName)
505 if (isHTMLEndTagOptional(LastNotClosedTagName))
508 bool OpenLineInvalid;
509 const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
512 bool CloseLineInvalid;
513 const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
517 if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine)
518 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
519 << HST->getTagName() << HET->getTagName()
520 << HST->getSourceRange() << HET->getSourceRange();
522 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
523 << HST->getTagName() << HET->getTagName()
524 << HST->getSourceRange();
525 Diag(HET->getLocation(), diag::note_doc_html_end_tag)
526 << HET->getSourceRange();
533 FullComment *Sema::actOnFullComment(
534 ArrayRef<BlockContentComment *> Blocks) {
535 FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo);
536 resolveParamCommandIndexes(FC);
540 void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) {
541 if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed)
544 ParagraphComment *Paragraph = Command->getParagraph();
545 if (Paragraph->isWhitespace()) {
546 SourceLocation DiagLoc;
547 if (Command->getNumArgs() > 0)
548 DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
549 if (!DiagLoc.isValid())
550 DiagLoc = Command->getCommandNameRange(Traits).getEnd();
551 Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
552 << Command->getCommandMarker()
553 << Command->getCommandName(Traits)
554 << Command->getSourceRange();
558 void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
559 if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
561 if (isFunctionDecl()) {
562 if (ThisDeclInfo->ResultType->isVoidType()) {
564 switch (ThisDeclInfo->CommentDecl->getKind()) {
566 if (ThisDeclInfo->IsObjCMethod)
571 case Decl::CXXConstructor:
574 case Decl::CXXDestructor:
578 Diag(Command->getLocation(),
579 diag::warn_doc_returns_attached_to_a_void_function)
580 << Command->getCommandMarker()
581 << Command->getCommandName(Traits)
583 << Command->getSourceRange();
587 else if (isObjCPropertyDecl())
590 Diag(Command->getLocation(),
591 diag::warn_doc_returns_not_attached_to_a_function_decl)
592 << Command->getCommandMarker()
593 << Command->getCommandName(Traits)
594 << Command->getSourceRange();
597 void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
598 const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
599 const BlockCommandComment *PrevCommand = NULL;
600 if (Info->IsBriefCommand) {
602 BriefCommand = Command;
605 PrevCommand = BriefCommand;
606 } else if (Info->IsHeaderfileCommand) {
607 if (!HeaderfileCommand) {
608 HeaderfileCommand = Command;
611 PrevCommand = HeaderfileCommand;
613 // We don't want to check this command for duplicates.
616 StringRef CommandName = Command->getCommandName(Traits);
617 StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
618 Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
619 << Command->getCommandMarker()
621 << Command->getSourceRange();
622 if (CommandName == PrevCommandName)
623 Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
624 << PrevCommand->getCommandMarker()
626 << PrevCommand->getSourceRange();
628 Diag(PrevCommand->getLocation(),
629 diag::note_doc_block_command_previous_alias)
630 << PrevCommand->getCommandMarker()
635 void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) {
636 if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand)
639 const Decl *D = ThisDeclInfo->CommentDecl;
643 if (D->hasAttr<DeprecatedAttr>() ||
644 D->hasAttr<AvailabilityAttr>() ||
645 D->hasAttr<UnavailableAttr>())
648 Diag(Command->getLocation(),
649 diag::warn_doc_deprecated_not_sync)
650 << Command->getSourceRange();
652 // Try to emit a fixit with a deprecation attribute.
653 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
654 // Don't emit a Fix-It for non-member function definitions. GCC does not
655 // accept attributes on them.
656 const DeclContext *Ctx = FD->getDeclContext();
657 if ((!Ctx || !Ctx->isRecord()) &&
658 FD->doesThisDeclarationHaveABody())
661 StringRef AttributeSpelling = "__attribute__((deprecated))";
663 TokenValue Tokens[] = {
664 tok::kw___attribute, tok::l_paren, tok::l_paren,
665 PP->getIdentifierInfo("deprecated"),
666 tok::r_paren, tok::r_paren
668 StringRef MacroName = PP->getLastMacroWithSpelling(FD->getLocation(),
670 if (!MacroName.empty())
671 AttributeSpelling = MacroName;
674 SmallString<64> TextToInsert(" ");
675 TextToInsert += AttributeSpelling;
676 Diag(FD->getLocEnd(),
677 diag::note_add_deprecation_attr)
678 << FixItHint::CreateInsertion(FD->getLocEnd().getLocWithOffset(1),
683 void Sema::resolveParamCommandIndexes(const FullComment *FC) {
684 if (!isFunctionDecl()) {
685 // We already warned that \\param commands are not attached to a function
690 SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
692 // Comment AST nodes that correspond to \c ParamVars for which we have
693 // found a \\param command or NULL if no documentation was found so far.
694 SmallVector<ParamCommandComment *, 8> ParamVarDocs;
696 ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
697 ParamVarDocs.resize(ParamVars.size(), NULL);
699 // First pass over all \\param commands: resolve all parameter names.
700 for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end();
702 ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
703 if (!PCC || !PCC->hasParamName())
705 StringRef ParamName = PCC->getParamNameAsWritten();
707 // Check that referenced parameter name is in the function decl.
708 const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
710 if (ResolvedParamIndex == ParamCommandComment::VarArgParamIndex) {
711 PCC->setIsVarArgParam();
714 if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) {
715 UnresolvedParamCommands.push_back(PCC);
718 PCC->setParamIndex(ResolvedParamIndex);
719 if (ParamVarDocs[ResolvedParamIndex]) {
720 SourceRange ArgRange = PCC->getParamNameRange();
721 Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
722 << ParamName << ArgRange;
723 ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
724 Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
725 << PrevCommand->getParamNameRange();
727 ParamVarDocs[ResolvedParamIndex] = PCC;
730 // Find parameter declarations that have no corresponding \\param.
731 SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
732 for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) {
733 if (!ParamVarDocs[i])
734 OrphanedParamDecls.push_back(ParamVars[i]);
737 // Second pass over unresolved \\param commands: do typo correction.
738 // Suggest corrections from a set of parameter declarations that have no
739 // corresponding \\param.
740 for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) {
741 const ParamCommandComment *PCC = UnresolvedParamCommands[i];
743 SourceRange ArgRange = PCC->getParamNameRange();
744 StringRef ParamName = PCC->getParamNameAsWritten();
745 Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
746 << ParamName << ArgRange;
748 // All parameters documented -- can't suggest a correction.
749 if (OrphanedParamDecls.size() == 0)
752 unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
753 if (OrphanedParamDecls.size() == 1) {
754 // If one parameter is not documented then that parameter is the only
755 // possible suggestion.
756 CorrectedParamIndex = 0;
758 // Do typo correction.
759 CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
762 if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
763 const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
764 if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
765 Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
766 << CorrectedII->getName()
767 << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
772 bool Sema::isFunctionDecl() {
775 if (!ThisDeclInfo->IsFilled)
777 return ThisDeclInfo->getKind() == DeclInfo::FunctionKind;
780 bool Sema::isAnyFunctionDecl() {
781 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
782 isa<FunctionDecl>(ThisDeclInfo->CurrentDecl);
785 bool Sema::isFunctionOrMethodVariadic() {
786 if (!isAnyFunctionDecl() && !isObjCMethodDecl())
788 if (const FunctionDecl *FD =
789 dyn_cast<FunctionDecl>(ThisDeclInfo->CurrentDecl))
790 return FD->isVariadic();
791 if (const ObjCMethodDecl *MD =
792 dyn_cast<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl))
793 return MD->isVariadic();
797 bool Sema::isObjCMethodDecl() {
798 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
799 isa<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl);
802 bool Sema::isFunctionPointerVarDecl() {
805 if (!ThisDeclInfo->IsFilled)
807 if (ThisDeclInfo->getKind() == DeclInfo::VariableKind) {
808 if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDeclInfo->CurrentDecl)) {
809 QualType QT = VD->getType();
810 return QT->isFunctionPointerType();
816 bool Sema::isObjCPropertyDecl() {
819 if (!ThisDeclInfo->IsFilled)
821 return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty;
824 bool Sema::isTemplateOrSpecialization() {
827 if (!ThisDeclInfo->IsFilled)
829 return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
832 bool Sema::isRecordLikeDecl() {
835 if (!ThisDeclInfo->IsFilled)
837 return isUnionDecl() || isClassOrStructDecl()
838 || isObjCInterfaceDecl() || isObjCProtocolDecl();
841 bool Sema::isUnionDecl() {
844 if (!ThisDeclInfo->IsFilled)
846 if (const RecordDecl *RD =
847 dyn_cast_or_null<RecordDecl>(ThisDeclInfo->CurrentDecl))
848 return RD->isUnion();
852 bool Sema::isClassOrStructDecl() {
855 if (!ThisDeclInfo->IsFilled)
857 return ThisDeclInfo->CurrentDecl &&
858 isa<RecordDecl>(ThisDeclInfo->CurrentDecl) &&
862 bool Sema::isClassTemplateDecl() {
865 if (!ThisDeclInfo->IsFilled)
867 return ThisDeclInfo->CurrentDecl &&
868 (isa<ClassTemplateDecl>(ThisDeclInfo->CurrentDecl));
871 bool Sema::isFunctionTemplateDecl() {
874 if (!ThisDeclInfo->IsFilled)
876 return ThisDeclInfo->CurrentDecl &&
877 (isa<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl));
880 bool Sema::isObjCInterfaceDecl() {
883 if (!ThisDeclInfo->IsFilled)
885 return ThisDeclInfo->CurrentDecl &&
886 isa<ObjCInterfaceDecl>(ThisDeclInfo->CurrentDecl);
889 bool Sema::isObjCProtocolDecl() {
892 if (!ThisDeclInfo->IsFilled)
894 return ThisDeclInfo->CurrentDecl &&
895 isa<ObjCProtocolDecl>(ThisDeclInfo->CurrentDecl);
898 ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
899 if (!ThisDeclInfo->IsFilled)
901 return ThisDeclInfo->ParamVars;
904 void Sema::inspectThisDecl() {
905 ThisDeclInfo->fill();
908 unsigned Sema::resolveParmVarReference(StringRef Name,
909 ArrayRef<const ParmVarDecl *> ParamVars) {
910 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
911 const IdentifierInfo *II = ParamVars[i]->getIdentifier();
912 if (II && II->getName() == Name)
915 if (Name == "..." && isFunctionOrMethodVariadic())
916 return ParamCommandComment::VarArgParamIndex;
917 return ParamCommandComment::InvalidParamIndex;
921 class SimpleTypoCorrector {
923 const unsigned MaxEditDistance;
925 const NamedDecl *BestDecl;
926 unsigned BestEditDistance;
931 SimpleTypoCorrector(StringRef Typo) :
932 Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
933 BestDecl(NULL), BestEditDistance(MaxEditDistance + 1),
934 BestIndex(0), NextIndex(0)
937 void addDecl(const NamedDecl *ND);
939 const NamedDecl *getBestDecl() const {
940 if (BestEditDistance > MaxEditDistance)
946 unsigned getBestDeclIndex() const {
947 assert(getBestDecl());
952 void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
953 unsigned CurrIndex = NextIndex++;
955 const IdentifierInfo *II = ND->getIdentifier();
959 StringRef Name = II->getName();
960 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
961 if (MinPossibleEditDistance > 0 &&
962 Typo.size() / MinPossibleEditDistance < 3)
965 unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
966 if (EditDistance < BestEditDistance) {
967 BestEditDistance = EditDistance;
969 BestIndex = CurrIndex;
972 } // unnamed namespace
974 unsigned Sema::correctTypoInParmVarReference(
976 ArrayRef<const ParmVarDecl *> ParamVars) {
977 SimpleTypoCorrector Corrector(Typo);
978 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i)
979 Corrector.addDecl(ParamVars[i]);
980 if (Corrector.getBestDecl())
981 return Corrector.getBestDeclIndex();
983 return ParamCommandComment::InvalidParamIndex;
987 bool ResolveTParamReferenceHelper(
989 const TemplateParameterList *TemplateParameters,
990 SmallVectorImpl<unsigned> *Position) {
991 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
992 const NamedDecl *Param = TemplateParameters->getParam(i);
993 const IdentifierInfo *II = Param->getIdentifier();
994 if (II && II->getName() == Name) {
995 Position->push_back(i);
999 if (const TemplateTemplateParmDecl *TTP =
1000 dyn_cast<TemplateTemplateParmDecl>(Param)) {
1001 Position->push_back(i);
1002 if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
1005 Position->pop_back();
1010 } // unnamed namespace
1012 bool Sema::resolveTParamReference(
1014 const TemplateParameterList *TemplateParameters,
1015 SmallVectorImpl<unsigned> *Position) {
1017 if (!TemplateParameters)
1020 return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
1024 void CorrectTypoInTParamReferenceHelper(
1025 const TemplateParameterList *TemplateParameters,
1026 SimpleTypoCorrector &Corrector) {
1027 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1028 const NamedDecl *Param = TemplateParameters->getParam(i);
1029 Corrector.addDecl(Param);
1031 if (const TemplateTemplateParmDecl *TTP =
1032 dyn_cast<TemplateTemplateParmDecl>(Param))
1033 CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
1037 } // unnamed namespace
1039 StringRef Sema::correctTypoInTParamReference(
1041 const TemplateParameterList *TemplateParameters) {
1042 SimpleTypoCorrector Corrector(Typo);
1043 CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
1044 if (const NamedDecl *ND = Corrector.getBestDecl()) {
1045 const IdentifierInfo *II = ND->getIdentifier();
1046 assert(II && "SimpleTypoCorrector should not return this decl");
1047 return II->getName();
1052 InlineCommandComment::RenderKind
1053 Sema::getInlineCommandRenderKind(StringRef Name) const {
1054 assert(Traits.getCommandInfo(Name)->IsInlineCommand);
1056 return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
1057 .Case("b", InlineCommandComment::RenderBold)
1058 .Cases("c", "p", InlineCommandComment::RenderMonospaced)
1059 .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
1060 .Default(InlineCommandComment::RenderNormal);
1063 } // end namespace comments
1064 } // end namespace clang