Finding a particular constructor

Hi,

I have a class with a large number of constructors which differ only slightly. I'd like to be able to distinguish these by looking at the names of the arguments, but I cannot seem to find them.

class A {
public:
      A(int x);
      A(int x, float y);
      A(double w, int x, char *z);
      ...
}

I'd like to be able to (for example) look at a particular CXXConstructExpr and get the value of "x", whichever position "x" may have.

Where should I be looking?

Call getConstructor() on your CXXConstructExpr to get the CXXConstructorDecl that matches your function. Then iterate over the parameters till you find the one called “x”. Get the index of the parameter and use getArg(index) on CXXConstructExpr to get the argument corresponding to “x”.

Call getConstructor() on your CXXConstructExpr to get the
CXXConstructorDecl that matches your function. Then iterate over the
parameters till you find the one called "x". Get the index of the
parameter and use getArg(index) on CXXConstructExpr to get the argument
corresponding to "x".

How do I get the name of the parameter? I tried doing

       for (auto it = ctorExpr->getConstructor()->param_begin();
           it != ctorExpr->getConstructor()->param_end(); ++it)
       {
         cerr << (*it)->getDeclName().getNameAsString() << endl;
       }

but all I get are empty strings.

Call getConstructor() on your CXXConstructExpr to get the
CXXConstructorDecl that matches your function. Then iterate over the
parameters till you find the one called "x". Get the index of the
parameter and use getArg(index) on CXXConstructExpr to get the argument
corresponding to "x".

How do I get the name of the parameter? I tried doing

      for (auto it = ctorExpr->getConstructor()->**param_begin();
          it != ctorExpr->getConstructor()->**param_end(); ++it)
      {
        cerr << (*it)->getDeclName().**getNameAsString() << endl;
      }

but all I get are empty strings.

That code doesn't compile for me. getDeclName() returns a DeclarationName
which doesn't have a getNameAsString() function. I think either:

(*it)->getDeclName().**getAsString()

or:

(*it)->getNameAsString()

should work better.

I still only get empty strings.

I'll try to construct a minimal example to recreate this.

The way I’d go would be to first locate the CXXConstructorDecl that is being called, look through its parameter list, get the index of the parameter named “x”, and then find the expression on that index in the argument list.

So as far as I know there is no more direct way of doing this.

Woops, sorry, for some reason I missed that there already have been plenty of answers.