1

I am converting C++ code to C++11. Since C++11 supports move construction I am replacing methods like

void foo(const Bar& obj);

with

void foo(Bar obj);

in places where I think it makes sense, for example in assignment operators. Unfortunately, the only way I know of that detects if the move constructor is actually used is adding debug messages and run the code.

What I would like to have is information about what the compiler is doing while compiling the source, to get an idea where it is using move construction and where not (and possibly why) so that I can get a better understanding where I need to change the program and where changes actually improved it (to avoid unnecessary copy constructions).

Is there a way to get this information? Maybe with CLang?

hochl
  • 12,524
  • 10
  • 53
  • 87
  • You can use GCC's `-fdump-tree-gimple` and examine the GIMPLE output to see which constructor is called. But do you really need to? If there's a move constructor why _wouldn't_ it be used? It will be used if it can be, and not otherwise. (Without knowing what the body of `foo` does it isn't possible to say whether your example change actually makes sense.) – Jonathan Wakely Oct 07 '15 at 21:42
  • Great -- that's going in the right direction!!! Why wouldn't it be used ... yes, I need something to figure out *if* it gets used or not, to help me understand in which cases I'm doing something wrong. As soon as I come up with a good example I'll extend my question. – hochl Oct 07 '15 at 23:13
  • acutally I'm still searching for a way to get annotated source what the compiler really does when compiling my programs :/ – hochl Jun 16 '16 at 10:41
  • Doesn't the GIMPLE output give that? – Jonathan Wakely Jun 16 '16 at 13:55
  • I'd prefer a clear output where you can see where a copy constructor or move constructor is used with source lines. I'm thinking this might be achieved with a MELT plugin, what do you think? It just seems really obscure to achieve it. Besides this, yes, sifting through tons of cryptic GIMPLE output gives this information, if you have the time ... :( – hochl Jun 16 '16 at 14:33
  • Why MELT rather than the gcc-python-plugin or C plugin API? It might be possible, but maybe not easy. And the output might be illegible, as a function call expression might invoke dozens of constructors or conversions for the various arguments. Maybe I'm still not clear exactly what you're trying to achieve, as I said originally: "It will be used if it can be, and not otherwise." If the problem is that you don't know when a move constructor gets used I think learning the language rules would be more constructive than annotating your code. – Jonathan Wakely Jun 16 '16 at 14:56
  • No, I'm quite sure about the language. The problem arises if you have tons of code you want to sift through; it would be helpful if you can verify where a copy is happening just by zipping through tons of files. I will see if gcc-python is an easier approach. – hochl Jun 17 '16 at 07:48
  • _"The problem arises"_ ... I still don't know what the problem is. You want to know whether a move constructor gets used, right? So lets say you get annotated source showing where it does/doesn't get used ... how do you know if it's not being used where it "should" be? You have to look at the call site and see if it should be passing an rvalue instead of an lvalue, or if passing an lvalue is intended ... so you still have to look at tons of code, in which case just look at the code without annotating it! A clear problem statement is still missing from the question IMHO. – Jonathan Wakely Jun 17 '16 at 12:21

0 Answers0