Using Visual Studio for Concept Location
This lab covers Concept Location using regular expressions commonly called grep (grepCL) and Concept Location using Dependency Search (CLDS). In Visual Studio 2005/2008, the most useful tools for concept location are:
1) Quick Find ( grepCL)
2) Find in Files (grepCL)
3) Find Symbol (grepCL and CLDS)
4) Go to definition (CLDS)
5) Find all references (CLDS)
6) Call Browser (CLDS)
7) Class View (CLDS)
Quick Find and Find in Files allow regular expression searches commonly called grep. They will search in the source code and comments. To use regular expressions select the checkbox in the advance find options. The difference between Quick Find and Find in Files is the way the search results are displayed. In Quick Find developers navigate from one result to another by clicking the Find Next button. Find in Files displays all the results in a separate window, and developers can then choose which results they want to inspect in more details. The end of the results contains a short section with statistics about the search. To use these tools go to the menu Edit->Find->Quick Search and Edit->Find->Find in Files (Figure 1).
Figure 1 Quick Find and Find in Files tools in Visual Studio
Find Symbol finds the places in the program where a name representing the search string or a name containing the search string is used. The name can be: a name of a class, of a method, or of a field. Find Symbol does not search inside comments. To use this tool go to the menu Edit->Find->Find Symbol (Figure 2).
Go To Definition finds the location where a method, class, or field is defined. To use this tool right click on a class, method, or field name and select “Go To Definition” from the menu (Figure 3).
Find All References. For a selected class, method, or class field, it finds all the places where the method or field is used or referred. To use this tool, right click on a class, method, or field name and select “Find All References” option (Figure 3).
Call Browser allows developers to browse through the clients ( callees methods – methods that call a method m) and suppliers (called – method called by method m) of a selected method m. To use this tool, right click on a method, select “Call Browser” option (Figure 3).
Figure 2. Find Symbol tool in Visual Studio
Figure 3: Go To Definition, Find all References, Call Browser in Visual Studio
Figure 4. Class View in Visual Studio
Class view presents an overview of the project at the class level. Developers can view the methods and fields for each class. Using the option View Class Diagram, developers can construct and visualize a class diagram with inheritance and association relationships. Use drag and drop to view classes in the diagram. To view the parent of a class right click on a class and select “Show Base Classes” option. To view the association between classes, right click on a class field and select “Show as Association” option.
Tasks for WinMerge
In WinMerge, consider the change request “add a save option to the edit view context menu”. Using the tools above answer the following questions:
Concept Location using Grep
T1: Is searching for the string “save” a good search?
Answer: No, it returns too many results to be helpful.
T2: Is searching for the string “save as” a better search? Why?
Answer: Yes, it appears in 4 files, multiformatText.h, Merge.rc, MergeDoc.cpp and VssPrompt.cpp. This search is better because 4 files can be visited; even if the concept location isn’t found the programmer can learn from this search and reformulate a query.
T3: We know that there is a “Save” option in WinMerge. When any save is done in MFC, it triggers a ON_COMMAND event. Perform a regular expression search for “command.*save”. How many files were returned? Is this search more helpful than searching for “Save as”? Justify your answer.
Answer: 5 files. This search is more helpful, because it include the MergeDoc.cpp ON_COMMAND event that saves a file.
T4: How many classes have a OnFileSave method? Which one should our new option call? Why?
Answer: 3 files: CDocument, CHexMergeDoc and CMergeDoc. Our method should call the method in CMergeDoc, because the change request is to add a save option to the edit view menu.
Concept Location using Dependency Search
T5: How many methods does the OnFileSave you selected in T4 call? Are all the methods part of WinMerge?
Answer: 11 methods are called. No, basic_string::c_str() is not part of WinMerge.
T6: Using Call browser -> Show call graph search, how many methods call OnFileSave? The method is called less often than expected; why do you think this is?
Answer: The method is called 0 times. It is only called from ON_COMMAND events; the call browser doesn’t know the ON_COMMAND parameters are calls to the method.
T7: Using the Class View tool draw a class diagram with 3 classes that extend the class
CDocument. Export the diagram as a picture and include it in this document.
Classes extending the CDocument class
T8: Name the files and the line number where a variable of type pLeft or pRight are declared. Hint: try a regular expression search.
Answer: ChildFrm.cpp line 209,210,521; MainFrm.cpp line 1828, 1877, 1878; MergeDoc.cpp line 2286, 2287, 2523, 2524
T9: Consider the following change request: “add a new dialog to show the user a tip of the day to WinMerge”. Use CLDS to find the file path concept, and a piece of code that will change in response to the above change request. The MFC framework calls the main function, so start in CMergeApp. Name all the classes, methods and fields in the order you visited. Justify your decisions.
Answer:
Table 1. CLDS Steps
# / Classes, methods, fields visited / Tool used / Justification1 / CMergeApp / N/A / Starting point for CLDS
2 / Notepad_plus::init(HINSTANCE hInst, HWND parent, const TCHAR *cmdLine, CmdLineParams *cmdLineParams) / Call Browser / Called in starting point method. Initialize the main window
3 / Notepad_plus / Go To Definition / Implements the main window of the program
4 / Window / Class View / Notepad_plus extends Window
5 / Notepad_plus::runProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) / Class View / This method implements the window procedure and handle the events
6 / Notepad_plus::_pEditView / Class View / This field is used to implement different messages
7 / SchintillaEditView / Go To Definition / Learn more about the type of _pEditView. It is not related to the file path concept – backtrack to 5
8 / Notepad_plus::_findReplaceDialog / Class View / Based on its name, this field is related to the search dialog
9 / FindReplaceDialog::finishFilesSearch(int count) / Find All References / The file path is displayed at the end of the search. The concept is implemented in its suppliers
10 / FindReplaceDialog::finder / Go To Definition / Check the type of the finder field
11 / Finder::finishFilesSearch(int count) / Go To Definition / The concept is implemented in its suppliers
12 / Finder / Go To Definition / Visit the whole class Finder – looks closely related to the concept.
13 / FoundInfo / Class View / There are several fields in the Finder class of type FoundInfo or templates using FoundInfo.
In response to the change request, the struct FoundInfo will be extended with the size of the file.
14 / FoundInfo::_fullPath / N/A / FoundInfo stores the full path of the file, the start and end point for the found string. Concept Located
T10: Using Class View tool draw a diagram with the visited classes for the previous CLDS search.
Locating “file path” concept using CLDS
Notes for the instructors:
- Show students how to use the tools before you handle them the tasks.
- The answers added in this document are for guidance only. Students may find alternative and correct answers not included in this document. For example, at T9, if students are able to locate a piece of code and justify why thatpiece of code will be changed in response to the change request,they should get full credits for their answer.