79 lines · cpp
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-exception6//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 for11// FunctionStmt and SubroutineStmt to access the names of functions &12// subroutines. It also has Pre functions for FunctionSubprogram and13// SubroutineSubprogram so a Bool can be set to show that it is the definition14// 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 nodes17// respectively.18//19//===----------------------------------------------------------------------===//20 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"25 26using namespace Fortran::frontend;27 28class PrintFunctionNamesAction : public PluginParseTreeAction {29 30 // Visitor struct that defines Pre/Post functions for different types of nodes31 struct ParseTreeVisitor {32 template <typename A> bool Pre(const A &) { return true; }33 template <typename A> void Post(const A &) {}34 35 bool Pre(const Fortran::parser::FunctionSubprogram &) {36 isInSubprogram_ = true;37 return true;38 }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";43 fcounter++;44 isInSubprogram_ = false;45 }46 }47 48 bool Pre(const Fortran::parser::SubroutineSubprogram &) {49 isInSubprogram_ = true;50 return true;51 }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";56 scounter++;57 isInSubprogram_ = false;58 }59 }60 61 int fcounter{0};62 int scounter{0};63 64 private:65 bool isInSubprogram_{false};66 };67 68 void executeAction() override {69 ParseTreeVisitor visitor;70 Fortran::parser::Walk(getParsing().parseTree(), visitor);71 72 llvm::outs() << "\n==== Functions: " << visitor.fcounter << " ====\n";73 llvm::outs() << "==== Subroutines: " << visitor.scounter << " ====\n";74 }75};76 77static FrontendPluginRegistry::Add<PrintFunctionNamesAction> X(78 "print-fns", "Print Function names");79