1 //===-- examples/flang-omp-report-plugin/flang-omp-report.cpp -------------===//
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
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.
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>
16 //===----------------------------------------------------------------------===//
18 #include "FlangOmpReportVisitor.h"
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"
27 using namespace Fortran::frontend
;
28 using namespace Fortran::parser
;
30 LLVM_YAML_IS_SEQUENCE_VECTOR(LogRecord
)
31 LLVM_YAML_IS_SEQUENCE_VECTOR(ClauseInfo
)
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
);
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
);
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
;
62 // Walk the parse tree
63 Walk(parseTree
, visitor
);
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
;
74 static FrontendPluginRegistry::Add
<FlangOmpReport
> X("flang-omp-report",
75 "Generate a YAML summary of OpenMP constructs and clauses");