Andrew Rogers 9c5f451d1c
[llvm] annotate interfaces in llvm/Support for DLL export (#136014)
## Purpose

This patch is one in a series of code-mods that annotate LLVM’s public
interface for export. This patch annotates the `llvm/Support` library.
These annotations currently have no meaningful impact on the LLVM build;
however, they are a prerequisite to support an LLVM Windows DLL (shared
library) build.

## Background

This effort is tracked in #109483. Additional context is provided in
[this
discourse](https://discourse.llvm.org/t/psa-annotating-llvm-public-interface/85307),
and documentation for `LLVM_ABI` and related annotations is found in the
LLVM repo
[here](https://github.com/llvm/llvm-project/blob/main/llvm/docs/InterfaceExportAnnotations.rst).

The bulk of these changes were generated automatically using the
[Interface Definition Scanner (IDS)](https://github.com/compnerd/ids)
tool, followed formatting with `git clang-format`.

The following manual adjustments were also applied after running IDS on
Linux:
- Add `#include "llvm/Support/Compiler.h"` to files where it was not
auto-added by IDS due to no pre-existing block of include statements.
- Add `LLVM_ABI` to Windows-only code (auto generated with IDS on
Windows)
- Explicitly make classes non-copyable where needed to due IDS adding
`LLVM_ABI` at the class level
- Add `LLVM_TEMPLATE_ABI` and `LLVM_EXPORT_TEMPLATE` to exported
instantiated templates
- Add `LLVM_ABI_FRIEND` to a small number of `friend` function
declarations
- Add `LLVM_ABI` to a subset of private class methods and fields that
require export
- Add `LLVM_ABI` to a small number of symbols that require export but
are not declared in headers
- Add `LLVM_ABI` functions defined via X-macro

## Validation

Local builds and tests to validate cross-platform compatibility. This
included llvm, clang, and lldb on the following configurations:

- Windows with MSVC
- Windows with Clang
- Linux with GCC
- Linux with Clang
- Darwin with Clang
2025-05-12 12:13:52 -07:00

130 lines
4.2 KiB
C++

//===- Support/Chrono.cpp - Utilities for Timing Manipulation ---*- 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
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/Chrono.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/raw_ostream.h"
namespace llvm {
using namespace sys;
LLVM_ABI const char llvm::detail::unit<std::ratio<3600>>::value[] = "h";
LLVM_ABI const char llvm::detail::unit<std::ratio<60>>::value[] = "m";
LLVM_ABI const char llvm::detail::unit<std::ratio<1>>::value[] = "s";
LLVM_ABI const char llvm::detail::unit<std::milli>::value[] = "ms";
LLVM_ABI const char llvm::detail::unit<std::micro>::value[] = "us";
LLVM_ABI const char llvm::detail::unit<std::nano>::value[] = "ns";
static inline struct tm getStructTM(TimePoint<> TP) {
struct tm Storage;
std::time_t OurTime = toTimeT(TP);
#if defined(LLVM_ON_UNIX)
struct tm *LT = ::localtime_r(&OurTime, &Storage);
assert(LT);
(void)LT;
#endif
#if defined(_WIN32)
int Error = ::localtime_s(&Storage, &OurTime);
assert(!Error);
(void)Error;
#endif
return Storage;
}
static inline struct tm getStructTMUtc(UtcTime<> TP) {
struct tm Storage;
std::time_t OurTime = toTimeT(TP);
#if defined(LLVM_ON_UNIX)
struct tm *LT = ::gmtime_r(&OurTime, &Storage);
assert(LT);
(void)LT;
#endif
#if defined(_WIN32)
int Error = ::gmtime_s(&Storage, &OurTime);
assert(!Error);
(void)Error;
#endif
return Storage;
}
raw_ostream &operator<<(raw_ostream &OS, TimePoint<> TP) {
struct tm LT = getStructTM(TP);
char Buffer[sizeof("YYYY-MM-DD HH:MM:SS")];
strftime(Buffer, sizeof(Buffer), "%Y-%m-%d %H:%M:%S", &LT);
return OS << Buffer << '.'
<< format("%.9lu",
long((TP.time_since_epoch() % std::chrono::seconds(1))
.count()));
}
template <class T>
static void format(const T &Fractional, struct tm &LT, raw_ostream &OS,
StringRef Style) {
using namespace std::chrono;
// Handle extensions first. strftime mangles unknown %x on some platforms.
if (Style.empty()) Style = "%Y-%m-%d %H:%M:%S.%N";
std::string Format;
raw_string_ostream FStream(Format);
for (unsigned I = 0; I < Style.size(); ++I) {
if (Style[I] == '%' && Style.size() > I + 1) switch (Style[I + 1]) {
case 'L': // Milliseconds, from Ruby.
FStream << llvm::format(
"%.3lu", (long)duration_cast<milliseconds>(Fractional).count());
++I;
continue;
case 'f': // Microseconds, from Python.
FStream << llvm::format(
"%.6lu", (long)duration_cast<microseconds>(Fractional).count());
++I;
continue;
case 'N': // Nanoseconds, from date(1).
FStream << llvm::format(
"%.9lu", (long)duration_cast<nanoseconds>(Fractional).count());
++I;
continue;
case '%': // Consume %%, so %%f parses as (%%)f not %(%f)
FStream << "%%";
++I;
continue;
}
FStream << Style[I];
}
FStream.flush();
char Buffer[256]; // Should be enough for anywhen.
size_t Len = strftime(Buffer, sizeof(Buffer), Format.c_str(), &LT);
OS << (Len ? Buffer : "BAD-DATE-FORMAT");
}
void format_provider<UtcTime<std::chrono::seconds>>::format(
const UtcTime<std::chrono::seconds> &T, raw_ostream &OS, StringRef Style) {
using namespace std::chrono;
UtcTime<seconds> Truncated =
UtcTime<seconds>(duration_cast<seconds>(T.time_since_epoch()));
auto Fractional = T - Truncated;
struct tm LT = getStructTMUtc(Truncated);
llvm::format(Fractional, LT, OS, Style);
}
void format_provider<TimePoint<>>::format(const TimePoint<> &T, raw_ostream &OS,
StringRef Style) {
using namespace std::chrono;
TimePoint<seconds> Truncated = time_point_cast<seconds>(T);
auto Fractional = T - Truncated;
struct tm LT = getStructTM(Truncated);
llvm::format(Fractional, LT, OS, Style);
}
} // namespace llvm