19#include "llvm/ADT/StringSwitch.h"
25#include "clang/AST/CommentHTMLTagsProperties.inc"
28Sema::Sema(llvm::BumpPtrAllocator &Allocator,
const SourceManager &SourceMgr,
31 Allocator(Allocator), SourceMgr(SourceMgr), Diags(Diags), Traits(Traits),
40 ThisDeclInfo =
new (Allocator)
DeclInfo;
42 ThisDeclInfo->IsFilled =
false;
58 checkContainerDecl(BC);
70 checkBlockCommandEmptyParagraph(Command);
71 checkBlockCommandDuplicate(Command);
75 checkReturnsCommand(Command);
76 checkDeprecatedCommand(Command);
89 if (!involvesFunctionType())
91 diag::warn_doc_param_not_attached_to_a_function_decl)
103 std::optional<unsigned> DiagSelect;
104 switch (
Comment->getCommandID()) {
105 case CommandTraits::KCI_function:
106 if (!isAnyFunctionDecl() && !isFunctionTemplateDecl())
107 DiagSelect = diag::CallableKind::Function;
109 case CommandTraits::KCI_functiongroup:
110 if (!isAnyFunctionDecl() && !isFunctionTemplateDecl())
111 DiagSelect = diag::CallableKind::FunctionGroup;
113 case CommandTraits::KCI_method:
114 DiagSelect = diag::CallableKind::Method;
116 case CommandTraits::KCI_methodgroup:
117 DiagSelect = diag::CallableKind::MethodGroup;
119 case CommandTraits::KCI_callback:
120 DiagSelect = diag::CallableKind::Callback;
124 Diag(Comment->
getLocation(), diag::warn_doc_function_method_decl_mismatch)
129void Sema::checkContainerDeclVerbatimLine(
const BlockCommandComment *Comment) {
130 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
131 if (!Info->IsRecordLikeDeclarationCommand)
133 std::optional<unsigned> DiagSelect;
134 switch (Comment->getCommandID()) {
135 case CommandTraits::KCI_class:
136 if (!isClassOrStructOrTagTypedefDecl() && !isClassTemplateDecl())
137 DiagSelect = diag::DeclContainerKind::Class;
142 if (DiagSelect && Comment->getCommandMarker() && isObjCInterfaceDecl())
143 DiagSelect = std::nullopt;
145 case CommandTraits::KCI_interface:
146 if (!isObjCInterfaceDecl())
147 DiagSelect = diag::DeclContainerKind::Interface;
149 case CommandTraits::KCI_protocol:
150 if (!isObjCProtocolDecl())
151 DiagSelect = diag::DeclContainerKind::Protocol;
153 case CommandTraits::KCI_struct:
154 if (!isClassOrStructOrTagTypedefDecl())
155 DiagSelect = diag::DeclContainerKind::Struct;
157 case CommandTraits::KCI_union:
159 DiagSelect = diag::DeclContainerKind::Union;
162 DiagSelect = std::nullopt;
166 Diag(Comment->getLocation(), diag::warn_doc_api_container_decl_mismatch)
167 << Comment->getCommandMarker() << (*DiagSelect) << (*DiagSelect)
168 << Comment->getSourceRange();
172 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
173 if (!Info->IsRecordLikeDetailCommand || isRecordLikeDecl())
175 std::optional<unsigned> DiagSelect;
176 switch (Comment->getCommandID()) {
177 case CommandTraits::KCI_classdesign:
178 DiagSelect = diag::DocCommandKind::ClassDesign;
180 case CommandTraits::KCI_coclass:
181 DiagSelect = diag::DocCommandKind::CoClass;
183 case CommandTraits::KCI_dependency:
184 DiagSelect = diag::DocCommandKind::Dependency;
186 case CommandTraits::KCI_helper:
187 DiagSelect = diag::DocCommandKind::Helper;
189 case CommandTraits::KCI_helperclass:
190 DiagSelect = diag::DocCommandKind::HelperClass;
192 case CommandTraits::KCI_helps:
193 DiagSelect = diag::DocCommandKind::Helps;
195 case CommandTraits::KCI_instancesize:
196 DiagSelect = diag::DocCommandKind::InstanceSize;
198 case CommandTraits::KCI_ownership:
199 DiagSelect = diag::DocCommandKind::Ownership;
201 case CommandTraits::KCI_performance:
202 DiagSelect = diag::DocCommandKind::Performance;
204 case CommandTraits::KCI_security:
205 DiagSelect = diag::DocCommandKind::Security;
207 case CommandTraits::KCI_superclass:
208 DiagSelect = diag::DocCommandKind::Superclass;
211 DiagSelect = std::nullopt;
215 Diag(Comment->getLocation(), diag::warn_doc_container_decl_mismatch)
216 << Comment->getCommandMarker() << (*DiagSelect)
217 << Comment->getSourceRange();
223 return llvm::StringSwitch<ParamCommandPassDirection>(Arg)
234 std::string ArgLower = Arg.lower();
244 const char *FixedName =
246 Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction)
249 Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction) << ArgRange;
269 auto *A =
new (Allocator)
277 checkBlockCommandEmptyParagraph(Command);
289 if (isExplicitFunctionTemplateInstantiation()) {
295 if (!isTemplateOrSpecialization())
297 diag::warn_doc_tparam_not_attached_to_a_template_decl)
311 auto *A =
new (Allocator)
315 if (!isTemplateOrSpecialization()) {
321 ThisDeclInfo->TemplateParameters;
323 if (resolveTParamReference(Arg, TemplateParameters, &Position)) {
328 Diag(ArgLocBegin, diag::warn_doc_tparam_duplicate)
330 Diag(PrevCommand->
getLocation(), diag::note_doc_tparam_previous)
333 PrevCommand = Command;
338 Diag(ArgLocBegin, diag::warn_doc_tparam_not_found)
341 if (!TemplateParameters || TemplateParameters->
size() == 0)
344 StringRef CorrectedName;
345 if (TemplateParameters->
size() == 1) {
351 CorrectedName = correctTypoInTParamReference(Arg, TemplateParameters);
354 if (!CorrectedName.empty()) {
355 Diag(ArgLocBegin, diag::note_doc_tparam_name_suggestion)
364 checkBlockCommandEmptyParagraph(Command);
372 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
375 CommandLocBegin, CommandLocEnd, CommandID,
376 getInlineCommandRenderKind(CommandName), CommandMarker, Args);
381 StringRef CommandName) {
382 unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID();
388 unsigned CommandID) {
401 unsigned CommandID) {
402 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
419 Block->setCloseName(CloseName, CloseNameLocBegin);
420 Block->setLines(Lines);
433 checkFunctionDeclVerbatimLine(VL);
434 checkContainerDeclVerbatimLine(VL);
447 bool IsSelfClosing) {
449 Tag->setGreaterLoc(GreaterLoc);
451 Tag->setSelfClosing();
452 else if (!isHTMLEndTagForbidden(Tag->getTagName()))
453 HTMLOpenTags.push_back(Tag);
461 if (isHTMLEndTagForbidden(TagName)) {
462 Diag(HET->
getLocation(), diag::warn_doc_html_end_forbidden)
468 bool FoundOpen =
false;
470 I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend();
472 if ((*I)->getTagName() == TagName) {
478 Diag(HET->
getLocation(), diag::warn_doc_html_end_unbalanced)
484 while (!HTMLOpenTags.empty()) {
486 StringRef LastNotClosedTagName = HST->
getTagName();
487 if (LastNotClosedTagName == TagName) {
494 if (isHTMLEndTagOptional(LastNotClosedTagName))
497 bool OpenLineInvalid;
498 const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
501 bool CloseLineInvalid;
502 const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
506 if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine) {
507 Diag(HST->
getLocation(), diag::warn_doc_html_start_end_mismatch)
512 Diag(HST->
getLocation(), diag::warn_doc_html_start_end_mismatch)
515 Diag(HET->
getLocation(), diag::note_doc_html_end_tag)
527 resolveParamCommandIndexes(FC);
530 while (!HTMLOpenTags.empty()) {
535 Diag(HST->
getLocation(), diag::warn_doc_html_missing_end_tag)
544 if (Traits.getCommandInfo(Command->
getCommandID())->IsEmptyParagraphAllowed)
554 Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
561void Sema::checkReturnsCommand(
const BlockCommandComment *Command) {
562 if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
565 assert(ThisDeclInfo &&
"should not call this check on a bare comment");
569 if (isObjCPropertyDecl())
571 if (involvesFunctionType()) {
573 "should have a valid return type");
583 case Decl::CXXConstructor:
586 case Decl::CXXDestructor:
590 Diag(Command->getLocation(),
591 diag::warn_doc_returns_attached_to_a_void_function)
592 << Command->getCommandMarker()
593 << Command->getCommandName(Traits)
595 << Command->getSourceRange();
600 Diag(Command->getLocation(),
601 diag::warn_doc_returns_not_attached_to_a_function_decl)
602 << Command->getCommandMarker()
603 << Command->getCommandName(Traits)
604 << Command->getSourceRange();
608 const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
609 const BlockCommandComment *PrevCommand =
nullptr;
610 if (Info->IsBriefCommand) {
612 BriefCommand = Command;
615 PrevCommand = BriefCommand;
616 }
else if (Info->IsHeaderfileCommand) {
617 if (!HeaderfileCommand) {
618 HeaderfileCommand = Command;
621 PrevCommand = HeaderfileCommand;
626 StringRef CommandName = Command->getCommandName(Traits);
627 StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
628 Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
629 << Command->getCommandMarker()
631 << Command->getSourceRange();
632 if (CommandName == PrevCommandName)
633 Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
634 << PrevCommand->getCommandMarker()
636 << PrevCommand->getSourceRange();
638 Diag(PrevCommand->getLocation(),
639 diag::note_doc_block_command_previous_alias)
640 << PrevCommand->getCommandMarker()
646 if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand)
649 assert(ThisDeclInfo &&
"should not call this check on a bare comment");
651 const Decl *D = ThisDeclInfo->CommentDecl;
655 if (D->hasAttr<DeprecatedAttr>() ||
656 D->hasAttr<AvailabilityAttr>() ||
657 D->hasAttr<UnavailableAttr>())
660 Diag(Command->getLocation(), diag::warn_doc_deprecated_not_sync)
661 << Command->getSourceRange() << Command->getCommandMarker();
664 if (
const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
667 const DeclContext *Ctx = FD->getDeclContext();
668 if ((!Ctx || !Ctx->isRecord()) &&
669 FD->doesThisDeclarationHaveABody())
672 const LangOptions &LO = FD->getLangOpts();
673 const bool DoubleSquareBracket = LO.CPlusPlus14 || LO.C23;
674 StringRef AttributeSpelling =
675 DoubleSquareBracket ?
"[[deprecated]]" :
"__attribute__((deprecated))";
681 if (DoubleSquareBracket) {
682 TokenValue Tokens[] = {tok::l_square, tok::l_square,
683 PP->getIdentifierInfo(
"deprecated"),
684 tok::r_square, tok::r_square};
685 MacroName = PP->getLastMacroWithSpelling(FD->getLocation(), Tokens);
686 if (!MacroName.empty())
687 AttributeSpelling = MacroName;
690 if (MacroName.empty()) {
691 TokenValue Tokens[] = {
692 tok::kw___attribute, tok::l_paren,
693 tok::l_paren, PP->getIdentifierInfo(
"deprecated"),
694 tok::r_paren, tok::r_paren};
695 StringRef MacroName =
696 PP->getLastMacroWithSpelling(FD->getLocation(), Tokens);
697 if (!MacroName.empty())
698 AttributeSpelling = MacroName;
702 SmallString<64> TextToInsert = AttributeSpelling;
704 SourceLocation Loc = FD->getSourceRange().getBegin();
705 Diag(Loc, diag::note_add_deprecation_attr)
710void Sema::resolveParamCommandIndexes(
const FullComment *FC) {
711 if (!involvesFunctionType()) {
717 SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
721 SmallVector<ParamCommandComment *, 8> ParamVarDocs;
723 ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
724 ParamVarDocs.resize(ParamVars.size(),
nullptr);
729 ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
730 if (!PCC || !PCC->hasParamName())
732 StringRef ParamName = PCC->getParamNameAsWritten();
735 const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
738 PCC->setIsVarArgParam();
742 UnresolvedParamCommands.push_back(PCC);
745 PCC->setParamIndex(ResolvedParamIndex);
746 if (ParamVarDocs[ResolvedParamIndex]) {
747 SourceRange ArgRange = PCC->getParamNameRange();
748 Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
749 << ParamName << ArgRange;
750 ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
751 Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
752 << PrevCommand->getParamNameRange();
754 ParamVarDocs[ResolvedParamIndex] = PCC;
758 SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
759 for (
unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) {
760 if (!ParamVarDocs[i])
761 OrphanedParamDecls.push_back(ParamVars[i]);
767 for (
unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) {
768 const ParamCommandComment *PCC = UnresolvedParamCommands[i];
770 SourceRange ArgRange = PCC->getParamNameRange();
771 StringRef ParamName = PCC->getParamNameAsWritten();
772 Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
773 << ParamName << ArgRange;
776 if (OrphanedParamDecls.size() == 0)
780 if (OrphanedParamDecls.size() == 1) {
783 CorrectedParamIndex = 0;
786 CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
790 const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
791 if (
const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
792 Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
793 << CorrectedII->getName()
799bool Sema::involvesFunctionType() {
802 if (!ThisDeclInfo->IsFilled)
804 return ThisDeclInfo->involvesFunctionType();
807bool Sema::isFunctionDecl() {
810 if (!ThisDeclInfo->IsFilled)
815bool Sema::isAnyFunctionDecl() {
816 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
820bool Sema::isFunctionOrMethodVariadic() {
823 if (!ThisDeclInfo->IsFilled)
825 return ThisDeclInfo->IsVariadic;
828bool Sema::isObjCMethodDecl() {
829 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
833bool Sema::isFunctionPointerVarDecl() {
836 if (!ThisDeclInfo->IsFilled)
839 if (
const VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDeclInfo->CurrentDecl)) {
840 QualType QT = VD->getType();
841 return QT->isFunctionPointerType();
847bool Sema::isObjCPropertyDecl() {
850 if (!ThisDeclInfo->IsFilled)
852 return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty;
855bool Sema::isTemplateOrSpecialization() {
858 if (!ThisDeclInfo->IsFilled)
863bool Sema::isExplicitFunctionTemplateInstantiation() {
866 if (!ThisDeclInfo->IsFilled)
868 if (
const auto *FD = dyn_cast<FunctionDecl>(ThisDeclInfo->CurrentDecl)) {
876bool Sema::isRecordLikeDecl() {
879 if (!ThisDeclInfo->IsFilled)
881 return isUnionDecl() || isClassOrStructDecl() || isObjCInterfaceDecl() ||
882 isObjCProtocolDecl();
885bool Sema::isUnionDecl() {
888 if (!ThisDeclInfo->IsFilled)
890 if (
const RecordDecl *RD =
891 dyn_cast_or_null<RecordDecl>(ThisDeclInfo->CurrentDecl))
892 return RD->isUnion();
896 if (
auto *record = dyn_cast_or_null<RecordDecl>(D))
897 return !record->isUnion();
902bool Sema::isClassOrStructDecl() {
914bool Sema::isClassOrStructOrTagTypedefDecl() {
926 if (
auto *ThisTypedefDecl = dyn_cast<TypedefDecl>(ThisDeclInfo->
CurrentDecl))
927 if (
auto *D = ThisTypedefDecl->getUnderlyingType()->getAsRecordDecl())
933bool Sema::isClassTemplateDecl() {
936 if (!ThisDeclInfo->IsFilled)
938 return ThisDeclInfo->CurrentDecl &&
942bool Sema::isFunctionTemplateDecl() {
945 if (!ThisDeclInfo->IsFilled)
947 return ThisDeclInfo->CurrentDecl &&
951bool Sema::isObjCInterfaceDecl() {
954 if (!ThisDeclInfo->IsFilled)
956 return ThisDeclInfo->CurrentDecl &&
960bool Sema::isObjCProtocolDecl() {
963 if (!ThisDeclInfo->IsFilled)
965 return ThisDeclInfo->CurrentDecl &&
969ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
970 if (!ThisDeclInfo->IsFilled)
972 return ThisDeclInfo->ParamVars;
975void Sema::inspectThisDecl() {
976 ThisDeclInfo->fill();
979unsigned Sema::resolveParmVarReference(StringRef Name,
980 ArrayRef<const ParmVarDecl *> ParamVars) {
981 for (
unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
982 const IdentifierInfo *II = ParamVars[i]->getIdentifier();
983 if (II && II->getName() == Name)
986 if (Name ==
"..." && isFunctionOrMethodVariadic())
992Sema::correctTypoInParmVarReference(StringRef Typo,
993 ArrayRef<const ParmVarDecl *> ParamVars) {
994 SimpleTypoCorrection STC(Typo);
995 for (
unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
996 const ParmVarDecl *Param = ParamVars[i];
1000 STC.add(Param->getIdentifier());
1003 if (STC.hasCorrection())
1004 return STC.getCorrectionIndex();
1010bool ResolveTParamReferenceHelper(
1012 const TemplateParameterList *TemplateParameters,
1013 SmallVectorImpl<unsigned> *Position) {
1014 for (
unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1015 const NamedDecl *Param = TemplateParameters->getParam(i);
1016 const IdentifierInfo *II = Param->getIdentifier();
1017 if (II && II->getName() == Name) {
1018 Position->push_back(i);
1022 if (
const TemplateTemplateParmDecl *TTP =
1023 dyn_cast<TemplateTemplateParmDecl>(Param)) {
1024 Position->push_back(i);
1025 if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
1028 Position->pop_back();
1035bool Sema::resolveTParamReference(
1037 const TemplateParameterList *TemplateParameters,
1038 SmallVectorImpl<unsigned> *Position) {
1040 if (!TemplateParameters)
1043 return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
1047void CorrectTypoInTParamReferenceHelper(
1048 const TemplateParameterList *TemplateParameters,
1049 SimpleTypoCorrection &STC) {
1050 for (
unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1051 const NamedDecl *Param = TemplateParameters->getParam(i);
1055 STC.add(Param->getIdentifier());
1057 if (
const TemplateTemplateParmDecl *TTP =
1058 dyn_cast<TemplateTemplateParmDecl>(Param))
1059 CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(), STC);
1064StringRef Sema::correctTypoInTParamReference(
1066 const TemplateParameterList *TemplateParameters) {
1067 SimpleTypoCorrection STC(Typo);
1068 CorrectTypoInTParamReferenceHelper(TemplateParameters, STC);
1070 if (
auto CorrectedTParamReference = STC.getCorrection())
1071 return *CorrectedTParamReference;
1077 assert(Traits.getCommandInfo(Name)->IsInlineCommand);
1079 return llvm::StringSwitch<InlineCommandRenderKind>(Name)
Defines the C++ template declaration subclasses.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
Defines the clang::Preprocessor interface.
Defines the SourceManager interface.
Decl - This represents one declaration (or definition), e.g.
Concrete class used by the front-end to report problems and issues.
static FixItHint CreateReplacement(CharSourceRange RemoveRange, StringRef Code)
Create a code modification hint that replaces the given source range with the given code string.
static FixItHint CreateInsertion(SourceLocation InsertionLoc, StringRef Code, bool BeforePreviousInsertions=false)
Create a code modification hint that inserts the given code string at a specific location.
One of these records is kept for each identifier that is lexed.
StringRef getName() const
Return the actual identifier string.
This represents a decl that may have a name.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
Stores a list of template parameters for a TemplateDecl and its derived classes.
NamedDecl * getParam(unsigned Idx)
std::variant< struct RequiresDecl, struct HeaderDecl, struct UmbrellaDirDecl, struct ModuleDecl, struct ExcludeDecl, struct ExportDecl, struct ExportAsDecl, struct ExternModuleDecl, struct UseDecl, struct LinkDecl, struct ConfigMacrosDecl, struct ConflictDecl > Decl
All declarations that can appear in a module declaration.
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
LLVM_READONLY bool isWhitespace(unsigned char c)
Return true if this character is horizontal or vertical ASCII whitespace: ' ', '\t',...
TemplateSpecializationKind
Describes the kind of template specialization that a particular template specialization declaration r...
@ TSK_ExplicitInstantiationDefinition
This template specialization was instantiated from a template due to an explicit instantiation defini...
@ TSK_ExplicitInstantiationDeclaration
This template specialization was instantiated from a template due to an explicit instantiation declar...