https://github.com/Mr-Anyone updated 
https://github.com/llvm/llvm-project/pull/140828

>From 393ccc154e2f1d68ef25d660c8063ab9674ab09b Mon Sep 17 00:00:00 2001
From: Vincent <l...@viceroygroup.ca>
Date: Tue, 20 May 2025 21:25:32 -0400
Subject: [PATCH 1/5] [clang][TableGen] Fix Duplicate Entries in TableGen

Fixed TableGen duplicate issues that causes the wrong interrupt
attribute from being selected.

resolves #140701
---
 clang/test/AST/ast-dump-riscv-attributes.cpp | 12 ++++++++
 clang/utils/TableGen/ClangAttrEmitter.cpp    | 31 ++++++++++++++++----
 2 files changed, 38 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/AST/ast-dump-riscv-attributes.cpp

diff --git a/clang/test/AST/ast-dump-riscv-attributes.cpp 
b/clang/test/AST/ast-dump-riscv-attributes.cpp
new file mode 100644
index 0000000000000..7efe626072311
--- /dev/null
+++ b/clang/test/AST/ast-dump-riscv-attributes.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -triple riscv64 -ast-dump -ast-dump-filter c23 -std=c23 -x 
c %s | FileCheck --strict-whitespace %s
+
+// CHECK:       FunctionDecl{{.*}}pre_c23
+// CHECK-NEXT:    |-CompoundStmt
+// CHECK-NEXT:    `-RISCVInterruptAttr{{.*}}supervisor
+__attribute__((interrupt("supervisor"))) void pre_c23(){}
+
+// CHECK:       FunctionDecl{{.*}}in_c23
+// CHECK-NEXT:    |-CompoundStmt
+// CHECK-NEXT:    `-RISCVInterruptAttr{{.*}}supervisor
+// CHECK-NOT:     `-RISCVInterruptAttr{{.*}}machine
+[[gnu::interrupt("supervisor")]] void in_c23(){}
diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp 
b/clang/utils/TableGen/ClangAttrEmitter.cpp
index 9684ec9520e5a..094ea0564f2d7 100644
--- a/clang/utils/TableGen/ClangAttrEmitter.cpp
+++ b/clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -20,6 +20,7 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/Support/ErrorHandling.h"
@@ -3667,6 +3668,12 @@ static bool GenerateTargetSpecificAttrChecks(const 
Record *R,
 static void GenerateHasAttrSpellingStringSwitch(
     ArrayRef<std::pair<const Record *, FlattenedSpelling>> Attrs,
     raw_ostream &OS, StringRef Variety, StringRef Scope = "") {
+
+  // It turns out that duplicate records for a given spelling. This map 
combines
+  // matching test strings using '||'. For example, if there are three
+  // conditions A, B, and C, the final result will be: A || B || C.
+  llvm::StringMap<std::string> TestStringMap;
+
   for (const auto &[Attr, Spelling] : Attrs) {
     // C++11-style attributes have specific version information associated with
     // them. If the attribute has no scope, the version information must not
@@ -3727,12 +3734,26 @@ static void GenerateHasAttrSpellingStringSwitch(
       }
     }
 
-    std::string TestStr = !Test.empty()
-                              ? Test + " ? " + itostr(Version) + " : 0"
-                              : itostr(Version);
-    if (Scope.empty() || Scope == Spelling.nameSpace())
-      OS << "    .Case(\"" << Spelling.name() << "\", " << TestStr << ")\n";
+    std::string TestStr =
+        !Test.empty() ? '(' + Test + " ? " + itostr(Version) + " : 0" + ')'
+                      : '(' + itostr(Version) + ')';
+
+    if (Scope.empty() || Scope == Spelling.nameSpace()) {
+      if (TestStringMap.contains(Spelling.name())) {
+        TestStringMap[Spelling.name()] += " || " + TestStr;
+      } else {
+        TestStringMap[Spelling.name()] = TestStr;
+      }
+    }
+  }
+
+  // Create the actual string switch statement after all the attributes have
+  // been parsed
+  for (auto &entry : TestStringMap) {
+    OS << "    .Case(\"" << entry.getKey() << "\", " << entry.getValue()
+       << ")\n";
   }
+
   OS << "    .Default(0);\n";
 }
 

>From 475f240822f34a00e26740a4ad3576fab14accf1 Mon Sep 17 00:00:00 2001
From: Vincent <l...@viceroygroup.ca>
Date: Tue, 20 May 2025 22:18:36 -0400
Subject: [PATCH 2/5] update comments

---
 clang/utils/TableGen/ClangAttrEmitter.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp 
b/clang/utils/TableGen/ClangAttrEmitter.cpp
index 094ea0564f2d7..d2b873adfdbdb 100644
--- a/clang/utils/TableGen/ClangAttrEmitter.cpp
+++ b/clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -3669,8 +3669,8 @@ static void GenerateHasAttrSpellingStringSwitch(
     ArrayRef<std::pair<const Record *, FlattenedSpelling>> Attrs,
     raw_ostream &OS, StringRef Variety, StringRef Scope = "") {
 
-  // It turns out that duplicate records for a given spelling. This map 
combines
-  // matching test strings using '||'. For example, if there are three
+  // It turns out that there are duplicate records for a given spelling. This 
map
+  // combines matching test strings using '||'. For example, if there are three
   // conditions A, B, and C, the final result will be: A || B || C.
   llvm::StringMap<std::string> TestStringMap;
 

>From 7a5e56c13d2fe25fca26e6d31473e35b4bb18f03 Mon Sep 17 00:00:00 2001
From: Vincent <l...@viceroygroup.ca>
Date: Tue, 20 May 2025 22:24:14 -0400
Subject: [PATCH 3/5] Fixed Clang Format

---
 clang/utils/TableGen/ClangAttrEmitter.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp 
b/clang/utils/TableGen/ClangAttrEmitter.cpp
index d2b873adfdbdb..05580716db752 100644
--- a/clang/utils/TableGen/ClangAttrEmitter.cpp
+++ b/clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -3669,9 +3669,9 @@ static void GenerateHasAttrSpellingStringSwitch(
     ArrayRef<std::pair<const Record *, FlattenedSpelling>> Attrs,
     raw_ostream &OS, StringRef Variety, StringRef Scope = "") {
 
-  // It turns out that there are duplicate records for a given spelling. This 
map
-  // combines matching test strings using '||'. For example, if there are three
-  // conditions A, B, and C, the final result will be: A || B || C.
+  // It turns out that there are duplicate records for a given spelling. This
+  // map combines matching test strings using '||'. For example, if there are
+  // three conditions A, B, and C, the final result will be: A || B || C.
   llvm::StringMap<std::string> TestStringMap;
 
   for (const auto &[Attr, Spelling] : Attrs) {

>From adaf399fbc0175672184c5297e0e658406389059 Mon Sep 17 00:00:00 2001
From: Vincent <l...@viceroygroup.ca>
Date: Wed, 21 May 2025 00:22:47 -0400
Subject: [PATCH 4/5] Added Release Docs

---
 clang/docs/ReleaseNotes.rst | 1 +
 1 file changed, 1 insertion(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8e6cf62e11752..65bd9f8a8b831 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -628,6 +628,7 @@ Bug Fixes in This Version
 - Fix crash due to unknown references and pointer implementation and handling 
of
   base classes. (GH139452)
 - Fixed an assertion failure in serialization of constexpr structs containing 
unions. (#GH140130)
+- Fixed duplicate entries in TableGen that caused the wrong attribute to be 
selected. (GH#140701)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

>From df5403610701df4c21a0f508a91c4690e5c15dc9 Mon Sep 17 00:00:00 2001
From: Vincent <l...@viceroygroup.ca>
Date: Wed, 21 May 2025 10:08:05 -0400
Subject: [PATCH 5/5] Minor nits

---
 clang/utils/TableGen/ClangAttrEmitter.cpp | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp 
b/clang/utils/TableGen/ClangAttrEmitter.cpp
index 05580716db752..45fb0d4735c86 100644
--- a/clang/utils/TableGen/ClangAttrEmitter.cpp
+++ b/clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -3739,18 +3739,17 @@ static void GenerateHasAttrSpellingStringSwitch(
                       : '(' + itostr(Version) + ')';
 
     if (Scope.empty() || Scope == Spelling.nameSpace()) {
-      if (TestStringMap.contains(Spelling.name())) {
+      if (TestStringMap.contains(Spelling.name()))
         TestStringMap[Spelling.name()] += " || " + TestStr;
-      } else {
+      else
         TestStringMap[Spelling.name()] = TestStr;
-      }
     }
   }
 
   // Create the actual string switch statement after all the attributes have
-  // been parsed
-  for (auto &entry : TestStringMap) {
-    OS << "    .Case(\"" << entry.getKey() << "\", " << entry.getValue()
+  // been parsed.
+  for (auto &Entry : TestStringMap) {
+    OS << "    .Case(\"" << Entry.getKey() << "\", " << Entry.getValue()
        << ")\n";
   }
 

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to