72 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8// This plugin parses a Fortran source file and generates a YAML report with9// all the OpenMP constructs and clauses and which line they're located on.10//11// The plugin may be invoked as:12// ./bin/flang -fc1 -load lib/flangOmpReport.so -plugin flang-omp-report13// -fopenmp14//15//===----------------------------------------------------------------------===//16 17#include "FlangOmpReportVisitor.h"18 19#include "flang/Frontend/FrontendActions.h"20#include "flang/Frontend/FrontendPluginRegistry.h"21#include "flang/Parser/dump-parse-tree.h"22#include "llvm/Support/YAMLParser.h"23#include "llvm/Support/YAMLTraits.h"24 25using namespace Fortran::frontend;26using namespace Fortran::parser;27 28LLVM_YAML_IS_SEQUENCE_VECTOR(LogRecord)29LLVM_YAML_IS_SEQUENCE_VECTOR(ClauseInfo)30namespace llvm {31namespace yaml {32using llvm::yaml::IO;33using llvm::yaml::MappingTraits;34template <> struct MappingTraits<ClauseInfo> {35 static void mapping(IO &io, ClauseInfo &info) {36 io.mapRequired("clause", info.clause);37 io.mapRequired("details", info.clauseDetails);38 }39};40template <> struct MappingTraits<LogRecord> {41 static void mapping(IO &io, LogRecord &info) {42 io.mapRequired("file", info.file);43 io.mapRequired("line", info.line);44 io.mapRequired("construct", info.construct);45 io.mapRequired("clauses", info.clauses);46 }47};48} // namespace yaml49} // namespace llvm50 51class FlangOmpReport : public PluginParseTreeAction {52 void executeAction() override {53 // Prepare the parse tree and the visitor54 Parsing &parsing = getParsing();55 OpenMPCounterVisitor visitor;56 visitor.parsing = &parsing;57 58 // Walk the parse tree59 Walk(parsing.parseTree(), visitor);60 61 // Dump the output62 std::unique_ptr<llvm::raw_pwrite_stream> OS{63 createOutputFile(/*extension=*/"yaml")};64 llvm::yaml::Output yout(*OS);65 66 yout << visitor.constructClauses;67 }68};69 70static FrontendPluginRegistry::Add<FlangOmpReport> X("flang-omp-report",71 "Generate a YAML summary of OpenMP constructs and clauses");72