How to get a list of used units with DelphiAST | SourceOddity


http://sourceoddity.com/blog/2015/02/how-to-get-a-list-of-used-units-with-delphiast/

Comments

  1. Interesting. Why the approach of going through XML, rather than using the AST methods directly?

    ReplyDelete
  2. David Millington only because it requires a bit less code. Of course you can do the same just using TSyntaxNode class without any XML.

    ReplyDelete
  3. David Millington also an XML is easier to visualize. It's important when you are writting a blog post :)

    ReplyDelete
  4. I used to watch the generated DCU files for this. Your solution is neater.

    ReplyDelete
  5. Is it possible to show unused units with this approach?

    ReplyDelete
  6. Q: How does it handle compiler conditionals within the uses clause?

    ReplyDelete
  7. Dirk Carstensen The short answer is yes. But this will require much more work. To get a list of classes, types, variables etc and to figure out which of them are not used.

    Nicholas Ring It works just like a real compiler. Parser can handle compiler directives like {$DEFINE ..}, {$IFDEF...} etc.

    As you can see in the demo app, it calls ASTBuilder.InitDefinesDefinedByCompiler method, that defines some standard compiler defines like VER120, CPUX86 or UNICODE. Also you can define something by calling ASTBuilder.AddDefine('SOMETHINGELSE');

    ReplyDelete
  8. It did not work for me Access violation on line
    PreambleSize := TEncoding.GetBufferEncoding(Buffer, Encoding, TEncoding.Default);

    Compiled on Delphi xe
    changed
    Content.Read(Buffer, 0, Content.Size); to Content.Read(Buffer, Content.Size);

    ReplyDelete
  9. shlomo abuisak The AV is because of the incorrect conversion with the Content.Read changes. What needs to be done is to change the Content.Read to be:

    Content.Read(Buffer[0], Content.Size);

    ReplyDelete

Post a Comment