1 | //===- InstallAPI/HeaderFile.h ----------------------------------*- C++ -*-===// |
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 | /// |
9 | /// Representations of a library's headers for InstallAPI. |
10 | /// |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLVM_CLANG_INSTALLAPI_HEADERFILE_H |
14 | #define |
15 | |
16 | #include "clang/Basic/FileManager.h" |
17 | #include "clang/Basic/LangStandard.h" |
18 | #include "clang/InstallAPI/MachO.h" |
19 | #include "llvm/ADT/StringRef.h" |
20 | #include "llvm/Support/ErrorHandling.h" |
21 | #include "llvm/Support/Regex.h" |
22 | #include <optional> |
23 | #include <string> |
24 | |
25 | namespace clang::installapi { |
26 | enum class { |
27 | /// Represents declarations accessible to all clients. |
28 | , |
29 | /// Represents declarations accessible to a disclosed set of clients. |
30 | , |
31 | /// Represents declarations only accessible as implementation details to the |
32 | /// input library. |
33 | , |
34 | /// Unset or unknown type. |
35 | , |
36 | }; |
37 | |
38 | inline StringRef (const HeaderType T) { |
39 | switch (T) { |
40 | case HeaderType::Public: |
41 | return "Public" ; |
42 | case HeaderType::Private: |
43 | return "Private" ; |
44 | case HeaderType::Project: |
45 | return "Project" ; |
46 | case HeaderType::Unknown: |
47 | return "Unknown" ; |
48 | } |
49 | llvm_unreachable("unexpected header type" ); |
50 | } |
51 | |
52 | class { |
53 | /// Full input path to header. |
54 | std::string ; |
55 | /// Access level of header. |
56 | HeaderType ; |
57 | /// Expected way header will be included by clients. |
58 | std::string ; |
59 | /// Supported language mode for header. |
60 | std::optional<clang::Language> ; |
61 | /// Exclude header file from processing. |
62 | bool {false}; |
63 | /// Add header file to processing. |
64 | bool {false}; |
65 | /// Specify that header file is the umbrella header for library. |
66 | bool {false}; |
67 | |
68 | public: |
69 | () = delete; |
70 | (StringRef FullPath, HeaderType Type, |
71 | StringRef IncludeName = StringRef(), |
72 | std::optional<clang::Language> Language = std::nullopt) |
73 | : FullPath(FullPath), Type(Type), IncludeName(IncludeName), |
74 | Language(Language) {} |
75 | |
76 | static llvm::Regex (); |
77 | |
78 | HeaderType () const { return Type; } |
79 | StringRef () const { return IncludeName; } |
80 | StringRef () const { return FullPath; } |
81 | |
82 | void (bool V = true) { Extra = V; } |
83 | void (bool V = true) { Excluded = V; } |
84 | void (bool V = true) { Umbrella = V; } |
85 | bool () const { return Extra; } |
86 | bool () const { return Excluded; } |
87 | bool () const { return Umbrella; } |
88 | |
89 | bool () const { |
90 | return Type != HeaderType::Project && !IncludeName.empty(); |
91 | } |
92 | |
93 | bool (const HeaderFile &Other) const { |
94 | return std::tie(args: Type, args: FullPath, args: IncludeName, args: Language, args: Excluded, args: Extra, |
95 | args: Umbrella) == std::tie(args: Other.Type, args: Other.FullPath, |
96 | args: Other.IncludeName, args: Other.Language, |
97 | args: Other.Excluded, args: Other.Extra, |
98 | args: Other.Umbrella); |
99 | } |
100 | }; |
101 | |
102 | /// Glob that represents a pattern of header files to retreive. |
103 | class { |
104 | private: |
105 | std::string ; |
106 | llvm::Regex ; |
107 | HeaderType ; |
108 | bool {false}; |
109 | |
110 | public: |
111 | (StringRef GlobString, llvm::Regex &&, HeaderType Type); |
112 | |
113 | /// Create a header glob from string for the header access level. |
114 | static llvm::Expected<std::unique_ptr<HeaderGlob>> |
115 | (StringRef GlobString, HeaderType Type); |
116 | |
117 | /// Query if provided header matches glob. |
118 | bool (const HeaderFile &); |
119 | |
120 | /// Query if a header was matched in the glob, used primarily for error |
121 | /// reporting. |
122 | bool () { return FoundMatch; } |
123 | |
124 | /// Provide back input glob string. |
125 | StringRef () { return GlobString; } |
126 | }; |
127 | |
128 | /// Assemble expected way header will be included by clients. |
129 | /// As in what maps inside the brackets of `#include <IncludeName.h>` |
130 | /// For example, |
131 | /// "/System/Library/Frameworks/Foo.framework/Headers/Foo.h" returns |
132 | /// "Foo/Foo.h" |
133 | /// |
134 | /// \param FullPath Path to the header file which includes the library |
135 | /// structure. |
136 | std::optional<std::string> (const StringRef FullPath); |
137 | using = std::vector<HeaderFile>; |
138 | |
139 | /// Determine if Path is a header file. |
140 | /// It does not touch the file system. |
141 | /// |
142 | /// \param Path File path to file. |
143 | bool (StringRef Path); |
144 | |
145 | /// Given input directory, collect all header files. |
146 | /// |
147 | /// \param FM FileManager for finding input files. |
148 | /// \param Directory Path to directory file. |
149 | llvm::Expected<PathSeq> enumerateFiles(clang::FileManager &FM, |
150 | StringRef Directory); |
151 | |
152 | } // namespace clang::installapi |
153 | |
154 | #endif // LLVM_CLANG_INSTALLAPI_HEADERFILE_H |
155 | |