[clang] 385752c - [clang][TableGen] Fix Duplicate Entries in TableGen (#140828)

via cfe-commits cfe-commits at lists.llvm.org
Wed May 21 09:27:52 PDT 2025


Author: Vincent
Date: 2025-05-21T12:27:48-04:00
New Revision: 385752c9b5f20416ed56080be46fe4b84bb59104

URL: https://github.com/llvm/llvm-project/commit/385752c9b5f20416ed56080be46fe4b84bb59104
DIFF: https://github.com/llvm/llvm-project/commit/385752c9b5f20416ed56080be46fe4b84bb59104.diff

LOG: [clang][TableGen] Fix Duplicate Entries in TableGen (#140828)

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

resolves #140701

Added: 
    clang/test/AST/ast-dump-riscv-attributes.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/utils/TableGen/ClangAttrEmitter.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b466f3758e0b6..648b32c659b4f 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
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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..45fb0d4735c86 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 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) {
     // 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,25 @@ 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";
 }
 


        


More information about the cfe-commits mailing list