xref: /llvm-project/flang/examples/FlangOmpReport/FlangOmpReport.cpp (revision 06eb10dadfaeaadc5d0d95d38bea4bfb5253e077)
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 -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 
25 using namespace Fortran::frontend;
26 using namespace Fortran::parser;
27 
28 LLVM_YAML_IS_SEQUENCE_VECTOR(LogRecord)
29 LLVM_YAML_IS_SEQUENCE_VECTOR(ClauseInfo)
30 namespace llvm {
31 namespace yaml {
32 using llvm::yaml::IO;
33 using llvm::yaml::MappingTraits;
34 template <> 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 };
40 template <> 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 yaml
49 } // namespace llvm
50 
51 class 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 
70 static FrontendPluginRegistry::Add<FlangOmpReport> X("flang-omp-report",
71     "Generate a YAML summary of OpenMP constructs and clauses");
72