Debian Codegraph @

libcextract/Passes.hh

raw
1 //===- Passes.hh - Pass manager and pass list for clang-extract -*- C++ -*-===//
2 //
3 // This project is licensed 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 //
9 /// \file
10 /// Pass manager and pass list for clang-extract.
11 //
12 //===----------------------------------------------------------------------===//
13
14 /* Author: Giuliano Belinassi */
15
16 #pragma once
17
18 #include "ArgvParser.hh"
19 #include "InlineAnalysis.hh"
20 #include "SymbolExternalizer.hh"
21 #include "ExpansionPolicy.hh"
22 #include "clang/Frontend/ASTUnit.h"
23
24 using namespace clang;
25
26 class Pass;
27
28 /** @brief Pass manager class: run multiple passes
29 *
30 * This class encapsulates the engine to run a list of passes. The
31 * passes list is declared in this class constructor.
32 * */
33 class PassManager {
34
35 public:
36 PassManager();
37 ~PassManager();
38
39 /** Run all passes. */
40 int Run_Passes(ArgvParser &args);
41
42 /** Context object in which holds the global state of the pass manager.
43 It is also used to communicate between the passes. */
44 class Context
45 {
46 public:
47 Context(ArgvParser &args)
48 : FuncExtractNames(args.Get_Functions_To_Extract()),
49 Externalize(args.Get_Symbols_To_Externalize()),
50 OutputFile(args.Get_Output_File()),
51 IgnoreClangErrors(args.Get_Ignore_Clang_Errors()),
52 ExternalizationDisabled(args.Is_Externalization_Disabled()),
53 KeepIncludes(args.Should_Keep_Includes()),
54 DumpPasses(args.Should_Dump_Passes()),
55 RenameSymbols(args.Should_Rename_Symbols()),
56 Kernel(args.Is_Kernel()),
57 Ibt(args.Has_Ibt()),
58 AllowLateExternalizations(args.Get_Allow_Late_Externalization()),
59 PatchObject(args.Get_PatchObject()),
60 HeadersToExpand(args.Get_Headers_To_Expand()),
61 HeadersToNotExpand(args.Get_Headers_To_Not_Expand()),
62 ClangArgs(args.Get_Args_To_Clang()),
63 Debuginfos(args.Get_Debuginfo_Path()),
64 IpaclonesPath(args.Get_Ipaclones_Path()),
65 SymversPath(args.Get_Symvers_Path()),
66 DscOutputPath(args.Get_Dsc_Output_Path()),
67 OutputFunctionPrototypeHeader(args.Get_Output_Path_To_Prototype_Header()),
68 IncExpansionPolicy(IncludeExpansionPolicy::Get_Overriding(
69 args.Get_Include_Expansion_Policy(), Kernel)),
70 NamesLog(),
71 PassNum(0),
72 IA(Debuginfos, IpaclonesPath, SymversPath, args.Is_Kernel())
73 {
74 }
75
76 /** The Abstract Syntax Tree. */
77 std::unique_ptr<ASTUnit> AST;
78
79 /** The Overlay File System between the real filesystem and the
80 in-memory file system. */
81 IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OFS;
82
83 /** The in-memory file system used to hold our temporary code. */
84 IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> MFS;
85
86 /** List of functions to extract. */
87 std::vector<std::string> &FuncExtractNames;
88
89 /** List of functions to externalize. */
90 std::vector<std::string> &Externalize;
91
92 /** The final output file name. */
93 std::string &OutputFile;
94
95 /** Should we ignore compilation errors from clang? */
96 bool IgnoreClangErrors;
97
98 /** Is the externalization passes disabled? */
99 bool ExternalizationDisabled;
100
101 /** Should we keep the includes whenever is possible? */
102 bool KeepIncludes;
103
104 /** Should the passes dump something? */
105 bool DumpPasses;
106
107 /** Should symbols marked for extraction be renamed? */
108 bool RenameSymbols;
109
110 /** If the source code comes from Linux Kernel */
111 bool Kernel;
112
113 /** If the code was compiled with IBT support */
114 bool Ibt;
115
116 /** If we can late externalize variables. */
117 bool AllowLateExternalizations;
118
119 /** Object that will be patched. */
120 std::string PatchObject;
121
122 /** Which includes we must expand? */
123 std::vector<std::string> HeadersToExpand;
124
125 /** Which includes we must NOT expand? */
126 std::vector<std::string> HeadersToNotExpand;
127
128 /** The arguments that will be sent to clang when building the AST. */
129 std::vector<const char *> &ClangArgs;
130
131 /* Path to Debuginfo, if exists. */
132 std::vector<std::string> &Debuginfos;
133
134 /* Path to Ipaclones, if exists. */
135 const char *IpaclonesPath;
136
137 /* Path to Symvers, if exists. */
138 const char *SymversPath;
139
140 /* Path to libpulp .dsc file for output. */
141 const char *DscOutputPath;
142
143 /* Output path to a file containing foward declarations of all functions. */
144 const char *OutputFunctionPrototypeHeader;
145
146 /* Policy used to expand includes. */
147 IncludeExpansionPolicy::Policy IncExpansionPolicy;
148
149 /** Log of changed names. */
150 std::vector<ExternalizerLogEntry> NamesLog;
151
152 /** Current pass number in the passes list. */
153 int PassNum;
154
155 /** Path to input file. */
156 std::string InputPath;
157
158 /** Generated code by the pass. */
159 std::string CodeOutput;
160
161 /* InlineAnalysis object that will persists through the entire analysis.
162 Avoid rebuilding it as it may require parsing several very large
163 files, thus becoming very slow. */
164 InlineAnalysis IA;
165 };
166
167 private:
168 /** Pass list. */
169 std::vector<Pass *> Passes;
170 };
171
172 class Pass {
173 public:
174 /** Default destructor. */
175 virtual ~Pass() {};
176
177 /** Used to determine if pass should run. Must be implemented by child
178 class. */
179 virtual bool Gate(PassManager::Context *) = 0;
180
181 /** What should be done in the pass. Must be implemented by child class. */
182 virtual bool Run_Pass(PassManager::Context *) = 0;
183
184 /** Dump the pass result to a file. Must be implemented by child class. */
185 virtual void Dump_Result(PassManager::Context *) = 0;
186
187 friend class PassManager;
188
189 protected:
190 /** Determine the output file for the pass dump file. */
191 std::string Get_Dump_Name_From_Input(PassManager::Context *ctx);
192
193 /** Name of current pass. */
194 const char *PassName;
195 };
196
197 bool Build_ASTUnit(PassManager::Context *ctx,
198 IntrusiveRefCntPtr<vfs::FileSystem> fs = nullptr);