https://github.com/mstorsjo updated 
https://github.com/llvm/llvm-project/pull/159338

From 80607ae5154313cbafe506cc1bb72486b530521d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Storsj=C3=B6?= <[email protected]>
Date: Wed, 17 Sep 2025 14:25:50 +0300
Subject: [PATCH 1/2] [clang] Avoid warnings about enum mismatch in ternary
 expressions. NFC.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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.
---
 clang/lib/Sema/SemaDeclAttr.cpp | 6 ++++--
 clang/lib/Sema/SemaStmtAttr.cpp | 6 ++++--
 2 files changed, 8 insertions(+), 4 deletions(-)

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);

From 44cc6a81ac853b0c683f17f29b75822d8ffef041 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Storsj=C3=B6?= <[email protected]>
Date: Wed, 17 Sep 2025 20:52:44 +0300
Subject: [PATCH 2/2] Split the if statement instead of using an inline ternary
 expression.

---
 clang/lib/Sema/SemaDeclAttr.cpp | 11 +++++------
 clang/lib/Sema/SemaStmtAttr.cpp | 11 +++++------
 2 files changed, 10 insertions(+), 12 deletions(-)

diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 7b3dc80e38913..e5c93a6a732bf 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -6835,12 +6835,11 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, 
const ParsedAttr &AL,
   // though they were unknown attributes.
   if (AL.getKind() == ParsedAttr::UnknownAttribute ||
       !AL.existsInTarget(S.Context.getTargetInfo())) {
-    if (AL.isRegularKeywordAttribute() || AL.isDeclspecAttribute()) {
-      S.Diag(AL.getLoc(), AL.isRegularKeywordAttribute()
-                              ? static_cast<unsigned>(
-                                    diag::err_keyword_not_supported_on_target)
-                              : static_cast<unsigned>(
-                                    diag::warn_unhandled_ms_attribute_ignored))
+    if (AL.isRegularKeywordAttribute()) {
+      S.Diag(AL.getLoc(), diag::err_keyword_not_supported_on_target)
+          << AL.getAttrName() << AL.getRange();
+    } else if (AL.isDeclspecAttribute()) {
+      S.Diag(AL.getLoc(), 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 8cac790e969fa..50acc83f1841c 100644
--- a/clang/lib/Sema/SemaStmtAttr.cpp
+++ b/clang/lib/Sema/SemaStmtAttr.cpp
@@ -672,12 +672,11 @@ static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, 
const ParsedAttr &A,
       !(A.existsInTarget(S.Context.getTargetInfo()) ||
         (S.Context.getLangOpts().SYCLIsDevice && Aux &&
          A.existsInTarget(*Aux)))) {
-    if (A.isRegularKeywordAttribute() || A.isDeclspecAttribute()) {
-      S.Diag(A.getLoc(), A.isRegularKeywordAttribute()
-                             ? static_cast<unsigned>(
-                                   diag::err_keyword_not_supported_on_target)
-                             : static_cast<unsigned>(
-                                   diag::warn_unhandled_ms_attribute_ignored))
+    if (A.isRegularKeywordAttribute()) {
+      S.Diag(A.getLoc(), diag::err_keyword_not_supported_on_target)
+          << A << A.getRange();
+    } else if (A.isDeclspecAttribute()) {
+      S.Diag(A.getLoc(), diag::warn_unhandled_ms_attribute_ignored)
           << A << A.getRange();
     } else {
       S.DiagnoseUnknownAttribute(A);

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to