Ignore GCC-14 and Clang false positives about dangling pointers

For GCC-13, cfb28fbfb5 (in master)
added -Wno-dangling-reference, and that commit explains why the
false positive is triggered by calling find_widget. For GCC-14
there's an attribute so we can flag this specific function as ok.

Clang complains about unknown attributes in the gnu:: namespace,
so has to have the #if, and the #if means we need the #ifdef.

There are still warnings in src/actions/attack.cpp, but that area is
being worked on for an infinite recursion bug, and those warnings log
in a single batch instead of being spread across multiple .cpp files.

(cherry picked from commit e42f83a8d2,
with an improvement to the documentation after cherry-picking)
This commit is contained in:
Steve Cotton 2024-06-14 14:44:26 +02:00 committed by Steve Cotton
parent 5e8df616d9
commit b0c8b69942
6 changed files with 33 additions and 7 deletions

View file

@ -29,6 +29,7 @@
#include "exceptions.hpp"
#include "game_version.hpp"
#include "global.hpp"
namespace game_config {
extern std::string path;
@ -440,7 +441,7 @@ void clear_binary_paths_cache();
* Returns a vector with all possible paths to a given type of binary,
* e.g. 'images', 'sounds', etc,
*/
const std::vector<std::string>& get_binary_paths(const std::string& type);
NOT_DANGLING const std::vector<std::string>& get_binary_paths(const std::string& type);
/**
* Returns a complete path to the actual file of a given @a type

View file

@ -40,3 +40,23 @@
#if defined(__GNUC__) && !defined(__clang__)
#endif
/*
* GCC-13 and GCC-14 warn about functions that return a reference and whose arguments also include a
* reference to a temporary, because they assume that the returned reference may point into the
* argument. This causes false positives for functions that take a std::map-like object as a
* separate argument (or as their "this" pointer), where the temporary being passed in is only used
* as a key to find an object in the map, and the returned reference points to an object owned by
* the map. Similarly, it's a false positive for data owned by a singleton.
*
* GCC-14 supports supressing the warnings with [[gnu::no_dangling]]. Clang complains about unknown
* attributes in the gnu:: namespace, so has to have the #if, and the #if means we need the #ifdef.
*/
#ifdef __has_cpp_attribute
#if __has_cpp_attribute(gnu::no_dangling)
#define NOT_DANGLING [[gnu::no_dangling]]
#endif
#endif
#ifndef NOT_DANGLING
#define NOT_DANGLING
#endif

View file

@ -15,6 +15,7 @@
#pragma once
#include "global.hpp"
#include "gui/widgets/helper.hpp"
#include "gui/widgets/widget.hpp"
#include "utils/const_clone.hpp"
@ -66,7 +67,7 @@ T& get_parent(widget& child)
* @returns The widget with the id.
*/
template <class T>
T* find_widget(utils::const_clone_ptr<widget, T> widget,
NOT_DANGLING T* find_widget(utils::const_clone_ptr<widget, T> widget,
const std::string& id,
const bool must_be_active,
const bool must_exist)
@ -93,7 +94,7 @@ T* find_widget(utils::const_clone_ptr<widget, T> widget,
* @returns The widget with the id.
*/
template <class T>
T& find_widget(utils::const_clone_ptr<widget, T> widget,
NOT_DANGLING T& find_widget(utils::const_clone_ptr<widget, T> widget,
const std::string& id,
const bool must_be_active)
{

View file

@ -15,6 +15,7 @@
#pragma once
#include "global.hpp"
#include "tooltips.hpp"
#include "tstring.hpp"
@ -307,7 +308,7 @@ private:
const std::map<std::string_view, hotkey::hotkey_command>& get_hotkey_commands();
/** returns the hotkey_command with the given name */
const hotkey_command& get_hotkey_command(const std::string& command);
NOT_DANGLING const hotkey_command& get_hotkey_command(const std::string& command);
bool is_scope_active(scope s);
bool is_scope_active(hk_scopes s);

View file

@ -23,6 +23,7 @@
#include "color.hpp"
#include "config.hpp"
#include "generic_event.hpp"
#include "global.hpp"
#include "sdl/rect.hpp"
#include <memory>
@ -320,7 +321,7 @@ public:
static void set_known_themes(const game_config_view* cfg);
/** Returns the saved config for the theme with the given ID. */
static const config& get_theme_config(const std::string& id);
NOT_DANGLING static const config& get_theme_config(const std::string& id);
/** Returns minimal info about saved themes, optionally including hidden ones. */
static std::vector<theme_info> get_basic_theme_info(bool include_hidden = false);

View file

@ -16,6 +16,7 @@
#pragma once
#include "config.hpp"
#include "global.hpp"
#include "utils/name_generator.hpp"
#include <array>
@ -94,9 +95,10 @@ const std::string& gender_string(unit_race::GENDER gender);
/**
* Chooses a value from the given config based on gender. If the value for
* the specified gender is blank, then @a default_key is chosen instead.
* the specified gender is blank, then @a default_key is used to look up a
* value instead.
*/
const config::attribute_value & gender_value(
NOT_DANGLING const config::attribute_value & gender_value(
const config & cfg, unit_race::GENDER gender, const std::string & male_key,
const std::string & female_key, const std::string & default_key);