Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:49

0001 //===- Utils.h --------------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_DWARFLINKER_UTILS_H
0010 #define LLVM_DWARFLINKER_UTILS_H
0011 
0012 #include "llvm/ADT/SmallString.h"
0013 #include "llvm/ADT/Twine.h"
0014 #include "llvm/Support/Error.h"
0015 #include "llvm/Support/FileSystem.h"
0016 #include "llvm/Support/Path.h"
0017 
0018 namespace llvm {
0019 namespace dwarf_linker {
0020 
0021 /// This function calls \p Iteration() until it returns false.
0022 /// If number of iterations exceeds \p MaxCounter then an Error is returned.
0023 /// This function should be used for loops which assumed to have number of
0024 /// iterations significantly smaller than \p MaxCounter to avoid infinite
0025 /// looping in error cases.
0026 inline Error finiteLoop(function_ref<Expected<bool>()> Iteration,
0027                         size_t MaxCounter = 100000) {
0028   size_t iterationsCounter = 0;
0029   while (iterationsCounter++ < MaxCounter) {
0030     Expected<bool> IterationResultOrError = Iteration();
0031     if (!IterationResultOrError)
0032       return IterationResultOrError.takeError();
0033     if (!IterationResultOrError.get())
0034       return Error::success();
0035   }
0036   return createStringError(std::errc::invalid_argument, "Infinite recursion");
0037 }
0038 
0039 /// Make a best effort to guess the
0040 /// Xcode.app/Contents/Developer path from an SDK path.
0041 inline StringRef guessDeveloperDir(StringRef SysRoot) {
0042   // Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
0043   auto it = sys::path::rbegin(SysRoot);
0044   auto end = sys::path::rend(SysRoot);
0045   if (it == end || !it->ends_with(".sdk"))
0046     return {};
0047   ++it;
0048   // Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs
0049   if (it == end || *it != "SDKs")
0050     return {};
0051   auto developerEnd = it;
0052   ++it;
0053   while (it != end) {
0054     // Contents/Developer/Platforms/MacOSX.platform/Developer
0055     if (*it != "Developer")
0056       return {};
0057     ++it;
0058     if (it == end)
0059       return {};
0060     if (*it == "Contents")
0061       return StringRef(SysRoot.data(),
0062                        developerEnd - sys::path::rend(SysRoot) - 1);
0063     // Contents/Developer/Platforms/MacOSX.platform
0064     if (!it->ends_with(".platform"))
0065       return {};
0066     ++it;
0067     // Contents/Developer/Platforms
0068     if (it == end || *it != "Platforms")
0069       return {};
0070     developerEnd = it;
0071     ++it;
0072   }
0073   return {};
0074 }
0075 
0076 /// Make a best effort to determine whether Path is inside a toolchain.
0077 inline bool isInToolchainDir(StringRef Path) {
0078   // Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-05-15-a.xctoolchain/usr/lib/swift/macosx/_StringProcessing.swiftmodule/arm64-apple-macos.private.swiftinterface
0079   for (auto it = sys::path::rbegin(Path), end = sys::path::rend(Path);
0080        it != end; ++it) {
0081     if (it->ends_with(".xctoolchain")) {
0082       ++it;
0083       if (it == end)
0084         return false;
0085       if (*it != "Toolchains")
0086         return false;
0087       ++it;
0088       if (it == end)
0089         return false;
0090       if (*it != "Developer")
0091         return false;
0092       return true;
0093     }
0094   }
0095   return false;
0096 }
0097 
0098 inline bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
0099   // Debug info can contain paths from any OS, not necessarily
0100   // an OS we're currently running on. Moreover different compilation units can
0101   // be compiled on different operating systems and linked together later.
0102   return sys::path::is_absolute(Path, sys::path::Style::posix) ||
0103          sys::path::is_absolute(Path, sys::path::Style::windows);
0104 }
0105 
0106 } // end of namespace dwarf_linker
0107 } // end of namespace llvm
0108 
0109 #endif // LLVM_DWARFLINKER_UTILS_H