Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions maldoca/base/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ cc_test(
],
)

cc_library(
name = "path",
srcs = ["path.cc"],
hdrs = ["path.h"],
deps = [
"@abseil-cpp//absl/base:core_headers",
"@abseil-cpp//absl/strings",
],
)

cc_library(
name = "filesystem",
srcs = ["filesystem.cc"],
Expand All @@ -62,6 +72,18 @@ cc_library(
],
)

cc_library(
name = "get_runfiles_dir",
srcs = ["get_runfiles_dir.cc"],
hdrs = ["get_runfiles_dir.h"],
deps = [
":path",
"@abseil-cpp//absl/log:check",
"@abseil-cpp//absl/strings:string_view",
"@rules_cc//cc/runfiles",
],
)

cc_library(
name = "source_location",
hdrs = ["source_location.h"],
Expand Down
35 changes: 35 additions & 0 deletions maldoca/base/get_runfiles_dir.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2025 Google LLC
//
// 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
//
// https://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.

#include "maldoca/base/get_runfiles_dir.h"

#include <string>

#include "rules_cc/cc/runfiles/runfiles.h"
#include "absl/log/check.h"
#include "absl/strings/string_view.h"
#include "maldoca/base/path.h"

namespace maldoca {

std::string GetDataDependencyFilepath(absl::string_view path) {
using ::rules_cc::cc::runfiles::Runfiles;
std::string error;
std::unique_ptr<Runfiles> runfiles(
Runfiles::CreateForTest(BAZEL_CURRENT_REPOSITORY, &error));
CHECK(runfiles != nullptr) << error;
return runfiles->Rlocation(JoinPath("com_google_maldoca", path));
}

} // namespace maldoca
28 changes: 28 additions & 0 deletions maldoca/base/get_runfiles_dir.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2025 Google LLC
//
// 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
//
// https://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.

#ifndef MALDOCA_BASE_GET_RUNFILES_DIR_H_
#define MALDOCA_BASE_GET_RUNFILES_DIR_H_

#include <string>

#include "absl/strings/string_view.h"

namespace maldoca {

std::string GetDataDependencyFilepath(absl::string_view path);

} // namespace maldoca

#endif // MALDOCA_BASE_GET_RUNFILES_DIR_H_
124 changes: 124 additions & 0 deletions maldoca/base/path.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright 2025 Google LLC
//
// 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
//
// https://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.

#include "maldoca/base/path.h"

#include <string.h>

#include <initializer_list>
#include <string>
#include <utility>

#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"

namespace maldoca {

// 40% of the time in JoinPath() is from calls with 2 arguments, so we
// specialize that case.
std::string JoinPath(absl::string_view path1, absl::string_view path2) {
if (path1.empty()) return std::string(path2);
if (path2.empty()) return std::string(path1);
if (path1.back() == '/') {
if (path2.front() == '/')
return absl::StrCat(path1, absl::ClippedSubstr(path2, 1));
} else {
if (path2.front() != '/') return absl::StrCat(path1, "/", path2);
}
return absl::StrCat(path1, path2);
}

namespace internal {

std::string JoinPathImpl(bool honor_abs,
std::initializer_list<absl::string_view> paths) {
std::string result;

if (paths.size() != 0) {
// This size calculation is worst-case: it assumes one extra "/" for every
// path other than the first.
size_t total_size = paths.size() - 1;
for (const absl::string_view path : paths) total_size += path.size();
result.resize(total_size);

auto begin = result.begin();
auto out = begin;
bool trailing_slash = false;
for (absl::string_view path : paths) {
if (path.empty()) continue;
if (path.front() == '/') {
if (honor_abs) {
out = begin; // wipe out whatever we've built up so far.
} else if (trailing_slash) {
path.remove_prefix(1);
}
} else {
if (!trailing_slash && out != begin) *out++ = '/';
}
const size_t this_size = path.size();
memcpy(&*out, path.data(), this_size);
out += this_size;
trailing_slash = out[-1] == '/';
}
result.erase(out - begin);
}
return result;
}

std::pair<absl::string_view, absl::string_view> SplitBasename(
absl::string_view path) {
path = Basename(path);

size_t pos = path.find_last_of('.');
if (pos == absl::string_view::npos)
return std::make_pair(path, absl::ClippedSubstr(path, path.size(), 0));
return std::make_pair(path.substr(0, pos),
absl::ClippedSubstr(path, pos + 1));
}

} // namespace internal

bool IsAbsolutePath(absl::string_view path) {
return !path.empty() && path[0] == '/';
}

std::pair<absl::string_view, absl::string_view> SplitPath(
absl::string_view path) {
absl::string_view::size_type pos = path.find_last_of('/');

// Handle the case with no '/' in 'path'.
if (pos == absl::string_view::npos)
return std::make_pair(path.substr(0, 0), path);

// Handle the case with a single leading '/' in 'path'.
if (pos == 0)
return std::make_pair(path.substr(0, 1), absl::ClippedSubstr(path, 1));

return std::make_pair(path.substr(0, pos),
absl::ClippedSubstr(path, pos + 1));
}

absl::string_view Basename(absl::string_view path) {
return SplitPath(path).second;
}

absl::string_view Stem(absl::string_view path) {
return internal::SplitBasename(path).first;
}

absl::string_view Extension(absl::string_view path) {
return internal::SplitBasename(path).second;
}

} // namespace maldoca
106 changes: 106 additions & 0 deletions maldoca/base/path.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright 2025 Google LLC
//
// 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
//
// https://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.

#ifndef MALDOCA_BASE_PATH_H_
#define MALDOCA_BASE_PATH_H_

#include <initializer_list>
#include <string>
#include <utility>

#include "absl/base/attributes.h"
#include "absl/strings/string_view.h"

// A set of file pathname manipulation routines.
// Calls to each of the following functions assume their input is
// well-formed (for some currently nebulous definition of the word).
//
// This collection is largely modeled on Python's os.path module.
// Filenames are restricted to ASCII characters.

namespace maldoca {

namespace internal {
// Not part of the public API.
// Given a collection of file paths, append them all together,
// ensuring that the proper path separators are inserted between them.
std::string JoinPathImpl(bool honor_abs,
std::initializer_list<absl::string_view> paths);
// Return the parts of the basename of path, split on the final ".".
// If there is no "." in the basename or "." is the final character in the
// basename, the second value will be empty.
std::pair<absl::string_view, absl::string_view> SplitBasename(
absl::string_view path);
} // namespace internal

// Join multiple paths together.
// JoinPath unconditionally joins all paths together.
// For example:
// Arguments | JoinPath | JoinPathRespectAbsolute
// ---------------------------+---------------------+-----------------------
// '/foo', 'bar' | /foo/bar | /foo/bar
// '/foo/', 'bar' | /foo/bar | /foo/bar
// '/foo', '/bar' | /foo/bar | /bar
// '/foo', '/bar', '/baz' | /foo/bar/baz | /baz
//
// All paths will be treated as relative paths, regardless of whether or not
// they start with a leading '/'. That is, all paths will be concatenated
// together, with the appropriate path separator inserted in between.
// Arguments must be convertible to absl::string_view.
//
// Usage:
// string path = maldoca::path::JoinPath("/cns", dirname, filename);
// string path = maldoca::path::JoinPath(getenv("TEST_SRCDIR"), filename);
//
// 0, 1, 2-path specializations exist to optimize common cases.
inline std::string JoinPath() { return std::string(); }
inline std::string JoinPath(absl::string_view path) {
return std::string(path.data(), path.size());
}
std::string JoinPath(absl::string_view path1, absl::string_view path2);
template <typename... T>
inline std::string JoinPath(absl::string_view path1, absl::string_view path2,
absl::string_view path3, const T&... args) {
return internal::JoinPathImpl(false, {path1, path2, path3, args...});
}

// Return true if path is absolute.
bool IsAbsolutePath(absl::string_view path);

// Return the parts of the path, split on the final "/". If there is no
// "/" in the path, the first part of the output is empty and the second
// is the input. If the only "/" in the path is the first character, it is
// the first part of the output.
std::pair<absl::string_view, absl::string_view> SplitPath(
absl::string_view path);

// Returns the part of the path after the final "/". If there is no
// "/" in the path, the result is the same as the input.
// Note that this function's behavior differs from the Unix basename
// command if path ends with "/". For such paths, this function returns the
// empty string.
absl::string_view Basename(absl::string_view path);

// Returns the part of the basename of path prior to the final ".". If
// there is no "." in the basename, this is equivalent to file::Basename(path).
absl::string_view Stem(absl::string_view path ABSL_ATTRIBUTE_LIFETIME_BOUND);

// Returns the part of the basename of path after the final ".". If
// there is no "." in the basename, the result is empty.
absl::string_view Extension(
absl::string_view path ABSL_ATTRIBUTE_LIFETIME_BOUND);

} // namespace maldoca

#endif // MALDOCA_BASE_PATH_H_
1 change: 1 addition & 0 deletions maldoca/js/ast/transforms/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ cc_library(
deps = [
"//maldoca/js/ast",
"//maldoca/js/ast/transforms/erase_comments:pass",
"//maldoca/js/ast/transforms/extract_prelude:pass",
"//maldoca/js/babel:babel_cc_proto",
"//maldoca/js/driver:driver_cc_proto",
"@abseil-cpp//absl/status",
Expand Down
56 changes: 56 additions & 0 deletions maldoca/js/ast/transforms/extract_prelude/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2024 Google LLC
#
# 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
#
# https://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.

load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_cc//cc:cc_test.bzl", "cc_test")

licenses(["notice"])

package(
default_applicable_licenses = ["//:license"],
default_visibility = [
"//maldoca/js:__subpackages__",
],
)

cc_library(
name = "pass",
srcs = ["pass.cc"],
hdrs = ["pass.h"],
deps = [
"//maldoca/js/ast",
"//maldoca/js/babel:babel_cc_proto",
"//maldoca/js/driver:driver_cc_proto",
"@abseil-cpp//absl/log",
"@abseil-cpp//absl/log:check",
"@abseil-cpp//absl/strings",
"@abseil-cpp//absl/strings:string_view",
"@abseil-cpp//absl/types:variant",
],
)

cc_test(
name = "pass_test",
srcs = ["pass_test.cc"],
deps = [
":pass",
"//maldoca/base/testing:status_matchers",
"//maldoca/js/babel:babel_cc_proto",
"//maldoca/js/driver",
"//maldoca/js/driver:conversion",
"//maldoca/js/quickjs_babel",
"@abseil-cpp//absl/time",
"@googletest//:gtest_main",
],
)
Loading