Skip to content

Commit

Permalink
v0.0.0-0.nightly.2024.09.11
Browse files Browse the repository at this point in the history
  • Loading branch information
ogra committed Sep 11, 2024
2 parents df01c78 + c33c9a0 commit d56fe9f
Show file tree
Hide file tree
Showing 161 changed files with 4,138 additions and 1,151 deletions.
23 changes: 21 additions & 2 deletions core/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,28 @@
# Exceptions. See /LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# The prelude, and all of its dependencies.
load("manifest.bzl", "manifest")

# Raw prelude files.
filegroup(
name = "prelude",
name = "prelude_files",
srcs = ["prelude.carbon"] + glob(["prelude/**/*.carbon"]),
)

# A list of prelude inputs.
# This is consumed by //toolchain/install:install_paths.
manifest(
name = "prelude_manifest",
srcs = [":prelude_files"],
out = "prelude_manifest.txt",
)

# All files for the toolchain install.
filegroup(
name = "prelude",
srcs = [
":prelude_files",
":prelude_manifest.txt",
],
visibility = ["//visibility:public"],
)
25 changes: 25 additions & 0 deletions core/manifest.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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

"""Rule for producing a manifest for a filegroup."""

def _manifest(ctx):
dir = ctx.build_file_path.removesuffix("BUILD")
content = [f.path.removeprefix(dir) for f in ctx.files.srcs]
ctx.actions.write(ctx.outputs.out, "\n".join(content) + "\n")

return [
DefaultInfo(
files = depset(direct = [ctx.outputs.out]),
default_runfiles = ctx.runfiles(files = [ctx.outputs.out]),
),
]

manifest = rule(
implementation = _manifest,
attrs = {
"out": attr.output(mandatory = True),
"srcs": attr.label_list(mandatory = True),
},
)
31 changes: 26 additions & 5 deletions testing/file_test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Hides `{{` from jekyll's liquid parsing. Note endraw at the bottom.

A typical BUILD target will look like:

```
```starlark
load("rules.bzl", "file_test")

file_test(
Expand All @@ -35,7 +35,7 @@ file_test(

A typical implementation will look like:

```
```cpp
#include "my_library.h"

#include "testing/file_test/file_test_base.h"
Expand Down Expand Up @@ -80,6 +80,21 @@ The main test file and any split-files must have a `fail_` prefix if and only if
they have an associated error. An exception is that the main test file may omit
`fail_` when it contains split-files that have a `fail_` prefix.
## Content replacement
Some keywords can be inserted for content:
- ```
[[@TEST_NAME]]
```
Replaces with the test name, which is the filename with the extension and
any `fail_` or `todo_` prefixes removed. For split files, this is based on
the split filename.
The `[[@` string is reserved for future replacements, but `[[` is allowed in
content (comment markers don't allow `[[`).
## Comment markers
Settings in files are provided in comments, similar to `FileCheck` syntax.
Expand All @@ -106,7 +121,9 @@ Supported comment markers are:
check line refers to any line in the test, all STDOUT check lines are placed
at the end of the file instead of immediately after AUTOUPDATE.
- `// ARGS: <arguments>`
- ```
// ARGS: <arguments>
```
Provides a space-separated list of arguments, which will be passed to
RunWithFiles as test_args. These are intended for use by the command as
Expand All @@ -131,15 +148,19 @@ Supported comment markers are:
ARGS can be specified at most once. If not provided, the FileTestBase child
is responsible for providing default arguments.
- `// SET-CHECK-SUBSET`
- ```
// SET-CHECK-SUBSET
```
By default, all lines of output must have a CHECK match. Adding this as a
option sets it so that non-matching lines are ignored. All provided
CHECK:STDOUT: and CHECK:STDERR: lines must still have a match in output.
SET-CHECK-SUBSET can be specified at most once.
- `// --- <filename>`
- ```
// --- <filename>
```
By default, all file content is provided to the test as a single file in
test_files. Using this marker allows the file to be split into multiple
Expand Down
67 changes: 59 additions & 8 deletions testing/file_test/file_test_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ struct SplitState {
auto has_splits() const -> bool { return file_index > 0; }

auto add_content(llvm::StringRef line) -> void {
content.append(line);
content.append(line.str());
content.append("\n");
}

Expand All @@ -462,13 +462,63 @@ struct SplitState {
int file_index = 0;
};

// Replaces the content keywords.
//
// TEST_NAME is the only content keyword at present, but we do validate that
// other names are reserved.
static auto ReplaceContentKeywords(llvm::StringRef filename,
std::string* content) -> ErrorOr<Success> {
static constexpr llvm::StringLiteral Prefix = "[[@";

auto keyword_pos = content->find(Prefix);
// Return early if not finding anything.
if (keyword_pos == std::string::npos) {
return Success();
}

// Construct the test name by getting the base name without the extension,
// then removing any "fail_" or "todo_" prefixes.
llvm::StringRef test_name = filename;
if (auto last_slash = test_name.rfind("/");
last_slash != llvm::StringRef::npos) {
test_name = test_name.substr(last_slash + 1);
}
if (auto ext_dot = test_name.find("."); ext_dot != llvm::StringRef::npos) {
test_name = test_name.substr(0, ext_dot);
}
// Note this also handles `fail_todo_` and `todo_fail_`.
test_name.consume_front("todo_");
test_name.consume_front("fail_");
test_name.consume_front("todo_");

while (keyword_pos != std::string::npos) {
static constexpr llvm::StringLiteral TestName = "[[@TEST_NAME]]";
auto keyword = llvm::StringRef(*content).substr(keyword_pos);
if (keyword.starts_with(TestName)) {
content->replace(keyword_pos, TestName.size(), test_name);
keyword_pos += test_name.size();
} else if (keyword.starts_with("[[@LINE")) {
// Just move past the prefix to find the next one.
keyword_pos += Prefix.size();
} else {
return ErrorBuilder()
<< "Unexpected use of `[[@` at `" << keyword.substr(0, 5) << "`";
}
keyword_pos = content->find(Prefix, keyword_pos);
}
return Success();
}

// Adds a file. Used for both split and unsplit test files.
static auto AddTestFile(llvm::StringRef filename, std::string* content,
llvm::SmallVector<FileTestBase::TestFile>* test_files)
-> void {
-> ErrorOr<Success> {
CARBON_RETURN_IF_ERROR(ReplaceContentKeywords(filename, content));

test_files->push_back(
{.filename = filename.str(), .content = std::move(*content)});
content->clear();
return Success();
}

// Process file split ("---") lines when found. Returns true if the line is
Expand Down Expand Up @@ -500,7 +550,8 @@ static auto TryConsumeSplit(

// On a file split, add the previous file, then start a new one.
if (split->has_splits()) {
AddTestFile(split->filename, &split->content, test_files);
CARBON_RETURN_IF_ERROR(
AddTestFile(split->filename, &split->content, test_files));
} else {
split->content.clear();
if (split->found_code_pre_split) {
Expand Down Expand Up @@ -646,14 +697,14 @@ static auto TransformExpectation(int line_index, llvm::StringRef in)
// Once all content is processed, do any remaining split processing.
static auto FinishSplit(llvm::StringRef test_name, SplitState* split,
llvm::SmallVector<FileTestBase::TestFile>* test_files)
-> void {
-> ErrorOr<Success> {
if (split->has_splits()) {
AddTestFile(split->filename, &split->content, test_files);
return AddTestFile(split->filename, &split->content, test_files);
} else {
// If no file splitting happened, use the main file as the test file.
// There will always be a `/` unless tests are in the repo root.
AddTestFile(test_name.drop_front(test_name.rfind("/") + 1), &split->content,
test_files);
return AddTestFile(test_name.drop_front(test_name.rfind("/") + 1),
&split->content, test_files);
}
}

Expand Down Expand Up @@ -826,7 +877,7 @@ auto FileTestBase::ProcessTestFile(TestContext& context) -> ErrorOr<Success> {
}

context.has_splits = split.has_splits();
FinishSplit(test_name_, &split, &context.test_files);
CARBON_RETURN_IF_ERROR(FinishSplit(test_name_, &split, &context.test_files));

// Assume there is always a suffix `\n` in output.
if (!context.expected_stdout.empty()) {
Expand Down
Loading

0 comments on commit d56fe9f

Please sign in to comment.