Skip to content

Commit 620b38e

Browse files
committed
fixed -Wlifetime-safety-cross-tu-suggestions Clang warnings
1 parent 0c04a76 commit 620b38e

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
@@ -394,7 +394,7 @@ namespace {
394394
class StdCharBufStream : public simplecpp::TokenList::Stream {
395395
public:
396396
// cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members
397-
StdCharBufStream(const unsigned char* str, std::size_t size)
397+
StdCharBufStream(const unsigned char* str SIMPLECPP_LIFETIMEBOUND, std::size_t size)
398398
: str(str)
399399
, size(size)
400400
{
@@ -1509,12 +1509,12 @@ namespace simplecpp {
15091509

15101510
class Macro {
15111511
public:
1512-
explicit Macro(std::vector<std::string> &f) : nameTokDef(nullptr), valueToken(nullptr), endToken(nullptr), files(f), tokenListDefine(f), variadic(false), variadicOpt(false), valueDefinedInCode_(false) {}
1512+
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) {}
15131513

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

simplecpp.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,20 @@ namespace simplecpp {
8484
struct View
8585
{
8686
// cppcheck-suppress noExplicitConstructor
87-
View(const char* data)
87+
View(const char* data SIMPLECPP_LIFETIMEBOUND)
8888
: mData(data)
8989
, mSize(strlen(data))
9090
{}
9191

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

9999
// cppcheck-suppress noExplicitConstructor
100-
View(const std::string& str)
100+
View(const std::string& str SIMPLECPP_LIFETIMEBOUND)
101101
: mData(str.data())
102102
, mSize(str.size())
103103
{}
@@ -265,9 +265,9 @@ namespace simplecpp {
265265
public:
266266
class Stream;
267267

268-
explicit TokenList(std::vector<std::string> &filenames);
268+
explicit TokenList(std::vector<std::string> &filenames SIMPLECPP_LIFETIMEBOUND);
269269
/** generates a token list from the given std::istream parameter */
270-
TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
270+
TokenList(std::istream &istr, std::vector<std::string> &filenames SIMPLECPP_LIFETIMEBOUND, const std::string &filename=std::string(), OutputList *outputList = nullptr);
271271
/** generates a token list from the given buffer */
272272
template<size_t size>
273273
TokenList(const char (&data)[size], std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
@@ -305,7 +305,7 @@ namespace simplecpp {
305305
#endif // __cpp_lib_span
306306

307307
/** generates a token list from the given filename parameter */
308-
TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList = nullptr);
308+
TokenList(const std::string &filename, std::vector<std::string> &filenames SIMPLECPP_LIFETIMEBOUND, OutputList *outputList = nullptr);
309309
TokenList(const TokenList &other);
310310
TokenList(TokenList &&other);
311311
~TokenList();
@@ -385,7 +385,7 @@ namespace simplecpp {
385385
const std::string& file(const Location& loc) const;
386386

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

390390
void combineOperators();
391391

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)