LLVM 22.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 <assert.h>
26#include <cerrno>
27#include <cstdio>
28#include <cstdlib>
29#include <cstring>
30#include <string>
31#include <sys/types.h>
32#include <sys/wait.h>
33
34#ifdef HAVE_UNISTD_H
35#include <unistd.h>
36#endif
37
38#include <sys/time.h>
39#include <time.h>
40
41#include <dlfcn.h>
42
43# include <fcntl.h>
44
45/// This function builds an error message into \p ErrMsg using the \p prefix
46/// string and the Unix error number given by \p errnum. If errnum is -1, the
47/// default then the value of errno is used.
48/// Make an error message
49///
50/// If the error number can be converted to a string, it will be
51/// separated from prefix by ": ".
52static inline bool MakeErrMsg(
53 std::string* ErrMsg, const std::string& prefix, int errnum = -1) {
54 if (!ErrMsg)
55 return true;
56 if (errnum == -1)
57 errnum = errno;
58 *ErrMsg = prefix + ": " + llvm::sys::StrError(errnum);
59 return true;
60}
61
62// Include StrError(errnum) in a fatal error message.
63[[noreturn]] static inline void ReportErrnumFatal(const char *Msg, int errnum) {
64 std::string ErrMsg;
65 MakeErrMsg(&ErrMsg, Msg, errnum);
67}
68
69namespace llvm {
70namespace sys {
71
72/// Convert a struct timeval to a duration. Note that timeval can be used both
73/// as a time point and a duration. Be sure to check what the input represents.
74inline std::chrono::microseconds toDuration(const struct timeval &TV) {
75 return std::chrono::seconds(TV.tv_sec) +
76 std::chrono::microseconds(TV.tv_usec);
77}
78
79/// Convert a time point to struct timespec.
80inline struct timespec toTimeSpec(TimePoint<> TP) {
81 using namespace std::chrono;
82
83 struct timespec RetVal;
84 RetVal.tv_sec = toTimeT(TP);
85 RetVal.tv_nsec = (TP.time_since_epoch() % seconds(1)).count();
86 return RetVal;
87}
88
89/// Convert a time point to struct timeval.
90inline struct timeval toTimeVal(TimePoint<std::chrono::microseconds> TP) {
91 using namespace std::chrono;
92
93 struct timeval RetVal;
94 RetVal.tv_sec = toTimeT(TP);
95 RetVal.tv_usec = (TP.time_since_epoch() % seconds(1)).count();
96 return RetVal;
97}
98
99} // namespace sys
100} // namespace llvm
101
102#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:52
static void ReportErrnumFatal(const char *Msg, int errnum)
Definition Unix.h:63
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
std::chrono::nanoseconds toDuration(FILETIME Time)
LLVM_ABI 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:80
struct timeval toTimeVal(TimePoint< std::chrono::microseconds > TP)
Convert a time point to struct timeval.
Definition Unix.h:90
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.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:867