Skip to content

Conversation

mstorsjo
Copy link
Member

This avoids the following kind of warning when built with GCC:

../../clang/lib/Sema/SemaStmtAttr.cpp: In function ‘clang::Attr* ProcessStmtAttribute(clang::Sema&, clang::Stmt*, const clang::ParsedAttr&, clang::SourceRange)’:
../../clang/lib/Sema/SemaStmtAttr.cpp:677:30: warning: enumerated mismatch in conditional expression: ‘clang::diag::<unnamed enum>’ vs ‘clang::diag::<unnamed enum>’ [-Wenum-compare]
  676 |       S.Diag(A.getLoc(), A.isRegularKeywordAttribute()
      |                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  677 |                              ? diag::err_keyword_not_supported_on_targe
      |                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  678 |                              : diag::warn_unhandled_ms_attribute_ignore )
      |                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

These enums are non-overlapping, but due they are defined in different enum scopes due to how they are generated with tablegen.

This avoids the following kind of warning when built with GCC:

    ../../clang/lib/Sema/SemaStmtAttr.cpp: In function ‘clang::Attr* ProcessStmtAttribute(clang::Sema&, clang::Stmt*, const clang::ParsedAttr&, clang::SourceRange)’:
    ../../clang/lib/Sema/SemaStmtAttr.cpp:677:30: warning: enumerated mismatch in conditional expression: ‘clang::diag::<unnamed enum>’ vs ‘clang::diag::<unnamed enum>’ [-Wenum-compare]
      676 |       S.Diag(A.getLoc(), A.isRegularKeywordAttribute()
          |                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      677 |                              ? diag::err_keyword_not_supported_on_targe
          |                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      678 |                              : diag::warn_unhandled_ms_attribute_ignore )
          |                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

These enums are non-overlapping, but due they are defined in different
enum scopes due to how they are generated with tablegen.
@mstorsjo mstorsjo requested a review from erichkeane September 17, 2025 11:52
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Sep 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 17, 2025

@llvm/pr-subscribers-clang

Author: Martin Storsjö (mstorsjo)

Changes

This avoids the following kind of warning when built with GCC:

../../clang/lib/Sema/SemaStmtAttr.cpp: In function ‘clang::Attr* ProcessStmtAttribute(clang::Sema&amp;, clang::Stmt*, const clang::ParsedAttr&amp;, clang::SourceRange)’:
../../clang/lib/Sema/SemaStmtAttr.cpp:677:30: warning: enumerated mismatch in conditional expression: ‘clang::diag::&lt;unnamed enum&gt;’ vs ‘clang::diag::&lt;unnamed enum&gt;’ [-Wenum-compare]
  676 |       S.Diag(A.getLoc(), A.isRegularKeywordAttribute()
      |                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  677 |                              ? diag::err_keyword_not_supported_on_targe
      |                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  678 |                              : diag::warn_unhandled_ms_attribute_ignore )
      |                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

These enums are non-overlapping, but due they are defined in different enum scopes due to how they are generated with tablegen.


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

2 Files Affected:

  • (modified) clang/lib/Sema/SemaDeclAttr.cpp (+4-2)
  • (modified) clang/lib/Sema/SemaStmtAttr.cpp (+4-2)
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 44906456f3371..7b3dc80e38913 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -6837,8 +6837,10 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
       !AL.existsInTarget(S.Context.getTargetInfo())) {
     if (AL.isRegularKeywordAttribute() || AL.isDeclspecAttribute()) {
       S.Diag(AL.getLoc(), AL.isRegularKeywordAttribute()
-                              ? diag::err_keyword_not_supported_on_target
-                              : diag::warn_unhandled_ms_attribute_ignored)
+                              ? static_cast<unsigned>(
+                                    diag::err_keyword_not_supported_on_target)
+                              : static_cast<unsigned>(
+                                    diag::warn_unhandled_ms_attribute_ignored))
           << AL.getAttrName() << AL.getRange();
     } else {
       S.DiagnoseUnknownAttribute(AL);
diff --git a/clang/lib/Sema/SemaStmtAttr.cpp b/clang/lib/Sema/SemaStmtAttr.cpp
index 77aa7164d4555..8cac790e969fa 100644
--- a/clang/lib/Sema/SemaStmtAttr.cpp
+++ b/clang/lib/Sema/SemaStmtAttr.cpp
@@ -674,8 +674,10 @@ static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const ParsedAttr &A,
          A.existsInTarget(*Aux)))) {
     if (A.isRegularKeywordAttribute() || A.isDeclspecAttribute()) {
       S.Diag(A.getLoc(), A.isRegularKeywordAttribute()
-                             ? diag::err_keyword_not_supported_on_target
-                             : diag::warn_unhandled_ms_attribute_ignored)
+                             ? static_cast<unsigned>(
+                                   diag::err_keyword_not_supported_on_target)
+                             : static_cast<unsigned>(
+                                   diag::warn_unhandled_ms_attribute_ignored))
           << A << A.getRange();
     } else {
       S.DiagnoseUnknownAttribute(A);

@mstorsjo
Copy link
Member Author

This is also not very pretty, but again, this is a mostly reasonable warning - unfortunately our diagnostics aren't all in one large enum for (presumably) technical reasons, even if they are forced to be non-overlapping.

@cor3ntin
Copy link
Contributor

I think it might be cleaner in that case to have two different if statements (one for isRegularKeywordAttribute and one for isDeclspecAttribute)

@brunodf-snps
Copy link
Contributor

@mstorsjo It seems you are (by coincidence) looking into warnings of a gcc build at the same time as I was. There was a third PR about a gcc warning: #158635

!(A.existsInTarget(S.Context.getTargetInfo()) ||
(S.Context.getLangOpts().SYCLIsDevice && Aux &&
A.existsInTarget(*Aux)))) {
if (A.isRegularKeywordAttribute() || A.isDeclspecAttribute()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think I agree with corentin, just breaking this up into 2 branches (an if (A.isRegularKeywordAttribute()) and else if (A.isDeclspecAttribute()) is probably better. As it is, we're basically checking the regular keyword 2x.

Else, this patch is fine for me.

@mstorsjo
Copy link
Member Author

I think it might be cleaner in that case to have two different if statements (one for isRegularKeywordAttribute and one for isDeclspecAttribute)

Sounds reasonable, thanks - I updated the PR with such a change.

@mstorsjo mstorsjo merged commit 0d989b2 into llvm:main Sep 17, 2025
9 checks passed
@mstorsjo mstorsjo deleted the clang-mixed-enum-ternary branch September 17, 2025 19:29
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.

5 participants