diff --git a/runtime/Cpp/runtime/src/tree/xpath/XPath.cpp b/runtime/Cpp/runtime/src/tree/xpath/XPath.cpp index 7ebd1b8691..00c1e2c5d8 100644 --- a/runtime/Cpp/runtime/src/tree/xpath/XPath.cpp +++ b/runtime/Cpp/runtime/src/tree/xpath/XPath.cpp @@ -3,6 +3,7 @@ * can be found in the LICENSE.txt file in the project root. */ +#include #include #include #include @@ -128,14 +129,13 @@ std::unique_ptr XPath::getXPathElement(Token *wordToken, bool anyw } } -static ParserRuleContext dummyRoot; - std::vector XPath::findAll(ParseTree *tree, std::string const& xpath, Parser *parser) { XPath p(parser, xpath); return p.evaluate(tree); } std::vector XPath::evaluate(ParseTree *t) { + ParserRuleContext dummyRoot; dummyRoot.children = { t }; // don't set t's parent. std::vector work = { &dummyRoot }; @@ -158,5 +158,7 @@ std::vector XPath::evaluate(ParseTree *t) { work = next; } + // dummyRoot is local to this call, so it must not be handed back (e.g. for "//*"). + work.erase(std::remove(work.begin(), work.end(), &dummyRoot), work.end()); return work; } diff --git a/runtime/Cpp/runtime/tests/XPathTest.cpp b/runtime/Cpp/runtime/tests/XPathTest.cpp new file mode 100644 index 0000000000..4e37bb32b3 --- /dev/null +++ b/runtime/Cpp/runtime/tests/XPathTest.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include + +#include "gtest/gtest.h" +#include "antlr4-runtime.h" +#include "tree/xpath/XPath.h" + +namespace antlr4 { +namespace { + + class TestContext final : public ParserRuleContext { + public: + using ParserRuleContext::ParserRuleContext; + + size_t getRuleIndex() const override { return 0; } + }; + + class TestParser final : public Parser { + public: + explicit TestParser(TokenStream *input) : Parser(input) {} + + std::string getGrammarFileName() const override { return "Test.g4"; } + + const std::vector& getRuleNames() const override { + static const std::vector ruleNames = { "r" }; + return ruleNames; + } + + const dfa::Vocabulary& getVocabulary() const override { + static const dfa::Vocabulary vocabulary({}, {}); + return vocabulary; + } + + const atn::ATN& getATN() const override { + static const atn::ATN atn; + return atn; + } + }; + + struct Tree final { + std::vector> nodes; + TestContext *root = nullptr; + }; + + // Builds r(r r r ...) with `width` children. + Tree makeTree(size_t width) { + Tree tree; + tree.nodes.push_back(std::make_unique()); + tree.root = tree.nodes.back().get(); + for (size_t i = 0; i < width; ++i) { + tree.nodes.push_back(std::make_unique(tree.root, 0)); + tree.root->addChild(tree.nodes.back().get()); + } + return tree; + } + + TEST(XPath, FindAllReturnsOnlyTreeNodes) { + std::vector> tokens; + tokens.push_back(std::make_unique(Token::EOF)); + ListTokenSource source(std::move(tokens)); + CommonTokenStream stream(&source); + TestParser parser(&stream); + Tree tree = makeTree(3); + + std::vector found = tree::xpath::XPath::findAll(tree.root, "//*", &parser); + ASSERT_EQ(found.size(), tree.nodes.size()); + for (size_t i = 0; i < found.size(); ++i) { + EXPECT_EQ(found[i], tree.nodes[i].get()); + } + } + + TEST(XPath, FindAllIsThreadSafeAcrossParsers) { + constexpr size_t kThreads = 8; + constexpr size_t kIterations = 2000; + constexpr size_t kWidth = 4; + + { + // Populate the shared token type / rule index caches up front so that + // only XPath::findAll runs concurrently below. + std::vector> tokens; + tokens.push_back(std::make_unique(Token::EOF)); + ListTokenSource source(std::move(tokens)); + CommonTokenStream stream(&source); + TestParser parser(&stream); + parser.getTokenType("r"); + parser.getRuleIndex("r"); + } + + std::vector threads; + std::vector failures(kThreads, 0); + for (size_t t = 0; t < kThreads; ++t) { + threads.emplace_back([&failures, t]() { + std::vector> tokens; + tokens.push_back(std::make_unique(Token::EOF)); + ListTokenSource source(std::move(tokens)); + CommonTokenStream stream(&source); + TestParser parser(&stream); + Tree tree = makeTree(kWidth); + + for (size_t i = 0; i < kIterations; ++i) { + std::vector found = tree::xpath::XPath::findAll(tree.root, "/r/r", &parser); + bool ok = found.size() == kWidth; + for (auto *node : found) { + ok = ok && node->parent == tree.root; + } + if (!ok) { + ++failures[t]; + } + } + }); + } + for (auto &thread : threads) { + thread.join(); + } + for (size_t t = 0; t < kThreads; ++t) { + EXPECT_EQ(failures[t], 0) << "thread " << t << " got nodes from another thread's tree"; + } + } + +} // namespace +} // namespace antlr4