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