Download - HSR - Institutional Repository
Transcript
3. Implementation Step 1 : Char pointer refactoring In the first step the CharPointerChecker marks C string variables that can be refactored into std::string objects. When a programmer applies the refactoring through a marker, the CharPointerQuickFix starts by replacing the C string definition with the definition of a std::string variable. Then it uses an ASTVisitor to find subsequent occurrences of the variable. In order to handle the different refactoring cases there is a set of subclasses of the abstract StringRefactoring class. Each subclass can perform a different refactoring. For example, there is a StrlenRefactoring class that can replace a call to the strlen() function with a call to the size() member function. Table 3.1 shows all the StringRefactoring subclasses and how the C string functions are mapped into functions from the <string> / <algorithm> headers. For each occurrence of the variable, the visitor tries to find an instance of an applicable StringRefactoring subclass and then uses it to refactor that occurrence. Finally, after all occurrences have been refactored, the quick-fix adds the necessary include statements and completes the refactoring by performing a rewrite of the AST. The process of the Char pointer refactoring is shown in Figure 3.6 in the form of a flow chart. 55