1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150 |
- //===--- CommentSema.cpp - Doxygen comment semantic analysis --------------===//
- //
- // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
- // See https://llvm.org/LICENSE.txt for license information.
- // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- //
- //===----------------------------------------------------------------------===//
- #include "clang/AST/CommentSema.h"
- #include "clang/AST/Attr.h"
- #include "clang/AST/CommentCommandTraits.h"
- #include "clang/AST/CommentDiagnostic.h"
- #include "clang/AST/Decl.h"
- #include "clang/AST/DeclTemplate.h"
- #include "clang/Basic/LLVM.h"
- #include "clang/Basic/SourceManager.h"
- #include "clang/Lex/Preprocessor.h"
- #include "llvm/ADT/SmallString.h"
- #include "llvm/ADT/StringSwitch.h"
- namespace clang {
- namespace comments {
- namespace {
- #include "clang/AST/CommentHTMLTagsProperties.inc"
- } // end anonymous namespace
- Sema::Sema(llvm::BumpPtrAllocator &Allocator, const SourceManager &SourceMgr,
- DiagnosticsEngine &Diags, CommandTraits &Traits,
- const Preprocessor *PP) :
- Allocator(Allocator), SourceMgr(SourceMgr), Diags(Diags), Traits(Traits),
- PP(PP), ThisDeclInfo(nullptr), BriefCommand(nullptr),
- HeaderfileCommand(nullptr) {
- }
- void Sema::setDecl(const Decl *D) {
- if (!D)
- return;
- ThisDeclInfo = new (Allocator) DeclInfo;
- ThisDeclInfo->CommentDecl = D;
- ThisDeclInfo->IsFilled = false;
- }
- ParagraphComment *Sema::actOnParagraphComment(
- ArrayRef<InlineContentComment *> Content) {
- return new (Allocator) ParagraphComment(Content);
- }
- BlockCommandComment *Sema::actOnBlockCommandStart(
- SourceLocation LocBegin,
- SourceLocation LocEnd,
- unsigned CommandID,
- CommandMarkerKind CommandMarker) {
- BlockCommandComment *BC = new (Allocator) BlockCommandComment(LocBegin, LocEnd,
- CommandID,
- CommandMarker);
- checkContainerDecl(BC);
- return BC;
- }
- void Sema::actOnBlockCommandArgs(BlockCommandComment *Command,
- ArrayRef<BlockCommandComment::Argument> Args) {
- Command->setArgs(Args);
- }
- void Sema::actOnBlockCommandFinish(BlockCommandComment *Command,
- ParagraphComment *Paragraph) {
- Command->setParagraph(Paragraph);
- checkBlockCommandEmptyParagraph(Command);
- checkBlockCommandDuplicate(Command);
- if (ThisDeclInfo) {
- // These checks only make sense if the comment is attached to a
- // declaration.
- checkReturnsCommand(Command);
- checkDeprecatedCommand(Command);
- }
- }
- ParamCommandComment *Sema::actOnParamCommandStart(
- SourceLocation LocBegin,
- SourceLocation LocEnd,
- unsigned CommandID,
- CommandMarkerKind CommandMarker) {
- ParamCommandComment *Command =
- new (Allocator) ParamCommandComment(LocBegin, LocEnd, CommandID,
- CommandMarker);
- if (!involvesFunctionType())
- Diag(Command->getLocation(),
- diag::warn_doc_param_not_attached_to_a_function_decl)
- << CommandMarker
- << Command->getCommandNameRange(Traits);
- return Command;
- }
- void Sema::checkFunctionDeclVerbatimLine(const BlockCommandComment *Comment) {
- const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
- if (!Info->IsFunctionDeclarationCommand)
- return;
- unsigned DiagSelect;
- switch (Comment->getCommandID()) {
- case CommandTraits::KCI_function:
- DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 1 : 0;
- break;
- case CommandTraits::KCI_functiongroup:
- DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 2 : 0;
- break;
- case CommandTraits::KCI_method:
- DiagSelect = !isObjCMethodDecl() ? 3 : 0;
- break;
- case CommandTraits::KCI_methodgroup:
- DiagSelect = !isObjCMethodDecl() ? 4 : 0;
- break;
- case CommandTraits::KCI_callback:
- DiagSelect = !isFunctionPointerVarDecl() ? 5 : 0;
- break;
- default:
- DiagSelect = 0;
- break;
- }
- if (DiagSelect)
- Diag(Comment->getLocation(), diag::warn_doc_function_method_decl_mismatch)
- << Comment->getCommandMarker()
- << (DiagSelect-1) << (DiagSelect-1)
- << Comment->getSourceRange();
- }
- void Sema::checkContainerDeclVerbatimLine(const BlockCommandComment *Comment) {
- const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
- if (!Info->IsRecordLikeDeclarationCommand)
- return;
- unsigned DiagSelect;
- switch (Comment->getCommandID()) {
- case CommandTraits::KCI_class:
- DiagSelect =
- (!isClassOrStructOrTagTypedefDecl() && !isClassTemplateDecl()) ? 1
- : 0;
- // Allow @class command on @interface declarations.
- // FIXME. Currently, \class and @class are indistinguishable. So,
- // \class is also allowed on an @interface declaration
- if (DiagSelect && Comment->getCommandMarker() && isObjCInterfaceDecl())
- DiagSelect = 0;
- break;
- case CommandTraits::KCI_interface:
- DiagSelect = !isObjCInterfaceDecl() ? 2 : 0;
- break;
- case CommandTraits::KCI_protocol:
- DiagSelect = !isObjCProtocolDecl() ? 3 : 0;
- break;
- case CommandTraits::KCI_struct:
- DiagSelect = !isClassOrStructOrTagTypedefDecl() ? 4 : 0;
- break;
- case CommandTraits::KCI_union:
- DiagSelect = !isUnionDecl() ? 5 : 0;
- break;
- default:
- DiagSelect = 0;
- break;
- }
- if (DiagSelect)
- Diag(Comment->getLocation(), diag::warn_doc_api_container_decl_mismatch)
- << Comment->getCommandMarker()
- << (DiagSelect-1) << (DiagSelect-1)
- << Comment->getSourceRange();
- }
- void Sema::checkContainerDecl(const BlockCommandComment *Comment) {
- const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
- if (!Info->IsRecordLikeDetailCommand || isRecordLikeDecl())
- return;
- unsigned DiagSelect;
- switch (Comment->getCommandID()) {
- case CommandTraits::KCI_classdesign:
- DiagSelect = 1;
- break;
- case CommandTraits::KCI_coclass:
- DiagSelect = 2;
- break;
- case CommandTraits::KCI_dependency:
- DiagSelect = 3;
- break;
- case CommandTraits::KCI_helper:
- DiagSelect = 4;
- break;
- case CommandTraits::KCI_helperclass:
- DiagSelect = 5;
- break;
- case CommandTraits::KCI_helps:
- DiagSelect = 6;
- break;
- case CommandTraits::KCI_instancesize:
- DiagSelect = 7;
- break;
- case CommandTraits::KCI_ownership:
- DiagSelect = 8;
- break;
- case CommandTraits::KCI_performance:
- DiagSelect = 9;
- break;
- case CommandTraits::KCI_security:
- DiagSelect = 10;
- break;
- case CommandTraits::KCI_superclass:
- DiagSelect = 11;
- break;
- default:
- DiagSelect = 0;
- break;
- }
- if (DiagSelect)
- Diag(Comment->getLocation(), diag::warn_doc_container_decl_mismatch)
- << Comment->getCommandMarker()
- << (DiagSelect-1)
- << Comment->getSourceRange();
- }
- /// Turn a string into the corresponding PassDirection or -1 if it's not
- /// valid.
- static int getParamPassDirection(StringRef Arg) {
- return llvm::StringSwitch<int>(Arg)
- .Case("[in]", ParamCommandComment::In)
- .Case("[out]", ParamCommandComment::Out)
- .Cases("[in,out]", "[out,in]", ParamCommandComment::InOut)
- .Default(-1);
- }
- void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command,
- SourceLocation ArgLocBegin,
- SourceLocation ArgLocEnd,
- StringRef Arg) {
- std::string ArgLower = Arg.lower();
- int Direction = getParamPassDirection(ArgLower);
- if (Direction == -1) {
- // Try again with whitespace removed.
- llvm::erase_if(ArgLower, clang::isWhitespace);
- Direction = getParamPassDirection(ArgLower);
- SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
- if (Direction != -1) {
- const char *FixedName = ParamCommandComment::getDirectionAsString(
- (ParamCommandComment::PassDirection)Direction);
- Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction)
- << ArgRange << FixItHint::CreateReplacement(ArgRange, FixedName);
- } else {
- Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction) << ArgRange;
- Direction = ParamCommandComment::In; // Sane fall back.
- }
- }
- Command->setDirection((ParamCommandComment::PassDirection)Direction,
- /*Explicit=*/true);
- }
- void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command,
- SourceLocation ArgLocBegin,
- SourceLocation ArgLocEnd,
- StringRef Arg) {
- // Parser will not feed us more arguments than needed.
- assert(Command->getNumArgs() == 0);
- if (!Command->isDirectionExplicit()) {
- // User didn't provide a direction argument.
- Command->setDirection(ParamCommandComment::In, /* Explicit = */ false);
- }
- typedef BlockCommandComment::Argument Argument;
- Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
- ArgLocEnd),
- Arg);
- Command->setArgs(llvm::makeArrayRef(A, 1));
- }
- void Sema::actOnParamCommandFinish(ParamCommandComment *Command,
- ParagraphComment *Paragraph) {
- Command->setParagraph(Paragraph);
- checkBlockCommandEmptyParagraph(Command);
- }
- TParamCommandComment *Sema::actOnTParamCommandStart(
- SourceLocation LocBegin,
- SourceLocation LocEnd,
- unsigned CommandID,
- CommandMarkerKind CommandMarker) {
- TParamCommandComment *Command =
- new (Allocator) TParamCommandComment(LocBegin, LocEnd, CommandID,
- CommandMarker);
- if (!isTemplateOrSpecialization())
- Diag(Command->getLocation(),
- diag::warn_doc_tparam_not_attached_to_a_template_decl)
- << CommandMarker
- << Command->getCommandNameRange(Traits);
- return Command;
- }
- void Sema::actOnTParamCommandParamNameArg(TParamCommandComment *Command,
- SourceLocation ArgLocBegin,
- SourceLocation ArgLocEnd,
- StringRef Arg) {
- // Parser will not feed us more arguments than needed.
- assert(Command->getNumArgs() == 0);
- typedef BlockCommandComment::Argument Argument;
- Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
- ArgLocEnd),
- Arg);
- Command->setArgs(llvm::makeArrayRef(A, 1));
- if (!isTemplateOrSpecialization()) {
- // We already warned that this \\tparam is not attached to a template decl.
- return;
- }
- const TemplateParameterList *TemplateParameters =
- ThisDeclInfo->TemplateParameters;
- SmallVector<unsigned, 2> Position;
- if (resolveTParamReference(Arg, TemplateParameters, &Position)) {
- Command->setPosition(copyArray(llvm::makeArrayRef(Position)));
- TParamCommandComment *&PrevCommand = TemplateParameterDocs[Arg];
- if (PrevCommand) {
- SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
- Diag(ArgLocBegin, diag::warn_doc_tparam_duplicate)
- << Arg << ArgRange;
- Diag(PrevCommand->getLocation(), diag::note_doc_tparam_previous)
- << PrevCommand->getParamNameRange();
- }
- PrevCommand = Command;
- return;
- }
- SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
- Diag(ArgLocBegin, diag::warn_doc_tparam_not_found)
- << Arg << ArgRange;
- if (!TemplateParameters || TemplateParameters->size() == 0)
- return;
- StringRef CorrectedName;
- if (TemplateParameters->size() == 1) {
- const NamedDecl *Param = TemplateParameters->getParam(0);
- const IdentifierInfo *II = Param->getIdentifier();
- if (II)
- CorrectedName = II->getName();
- } else {
- CorrectedName = correctTypoInTParamReference(Arg, TemplateParameters);
- }
- if (!CorrectedName.empty()) {
- Diag(ArgLocBegin, diag::note_doc_tparam_name_suggestion)
- << CorrectedName
- << FixItHint::CreateReplacement(ArgRange, CorrectedName);
- }
- }
- void Sema::actOnTParamCommandFinish(TParamCommandComment *Command,
- ParagraphComment *Paragraph) {
- Command->setParagraph(Paragraph);
- checkBlockCommandEmptyParagraph(Command);
- }
- InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
- SourceLocation CommandLocEnd,
- unsigned CommandID) {
- ArrayRef<InlineCommandComment::Argument> Args;
- StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
- return new (Allocator) InlineCommandComment(
- CommandLocBegin,
- CommandLocEnd,
- CommandID,
- getInlineCommandRenderKind(CommandName),
- Args);
- }
- InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
- SourceLocation CommandLocEnd,
- unsigned CommandID,
- SourceLocation ArgLocBegin,
- SourceLocation ArgLocEnd,
- StringRef Arg) {
- typedef InlineCommandComment::Argument Argument;
- Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
- ArgLocEnd),
- Arg);
- StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
- return new (Allocator) InlineCommandComment(
- CommandLocBegin,
- CommandLocEnd,
- CommandID,
- getInlineCommandRenderKind(CommandName),
- llvm::makeArrayRef(A, 1));
- }
- InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
- SourceLocation LocEnd,
- StringRef CommandName) {
- unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID();
- return actOnUnknownCommand(LocBegin, LocEnd, CommandID);
- }
- InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
- SourceLocation LocEnd,
- unsigned CommandID) {
- ArrayRef<InlineCommandComment::Argument> Args;
- return new (Allocator) InlineCommandComment(
- LocBegin, LocEnd, CommandID,
- InlineCommandComment::RenderNormal,
- Args);
- }
- TextComment *Sema::actOnText(SourceLocation LocBegin,
- SourceLocation LocEnd,
- StringRef Text) {
- return new (Allocator) TextComment(LocBegin, LocEnd, Text);
- }
- VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc,
- unsigned CommandID) {
- StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
- return new (Allocator) VerbatimBlockComment(
- Loc,
- Loc.getLocWithOffset(1 + CommandName.size()),
- CommandID);
- }
- VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc,
- StringRef Text) {
- return new (Allocator) VerbatimBlockLineComment(Loc, Text);
- }
- void Sema::actOnVerbatimBlockFinish(
- VerbatimBlockComment *Block,
- SourceLocation CloseNameLocBegin,
- StringRef CloseName,
- ArrayRef<VerbatimBlockLineComment *> Lines) {
- Block->setCloseName(CloseName, CloseNameLocBegin);
- Block->setLines(Lines);
- }
- VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin,
- unsigned CommandID,
- SourceLocation TextBegin,
- StringRef Text) {
- VerbatimLineComment *VL = new (Allocator) VerbatimLineComment(
- LocBegin,
- TextBegin.getLocWithOffset(Text.size()),
- CommandID,
- TextBegin,
- Text);
- checkFunctionDeclVerbatimLine(VL);
- checkContainerDeclVerbatimLine(VL);
- return VL;
- }
- HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin,
- StringRef TagName) {
- return new (Allocator) HTMLStartTagComment(LocBegin, TagName);
- }
- void Sema::actOnHTMLStartTagFinish(
- HTMLStartTagComment *Tag,
- ArrayRef<HTMLStartTagComment::Attribute> Attrs,
- SourceLocation GreaterLoc,
- bool IsSelfClosing) {
- Tag->setAttrs(Attrs);
- Tag->setGreaterLoc(GreaterLoc);
- if (IsSelfClosing)
- Tag->setSelfClosing();
- else if (!isHTMLEndTagForbidden(Tag->getTagName()))
- HTMLOpenTags.push_back(Tag);
- }
- HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin,
- SourceLocation LocEnd,
- StringRef TagName) {
- HTMLEndTagComment *HET =
- new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName);
- if (isHTMLEndTagForbidden(TagName)) {
- Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden)
- << TagName << HET->getSourceRange();
- HET->setIsMalformed();
- return HET;
- }
- bool FoundOpen = false;
- for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator
- I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend();
- I != E; ++I) {
- if ((*I)->getTagName() == TagName) {
- FoundOpen = true;
- break;
- }
- }
- if (!FoundOpen) {
- Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced)
- << HET->getSourceRange();
- HET->setIsMalformed();
- return HET;
- }
- while (!HTMLOpenTags.empty()) {
- HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val();
- StringRef LastNotClosedTagName = HST->getTagName();
- if (LastNotClosedTagName == TagName) {
- // If the start tag is malformed, end tag is malformed as well.
- if (HST->isMalformed())
- HET->setIsMalformed();
- break;
- }
- if (isHTMLEndTagOptional(LastNotClosedTagName))
- continue;
- bool OpenLineInvalid;
- const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
- HST->getLocation(),
- &OpenLineInvalid);
- bool CloseLineInvalid;
- const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
- HET->getLocation(),
- &CloseLineInvalid);
- if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine) {
- Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
- << HST->getTagName() << HET->getTagName()
- << HST->getSourceRange() << HET->getSourceRange();
- HST->setIsMalformed();
- } else {
- Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
- << HST->getTagName() << HET->getTagName()
- << HST->getSourceRange();
- Diag(HET->getLocation(), diag::note_doc_html_end_tag)
- << HET->getSourceRange();
- HST->setIsMalformed();
- }
- }
- return HET;
- }
- FullComment *Sema::actOnFullComment(
- ArrayRef<BlockContentComment *> Blocks) {
- FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo);
- resolveParamCommandIndexes(FC);
- // Complain about HTML tags that are not closed.
- while (!HTMLOpenTags.empty()) {
- HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val();
- if (isHTMLEndTagOptional(HST->getTagName()))
- continue;
- Diag(HST->getLocation(), diag::warn_doc_html_missing_end_tag)
- << HST->getTagName() << HST->getSourceRange();
- HST->setIsMalformed();
- }
- return FC;
- }
- void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) {
- if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed)
- return;
- ParagraphComment *Paragraph = Command->getParagraph();
- if (Paragraph->isWhitespace()) {
- SourceLocation DiagLoc;
- if (Command->getNumArgs() > 0)
- DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
- if (!DiagLoc.isValid())
- DiagLoc = Command->getCommandNameRange(Traits).getEnd();
- Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
- << Command->getCommandMarker()
- << Command->getCommandName(Traits)
- << Command->getSourceRange();
- }
- }
- void Sema::checkReturnsCommand(const BlockCommandComment *Command) {
- if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
- return;
- assert(ThisDeclInfo && "should not call this check on a bare comment");
- // We allow the return command for all @properties because it can be used
- // to document the value that the property getter returns.
- if (isObjCPropertyDecl())
- return;
- if (involvesFunctionType()) {
- assert(!ThisDeclInfo->ReturnType.isNull() &&
- "should have a valid return type");
- if (ThisDeclInfo->ReturnType->isVoidType()) {
- unsigned DiagKind;
- switch (ThisDeclInfo->CommentDecl->getKind()) {
- default:
- if (ThisDeclInfo->IsObjCMethod)
- DiagKind = 3;
- else
- DiagKind = 0;
- break;
- case Decl::CXXConstructor:
- DiagKind = 1;
- break;
- case Decl::CXXDestructor:
- DiagKind = 2;
- break;
- }
- Diag(Command->getLocation(),
- diag::warn_doc_returns_attached_to_a_void_function)
- << Command->getCommandMarker()
- << Command->getCommandName(Traits)
- << DiagKind
- << Command->getSourceRange();
- }
- return;
- }
- Diag(Command->getLocation(),
- diag::warn_doc_returns_not_attached_to_a_function_decl)
- << Command->getCommandMarker()
- << Command->getCommandName(Traits)
- << Command->getSourceRange();
- }
- void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) {
- const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
- const BlockCommandComment *PrevCommand = nullptr;
- if (Info->IsBriefCommand) {
- if (!BriefCommand) {
- BriefCommand = Command;
- return;
- }
- PrevCommand = BriefCommand;
- } else if (Info->IsHeaderfileCommand) {
- if (!HeaderfileCommand) {
- HeaderfileCommand = Command;
- return;
- }
- PrevCommand = HeaderfileCommand;
- } else {
- // We don't want to check this command for duplicates.
- return;
- }
- StringRef CommandName = Command->getCommandName(Traits);
- StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
- Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
- << Command->getCommandMarker()
- << CommandName
- << Command->getSourceRange();
- if (CommandName == PrevCommandName)
- Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
- << PrevCommand->getCommandMarker()
- << PrevCommandName
- << PrevCommand->getSourceRange();
- else
- Diag(PrevCommand->getLocation(),
- diag::note_doc_block_command_previous_alias)
- << PrevCommand->getCommandMarker()
- << PrevCommandName
- << CommandName;
- }
- void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) {
- if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand)
- return;
- assert(ThisDeclInfo && "should not call this check on a bare comment");
- const Decl *D = ThisDeclInfo->CommentDecl;
- if (!D)
- return;
- if (D->hasAttr<DeprecatedAttr>() ||
- D->hasAttr<AvailabilityAttr>() ||
- D->hasAttr<UnavailableAttr>())
- return;
- Diag(Command->getLocation(), diag::warn_doc_deprecated_not_sync)
- << Command->getSourceRange() << Command->getCommandMarker();
- // Try to emit a fixit with a deprecation attribute.
- if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
- // Don't emit a Fix-It for non-member function definitions. GCC does not
- // accept attributes on them.
- const DeclContext *Ctx = FD->getDeclContext();
- if ((!Ctx || !Ctx->isRecord()) &&
- FD->doesThisDeclarationHaveABody())
- return;
- const LangOptions &LO = FD->getLangOpts();
- const bool DoubleSquareBracket = LO.CPlusPlus14 || LO.C2x;
- StringRef AttributeSpelling =
- DoubleSquareBracket ? "[[deprecated]]" : "__attribute__((deprecated))";
- if (PP) {
- // Try to find a replacement macro:
- // - In C2x/C++14 we prefer [[deprecated]].
- // - If not found or an older C/C++ look for __attribute__((deprecated)).
- StringRef MacroName;
- if (DoubleSquareBracket) {
- TokenValue Tokens[] = {tok::l_square, tok::l_square,
- PP->getIdentifierInfo("deprecated"),
- tok::r_square, tok::r_square};
- MacroName = PP->getLastMacroWithSpelling(FD->getLocation(), Tokens);
- if (!MacroName.empty())
- AttributeSpelling = MacroName;
- }
- if (MacroName.empty()) {
- TokenValue Tokens[] = {
- tok::kw___attribute, tok::l_paren,
- tok::l_paren, PP->getIdentifierInfo("deprecated"),
- tok::r_paren, tok::r_paren};
- StringRef MacroName =
- PP->getLastMacroWithSpelling(FD->getLocation(), Tokens);
- if (!MacroName.empty())
- AttributeSpelling = MacroName;
- }
- }
- SmallString<64> TextToInsert = AttributeSpelling;
- TextToInsert += " ";
- SourceLocation Loc = FD->getSourceRange().getBegin();
- Diag(Loc, diag::note_add_deprecation_attr)
- << FixItHint::CreateInsertion(Loc, TextToInsert);
- }
- }
- void Sema::resolveParamCommandIndexes(const FullComment *FC) {
- if (!involvesFunctionType()) {
- // We already warned that \\param commands are not attached to a function
- // decl.
- return;
- }
- SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
- // Comment AST nodes that correspond to \c ParamVars for which we have
- // found a \\param command or NULL if no documentation was found so far.
- SmallVector<ParamCommandComment *, 8> ParamVarDocs;
- ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
- ParamVarDocs.resize(ParamVars.size(), nullptr);
- // First pass over all \\param commands: resolve all parameter names.
- for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end();
- I != E; ++I) {
- ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
- if (!PCC || !PCC->hasParamName())
- continue;
- StringRef ParamName = PCC->getParamNameAsWritten();
- // Check that referenced parameter name is in the function decl.
- const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
- ParamVars);
- if (ResolvedParamIndex == ParamCommandComment::VarArgParamIndex) {
- PCC->setIsVarArgParam();
- continue;
- }
- if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) {
- UnresolvedParamCommands.push_back(PCC);
- continue;
- }
- PCC->setParamIndex(ResolvedParamIndex);
- if (ParamVarDocs[ResolvedParamIndex]) {
- SourceRange ArgRange = PCC->getParamNameRange();
- Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
- << ParamName << ArgRange;
- ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
- Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
- << PrevCommand->getParamNameRange();
- }
- ParamVarDocs[ResolvedParamIndex] = PCC;
- }
- // Find parameter declarations that have no corresponding \\param.
- SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
- for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) {
- if (!ParamVarDocs[i])
- OrphanedParamDecls.push_back(ParamVars[i]);
- }
- // Second pass over unresolved \\param commands: do typo correction.
- // Suggest corrections from a set of parameter declarations that have no
- // corresponding \\param.
- for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) {
- const ParamCommandComment *PCC = UnresolvedParamCommands[i];
- SourceRange ArgRange = PCC->getParamNameRange();
- StringRef ParamName = PCC->getParamNameAsWritten();
- Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
- << ParamName << ArgRange;
- // All parameters documented -- can't suggest a correction.
- if (OrphanedParamDecls.size() == 0)
- continue;
- unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
- if (OrphanedParamDecls.size() == 1) {
- // If one parameter is not documented then that parameter is the only
- // possible suggestion.
- CorrectedParamIndex = 0;
- } else {
- // Do typo correction.
- CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
- OrphanedParamDecls);
- }
- if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
- const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
- if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
- Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
- << CorrectedII->getName()
- << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
- }
- }
- }
- bool Sema::involvesFunctionType() {
- if (!ThisDeclInfo)
- return false;
- if (!ThisDeclInfo->IsFilled)
- inspectThisDecl();
- return ThisDeclInfo->involvesFunctionType();
- }
- bool Sema::isFunctionDecl() {
- if (!ThisDeclInfo)
- return false;
- if (!ThisDeclInfo->IsFilled)
- inspectThisDecl();
- return ThisDeclInfo->getKind() == DeclInfo::FunctionKind;
- }
- bool Sema::isAnyFunctionDecl() {
- return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
- isa<FunctionDecl>(ThisDeclInfo->CurrentDecl);
- }
- bool Sema::isFunctionOrMethodVariadic() {
- if (!ThisDeclInfo)
- return false;
- if (!ThisDeclInfo->IsFilled)
- inspectThisDecl();
- return ThisDeclInfo->IsVariadic;
- }
- bool Sema::isObjCMethodDecl() {
- return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
- isa<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl);
- }
- bool Sema::isFunctionPointerVarDecl() {
- if (!ThisDeclInfo)
- return false;
- if (!ThisDeclInfo->IsFilled)
- inspectThisDecl();
- if (ThisDeclInfo->getKind() == DeclInfo::VariableKind) {
- if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDeclInfo->CurrentDecl)) {
- QualType QT = VD->getType();
- return QT->isFunctionPointerType();
- }
- }
- return false;
- }
- bool Sema::isObjCPropertyDecl() {
- if (!ThisDeclInfo)
- return false;
- if (!ThisDeclInfo->IsFilled)
- inspectThisDecl();
- return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty;
- }
- bool Sema::isTemplateOrSpecialization() {
- if (!ThisDeclInfo)
- return false;
- if (!ThisDeclInfo->IsFilled)
- inspectThisDecl();
- return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
- }
- bool Sema::isRecordLikeDecl() {
- if (!ThisDeclInfo)
- return false;
- if (!ThisDeclInfo->IsFilled)
- inspectThisDecl();
- return isUnionDecl() || isClassOrStructDecl() || isObjCInterfaceDecl() ||
- isObjCProtocolDecl();
- }
- bool Sema::isUnionDecl() {
- if (!ThisDeclInfo)
- return false;
- if (!ThisDeclInfo->IsFilled)
- inspectThisDecl();
- if (const RecordDecl *RD =
- dyn_cast_or_null<RecordDecl>(ThisDeclInfo->CurrentDecl))
- return RD->isUnion();
- return false;
- }
- static bool isClassOrStructDeclImpl(const Decl *D) {
- if (auto *record = dyn_cast_or_null<RecordDecl>(D))
- return !record->isUnion();
- return false;
- }
- bool Sema::isClassOrStructDecl() {
- if (!ThisDeclInfo)
- return false;
- if (!ThisDeclInfo->IsFilled)
- inspectThisDecl();
- if (!ThisDeclInfo->CurrentDecl)
- return false;
- return isClassOrStructDeclImpl(ThisDeclInfo->CurrentDecl);
- }
- bool Sema::isClassOrStructOrTagTypedefDecl() {
- if (!ThisDeclInfo)
- return false;
- if (!ThisDeclInfo->IsFilled)
- inspectThisDecl();
- if (!ThisDeclInfo->CurrentDecl)
- return false;
- if (isClassOrStructDeclImpl(ThisDeclInfo->CurrentDecl))
- return true;
- if (auto *ThisTypedefDecl = dyn_cast<TypedefDecl>(ThisDeclInfo->CurrentDecl)) {
- auto UnderlyingType = ThisTypedefDecl->getUnderlyingType();
- if (auto ThisElaboratedType = dyn_cast<ElaboratedType>(UnderlyingType)) {
- auto DesugaredType = ThisElaboratedType->desugar();
- if (auto *DesugaredTypePtr = DesugaredType.getTypePtrOrNull()) {
- if (auto *ThisRecordType = dyn_cast<RecordType>(DesugaredTypePtr)) {
- return isClassOrStructDeclImpl(ThisRecordType->getAsRecordDecl());
- }
- }
- }
- }
- return false;
- }
- bool Sema::isClassTemplateDecl() {
- if (!ThisDeclInfo)
- return false;
- if (!ThisDeclInfo->IsFilled)
- inspectThisDecl();
- return ThisDeclInfo->CurrentDecl &&
- (isa<ClassTemplateDecl>(ThisDeclInfo->CurrentDecl));
- }
- bool Sema::isFunctionTemplateDecl() {
- if (!ThisDeclInfo)
- return false;
- if (!ThisDeclInfo->IsFilled)
- inspectThisDecl();
- return ThisDeclInfo->CurrentDecl &&
- (isa<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl));
- }
- bool Sema::isObjCInterfaceDecl() {
- if (!ThisDeclInfo)
- return false;
- if (!ThisDeclInfo->IsFilled)
- inspectThisDecl();
- return ThisDeclInfo->CurrentDecl &&
- isa<ObjCInterfaceDecl>(ThisDeclInfo->CurrentDecl);
- }
- bool Sema::isObjCProtocolDecl() {
- if (!ThisDeclInfo)
- return false;
- if (!ThisDeclInfo->IsFilled)
- inspectThisDecl();
- return ThisDeclInfo->CurrentDecl &&
- isa<ObjCProtocolDecl>(ThisDeclInfo->CurrentDecl);
- }
- ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
- if (!ThisDeclInfo->IsFilled)
- inspectThisDecl();
- return ThisDeclInfo->ParamVars;
- }
- void Sema::inspectThisDecl() {
- ThisDeclInfo->fill();
- }
- unsigned Sema::resolveParmVarReference(StringRef Name,
- ArrayRef<const ParmVarDecl *> ParamVars) {
- for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
- const IdentifierInfo *II = ParamVars[i]->getIdentifier();
- if (II && II->getName() == Name)
- return i;
- }
- if (Name == "..." && isFunctionOrMethodVariadic())
- return ParamCommandComment::VarArgParamIndex;
- return ParamCommandComment::InvalidParamIndex;
- }
- namespace {
- class SimpleTypoCorrector {
- const NamedDecl *BestDecl;
- StringRef Typo;
- const unsigned MaxEditDistance;
- unsigned BestEditDistance;
- unsigned BestIndex;
- unsigned NextIndex;
- public:
- explicit SimpleTypoCorrector(StringRef Typo)
- : BestDecl(nullptr), Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
- BestEditDistance(MaxEditDistance + 1), BestIndex(0), NextIndex(0) {}
- void addDecl(const NamedDecl *ND);
- const NamedDecl *getBestDecl() const {
- if (BestEditDistance > MaxEditDistance)
- return nullptr;
- return BestDecl;
- }
- unsigned getBestDeclIndex() const {
- assert(getBestDecl());
- return BestIndex;
- }
- };
- void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
- unsigned CurrIndex = NextIndex++;
- const IdentifierInfo *II = ND->getIdentifier();
- if (!II)
- return;
- StringRef Name = II->getName();
- unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
- if (MinPossibleEditDistance > 0 &&
- Typo.size() / MinPossibleEditDistance < 3)
- return;
- unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
- if (EditDistance < BestEditDistance) {
- BestEditDistance = EditDistance;
- BestDecl = ND;
- BestIndex = CurrIndex;
- }
- }
- } // end anonymous namespace
- unsigned Sema::correctTypoInParmVarReference(
- StringRef Typo,
- ArrayRef<const ParmVarDecl *> ParamVars) {
- SimpleTypoCorrector Corrector(Typo);
- for (unsigned i = 0, e = ParamVars.size(); i != e; ++i)
- Corrector.addDecl(ParamVars[i]);
- if (Corrector.getBestDecl())
- return Corrector.getBestDeclIndex();
- else
- return ParamCommandComment::InvalidParamIndex;
- }
- namespace {
- bool ResolveTParamReferenceHelper(
- StringRef Name,
- const TemplateParameterList *TemplateParameters,
- SmallVectorImpl<unsigned> *Position) {
- for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
- const NamedDecl *Param = TemplateParameters->getParam(i);
- const IdentifierInfo *II = Param->getIdentifier();
- if (II && II->getName() == Name) {
- Position->push_back(i);
- return true;
- }
- if (const TemplateTemplateParmDecl *TTP =
- dyn_cast<TemplateTemplateParmDecl>(Param)) {
- Position->push_back(i);
- if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
- Position))
- return true;
- Position->pop_back();
- }
- }
- return false;
- }
- } // end anonymous namespace
- bool Sema::resolveTParamReference(
- StringRef Name,
- const TemplateParameterList *TemplateParameters,
- SmallVectorImpl<unsigned> *Position) {
- Position->clear();
- if (!TemplateParameters)
- return false;
- return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
- }
- namespace {
- void CorrectTypoInTParamReferenceHelper(
- const TemplateParameterList *TemplateParameters,
- SimpleTypoCorrector &Corrector) {
- for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
- const NamedDecl *Param = TemplateParameters->getParam(i);
- Corrector.addDecl(Param);
- if (const TemplateTemplateParmDecl *TTP =
- dyn_cast<TemplateTemplateParmDecl>(Param))
- CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
- Corrector);
- }
- }
- } // end anonymous namespace
- StringRef Sema::correctTypoInTParamReference(
- StringRef Typo,
- const TemplateParameterList *TemplateParameters) {
- SimpleTypoCorrector Corrector(Typo);
- CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
- if (const NamedDecl *ND = Corrector.getBestDecl()) {
- const IdentifierInfo *II = ND->getIdentifier();
- assert(II && "SimpleTypoCorrector should not return this decl");
- return II->getName();
- }
- return StringRef();
- }
- InlineCommandComment::RenderKind
- Sema::getInlineCommandRenderKind(StringRef Name) const {
- assert(Traits.getCommandInfo(Name)->IsInlineCommand);
- return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
- .Case("b", InlineCommandComment::RenderBold)
- .Cases("c", "p", InlineCommandComment::RenderMonospaced)
- .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
- .Case("anchor", InlineCommandComment::RenderAnchor)
- .Default(InlineCommandComment::RenderNormal);
- }
- } // end namespace comments
- } // end namespace clang
|