Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions runtime/Cpp/runtime/src/tree/xpath/XPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* can be found in the LICENSE.txt file in the project root.
*/

#include <algorithm>
#include <memory>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -128,14 +129,13 @@ std::unique_ptr<XPathElement> XPath::getXPathElement(Token *wordToken, bool anyw
}
}

static ParserRuleContext dummyRoot;

std::vector<ParseTree *> XPath::findAll(ParseTree *tree, std::string const& xpath, Parser *parser) {
XPath p(parser, xpath);
return p.evaluate(tree);
}

std::vector<ParseTree *> XPath::evaluate(ParseTree *t) {
ParserRuleContext dummyRoot;
dummyRoot.children = { t }; // don't set t's parent.

std::vector<ParseTree *> work = { &dummyRoot };
Expand All @@ -158,5 +158,7 @@ std::vector<ParseTree *> 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;
}
123 changes: 123 additions & 0 deletions runtime/Cpp/runtime/tests/XPathTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#include <memory>
#include <string>
#include <thread>
#include <vector>

#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<std::string>& getRuleNames() const override {
static const std::vector<std::string> 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<std::unique_ptr<TestContext>> 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<TestContext>());
tree.root = tree.nodes.back().get();
for (size_t i = 0; i < width; ++i) {
tree.nodes.push_back(std::make_unique<TestContext>(tree.root, 0));
tree.root->addChild(tree.nodes.back().get());
}
return tree;
}

TEST(XPath, FindAllReturnsOnlyTreeNodes) {
std::vector<std::unique_ptr<Token>> tokens;
tokens.push_back(std::make_unique<CommonToken>(Token::EOF));
ListTokenSource source(std::move(tokens));
CommonTokenStream stream(&source);
TestParser parser(&stream);
Tree tree = makeTree(3);

std::vector<tree::ParseTree *> 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<std::unique_ptr<Token>> tokens;
tokens.push_back(std::make_unique<CommonToken>(Token::EOF));
ListTokenSource source(std::move(tokens));
CommonTokenStream stream(&source);
TestParser parser(&stream);
parser.getTokenType("r");
parser.getRuleIndex("r");
}

std::vector<std::thread> threads;
std::vector<int> failures(kThreads, 0);
for (size_t t = 0; t < kThreads; ++t) {
threads.emplace_back([&failures, t]() {
std::vector<std::unique_ptr<Token>> tokens;
tokens.push_back(std::make_unique<CommonToken>(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<tree::ParseTree *> 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
Loading