mirror of
https://github.com/apple/swift.git
synced 2026-02-27 18:26:24 +01:00
LLVM is switching to use these APIs instead of report_fatal_error. One
unfortunate characteristic of these APIs is that they take Twines and StringRefs
so if one wants to use a type that uses raw_ostream based print APIs, one has to
by hand create a SmallString, create an ostream to put the value into the
SmallString and then call the API. These helpers just perform that initial bit
of work of creating the SmallString and ostream and pass in the ostream to the
callback. After the callback returns, we pass the SmallString to
reportFatal{Internal,Usage}Error.
I also imported the llvm APIs into the swift namespace as well.
69 lines
2.3 KiB
C++
69 lines
2.3 KiB
C++
//===--- LLVMExtras.h -----------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file provides additional functionality on top of LLVM types
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SWIFT_BASIC_LLVMEXTRAS_H
|
|
#define SWIFT_BASIC_LLVMEXTRAS_H
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
#include "llvm/ADT/SetVector.h"
|
|
#include "llvm/ADT/SmallString.h"
|
|
#include "llvm/ADT/SmallVector.h"
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
namespace swift {
|
|
|
|
/// A SetVector that does no allocations under the specified size
|
|
///
|
|
/// swift::SmallSetVector provides the old SmallSetVector semantics that allow
|
|
/// storing types that don't have `operator==`.
|
|
template <typename T, unsigned N>
|
|
using SmallSetVector = llvm::SetVector<T, llvm::SmallVector<T, N>,
|
|
llvm::SmallDenseSet<T, N, llvm::DenseMapInfo<T>>>;
|
|
|
|
using llvm::reportFatalInternalError;
|
|
using llvm::reportFatalUsageError;
|
|
|
|
/// A helper for reportFatalInternalError that allows for a raw_ostream to be
|
|
/// used so that normal ostream printing constructs can be used with
|
|
/// reportFatalInternalError.
|
|
static inline void reportFatalInternalError(
|
|
llvm::function_ref<void(llvm::raw_ostream &)> callback) {
|
|
llvm::SmallString<64> result;
|
|
{
|
|
llvm::raw_svector_ostream os(result);
|
|
callback(os);
|
|
}
|
|
reportFatalInternalError(result.str());
|
|
}
|
|
|
|
/// A helper for reportFatalInternalError that allows for a raw_ostream to be
|
|
/// used so that normal ostream printing constructs can be used with
|
|
/// reportFatalInternalError.
|
|
static inline void
|
|
reportFatalUsageError(llvm::function_ref<void(llvm::raw_ostream &)> callback) {
|
|
llvm::SmallString<64> result;
|
|
{
|
|
llvm::raw_svector_ostream os(result);
|
|
callback(os);
|
|
}
|
|
reportFatalUsageError(result.str());
|
|
}
|
|
|
|
} // namespace swift
|
|
|
|
#endif // SWIFT_BASIC_LLVMEXTRAS_H
|