aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/llvm/include/llvm/DWARFLinker/Utils.h
blob: 23e59c967011a58ecb64163ed3d5fe7f66887f4e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//===- Utils.h --------------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_DWARFLINKER_UTILS_H
#define LLVM_DWARFLINKER_UTILS_H

#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"

namespace llvm {
namespace dwarf_linker {

/// This function calls \p Iteration() until it returns false.
/// If number of iterations exceeds \p MaxCounter then an Error is returned.
/// This function should be used for loops which assumed to have number of
/// iterations significantly smaller than \p MaxCounter to avoid infinite
/// looping in error cases.
inline Error finiteLoop(function_ref<Expected<bool>()> Iteration,
                        size_t MaxCounter = 100000) {
  size_t iterationsCounter = 0;
  while (iterationsCounter++ < MaxCounter) {
    Expected<bool> IterationResultOrError = Iteration();
    if (!IterationResultOrError)
      return IterationResultOrError.takeError();
    if (!IterationResultOrError.get())
      return Error::success();
  }
  return createStringError(std::errc::invalid_argument, "Infinite recursion");
}

/// Make a best effort to guess the
/// Xcode.app/Contents/Developer/Toolchains/ path from an SDK path.
inline SmallString<128> guessToolchainBaseDir(StringRef SysRoot) {
  SmallString<128> Result;
  // Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
  StringRef Base = sys::path::parent_path(SysRoot);
  if (sys::path::filename(Base) != "SDKs")
    return Result;
  Base = sys::path::parent_path(Base);
  Result = Base;
  Result += "/Toolchains";
  return Result;
}

inline bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
  // Debug info can contain paths from any OS, not necessarily
  // an OS we're currently running on. Moreover different compilation units can
  // be compiled on different operating systems and linked together later.
  return sys::path::is_absolute(Path, sys::path::Style::posix) ||
         sys::path::is_absolute(Path, sys::path::Style::windows);
}

} // end of namespace dwarf_linker
} // end of namespace llvm

#endif // LLVM_DWARFLINKER_UTILS_H