Skip to content

Commit

Permalink
testrunner: prefer to pass code as sized const char[] (#6381)
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave authored May 16, 2024
1 parent b5cb0f8 commit 5f9039e
Show file tree
Hide file tree
Showing 37 changed files with 1,749 additions and 1,361 deletions.
22 changes: 18 additions & 4 deletions test/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ namespace simplecpp {
// TODO: make Tokenizer private
class SimpleTokenizer : public Tokenizer {
public:
SimpleTokenizer(ErrorLogger& errorlogger, const char code[], bool cpp = true)
template<size_t size>
SimpleTokenizer(ErrorLogger& errorlogger, const char (&code)[size], bool cpp = true)
: Tokenizer{s_settings, errorlogger}
{
if (!tokenize(code, cpp))
Expand Down Expand Up @@ -71,7 +72,20 @@ class SimpleTokenizer : public Tokenizer {
* @param configuration E.g. "A" for code where "#ifdef A" is true
* @return false if source code contains syntax errors
*/
bool tokenize(const char code[],
template<size_t size>
bool tokenize(const char (&code)[size],
bool cpp = true,
const std::string &configuration = emptyString)
{
std::istringstream istr(code);
if (!list.createTokens(istr, cpp ? "test.cpp" : "test.c"))
return false;

return simplifyTokens1(configuration);
}

// TODO: get rid of this
bool tokenize(const std::string& code,
bool cpp = true,
const std::string &configuration = emptyString)
{
Expand All @@ -90,8 +104,8 @@ class SimpleTokenizer : public Tokenizer {
class SimpleTokenList
{
public:

explicit SimpleTokenList(const char code[], Standards::Language lang = Standards::Language::CPP)
template<size_t size>
explicit SimpleTokenList(const char (&code)[size], Standards::Language lang = Standards::Language::CPP)
{
std::istringstream iss(code);
if (!list.createTokens(iss, lang))
Expand Down
3 changes: 2 additions & 1 deletion test/test64bit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class Test64BitPortability : public TestFixture {
}

#define check(code) check_(code, __FILE__, __LINE__)
void check_(const char code[], const char* file, int line) {
template<size_t size>
void check_(const char (&code)[size], const char* file, int line) {
// Tokenize..
SimpleTokenizer tokenizer(settings, *this);
ASSERT_LOC(tokenizer.tokenize(code), file, line);
Expand Down
3 changes: 2 additions & 1 deletion test/testassert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class TestAssert : public TestFixture {
const Settings settings = settingsBuilder().severity(Severity::warning).build();

#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
void check_(const char* file, int line, const char code[]) {
template<size_t size>
void check_(const char* file, int line, const char (&code)[size]) {
// Tokenize..
SimpleTokenizer tokenizer(settings, *this);
ASSERT_LOC(tokenizer.tokenize(code), file, line);
Expand Down
Loading

0 comments on commit 5f9039e

Please sign in to comment.