forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use upstream GoogleTest and add related test utils. (carbon-language#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.
- Loading branch information
Showing
44 changed files
with
422 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
|
||
#include "common/check.h" | ||
|
||
#include "gtest/gtest.h" | ||
#include <gtest/gtest.h> | ||
|
||
namespace Carbon { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.