Debian Codegraph @

libcextract/FunctionExternalizeFinder.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 #include "InlineAnalysis.hh"
17
18 #include "clang/Analysis/CallGraph.h"
19 #include "clang/Frontend/ASTUnit.h"
20 #include "InlineAnalysis.hh"
21
22 #include <vector>
23 #include <unordered_set>
24
25 using namespace clang;
26
27 class FunctionExternalizeFinder
28 {
29 public:
30 FunctionExternalizeFinder(ASTUnit *ast,
31 std::vector<std::string> &to_extract,
32 std::vector<std::string> &to_exernalize,
33 bool keep_includes,
34 InlineAnalysis &IA);
35
36 bool Should_Externalize(CallGraphNode *node);
37 bool Should_Externalize(const DeclaratorDecl *decl);
38
39 void Run_Analysis(void);
40
41 std::vector<std::string> Get_To_Externalize(void);
42
43 protected:
44
45 bool Analyze_Node(CallGraphNode *);
46 bool Analyze_Function(FunctionDecl *);
47
48 bool Externalize_DeclRefs(FunctionDecl *decl);
49 bool Externalize_DeclRefs(Stmt *stmt);
50
51 inline bool Must_Not_Externalize(const std::string &name)
52 {
53 return MustNotExternalize.find(name) != MustNotExternalize.end();
54 }
55
56 inline bool Must_Not_Externalize(const DeclaratorDecl *decl)
57 {
58 return Must_Not_Externalize(decl->getName().str());
59 }
60
61 inline bool Is_Marked_For_Externalization(const std::string &name)
62 {
63 return MustExternalize.find(name) != MustExternalize.end();
64 }
65
66 inline bool Mark_For_Externalization(const std::string &name)
67 {
68 bool externalized = false;
69
70 if (!Is_Marked_For_Externalization(name)) {
71 externalized = true;
72 MustExternalize.insert(name);
73 }
74
75 return externalized;
76 }
77
78 std::unordered_set<std::string> MustExternalize;
79 std::unordered_set<std::string> MustNotExternalize;
80 std::unordered_set<std::string> ToExtract;
81
82 inline bool Should_Extract(const std::string &name)
83 {
84 return ToExtract.find(name) != ToExtract.end();
85 }
86
87 inline bool Should_Extract(const FunctionDecl *func)
88 {
89 return Should_Extract(func->getName().str());
90 }
91
92 std::unordered_set<const CallGraphNode *> AnalyzedNodes;
93 bool Is_Already_Analyzed(const CallGraphNode* node)
94 {
95 return AnalyzedNodes.find(node) != AnalyzedNodes.end();
96 }
97
98 bool KeepIncludes;
99 ASTUnit *AST;
100 InlineAnalysis &IA;
101 };