Code Analysis and Refactoring with CDT


Published on

Published in: Technology
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Code Analysis and Refactoring with CDT

  1. 1. Code Analysis and Refactoring with the CDT Doug Schaefer Wind River CDT Project Lead.
  2. 2. What is the CDT? <ul><li>A C/C++ Development Environment on Eclipse </li></ul><ul><ul><li>Intellig ent Code Editor </li></ul></ul><ul><ul><li>Source Navigation features using an index and internal parser </li></ul></ul><ul><ul><li>Build with integration with different tool chains </li></ul></ul><ul><ul><li>Debug with integration with different debug engines </li></ul></ul><ul><li>Out of the box support for GNU tools </li></ul><ul><ul><li>To facilitate testing and to ease adoption </li></ul></ul><ul><li>Extended by many vendors to support their own platforms </li></ul><ul><ul><li>Wind River, QNX, Nokia/Symbian, IBM, Intel, and many more </li></ul></ul>
  3. 3. What is the CDT Used For? <ul><li>Embedded Development </li></ul><ul><ul><li>Leading IDE solution for the mobile and device market </li></ul></ul><ul><ul><li>Adopted as IDE by most RTOS and silicon vendors </li></ul></ul><ul><li>Linux development </li></ul><ul><ul><li>Included in most enterprise Linux distributions </li></ul></ul><ul><ul><li>Red Hat working to create “Visual Studio” for Linux </li></ul></ul><ul><li>High Performance Computing </li></ul><ul><ul><li>Basis for Parallel Tools Platform and Fortran IDEs </li></ul></ul><ul><li>Cross Platform Development </li></ul><ul><ul><li>Windows, Linux, MacOS X, most RTOSes, ... </li></ul></ul>
  4. 4. Why does the CDT have a parser? <ul><li>Original intention was to support UML modeling and code gen </li></ul><ul><ul><li>Started by the CDT team at Rational </li></ul></ul><ul><li>Started with Outline view support </li></ul><ul><ul><li>CmodelBuilder to populate the Cmodel </li></ul></ul><ul><li>Moved on to populate index </li></ul><ul><ul><li>Full AST to capture symbol defs and references </li></ul></ul><ul><li>And now we have refactoring </li></ul><ul><ul><li>And they said it couldn't be done! </li></ul></ul>
  5. 5. The C Model <ul><li>Adaptation of resources to C/C++ type things </li></ul><ul><ul><li>mainly intended to populate Project Navigator and Outline View </li></ul></ul><ul><ul><li>Adds to Resource level </li></ul></ul><ul><ul><ul><li>ICProject, source and output folders, source and binary files </li></ul></ul></ul><ul><ul><li>And inside the file level </li></ul></ul><ul><ul><ul><li>ITranslationUnit and IBinary </li></ul></ul></ul><ul><li>Entry point is the CoreModel class </li></ul><ul><ul><li>No nice OSGi service yet, not even an adapter </li></ul></ul><ul><ul><li>e.g., ICProject cproj = CoreModel.getDefault.create(project); </li></ul></ul><ul><ul><li>Root element is ICElement, open type hierarchy to see it all </li></ul></ul>
  6. 6. CModel is good, but... <ul><li>Focused mainly on UI type things </li></ul><ul><ul><li>Simple, easy to understand </li></ul></ul><ul><ul><li>But not necessarily the most accurate representation </li></ul></ul><ul><li>Traditional compilers have Abstract Syntax Trees </li></ul><ul><ul><li>Starts at the token level </li></ul></ul><ul><ul><li>Arranged in syntax tree </li></ul></ul><ul><ul><li>Semantic analysis support for “understanding” what's in the tree </li></ul></ul><ul><ul><li>Enough information to translate into executable object code </li></ul></ul><ul><li>The CDT has an AST! </li></ul>
  7. 7. Getting a CDT AST <ul><li>Entry point is ITranslationUnit from the Cmodel </li></ul><ul><ul><li>IASTTranslationUnit ast = tu.getAST(); </li></ul></ul><ul><li>Uses parser to create a Full AST </li></ul><ul><ul><li>Determines language from content type </li></ul></ul><ul><ul><li>Invokes associated parser for the language </li></ul></ul><ul><ul><li>Produces a language dependent AST </li></ul></ul><ul><ul><ul><li>ICASTTranslationUnit, ICPPASTTranslationUnit </li></ul></ul></ul><ul><li>ASTVisitor provided to facilitate walking the AST </li></ul><ul><ul><li>Trasitional visitor pattern, accept/visit/leave </li></ul></ul>
  8. 9. Semantic Analysis <ul><li>“Bindings” connect Declarations and References </li></ul><ul><ul><li>Represent semantic concepts and their properties </li></ul></ul><ul><ul><li>IBinding is root of type hierarchy </li></ul></ul><ul><li>IASTName represents point of declaration and reference </li></ul><ul><ul><li>getBinding() returns IBinding for given name </li></ul></ul><ul><li>Find Declarations and References from IASTTranslationUnit </li></ul><ul><ul><li>IASTName[] getDeclarationsInAST(IBinding binding) </li></ul></ul><ul><ul><li>IASTName[] getReferencesInAST(IBinding binding) </li></ul></ul>
  9. 10. AST is great, but... <ul><li>Creating Full AST is expensive </li></ul><ul><ul><li>both size (lots of objects) and time (parsing is compute intensive) </li></ul></ul><ul><li>Finding all References in a large project prohibitive </li></ul><ul><ul><li>Parsing every source file very time consuming (hours). </li></ul></ul><ul><li>We need a caching strategy... </li></ul>
  10. 11. Introducing the Index and Indexer <ul><li>Index captures all Names and Bindings in a project </li></ul><ul><ul><li>Stored in a hand coded database </li></ul></ul><ul><ul><li>Used for C/C++ Search, Open Declaration </li></ul></ul><ul><li>Indexer runs in the background on file saves </li></ul><ul><ul><li>Parses files and walks AST adding names and bindings to DB </li></ul></ul><ul><li>Mode added to AST to reuse contents of Index instead of parsing Header files </li></ul><ul><ul><li>Huge performance improvement similar to pre-compiled headers </li></ul></ul><ul><ul><li>Some cost in accuracy due to conditional compile directives </li></ul></ul>
  11. 12. Parsing using the Index <ul><li>Parsers operate in different modes/styles </li></ul><ul><ul><li>e.g. ITranslationUnit.AST_SKIP_INDEXED_HEADERS </li></ul></ul><ul><li>Pass in Index and flags to parser </li></ul><ul><ul><li>ITranslationUnit.getAST(IIndex index, int style) </li></ul></ul><ul><li>Get Index from Index Manager </li></ul><ul><ul><li>CCorePlugin.getIndexManager().getIndex(ICProject[] projects) </li></ul></ul><ul><ul><li>Acquire and release read index lock to avoid collision with indexer </li></ul></ul>
  12. 13. Using the Index <ul><li>Use index in AST searches </li></ul><ul><ul><li>IName[] IASTTranslationUnit.getReferences(IBinding[] binding) </li></ul></ul><ul><ul><li>IName super class for AST Names and Index Names </li></ul></ul><ul><li>IBindings are handled automagically </li></ul><ul><ul><li>If parsed using skip headers </li></ul></ul><ul><ul><li>IASTName.getBinding() will return the binding from the index </li></ul></ul><ul><li>Information can be retrieved from the index directly </li></ul><ul><ul><li>e.g., IIndex.findBindings() which takes a Pattern and an IndexFilter </li></ul></ul>
  13. 14. A Simple Static Analysis Tool <ul><li>Find all functions that call 'printf' </li></ul><ul><ul><li>Find ICProject, get IIndex for it </li></ul></ul><ul><ul><li>IIndex.findBinding() for IFunction bindings called printf </li></ul></ul><ul><ul><li>IIndex.findReference() for the binding </li></ul></ul><ul><ul><li>For each reference name, create AST for containing file </li></ul></ul><ul><ul><li>Find IASTNode at the offset in the reference name </li></ul></ul><ul><ul><li>Search node parents looking for IASTFunctionDefinition </li></ul></ul><ul><ul><li>Get declarator for function and print out the IASTName for it </li></ul></ul>
  14. 15. Introducing CDT Refactoring <ul><li>Very similar to JDT Refactoring </li></ul><ul><ul><li>Reuses the LTK plug-in contributed by JDT team </li></ul></ul><ul><li>Uses semantic analysis capabilities to create TextEdits </li></ul><ul><ul><li>CRefactoring is root of type hierarchy </li></ul></ul><ul><li>Three steps </li></ul><ul><ul><li>checkInitialConditions to set up for refactoring for user options </li></ul></ul><ul><ul><li>checkFinalConditions to ensure user entered options are valid </li></ul></ul><ul><ul><li>collectModifications to set up refactoring changes </li></ul></ul>
  15. 16. AST Rewriting and the LTK <ul><li>ModificationCollector manages ASTRewrites </li></ul><ul><li>ASTRewrite creates TextEdits to affect AST changes </li></ul><ul><ul><li>Insert, remove, replace nodes </li></ul></ul><ul><li>LTK uses TextEdits to show preview and to execute the change </li></ul><ul><ul><li>Which makes refactoring easy to create </li></ul></ul><ul><li>LTK also provides UI framework for co-ordinating refactoring </li></ul><ul><ul><li>Add your own input page for user selectable options </li></ul></ul>
  16. 17. Simple Refactoring <ul><li>Change calls to printf with calls to log collector </li></ul><ul><ul><li>Left as an exercise for the reader... </li></ul></ul><ul><li>Look at the current set of CDT refactorings </li></ul><ul><ul><li>Extract Constant </li></ul></ul><ul><ul><li>Extract Function </li></ul></ul><ul><ul><li>Generate Getters and Setters </li></ul></ul><ul><ul><li>Hide Method </li></ul></ul><ul><ul><li>Implement Method </li></ul></ul>
  17. 18. Next Steps <ul><li>Framework needs documentation </li></ul><ul><ul><li>Spread the word on what can be done and how </li></ul></ul><ul><li>Framework a bit young and needs exercise </li></ul><ul><ul><li>Architecture needs a little clean up, but not much </li></ul></ul><ul><ul><li>Feel free to submit bugs, patches, and new functionality </li></ul></ul><ul><ul><li>Discuss on cdt-dev mailing list </li></ul></ul><ul><li>Let your imagination go wild and see what the CDT can do for you! </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.