Kevin S
Kevin S

Reputation: 531

How can I hash Clang AST Nodes for use as key in a C++ unordered map?

I need to associate external information with Clang AST nodes. I'd like to do this, at least for now, by using such nodes as keys in unordered maps (C++). However, the Clang AST node types don't appear to support the hash functions that would be required to use them as keys in maps. Declaring an unordered map with key type, clang::VarDecl, for example, causes the following compiler error (g++).

/usr/include/c++/7/bits/hashtable_policy.h:87:34: error: no match for call to '(const std::hash<clang::VarDecl>) (const clang::VarDecl&)' 

The narrow question is, Is there a way to use Clang AST nodes as keys in unordered maps in C++?

The broader question is, What is the best way to establish a machine-readable associative store linking AST nodes to associated information?

Upvotes: 2

Views: 335

Answers (1)

Vishesh Agarwal
Vishesh Agarwal

Reputation: 141

You can get a hash of the cursor while iterating over the AST using clang_hashCursor. It is stable across multiple runs on the same file. But I am not sure if it is guaranteed to be unique within a translation unit for multiple cursors, but then again, once you have a value to start with, you can do any operation to keep it unique within the TU.

Upvotes: 1

Related Questions