Skip to content

Commit 1e41218

Browse files
committed
fixed -Wlifetime-safety-intra-tu-suggestions Clang warnings
1 parent 9097d44 commit 1e41218

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);
@@ -1680,7 +1690,7 @@ namespace simplecpp {
16801690
}
16811691

16821692
/** how has this macro been used so far */
1683-
const std::list<Location> &usage() const {
1693+
const std::list<Location> &usage() const SIMPLECPP_LIFETIMEBOUND {
16841694
return usageList;
16851695
}
16861696

@@ -1870,7 +1880,7 @@ namespace simplecpp {
18701880

18711881
const Token *appendTokens(TokenList &tokens,
18721882
const Location &rawloc,
1873-
const Token * const lpar,
1883+
const Token * const lpar SIMPLECPP_LIFETIMEBOUND,
18741884
const MacroMap &macros,
18751885
const std::set<TokenString> &expandedmacros,
18761886
const std::vector<const Token*> &parametertokens) const {
@@ -2997,7 +3007,7 @@ static long long evaluate(simplecpp::TokenList &expr, const simplecpp::DUI &dui,
29973007
return expr.cfront() && expr.cfront() == expr.cback() && expr.cfront()->number ? stringToLL(expr.cfront()->str()) : 0LL;
29983008
}
29993009

3000-
static const simplecpp::Token *gotoNextLine(const simplecpp::Token *tok)
3010+
static const simplecpp::Token *gotoNextLine(const simplecpp::Token *tok SIMPLECPP_LIFETIMEBOUND)
30013011
{
30023012
const unsigned int line = tok->location.line;
30033013
const unsigned int file = tok->location.fileIndex;

simplecpp.h

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

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

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

162-
const TokenString& str() const {
172+
const TokenString& str() const SIMPLECPP_LIFETIMEBOUND {
163173
return string;
164174
}
165175
void setstr(const std::string &s) {
@@ -479,22 +489,22 @@ namespace simplecpp {
479489
size_type size() const {
480490
return mData.size();
481491
}
482-
iterator begin() {
492+
iterator begin() SIMPLECPP_LIFETIMEBOUND {
483493
return mData.begin();
484494
}
485-
iterator end() {
495+
iterator end() SIMPLECPP_LIFETIMEBOUND {
486496
return mData.end();
487497
}
488-
const_iterator begin() const {
498+
const_iterator begin() const SIMPLECPP_LIFETIMEBOUND {
489499
return mData.begin();
490500
}
491-
const_iterator end() const {
501+
const_iterator end() const SIMPLECPP_LIFETIMEBOUND {
492502
return mData.end();
493503
}
494-
const_iterator cbegin() const {
504+
const_iterator cbegin() const SIMPLECPP_LIFETIMEBOUND {
495505
return mData.cbegin();
496506
}
497-
const_iterator cend() const {
507+
const_iterator cend() const SIMPLECPP_LIFETIMEBOUND {
498508
return mData.cend();
499509
}
500510

@@ -591,6 +601,8 @@ namespace simplecpp {
591601
# pragma warning(pop)
592602
#endif
593603

604+
#undef SIMPLECPP_LIFETIMEBOUND
605+
594606
#undef SIMPLECPP_LIB
595607

596608
#endif

0 commit comments

Comments
 (0)