mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-23 04:32:42 +00:00
This moves over to the vanilla upstream GoogleTest pulled in the more expected manner with Bazel. It also adds Abseil and Google Benchmark libraries in the same fashion (there are cross dependencies here). As part of this, also introduce a dependency check test that can enforce basic layering of dependencies. For example, this lets us ensure that non-test Carbon code only depends on LLVM and Clang despite having other libraries available. There remains some cleanup to improve the way these dependency tests work, but this at least ensures we don't regress. I've also provided workarounds to allow both Carbon code and LLVM code to freely be used with GoogleTest (and other `std::ostream` based output code). This is done by extending the code in `//common/ostream.h`. One downside is that it requires opening the `llvm` namespace and adding an ADL_found overload there. I think on balance this is still a win and doesn't make me too nervous. The new version of GoogleTest requires printing more often from matchers and so I've also added several printing routines to types that previously didn't require them. Otherwise, most of the updates are just using the more conventional upstream style of including the headers and adding `ostream.h` where it is needed. I did consider moving code over to use `std::ostream` instead of LLVM's `raw_ostream`, but the advantages of not doing virtual dispatch still seem significant, and it also seems good to retain access to LLVM's formatting utilities built around `raw_ostream` given that we can't pull arbitrary dependencies into Carbon code outside of test code. All of this was slightly motivated by requests for newer features in GoogleTest, but much more-so by my desire to have access to Google Benchmark and Abseil when writing benchmarks. For example, using Abseil's random number generator seems extremely helpful when generating inputs for benchmarks. The growing dependencies between these packages further motivated me to just pull them all in and ensure they worked well.
96 lines
3.8 KiB
C++
96 lines
3.8 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#ifndef COMMON_OSTREAM_H_
|
|
#define COMMON_OSTREAM_H_
|
|
|
|
#include <ostream>
|
|
|
|
#include "llvm/Support/raw_os_ostream.h"
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
namespace Carbon {
|
|
|
|
// Support raw_ostream << for types which implement:
|
|
// void Print(llvm::raw_ostream& out) const;
|
|
template <typename T, typename std::enable_if<std::is_member_function_pointer<
|
|
decltype(&T::Print)>::value>::type* = nullptr>
|
|
auto operator<<(llvm::raw_ostream& out, const T& obj) -> llvm::raw_ostream& {
|
|
obj.Print(out);
|
|
return out;
|
|
}
|
|
|
|
// Prevents raw_ostream << for pointers to printable types.
|
|
template <typename T, typename std::enable_if<std::is_member_function_pointer<
|
|
decltype(&T::Print)>::value>::type* = nullptr>
|
|
__attribute__((unavailable(
|
|
"Received a pointer to a printable type, are you missing a `*`? "
|
|
"To print as a pointer, cast to void*."))) auto
|
|
operator<<(llvm::raw_ostream& out, const T* /*obj*/) -> llvm::raw_ostream&;
|
|
|
|
// Support std::ostream << for types which implement:
|
|
// void Print(llvm::raw_ostream& out) const;
|
|
template <typename T, typename std::enable_if<std::is_member_function_pointer<
|
|
decltype(&T::Print)>::value>::type* = nullptr>
|
|
auto operator<<(std::ostream& out, const T& obj) -> std::ostream& {
|
|
llvm::raw_os_ostream raw_os(out);
|
|
obj.Print(raw_os);
|
|
return out;
|
|
}
|
|
|
|
// Prevents std::ostream << for pointers to printable types.
|
|
template <typename T, typename std::enable_if<std::is_member_function_pointer<
|
|
decltype(&T::Print)>::value>::type* = nullptr>
|
|
__attribute__((unavailable(
|
|
"Received a pointer to a printable type, are you missing a `*`? "
|
|
"To print as a pointer, cast to void*."))) auto
|
|
operator<<(std::ostream& out, const T* /*obj*/) -> std::ostream&;
|
|
|
|
// Allow GoogleTest and GoogleMock to print even pointers by dereferencing them.
|
|
// This is important to allow automatic printing of arguments of mocked APIs.
|
|
template <typename T, typename std::enable_if<std::is_member_function_pointer<
|
|
decltype(&T::Print)>::value>::type* = nullptr>
|
|
void PrintTo(const T* p, std::ostream* out) {
|
|
*out << static_cast<const void*>(p);
|
|
|
|
// Also print the object if non-null.
|
|
if (p) {
|
|
*out << " pointing to " << *p;
|
|
}
|
|
}
|
|
|
|
} // namespace Carbon
|
|
|
|
namespace llvm {
|
|
|
|
// Injects an `operator<<` overload into the `llvm` namespace which detects LLVM
|
|
// types with `raw_ostream` overloads and uses that to map to a `std::ostream`
|
|
// overload. This allows LLVM types to be printed to `std::ostream` via their
|
|
// `raw_ostream` operator overloads, which is needed both for logging and
|
|
// testing.
|
|
//
|
|
// To make this overload be unusually low priority, it is designed to take even
|
|
// the `std::ostream` parameter as a template, and SFINAE disable itself unless
|
|
// that template parameter matches `std::ostream`. This ensures that an
|
|
// *explicit* operator will be preferred when provided. Some LLVM types may have
|
|
// this, and so we want to prioritize accordingly.
|
|
//
|
|
// It would be slightly cleaner for LLVM itself to provide this overload in
|
|
// `raw_os_ostream.h` so that we wouldn't need to inject into its namespace, but
|
|
// supporting `std::ostream` isn't a priority for LLVM so we handle it locally
|
|
// instead.
|
|
template <typename S, typename T,
|
|
typename = std::enable_if_t<std::is_base_of_v<
|
|
std::ostream, std::remove_reference_t<std::remove_cv_t<S>>>>,
|
|
typename = std::enable_if_t<!std::is_same_v<
|
|
std::remove_reference_t<std::remove_cv_t<T>>, raw_ostream>>>
|
|
auto operator<<(S& standard_out, const T& value) -> S& {
|
|
raw_os_ostream(standard_out) << value;
|
|
return standard_out;
|
|
}
|
|
|
|
} // namespace llvm
|
|
|
|
#endif // COMMON_OSTREAM_H_
|