Author: Haojian Wu
Date: 2022-08-11T13:16:01+02:00
New Revision: e935f7fd0cbc698438bdabc3b836ab25baa9a174

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

LOG: [pseudo] Fix a bug in checking the duplicated grammar rules.

Added: 
    

Modified: 
    clang-tools-extra/pseudo/lib/grammar/GrammarBNF.cpp
    clang-tools-extra/pseudo/unittests/GrammarTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/pseudo/lib/grammar/GrammarBNF.cpp 
b/clang-tools-extra/pseudo/lib/grammar/GrammarBNF.cpp
index 410b151ff2145..9706f17eab848 100644
--- a/clang-tools-extra/pseudo/lib/grammar/GrammarBNF.cpp
+++ b/clang-tools-extra/pseudo/lib/grammar/GrammarBNF.cpp
@@ -327,8 +327,13 @@ class GrammarBuilder {
             "Token-like name {0} is used as a nonterminal", 
G.symbolName(SID)));
       }
     }
-    for (RuleID RID = 0; RID + 1u < T.Rules.size(); ++RID) {
-      if (T.Rules[RID] == T.Rules[RID + 1])
+    llvm::DenseSet<llvm::hash_code> VisitedRules;
+    for (RuleID RID = 0; RID < T.Rules.size(); ++RID) {
+      const auto &R = T.Rules[RID];
+      auto Code = llvm::hash_combine(
+          R.Target, llvm::hash_combine_range(R.seq().begin(), R.seq().end()));
+      auto [_, New] = VisitedRules.insert(Code);
+      if (!New)
         Diagnostics.push_back(
             llvm::formatv("Duplicate rule: `{0}`", G.dumpRule(RID)));
     }

diff  --git a/clang-tools-extra/pseudo/unittests/GrammarTest.cpp 
b/clang-tools-extra/pseudo/unittests/GrammarTest.cpp
index e8641f8edf6f1..6b6b47b8a2dbe 100644
--- a/clang-tools-extra/pseudo/unittests/GrammarTest.cpp
+++ b/clang-tools-extra/pseudo/unittests/GrammarTest.cpp
@@ -136,6 +136,18 @@ TEST_F(GrammarTest, Diagnostics) {
                          "Unknown attribute 'unknown'"));
 }
 
+TEST_F(GrammarTest, DuplicatedDiagnostics) {
+  build(R"cpp(
+    _ := test
+
+    test := INT
+    test := DOUBLE
+    test := INT
+  )cpp");
+
+  EXPECT_THAT(Diags, UnorderedElementsAre("Duplicate rule: `test := INT`"));
+}
+
 TEST_F(GrammarTest, FirstAndFollowSets) {
   build(
       R"bnf(


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

Reply via email to