T O P I C R E V I E W |
eve |
Posted - Jun 06 2007 : 07:45:03 AM Hi,
suppose you have C-style struct like this:
struct MyStruct { int member1; int member2; };
and that you use initialize it like this: MyStruct s = { 1, 2 }; // INIT
and use it like this: cout << s.member1 << endl; // USE
If you now do a 'find references' on member1 , then VAX will only show the 'USE' line, but not the 'INIT' line,
I don't know if this is a bug or a sug, but it can be rather confusing if you think that find reference will show all places where a struct member is used.
Regards, eli
|
9 L A T E S T R E P L I E S (Newest First) |
feline |
Posted - Jun 08 2007 : 07:48:14 AM I have put a feature request in for this:
case=6990 |
eve |
Posted - Jun 08 2007 : 01:50:52 AM Nice, I didn't know that this worked. Probably in the time C was invented, the {} keys were expensive to use ;-)
As you probably know, initialisations like this are also allowed:
struct MyStruct { int member1; int member2; };
MyStruct s = { 1 };
I seem to remember that member2 would in this case guaranteed to be initialized to 0, but I'm not entirely sure. If that is true, then the above initialisation is a reference to member2 as well.
Also I read somewhere that C99 supports things like this:
MyStruct s = { .member2 = 2 }; |
feline |
Posted - Jun 07 2007 : 12:06:09 PM Struct initialization on its own sounds more reasonable. I am wary of opening the flood gates to a load of related requests here
Thinking simply about structure initialisation the following code compiles perfectly happily in VS2005:
struct MyStruct {
int member1;
int member2;
};
struct BiggerStruct
{
int nBigger1;
MyStruct foo;
};
static void testUseAndInitStructure()
{
MyStruct s = { 1, 2 }; // INIT
BiggerStruct foo2 = { 0, 1, 2 }; // INIT of 2 things at once...
// cout << s.member1 << endl; // USE
}
it is the second INIT that bothers me a little.
Initialisation like this has a well defined syntax, I am just trying to think of edge cases that are likely to cause problems. |
eve |
Posted - Jun 07 2007 : 01:49:36 AM For me it is probably not worth the effort in this particular case. But limitations like this undermine my (on probably others) 'trust' in find references, knowing that it can miss certain less obvious references. If you know what I mean.
I have been thinking some more about memset, memcpy and the others: I do no longer agree that VAX should also find these in a 'find references' since these functions work on void pointers. If VAX would have to find these then it would also have to trace the pointers of other usages of the address-off operator. In my opinion the struct initialization construction is something totally different, and I guess, also a bit easier to implement.
|
feline |
Posted - Jun 06 2007 : 2:08:17 PM I am not sure what to say here. I am very reluctant, since I suspect we could encounter some very nasty edge cases if we try to do this.
At the same time, you have identified a limitation of Find References.
Have you considered a regular expression find in files via the IDE? |
eve |
Posted - Jun 06 2007 : 12:09:36 PM Indeed searching for the structure is what I have been doing now. And I did not know about the next result key, thanks for that!
I'm afraid that the search feature will not help me much in this case, because there will be too much false positives (all the member assignments). But I like the search idea nonetheless! If I read your answer, then I get the idea that the chance that 'find references' will ever just match these situations is rather small? Not that I mind too much, my refactorings are nearing the end :-)) |
feline |
Posted - Jun 06 2007 : 11:56:43 AM Ouch. However the same problem occurs with the memset() calls, and its relatives. We are considering some form of find within Find References Results.
When this is done you could use Find References on the structure name, and then search the results for "=" or other things that are likely to indicate code you need to update.
For now a Find References on the structure name and just stepping through each result with the default "next result" key:
http://www.wholetomato.com/products/features/findReferences.asp
is probably the best answer.
Perhaps reworking the code to use a more findable assignment and copy approach while you are at it is a good idea, for next time the code changes? |
eve |
Posted - Jun 06 2007 : 09:49:29 AM Unfortunately yes, we still have a lot of C like code with lots of static structs, mallocs, and memsets.
The biggest problem that I encountered with this is that during a refactoring you might find some initializations (those that use normal assignments), and miss some others. And if you then think your work is done... :-( |
feline |
Posted - Jun 06 2007 : 09:19:04 AM Interesting...
At a basic level Find References is never going to find the INIT line, since the item it is searching for is not used on this line.
Thinking about this, you can make a "similar" argument for "memset()" and "memcpy()" on a structure, both will modify all members of the structure, and neither will be detected by Find References on a member name.
Do you have a lot of code where this happens? |