1 //===-- PrintFlangFunctionNames.cpp ---------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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
17 // respectively.
18 //
19 //===----------------------------------------------------------------------===//
20 
21 #include "flang/Frontend/CompilerInstance.h"
22 #include "flang/Frontend/FrontendActions.h"
23 #include "flang/Frontend/FrontendPluginRegistry.h"
24 #include "flang/Parser/dump-parse-tree.h"
25 #include "flang/Parser/parsing.h"
26 
27 using namespace Fortran::frontend;
28 
29 class PrintFunctionNamesAction : public PluginParseTreeAction {
30 
31   // Visitor struct that defines Pre/Post functions for different types of nodes
32   struct ParseTreeVisitor {
33     template <typename A> bool Pre(const A &) { return true; }
34     template <typename A> void Post(const A &) {}
35 
36     bool Pre(const Fortran::parser::FunctionSubprogram &) {
37       isInSubprogram_ = true;
38       return true;
39     }
40     void Post(const Fortran::parser::FunctionStmt &f) {
41       if (isInSubprogram_) {
42         llvm::outs() << "Function:\t"
43                      << std::get<Fortran::parser::Name>(f.t).ToString() << "\n";
44         fcounter++;
45         isInSubprogram_ = false;
46       }
47     }
48 
49     bool Pre(const Fortran::parser::SubroutineSubprogram &) {
50       isInSubprogram_ = true;
51       return true;
52     }
53     void Post(const Fortran::parser::SubroutineStmt &s) {
54       if (isInSubprogram_) {
55         llvm::outs() << "Subroutine:\t"
56                      << std::get<Fortran::parser::Name>(s.t).ToString() << "\n";
57         scounter++;
58         isInSubprogram_ = false;
59       }
60     }
61 
62     int fcounter{0};
63     int scounter{0};
64 
65   private:
66     bool isInSubprogram_{false};
67   };
68 
69   void ExecuteAction() override {
70     auto &parseTree{instance().parsing().parseTree()};
71 
72     ParseTreeVisitor visitor;
73     Fortran::parser::Walk(parseTree, visitor);
74 
75     llvm::outs() << "\n====   Functions: " << visitor.fcounter << " ====\n";
76     llvm::outs() << "==== Subroutines: " << visitor.scounter << " ====\n";
77   }
78 };
79 
80 static FrontendPluginRegistry::Add<PrintFunctionNamesAction> X(
81     "print-fns", "Print Function names");
82