clang 20.0.0git
HeaderFile.h
Go to the documentation of this file.
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 LLVM_CLANG_INSTALLAPI_HEADERFILE_H
15
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
25namespace clang::installapi {
26enum class HeaderType {
27 /// Represents declarations accessible to all clients.
28 Public,
29 /// Represents declarations accessible to a disclosed set of clients.
30 Private,
31 /// Represents declarations only accessible as implementation details to the
32 /// input library.
33 Project,
34 /// Unset or unknown type.
35 Unknown,
36};
37
38inline StringRef getName(const HeaderType T) {
39 switch (T) {
41 return "Public";
43 return "Private";
45 return "Project";
47 return "Unknown";
48 }
49 llvm_unreachable("unexpected header type");
50}
51
53 /// Full input path to header.
54 std::string FullPath;
55 /// Access level of header.
57 /// Expected way header will be included by clients.
58 std::string IncludeName;
59 /// Supported language mode for header.
60 std::optional<clang::Language> Language;
61 /// Exclude header file from processing.
62 bool Excluded{false};
63 /// Add header file to processing.
64 bool Extra{false};
65 /// Specify that header file is the umbrella header for library.
66 bool Umbrella{false};
67
68public:
69 HeaderFile() = delete;
70 HeaderFile(StringRef FullPath, HeaderType Type,
71 StringRef IncludeName = StringRef(),
72 std::optional<clang::Language> Language = std::nullopt)
73 : FullPath(FullPath), Type(Type), IncludeName(IncludeName),
75
76 static llvm::Regex getFrameworkIncludeRule();
77
78 HeaderType getType() const { return Type; }
79 StringRef getIncludeName() const { return IncludeName; }
80 StringRef getPath() const { return FullPath; }
81
82 void setExtra(bool V = true) { Extra = V; }
83 void setExcluded(bool V = true) { Excluded = V; }
84 void setUmbrellaHeader(bool V = true) { Umbrella = V; }
85 bool isExtra() const { return Extra; }
86 bool isExcluded() const { return Excluded; }
87 bool isUmbrellaHeader() const { return Umbrella; }
88
89 bool useIncludeName() const {
90 return Type != HeaderType::Project && !IncludeName.empty();
91 }
92
93 bool operator==(const HeaderFile &Other) const {
94 return std::tie(Type, FullPath, IncludeName, Language, Excluded, Extra,
95 Umbrella) == std::tie(Other.Type, Other.FullPath,
96 Other.IncludeName, Other.Language,
97 Other.Excluded, Other.Extra,
98 Other.Umbrella);
99 }
100
101 bool operator<(const HeaderFile &Other) const {
102 /// For parsing of headers based on ordering,
103 /// group by type, then whether its an umbrella.
104 /// Capture 'extra' headers last.
105 /// This optimizes the chance of a sucessful parse for
106 /// headers that violate IWYU.
107 if (isExtra() && Other.isExtra())
108 return std::tie(Type, Umbrella) < std::tie(Other.Type, Other.Umbrella);
109
110 return std::tie(Type, Umbrella, Extra, FullPath) <
111 std::tie(Other.Type, Other.Umbrella, Other.Extra, Other.FullPath);
112 }
113};
114
115/// Glob that represents a pattern of header files to retreive.
117private:
118 std::string GlobString;
119 llvm::Regex Rule;
121 bool FoundMatch{false};
122
123public:
124 HeaderGlob(StringRef GlobString, llvm::Regex &&, HeaderType Type);
125
126 /// Create a header glob from string for the header access level.
128 create(StringRef GlobString, HeaderType Type);
129
130 /// Query if provided header matches glob.
131 bool match(const HeaderFile &Header);
132
133 /// Query if a header was matched in the glob, used primarily for error
134 /// reporting.
135 bool didMatch() { return FoundMatch; }
136
137 /// Provide back input glob string.
138 StringRef str() { return GlobString; }
139};
140
141/// Assemble expected way header will be included by clients.
142/// As in what maps inside the brackets of `#include <IncludeName.h>`
143/// For example,
144/// "/System/Library/Frameworks/Foo.framework/Headers/Foo.h" returns
145/// "Foo/Foo.h"
146///
147/// \param FullPath Path to the header file which includes the library
148/// structure.
149std::optional<std::string> createIncludeHeaderName(const StringRef FullPath);
150using HeaderSeq = std::vector<HeaderFile>;
151
152/// Determine if Path is a header file.
153/// It does not touch the file system.
154///
155/// \param Path File path to file.
156bool isHeaderFile(StringRef Path);
157
158/// Given input directory, collect all header files.
159///
160/// \param FM FileManager for finding input files.
161/// \param Directory Path to directory file.
163 StringRef Directory);
164
165} // namespace clang::installapi
166
167#endif // LLVM_CLANG_INSTALLAPI_HEADERFILE_H
#define V(N, I)
Definition: ASTContext.h:3443
IndirectLocalPath & Path
Defines the clang::FileManager interface and associated types.
Implements support for file system lookup, file system caching, and directory search management.
Definition: FileManager.h:53
The base class of the type hierarchy.
Definition: Type.h:1828
bool operator==(const HeaderFile &Other) const
Definition: HeaderFile.h:93
void setExcluded(bool V=true)
Definition: HeaderFile.h:83
HeaderType getType() const
Definition: HeaderFile.h:78
bool operator<(const HeaderFile &Other) const
Definition: HeaderFile.h:101
void setExtra(bool V=true)
Definition: HeaderFile.h:82
StringRef getIncludeName() const
Definition: HeaderFile.h:79
void setUmbrellaHeader(bool V=true)
Definition: HeaderFile.h:84
StringRef getPath() const
Definition: HeaderFile.h:80
HeaderFile(StringRef FullPath, HeaderType Type, StringRef IncludeName=StringRef(), std::optional< clang::Language > Language=std::nullopt)
Definition: HeaderFile.h:70
static llvm::Regex getFrameworkIncludeRule()
Definition: HeaderFile.cpp:15
Glob that represents a pattern of header files to retreive.
Definition: HeaderFile.h:116
StringRef str()
Provide back input glob string.
Definition: HeaderFile.h:138
static llvm::Expected< std::unique_ptr< HeaderGlob > > create(StringRef GlobString, HeaderType Type)
Create a header glob from string for the header access level.
Definition: HeaderFile.cpp:79
bool didMatch()
Query if a header was matched in the glob, used primarily for error reporting.
Definition: HeaderFile.h:135
bool match(const HeaderFile &Header)
Query if provided header matches glob.
Definition: HeaderFile.cpp:69
The DirectoryScanner for collecting library files on the file system.
Definition: Context.h:20
llvm::Expected< PathSeq > enumerateFiles(clang::FileManager &FM, StringRef Directory)
Given input directory, collect all header files.
Definition: HeaderFile.cpp:45
std::vector< HeaderFile > HeaderSeq
Definition: HeaderFile.h:150
@ Public
Represents declarations accessible to all clients.
@ Private
Represents declarations accessible to a disclosed set of clients.
@ Unknown
Unset or unknown type.
@ Project
Represents declarations only accessible as implementation details to the input library.
std::optional< std::string > createIncludeHeaderName(const StringRef FullPath)
Assemble expected way header will be included by clients.
Definition: HeaderFile.cpp:19
StringRef getName(const HeaderType T)
Definition: HeaderFile.h:38
bool isHeaderFile(StringRef Path)
Determine if Path is a header file.
Definition: HeaderFile.cpp:39
Language
The language for the input, used to select and validate the language standard and possible actions.
Definition: LangStandard.h:23
const FunctionProtoType * T
@ Other
Other implicit parameter.