1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 15:28:11 +00:00

JSSpecCompiler: Recurse into the correct subtrees in RecursiveASTVisitor

RecursiveASTVisitor was recursing into the subtrees of an old root if it
was changed in on_entry callback. Fix that by querying root pointer just
after on_entry callback returns. While on it, also use
`AK::TemporaryChange` instead of setting `m_current_subtree_pointer`
manually.

As it turns out, `FunctionCallCanonicalizationPass` was relying on being
able to replace tree on entry, and the bug in RecursiveASTVisitor made
the pass to not fully canonicalize nested function calls.

The changes to GenericASTPass.cpp alone are enough to fix the problem
but it is canonical (for some definition of canonicity) to only change
trees in on_leave. Therefore, the commit also switches
FunctionCallCanonicalizationPass to on_leave callback.

A test for this fix and one from the previous commit is also included.
This commit is contained in:
Dan Klishch 2024-01-16 01:07:00 -05:00 committed by Andrew Kaster
parent 33b36476d9
commit b4a9fde756
6 changed files with 77 additions and 9 deletions

View file

@ -4,8 +4,10 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Compiler/GenericASTPass.h"
#include <AK/TemporaryChange.h>
#include "AST/AST.h"
#include "Compiler/GenericASTPass.h"
#include "Function.h"
namespace JSSpecCompiler {
@ -34,10 +36,10 @@ void RecursiveASTVisitor::replace_current_node_with(NullableTree tree)
RecursionDecision RecursiveASTVisitor::recurse(Tree root, NodeSubtreePointer& pointer)
{
RecursionDecision decision;
TemporaryChange change { m_current_subtree_pointer, &pointer };
m_current_subtree_pointer = &pointer;
decision = on_entry(root);
RecursionDecision decision = on_entry(root);
root = pointer.get({});
if (decision == RecursionDecision::Recurse) {
for (auto& child : root->subtrees()) {
@ -46,7 +48,6 @@ RecursionDecision RecursiveASTVisitor::recurse(Tree root, NodeSubtreePointer& po
}
}
m_current_subtree_pointer = &pointer;
on_leave(root);
return RecursionDecision::Continue;