Path: blob/main/contrib/llvm-project/clang/lib/InstallAPI/HeaderFile.cpp
35232 views
//===- HeaderFile.cpp ------------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "clang/InstallAPI/HeaderFile.h"9#include "llvm/TextAPI/Utils.h"1011using namespace llvm;12namespace clang::installapi {1314llvm::Regex HeaderFile::getFrameworkIncludeRule() {15return llvm::Regex("/(.+)\\.framework/(.+)?Headers/(.+)");16}1718std::optional<std::string> createIncludeHeaderName(const StringRef FullPath) {19// Headers in usr(/local)*/include.20std::string Pattern = "/include/";21auto PathPrefix = FullPath.find(Pattern);22if (PathPrefix != StringRef::npos) {23PathPrefix += Pattern.size();24return FullPath.drop_front(PathPrefix).str();25}2627// Framework Headers.28SmallVector<StringRef, 4> Matches;29HeaderFile::getFrameworkIncludeRule().match(FullPath, &Matches);30// Returned matches are always in stable order.31if (Matches.size() != 4)32return std::nullopt;3334return Matches[1].drop_front(Matches[1].rfind('/') + 1).str() + "/" +35Matches[3].str();36}3738bool isHeaderFile(StringRef Path) {39return StringSwitch<bool>(sys::path::extension(Path))40.Cases(".h", ".H", ".hh", ".hpp", ".hxx", true)41.Default(false);42}4344llvm::Expected<PathSeq> enumerateFiles(FileManager &FM, StringRef Directory) {45PathSeq Files;46std::error_code EC;47auto &FS = FM.getVirtualFileSystem();48for (llvm::vfs::recursive_directory_iterator i(FS, Directory, EC), ie;49i != ie; i.increment(EC)) {50if (EC)51return errorCodeToError(EC);5253// Skip files that do not exist. This usually happens for broken symlinks.54if (FS.status(i->path()) == std::errc::no_such_file_or_directory)55continue;5657StringRef Path = i->path();58if (isHeaderFile(Path))59Files.emplace_back(Path);60}6162return Files;63}6465HeaderGlob::HeaderGlob(StringRef GlobString, Regex &&Rule, HeaderType Type)66: GlobString(GlobString), Rule(std::move(Rule)), Type(Type) {}6768bool HeaderGlob::match(const HeaderFile &Header) {69if (Header.getType() != Type)70return false;7172bool Match = Rule.match(Header.getPath());73if (Match)74FoundMatch = true;75return Match;76}7778Expected<std::unique_ptr<HeaderGlob>> HeaderGlob::create(StringRef GlobString,79HeaderType Type) {80auto Rule = MachO::createRegexFromGlob(GlobString);81if (!Rule)82return Rule.takeError();8384return std::make_unique<HeaderGlob>(GlobString, std::move(*Rule), Type);85}8687} // namespace clang::installapi888990