Improvements to Find all references in Visual Studio 2010
Hello, my name is Raman Sharma, and I am a Program Manager on the VC++ team. Through this blog post, I wish to highlight some changes we have made to an important feature in the C++ IDE, called “Find all References”. As most of you would know, this feature is used to search for references to any element of your code (classes, class members, functions etc.), inside the entire solution. In Visual Studio 2010, we have made some changes to this feature to provide more flexibility.
Let’s look at these changes through an example. Let’s say you have the following piece of code:
Figure 1: Sample Code
The Old World
In Visual Studio 2008, if you invoke “Find all References” on the member function printArea of class Circle as in:
Figure 2: Invoke Find all References
You will see the results showing line numbers 7, 20 & 26 as below:
Figure 3: Results in VS 2008
It is to be noted that the results window does not list all the places the word “printArea” appears. It only lists specifically the places where printArea means the “member function printArea of class Circle” (on which Find all References was invoked). The point is that this is in some sense a compiler-verified-search (I just coined that term but the concept is correctJ) wherein you will get exactly the C++ symbol you searched for.
The New World
In Visual Studio 2010 we have effectively created two modes of search: one that focuses on speed and the other that focuses on accuracy. Needless to say, you can specify the default mode through options and we will remember that setting. More on that later in this post.
If you perform the same operation as described above in VS 2010, by default you will get the following results:
Figure 4: Default results in VS 2010
Please note that this time around, the results window does list all the places the word “printArea” appears (including the comments!). Why? Because we wanted to provide the user with an option to search without invoking the compiler. Needless to say, this option will speed up the search significantly, especially for large projects with a lot of hits.
It’s worth mentioning that this search is unlike just searching for some text in the entire solution. This is because for this search, the C++ IDE uses an index to narrow the list of files to search. So it doesn’t look through all the files in the solution. The outcome is significantly better performance than just “Find in Files” (or grep), especially for large solutions.
However, we understand that there can be instances where you want to reduce the ambiguity by filtering the results further. This means you want better accuracy. If you search for a member function of a class, you only want references to that member function of that class. This is even if there are other classes with same named members or other overloads for this function within the same class. To do this, right-click on the results in the results window and invoke “Resolve Results”:
Figure 5: Invoke Resolve Results
“Resolving results” uses the compiler to actually verify/confirm the entries and removes references that don’t precisely match. You will be presented with the following results which contain only exact references to Circle::printArea:
Figure 6: Resolved results in VS 2010
If you are feeling more adventurous, there is another part of the accuracy mode which is like asking the following question to the compiler: “Filter out as many extraneous results as you can, and for those you are not sure about, show them anyway”. The reason you would want to ask that question is if you want comments, code in different macro states etc. to be included in the search results.
The way to ask this question is to simply right-click on the resolved results in the window and to uncheck “Hide Unconfirmed”:
Figure 7: Uncheck “Hide Unconfirmed”
Doing this will make sure that search results will exclude only those results which the compiler has verified are definitely not references of Circle::printArea. Anything that the compiler verifies is correct and anything it is not sure about will be listed. The results will be as shown below:
Figure 8: Resolved and Unconfirmed results in VS 2010
Notice this time the results include the comments. It’s worth mentioning that this is the only mode that was supported by VS 2008
The default mode for search is the speed mode. This means, when you invoke Find-all-References, you will see all the places your search item appears. However as with everything else in Visual Studio, there is a way to change this default behavior. If you go to “Tools -> Options -> Text Editor-> C/C++ ->Advanced”, under References you will find two new options “Disable Resolving” and “Hide Unconfirmed”:
Figure 9: Options to set default search mode
By default the “Disable Resolving” flag is set to True (means Speed mode). Setting it to False would cause all results to be verified with the compiler (means Accuracy mode). Similarly the default value of “Hide Unconfirmed” flag is True. Setting it to False will ensure that search results will contain unconfirmed results in addition to the resolved results. The second flag makes sense only when the first flag is set to False. Also to be noted is that resolving/unresolving from the results window will not affect the values of these flags. These are global settings meant to specify default behavior.
Overall, we believe that these changes have introduced more flexibility enabling users to optimize their experience based on their own needs. We are excited about this change and hope you will like it.