hokein created this revision.
hokein added a reviewer: sammccall.
hokein requested review of this revision.
Herald added a project: clang.

The linear scan should not escape the TargetedStates range.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120723

Files:
  clang/lib/Tooling/Syntax/Pseudo/LRTable.cpp
  clang/test/Syntax/lr-build-basic.test


Index: clang/test/Syntax/lr-build-basic.test
===================================================================
--- clang/test/Syntax/lr-build-basic.test
+++ clang/test/Syntax/lr-build-basic.test
@@ -1,24 +1,29 @@
 _ := expr
-expr := IDENTIFIER
+expr := id
+id := IDENTIFIER
 
 # RUN: clang-pseudo -grammar %s -print-graph | FileCheck %s 
--check-prefix=GRAPH
 #      GRAPH: States:
 # GRPAH-NEXT: State 0
 # GRPAH-NEXT:     _ :=  • expr
-# GRPAH-NEXT:     expr :=  • IDENTIFIER
+# GRPAH-NEXT:     expr :=  • id
+# GRPAH-NEXT:     id :=  • IDENTIFIER
 # GRPAH-NEXT: State 1
 # GRPAH-NEXT:     _ := expr • 
 # GRPAH-NEXT: State 2
-# GRPAH-NEXT:     expr := IDENTIFIER • 
-# GRPAH-NEXT: 0 ->[expr] 1
-# GRPAH-NEXT: 0 ->[IDENTIFIER] 2
+# GRPAH-NEXT:     expr := id • 
+# GRPAH-NEXT: State 3
+# GRPAH-NEXT:     id := IDENTIFIER • 
 
 # RUN: clang-pseudo -grammar %s -print-table | FileCheck %s 
--check-prefix=TABLE
 #      TABLE: LRTable:
 # TABLE-NEXT: State 0
-# TABLE-NEXT:     'IDENTIFIER': shift state 2
+# TABLE-NEXT:     'IDENTIFIER': shift state 3
 # TABLE-NEXT:     'expr': go to state 1
+# TABLE-NEXT:     'id': go to state 2
 # TABLE-NEXT: State 1
 # TABLE-NEXT:     'EOF': accept
 # TABLE-NEXT: State 2
-# TABLE-NEXT:     'EOF': reduce by rule 1 'expr := IDENTIFIER'
+# TABLE-NEXT:     'EOF': reduce by rule 1 'expr := id'
+# TABLE-NEXT: State 3
+# TABLE-NEXT:     'EOF': reduce by rule 2 'id := IDENTIFIER'
Index: clang/lib/Tooling/Syntax/Pseudo/LRTable.cpp
===================================================================
--- clang/lib/Tooling/Syntax/Pseudo/LRTable.cpp
+++ clang/lib/Tooling/Syntax/Pseudo/LRTable.cpp
@@ -110,14 +110,15 @@
 
   assert(llvm::is_sorted(TargetedStates) &&
          "subrange of the StateIdx should be sorted!");
-  const LRTable::StateID *It = llvm::partition_point(
+  const LRTable::StateID *Start = llvm::partition_point(
       TargetedStates, [&Src](LRTable::StateID S) { return S < Src; });
-  if (It == TargetedStates.end())
+  if (Start == TargetedStates.end())
     return {};
-  size_t Start = It - States.data(), End = Start;
-  while (End < States.size() && States[End] == Src)
+  const LRTable::StateID *End = Start;
+  while (End != TargetedStates.end() && *End == Src)
     ++End;
-  return llvm::makeArrayRef(&Actions[Start], End - Start);
+  return llvm::makeArrayRef(&Actions[Start - States.data()],
+                            /*length=*/End - Start);
 }
 
 } // namespace pseudo


Index: clang/test/Syntax/lr-build-basic.test
===================================================================
--- clang/test/Syntax/lr-build-basic.test
+++ clang/test/Syntax/lr-build-basic.test
@@ -1,24 +1,29 @@
 _ := expr
-expr := IDENTIFIER
+expr := id
+id := IDENTIFIER
 
 # RUN: clang-pseudo -grammar %s -print-graph | FileCheck %s --check-prefix=GRAPH
 #      GRAPH: States:
 # GRPAH-NEXT: State 0
 # GRPAH-NEXT:     _ :=  • expr
-# GRPAH-NEXT:     expr :=  • IDENTIFIER
+# GRPAH-NEXT:     expr :=  • id
+# GRPAH-NEXT:     id :=  • IDENTIFIER
 # GRPAH-NEXT: State 1
 # GRPAH-NEXT:     _ := expr • 
 # GRPAH-NEXT: State 2
-# GRPAH-NEXT:     expr := IDENTIFIER • 
-# GRPAH-NEXT: 0 ->[expr] 1
-# GRPAH-NEXT: 0 ->[IDENTIFIER] 2
+# GRPAH-NEXT:     expr := id • 
+# GRPAH-NEXT: State 3
+# GRPAH-NEXT:     id := IDENTIFIER • 
 
 # RUN: clang-pseudo -grammar %s -print-table | FileCheck %s --check-prefix=TABLE
 #      TABLE: LRTable:
 # TABLE-NEXT: State 0
-# TABLE-NEXT:     'IDENTIFIER': shift state 2
+# TABLE-NEXT:     'IDENTIFIER': shift state 3
 # TABLE-NEXT:     'expr': go to state 1
+# TABLE-NEXT:     'id': go to state 2
 # TABLE-NEXT: State 1
 # TABLE-NEXT:     'EOF': accept
 # TABLE-NEXT: State 2
-# TABLE-NEXT:     'EOF': reduce by rule 1 'expr := IDENTIFIER'
+# TABLE-NEXT:     'EOF': reduce by rule 1 'expr := id'
+# TABLE-NEXT: State 3
+# TABLE-NEXT:     'EOF': reduce by rule 2 'id := IDENTIFIER'
Index: clang/lib/Tooling/Syntax/Pseudo/LRTable.cpp
===================================================================
--- clang/lib/Tooling/Syntax/Pseudo/LRTable.cpp
+++ clang/lib/Tooling/Syntax/Pseudo/LRTable.cpp
@@ -110,14 +110,15 @@
 
   assert(llvm::is_sorted(TargetedStates) &&
          "subrange of the StateIdx should be sorted!");
-  const LRTable::StateID *It = llvm::partition_point(
+  const LRTable::StateID *Start = llvm::partition_point(
       TargetedStates, [&Src](LRTable::StateID S) { return S < Src; });
-  if (It == TargetedStates.end())
+  if (Start == TargetedStates.end())
     return {};
-  size_t Start = It - States.data(), End = Start;
-  while (End < States.size() && States[End] == Src)
+  const LRTable::StateID *End = Start;
+  while (End != TargetedStates.end() && *End == Src)
     ++End;
-  return llvm::makeArrayRef(&Actions[Start], End - Start);
+  return llvm::makeArrayRef(&Actions[Start - States.data()],
+                            /*length=*/End - Start);
 }
 
 } // namespace pseudo
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to