clang  19.0.0git
HeaderFile.cpp
Go to the documentation of this file.
1 //===- HeaderFile.cpp ------------------------------------------*- 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 
10 #include "llvm/TextAPI/Utils.h"
11 
12 using namespace llvm;
13 namespace clang::installapi {
14 
15 llvm::Regex HeaderFile::getFrameworkIncludeRule() {
16  return llvm::Regex("/(.+)\\.framework/(.+)?Headers/(.+)");
17 }
18 
19 std::optional<std::string> createIncludeHeaderName(const StringRef FullPath) {
20  // Headers in usr(/local)*/include.
21  std::string Pattern = "/include/";
22  auto PathPrefix = FullPath.find(Pattern);
23  if (PathPrefix != StringRef::npos) {
24  PathPrefix += Pattern.size();
25  return FullPath.drop_front(PathPrefix).str();
26  }
27 
28  // Framework Headers.
30  HeaderFile::getFrameworkIncludeRule().match(FullPath, &Matches);
31  // Returned matches are always in stable order.
32  if (Matches.size() != 4)
33  return std::nullopt;
34 
35  return Matches[1].drop_front(Matches[1].rfind('/') + 1).str() + "/" +
36  Matches[3].str();
37 }
38 
39 bool isHeaderFile(StringRef Path) {
40  return StringSwitch<bool>(sys::path::extension(Path))
41  .Cases(".h", ".H", ".hh", ".hpp", ".hxx", true)
42  .Default(false);
43 }
44 
46  PathSeq Files;
47  std::error_code EC;
48  auto &FS = FM.getVirtualFileSystem();
49  for (llvm::vfs::recursive_directory_iterator i(FS, Directory, EC), ie;
50  i != ie; i.increment(EC)) {
51  if (EC)
52  return errorCodeToError(EC);
53 
54  // Skip files that do not exist. This usually happens for broken symlinks.
55  if (FS.status(i->path()) == std::errc::no_such_file_or_directory)
56  continue;
57 
58  StringRef Path = i->path();
59  if (isHeaderFile(Path))
60  Files.emplace_back(Path);
61  }
62 
63  return Files;
64 }
65 
66 HeaderGlob::HeaderGlob(StringRef GlobString, Regex &&Rule, HeaderType Type)
67  : GlobString(GlobString), Rule(std::move(Rule)), Type(Type) {}
68 
69 bool HeaderGlob::match(const HeaderFile &Header) {
70  if (Header.getType() != Type)
71  return false;
72 
73  bool Match = Rule.match(Header.getPath());
74  if (Match)
75  FoundMatch = true;
76  return Match;
77 }
78 
80  HeaderType Type) {
81  auto Rule = MachO::createRegexFromGlob(GlobString);
82  if (!Rule)
83  return Rule.takeError();
84 
85  return std::make_unique<HeaderGlob>(GlobString, std::move(*Rule), Type);
86 }
87 
88 } // namespace clang::installapi
llvm::MachO::PathSeq PathSeq
Definition: MachO.h:47
Implements support for file system lookup, file system caching, and directory search management.
Definition: FileManager.h:53
llvm::vfs::FileSystem & getVirtualFileSystem() const
Definition: FileManager.h:251
The base class of the type hierarchy.
Definition: Type.h:1813
HeaderType getType() const
Definition: HeaderFile.h:78
StringRef getPath() const
Definition: HeaderFile.h:80
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 match(const HeaderFile &Header)
Query if provided header matches glob.
Definition: HeaderFile.cpp:69
Representations of a library's headers for InstallAPI.
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::optional< std::string > createIncludeHeaderName(const StringRef FullPath)
Assemble expected way header will be included by clients.
Definition: HeaderFile.cpp:19
bool isHeaderFile(StringRef Path)
Determine if Path is a header file.
Definition: HeaderFile.cpp:39
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
Definition: Format.h:5433