LLVM 20.0.0git
Unix.h
Go to the documentation of this file.
1//===- llvm/Support/Unix/Unix.h - Common Unix Include File -------*- 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//
9// This file defines things specific to Unix implementations.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_SUPPORT_UNIX_UNIX_H
14#define LLVM_LIB_SUPPORT_UNIX_UNIX_H
15
16//===----------------------------------------------------------------------===//
17//=== WARNING: Implementation here must contain only generic UNIX code that
18//=== is guaranteed to work on all UNIX variants.
19//===----------------------------------------------------------------------===//
20
21#include "llvm/Config/config.h"
22#include "llvm/Support/Chrono.h"
23#include "llvm/Support/Errno.h"
25#include <algorithm>
26#include <assert.h>
27#include <cerrno>
28#include <cstdio>
29#include <cstdlib>
30#include <cstring>
31#include <string>
32#include <sys/types.h>
33#include <sys/wait.h>
34
35#ifdef HAVE_UNISTD_H
36#include <unistd.h>
37#endif
38
39#include <sys/time.h>
40#include <time.h>
41
42#include <dlfcn.h>
43
44# include <fcntl.h>
45
46/// This function builds an error message into \p ErrMsg using the \p prefix
47/// string and the Unix error number given by \p errnum. If errnum is -1, the
48/// default then the value of errno is used.
49/// Make an error message
50///
51/// If the error number can be converted to a string, it will be
52/// separated from prefix by ": ".
53static inline bool MakeErrMsg(
54 std::string* ErrMsg, const std::string& prefix, int errnum = -1) {
55 if (!ErrMsg)
56 return true;
57 if (errnum == -1)
58 errnum = errno;
59 *ErrMsg = prefix + ": " + llvm::sys::StrError(errnum);
60 return true;
61}
62
63// Include StrError(errnum) in a fatal error message.
64[[noreturn]] static inline void ReportErrnumFatal(const char *Msg, int errnum) {
65 std::string ErrMsg;
66 MakeErrMsg(&ErrMsg, Msg, errnum);
68}
69
70namespace llvm {
71namespace sys {
72
73/// Convert a struct timeval to a duration. Note that timeval can be used both
74/// as a time point and a duration. Be sure to check what the input represents.
75inline std::chrono::microseconds toDuration(const struct timeval &TV) {
76 return std::chrono::seconds(TV.tv_sec) +
77 std::chrono::microseconds(TV.tv_usec);
78}
79
80/// Convert a time point to struct timespec.
81inline struct timespec toTimeSpec(TimePoint<> TP) {
82 using namespace std::chrono;
83
84 struct timespec RetVal;
85 RetVal.tv_sec = toTimeT(TP);
86 RetVal.tv_nsec = (TP.time_since_epoch() % seconds(1)).count();
87 return RetVal;
88}
89
90/// Convert a time point to struct timeval.
91inline struct timeval toTimeVal(TimePoint<std::chrono::microseconds> TP) {
92 using namespace std::chrono;
93
94 struct timeval RetVal;
95 RetVal.tv_sec = toTimeT(TP);
96 RetVal.tv_usec = (TP.time_since_epoch() % seconds(1)).count();
97 return RetVal;
98}
99
100} // namespace sys
101} // namespace llvm
102
103#endif
static bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix, int errnum=-1)
This function builds an error message into ErrMsg using the prefix string and the Unix error number g...
Definition: Unix.h:53
static void ReportErrnumFatal(const char *Msg, int errnum)
Definition: Unix.h:64
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
std::chrono::nanoseconds toDuration(FILETIME Time)
std::string StrError()
Returns a string representation of the errno value, using whatever thread-safe variant of strerror() ...
Definition: Errno.cpp:26
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.
Definition: Chrono.h:34
struct timespec toTimeSpec(TimePoint<> TP)
Convert a time point to struct timespec.
Definition: Unix.h:81
struct timeval toTimeVal(TimePoint< std::chrono::microseconds > TP)
Convert a time point to struct timeval.
Definition: Unix.h:91
std::time_t toTimeT(TimePoint<> TP)
Convert a TimePoint to std::time_t.
Definition: Chrono.h:50
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition: STLExtras.h:1938
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858