CodeQL library for C/C++

Module HashCons

Provides an implementation of Hash consing. See

The predicate hashCons converts an expression into a HashCons, which is an abstract type presenting the hash-cons of the expression. If two expressions have the same HashCons then they are structurally equal.

Important note: this library ignores the possibility that the value of an expression might change between one occurrence and the next. For example:

x = a+b;
y = a+b;

In this example, both copies of the expression a+b will hash-cons to the same value, even though the value of a has changed. This is the intended behavior of this library. If you care about the value of the expression being the same, then you should use the GlobalValueNumbering library instead.

To determine if the expression x is structurally equal to the expression y, use the library like this:

hashCons(x) = hashCons(y)

Import path

import semmle.code.cpp.valuenumbering.HashCons



Provides classes and predicates for working with C/C++ code.



Gets the hash cons of field initializer expressions [0..i), where i > 0, for the class aggregate literal cal of type c, where head is the hash cons of the i’th initializer expression.


Holds if the expression is explicitly handled by hashCons. Unanalyzable expressions still need to be given a hash-cons, but it will be a unique number that is not shared with any other expression.


Gets the hash-cons of expression e.



HashCons is the hash-cons of an expression. The relationship between Expr and HC is many-to-one: every Expr has exactly one HC, but multiple expressions can have the same HC. If two expressions have the same HC, it means that they are structurally equal. The HC is an opaque value. The only use for the HC of an expression is to find other expressions that are structurally equal to it. Use the predicate hashCons to get the HC for an Expr.