This is an archive of the discontinued LLVM Phabricator instance.

[flang][driver] Add print function name Plugin example
ClosedPublic

Authored by stuartellis on Jul 29 2021, 10:33 AM.

Details

Summary

Replacing Hello World example Plugin with one that counts and prints the names of
functions and subroutines.
This involves changing the PluginParseTreeAction Plugin base class to
inherit from PrescanAndSemaAction class to get access to the Parse Tree
so that the Plugin can walk it.
Additionally, there are tests of this new Plugin to check it prints the correct
things in different circumstances.

Depends on: D106137

Diff Detail

Event Timeline

stuartellis created this revision.Jul 29 2021, 10:33 AM
stuartellis requested review of this revision.Jul 29 2021, 10:33 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 29 2021, 10:33 AM
stuartellis added a project: Restricted Project.Jul 29 2021, 10:35 AM

Thank you for working on this @stuartellis ! This plugin is going to be a great reference example for walking the parse tree.

I have a couple of suggestions:

  • I think that you can replace flangHelloWorldPlugin with flangPrintFunctionNames. The former becomes uninteresting when compared to the latter :)
  • Could you add more tests? More on this below.

Ideas for extra tests:

  • function + subroutine calls (these should not be taken into account, right?)
  • functions within INTERFACE s
  • external vs internal functions and subroutines

I know that @kiranchandramohan used the visitor API before, so perhaps he'll have another suggestion.

flang/examples/PrintFlangFunctionNames/CMakeLists.txt
6

FIXME

flang/examples/PrintFlangFunctionNames/PrintFlangFunctionNames.cpp
82

FIXME

flang/test/Driver/plugin-print-fns.f90
29 ↗(On Diff #362820)

FIXME

I think using Function and Subroutine Statement in the Post statement will cause to include function interface specification and miss cases like module procedures.

https://www.ibm.com/docs/en/xl-fortran-aix/16.1.0?topic=attributes-module-procedure-fortran-2008

Address review comments
Extending testing to different types of functions/subroutines
Removing flangHelloWorldPlugin

Thank you for addressing my comments @stuartellis ! I've left a couple of comments inline. I would be tempted to further split the test file into multiple files:

  • one test file to show that function and subroutine definitions are counted as expected (count should be != 0)
  • one test file to show that function/subroutine calls are ignored (with count == 0, if possible)
  • one test file to show that interfaces are not taken into account (with count == 0, if possible)
  • one test file with a misspelled plugin name

Also, the tests should probably be located in flang/test/examples rather than flang/test/Driver.

flang/examples/PrintFlangFunctionNames/PrintFlangFunctionNames.cpp
68

Non-public data members should be named with leading miniscule (lower-case) letters, internal camelCase capitalization, and a trailing underscore, e.g. DoubleEntryBookkeepingSystem myLedger_;.

https://github.com/llvm/llvm-project/blob/main/flang/docs/C%2B%2Bstyle.md#naming

flang/test/Driver/plugin-print-fns.f90
9–13 ↗(On Diff #366285)

Could you move this test to a separate file? It's orthogonal to other things being tested here and IMO it makes sense to keep it separate. Basically, I would split the testing as follows:

  • test for expected output to verify that the plugin works as expected when _everything is fine_
  • test for diagnostics to verify that any abnormal behavior is reported in a sane way.

Otherwise, different tests blend with each other. And by moving them to separate files, you can give the test files a bit more accurate/descriptive names, e.g.:

  • functions_and_subroutines.f90
  • invalid_plugin_name.f90

LGTM. I have one comment.

flang/examples/PrintFlangFunctionNames/PrintFlangFunctionNames.cpp
61

Nit: Do you want to count this along with Subroutines or as a separate category? If you are not counting then it might be OK to skip it completely.

There are also statement functions. How will you count them? If not counting then feel free to skip.
https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vnb9/index.html

Addressing review comments:
Split test into multiple files

I have split up the test and moved them into a new flang/text/Examples directory, but kept one test in the flang/test/Drivers directory, which doesn't reference the example plugin, for testing the incorrect plugin name Error diagnostic.

I have chosen for this plugin to skip counting/printing of Statement Functions or Module Procedures, which would be dealt with through StmtFunctionStmt and MpSubprogramStmt nodes respectively. I have adjusted the comment in the Plugin to make note of this.

stuartellis edited the summary of this revision. (Show Details)Aug 17 2021, 9:57 AM
awarzynski accepted this revision.Aug 18 2021, 2:28 AM

With the latest update, the tests make it clear what is supported. Thank you, LGTM!

This revision is now accepted and ready to land.Aug 18 2021, 2:28 AM
This revision was automatically updated to reflect the committed changes.
flang/include/flang/Frontend/FrontendActions.h