Debian Codegraph @

libcextract/FunctionDepsFinder.hh

raw
1 //===- FunctionExternalizeFinder.hh - Find symbols to be externalized *- 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 /// Finds which symbols must be externalized when extracting functions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 /* Author: Giuliano Belinassi */
15
16 #pragma once
17
18 #include "MacroWalker.hh"
19 #include "IncludeTree.hh"
20 #include "PrettyPrint.hh"
21 #include "Closure.hh"
22 #include "Passes.hh"
23
24 #include <clang/Tooling/Tooling.h>
25 #include <clang/Analysis/CallGraph.h>
26 #include <unordered_set>
27
28 using namespace clang;
29
30 /** Function Dependency Finder.
31 *
32 * This class wraps all code necessary to find all Decl in the
33 * Abstract Syntax Tree that is necessary for a given function to
34 * compile.
35 *
36 * As example, assume we have the following source code:
37 *
38 * int f();
39 * int g()
40 * {
41 * f();
42 * }
43 *
44 * int h()
45 * {
46 * }
47 *
48 *
49 * If we want to extract function `g`, we have to extract `f` as well, once it
50 * is called by `g`. OTOH, if we want to only extract `h`, we don't need to do
51 * anything else since this function doesn't call anything and can be compiled
52 * by itself.
53 *
54 */
55 class FunctionDependencyFinder
56 {
57 public:
58 FunctionDependencyFinder(PassManager::Context *);
59
60 /** Print the marked nodes as they appear in the AST. */
61 void Print(void);
62
63 /** Run the analysis on function `function`*/
64 bool Run_Analysis(std::vector<std::string> const &function);
65
66 protected:
67
68 /** Given a list of functions in `funcnames`, compute the closure of those
69 functions. That means find all Types, functions, and global variables
70 that are reachable from those functions. */
71 bool Find_Functions_Required(std::vector<std::string> const &funcnames);
72
73 /** Insert Decls that comes from #include's which are not expanded. This
74 is necessary to make sure we don't remove decls that are used in the
75 header program. */
76 void Insert_Decls_From_Non_Expanded_Includes(void);
77
78 /* Remove redundant decls, whose source location is inside another decl. */
79 void Remove_Redundant_Decls();
80
81 /** The AST that are being used in our analysis. */
82 ASTUnit* AST;
83
84 /* Tree of #includes. */
85 IncludeTree IT;
86
87 /* Should we keep the includes when printing? */
88 bool KeepIncludes;
89
90 /* Visitor that sweeps through the AST. Kept as pointer to avoid declaring
91 the DeclClosureVisitor class into this .h to speedup build time. */
92 DeclClosureVisitor Visitor;
93 };