Skip to content

Commit 77ea912

Browse files
authored
[clang][diagnostics] Refactor "warn_doc_container_decl_mismatch" to use enum_select (#147120)
Related: #123121 This patch refactors the `warn_doc_container_decl_mismatch` diagnostic to use `enum_select` instead of `select`. This gets rid of magic numbers and improves readability in the caller site. @cor3ntin @erichkeane
1 parent 1f28da6 commit 77ea912

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

clang/include/clang/Basic/DiagnosticCommentKinds.td

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ def warn_doc_api_container_decl_mismatch : Warning<
9090
InGroup<Documentation>, DefaultIgnore;
9191

9292
def warn_doc_container_decl_mismatch : Warning<
93-
"'%select{\\|@}0%select{classdesign|coclass|dependency|helper"
94-
"|helperclass|helps|instancesize|ownership|performance|security|superclass}1' "
93+
"'%select{\\|@}0%enum_select<DocCommandKind>{%ClassDesign{classdesign}|"
94+
"%CoClass{coclass}|%Dependency{dependency}|%Helper{helper}|%HelperClass{helperclass}|"
95+
"%Helps{helps}|%InstanceSize{instancesize}|%Ownership{ownership}|"
96+
"%Performance{performance}|%Security{security}|%Superclass{superclass}}1' "
9597
"command should not be used in a comment attached to a non-container declaration">,
9698
InGroup<Documentation>, DefaultIgnore;
9799

clang/lib/AST/CommentSema.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -171,50 +171,49 @@ void Sema::checkContainerDecl(const BlockCommandComment *Comment) {
171171
const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
172172
if (!Info->IsRecordLikeDetailCommand || isRecordLikeDecl())
173173
return;
174-
unsigned DiagSelect;
174+
std::optional<unsigned> DiagSelect;
175175
switch (Comment->getCommandID()) {
176176
case CommandTraits::KCI_classdesign:
177-
DiagSelect = 1;
177+
DiagSelect = diag::DocCommandKind::ClassDesign;
178178
break;
179179
case CommandTraits::KCI_coclass:
180-
DiagSelect = 2;
180+
DiagSelect = diag::DocCommandKind::CoClass;
181181
break;
182182
case CommandTraits::KCI_dependency:
183-
DiagSelect = 3;
183+
DiagSelect = diag::DocCommandKind::Dependency;
184184
break;
185185
case CommandTraits::KCI_helper:
186-
DiagSelect = 4;
186+
DiagSelect = diag::DocCommandKind::Helper;
187187
break;
188188
case CommandTraits::KCI_helperclass:
189-
DiagSelect = 5;
189+
DiagSelect = diag::DocCommandKind::HelperClass;
190190
break;
191191
case CommandTraits::KCI_helps:
192-
DiagSelect = 6;
192+
DiagSelect = diag::DocCommandKind::Helps;
193193
break;
194194
case CommandTraits::KCI_instancesize:
195-
DiagSelect = 7;
195+
DiagSelect = diag::DocCommandKind::InstanceSize;
196196
break;
197197
case CommandTraits::KCI_ownership:
198-
DiagSelect = 8;
198+
DiagSelect = diag::DocCommandKind::Ownership;
199199
break;
200200
case CommandTraits::KCI_performance:
201-
DiagSelect = 9;
201+
DiagSelect = diag::DocCommandKind::Performance;
202202
break;
203203
case CommandTraits::KCI_security:
204-
DiagSelect = 10;
204+
DiagSelect = diag::DocCommandKind::Security;
205205
break;
206206
case CommandTraits::KCI_superclass:
207-
DiagSelect = 11;
207+
DiagSelect = diag::DocCommandKind::Superclass;
208208
break;
209209
default:
210-
DiagSelect = 0;
210+
DiagSelect = std::nullopt;
211211
break;
212212
}
213213
if (DiagSelect)
214214
Diag(Comment->getLocation(), diag::warn_doc_container_decl_mismatch)
215-
<< Comment->getCommandMarker()
216-
<< (DiagSelect-1)
217-
<< Comment->getSourceRange();
215+
<< Comment->getCommandMarker() << (*DiagSelect)
216+
<< Comment->getSourceRange();
218217
}
219218

220219
/// Turn a string into the corresponding PassDirection or -1 if it's not

0 commit comments

Comments
 (0)