Skip to content

Commit

Permalink
Improve unified_diff tool (#90)
Browse files Browse the repository at this point in the history
* Added ability for unified_diff tool flag `--strip_file_header_prefix` to accept re2 expressions.
* Fixed various issues for bazelmod based builds.
  • Loading branch information
helly25 authored Feb 20, 2025
1 parent 2b8f3bb commit bae6628
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 7 deletions.
1 change: 1 addition & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# To debug bazel options, uncomment next line.
# common --announce_rc

common --consistent_labels
common --enable_bzlmod
common --noincompatible_enable_cc_toolchain_resolution

Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* Downgraded Clang from 19.1.7 to 19.1.6.
* Updated GitHub workflow to test both Bazel flavors.
* Fixed issue in with struct names generation when compiling with Clang in mode ASAN.
* Added ability for unified_diff tool flag `--strip_file_header_prefix` to accept re2 expressions.
* Fixed various issues for bazelmod based builds.

# 0.3.0

Expand Down
12 changes: 10 additions & 2 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ bazel_dep(name = "googletest", version = "1.16.0", repo_name = "com_google_googl
bazel_dep(name = "google_benchmark", version = "1.9.1", repo_name = "com_github_google_benchmark")
# In Bazelmod we do not need to specify the protobuf version yet.
# bazel_dep(name = "protobuf", version = "29.3", repo_name = "com_google_protobuf")

bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
git_override(
module_name = "hedron_compile_commands",
commit = "6dd21b47db481a70c61698742438230e2399b639",
remote = "https://github.com/hedronvision/bazel-compile-commands-extractor.git",
)

bazel_dep(name = "toolchains_llvm", version = "1.3.0")

git_override(
Expand All @@ -34,11 +42,11 @@ git_override(
commit = "e831f94a8b7f3a39391f5822adcab8e4d443c03b", # Add more tools by default (#463)
)

llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm")
llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm", dev_dependency = True)

llvm.toolchain(
name = "llvm_toolchain_llvm",
llvm_version = "19.1.6",
)
use_repo(llvm, "llvm_toolchain_llvm")
register_toolchains("@llvm_toolchain_llvm//:all")
register_toolchains("@llvm_toolchain_llvm//:all", dev_dependency = True)
14 changes: 14 additions & 0 deletions REPO.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# SPDX-FileCopyrightText: Copyright (c) The helly25/mbo authors (helly25.com)
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
2 changes: 1 addition & 1 deletion mbo/diff/tests/diff_test_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def diff_test_test(
--ignore_matching_lines={ignore_matching_lines} \\
--ignore_space_change={ignore_space_change} \\
--strip_comments={strip_comments} \\
--strip_file_header_prefix="external/com_helly25_mbo/" \\
--strip_file_header_prefix="external/(com_)?helly25_mbo[^/]*/" \\
--strip_parsed_comments={strip_parsed_comments} \\
|| true
""".format(
Expand Down
8 changes: 7 additions & 1 deletion mbo/diff/unified_diff.cc
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,13 @@ class Chunk {

private:
static std::string FileHeader(const file::Artefact& info, const UnifiedDiff::Options& options) {
std::string_view name = absl::StripPrefix(info.name, options.strip_file_header_prefix);
std::string_view name;
if (options.strip_file_header_prefix.find_first_of(".*?()[]|") == std::string::npos) {
name = absl::StripPrefix(info.name, options.strip_file_header_prefix);
} else {
name = info.name;
RE2::Consume(&name, options.strip_file_header_prefix);
}
return absl::StrCat(info.name.empty() ? "-" : name, " ", absl::FormatTime(options.time_format, info.time, info.tz));
}

Expand Down
4 changes: 3 additions & 1 deletion mbo/diff/unified_diff_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ ABSL_FLAG(
std::string,
strip_file_header_prefix,
"",
"If this is a prefix to a filename in the header, then remove from header.");
"\
If this is a prefix to a filename in the header, then remove from filename in header. This can be \
a regular expression (https://github.com/google/re2/wiki/Syntax).");
ABSL_FLAG(
bool,
strip_parsed_comments,
Expand Down
5 changes: 3 additions & 2 deletions mbo/mope/mope.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,12 @@ def _gen_name_base_no_ext(file):

def _build_relative(ctx, file):
path = "/".join(ctx.build_file_path.split("/")[:-1])
return file.short_path.removeprefix("../com_helly25_mbo/").removeprefix(path).removeprefix("/")
res = path + "/" + file.basename
return res

def _mope_rule_impl(ctx):
srcs = {_template_base_no_ext(src): src for src in ctx.files.srcs}
outs = {_gen_name_base_no_ext(out): ctx.actions.declare_file(_build_relative(ctx, out)) for out in ctx.outputs.outs}
outs = {_gen_name_base_no_ext(out): out for out in ctx.outputs.outs}
inis = {_base_no_ext(ini): ini.path for ini in ctx.files.data if ini.extension == "ini"}
if srcs.keys() != outs.keys():
fail("Files in `srcs` and `outs` do not match on basenames without extensions.")
Expand Down

0 comments on commit bae6628

Please sign in to comment.