[clang-tools-extra] ed1b327 - [pseudo] Print the GSS::Node details when the unittest fails, NFC.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Tue May 3 13:07:21 PDT 2022
Author: Haojian Wu
Date: 2022-05-03T22:06:10+02:00
New Revision: ed1b32791dbb4c02cd761742a63cdf1e9d644ae6
URL: https://github.com/llvm/llvm-project/commit/ed1b32791dbb4c02cd761742a63cdf1e9d644ae6
DIFF: https://github.com/llvm/llvm-project/commit/ed1b32791dbb4c02cd761742a63cdf1e9d644ae6.diff
LOG: [pseudo] Print the GSS::Node details when the unittest fails, NFC.
Added:
Modified:
clang-tools-extra/pseudo/include/clang-pseudo/GLR.h
clang-tools-extra/pseudo/lib/GLR.cpp
clang-tools-extra/pseudo/unittests/GLRTest.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/pseudo/include/clang-pseudo/GLR.h b/clang-tools-extra/pseudo/include/clang-pseudo/GLR.h
index d66dbe5897762..f4eb424dd53fa 100644
--- a/clang-tools-extra/pseudo/include/clang-pseudo/GLR.h
+++ b/clang-tools-extra/pseudo/include/clang-pseudo/GLR.h
@@ -108,6 +108,7 @@ struct GSS {
llvm::BumpPtrAllocator Arena;
unsigned NodeCount = 0;
};
+llvm::raw_ostream &operator<<(llvm::raw_ostream &, const GSS::Node &);
// Parameters for the GLR parsing.
struct ParseParams {
diff --git a/clang-tools-extra/pseudo/lib/GLR.cpp b/clang-tools-extra/pseudo/lib/GLR.cpp
index 542533ee801d4..c1fdd8680ca85 100644
--- a/clang-tools-extra/pseudo/lib/GLR.cpp
+++ b/clang-tools-extra/pseudo/lib/GLR.cpp
@@ -12,6 +12,7 @@
#include "clang/Basic/TokenKinds.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FormatVariadic.h"
@@ -26,6 +27,15 @@ namespace pseudo {
using StateID = LRTable::StateID;
+llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const GSS::Node &N) {
+ std::vector<std::string> ParentStates;
+ for (const auto *Parent : N.parents())
+ ParentStates.push_back(llvm::formatv("{0}", Parent->State));
+ OS << llvm::formatv("state {0}, parsed symbol {1}, parents {2}", N.State,
+ N.Payload->symbol(), llvm::join(ParentStates, ", "));
+ return OS;
+}
+
const ForestNode &glrParse(const TokenStream &Tokens,
const ParseParams &Params) {
llvm::ArrayRef<ForestNode> Terminals = Params.Forest.createTerminals(Tokens);
diff --git a/clang-tools-extra/pseudo/unittests/GLRTest.cpp b/clang-tools-extra/pseudo/unittests/GLRTest.cpp
index fc19e0eec7ccc..1998c27dd90a5 100644
--- a/clang-tools-extra/pseudo/unittests/GLRTest.cpp
+++ b/clang-tools-extra/pseudo/unittests/GLRTest.cpp
@@ -19,6 +19,14 @@
namespace clang {
namespace pseudo {
+
+llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
+ const std::vector<const GSS::Node *> &Heads) {
+ for (const auto *Head : Heads)
+ OS << *Head << "\n";
+ return OS;
+}
+
namespace {
using Action = LRTable::Action;
@@ -28,16 +36,6 @@ MATCHER_P(state, StateID, "") { return arg->State == StateID; }
MATCHER_P(parsedSymbol, FNode, "") { return arg->Payload == FNode; }
MATCHER_P(parsedSymbolID, SID, "") { return arg->Payload->symbol() == SID; }
-
-// llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const NewHeadResult &R) {
-// std::vector<std::string> ParentStates;
-// for (const auto &P : R.Parents)
-// ParentStates.push_back(llvm::formatv("{0}", P->State));
-// OS << llvm::formatv("state {0}, parsed symbol {1}, parents {2}", R.State,
-// R.Parsed->symbol(), llvm::join(ParentStates, " "));
-// return OS;
-// }
-
testing::Matcher<const GSS::Node *>
parents(llvm::ArrayRef<const GSS::Node *> Parents) {
return testing::Property(&GSS::Node::parents,
@@ -134,7 +132,8 @@ TEST_F(GLRTest, ShiftMergingHeads) {
AllOf(state(4), parsedSymbol(&SemiTerminal),
parents({GSSNode1, GSSNode2})),
AllOf(state(5), parsedSymbol(&SemiTerminal),
- parents({GSSNode3}))));
+ parents({GSSNode3}))))
+ << NewHeadResults;
}
TEST_F(GLRTest, ReduceConflictsSplitting) {
@@ -161,13 +160,12 @@ TEST_F(GLRTest, ReduceConflictsSplitting) {
{GSSNode1, Action::reduce(ruleFor("enum-name"))}};
glrReduce(PendingReduce, {*G, Table, Arena, GSStack},
captureNewHeads());
- // Verify
EXPECT_THAT(NewHeadResults,
testing::UnorderedElementsAre(
AllOf(state(2), parsedSymbolID(id("class-name")),
parents({GSSNode0})),
AllOf(state(3), parsedSymbolID(id("enum-name")),
- parents({GSSNode0}))));
+ parents({GSSNode0})))) << NewHeadResults;
}
TEST_F(GLRTest, ReduceSplittingDueToMultipleBases) {
@@ -205,7 +203,7 @@ TEST_F(GLRTest, ReduceSplittingDueToMultipleBases) {
AllOf(state(5), parsedSymbolID(id("ptr-operator")),
parents({GSSNode2})),
AllOf(state(6), parsedSymbolID(id("ptr-operator")),
- parents({GSSNode3}))));
+ parents({GSSNode3})))) << NewHeadResults;
// Verify that the payload of the two new heads is shared, only a single
// ptr-operator node is created in the forest.
EXPECT_EQ(NewHeadResults[0]->Payload, NewHeadResults[1]->Payload);
@@ -258,7 +256,8 @@ TEST_F(GLRTest, ReduceJoiningWithMultipleBases) {
// Verify that the stack heads are joint at state 5 after reduces.
EXPECT_THAT(NewHeadResults, testing::UnorderedElementsAre(AllOf(
state(5), parsedSymbolID(id("type-name")),
- parents({GSSNode1, GSSNode2}))));
+ parents({GSSNode1, GSSNode2}))))
+ << NewHeadResults;
// Verify that we create an ambiguous ForestNode of two parses of `type-name`.
EXPECT_EQ(NewHeadResults.front()->Payload->dumpRecursive(*G),
"[ 1, end) type-name := <ambiguous>\n"
@@ -312,7 +311,8 @@ TEST_F(GLRTest, ReduceJoiningWithSameBase) {
EXPECT_THAT(NewHeadResults, testing::UnorderedElementsAre(
AllOf(state(5), parsedSymbolID(id("pointer")),
- parents({GSSNode0}))));
+ parents({GSSNode0}))))
+ << NewHeadResults;
EXPECT_EQ(NewHeadResults.front()->Payload->dumpRecursive(*G),
"[ 0, end) pointer := <ambiguous>\n"
"[ 0, end) ├─pointer := class-name *\n"
More information about the cfe-commits
mailing list