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
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ add_library(bonsai_library STATIC)
add_library(bonsai::library ALIAS bonsai_library)

target_compile_features(bonsai_library PUBLIC cxx_std_20)

# include/Error.h trims this prefix off __FILE__, so that the "[internal]
# Error: <file>:<line>" lines checked in under tests/bonsai/error/*.expect do
# not depend on the name of the directory the repository was cloned into.
# PUBLIC because bonsai_compiler and bonsai_test_runner include Error.h too;
# BUILD_INTERFACE keeps a local absolute path out of anything installed.
target_compile_definitions(
bonsai_library
PUBLIC
"$<BUILD_INTERFACE:BONSAI_SOURCE_DIR=\"${PROJECT_SOURCE_DIR}\">"
)

target_link_libraries(bonsai_library PUBLIC ModernLLVM::LLVM)

target_sources(
Expand Down
45 changes: 33 additions & 12 deletions include/Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,50 @@
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>

namespace bonsai {

class Error final : public std::runtime_error {
using std::runtime_error::runtime_error;
};

namespace detail {

// __FILE__ is an absolute path into whatever directory the repository was
// cloned into. Trim it back to a repository-relative path, so that the
// "[internal] Error: <file>:<line>" lines that tests/bonsai/error/*.expect
// compares byte-for-byte do not depend on where the checkout lives.
inline std::string source_relative_path(const char *file) {
std::string f(file);

#ifdef BONSAI_SOURCE_DIR
// The repository root, injected by the top-level CMakeLists.txt.
constexpr std::string_view root = BONSAI_SOURCE_DIR;
if (!root.empty() && f.size() > root.size() &&
f.compare(0, root.size(), root) == 0 && f[root.size()] == '/') {
return f.substr(root.size() + 1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: invalid case style for constexpr variable 'root' [readability-identifier-naming]

Suggested change
return f.substr(root.size() + 1);
constexpr std::string_view ROOT = BONSAI_SOURCE_DIR;
if (!ROOT.empty() && f.size() > ROOT.size() &&
f.compare(0, ROOT.size(), ROOT) == 0 && f[ROOT.size()] == '/') {
return f.substr(ROOT.size() + 1);

}
#endif

// For a translation unit built outside our CMake, or one whose __FILE__
// has been rewritten (-ffile-prefix-map), fall back to the last top-level
// source directory. Printing the whole path is the honest last resort.
for (const std::string_view dir : {"/src/", "/include/", "/tests/"}) {
if (const size_t pos = f.rfind(dir); pos != std::string::npos) {
return f.substr(pos + 1);
}
}
return f;
}

} // namespace detail

class ErrorReport {
public:
ErrorReport(const char *cond_str, const char *file, size_t line) {
// Print the file path proceeding the root directory (inclusive).
constexpr std::string_view ROOT_DIRECTORY = "bonsai";
std::string f(file);

// TODO(cgyurgyik): Fix this hack.
// Finds the last occurrence of `bonsai` to conform with Github Actions,
// where both the WORKSPACE and the REPOSITORY are named `bonsai`.
if (size_t pos = f.rfind(ROOT_DIRECTORY); pos != std::string::npos) {
f = f.substr(pos + ROOT_DIRECTORY.length() + 1);
}

stream << "[internal] Error: ";
stream << f << ":" << line << "\n";
stream << detail::source_relative_path(file) << ":" << line << "\n";
if (cond_str) {
stream << "\n--> " << cond_str << "\n";
}
Expand Down
11 changes: 7 additions & 4 deletions src/Lower/Scans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ build_scan_func(const std::vector<TypedVar> &args, const std::string &func_name,
std::optional<AggOp::OpType> op;
Expr map_func;

ScansToCalls(std::shared_ptr<Function> func,
std::optional<AggOp::OpType> op, Expr map_func)
: func(std::move(func)), op(std::move(op)),
map_func(std::move(map_func)) {
// The parameters deliberately do not share the members' names. The
// body reads `func` after the mem-initializer has moved from it, so a
// parameter named `func` would shadow the member and be null there.
ScansToCalls(std::shared_ptr<Function> scan_func,
std::optional<AggOp::OpType> agg_op, Expr map)
: func(std::move(scan_func)), op(std::move(agg_op)),
map_func(std::move(map)) {
write_expr =
Var::make(func->args.back().type, func->args.back().name);
write_loc =
Expand Down
8 changes: 6 additions & 2 deletions src/Lower/TypeInference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,12 @@ infer_types(const std::shared_ptr<ir::Function> &fnotypes,
coerce_return_types(std::move(ftypes->body), ftypes->ret_type);
ftypes->body = infer_build_types(std::move(ftypes->body), ftypes->ret_type);

internal_assert(ftypes->ret_type.is<ir::Void_t>() ||
always_returns(ftypes->body))
// Keep the internal_assert invocation on one line: GCC reports __LINE__ as
// the line the macro name is on and Clang as the line the closing paren is
// on, and tests/bonsai/error/no-return.expect records that number.
const bool returns_on_all_paths =
ftypes->ret_type.is<ir::Void_t>() || always_returns(ftypes->body);
internal_assert(returns_on_all_paths)
<< "Function: " << ftypes->name
<< " does not return in all code paths.";

Expand Down
4 changes: 2 additions & 2 deletions tests/bonsai/error/no-return.expect
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---CODE---
1
---STDERR---
[internal] Error: src/Lower/TypeInference.cpp:510
[internal] Error: src/Lower/TypeInference.cpp:514

--> ftypes->ret_type.is<ir::Void_t>() || always_returns(ftypes->body)
--> returns_on_all_paths
Function: foo does not return in all code paths.
Loading