Skip to content

Commit b1aa3e4

Browse files
authored
[clang][diagnostics] Refactor "warn_doc_api_container_decl_mismatch" to use enum_select (#146433)
Related: #123121 This patch refactors the `warn_doc_api_container_decl_mismatch` diagnostic to use enum_select instead of select. This gets rid of magic numbers and improves readability in the caller site.
1 parent 6d7be75 commit b1aa3e4

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

clang/include/clang/Basic/DiagnosticCommentKinds.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ def warn_doc_function_method_decl_mismatch : Warning<
8484
InGroup<Documentation>, DefaultIgnore;
8585

8686
def warn_doc_api_container_decl_mismatch : Warning<
87-
"'%select{\\|@}0%select{class|interface|protocol|struct|union}1' "
87+
"'%select{\\|@}0%enum_select<DeclContainerKind>{%Class{class}"
88+
"|%Interface{interface}|%Protocol{protocol}|%Struct{struct}|%Union{union}}1' "
8889
"command should not be used in a comment attached to a "
8990
"non-%select{class|interface|protocol|struct|union}2 declaration">,
9091
InGroup<Documentation>, DefaultIgnore;

clang/lib/AST/CommentSema.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,39 +132,42 @@ void Sema::checkContainerDeclVerbatimLine(const BlockCommandComment *Comment) {
132132
const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
133133
if (!Info->IsRecordLikeDeclarationCommand)
134134
return;
135-
unsigned DiagSelect;
135+
std::optional<unsigned> DiagSelect;
136136
switch (Comment->getCommandID()) {
137137
case CommandTraits::KCI_class:
138-
DiagSelect =
139-
(!isClassOrStructOrTagTypedefDecl() && !isClassTemplateDecl()) ? 1
140-
: 0;
138+
if (!isClassOrStructOrTagTypedefDecl() && !isClassTemplateDecl())
139+
DiagSelect = diag::DeclContainerKind::Class;
140+
141141
// Allow @class command on @interface declarations.
142142
// FIXME. Currently, \class and @class are indistinguishable. So,
143143
// \class is also allowed on an @interface declaration
144144
if (DiagSelect && Comment->getCommandMarker() && isObjCInterfaceDecl())
145-
DiagSelect = 0;
145+
DiagSelect = std::nullopt;
146146
break;
147147
case CommandTraits::KCI_interface:
148-
DiagSelect = !isObjCInterfaceDecl() ? 2 : 0;
148+
if (!isObjCInterfaceDecl())
149+
DiagSelect = diag::DeclContainerKind::Interface;
149150
break;
150151
case CommandTraits::KCI_protocol:
151-
DiagSelect = !isObjCProtocolDecl() ? 3 : 0;
152+
if (!isObjCProtocolDecl())
153+
DiagSelect = diag::DeclContainerKind::Protocol;
152154
break;
153155
case CommandTraits::KCI_struct:
154-
DiagSelect = !isClassOrStructOrTagTypedefDecl() ? 4 : 0;
156+
if (!isClassOrStructOrTagTypedefDecl())
157+
DiagSelect = diag::DeclContainerKind::Struct;
155158
break;
156159
case CommandTraits::KCI_union:
157-
DiagSelect = !isUnionDecl() ? 5 : 0;
160+
if (!isUnionDecl())
161+
DiagSelect = diag::DeclContainerKind::Union;
158162
break;
159163
default:
160-
DiagSelect = 0;
164+
DiagSelect = std::nullopt;
161165
break;
162166
}
163167
if (DiagSelect)
164168
Diag(Comment->getLocation(), diag::warn_doc_api_container_decl_mismatch)
165-
<< Comment->getCommandMarker()
166-
<< (DiagSelect-1) << (DiagSelect-1)
167-
<< Comment->getSourceRange();
169+
<< Comment->getCommandMarker() << (*DiagSelect) << (*DiagSelect)
170+
<< Comment->getSourceRange();
168171
}
169172

170173
void Sema::checkContainerDecl(const BlockCommandComment *Comment) {

0 commit comments

Comments
 (0)