Debian Codegraph @

libcextract/ExpansionPolicy.cpp

raw
1 //===- ExpansionPolicy.cpp - Declare header expansion policy --- *- 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 /// Implements a Expansion Policy for #include headers.
11 //
12 //===----------------------------------------------------------------------===//
13
14 /* Author: Giuliano Belinassi, Marcos Paulo de Souza. */
15
16 #include "ExpansionPolicy.hh"
17 #include "NonLLVMMisc.hh"
18
19 #include <llvm/Support/raw_ostream.h>
20
21 bool KernelExpansionPolicy::Must_Expand(const StringRef &absolute_path,
22 const StringRef &relative_path)
23 {
24 std::vector<std::string> include_paths = { "/include/", "/arch/" };
25
26 for (auto &path : include_paths) {
27 if (absolute_path.find(path) != std::string::npos)
28 return false;
29 }
30
31 // The subpath wasn't found, so it's not a public header. In this case we
32 // should expand it.
33 return true;
34 }
35
36 bool SystemExpansionPolicy::Must_Expand(const StringRef &absolute_path,
37 const StringRef &relative_path)
38 {
39 const char *absolute_path_c = absolute_path.data();
40
41 /* Look for system headers by looking to specific prefixes. */
42 const char *include_paths[] = { "/usr/include/", "/usr/lib64/",
43 "/usr/lib/", "/usr/local/include/", };
44 for (unsigned i = 0; i < ARRAY_LENGTH(include_paths); i++) {
45 if (prefix(include_paths[i], absolute_path_c)) {
46 return false; // Do not expand.
47 }
48 }
49
50 /* Expand anything that doesn't match this. */
51 return true;
52 }
53
54 bool CompilerExpansionPolicy::Must_Expand(const StringRef &absolute_path,
55 const StringRef &relative_path)
56 {
57 const char *absolute_path_c = absolute_path.data();
58
59 /* Look for clang compiler headers by looking to specific prefixes. */
60 const char *include_paths[] = { "/usr/lib64/clang/", "/usr/lib/clang/",
61 "/usr/local/lib64/clang/", "/usr/local/lib64/clang/", };
62 for (unsigned i = 0; i < ARRAY_LENGTH(include_paths); i++) {
63 if (prefix(include_paths[i], absolute_path_c)) {
64 return false; // Do not expand.
65 }
66 }
67
68 /* Expand anything that doesn't match this. */
69 return true;
70 }
71
72 bool CompilerExpansionPolicy::Must_Not_Expand(const StringRef &absolute_path,
73 const StringRef &relative_path)
74 {
75 return !Must_Expand(absolute_path, relative_path);
76 }
77
78 std::unique_ptr<IncludeExpansionPolicy> IncludeExpansionPolicy::Get_Expansion_Policy_Unique(
79 IncludeExpansionPolicy::Policy p)
80 {
81 switch (p) {
82 case Policy::NOTHING:
83 return std::make_unique<NoIncludeExpansionPolicy>(
84 NoIncludeExpansionPolicy());
85 break;
86
87 case Policy::EVERYTHING:
88 return std::make_unique<ExpandEverythingExpansionPolicy>(
89 ExpandEverythingExpansionPolicy());
90 break;
91
92 case Policy::KERNEL:
93 return std::make_unique<KernelExpansionPolicy>(
94 KernelExpansionPolicy());
95 break;
96
97 case Policy::SYSTEM:
98 return std::make_unique<SystemExpansionPolicy>(
99 SystemExpansionPolicy());
100 break;
101
102 case Policy::COMPILER:
103 return std::make_unique<CompilerExpansionPolicy>(
104 CompilerExpansionPolicy());
105 break;
106
107 default:
108 assert(false && "Invalid policy");
109 }
110 }
111
112 IncludeExpansionPolicy::Policy IncludeExpansionPolicy::Get_From_String(const char *str)
113 {
114 if (str == nullptr) {
115 return IncludeExpansionPolicy::INVALID;
116 }
117
118 struct {
119 const char *string;
120 Policy policy;
121 } policies[] = {
122 { "nothing", IncludeExpansionPolicy::NOTHING },
123 { "everything", IncludeExpansionPolicy::EVERYTHING },
124 { "kernel", IncludeExpansionPolicy::KERNEL },
125 { "system", IncludeExpansionPolicy::SYSTEM },
126 { "compiler", IncludeExpansionPolicy::COMPILER },
127 };
128
129 for (unsigned long i = 0; i < ARRAY_LENGTH(policies); i++) {
130 if (strcmp(str, policies[i].string) == 0) {
131 return policies[i].policy;
132 }
133 }
134
135 return IncludeExpansionPolicy::NOTHING;
136 }
137
138
139 /* Return true if headers passed through -include must be expanded. */
140 bool IncludeExpansionPolicy::Expand_Minus_Includes(IncludeExpansionPolicy::Policy policy)
141 {
142 switch (policy) {
143 case INVALID:
144 case EVERYTHING:
145 case SYSTEM:
146 case COMPILER:
147 return true;
148 break;
149
150 case NOTHING:
151 case KERNEL:
152 return false;
153 break;
154 }
155 }