improving code reuse in clang tools with clangmetatool
play

Improving code reuse in clang tools with clangmetatool Daniel Ruoso - PowerPoint PPT Presentation

Improving code reuse in clang tools with clangmetatool Daniel Ruoso druoso@bloomberg.net Bloomberg October 17, 2018 Static Analysis and Automated Refactoring at Bloomberg Static Analysis and Automated Refactoring at Bloomberg


  1. Improving code reuse in clang tools with clangmetatool Daniel Ruoso druoso@bloomberg.net Bloomberg October 17, 2018

  2. Static Analysis and Automated Refactoring at Bloomberg ◮ ◮ ◮

  3. Static Analysis and Automated Refactoring at Bloomberg ◮ 30+ years of code ◮ ◮

  4. Static Analysis and Automated Refactoring at Bloomberg ◮ 30+ years of code ◮ substantial amount of reuse ◮

  5. Static Analysis and Automated Refactoring at Bloomberg ◮ 30+ years of code ◮ substantial amount of reuse ◮ continuously integrated and deployed

  6. Writing Language Tools – A brief History ◮ ◮ ◮

  7. Writing Language Tools – A brief History ◮ tools space with gcc ◮ ◮

  8. Writing Language Tools – A brief History ◮ tools space with gcc ◮ llvm3.8 boom ◮

  9. Writing Language Tools – A brief History ◮ tools space with gcc ◮ llvm3.8 boom ◮ clangTooling

  10. My first clang tool ◮ ◮ ◮

  11. My first clang tool ◮ exercise: re-implement include-what-you-use ◮ ◮

  12. My first clang tool ◮ exercise: re-implement include-what-you-use ◮ unsure about life-cycle? just use globals ◮

  13. My first clang tool ◮ exercise: re-implement include-what-you-use ◮ unsure about life-cycle? just use globals ◮ unsure about when to rewrite? just rewrite asap

  14. My first clang tool ◮ ◮ ◮

  15. My first clang tool ◮ so many stub doxygen docs ◮ ◮

  16. My first clang tool ◮ so many stub doxygen docs ◮ so many callbacks ◮

  17. My first clang tool ◮ so many stub doxygen docs ◮ so many callbacks ◮ life-cycle of objects unclear

  18. My first clang tool – Lessons ◮ ◮ ◮

  19. My first clang tool – Lessons ◮ writing a clang tool is actually not that hard ◮ ◮

  20. My first clang tool – Lessons ◮ writing a clang tool is actually not that hard ◮ not a single line of reusable code ◮

  21. My first clang tool – Lessons ◮ writing a clang tool is actually not that hard ◮ not a single line of reusable code ◮ tightly coupling: analysis, rewriting, data collection

  22. Principles ◮ ◮ ◮

  23. Principles ◮ Refactoring tool should make smallest possible change ◮ ◮

  24. Principles ◮ Refactoring tool should make smallest possible change ◮ Create the tool, run it, throw it away ◮

  25. Principles ◮ Refactoring tool should make smallest possible change ◮ Create the tool, run it, throw it away ◮ Design Patterns: Collect, Analyze, Rewrite

  26. Design Pattern: Data Collectors ◮ ◮ ◮

  27. Design Pattern: Data Collectors ◮ Register callbacks, stores data in member ◮ ◮

  28. Design Pattern: Data Collectors ◮ Register callbacks, stores data in member ◮ No specific analysis performed ◮

  29. Design Pattern: Data Collectors ◮ Register callbacks, stores data in member ◮ No specific analysis performed ◮ Expose the data in a useful way

  30. Design Pattern: Analysis ◮ ◮ ◮

  31. Design Pattern: Analysis ◮ Single entry point ◮ ◮

  32. Design Pattern: Analysis ◮ Single entry point ◮ Straight-forward imperative code ◮

  33. Design Pattern: Analysis ◮ Single entry point ◮ Straight-forward imperative code ◮ As little tool-specific code as possible

  34. Design Pattern: Refactoring ◮ ◮ ◮

  35. Design Pattern: Refactoring ◮ Already part of the tooling API ◮ ◮

  36. Design Pattern: Refactoring ◮ Already part of the tooling API ◮ Just fill in the ReplacementsMap ◮

  37. Design Pattern: Refactoring ◮ Already part of the tooling API ◮ Just fill in the ReplacementsMap ◮ Handles coherency for you

  38. clangmetatool ◮ Life-cycle management ◮ Data collectors ◮ Reusable Analysis

  39. clangmetatool: life-cycle management int main(int argc, const char* argv[]) { 1 llvm::cl::OptionCategory MyToolCategory("my-tool options"); 2 llvm::cl::extrahelp CommonHelp 3 (clang::tooling::CommonOptionsParser::HelpMessage); 4 clang::tooling::CommonOptionsParser 5 optionsParser(argc, argv, MyToolCategory); 6 clang::tooling::RefactoringTool tool(optionsParser.getCompilations(), 7 optionsParser.getSourcePathList()); 8 clangmetatool::MetaToolFactory< clangmetatool::MetaTool<MyTool> > 9 raf(tool.getReplacements()); 10 int r = tool.runAndSave(&raf); 11 return r; 12 } 13

  40. clangmetatool: life-cycle management class MyTool { 1 private: 2 SomeDataCollector collector1; 3 SomeOtherDataCollector collector2; 4 public: 5 MyTool(clang::CompilerInstance* ci, clang::ast_matchers::MatchFinder *f) 6 :collector1(ci, f), collector2(ci, f) { 7 // the individual collectors will register their callbacks in their 8 // constructor, the tool doesn’t really need to do anything else here. 9 } 10 void postProcessing 11 (std::map<std::string, clang::tooling::Replacements> &replacementsMap) { 12 // use data from collector1 and collector2 13 // generate warnings and notices 14 // add replacements to replacementsMap 15 } 16 }; 17

  41. clangmetatool: reusable data-collector class WhoCallsIt { 1 private: 2 clangmetatool::collectors::FindCalls fc; 3 public: 4 MyTool(clang::CompilerInstance* ci, clang::ast_matchers::MatchFinder *f) 5 :(ci, f, "legacyfunction") { 6 } 7 void postProcessing 8 (std::map<std::string, clang::tooling::Replacements> &replacementsMap) { 9 FindCallsData *fcd = fc.getData(); 10 auto calls_it = fcd->call_ref.begin(); 11 while (calls_it != fcd->call_ref.end()) { 12 // do something for each call to legacyfunction 13 } 14 } 15 }; 16

  42. clangmetatool: reusable analysis clangmetatool::propagation::ConstantCStringPropagator prop(ci); 1 PropagationResult<std::string> r = prop.runPropagation(funcdecl, vdrefexpr); 2 if (!r.isUnresolved()) { 3 std::cout 4 << "value of variable at this point is " 5 << r.getResult() 6 << std::endl; 7 } 8

  43. Impact at Bloomberg ◮ low cost to writing new tools ◮ custom static analysis accessible ◮ automated refactoring on the rise

  44. Questions? druoso@bloomberg.net https://bloomberg.github.io/clangmetatool

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend