Skip to content

Commit 00a131e

Browse files
authored
improved simplecpp::TokenList constructors (#599)
1 parent 077bed1 commit 00a131e

File tree

2 files changed

+129
-16
lines changed

2 files changed

+129
-16
lines changed

simplecpp.h

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,46 @@ namespace simplecpp {
6969
enum cppstd_t : std::int8_t { CPPUnknown=-1, CPP03, CPP11, CPP14, CPP17, CPP20, CPP23, CPP26 };
7070

7171
using TokenString = std::string;
72+
73+
#if defined(__cpp_lib_string_view) && !defined(__cpp_lib_span)
74+
using View = std::string_view;
75+
#else
76+
struct View
77+
{
78+
// cppcheck-suppress noExplicitConstructor
79+
View(const char* data)
80+
: mData(data)
81+
, mSize(strlen(data))
82+
{}
83+
84+
// only provide when std::span is not available so using untyped initilization won't use View
85+
#if !defined(__cpp_lib_span)
86+
View(const char* data, std::size_t size)
87+
: mData(data)
88+
, mSize(size)
89+
{}
90+
91+
// cppcheck-suppress noExplicitConstructor
92+
View(const std::string& str)
93+
: mData(str.data())
94+
, mSize(str.size())
95+
{}
96+
#endif // !defined(__cpp_lib_span)
97+
98+
const char* data() const {
99+
return mData;
100+
}
101+
102+
std::size_t size() const {
103+
return mSize;
104+
}
105+
106+
private:
107+
const char* mData;
108+
std::size_t mSize;
109+
};
110+
#endif // defined(__cpp_lib_string_view) && !defined(__cpp_lib_span)
111+
72112
class Macro;
73113

74114
/**
@@ -217,7 +257,6 @@ namespace simplecpp {
217257
explicit TokenList(std::vector<std::string> &filenames);
218258
/** generates a token list from the given std::istream parameter */
219259
TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
220-
#ifdef SIMPLECPP_TOKENLIST_ALLOW_PTR
221260
/** generates a token list from the given buffer */
222261
template<size_t size>
223262
TokenList(const char (&data)[size], std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
@@ -228,7 +267,7 @@ namespace simplecpp {
228267
TokenList(const unsigned char (&data)[size], std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
229268
: TokenList(data, size-1, filenames, filename, outputList, 0)
230269
{}
231-
270+
#ifdef SIMPLECPP_TOKENLIST_ALLOW_PTR
232271
/** generates a token list from the given buffer */
233272
TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
234273
: TokenList(data, size, filenames, filename, outputList, 0)
@@ -237,13 +276,11 @@ namespace simplecpp {
237276
TokenList(const char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
238277
: TokenList(reinterpret_cast<const unsigned char*>(data), size, filenames, filename, outputList, 0)
239278
{}
240-
#endif
241-
#if defined(__cpp_lib_string_view) && !defined(__cpp_lib_span)
279+
#endif // SIMPLECPP_TOKENLIST_ALLOW_PTR
242280
/** generates a token list from the given buffer */
243-
TokenList(std::string_view data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
281+
TokenList(View data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
244282
: TokenList(reinterpret_cast<const unsigned char*>(data.data()), data.size(), filenames, filename, outputList, 0)
245283
{}
246-
#endif
247284
#ifdef __cpp_lib_span
248285
/** generates a token list from the given buffer */
249286
TokenList(std::span<const char> data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
@@ -254,7 +291,7 @@ namespace simplecpp {
254291
TokenList(std::span<const unsigned char> data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
255292
: TokenList(data.data(), data.size(), filenames, filename, outputList, 0)
256293
{}
257-
#endif
294+
#endif // __cpp_lib_span
258295

259296
/** generates a token list from the given filename parameter */
260297
TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList = nullptr);

test.cpp

Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3299,20 +3299,97 @@ static void preprocess_files()
32993299
}
33003300
}
33013301

3302-
static void safe_api()
3302+
static void tokenlist_api()
33033303
{
3304-
// this test is to make sure the safe APIs are compiling
3305-
#if defined(__cpp_lib_string_view) || defined(__cpp_lib_span)
33063304
std::vector<std::string> filenames;
3307-
# if defined(__cpp_lib_string_view)
3305+
# if !defined(__cpp_lib_string_view) && !defined(__cpp_lib_span)
3306+
// sized array + size
3307+
{
3308+
char input[] = "code"; // NOLINT(misc-const-correctness)
3309+
simplecpp::TokenList(input,sizeof(input),filenames,"");
3310+
}
3311+
{
3312+
const char input[] = "code";
3313+
simplecpp::TokenList(input,sizeof(input),filenames,"");
3314+
}
3315+
{
3316+
unsigned char input[] = "code"; // NOLINT(misc-const-correctness)
3317+
simplecpp::TokenList(input,sizeof(input),filenames,"");
3318+
}
3319+
{
3320+
const unsigned char input[] = "code";
3321+
simplecpp::TokenList(input,sizeof(input),filenames,"");
3322+
}
3323+
#endif // !defined(__cpp_lib_string_view) && !defined(__cpp_lib_span)
3324+
// pointer via View
3325+
{
3326+
const char * const input = "code";
3327+
simplecpp::TokenList({input},filenames,"");
3328+
}
3329+
// sized array via View
3330+
{
3331+
char input[] = "code"; // NOLINT(misc-const-correctness)
3332+
simplecpp::TokenList(simplecpp::View{input},filenames,"");
3333+
}
3334+
{
3335+
const char input[] = "code";
3336+
simplecpp::TokenList(simplecpp::View{input},filenames,"");
3337+
}
3338+
// sized array + size via View/std::span
3339+
{
3340+
char input[] = "code"; // NOLINT(misc-const-correctness)
3341+
simplecpp::TokenList({input,sizeof(input)},filenames,"");
3342+
}
3343+
{
3344+
const char input[] = "code";
3345+
simplecpp::TokenList({input,sizeof(input)},filenames,"");
3346+
}
3347+
// sized array
3348+
{
3349+
char input[] = "code"; // NOLINT(misc-const-correctness)
3350+
simplecpp::TokenList(input,filenames,"");
3351+
}
3352+
{
3353+
const char input[] = "code";
3354+
simplecpp::TokenList(input,filenames,"");
3355+
}
3356+
{
3357+
unsigned char input[] = "code"; // NOLINT(misc-const-correctness)
3358+
simplecpp::TokenList(input,filenames,"");
3359+
}
3360+
{
3361+
const unsigned char input[] = "code";
3362+
simplecpp::TokenList(input,filenames,"");
3363+
}
3364+
// std::string via View/std::span (implicit)
3365+
{
3366+
std::string input = "code"; // NOLINT(misc-const-correctness)
3367+
simplecpp::TokenList(input,filenames,"");
3368+
}
3369+
{
3370+
const std::string input = "code";
3371+
simplecpp::TokenList(input,filenames,"");
3372+
}
3373+
// std::string via View/std::span (explicit)
3374+
{
3375+
std::string input = "code"; // NOLINT(misc-const-correctness)
3376+
simplecpp::TokenList({input},filenames,"");
3377+
}
3378+
{
3379+
const std::string input = "code";
3380+
simplecpp::TokenList({input},filenames,"");
3381+
}
3382+
3383+
// this test is to make sure the safe APIs are compiling
3384+
#ifdef __cpp_lib_string_view
33083385
{
33093386
const char input[] = "code";
33103387
const std::string_view sv = input;
33113388
// std::string_view can be implicitly converted into a std::span
33123389
simplecpp::TokenList(sv,filenames,"");
33133390
}
3314-
# endif
3315-
# ifdef __cpp_lib_span
3391+
#endif // __cpp_lib_string_view
3392+
#ifdef __cpp_lib_span
33163393
{
33173394
char input[] = "code";
33183395
const std::span sp = input;
@@ -3333,8 +3410,7 @@ static void safe_api()
33333410
const std::span sp = input;
33343411
simplecpp::TokenList(sp,filenames,"");
33353412
}
3336-
# endif
3337-
#endif
3413+
#endif // __cpp_lib_span
33383414
}
33393415

33403416
static void isAbsolutePath() {
@@ -3660,7 +3736,7 @@ int main(int argc, char **argv)
36603736

36613737
TEST_CASE(preprocess_files);
36623738

3663-
TEST_CASE(safe_api);
3739+
TEST_CASE(tokenlist_api);
36643740

36653741
TEST_CASE(isAbsolutePath);
36663742

0 commit comments

Comments
 (0)