Skip to content

Commit 9c6ed1e

Browse files
committed
fixed -Wlifetime-safety-intra-tu-suggestions Clang warnings
1 parent dc0bdd3 commit 9c6ed1e

3 files changed

Lines changed: 40 additions & 10 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: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@
6060
# include <sys/types.h>
6161
#endif
6262

63+
#if defined(__has_cpp_attribute)
64+
# if __has_cpp_attribute (clang::lifetimebound)
65+
# define SIMPLECPP_LIFETIMEBOUND [[clang::lifetimebound]]
66+
# else
67+
# define SIMPLECPP_LIFETIMEBOUND
68+
# endif
69+
#else
70+
# define SIMPLECPP_LIFETIMEBOUND
71+
#endif
72+
6373
static bool isHex(const std::string &s)
6474
{
6575
return s.size()>2 && (s.compare(0,2,"0x")==0 || s.compare(0,2,"0X")==0);
@@ -1679,7 +1689,7 @@ namespace simplecpp {
16791689
}
16801690

16811691
/** how has this macro been used so far */
1682-
const std::list<Location> &usage() const {
1692+
const std::list<Location> &usage() const SIMPLECPP_LIFETIMEBOUND {
16831693
return usageList;
16841694
}
16851695

@@ -1874,7 +1884,7 @@ namespace simplecpp {
18741884

18751885
const Token *appendTokens(TokenList &tokens,
18761886
const Location &rawloc,
1877-
const Token * const lpar,
1887+
const Token * const lpar SIMPLECPP_LIFETIMEBOUND,
18781888
const MacroMap &macros,
18791889
const std::set<TokenString> &expandedmacros,
18801890
const std::vector<const Token*> &parametertokens) const {
@@ -3001,7 +3011,7 @@ static long long evaluate(simplecpp::TokenList &expr, const simplecpp::DUI &dui,
30013011
return expr.cfront() && expr.cfront() == expr.cback() && expr.cfront()->number ? stringToLL(expr.cfront()->str()) : 0LL;
30023012
}
30033013

3004-
static const simplecpp::Token *gotoNextLine(const simplecpp::Token *tok)
3014+
static const simplecpp::Token *gotoNextLine(const simplecpp::Token *tok SIMPLECPP_LIFETIMEBOUND)
30053015
{
30063016
const unsigned int line = tok->location.line;
30073017
const unsigned int file = tok->location.fileIndex;

simplecpp.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@
4242
# define SIMPLECPP_LIB
4343
#endif
4444

45+
#if defined(__has_cpp_attribute)
46+
# if __has_cpp_attribute (clang::lifetimebound)
47+
# define SIMPLECPP_LIFETIMEBOUND [[clang::lifetimebound]]
48+
# else
49+
# define SIMPLECPP_LIFETIMEBOUND
50+
# endif
51+
#else
52+
# define SIMPLECPP_LIFETIMEBOUND
53+
#endif
54+
4555
#if defined(_MSC_VER)
4656
# pragma warning(push)
4757
// suppress warnings about "conversion from 'type1' to 'type2', possible loss of data"
@@ -160,7 +170,7 @@ namespace simplecpp {
160170

161171
Token &operator=(const Token &tok) = delete;
162172

163-
const TokenString& str() const {
173+
const TokenString& str() const SIMPLECPP_LIFETIMEBOUND {
164174
return string;
165175
}
166176
void setstr(const std::string &s) {
@@ -481,22 +491,22 @@ namespace simplecpp {
481491
size_type size() const {
482492
return mData.size();
483493
}
484-
iterator begin() {
494+
iterator begin() SIMPLECPP_LIFETIMEBOUND {
485495
return mData.begin();
486496
}
487-
iterator end() {
497+
iterator end() SIMPLECPP_LIFETIMEBOUND {
488498
return mData.end();
489499
}
490-
const_iterator begin() const {
500+
const_iterator begin() const SIMPLECPP_LIFETIMEBOUND {
491501
return mData.begin();
492502
}
493-
const_iterator end() const {
503+
const_iterator end() const SIMPLECPP_LIFETIMEBOUND {
494504
return mData.end();
495505
}
496-
const_iterator cbegin() const {
506+
const_iterator cbegin() const SIMPLECPP_LIFETIMEBOUND {
497507
return mData.cbegin();
498508
}
499-
const_iterator cend() const {
509+
const_iterator cend() const SIMPLECPP_LIFETIMEBOUND {
500510
return mData.cend();
501511
}
502512

@@ -600,6 +610,8 @@ namespace simplecpp {
600610
# pragma warning(pop)
601611
#endif
602612

613+
#undef SIMPLECPP_LIFETIMEBOUND
614+
603615
#undef SIMPLECPP_LIB
604616

605617
#endif

0 commit comments

Comments
 (0)