srctree

Robin Linden parent 0f043143 a8ad900d
meta/clang-tidy: Enable bugprone-unchecked-optional-access

.clang-tidy added: 83, removed: 45, total 38
@@ -7,9 +7,6 @@
#
# -bugprone-narrowing-conversions: Very noisy for not much gain.
#
# -bugprone-unchecked-optional-access: Makes clang-tidy hang during CI.
# https://github.com/llvm/llvm-project/issues/59492
#
# -cert-dcl21-cpp: Deprecated, will be removed in clang-tidy 19.
# https://releases.llvm.org/17.0.1/tools/clang/tools/extra/docs/clang-tidy/checks/cert/dcl21-cpp.html
#
@@ -60,7 +57,6 @@ Checks: >
-bugprone-easily-swappable-parameters,
-bugprone-exception-escape,
-bugprone-narrowing-conversions,
-bugprone-unchecked-optional-access,
-cert-dcl21-cpp,
-google-build-using-namespace,
-misc-const-correctness,
 
azm/amd64/assembler.h added: 83, removed: 45, total 38
@@ -117,7 +117,9 @@ public:
}
 
void mov(Reg32 dst, Imm32 imm32) {
emit(0xb8 + register_index(dst).value());
auto idx = register_index(dst);
assert(idx.has_value());
emit(0xb8 + idx.value());
emit(imm32);
}
 
 
browser/gui/app.cpp added: 83, removed: 45, total 38
@@ -599,11 +599,16 @@ void App::on_layout_updated() {
}
 
layout::LayoutBox const *App::get_hovered_node(geom::Position document_position) const {
if (!maybe_page_.has_value() || !page().layout.has_value()) {
if (!maybe_page_.has_value()) {
return nullptr;
}
 
return layout::box_at_position(*page().layout, document_position);
auto const &maybe_layout = page().layout;
if (!maybe_layout.has_value()) {
return nullptr;
}
 
return layout::box_at_position(*maybe_layout, document_position);
}
 
geom::Position App::to_document_position(geom::Position window_position) const {
@@ -617,11 +622,16 @@ void App::reset_scroll() {
}
 
void App::scroll(int pixels) {
if (!maybe_page_ || !page().layout.has_value()) {
if (!maybe_page_.has_value()) {
return;
}
 
auto const &layout = *page().layout;
auto const &maybe_layout = page().layout;
if (!maybe_layout.has_value()) {
return;
}
 
auto const &layout = *maybe_layout;
// Don't allow scrolling if the entire page fits on the screen.
if (static_cast<int>(window_.getSize().y) > layout.dimensions.margin_box().height) {
return;
@@ -714,15 +724,16 @@ void App::run_layout_widget() const {
void App::render_layout() {
assert(maybe_page_);
 
if (page().layout == std::nullopt) {
auto const &layout = page().layout;
if (layout == std::nullopt) {
return;
}
 
if (render_debug_) {
render::debug::render_layout_depth(*canvas_, *page().layout);
render::debug::render_layout_depth(*canvas_, *layout);
} else {
render::render_layout(*canvas_,
*page().layout,
*layout,
culling_enabled_ ? std::optional{geom::Rect{0,
-scroll_offset_y_,
static_cast<int>(window_.getSize().x),
 
browser/tui/tui.cpp added: 83, removed: 45, total 38
@@ -64,11 +64,12 @@ int main(int argc, char **argv) {
std::cout << dom::to_string(page->dom);
spdlog::info("Building TUI");
 
if (!page->layout.has_value()) {
auto const &layout = page->layout;
if (!layout.has_value()) {
spdlog::error("Unable to create a layout of {}", uri->uri);
return 1;
}
 
std::cout << tui::render(*page->layout) << '\n';
std::cout << tui::render(*layout) << '\n';
spdlog::info("Done");
}
 
etest/etest.cpp added: 83, removed: 45, total 38
@@ -7,6 +7,7 @@
#include "etest/cxx_compat.h"
#include "etest/etest2.h"
 
#include <cassert>
#include <functional>
#include <optional>
#include <string>
@@ -42,10 +43,12 @@ void disabled_test(std::string name, std::function<void()> body) noexcept {
}
 
void expect(bool b, std::optional<std::string_view> log_message, etest::source_location const &loc) noexcept {
assert(current_actions.has_value());
current_actions.value().get().expect(b, std::move(log_message), loc);
}
 
void require(bool b, std::optional<std::string_view> log_message, etest::source_location const &loc) {
assert(current_actions.has_value());
current_actions.value().get().require(b, std::move(log_message), loc);
}
 
 
gfx/opengl_canvas.cpp added: 83, removed: 45, total 38
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021-2023 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021-2024 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
@@ -13,7 +13,9 @@
#include <glad/gl.h>
 
#include <array>
#include <cassert>
#include <string_view>
#include <utility>
 
namespace gfx {
namespace {
@@ -24,7 +26,12 @@ std::string_view const vertex_shader{reinterpret_cast<char const *>(gfx_basic_sh
std::string_view const fragment_shader{reinterpret_cast<char const *>(gfx_rect_shader_frag), gfx_rect_shader_frag_len};
} // namespace
 
OpenGLCanvas::OpenGLCanvas() : border_shader_{OpenGLShader::create(vertex_shader, fragment_shader).value()} {
OpenGLCanvas::OpenGLCanvas()
: border_shader_{[] {
auto shader = OpenGLShader::create(vertex_shader, fragment_shader);
assert(shader.has_value());
return std::move(shader).value();
}()} {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
 
js/ast_executor.h added: 83, removed: 45, total 38
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2022-2023 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2022-2024 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
@@ -116,7 +116,9 @@ public:
for (auto const &statement : v.body) {
execute(statement);
if (returning) {
return *std::exchange(returning, std::nullopt);
auto ret = *std::move(returning);
returning = std::nullopt;
return ret;
}
}
 
 
layout/layout.cpp added: 83, removed: 45, total 38
@@ -302,12 +302,14 @@ void Layouter::layout_anonymous_block(LayoutBox &box, geom::Rect const &bounds)
 
// TODO(robinlinden): Handle cases where the text isn't a direct child of the anonymous block.
if (last_child_end + child->dimensions.margin_box().width > bounds.width) {
auto text = child->text();
if (text) {
auto maybe_text = child->text();
if (maybe_text.has_value()) {
std::string_view text = *maybe_text;
 
std::size_t best_split_point = std::string_view::npos;
for (auto split_point = text->find(' '); split_point != std::string_view::npos;
split_point = text->find(' ', split_point + 1)) {
if (last_child_end + font->measure(text->substr(0, split_point), font_size, weight).width
for (auto split_point = text.find(' '); split_point != std::string_view::npos;
split_point = text.find(' ', split_point + 1)) {
if (last_child_end + font->measure(text.substr(0, split_point), font_size, weight).width
> bounds.width) {
break;
}
@@ -317,18 +319,18 @@ void Layouter::layout_anonymous_block(LayoutBox &box, geom::Rect const &bounds)
 
if (best_split_point != std::string_view::npos) {
child->dimensions.content.width =
font->measure(text->substr(0, best_split_point), font_size, weight).width;
font->measure(text.substr(0, best_split_point), font_size, weight).width;
auto bonus_child = *child;
bonus_child.layout_text = std::string{text->substr(best_split_point + 1)};
bonus_child.layout_text = std::string{text.substr(best_split_point + 1)};
box.children.insert(box.children.begin() + i + 1, std::move(bonus_child));
current_line += 1;
last_child_end = 0;
 
// Adding a child may have had to relocate the container content.
child = &box.children[i];
child->layout_text = std::string{text->substr(0, best_split_point)};
child->layout_text = std::string{text.substr(0, best_split_point)};
} else {
child->dimensions.content.width = font->measure(*text, font_size, weight).width;
child->dimensions.content.width = font->measure(text, font_size, weight).width;
last_child_end += child->dimensions.margin_box().width;
}
}
 
layout/layout_box.cpp added: 83, removed: 45, total 38
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021-2023 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021-2024 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2022 Mikael Larsson <c.mikael.larsson@gmail.com>
//
// SPDX-License-Identifier: BSD-2-Clause
@@ -66,7 +66,9 @@ void print_box(LayoutBox const &box, std::ostream &os, std::uint8_t depth = 0) {
if (auto const *element = std::get_if<dom::Element>(&box.node->node)) {
os << element->name << '\n';
} else {
os << box.text().value() << '\n';
auto text = box.text();
assert(text.has_value());
os << text.value() << '\n';
}
 
for (std::uint8_t i = 0; i < depth; ++i) {
 
protocol/http_test.cpp added: 83, removed: 45, total 38
@@ -9,6 +9,7 @@
#include "protocol/response.h"
#include "uri/uri.h"
 
#include <cassert>
#include <cstddef>
#include <optional>
#include <string>
@@ -63,7 +64,9 @@ struct FakeSocket {
};
 
uri::Uri create_uri(std::string url = "http://example.com") {
return uri::Uri::parse(std::move(url)).value();
auto parsed = uri::Uri::parse(std::move(url));
assert(parsed.has_value());
return std::move(parsed).value();
}
 
FakeSocket create_chunked_socket(std::string const &body) {
 
uri/uri.cpp added: 83, removed: 45, total 38
@@ -9,6 +9,7 @@
 
#include <fmt/core.h>
 
#include <cassert>
#include <functional>
#include <optional>
#include <regex>
@@ -35,18 +36,26 @@ void normalize(Uri &uri) {
void complete_from_base_if_needed(Uri &uri, Uri const &base) {
if (uri.scheme.empty() && uri.authority.host.empty() && uri.path.starts_with('/')) {
// Origin-relative.
uri = Uri::parse(fmt::format("{}://{}{}", base.scheme, base.authority.host, uri.uri)).value();
auto new_uri = Uri::parse(fmt::format("{}://{}{}", base.scheme, base.authority.host, uri.uri));
assert(new_uri.has_value());
uri = *std::move(new_uri);
} else if (uri.scheme.empty() && uri.authority.host.empty() && !uri.path.empty()) {
// https://url.spec.whatwg.org/#path-relative-url-string
if (base.path == "/") {
uri = Uri::parse(fmt::format("{}/{}", base.uri, uri.uri)).value();
auto new_uri = Uri::parse(fmt::format("{}/{}", base.uri, uri.uri));
assert(new_uri);
uri = *std::move(new_uri);
} else {
auto end_of_last_path_segment = base.uri.find_last_of('/');
uri = Uri::parse(fmt::format("{}/{}", base.uri.substr(0, end_of_last_path_segment), uri.uri)).value();
auto new_uri = Uri::parse(fmt::format("{}/{}", base.uri.substr(0, end_of_last_path_segment), uri.uri));
assert(new_uri);
uri = *std::move(new_uri);
}
} else if (uri.scheme.empty() && !uri.authority.host.empty() && uri.uri.starts_with("//")) {
// Scheme-relative.
uri = Uri::parse(fmt::format("{}:{}", base.scheme, uri.uri)).value();
auto new_uri = Uri::parse(fmt::format("{}:{}", base.scheme, uri.uri));
assert(new_uri);
uri = *std::move(new_uri);
}
}