Skip to content

Commit 2e61935

Browse files
committed
fixed -Wlifetime-safety-intra-tu-suggestions Clang warnings
1 parent ebb8c3e commit 2e61935

3 files changed

Lines changed: 29 additions & 11 deletions

File tree

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
7373
add_compile_options_safe(-Wno-thread-safety-negative)
7474
add_compile_options_safe(-Wno-thread-safety-beta)
7575

76+
# TODO: check for proper AppleClang version
77+
# we do not add the annotation until C++20
78+
# the warning was introduced with Clang 23
79+
if(CMAKE_CXX_STANDARD LESS 20)
80+
add_compile_options_safe(-Wno-lifetime-safety-intra-tu-suggestions)
81+
endif()
82+
7683
# TODO: fix these?
7784
add_compile_options(-Wno-padded)
7885
add_compile_options(-Wno-sign-conversion)
@@ -83,6 +90,7 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
8390
# we are not interested in these
8491
set_source_files_properties(test.cpp PROPERTIES COMPILE_FLAGS "-Wno-multichar -Wno-four-char-constants")
8592

93+
# TODO: check for proper AppleClang version
8694
if (CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 14 OR CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 14)
8795
# TODO: verify this regression still exists in clang-15
8896
if (CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")

simplecpp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,7 +1679,7 @@ namespace simplecpp {
16791679
}
16801680

16811681
/** how has this macro been used so far */
1682-
const std::list<Location> &usage() const {
1682+
const std::list<Location> &usage() const SIMPLECPP_LIFETIMEBOUND {
16831683
return usageList;
16841684
}
16851685

@@ -1869,7 +1869,7 @@ namespace simplecpp {
18691869

18701870
const Token *appendTokens(TokenList &tokens,
18711871
const Location &rawloc,
1872-
const Token * const lpar,
1872+
const Token * const lpar SIMPLECPP_LIFETIMEBOUND,
18731873
const MacroMap &macros,
18741874
const std::set<TokenString> &expandedmacros,
18751875
const std::vector<const Token*> &parametertokens) const {
@@ -2997,7 +2997,7 @@ static long long evaluate(simplecpp::TokenList &expr, const simplecpp::DUI &dui,
29972997
return expr.cfront() && expr.cfront() == expr.cback() && expr.cfront()->number ? stringToLL(expr.cfront()->str()) : 0LL;
29982998
}
29992999

3000-
static const simplecpp::Token *gotoNextLine(const simplecpp::Token *tok)
3000+
static const simplecpp::Token *gotoNextLine(const simplecpp::Token *tok SIMPLECPP_LIFETIMEBOUND)
30013001
{
30023002
const unsigned int line = tok->location.line;
30033003
const unsigned int file = tok->location.fileIndex;

simplecpp.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@
4545
# include <sys/types.h>
4646
#endif
4747

48+
#if defined(__has_cpp_attribute)
49+
# if __has_cpp_attribute (clang::lifetimebound)
50+
# define SIMPLECPP_LIFETIMEBOUND [[clang::lifetimebound]]
51+
# else
52+
# define SIMPLECPP_LIFETIMEBOUND
53+
# endif
54+
#else
55+
# define SIMPLECPP_LIFETIMEBOUND
56+
#endif
57+
4858
#if defined(_MSC_VER)
4959
# pragma warning(push)
5060
// suppress warnings about "conversion from 'type1' to 'type2', possible loss of data"
@@ -163,7 +173,7 @@ namespace simplecpp {
163173

164174
Token &operator=(const Token &tok) = delete;
165175

166-
const TokenString& str() const {
176+
const TokenString& str() const SIMPLECPP_LIFETIMEBOUND {
167177
return string;
168178
}
169179
void setstr(const std::string &s) {
@@ -480,25 +490,25 @@ namespace simplecpp {
480490
using const_iterator = container_type::const_iterator;
481491
using size_type = container_type::size_type;
482492

483-
size_type size() const {
493+
size_type size() const SIMPLECPP_LIFETIMEBOUND {
484494
return mData.size();
485495
}
486-
iterator begin() {
496+
iterator begin() SIMPLECPP_LIFETIMEBOUND {
487497
return mData.begin();
488498
}
489-
iterator end() {
499+
iterator end() SIMPLECPP_LIFETIMEBOUND {
490500
return mData.end();
491501
}
492-
const_iterator begin() const {
502+
const_iterator begin() const SIMPLECPP_LIFETIMEBOUND {
493503
return mData.begin();
494504
}
495-
const_iterator end() const {
505+
const_iterator end() const SIMPLECPP_LIFETIMEBOUND {
496506
return mData.end();
497507
}
498-
const_iterator cbegin() const {
508+
const_iterator cbegin() const SIMPLECPP_LIFETIMEBOUND {
499509
return mData.cbegin();
500510
}
501-
const_iterator cend() const {
511+
const_iterator cend() const SIMPLECPP_LIFETIMEBOUND {
502512
return mData.cend();
503513
}
504514

0 commit comments

Comments
 (0)