From 5f670294790974befe285662bb6faaff9bcac305 Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Tue, 2 Nov 2021 20:14:12 -0700 Subject: [PATCH] Use upstream GoogleTest and add related test utils. (#876) 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. --- WORKSPACE | 39 +++++++++ bazel/check_deps/BUILD | 87 +++++++++++++++++++ bazel/check_deps/check_non_test_cc_deps.py | 74 ++++++++++++++++ common/BUILD | 9 +- common/check_test.cpp | 2 +- common/indirect_value_test.cpp | 4 +- common/ostream.h | 68 ++++++++++++++- common/string_helpers_test.cpp | 6 +- compile_flags.txt | 18 ++-- executable_semantics/BUILD | 5 ++ executable_semantics/ast/BUILD | 11 +-- executable_semantics/ast/expression_test.cpp | 5 +- executable_semantics/ast/pattern_test.cpp | 5 +- executable_semantics/common/BUILD | 8 +- executable_semantics/common/error_test.cpp | 2 +- executable_semantics/interpreter/BUILD | 5 +- executable_semantics/syntax/BUILD | 5 +- migrate_cpp/cpp_refactoring/BUILD | 9 +- .../cpp_refactoring/matcher_test_base.h | 5 +- toolchain/common/BUILD | 4 +- toolchain/common/yaml_test_helpers.cpp | 1 + toolchain/common/yaml_test_helpers.h | 6 +- toolchain/diagnostics/BUILD | 6 +- .../diagnostics/diagnostic_emitter_test.cpp | 5 +- toolchain/diagnostics/mocks.h | 3 +- toolchain/driver/BUILD | 4 +- toolchain/driver/driver_test.cpp | 5 +- toolchain/lexer/BUILD | 20 ++--- toolchain/lexer/numeric_literal_test.cpp | 6 +- toolchain/lexer/string_literal_test.cpp | 6 +- toolchain/lexer/token_kind_test.cpp | 5 +- toolchain/lexer/tokenized_buffer.cpp | 5 ++ toolchain/lexer/tokenized_buffer.h | 5 ++ toolchain/lexer/tokenized_buffer_test.cpp | 5 +- .../lexer/tokenized_buffer_test_helpers.h | 3 +- toolchain/parser/BUILD | 14 ++- toolchain/parser/parse_node_kind_test.cpp | 3 +- toolchain/parser/parse_test_helpers.h | 3 +- toolchain/parser/parse_tree.cpp | 14 +++ toolchain/parser/parse_tree.h | 11 +++ toolchain/parser/parse_tree_test.cpp | 6 +- toolchain/parser/precedence_test.cpp | 5 +- toolchain/source/BUILD | 4 +- toolchain/source/source_buffer_test.cpp | 5 +- 44 files changed, 422 insertions(+), 99 deletions(-) create mode 100644 bazel/check_deps/BUILD create mode 100644 bazel/check_deps/check_non_test_cc_deps.py diff --git a/WORKSPACE b/WORKSPACE index e529175a26dc..afd95bee5cc7 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -89,6 +89,45 @@ load( configure_clang_toolchain(name = "bazel_cc_toolchain") +############################################################################### +# Abseil libraries +############################################################################### + +abseil_version = "4a995b1eaa4a602f0d3a9ff8eac89d4649cd2fe8" + +http_archive( + name = "com_google_absl", + sha256 = "f5021900ff9a6b8f39406d15f460714660ab6b3e727754663d786d75ecad5ee0", + strip_prefix = "abseil-cpp-%s" % abseil_version, + urls = ["https://github.com/abseil/abseil-cpp/archive/%s.zip" % abseil_version], +) + +############################################################################### +# GoogleTest libraries +############################################################################### + +googletest_version = "075810f7a20405ea09a93f68847d6e963212fa62" + +http_archive( + name = "com_google_googletest", + sha256 = "19949c33e795197dbb8610672c18bff447dc31faef3257665d69d1bf0884d67b", + strip_prefix = "googletest-%s" % googletest_version, + urls = ["https://github.com/google/googletest/archive/%s.zip" % googletest_version], +) + +############################################################################### +# Google Benchmark libraries +############################################################################### + +benchmark_version = "0baacde3618ca617da95375e0af13ce1baadea47" + +http_archive( + name = "com_github_google_benchmark", + sha256 = "19949c33e795197dbb8610672c18bff447dc31faef3257665d69d1bf0884d67b", + strip_prefix = "benchmark-%s" % benchmark_version, + urls = ["https://github.com/google/benchmark/archive/%s.zip" % benchmark_version], +) + ############################################################################### # LLVM libraries ############################################################################### diff --git a/bazel/check_deps/BUILD b/bazel/check_deps/BUILD new file mode 100644 index 000000000000..8036552ea6c5 --- /dev/null +++ b/bazel/check_deps/BUILD @@ -0,0 +1,87 @@ +# 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 + +load("@mypy_integration//:mypy.bzl", "mypy_test") + +# This filegroup should contain all the non-test C++ rules in the repository to +# enable dependency checking. It can be regenerated by running the following +# query command: +# +# ``` +# bazelisk query 'kind("cc.* rule", attr(testonly, 0, //...))' +# ``` +filegroup( + name = "non_test_cc_rules", + data = [ + "//common:check", + "//common:indirect_value", + "//common:ostream", + "//common:string_helpers", + "//executable_semantics", + "//executable_semantics/ast", + "//executable_semantics/ast:class_definition", + "//executable_semantics/ast:declaration", + "//executable_semantics/ast:expression", + "//executable_semantics/ast:library_name", + "//executable_semantics/ast:member", + "//executable_semantics/ast:paren_contents", + "//executable_semantics/ast:pattern", + "//executable_semantics/ast:source_location", + "//executable_semantics/ast:statement", + "//executable_semantics/common:arena", + "//executable_semantics/common:error", + "//executable_semantics/common:nonnull", + "//executable_semantics/interpreter", + "//executable_semantics/interpreter:address", + "//executable_semantics/interpreter:dictionary", + "//executable_semantics/interpreter:exec_program", + "//executable_semantics/interpreter:field_path", + "//executable_semantics/interpreter:heap", + "//executable_semantics/interpreter:stack", + "//executable_semantics/interpreter:type_checker", + "//executable_semantics/syntax", + "//executable_semantics/syntax:bison_wrap", + "//migrate_cpp/cpp_refactoring", + "//migrate_cpp/cpp_refactoring:fn_inserter", + "//migrate_cpp/cpp_refactoring:for_range", + "//migrate_cpp/cpp_refactoring:matcher", + "//migrate_cpp/cpp_refactoring:var_decl", + "//toolchain/diagnostics:diagnostic_emitter", + "//toolchain/diagnostics:null_diagnostics", + "//toolchain/driver", + "//toolchain/driver:carbon", + "//toolchain/lexer:character_set", + "//toolchain/lexer:numeric_literal", + "//toolchain/lexer:string_literal", + "//toolchain/lexer:token_kind", + "//toolchain/lexer:tokenized_buffer", + "//toolchain/parser:parse_node_kind", + "//toolchain/parser:parse_tree", + "//toolchain/parser:precedence", + "//toolchain/source:source_buffer", + ], +) + +genquery( + name = "non_test_cc_deps.txt", + expression = "kind('cc.* rule', deps(//bazel/check_deps:non_test_cc_rules))", + opts = [ + "--notool_deps", + "--noimplicit_deps", + ], + scope = [":non_test_cc_rules"], +) + +py_test( + name = "check_non_test_cc_deps", + srcs = ["check_non_test_cc_deps.py"], + data = [":non_test_cc_deps.txt"], + main = "check_non_test_cc_deps.py", +) + +mypy_test( + name = "check_non_test_cc_deps_mypy_test", + include_imports = True, + deps = [":check_non_test_cc_deps"], +) diff --git a/bazel/check_deps/check_non_test_cc_deps.py b/bazel/check_deps/check_non_test_cc_deps.py new file mode 100644 index 000000000000..7a0f26d6cc5a --- /dev/null +++ b/bazel/check_deps/check_non_test_cc_deps.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 + +"""Check that non-test C++ rules only depend on Carbon and LLVM. + +Carbon works to ensure its user-visible libraries and binaries only depend on +their code and LLVM. Among other benefits, this provides a single, simple +license used for the whole project. + +However, we frequently use third-party projects and libraries where useful in +our test code. Here, we verify that the dependencies of non-test C++ rules only +include Carbon and LLVM code. +""" + +__copyright__ = """ +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 +""" + +import os +import sys +from pathlib import Path + +runfiles = Path(os.environ["TEST_SRCDIR"]) +deps_path = ( + runfiles / "carbon" / "bazel" / "check_deps" / "non_test_cc_deps.txt" +) +try: + with deps_path.open() as deps_file: + deps = deps_file.read().splitlines() +except FileNotFoundError: + sys.exit("ERROR: unable to find deps file: %s" % deps_path) + +for dep in deps: + print("Checking dependency: " + dep) + repo, _, rule = dep.partition("//") + if repo == "" and not rule.startswith("third_party"): + # Carbon code is always allowed. + continue + if repo == "@llvm-project": + package, _, rule = rule.partition(":") + + # Other packages in the LLVM project shouldn't be accidentally used + # in Carbon. We can expand the above list if use cases emerge. + if package not in ("llvm", "lld", "clang"): + sys.exit( + "ERROR: unexpected dependency into the LLVM project: %s" % dep + ) + + # Check for accidentally using the copy of GoogleTest in LLVM. + if rule in ("gmock", "gtest", "gtest_main"): + sys.exit( + "ERROR: dependency on LLVM's GoogleTest from non-test code: %s" + % dep + ) + + # The rest of LLVM, LLD, and Clang themselves are safe to depend on. + continue + if repo in ("@llvm_terminfo", "@llvm_zlib"): + # These are stubs wrapping system libraries for LLVM. They aren't + # distributed and so should be fine. + continue + if repo in ( + "@com_google_absl", + "@com_google_googletest", + "@com_github_google_benchmark", + ): + # This should never be reached from non-test code, but these targets do + # exist. Specially diagnose them to try to provide a more helpful + # message. + sys.exit("ERROR: dependency only allowed in test code: %s" % dep) + + # Conservatively fail if a dependency isn't explicitly allowed above. + sys.exit("ERROR: unknown dependency: %s" % dep) diff --git a/common/BUILD b/common/BUILD index 001140b9e099..8a0ff09c4de8 100644 --- a/common/BUILD +++ b/common/BUILD @@ -18,8 +18,7 @@ cc_test( srcs = ["check_test.cpp"], deps = [ ":check", - "@llvm-project//llvm:gtest", - "@llvm-project//llvm:gtest_main", + "@com_google_googletest//:gtest_main", ], ) @@ -33,8 +32,7 @@ cc_test( srcs = ["indirect_value_test.cpp"], deps = [ ":indirect_value", - "@llvm-project//llvm:gtest", - "@llvm-project//llvm:gtest_main", + "@com_google_googletest//:gtest_main", ], ) @@ -61,7 +59,6 @@ cc_test( srcs = ["string_helpers_test.cpp"], deps = [ ":string_helpers", - "@llvm-project//llvm:gtest", - "@llvm-project//llvm:gtest_main", + "@com_google_googletest//:gtest_main", ], ) diff --git a/common/check_test.cpp b/common/check_test.cpp index abc289546526..d17ff5cffa4d 100644 --- a/common/check_test.cpp +++ b/common/check_test.cpp @@ -4,7 +4,7 @@ #include "common/check.h" -#include "gtest/gtest.h" +#include namespace Carbon { diff --git a/common/indirect_value_test.cpp b/common/indirect_value_test.cpp index b9f903e69cc7..9263578e3dbd 100644 --- a/common/indirect_value_test.cpp +++ b/common/indirect_value_test.cpp @@ -4,9 +4,9 @@ #include "common/indirect_value.h" -#include +#include -#include "gtest/gtest.h" +#include namespace Carbon { namespace { diff --git a/common/ostream.h b/common/ostream.h index 1920a82493c5..5378279eb2b7 100644 --- a/common/ostream.h +++ b/common/ostream.h @@ -5,11 +5,14 @@ #ifndef COMMON_OSTREAM_H_ #define COMMON_OSTREAM_H_ +#include + +#include "llvm/Support/raw_os_ostream.h" #include "llvm/Support/raw_ostream.h" namespace Carbon { -// Support ostream << for types which implement: +// Support raw_ostream << for types which implement: // void Print(llvm::raw_ostream& out) const; template ::value>::type* = nullptr> @@ -18,7 +21,7 @@ auto operator<<(llvm::raw_ostream& out, const T& obj) -> llvm::raw_ostream& { return out; } -// Prevents ostream << for pointers to printable types. +// Prevents raw_ostream << for pointers to printable types. template ::value>::type* = nullptr> __attribute__((unavailable( @@ -26,6 +29,67 @@ __attribute__((unavailable( "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 ::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 ::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 ::value>::type* = nullptr> +void PrintTo(const T* p, std::ostream* out) { + *out << static_cast(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 = std::enable_if_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_ diff --git a/common/string_helpers_test.cpp b/common/string_helpers_test.cpp index 3a21772270ef..0d91945c281a 100644 --- a/common/string_helpers_test.cpp +++ b/common/string_helpers_test.cpp @@ -4,10 +4,10 @@ #include "common/string_helpers.h" -#include +#include +#include -#include "gmock/gmock.h" -#include "gtest/gtest.h" +#include using ::testing::Eq; using ::testing::Optional; diff --git a/compile_flags.txt b/compile_flags.txt index b9d01a5d249c..91e03d586dd4 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -37,7 +37,6 @@ -DGTEST_HAS_RTTI=0 -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS --DGTEST_USE_OWN_TR1_TUPLE=1 -iquote . -iquote @@ -62,19 +61,26 @@ bazel-bin/external/llvm_zlib bazel-execroot/external/bazel_tools -iquote bazel-bin/external/bazel_tools --Ibazel-execroot/external/llvm-project/llvm/utils/unittest/googletest/src -isystem bazel-execroot/external/llvm-project/llvm/include -isystem bazel-bin/external/llvm-project/llvm/include -isystem -bazel-execroot/external/llvm-project/llvm/utils/unittest/googlemock/include +bazel-execroot/external/com_google_googletest/googlemock -isystem -bazel-bin/external/llvm-project/llvm/utils/unittest/googlemock/include +bazel-bin/external/com_google_googletest/googlemock -isystem -bazel-execroot/external/llvm-project/llvm/utils/unittest/googletest/include +bazel-execroot/external/com_google_googletest/googlemock/include -isystem -bazel-bin/external/llvm-project/llvm/utils/unittest/googletest/include +bazel-bin/external/com_google_googletest/googlemock/include +-isystem +bazel-execroot/external/com_google_googletest/googletest +-isystem +bazel-bin/external/com_google_googletest/googletest +-isystem +bazel-execroot/external/com_google_googletest/googletest/include +-isystem +bazel-bin/external/com_google_googletest/googletest/include -std=c++17 -stdlib=libc++ -no-canonical-prefixes diff --git a/executable_semantics/BUILD b/executable_semantics/BUILD index dc6e9a1351d9..295486a3c7d9 100644 --- a/executable_semantics/BUILD +++ b/executable_semantics/BUILD @@ -4,6 +4,11 @@ load("//bazel/testing:lit_test.bzl", "lit_test") +package(default_visibility = [ + "//bazel/check_deps:__pkg__", + "//executable_semantics:__subpackages__", +]) + cc_binary( name = "executable_semantics", srcs = ["main.cpp"], diff --git a/executable_semantics/ast/BUILD b/executable_semantics/ast/BUILD index c0b81782460a..b4f284e7d12e 100644 --- a/executable_semantics/ast/BUILD +++ b/executable_semantics/ast/BUILD @@ -2,7 +2,10 @@ # Exceptions. See /LICENSE for license information. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -package(default_visibility = ["//executable_semantics:__subpackages__"]) +package(default_visibility = [ + "//bazel/check_deps:__pkg__", + "//executable_semantics:__subpackages__", +]) cc_library( name = "ast", @@ -62,8 +65,7 @@ cc_test( deps = [ ":expression", ":paren_contents", - "@llvm-project//llvm:gtest", - "@llvm-project//llvm:gtest_main", + "@com_google_googletest//:gtest_main", ], ) @@ -112,9 +114,8 @@ cc_test( deps = [ ":paren_contents", ":pattern", + "@com_google_googletest//:gtest_main", "@llvm-project//llvm:Support", - "@llvm-project//llvm:gtest", - "@llvm-project//llvm:gtest_main", ], ) diff --git a/executable_semantics/ast/expression_test.cpp b/executable_semantics/ast/expression_test.cpp index 11b759c030fe..95ca49323020 100644 --- a/executable_semantics/ast/expression_test.cpp +++ b/executable_semantics/ast/expression_test.cpp @@ -4,12 +4,13 @@ #include "executable_semantics/ast/expression.h" +#include +#include + #include #include "executable_semantics/ast/paren_contents.h" #include "executable_semantics/common/arena.h" -#include "gmock/gmock.h" -#include "gtest/gtest.h" #include "llvm/Support/Casting.h" namespace Carbon { diff --git a/executable_semantics/ast/pattern_test.cpp b/executable_semantics/ast/pattern_test.cpp index d7b23a32deba..e52cb35503d8 100644 --- a/executable_semantics/ast/pattern_test.cpp +++ b/executable_semantics/ast/pattern_test.cpp @@ -4,11 +4,12 @@ #include "executable_semantics/ast/pattern.h" +#include +#include + #include "executable_semantics/ast/expression.h" #include "executable_semantics/ast/paren_contents.h" #include "executable_semantics/common/arena.h" -#include "gmock/gmock.h" -#include "gtest/gtest.h" #include "llvm/Support/Casting.h" namespace Carbon { diff --git a/executable_semantics/common/BUILD b/executable_semantics/common/BUILD index e17d835e8634..1c58953f1b91 100644 --- a/executable_semantics/common/BUILD +++ b/executable_semantics/common/BUILD @@ -2,7 +2,10 @@ # Exceptions. See /LICENSE for license information. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -package(default_visibility = ["//executable_semantics:__subpackages__"]) +package(default_visibility = [ + "//bazel/check_deps:__pkg__", + "//executable_semantics:__subpackages__", +]) cc_library( name = "arena", @@ -25,8 +28,7 @@ cc_test( srcs = ["error_test.cpp"], deps = [ ":error", - "@llvm-project//llvm:gtest", - "@llvm-project//llvm:gtest_main", + "@com_google_googletest//:gtest_main", ], ) diff --git a/executable_semantics/common/error_test.cpp b/executable_semantics/common/error_test.cpp index d125b54b14de..46a7daae82cd 100644 --- a/executable_semantics/common/error_test.cpp +++ b/executable_semantics/common/error_test.cpp @@ -4,7 +4,7 @@ #include "executable_semantics/common/error.h" -#include "gtest/gtest.h" +#include namespace Carbon { namespace { diff --git a/executable_semantics/interpreter/BUILD b/executable_semantics/interpreter/BUILD index 0a760e301a61..bc29b83543b4 100644 --- a/executable_semantics/interpreter/BUILD +++ b/executable_semantics/interpreter/BUILD @@ -2,7 +2,10 @@ # Exceptions. See /LICENSE for license information. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -package(default_visibility = ["//executable_semantics:__pkg__"]) +package(default_visibility = [ + "//bazel/check_deps:__pkg__", + "//executable_semantics:__pkg__", +]) # These currently have to be a single build rule because of a dependency cycle # in printing. diff --git a/executable_semantics/syntax/BUILD b/executable_semantics/syntax/BUILD index e0f654c0eac5..92cc2025d7fd 100644 --- a/executable_semantics/syntax/BUILD +++ b/executable_semantics/syntax/BUILD @@ -4,7 +4,10 @@ load("@mypy_integration//:mypy.bzl", "mypy_test") -package(default_visibility = ["//executable_semantics:__pkg__"]) +package(default_visibility = [ + "//bazel/check_deps:__pkg__", + "//executable_semantics:__pkg__", +]) cc_library( name = "bison_wrap", diff --git a/migrate_cpp/cpp_refactoring/BUILD b/migrate_cpp/cpp_refactoring/BUILD index 8a233b710d80..0deb1406bd6b 100644 --- a/migrate_cpp/cpp_refactoring/BUILD +++ b/migrate_cpp/cpp_refactoring/BUILD @@ -34,10 +34,9 @@ cc_library( hdrs = ["matcher_test_base.h"], deps = [ ":matcher", + "@com_google_googletest//:gtest", "@llvm-project//clang:ast_matchers", "@llvm-project//clang:tooling", - "@llvm-project//llvm:gmock", - "@llvm-project//llvm:gtest", ], ) @@ -56,8 +55,8 @@ cc_test( deps = [ ":fn_inserter", ":matcher_test_base", + "@com_google_googletest//:gtest_main", "@llvm-project//clang:tooling", - "@llvm-project//llvm:gtest_main", ], ) @@ -74,8 +73,8 @@ cc_test( deps = [ ":for_range", ":matcher_test_base", + "@com_google_googletest//:gtest_main", "@llvm-project//clang:tooling", - "@llvm-project//llvm:gtest_main", ], ) @@ -92,7 +91,7 @@ cc_test( deps = [ ":matcher_test_base", ":var_decl", + "@com_google_googletest//:gtest_main", "@llvm-project//clang:tooling", - "@llvm-project//llvm:gtest_main", ], ) diff --git a/migrate_cpp/cpp_refactoring/matcher_test_base.h b/migrate_cpp/cpp_refactoring/matcher_test_base.h index d0f51ca0878f..9d0e70565721 100644 --- a/migrate_cpp/cpp_refactoring/matcher_test_base.h +++ b/migrate_cpp/cpp_refactoring/matcher_test_base.h @@ -5,11 +5,12 @@ #ifndef MIGRATE_CPP_CPP_REFACTORING_MATCHER_TEST_BASE_H_ #define MIGRATE_CPP_CPP_REFACTORING_MATCHER_TEST_BASE_H_ +#include +#include + #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Tooling/Core/Replacement.h" #include "clang/Tooling/Tooling.h" -#include "gmock/gmock.h" -#include "gtest/gtest.h" #include "migrate_cpp/cpp_refactoring/matcher_manager.h" namespace Carbon { diff --git a/toolchain/common/BUILD b/toolchain/common/BUILD index 21c8ed865e5d..0bbde85550a9 100644 --- a/toolchain/common/BUILD +++ b/toolchain/common/BUILD @@ -10,7 +10,7 @@ cc_library( srcs = ["yaml_test_helpers.cpp"], hdrs = ["yaml_test_helpers.h"], deps = [ - "@llvm-project//llvm:gmock", - "@llvm-project//llvm:gtest", + "//common:ostream", + "@com_google_googletest//:gtest", ], ) diff --git a/toolchain/common/yaml_test_helpers.cpp b/toolchain/common/yaml_test_helpers.cpp index 58380c0f1a28..46aceaae52ad 100644 --- a/toolchain/common/yaml_test_helpers.cpp +++ b/toolchain/common/yaml_test_helpers.cpp @@ -4,6 +4,7 @@ #include "toolchain/common/yaml_test_helpers.h" +#include "llvm/ADT/SmallString.h" #include "llvm/Support/YAMLParser.h" namespace Carbon::Testing::Yaml { diff --git a/toolchain/common/yaml_test_helpers.h b/toolchain/common/yaml_test_helpers.h index 058168fd26fc..eeb78d9288ff 100644 --- a/toolchain/common/yaml_test_helpers.h +++ b/toolchain/common/yaml_test_helpers.h @@ -47,13 +47,15 @@ #ifndef TOOLCHAIN_COMMON_YAML_TEST_HELPERS_H_ #define TOOLCHAIN_COMMON_YAML_TEST_HELPERS_H_ +#include +#include + #include #include #include #include -#include "gmock/gmock.h" -#include "gtest/gtest.h" +#include "common/ostream.h" namespace Carbon { namespace Testing { diff --git a/toolchain/diagnostics/BUILD b/toolchain/diagnostics/BUILD index 5752e35f62ab..9af2df3866b3 100644 --- a/toolchain/diagnostics/BUILD +++ b/toolchain/diagnostics/BUILD @@ -26,8 +26,8 @@ cc_library( hdrs = ["mocks.h"], deps = [ ":diagnostic_emitter", + "@com_google_googletest//:gtest", "@llvm-project//llvm:Support", - "@llvm-project//llvm:gmock", ], ) @@ -37,9 +37,7 @@ cc_test( deps = [ ":diagnostic_emitter", ":mocks", + "@com_google_googletest//:gtest_main", "@llvm-project//llvm:Support", - "@llvm-project//llvm:gmock", - "@llvm-project//llvm:gtest", - "@llvm-project//llvm:gtest_main", ], ) diff --git a/toolchain/diagnostics/diagnostic_emitter_test.cpp b/toolchain/diagnostics/diagnostic_emitter_test.cpp index 2a3be968a4ee..7783d55b9ff3 100644 --- a/toolchain/diagnostics/diagnostic_emitter_test.cpp +++ b/toolchain/diagnostics/diagnostic_emitter_test.cpp @@ -4,8 +4,9 @@ #include "toolchain/diagnostics/diagnostic_emitter.h" -#include "gmock/gmock.h" -#include "gtest/gtest.h" +#include +#include + #include "llvm/ADT/StringRef.h" #include "llvm/Support/FormatVariadic.h" #include "toolchain/diagnostics/mocks.h" diff --git a/toolchain/diagnostics/mocks.h b/toolchain/diagnostics/mocks.h index dd611e0a27e2..abecee576ec0 100644 --- a/toolchain/diagnostics/mocks.h +++ b/toolchain/diagnostics/mocks.h @@ -5,7 +5,8 @@ #ifndef TOOLCHAIN_DIAGNOSTICS_MOCKS_H_ #define TOOLCHAIN_DIAGNOSTICS_MOCKS_H_ -#include "gmock/gmock.h" +#include + #include "toolchain/diagnostics/diagnostic_emitter.h" namespace Carbon { diff --git a/toolchain/driver/BUILD b/toolchain/driver/BUILD index 6705938f3b4b..782700c095a5 100644 --- a/toolchain/driver/BUILD +++ b/toolchain/driver/BUILD @@ -27,10 +27,8 @@ cc_test( ":driver", "//toolchain/common:yaml_test_helpers", "//toolchain/lexer:tokenized_buffer_test_helpers", + "@com_google_googletest//:gtest_main", "@llvm-project//llvm:Support", - "@llvm-project//llvm:gmock", - "@llvm-project//llvm:gtest", - "@llvm-project//llvm:gtest_main", ], ) diff --git a/toolchain/driver/driver_test.cpp b/toolchain/driver/driver_test.cpp index 7202b41ba63a..dc546e5cc724 100644 --- a/toolchain/driver/driver_test.cpp +++ b/toolchain/driver/driver_test.cpp @@ -4,8 +4,9 @@ #include "toolchain/driver/driver.h" -#include "gmock/gmock.h" -#include "gtest/gtest.h" +#include +#include + #include "llvm/ADT/SmallString.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/SourceMgr.h" diff --git a/toolchain/lexer/BUILD b/toolchain/lexer/BUILD index 295aadb1aa33..1881ae66fb9d 100644 --- a/toolchain/lexer/BUILD +++ b/toolchain/lexer/BUILD @@ -19,9 +19,8 @@ cc_test( srcs = ["token_kind_test.cpp"], deps = [ ":token_kind", + "@com_google_googletest//:gtest_main", "@llvm-project//llvm:Support", - "@llvm-project//llvm:gtest", - "@llvm-project//llvm:gtest_main", ], ) @@ -58,11 +57,10 @@ cc_test( deps = [ ":numeric_literal", ":test_helpers", + "//common:ostream", "//toolchain/diagnostics:diagnostic_emitter", + "@com_google_googletest//:gtest_main", "@llvm-project//llvm:Support", - "@llvm-project//llvm:gmock", - "@llvm-project//llvm:gtest", - "@llvm-project//llvm:gtest_main", ], ) @@ -95,11 +93,10 @@ cc_test( deps = [ ":string_literal", ":test_helpers", + "//common:ostream", "//toolchain/diagnostics:diagnostic_emitter", + "@com_google_googletest//:gtest_main", "@llvm-project//llvm:Support", - "@llvm-project//llvm:gmock", - "@llvm-project//llvm:gtest", - "@llvm-project//llvm:gtest_main", ], ) @@ -124,6 +121,7 @@ cc_library( ":numeric_literal", ":string_literal", ":token_kind", + "//common:ostream", "//toolchain/diagnostics:diagnostic_emitter", "//toolchain/source:source_buffer", "@llvm-project//llvm:Support", @@ -136,8 +134,8 @@ cc_library( hdrs = ["tokenized_buffer_test_helpers.h"], deps = [ ":tokenized_buffer", + "@com_google_googletest//:gtest", "@llvm-project//llvm:Support", - "@llvm-project//llvm:gmock", ], ) @@ -150,10 +148,8 @@ cc_test( "//toolchain/common:yaml_test_helpers", "//toolchain/diagnostics:diagnostic_emitter", "//toolchain/diagnostics:mocks", + "@com_google_googletest//:gtest_main", "@llvm-project//llvm:Support", - "@llvm-project//llvm:gmock", - "@llvm-project//llvm:gtest", - "@llvm-project//llvm:gtest_main", ], ) diff --git a/toolchain/lexer/numeric_literal_test.cpp b/toolchain/lexer/numeric_literal_test.cpp index ee0fb48fe1e7..b69227b28939 100644 --- a/toolchain/lexer/numeric_literal_test.cpp +++ b/toolchain/lexer/numeric_literal_test.cpp @@ -4,12 +4,14 @@ #include "toolchain/lexer/numeric_literal.h" +#include +#include + #include #include #include -#include "gmock/gmock.h" -#include "gtest/gtest.h" +#include "common/ostream.h" #include "toolchain/diagnostics/diagnostic_emitter.h" #include "toolchain/lexer/test_helpers.h" diff --git a/toolchain/lexer/string_literal_test.cpp b/toolchain/lexer/string_literal_test.cpp index 897a7ce8bb04..34e097f5ef69 100644 --- a/toolchain/lexer/string_literal_test.cpp +++ b/toolchain/lexer/string_literal_test.cpp @@ -4,8 +4,10 @@ #include "toolchain/lexer/string_literal.h" -#include "gmock/gmock.h" -#include "gtest/gtest.h" +#include +#include + +#include "common/ostream.h" #include "toolchain/diagnostics/diagnostic_emitter.h" #include "toolchain/lexer/test_helpers.h" diff --git a/toolchain/lexer/token_kind_test.cpp b/toolchain/lexer/token_kind_test.cpp index 0ef6f276ea2f..4b8b1a1f6456 100644 --- a/toolchain/lexer/token_kind_test.cpp +++ b/toolchain/lexer/token_kind_test.cpp @@ -4,10 +4,11 @@ #include "toolchain/lexer/token_kind.h" +#include +#include + #include -#include "gmock/gmock.h" -#include "gtest/gtest.h" #include "llvm/ADT/StringRef.h" namespace Carbon { diff --git a/toolchain/lexer/tokenized_buffer.cpp b/toolchain/lexer/tokenized_buffer.cpp index 08db26a50b71..dee67fd23edb 100644 --- a/toolchain/lexer/tokenized_buffer.cpp +++ b/toolchain/lexer/tokenized_buffer.cpp @@ -863,6 +863,11 @@ auto TokenizedBuffer::AddToken(TokenInfo info) -> Token { return Token(static_cast(token_infos.size()) - 1); } +auto TokenizedBuffer::TokenIterator::Print(llvm::raw_ostream& output) const + -> void { + output << token.index; +} + auto TokenizedBuffer::SourceBufferLocationTranslator::GetLocation( const char* loc) -> Diagnostic::Location { assert(llvm::is_sorted(std::array{buffer_->source->Text().begin(), loc, diff --git a/toolchain/lexer/tokenized_buffer.h b/toolchain/lexer/tokenized_buffer.h index 02aef7bdec48..cca5edcbcfc6 100644 --- a/toolchain/lexer/tokenized_buffer.h +++ b/toolchain/lexer/tokenized_buffer.h @@ -8,6 +8,7 @@ #include #include +#include "common/ostream.h" #include "llvm/ADT/APInt.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/Optional.h" @@ -15,6 +16,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/ADT/iterator.h" #include "llvm/ADT/iterator_range.h" +#include "llvm/Support/raw_ostream.h" #include "toolchain/diagnostics/diagnostic_emitter.h" #include "toolchain/lexer/token_kind.h" #include "toolchain/source/source_buffer.h" @@ -195,6 +197,9 @@ class TokenizedBuffer { return *this; } + // Prints the raw token index. + auto Print(llvm::raw_ostream& output) const -> void; + private: friend class TokenizedBuffer; diff --git a/toolchain/lexer/tokenized_buffer_test.cpp b/toolchain/lexer/tokenized_buffer_test.cpp index 70aa242aca0d..2039becc1e87 100644 --- a/toolchain/lexer/tokenized_buffer_test.cpp +++ b/toolchain/lexer/tokenized_buffer_test.cpp @@ -4,10 +4,11 @@ #include "toolchain/lexer/tokenized_buffer.h" +#include +#include + #include -#include "gmock/gmock.h" -#include "gtest/gtest.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/None.h" #include "llvm/ADT/Sequence.h" diff --git a/toolchain/lexer/tokenized_buffer_test_helpers.h b/toolchain/lexer/tokenized_buffer_test_helpers.h index 86973972d789..ec8cc2c6362b 100644 --- a/toolchain/lexer/tokenized_buffer_test_helpers.h +++ b/toolchain/lexer/tokenized_buffer_test_helpers.h @@ -5,7 +5,8 @@ #ifndef TOOLCHAIN_LEXER_TOKENIZED_BUFFER_TEST_HELPERS_H_ #define TOOLCHAIN_LEXER_TOKENIZED_BUFFER_TEST_HELPERS_H_ -#include "gmock/gmock.h" +#include + #include "llvm/ADT/SmallString.h" #include "llvm/Support/Casting.h" #include "llvm/Support/YAMLParser.h" diff --git a/toolchain/parser/BUILD b/toolchain/parser/BUILD index 6a187d7b211b..938a72ec9c3c 100644 --- a/toolchain/parser/BUILD +++ b/toolchain/parser/BUILD @@ -19,9 +19,8 @@ cc_test( srcs = ["parse_node_kind_test.cpp"], deps = [ ":parse_node_kind", + "@com_google_googletest//:gtest_main", "@llvm-project//llvm:Support", - "@llvm-project//llvm:gtest", - "@llvm-project//llvm:gtest_main", ], ) @@ -51,8 +50,8 @@ cc_library( ":parse_node_kind", ":parse_tree", "//toolchain/lexer:tokenized_buffer", + "@com_google_googletest//:gtest", "@llvm-project//llvm:Support", - "@llvm-project//llvm:gmock", ], ) @@ -63,14 +62,13 @@ cc_test( ":parse_node_kind", ":parse_test_helpers", ":parse_tree", + "//common:ostream", "//toolchain/common:yaml_test_helpers", "//toolchain/diagnostics:diagnostic_emitter", "//toolchain/diagnostics:mocks", "//toolchain/lexer:tokenized_buffer", + "@com_google_googletest//:gtest_main", "@llvm-project//llvm:Support", - "@llvm-project//llvm:gmock", - "@llvm-project//llvm:gtest", - "@llvm-project//llvm:gtest_main", ], ) @@ -103,8 +101,6 @@ cc_test( deps = [ ":precedence", "//toolchain/lexer:token_kind", - "@llvm-project//llvm:gmock", - "@llvm-project//llvm:gtest", - "@llvm-project//llvm:gtest_main", + "@com_google_googletest//:gtest_main", ], ) diff --git a/toolchain/parser/parse_node_kind_test.cpp b/toolchain/parser/parse_node_kind_test.cpp index ae21820c1c70..74ac8a58785e 100644 --- a/toolchain/parser/parse_node_kind_test.cpp +++ b/toolchain/parser/parse_node_kind_test.cpp @@ -4,9 +4,10 @@ #include "toolchain/parser/parse_node_kind.h" +#include + #include -#include "gtest/gtest.h" #include "llvm/ADT/StringRef.h" namespace Carbon { diff --git a/toolchain/parser/parse_test_helpers.h b/toolchain/parser/parse_test_helpers.h index c7e3dfe86eca..ba9ec5c79939 100644 --- a/toolchain/parser/parse_test_helpers.h +++ b/toolchain/parser/parse_test_helpers.h @@ -5,12 +5,13 @@ #ifndef TOOLCHAIN_PARSER_PARSE_TEST_HELPERS_H_ #define TOOLCHAIN_PARSER_PARSE_TEST_HELPERS_H_ +#include + #include #include #include #include -#include "gmock/gmock.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" diff --git a/toolchain/parser/parse_tree.cpp b/toolchain/parser/parse_tree.cpp index 2314bbbb1876..16af4ca06eef 100644 --- a/toolchain/parser/parse_tree.cpp +++ b/toolchain/parser/parse_tree.cpp @@ -199,4 +199,18 @@ auto ParseTree::Verify() const -> bool { return true; } +auto ParseTree::Node::Print(llvm::raw_ostream& output) const -> void { + output << GetIndex(); +} + +auto ParseTree::PostorderIterator::Print(llvm::raw_ostream& output) const + -> void { + output << node.GetIndex(); +} + +auto ParseTree::SiblingIterator::Print(llvm::raw_ostream& output) const + -> void { + output << node.GetIndex(); +} + } // namespace Carbon diff --git a/toolchain/parser/parse_tree.h b/toolchain/parser/parse_tree.h index c6638e639f57..50d30a76fef8 100644 --- a/toolchain/parser/parse_tree.h +++ b/toolchain/parser/parse_tree.h @@ -7,10 +7,12 @@ #include +#include "common/ostream.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/iterator.h" #include "llvm/ADT/iterator_range.h" +#include "llvm/Support/raw_ostream.h" #include "toolchain/diagnostics/diagnostic_emitter.h" #include "toolchain/lexer/tokenized_buffer.h" #include "toolchain/parser/parse_node_kind.h" @@ -249,6 +251,9 @@ class ParseTree::Node { // FIXME: Maybe we can switch to stream operator overloads? [[nodiscard]] auto GetIndex() const -> int { return index; } + // Prints the node index. + auto Print(llvm::raw_ostream& output) const -> void; + private: friend ParseTree; friend Parser; @@ -298,6 +303,9 @@ class ParseTree::PostorderIterator return *this; } + // Prints the underlying node index. + auto Print(llvm::raw_ostream& output) const -> void; + private: friend class ParseTree; @@ -340,6 +348,9 @@ class ParseTree::SiblingIterator return *this; } + // Prints the underlying node index. + auto Print(llvm::raw_ostream& output) const -> void; + private: friend class ParseTree; diff --git a/toolchain/parser/parse_tree_test.cpp b/toolchain/parser/parse_tree_test.cpp index 9e1f66773bfd..d6562dac3de6 100644 --- a/toolchain/parser/parse_tree_test.cpp +++ b/toolchain/parser/parse_tree_test.cpp @@ -4,10 +4,12 @@ #include "toolchain/parser/parse_tree.h" +#include +#include + #include -#include "gmock/gmock.h" -#include "gtest/gtest.h" +#include "common/ostream.h" #include "llvm/ADT/Sequence.h" #include "llvm/Support/SourceMgr.h" #include "toolchain/common/yaml_test_helpers.h" diff --git a/toolchain/parser/precedence_test.cpp b/toolchain/parser/precedence_test.cpp index 252bf2794da6..896a35a6d928 100644 --- a/toolchain/parser/precedence_test.cpp +++ b/toolchain/parser/precedence_test.cpp @@ -4,8 +4,9 @@ #include "toolchain/parser/precedence.h" -#include "gmock/gmock.h" -#include "gtest/gtest.h" +#include +#include + #include "toolchain/lexer/token_kind.h" namespace Carbon { diff --git a/toolchain/source/BUILD b/toolchain/source/BUILD index 8e3eaaf79bec..aa30ad97668c 100644 --- a/toolchain/source/BUILD +++ b/toolchain/source/BUILD @@ -16,9 +16,7 @@ cc_test( srcs = ["source_buffer_test.cpp"], deps = [ ":source_buffer", + "@com_google_googletest//:gtest_main", "@llvm-project//llvm:Support", - "@llvm-project//llvm:gmock", - "@llvm-project//llvm:gtest", - "@llvm-project//llvm:gtest_main", ], ) diff --git a/toolchain/source/source_buffer_test.cpp b/toolchain/source/source_buffer_test.cpp index 8ef07e97ef97..e131c73fb49b 100644 --- a/toolchain/source/source_buffer_test.cpp +++ b/toolchain/source/source_buffer_test.cpp @@ -4,7 +4,8 @@ #include "toolchain/source/source_buffer.h" -#include "gtest/gtest.h" +#include + #include "llvm/ADT/SmallString.h" #include "llvm/ADT/Twine.h" #include "llvm/Support/FileSystem.h" @@ -49,7 +50,7 @@ TEST(SourceBufferTest, FileRep) { auto expected_buffer = SourceBuffer::CreateFromFile(test_file_path); ASSERT_TRUE(static_cast(expected_buffer)) - << "Error message: " << expected_buffer.takeError(); + << "Error message: " << toString(expected_buffer.takeError()); SourceBuffer& buffer = *expected_buffer;