Skip to content

Conversation

@ayokunle321
Copy link
Contributor

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.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 30, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 30, 2025

@llvm/pr-subscribers-clang

Author: Ayokunle Amodu (ayokunle321)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/146433.diff

2 Files Affected:

  • (modified) clang/include/clang/Basic/DiagnosticCommentKinds.td (+2-1)
  • (modified) clang/lib/AST/CommentSema.cpp (+16-13)
diff --git a/clang/include/clang/Basic/DiagnosticCommentKinds.td b/clang/include/clang/Basic/DiagnosticCommentKinds.td
index 1122ace3027d8..ded3eabc5a5ff 100644
--- a/clang/include/clang/Basic/DiagnosticCommentKinds.td
+++ b/clang/include/clang/Basic/DiagnosticCommentKinds.td
@@ -84,7 +84,8 @@ def warn_doc_function_method_decl_mismatch : Warning<
   InGroup<Documentation>, DefaultIgnore;
 
 def warn_doc_api_container_decl_mismatch : Warning<
-  "'%select{\\|@}0%select{class|interface|protocol|struct|union}1' "
+  "'%select{\\|@}0%enum_select<DeclContainerKind>{%Class{class}"
+  "|%Interface{interface}|%Protocol{protocol}|%Struct{struct}|%Union{union}}1' "
   "command should not be used in a comment attached to a "
   "non-%select{class|interface|protocol|struct|union}2 declaration">,
   InGroup<Documentation>, DefaultIgnore;
diff --git a/clang/lib/AST/CommentSema.cpp b/clang/lib/AST/CommentSema.cpp
index fb745fc560d2f..bd227f852435e 100644
--- a/clang/lib/AST/CommentSema.cpp
+++ b/clang/lib/AST/CommentSema.cpp
@@ -132,39 +132,42 @@ void Sema::checkContainerDeclVerbatimLine(const BlockCommandComment *Comment) {
   const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID());
   if (!Info->IsRecordLikeDeclarationCommand)
     return;
-  unsigned DiagSelect;
+  std::optional<unsigned> DiagSelect;
   switch (Comment->getCommandID()) {
     case CommandTraits::KCI_class:
-      DiagSelect =
-          (!isClassOrStructOrTagTypedefDecl() && !isClassTemplateDecl()) ? 1
-                                                                         : 0;
+      if (!isClassOrStructOrTagTypedefDecl() && !isClassTemplateDecl())
+        DiagSelect = diag::DeclContainerKind::Class;
+
       // 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;
+        DiagSelect = std::nullopt;
       break;
     case CommandTraits::KCI_interface:
-      DiagSelect = !isObjCInterfaceDecl() ? 2 : 0;
+      if (!isObjCInterfaceDecl())
+        DiagSelect = diag::DeclContainerKind::Interface;
       break;
     case CommandTraits::KCI_protocol:
-      DiagSelect = !isObjCProtocolDecl() ? 3 : 0;
+      if (!isObjCProtocolDecl())
+        DiagSelect = diag::DeclContainerKind::Protocol;
       break;
     case CommandTraits::KCI_struct:
-      DiagSelect = !isClassOrStructOrTagTypedefDecl() ? 4 : 0;
+      if (!isClassOrStructOrTagTypedefDecl())
+        DiagSelect = diag::DeclContainerKind::Struct;
       break;
     case CommandTraits::KCI_union:
-      DiagSelect = !isUnionDecl() ? 5 : 0;
+      if (!isUnionDecl())
+        DiagSelect = diag::DeclContainerKind::Union;
       break;
     default:
-      DiagSelect = 0;
+      DiagSelect = std::nullopt;
       break;
   }
   if (DiagSelect)
     Diag(Comment->getLocation(), diag::warn_doc_api_container_decl_mismatch)
-    << Comment->getCommandMarker()
-    << (DiagSelect-1) << (DiagSelect-1)
-    << Comment->getSourceRange();
+        << Comment->getCommandMarker() << (*DiagSelect) << (*DiagSelect)
+        << Comment->getSourceRange();
 }
 
 void Sema::checkContainerDecl(const BlockCommandComment *Comment) {

@github-actions
Copy link

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo.
Please turn off Keep my email addresses private setting in your account.
See LLVM Developer Policy and LLVM Discourse for more information.

Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks. will you need us to merge that for you?

@ayokunle321
Copy link
Contributor Author

thanks. will you need us to merge that for you?

Yes please

@zwuis
Copy link
Contributor

zwuis commented Jul 5, 2025

Can we merge this with private email address?

@AaronBallman
Copy link
Collaborator

Can we merge this with private email address?

Community policy is to have public email addresses: https://llvm.org/docs/DeveloperPolicy.html#email-addresses

@ayokunle321 ayokunle321 force-pushed the replace_warn_doc_api_container_decl_mismatch_with_enum_select branch from 14104ff to 6df6bf4 Compare July 8, 2025 00:30
@ayokunle321
Copy link
Contributor Author

ayokunle321 commented Jul 8, 2025

I reset head by 1 and committed with a public email. Is it good now? @cor3ntin @zwuis @erichkeane

@erichkeane erichkeane merged commit b1aa3e4 into llvm:main Jul 9, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants