Skip to content

Attributes Examples

João Bispo edited this page May 19, 2022 · 3 revisions

$function

$function.canonical, .isCanonical

In C/C++ you can have several nodes $function for the "same" function: one for the definition (or implementation) of the function, and one for each of the declarations (or prototypes).

In some cases it might be useful to treat all those cases as the same function, which is what $function.canonical does. It gives priority to the function definition, and if it does not exist, returns the first function declaration. $call.function also returns the canonical function.

$function.isCanonical returns true if the function is already canonical, and false otherwise.

Consider the following C code:

void foo();

void foo() {
	
}

void foo();

void bar();

void bar();

This Clava snippet:

for(const $function of Query.search("function", {isCanonical: true})) {
	println("Canonical function " + $function.signature + " at line " + $function.line);
}

Outputs:

Canonical function foo() at line 3
Canonical function bar() at line 9
Clone this wiki locally