Skip to content

Commit

Permalink
improvement: replace (un)likely macro with C++20 attribute (#1516)
Browse files Browse the repository at this point in the history
* improvement: replace (un)likely macro with C++20 attribute
  • Loading branch information
SamantaTarun committed Jun 17, 2023
1 parent c3d7394 commit 0ce207f
Show file tree
Hide file tree
Showing 33 changed files with 100 additions and 105 deletions.
1 change: 1 addition & 0 deletions copying.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ _the openage authors_ are:
| Talha Aamir | sarcxd | sarcxd 脿 gmail dawt com |
| Matthias Geiger | CountOmega | matthias dawt geiger1024 脿 outlook dawt com |
| Yuvraj Tetarwal | YuviTz1 | yuvi56789 脿 gmail dawt com |
| Tarun Samanta | TS | tarunsamanta77@gmail.com |

If you're a first-time committer, add yourself to the above list. This is not
just for legal reasons, but also to keep an overview of all those nicknames.
Expand Down
2 changes: 1 addition & 1 deletion libopenage/assets/assetmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ void AssetManager::check_updates() {

std::shared_ptr<Texture> AssetManager::get_missing_tex() {
// if not loaded, fetch the "missing" texture (big red X).
if (unlikely(this->missing_tex.get() == nullptr)) {
if (this->missing_tex.get() == nullptr) [[unlikely]] {
this->missing_tex = std::make_shared<Texture>(
this->asset_path["test"]["textures"]["missing.png"],
false);
Expand Down
2 changes: 1 addition & 1 deletion libopenage/assets/legacy_assetmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ void LegacyAssetManager::check_updates() {

std::shared_ptr<Texture> LegacyAssetManager::get_missing_tex() {
// if not loaded, fetch the "missing" texture (big red X).
if (unlikely(this->missing_tex.get() == nullptr)) {
if (this->missing_tex.get() == nullptr) [[unlikely]] {
this->missing_tex = std::make_shared<Texture>(
this->asset_path["test"]["textures"]["missing.png"],
false);
Expand Down
4 changes: 2 additions & 2 deletions libopenage/audio/opus_loading.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014-2019 the openage authors. See copying.md for legal info.
// Copyright 2014-2023 the openage authors. See copying.md for legal info.

#include "opus_loading.h"

Expand Down Expand Up @@ -30,7 +30,7 @@ static op_seek_func opus_seeker = [](void *stream,
int whence) -> int {

auto *file = reinterpret_cast<util::File *>(stream);
if (unlikely(not file->seekable())) {
if (not file->seekable()) [[unlikely]] {
return -1;
}

Expand Down
4 changes: 2 additions & 2 deletions libopenage/audio/resource.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014-2017 the openage authors. See copying.md for legal info.
// Copyright 2014-2023 the openage authors. See copying.md for legal info.

#include "resource.h"

Expand Down Expand Up @@ -32,7 +32,7 @@ int Resource::get_id() const {
std::shared_ptr<Resource> Resource::create_resource(AudioManager *manager,
const resource_def &def) {

if (unlikely(not def.location.is_file())) {
if (not def.location.is_file()) [[unlikely]] {
throw Error{ERR << "sound file does not exist: " << def.location};
}

Expand Down
7 changes: 3 additions & 4 deletions libopenage/datastructure/pairing_heap.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014-2018 the openage authors. See copying.md for legal info.
// Copyright 2014-2023 the openage authors. See copying.md for legal info.

#pragma once

Expand Down Expand Up @@ -256,8 +256,7 @@ class PairingHeap final {
// 1. link root children pairwise, last node may be alone
element_t first_pair = nullptr;
element_t previous_pair = nullptr;

while (unlikely(current_sibling != nullptr)) {
while (current_sibling != nullptr) [[unlikely]] {
element_t link0 = current_sibling;
element_t link1 = current_sibling->next_sibling;

Expand Down Expand Up @@ -627,7 +626,7 @@ class PairingHeap final {
* insert a node into the heap.
*/
void root_insert(const element_t &node) {
if (unlikely(this->root_node == nullptr)) {
if (this->root_node == nullptr) [[unlikely]] {
this->root_node = node;
} else {
this->root_node = this->root_node->link_with(node);
Expand Down
5 changes: 2 additions & 3 deletions libopenage/error/error.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013-2021 the openage authors. See copying.md for legal info.
// Copyright 2013-2023 the openage authors. See copying.md for legal info.

#include "error.h"

Expand All @@ -22,8 +22,7 @@ Error::Error(const log::message &msg, bool generate_backtrace, bool store_cause)
:
std::runtime_error{runtime_error_message},
msg(msg) {

if (unlikely(enable_break_on_create)) {
if (enable_break_on_create) [[unlikely]] {
BREAKPOINT;
}

Expand Down
4 changes: 2 additions & 2 deletions libopenage/error/error.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013-2019 the openage authors. See copying.md for legal info.
// Copyright 2013-2023 the openage authors. See copying.md for legal info.

#pragma once

Expand Down Expand Up @@ -152,7 +152,7 @@ inline std::string no_ensuring_message()
}

// ENSURE(condition, errormessage << variable << etcetc)
#define ENSURE(...) do { if (unlikely(not OPENAGE_ENS_FIRST(__VA_ARGS__))) { throw ::openage::error::Error(MSG(err) OPENAGE_ENS_REST(__VA_ARGS__)); } } while (0)
#define ENSURE(...) do { if (not OPENAGE_ENS_FIRST(__VA_ARGS__)) [[unlikely]] { throw ::openage::error::Error(MSG(err) OPENAGE_ENS_REST(__VA_ARGS__)); } } while (0)

/*
* expands to the first argument
Expand Down
6 changes: 3 additions & 3 deletions libopenage/event/eventstore.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2018 the openage authors. See copying.md for legal info.
// Copyright 2018-2023 the openage authors. See copying.md for legal info.

#include "eventstore.h"

Expand All @@ -13,7 +13,7 @@
namespace openage::event {

void EventStore::push(const std::shared_ptr<Event> &event) {
if (unlikely(event == nullptr)) {
if (event == nullptr) [[unlikely]] {
throw Error{ERR << "inserting nullptr event to queue"};
}

Expand Down Expand Up @@ -66,7 +66,7 @@ bool EventStore::erase(const std::shared_ptr<Event> &event) {

void EventStore::update(const std::shared_ptr<Event> &event) {
auto it = this->events.find(event);
if (unlikely(it != std::end(this->events))) {
if (it != std::end(this->events)) [[unlikely]] {
this->heap.update(it->second);
}
else {
Expand Down
2 changes: 1 addition & 1 deletion libopenage/presenter/assets/asset_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ void AssetManager::check_updates() {

std::shared_ptr<Texture> AssetManager::get_missing_tex() {
// if not loaded, fetch the "missing" texture (big red X).
if (unlikely(this->missing_tex.get() == nullptr)) {
if (this->missing_tex.get() == nullptr) [[unlikely]] {
this->missing_tex = std::make_shared<Texture>(
this->asset_dir["test"]["textures"]["missing.png"],
false);
Expand Down
10 changes: 4 additions & 6 deletions libopenage/pyinterface/exctranslate.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2019 the openage authors. See copying.md for legal info.
// Copyright 2015-2023 the openage authors. See copying.md for legal info.

#include "exctranslate.h"

Expand Down Expand Up @@ -55,8 +55,7 @@ void translate_exc_cpp_to_py() {
throw;

} catch (PyException &exc) {

if (unlikely(raise_cpp_pyexception == nullptr)) {
if (raise_cpp_pyexception == nullptr) [[unlikely]] {
throw Error(MSG(err) <<
"raise_pyexception_in_py is uninitialized; "
"can't translate C++ exception to Python exception.",
Expand All @@ -67,8 +66,7 @@ void translate_exc_cpp_to_py() {
raise_cpp_pyexception(&exc);

} catch (Error &exc) {

if (unlikely(raise_cpp_error == nullptr)) {
if (raise_cpp_error == nullptr) [[unlikely]] {
throw Error(MSG(err) <<
"raise_error_in_py is uninitialized; "
"can't translate C++ exception to Python exception.",
Expand All @@ -91,7 +89,7 @@ void translate_exc_cpp_to_py() {


void translate_exc_py_to_cpp() {
if (unlikely(describe_py_exception == nullptr)) {
if (describe_py_exception == nullptr) [[unlikely]] {
throw Error(MSG(err) <<
"describe_py_exception is uninitialized; "
"can't check for and translate Python exception to C++ exception.");
Expand Down
2 changes: 1 addition & 1 deletion libopenage/pyinterface/functional.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class Func {
* raises an Error if this->fptr is still uninitialized.
*/
inline void check_fptr() const {
if (unlikely(not this->fptr)) {
if (not this->fptr) [[unlikely]] {
throw Error(
MSG(err) << "Uninitialized Func object at " <<
util::symbol_name(static_cast<const void *>(this)) << ": "
Expand Down
8 changes: 4 additions & 4 deletions libopenage/pyinterface/pyobject.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2019 the openage authors. See copying.md for legal info.
// Copyright 2015-2023 the openage authors. See copying.md for legal info.

#include "pyobject.h"

Expand Down Expand Up @@ -101,7 +101,7 @@ PyObjectRef::~PyObjectRef() {


void PyObjectRef::set_ref(PyObject *ref) {
if (unlikely(this->ref != nullptr)) {
if (this->ref != nullptr) [[unlikely]] {
py_xdecref.call(this->ref);
}

Expand All @@ -111,7 +111,7 @@ void PyObjectRef::set_ref(PyObject *ref) {


void PyObjectRef::set_ref_without_incrementing(PyObject *ref) {
if (unlikely(this->ref != nullptr)) {
if (this->ref != nullptr) [[unlikely]] {
py_xdecref.call(this->ref);
}

Expand Down Expand Up @@ -259,7 +259,7 @@ std::string PyObjectRef::classname() const {


std::ostream &operator <<(std::ostream &os, const PyObjectRef &ref) {
if (unlikely(ref.get_ref() == nullptr)) {
if (ref.get_ref() == nullptr) [[unlikely]] {
os << "PyObjectRef[null]";
} else {
os << "PyObjectRef[" << ref.repr() << "]";
Expand Down
4 changes: 2 additions & 2 deletions libopenage/pyinterface/setup.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2019 the openage authors. See copying.md for legal info.
// Copyright 2015-2023 the openage authors. See copying.md for legal info.

#include "setup.h"

Expand Down Expand Up @@ -47,7 +47,7 @@ void add_py_if_component(void *thisptr, std::function<bool ()> checker) {
std::unique_lock<std::mutex> lock{checkers.lock};

// enforce that the object has an associated symbol name.
if (unlikely(not util::is_symbol(thisptr))) {
if (not util::is_symbol(thisptr)) [[unlikely]] {
throw Error(
MSG(err) <<
"Can't instantiate py interface component as non-global object. " <<
Expand Down
8 changes: 4 additions & 4 deletions libopenage/renderer/font/font.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2019 the openage authors. See copying.md for legal info.
// Copyright 2015-2023 the openage authors. See copying.md for legal info.

#include "font.h"

Expand Down Expand Up @@ -112,10 +112,10 @@ Font::Font(FontManager *font_manager, const font_description &description)

void Font::initialize(FT_Library ft_library) {
FT_Face ft_face;
if (unlikely(FT_New_Face(ft_library, this->description.font_file.c_str(), 0, &ft_face))) {
if (FT_New_Face(ft_library, this->description.font_file.c_str(), 0, &ft_face)) [[unlikely]] {
throw Error(MSG(err) << "Failed to create font from " << this->description.font_file);
}
if (unlikely(FT_Set_Char_Size(ft_face, 0, this->description.size * FREETYPE_UNIT, 72, 72))) {
if (FT_Set_Char_Size(ft_face, 0, this->description.size * FREETYPE_UNIT, 72, 72)) [[unlikely]] {
throw Error(MSG(err) << "Failed to set font face size to " << this->description.size);
}

Expand Down Expand Up @@ -213,7 +213,7 @@ std::vector<codepoint_t> Font::get_glyphs(const std::string &text) const {

std::unique_ptr<unsigned char[]> Font::load_glyph(codepoint_t codepoint, Glyph &glyph) const {
FT_Face ft_face = hb_ft_font_get_face(this->hb_font);
if (unlikely(FT_Load_Glyph(ft_face, codepoint, FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING | FT_LOAD_RENDER))) {
if (FT_Load_Glyph(ft_face, codepoint, FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING | FT_LOAD_RENDER)) [[unlikely]] {
return nullptr;
}

Expand Down
4 changes: 2 additions & 2 deletions libopenage/renderer/opengl/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ size_t GlBuffer::get_size() const {
}

void GlBuffer::upload_data(const uint8_t *data, size_t offset, size_t size) {
if (unlikely(offset + size > this->size)) {
if (offset + size > this->size) [[unlikely]] {
throw Error(MSG(err) << "Tried to upload more data to OpenGL buffer than can fit.");
}

Expand All @@ -50,7 +50,7 @@ void GlBuffer::upload_data(const uint8_t *data, size_t offset, size_t size) {
}

void GlBuffer::bind(GLenum target) const {
if (unlikely(!bool(this->handle))) {
if (!bool(this->handle)) [[unlikely]] {
throw Error(MSG(err) << "OpenGL buffer has been moved out of.");
}

Expand Down
2 changes: 1 addition & 1 deletion libopenage/renderer/opengl/vertex_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ GlVertexArray::GlVertexArray(const std::shared_ptr<GlContext> &context,
auto const &buf = buf_info.first;
auto const &info = buf_info.second;

if (unlikely(info.get_shader_input_map())) {
if (info.get_shader_input_map()) [[unlikely]] {
throw Error(MSG(err) << "Shader input mapping is unsupported when constructing a VAO from multiple buffers.");
}

Expand Down
4 changes: 2 additions & 2 deletions libopenage/renderer/resources/mesh_data.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2021 the openage authors. See copying.md for legal info.
// Copyright 2017-2023 the openage authors. See copying.md for legal info.

#include "mesh_data.h"

Expand Down Expand Up @@ -47,7 +47,7 @@ VertexInputInfo::VertexInputInfo(std::vector<vertex_input_t> inputs, vertex_layo

void VertexInputInfo::add_shader_input_map(std::unordered_map<size_t, size_t>&& in_map) {
for (auto mapping : in_map) {
if (unlikely(mapping.first >= this->inputs.size())) {
if (mapping.first >= this->inputs.size()) [[unlikely]] {
throw Error(MSG(err) << "A shader input mapping is out-of-range, exceeding the available number of attributes.");
}
}
Expand Down
6 changes: 3 additions & 3 deletions libopenage/renderer/resources/parser/parse_blendmask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ blending_mask parse_mask(const std::vector<std::string> &args) {
dir = std::stoul(args[0]);
}

if (unlikely(dir > 255)) {
if (dir > 255) [[unlikely]] {
throw Error(MSG(err) << "Reading .blmask file failed. Reason: "
<< "directions value " << dir << " is too large");
}
Expand All @@ -49,7 +49,7 @@ blending_mask parse_mask(const std::vector<std::string> &args) {

BlendPatternInfo parse_blendmask_file(const util::Path &file,
const std::shared_ptr<AssetCache> &cache) {
if (unlikely(not file.is_file())) {
if (not file.is_file()) [[unlikely]] {
throw Error(MSG(err) << "Reading .blmask file '"
<< file.get_name()
<< "' failed. Reason: File not found");
Expand Down Expand Up @@ -91,7 +91,7 @@ BlendPatternInfo parse_blendmask_file(const util::Path &file,
std::vector<std::string> args{util::split(line, ' ')};

// TODO: Avoid double lookup with keywordfuncs.find(args[0])
if (unlikely(not keywordfuncs.contains(args[0]))) {
if (not keywordfuncs.contains(args[0])) [[unlikely]] {
throw Error(MSG(err) << "Reading .blmask file '"
<< file.get_name()
<< "' failed. Reason: Keyword "
Expand Down
4 changes: 2 additions & 2 deletions libopenage/renderer/resources/parser/parse_blendtable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ PatternData parse_pattern(const std::vector<std::string> &args) {

BlendTableInfo parse_blendtable_file(const util::Path &file,
const std::shared_ptr<AssetCache> &cache) {
if (unlikely(not file.is_file())) {
if (not file.is_file()) [[unlikely]] {
throw Error(MSG(err) << "Reading .bltable file '"
<< file.get_name()
<< "' failed. Reason: File not found");
Expand Down Expand Up @@ -99,7 +99,7 @@ BlendTableInfo parse_blendtable_file(const util::Path &file,
std::vector<std::string> args{util::split(line, ' ')};

// TODO: Avoid double lookup with keywordfuncs.find(args[0])
if (unlikely(not keywordfuncs.contains(args[0]))) {
if (not keywordfuncs.contains(args[0])) [[unlikely]] {
throw Error(MSG(err) << "Reading .bltable file '"
<< file.get_name()
<< "' failed. Reason: Keyword "
Expand Down
6 changes: 3 additions & 3 deletions libopenage/renderer/resources/parser/parse_palette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ std::vector<uint8_t> parse_colours(const std::vector<std::string> &lines) {
for (auto arg : line_args) {
auto channel = std::stoul(arg);

if (unlikely(channel > 255)) {
if (channel > 255) [[unlikely]] {
throw Error(MSG(err) << "Reading .opal file failed. Reason: "
<< "color channel value " << channel << " is too large");
}
Expand All @@ -52,7 +52,7 @@ std::vector<uint8_t> parse_colours(const std::vector<std::string> &lines) {
}

PaletteInfo parse_palette_file(const util::Path &file) {
if (unlikely(not file.is_file())) {
if (not file.is_file()) [[unlikely]] {
throw Error(MSG(err) << "Reading .opal file '"
<< file.get_name()
<< "' failed. Reason: File not found");
Expand Down Expand Up @@ -91,7 +91,7 @@ PaletteInfo parse_palette_file(const util::Path &file) {
std::vector<std::string> args{util::split(line, ' ')};

// TODO: Avoid double lookup with keywordfuncs.find(args[0])
if (unlikely(not keywordfuncs.contains(args[0]))) {
if (not keywordfuncs.contains(args[0])) [[unlikely]] {
throw Error(MSG(err) << "Reading .opal file '"
<< file.get_name()
<< "' failed. Reason: Keyword "
Expand Down
Loading

0 comments on commit 0ce207f

Please sign in to comment.