How to Build Your First C++ Automated Refactoring Tool - Kristen Shaker - CppCon 2023

Learn to build a C++ automated refactoring tool using Clang's AST matchers, traversal methods, fixit hints, and various Clang tools.

Key takeaways
  1. Use Clang’s AST matchers to identify code patterns. AST matchers are a library of C++ classes that allow you to match specific patterns in the abstract syntax tree (AST) of your code. This can be used to identify code that needs to be refactored, or to perform other types of code analysis.
  2. Use Clang’s traversal methods to navigate the AST. Traversal methods are a set of functions that allow you to traverse the AST in a specific order. This can be used to find all of the nodes in the AST that match a certain pattern, or to perform other types of code analysis.
  3. Use Clang’s fixit hints to generate refactorings. Fixit hints are a way to tell Clang how to refactor your code. This can be used to automatically apply refactorings to your code, or to generate suggestions for refactorings that you can apply manually.
  4. Use Clang’s check API to write your own clang-tidy checks. The check API is a set of functions that allow you to write your own clang-tidy checks. This can be used to create checks that identify specific code patterns, or to perform other types of code analysis.
  5. Use Clang’s add-new-check script to generate a skeleton for a new clang-tidy check. The add-new-check script is a tool that can be used to generate a skeleton for a new clang-tidy check. This can save you time when writing your own clang-tidy checks.
  6. Use Clang’s clang-tidy tool to run your clang-tidy checks. The clang-tidy tool is a tool that can be used to run clang-tidy checks on your code. This can be used to identify code that needs to be refactored, or to perform other types of code analysis.
  7. Use Clang’s clang-query tool to visualize the AST of your code. The clang-query tool is a tool that can be used to visualize the AST of your code. This can be helpful for understanding how the AST is structured, and for identifying code that needs to be refactored.
  8. Use Clang’s clang-format tool to format your code. The clang-format tool is a tool that can be used to format your code according to a specific style guide. This can help to improve the readability and maintainability of your code.
  9. Use Clang’s clang-tidy-diff tool to compare the output of two clang-tidy runs. The clang-tidy-diff tool is a tool that can be used to compare the output of two clang-tidy runs. This can be helpful for identifying code that has been refactored, or for identifying code that needs to be refactored.
  10. Use Clang’s clang-tidy-online tool to run clang-tidy checks online. The clang-tidy-online tool is a tool that can be used to run clang-tidy checks online. This can be helpful for quickly identifying code that needs to be refactored, or for performing other types of code analysis.