Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/Chrono.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Support/Chrono.cpp - Utilities for Timing Manipulation ---*- 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
#include "llvm/Support/Chrono.h"
10
#include "llvm/Config/llvm-config.h"
11
#include "llvm/Support/Format.h"
12
#include "llvm/Support/raw_ostream.h"
13
14
namespace llvm {
15
16
using namespace sys;
17
18
const char llvm::detail::unit<std::ratio<3600>>::value[] = "h";
19
const char llvm::detail::unit<std::ratio<60>>::value[] = "m";
20
const char llvm::detail::unit<std::ratio<1>>::value[] = "s";
21
const char llvm::detail::unit<std::milli>::value[] = "ms";
22
const char llvm::detail::unit<std::micro>::value[] = "us";
23
const char llvm::detail::unit<std::nano>::value[] = "ns";
24
25
297
static inline struct tm getStructTM(TimePoint<> TP) {
26
297
  struct tm Storage;
27
297
  std::time_t OurTime = toTimeT(TP);
28
297
29
297
#if defined(LLVM_ON_UNIX)
30
297
  struct tm *LT = ::localtime_r(&OurTime, &Storage);
31
297
  assert(LT);
32
297
  (void)LT;
33
297
#endif
34
#if defined(_WIN32)
35
  int Error = ::localtime_s(&Storage, &OurTime);
36
  assert(!Error);
37
  (void)Error;
38
#endif
39
40
297
  return Storage;
41
297
}
42
43
239
raw_ostream &operator<<(raw_ostream &OS, TimePoint<> TP) {
44
239
  struct tm LT = getStructTM(TP);
45
239
  char Buffer[sizeof("YYYY-MM-DD HH:MM:SS")];
46
239
  strftime(Buffer, sizeof(Buffer), "%Y-%m-%d %H:%M:%S", &LT);
47
239
  return OS << Buffer << '.'
48
239
            << format("%.9lu",
49
239
                      long((TP.time_since_epoch() % std::chrono::seconds(1))
50
239
                               .count()));
51
239
}
52
53
void format_provider<TimePoint<>>::format(const TimePoint<> &T, raw_ostream &OS,
54
58
                                          StringRef Style) {
55
58
  using namespace std::chrono;
56
58
  TimePoint<seconds> Truncated = time_point_cast<seconds>(T);
57
58
  auto Fractional = T - Truncated;
58
58
  struct tm LT = getStructTM(Truncated);
59
58
  // Handle extensions first. strftime mangles unknown %x on some platforms.
60
58
  if (Style.empty()) 
Style = "%Y-%m-%d %H:%M:%S.%N"1
;
61
58
  std::string Format;
62
58
  raw_string_ostream FStream(Format);
63
820
  for (unsigned I = 0; I < Style.size(); 
++I762
) {
64
762
    if (Style[I] == '%' && 
Style.size() > I + 1274
) switch (Style[I + 1]) {
65
274
        case 'L':  // Milliseconds, from Ruby.
66
1
          FStream << llvm::format(
67
1
              "%.3lu", (long)duration_cast<milliseconds>(Fractional).count());
68
1
          ++I;
69
1
          continue;
70
274
        case 'f':  // Microseconds, from Python.
71
1
          FStream << llvm::format(
72
1
              "%.6lu", (long)duration_cast<microseconds>(Fractional).count());
73
1
          ++I;
74
1
          continue;
75
274
        case 'N':  // Nanoseconds, from date(1).
76
2
          FStream << llvm::format(
77
2
              "%.6lu", (long)duration_cast<nanoseconds>(Fractional).count());
78
2
          ++I;
79
2
          continue;
80
274
        case '%':  // Consume %%, so %%f parses as (%%)f not %(%f)
81
1
          FStream << "%%";
82
1
          ++I;
83
1
          continue;
84
757
      }
85
757
    FStream << Style[I];
86
757
  }
87
58
  FStream.flush();
88
58
  char Buffer[256];  // Should be enough for anywhen.
89
58
  size_t Len = strftime(Buffer, sizeof(Buffer), Format.c_str(), &LT);
90
58
  OS << (Len ? Buffer : 
"BAD-DATE-FORMAT"0
);
91
58
}
92
93
} // namespace llvm