Whole Tomato Software Forums
Whole Tomato Software Forums
Main Site | Profile | Register | Active Topics | Members | Search | FAQ
User name:
Password:
Save Password
Forgot your password?

 All Forums
 Visual Assist
 Technical Support
 find references & struct initialization
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

eve
Tomato Guru

Belgium
101 Posts

Posted - Jun 06 2007 :  07:45:03 AM  Show Profile  Reply with Quote
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

feline
Whole Tomato Software

United Kingdom
18939 Posts

Posted - Jun 06 2007 :  09:19:04 AM  Show Profile  Reply with Quote
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?

zen is the art of being at one with the two'ness
Go to Top of Page

eve
Tomato Guru

Belgium
101 Posts

Posted - Jun 06 2007 :  09:49:29 AM  Show Profile  Reply with Quote
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... :-(
Go to Top of Page

feline
Whole Tomato Software

United Kingdom
18939 Posts

Posted - Jun 06 2007 :  11:56:43 AM  Show Profile  Reply with Quote
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?

zen is the art of being at one with the two'ness
Go to Top of Page

eve
Tomato Guru

Belgium
101 Posts

Posted - Jun 06 2007 :  12:09:36 PM  Show Profile  Reply with Quote
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 :-))
Go to Top of Page

feline
Whole Tomato Software

United Kingdom
18939 Posts

Posted - Jun 06 2007 :  2:08:17 PM  Show Profile  Reply with Quote
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?

zen is the art of being at one with the two'ness
Go to Top of Page

eve
Tomato Guru

Belgium
101 Posts

Posted - Jun 07 2007 :  01:49:36 AM  Show Profile  Reply with Quote
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.

Go to Top of Page

feline
Whole Tomato Software

United Kingdom
18939 Posts

Posted - Jun 07 2007 :  12:06:09 PM  Show Profile  Reply with Quote
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.

zen is the art of being at one with the two'ness
Go to Top of Page

eve
Tomato Guru

Belgium
101 Posts

Posted - Jun 08 2007 :  01:50:52 AM  Show Profile  Reply with Quote
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 };
Go to Top of Page

feline
Whole Tomato Software

United Kingdom
18939 Posts

Posted - Jun 08 2007 :  07:48:14 AM  Show Profile  Reply with Quote
I have put a feature request in for this:

case=6990

zen is the art of being at one with the two'ness
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
© 2023 Whole Tomato Software, LLC Go To Top Of Page
Snitz Forums 2000