April 15, 2022

Let's say I have this example program.
I want to get the arguments of all the some_function(); in the whole program.
Even if the scope of the function call is never executed. (Ex. due to IF statement being negative.)

I tried to use __traits but it seems to not gather any information about function calls.
Or I'm missing something.

import std.stdio;
import std.traits;


void some_function(string argument){}


void main(){
  some_function("someargument1");
  some_function("someargument2");

  if (false){
    some_function("someargument3");
  }


/* __traits allMembers does not print function calls.
 I tried to use allMembers of __traits, but it does not contain the function calls.
 Only the functions definitions/declarations inside a module.
*/
foreach(memberss; __traits(allMembers, mixin(__MODULE__))){
		write(" ", isSomeFunction!(mixin(memberss)));
		writeln(" \t", fullyQualifiedName!(mixin(memberss)));
		
	}

April 15, 2022

On Friday, 15 April 2022 at 18:11:11 UTC, BoQsc wrote:

>

Let's say I have this example program.
I want to get the arguments of all the some_function(); in the whole program.
Even if the scope of the function call is never executed. (Ex. due to IF statement being negative.)

You can't do this with any of D's built-in introspection features (like __traits), because those features have no way of looking inside functions.

However, you can probably write a program to do this using the DMD frontend as a library.

Dub package (use the dmd:frontend subpackage): https://code.dlang.org/packages/dmd
Documentation: https://dlang.org/library/dmd/frontend.html

Unfortunately the documentation is somewhat incomplete, so you may need to spend some time reading the source code to figure out how it all works. Don't hesitate to ask if you have any questions.