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 (!isTemplateOrSpecialization())
291 diag::warn_doc_tparam_not_attached_to_a_template_decl)
305 auto *A =
new (Allocator)
309 if (!isTemplateOrSpecialization()) {
315 ThisDeclInfo->TemplateParameters;
317 if (resolveTParamReference(Arg, TemplateParameters, &Position)) {
322 Diag(ArgLocBegin, diag::warn_doc_tparam_duplicate)
324 Diag(PrevCommand->
getLocation(), diag::note_doc_tparam_previous)
327 PrevCommand = Command;
332 Diag(ArgLocBegin, diag::warn_doc_tparam_not_found)
335 if (!TemplateParameters || TemplateParameters->
size() == 0)
338 StringRef CorrectedName;
339 if (TemplateParameters->
size() == 1) {
345 CorrectedName = correctTypoInTParamReference(Arg, TemplateParameters);
348 if (!CorrectedName.empty()) {
349 Diag(ArgLocBegin, diag::note_doc_tparam_name_suggestion)
358 checkBlockCommandEmptyParagraph(Command);
366 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
369 CommandLocBegin, CommandLocEnd, CommandID,
370 getInlineCommandRenderKind(CommandName), CommandMarker, Args);
375 StringRef CommandName) {
376 unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID();
382 unsigned CommandID) {
395 unsigned CommandID) {
396 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name;
413 Block->setCloseName(CloseName, CloseNameLocBegin);
414 Block->setLines(Lines);
427 checkFunctionDeclVerbatimLine(VL);
428 checkContainerDeclVerbatimLine(VL);
441 bool IsSelfClosing) {
443 Tag->setGreaterLoc(GreaterLoc);
445 Tag->setSelfClosing();
446 else if (!isHTMLEndTagForbidden(Tag->getTagName()))
447 HTMLOpenTags.push_back(Tag);
455 if (isHTMLEndTagForbidden(TagName)) {
456 Diag(HET->
getLocation(), diag::warn_doc_html_end_forbidden)
462 bool FoundOpen =
false;
464 I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend();
466 if ((*I)->getTagName() == TagName) {
472 Diag(HET->
getLocation(), diag::warn_doc_html_end_unbalanced)
478 while (!HTMLOpenTags.empty()) {
480 StringRef LastNotClosedTagName = HST->
getTagName();
481 if (LastNotClosedTagName == TagName) {
488 if (isHTMLEndTagOptional(LastNotClosedTagName))
491 bool OpenLineInvalid;
492 const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
495 bool CloseLineInvalid;
496 const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
500 if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine) {
501 Diag(HST->
getLocation(), diag::warn_doc_html_start_end_mismatch)
506 Diag(HST->
getLocation(), diag::warn_doc_html_start_end_mismatch)
509 Diag(HET->
getLocation(), diag::note_doc_html_end_tag)
521 resolveParamCommandIndexes(FC);
524 while (!HTMLOpenTags.empty()) {
529 Diag(HST->
getLocation(), diag::warn_doc_html_missing_end_tag)
538 if (Traits.getCommandInfo(Command->
getCommandID())->IsEmptyParagraphAllowed)
548 Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
555void Sema::checkReturnsCommand(
const BlockCommandComment *Command) {
556 if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand)
559 assert(ThisDeclInfo &&
"should not call this check on a bare comment");
563 if (isObjCPropertyDecl())
565 if (involvesFunctionType()) {
567 "should have a valid return type");
577 case Decl::CXXConstructor:
580 case Decl::CXXDestructor:
584 Diag(Command->getLocation(),
585 diag::warn_doc_returns_attached_to_a_void_function)
586 << Command->getCommandMarker()
587 << Command->getCommandName(Traits)
589 << Command->getSourceRange();
594 Diag(Command->getLocation(),
595 diag::warn_doc_returns_not_attached_to_a_function_decl)
596 << Command->getCommandMarker()
597 << Command->getCommandName(Traits)
598 << Command->getSourceRange();
602 const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID());
603 const BlockCommandComment *PrevCommand =
nullptr;
604 if (Info->IsBriefCommand) {
606 BriefCommand = Command;
609 PrevCommand = BriefCommand;
610 }
else if (Info->IsHeaderfileCommand) {
611 if (!HeaderfileCommand) {
612 HeaderfileCommand = Command;
615 PrevCommand = HeaderfileCommand;
620 StringRef CommandName = Command->getCommandName(Traits);
621 StringRef PrevCommandName = PrevCommand->getCommandName(Traits);
622 Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate)
623 << Command->getCommandMarker()
625 << Command->getSourceRange();
626 if (CommandName == PrevCommandName)
627 Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous)
628 << PrevCommand->getCommandMarker()
630 << PrevCommand->getSourceRange();
632 Diag(PrevCommand->getLocation(),
633 diag::note_doc_block_command_previous_alias)
634 << PrevCommand->getCommandMarker()
640 if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand)
643 assert(ThisDeclInfo &&
"should not call this check on a bare comment");
645 const Decl *D = ThisDeclInfo->CommentDecl;
649 if (D->hasAttr<DeprecatedAttr>() ||
650 D->hasAttr<AvailabilityAttr>() ||
651 D->hasAttr<UnavailableAttr>())
654 Diag(Command->getLocation(), diag::warn_doc_deprecated_not_sync)
655 << Command->getSourceRange() << Command->getCommandMarker();
658 if (
const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
661 const DeclContext *Ctx = FD->getDeclContext();
662 if ((!Ctx || !Ctx->isRecord()) &&
663 FD->doesThisDeclarationHaveABody())
666 const LangOptions &LO = FD->getLangOpts();
667 const bool DoubleSquareBracket = LO.CPlusPlus14 || LO.C23;
668 StringRef AttributeSpelling =
669 DoubleSquareBracket ?
"[[deprecated]]" :
"__attribute__((deprecated))";
675 if (DoubleSquareBracket) {
676 TokenValue Tokens[] = {tok::l_square, tok::l_square,
677 PP->getIdentifierInfo(
"deprecated"),
678 tok::r_square, tok::r_square};
679 MacroName = PP->getLastMacroWithSpelling(FD->getLocation(), Tokens);
680 if (!MacroName.empty())
681 AttributeSpelling = MacroName;
684 if (MacroName.empty()) {
685 TokenValue Tokens[] = {
686 tok::kw___attribute, tok::l_paren,
687 tok::l_paren, PP->getIdentifierInfo(
"deprecated"),
688 tok::r_paren, tok::r_paren};
689 StringRef MacroName =
690 PP->getLastMacroWithSpelling(FD->getLocation(), Tokens);
691 if (!MacroName.empty())
692 AttributeSpelling = MacroName;
696 SmallString<64> TextToInsert = AttributeSpelling;
698 SourceLocation Loc = FD->getSourceRange().getBegin();
699 Diag(Loc, diag::note_add_deprecation_attr)
704void Sema::resolveParamCommandIndexes(
const FullComment *FC) {
705 if (!involvesFunctionType()) {
711 SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands;
715 SmallVector<ParamCommandComment *, 8> ParamVarDocs;
717 ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
718 ParamVarDocs.resize(ParamVars.size(),
nullptr);
723 ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I);
724 if (!PCC || !PCC->hasParamName())
726 StringRef ParamName = PCC->getParamNameAsWritten();
729 const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName,
732 PCC->setIsVarArgParam();
736 UnresolvedParamCommands.push_back(PCC);
739 PCC->setParamIndex(ResolvedParamIndex);
740 if (ParamVarDocs[ResolvedParamIndex]) {
741 SourceRange ArgRange = PCC->getParamNameRange();
742 Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate)
743 << ParamName << ArgRange;
744 ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
745 Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
746 << PrevCommand->getParamNameRange();
748 ParamVarDocs[ResolvedParamIndex] = PCC;
752 SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls;
753 for (
unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) {
754 if (!ParamVarDocs[i])
755 OrphanedParamDecls.push_back(ParamVars[i]);
761 for (
unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) {
762 const ParamCommandComment *PCC = UnresolvedParamCommands[i];
764 SourceRange ArgRange = PCC->getParamNameRange();
765 StringRef ParamName = PCC->getParamNameAsWritten();
766 Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found)
767 << ParamName << ArgRange;
770 if (OrphanedParamDecls.size() == 0)
774 if (OrphanedParamDecls.size() == 1) {
777 CorrectedParamIndex = 0;
780 CorrectedParamIndex = correctTypoInParmVarReference(ParamName,
784 const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex];
785 if (
const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
786 Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion)
787 << CorrectedII->getName()
793bool Sema::involvesFunctionType() {
796 if (!ThisDeclInfo->IsFilled)
798 return ThisDeclInfo->involvesFunctionType();
801bool Sema::isFunctionDecl() {
804 if (!ThisDeclInfo->IsFilled)
809bool Sema::isAnyFunctionDecl() {
810 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
814bool Sema::isFunctionOrMethodVariadic() {
817 if (!ThisDeclInfo->IsFilled)
819 return ThisDeclInfo->IsVariadic;
822bool Sema::isObjCMethodDecl() {
823 return isFunctionDecl() && ThisDeclInfo->CurrentDecl &&
827bool Sema::isFunctionPointerVarDecl() {
830 if (!ThisDeclInfo->IsFilled)
833 if (
const VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDeclInfo->CurrentDecl)) {
834 QualType QT = VD->getType();
835 return QT->isFunctionPointerType();
841bool Sema::isObjCPropertyDecl() {
844 if (!ThisDeclInfo->IsFilled)
846 return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty;
849bool Sema::isTemplateOrSpecialization() {
852 if (!ThisDeclInfo->IsFilled)
857bool Sema::isRecordLikeDecl() {
860 if (!ThisDeclInfo->IsFilled)
862 return isUnionDecl() || isClassOrStructDecl() || isObjCInterfaceDecl() ||
863 isObjCProtocolDecl();
866bool Sema::isUnionDecl() {
869 if (!ThisDeclInfo->IsFilled)
871 if (
const RecordDecl *RD =
872 dyn_cast_or_null<RecordDecl>(ThisDeclInfo->CurrentDecl))
873 return RD->isUnion();
877 if (
auto *record = dyn_cast_or_null<RecordDecl>(D))
878 return !record->isUnion();
883bool Sema::isClassOrStructDecl() {
895bool Sema::isClassOrStructOrTagTypedefDecl() {
907 if (
auto *ThisTypedefDecl = dyn_cast<TypedefDecl>(ThisDeclInfo->
CurrentDecl))
908 if (
auto *D = ThisTypedefDecl->getUnderlyingType()->getAsRecordDecl())
914bool Sema::isClassTemplateDecl() {
917 if (!ThisDeclInfo->IsFilled)
919 return ThisDeclInfo->CurrentDecl &&
923bool Sema::isFunctionTemplateDecl() {
926 if (!ThisDeclInfo->IsFilled)
928 return ThisDeclInfo->CurrentDecl &&
932bool Sema::isObjCInterfaceDecl() {
935 if (!ThisDeclInfo->IsFilled)
937 return ThisDeclInfo->CurrentDecl &&
941bool Sema::isObjCProtocolDecl() {
944 if (!ThisDeclInfo->IsFilled)
946 return ThisDeclInfo->CurrentDecl &&
950ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
951 if (!ThisDeclInfo->IsFilled)
953 return ThisDeclInfo->ParamVars;
956void Sema::inspectThisDecl() {
957 ThisDeclInfo->fill();
960unsigned Sema::resolveParmVarReference(StringRef Name,
961 ArrayRef<const ParmVarDecl *> ParamVars) {
962 for (
unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
963 const IdentifierInfo *II = ParamVars[i]->getIdentifier();
964 if (II && II->getName() == Name)
967 if (Name ==
"..." && isFunctionOrMethodVariadic())
973Sema::correctTypoInParmVarReference(StringRef Typo,
974 ArrayRef<const ParmVarDecl *> ParamVars) {
975 SimpleTypoCorrection STC(Typo);
976 for (
unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
977 const ParmVarDecl *Param = ParamVars[i];
981 STC.add(Param->getIdentifier());
984 if (STC.hasCorrection())
985 return STC.getCorrectionIndex();
991bool ResolveTParamReferenceHelper(
993 const TemplateParameterList *TemplateParameters,
994 SmallVectorImpl<unsigned> *Position) {
995 for (
unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
996 const NamedDecl *Param = TemplateParameters->getParam(i);
997 const IdentifierInfo *II = Param->getIdentifier();
998 if (II && II->getName() == Name) {
999 Position->push_back(i);
1003 if (
const TemplateTemplateParmDecl *TTP =
1004 dyn_cast<TemplateTemplateParmDecl>(Param)) {
1005 Position->push_back(i);
1006 if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(),
1009 Position->pop_back();
1016bool Sema::resolveTParamReference(
1018 const TemplateParameterList *TemplateParameters,
1019 SmallVectorImpl<unsigned> *Position) {
1021 if (!TemplateParameters)
1024 return ResolveTParamReferenceHelper(Name, TemplateParameters, Position);
1028void CorrectTypoInTParamReferenceHelper(
1029 const TemplateParameterList *TemplateParameters,
1030 SimpleTypoCorrection &STC) {
1031 for (
unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
1032 const NamedDecl *Param = TemplateParameters->getParam(i);
1036 STC.add(Param->getIdentifier());
1038 if (
const TemplateTemplateParmDecl *TTP =
1039 dyn_cast<TemplateTemplateParmDecl>(Param))
1040 CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(), STC);
1045StringRef Sema::correctTypoInTParamReference(
1047 const TemplateParameterList *TemplateParameters) {
1048 SimpleTypoCorrection STC(Typo);
1049 CorrectTypoInTParamReferenceHelper(TemplateParameters, STC);
1051 if (
auto CorrectedTParamReference = STC.getCorrection())
1052 return *CorrectedTParamReference;
1058 assert(Traits.getCommandInfo(Name)->IsInlineCommand);
1060 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',...