LLVM  10.0.0
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 #ifdef HAVE_SYS_PARAM_H
40 #include <sys/param.h>
41 #endif
42 
43 #ifdef HAVE_SYS_TIME_H
44 # include <sys/time.h>
45 #endif
46 #include <time.h>
47 
48 #ifdef HAVE_DLFCN_H
49 # include <dlfcn.h>
50 #endif
51 
52 #ifdef HAVE_FCNTL_H
53 # include <fcntl.h>
54 #endif
55 
56 /// This function builds an error message into \p ErrMsg using the \p prefix
57 /// string and the Unix error number given by \p errnum. If errnum is -1, the
58 /// default then the value of errno is used.
59 /// Make an error message
60 ///
61 /// If the error number can be converted to a string, it will be
62 /// separated from prefix by ": ".
63 static inline bool MakeErrMsg(
64  std::string* ErrMsg, const std::string& prefix, int errnum = -1) {
65  if (!ErrMsg)
66  return true;
67  if (errnum == -1)
68  errnum = errno;
69  *ErrMsg = prefix + ": " + llvm::sys::StrError(errnum);
70  return true;
71 }
72 
73 // Include StrError(errnum) in a fatal error message.
74 LLVM_ATTRIBUTE_NORETURN static inline void ReportErrnumFatal(const char *Msg,
75  int errnum) {
76  std::string ErrMsg;
77  MakeErrMsg(&ErrMsg, Msg, errnum);
79 }
80 
81 namespace llvm {
82 namespace sys {
83 
84 /// Convert a struct timeval to a duration. Note that timeval can be used both
85 /// as a time point and a duration. Be sure to check what the input represents.
86 inline std::chrono::microseconds toDuration(const struct timeval &TV) {
87  return std::chrono::seconds(TV.tv_sec) +
88  std::chrono::microseconds(TV.tv_usec);
89 }
90 
91 /// Convert a time point to struct timespec.
92 inline struct timespec toTimeSpec(TimePoint<> TP) {
93  using namespace std::chrono;
94 
95  struct timespec RetVal;
96  RetVal.tv_sec = toTimeT(TP);
97  RetVal.tv_nsec = (TP.time_since_epoch() % seconds(1)).count();
98  return RetVal;
99 }
100 
101 /// Convert a time point to struct timeval.
102 inline struct timeval toTimeVal(TimePoint<std::chrono::microseconds> TP) {
103  using namespace std::chrono;
104 
105  struct timeval RetVal;
106  RetVal.tv_sec = toTimeT(TP);
107  RetVal.tv_usec = (TP.time_since_epoch() % seconds(1)).count();
108  return RetVal;
109 }
110 
111 } // namespace sys
112 } // namespace llvm
113 
114 #endif
This class represents lattice values for constants.
Definition: AllocatorList.h:23
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
struct timespec toTimeSpec(TimePoint<> TP)
Convert a time point to struct timespec.
Definition: Unix.h:92
Definition: BitVector.h:937
std::string StrError()
Returns a string representation of the errno value, using whatever thread-safe variant of strerror() ...
Definition: Errno.cpp:31
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(adl_begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition: STLExtras.h:1248
std::time_t toTimeT(TimePoint<> TP)
Convert a TimePoint to std::time_t.
Definition: Chrono.h:36
struct timeval toTimeVal(TimePoint< std::chrono::microseconds > TP)
Convert a time point to struct timeval.
Definition: Unix.h:102
#define LLVM_ATTRIBUTE_NORETURN
Definition: Compiler.h:248
static LLVM_ATTRIBUTE_NORETURN void ReportErrnumFatal(const char *Msg, int errnum)
Definition: Unix.h:74
std::chrono::nanoseconds toDuration(FILETIME Time)
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:63
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.
Definition: Chrono.h:33