Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 6de313f

Browse files
author
Denys Smirnov
committed
expose node hashes; fixes #78
Signed-off-by: Denys Smirnov <[email protected]>
1 parent 6086f5a commit 6de313f

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

src/libuast.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ namespace uast {
5858
virtual T node() = 0;
5959
};
6060

61+
// NodeHash is a hash of a node subtree.
62+
struct NodeHash {
63+
uint8_t data[UAST_HASH_SIZE];
64+
};
65+
6166
// Context is a common interface implemented by all UAST contexts.
6267
template<class T> class Context {
6368
public:
@@ -75,6 +80,7 @@ namespace uast {
7580

7681
virtual Iterator<T>* Filter(T root, std::string query) = 0;
7782
virtual Iterator<T>* Iterate(T root, TreeOrder order) = 0;
83+
virtual NodeHash Hash(T root, HashFlags flags) = 0;
7884
};
7985

8086
// NodeCreator is an interface that creates new UAST nodes.
@@ -351,6 +357,11 @@ namespace uast {
351357
CheckError();
352358
return new RawIterator(it);
353359
}
360+
NodeHash Hash(NodeHandle root, HashFlags flags) {
361+
NodeHash h;
362+
UastHash(ctx, root, (void*)&h, flags);
363+
return h;
364+
}
354365
};
355366

356367
// PtrIterator is an iterator that casts NodeHandle directly to pointer type T.
@@ -414,6 +425,9 @@ namespace uast {
414425
auto it = new PtrIterator<T>(raw, true);
415426
return it;
416427
}
428+
NodeHash Hash(T root, HashFlags flags) {
429+
return ctx->Hash(ToHandle(root), flags);
430+
}
417431
void CheckError(){
418432
ctx->CheckError();
419433
}

src/uast.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ typedef struct UastIterator {
9191
typedef enum { UAST_BINARY = 0, UAST_YAML = 1 } UastFormat;
9292

9393
typedef enum {
94+
HASH_ALL = 0x0,
9495
HASH_NO_POS = 0x1,
9596
} HashFlags;
9697

tests/main.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ int main() {
2828

2929
// add the tests to the suite
3030
ADD_TEST(suite, "test of RoleNameForId()", TestRoleNameForId);
31+
ADD_TEST(suite, "test node hash", TestNodeHash);
3132
ADD_TEST(suite, "test of UastFilter() pointers", TestUastFilterPointers);
3233
ADD_TEST(suite, "test iteration (preorder)", TestUastIteratorPreOrder);
3334
ADD_TEST(suite, "test of UastFilter() counting", TestUastFilterCount);

tests/nodes_test.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,24 @@ void TestNodeFindError() {
642642
UastFree(ctx);
643643
}
644644

645+
void TestNodeHash() {
646+
Uast *ctx = NewUastMock();
647+
Node* module = newObject("Module");
648+
Node* child = newObject("Child");
649+
module->SetChild("field", child);
650+
651+
unsigned char hash[UAST_HASH_SIZE];
652+
unsigned char exp[UAST_HASH_SIZE] = {
653+
0xe6, 0xd2, 0x53, 0xe1, 0x26, 0xa, 0xaa, 0xa9,
654+
0x37, 0xcc, 0xfc, 0x42, 0xf, 0x52, 0x65, 0x48,
655+
0x1d, 0x59, 0x18, 0xce, 0x1, 0xad, 0xda, 0xa2,
656+
0x82, 0x4c, 0x74, 0x77, 0xae, 0xa1, 0x26, 0xb5};
657+
UastHash(ctx, NodeHandle(module), (void*)hash, HASH_ALL);
658+
CU_ASSERT_FATAL(memcmp(hash, exp, UAST_HASH_SIZE) == 0);
659+
660+
UastFree(ctx);
661+
}
662+
645663
void TestEmptyResult() {
646664
Uast *ctx = NewUastMock();
647665
Node* module = newObject("Module");

0 commit comments

Comments
 (0)