1 //===-- examples/flang-omp-report-plugin/flang-omp-report.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 // This plugin parses a Fortran source file and generates a YAML 9 // report with all the OpenMP constructs and clauses and which 10 // line they're located on. 11 // 12 // The plugin may be invoked as: 13 // ./bin/flang-new -fc1 -load lib/flangOmpReport.so -plugin 14 // flang-omp-report -fopenmp -o - <source_file.f90> 15 // 16 //===----------------------------------------------------------------------===// 17 18 #include "FlangOmpReportVisitor.h" 19 20 #include "flang/Frontend/CompilerInstance.h" 21 #include "flang/Frontend/FrontendActions.h" 22 #include "flang/Frontend/FrontendPluginRegistry.h" 23 #include "flang/Parser/dump-parse-tree.h" 24 #include "llvm/Support/YAMLParser.h" 25 #include "llvm/Support/YAMLTraits.h" 26 27 using namespace Fortran::frontend; 28 using namespace Fortran::parser; 29 30 LLVM_YAML_IS_SEQUENCE_VECTOR(LogRecord) 31 LLVM_YAML_IS_SEQUENCE_VECTOR(ClauseInfo) 32 namespace llvm { 33 namespace yaml { 34 using llvm::yaml::IO; 35 using llvm::yaml::MappingTraits; 36 template <> struct MappingTraits<ClauseInfo> { 37 static void mapping(IO &io, ClauseInfo &info) { 38 io.mapRequired("clause", info.clause); 39 io.mapRequired("details", info.clauseDetails); 40 } 41 }; 42 template <> struct MappingTraits<LogRecord> { 43 static void mapping(IO &io, LogRecord &info) { 44 io.mapRequired("file", info.file); 45 io.mapRequired("line", info.line); 46 io.mapRequired("construct", info.construct); 47 io.mapRequired("clauses", info.clauses); 48 } 49 }; 50 } // namespace yaml 51 } // namespace llvm 52 53 class FlangOmpReport : public PluginParseTreeAction { 54 void ExecuteAction() override { 55 // Prepare the parse tree and the visitor 56 CompilerInstance &ci = this->instance(); 57 Parsing &parsing = ci.parsing(); 58 const Program &parseTree = *parsing.parseTree(); 59 OpenMPCounterVisitor visitor; 60 visitor.parsing = &parsing; 61 62 // Walk the parse tree 63 Walk(parseTree, visitor); 64 65 // Dump the output 66 std::unique_ptr<llvm::raw_pwrite_stream> OS{ci.CreateDefaultOutputFile( 67 /*Binary=*/true, /*InFile=*/GetCurrentFileOrBufferName(), 68 /*Extension=*/".yaml")}; 69 llvm::yaml::Output yout(*OS); 70 yout << visitor.constructClauses; 71 } 72 }; 73 74 static FrontendPluginRegistry::Add<FlangOmpReport> X("flang-omp-report", 75 "Generate a YAML summary of OpenMP constructs and clauses"); 76