1 //===-- PrintFlangFunctionNames.cpp ---------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Small example Flang plugin to count/print Functions & Subroutines names.
10 // It walks the Parse Tree using a Visitor struct that has Post functions for
11 // FunctionStmt and SubroutineStmt to access the names of functions &
12 // subroutines. It also has Pre functions for FunctionSubprogram and
13 // SubroutineSubprogram so a Bool can be set to show that it is the definition
14 // of a function/subroutine, and not print those that are in an Interface.
15 // This plugin does not recognise Statement Functions or Module Procedures,
16 // which could be dealt with through StmtFunctionStmt and MpSubprogramStmt nodes
19 //===----------------------------------------------------------------------===//
21 #include "flang/Frontend/FrontendActions.h"
22 #include "flang/Frontend/FrontendPluginRegistry.h"
23 #include "flang/Parser/dump-parse-tree.h"
24 #include "flang/Parser/parsing.h"
26 using namespace Fortran::frontend
;
28 class PrintFunctionNamesAction
: public PluginParseTreeAction
{
30 // Visitor struct that defines Pre/Post functions for different types of nodes
31 struct ParseTreeVisitor
{
32 template <typename A
> bool Pre(const A
&) { return true; }
33 template <typename A
> void Post(const A
&) {}
35 bool Pre(const Fortran::parser::FunctionSubprogram
&) {
36 isInSubprogram_
= true;
39 void Post(const Fortran::parser::FunctionStmt
&f
) {
40 if (isInSubprogram_
) {
41 llvm::outs() << "Function:\t"
42 << std::get
<Fortran::parser::Name
>(f
.t
).ToString() << "\n";
44 isInSubprogram_
= false;
48 bool Pre(const Fortran::parser::SubroutineSubprogram
&) {
49 isInSubprogram_
= true;
52 void Post(const Fortran::parser::SubroutineStmt
&s
) {
53 if (isInSubprogram_
) {
54 llvm::outs() << "Subroutine:\t"
55 << std::get
<Fortran::parser::Name
>(s
.t
).ToString() << "\n";
57 isInSubprogram_
= false;
65 bool isInSubprogram_
{false};
68 void executeAction() override
{
69 ParseTreeVisitor visitor
;
70 Fortran::parser::Walk(getParsing().parseTree(), visitor
);
72 llvm::outs() << "\n==== Functions: " << visitor
.fcounter
<< " ====\n";
73 llvm::outs() << "==== Subroutines: " << visitor
.scounter
<< " ====\n";
77 static FrontendPluginRegistry::Add
<PrintFunctionNamesAction
> X(
78 "print-fns", "Print Function names");