Skip to content

Commit 05e9d09

Browse files
committed
fixed -Wlifetime-safety-cross-tu-suggestions Clang warnings
1 parent 7df28d9 commit 05e9d09

4 files changed

Lines changed: 23 additions & 12 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
7575

7676
# TODO: check for proper AppleClang version
7777
# we do not add the annotation until C++20
78-
# the warning was introduced with Clang 23
78+
# the warnings was introduced with Clang 23
7979
if(CMAKE_CXX_STANDARD LESS 20)
8080
add_compile_options_safe(-Wno-lifetime-safety-intra-tu-suggestions)
81+
add_compile_options_safe(-Wno-lifetime-safety-cross-tu-suggestions)
8182
endif()
8283

8384
# TODO: fix these?

simplecpp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ namespace {
389389
class StdCharBufStream : public simplecpp::TokenList::Stream {
390390
public:
391391
// cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members
392-
StdCharBufStream(const unsigned char* str, std::size_t size)
392+
StdCharBufStream(const unsigned char* str SIMPLECPP_LIFETIMEBOUND, std::size_t size)
393393
: str(str)
394394
, size(size)
395395
{
@@ -1508,12 +1508,12 @@ namespace simplecpp {
15081508

15091509
class Macro {
15101510
public:
1511-
explicit Macro(std::vector<std::string> &f) : nameTokDef(nullptr), valueToken(nullptr), endToken(nullptr), files(f), tokenListDefine(f), variadic(false), variadicOpt(false), valueDefinedInCode_(false) {}
1511+
explicit Macro(std::vector<std::string> &f SIMPLECPP_LIFETIMEBOUND) : nameTokDef(nullptr), valueToken(nullptr), endToken(nullptr), files(f), tokenListDefine(f), variadic(false), variadicOpt(false), valueDefinedInCode_(false) {}
15121512

15131513
/**
15141514
* @throws std::runtime_error thrown on bad macro syntax
15151515
*/
1516-
Macro(const Token *tok, std::vector<std::string> &f) : nameTokDef(nullptr), files(f), tokenListDefine(f), valueDefinedInCode_(true) {
1516+
Macro(const Token *tok, std::vector<std::string> &f SIMPLECPP_LIFETIMEBOUND) : nameTokDef(nullptr), files(f), tokenListDefine(f), valueDefinedInCode_(true) {
15171517
if (sameline(tok->previousSkipComments(), tok))
15181518
throw std::runtime_error("bad macro syntax");
15191519
if (tok->op != '#')

simplecpp.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,20 @@ namespace simplecpp {
8888
struct View
8989
{
9090
// cppcheck-suppress noExplicitConstructor
91-
View(const char* data)
91+
View(const char* data SIMPLECPP_LIFETIMEBOUND)
9292
: mData(data)
9393
, mSize(strlen(data))
9494
{}
9595

9696
// only provide when std::span is not available so using untyped initialization won't use View
9797
#if !defined(__cpp_lib_span)
98-
View(const char* data, std::size_t size)
98+
View(const char* data SIMPLECPP_LIFETIMEBOUND, std::size_t size)
9999
: mData(data)
100100
, mSize(size)
101101
{}
102102

103103
// cppcheck-suppress noExplicitConstructor
104-
View(const std::string& str)
104+
View(const std::string& str SIMPLECPP_LIFETIMEBOUND)
105105
: mData(str.data())
106106
, mSize(str.size())
107107
{}
@@ -269,9 +269,9 @@ namespace simplecpp {
269269
public:
270270
class Stream;
271271

272-
explicit TokenList(std::vector<std::string> &filenames);
272+
explicit TokenList(std::vector<std::string> &filenames SIMPLECPP_LIFETIMEBOUND);
273273
/** generates a token list from the given std::istream parameter */
274-
TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
274+
TokenList(std::istream &istr, std::vector<std::string> &filenames SIMPLECPP_LIFETIMEBOUND, const std::string &filename=std::string(), OutputList *outputList = nullptr);
275275
/** generates a token list from the given buffer */
276276
template<size_t size>
277277
TokenList(const char (&data)[size], std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
@@ -309,7 +309,7 @@ namespace simplecpp {
309309
#endif // __cpp_lib_span
310310

311311
/** generates a token list from the given filename parameter */
312-
TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList = nullptr);
312+
TokenList(const std::string &filename, std::vector<std::string> &filenames SIMPLECPP_LIFETIMEBOUND, OutputList *outputList = nullptr);
313313
TokenList(const TokenList &other);
314314
TokenList(TokenList &&other);
315315
~TokenList();
@@ -389,7 +389,7 @@ namespace simplecpp {
389389
const std::string& file(const Location& loc) const;
390390

391391
private:
392-
TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList, int /*unused*/);
392+
TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames SIMPLECPP_LIFETIMEBOUND, const std::string &filename, OutputList *outputList, int /*unused*/);
393393

394394
void combineOperators();
395395

test.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
#error "SIMPLECPP_TEST_SOURCE_DIR is not defined."
2323
#endif
2424

25+
#if defined(__has_cpp_attribute)
26+
# if __has_cpp_attribute (clang::lifetimebound)
27+
# define SIMPLECPP_LIFETIMEBOUND [[clang::lifetimebound]]
28+
# else
29+
# define SIMPLECPP_LIFETIMEBOUND
30+
# endif
31+
#else
32+
# define SIMPLECPP_LIFETIMEBOUND
33+
#endif
34+
2535
#define STRINGIZE_(x) #x
2636
#define STRINGIZE(x) STRINGIZE_(x)
2737

@@ -101,7 +111,7 @@ static void testcase(const std::string &name, void (*const f)(), int argc, char
101111

102112
#define TEST_CASE(F) (testcase(#F, F, argc, argv))
103113

104-
static simplecpp::TokenList makeTokenList(const char code[], std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), simplecpp::OutputList * const outputList=nullptr)
114+
static simplecpp::TokenList makeTokenList(const char code[], std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), simplecpp::OutputList * const outputList SIMPLECPP_LIFETIMEBOUND = nullptr)
105115
{
106116
switch (USE_INPUT) {
107117
case Input::Stringstream: {

0 commit comments

Comments
 (0)