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 report with
9// all the OpenMP constructs and clauses and which line they're located on.
10//
11// The plugin may be invoked as:
12// ./bin/flang-new -fc1 -load lib/flangOmpReport.so -plugin flang-omp-report
13// -fopenmp
14//
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(Key: "clause", Val&: info.clause);
37 io.mapRequired(Key: "details", Val&: info.clauseDetails);
38 }
39};
40template <> struct MappingTraits<LogRecord> {
41 static void mapping(IO &io, LogRecord &info) {
42 io.mapRequired(Key: "file", Val&: info.file);
43 io.mapRequired(Key: "line", Val&: info.line);
44 io.mapRequired(Key: "construct", Val&: info.construct);
45 io.mapRequired(Key: "clauses", Val&: info.clauses);
46 }
47};
48} // namespace yaml
49} // namespace llvm
50
51class FlangOmpReport : public PluginParseTreeAction {
52 void executeAction() override {
53 // Prepare the parse tree and the visitor
54 Parsing &parsing = getParsing();
55 OpenMPCounterVisitor visitor;
56 visitor.parsing = &parsing;
57
58 // Walk the parse tree
59 Walk(parsing.parseTree(), visitor);
60
61 // Dump the output
62 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

source code of flang/examples/FlangOmpReport/FlangOmpReport.cpp