Skip to content

Commit 33d9718

Browse files
committed
cleaned up passing of buffers
1 parent 60b7e35 commit 33d9718

File tree

8 files changed

+40
-40
lines changed

8 files changed

+40
-40
lines changed

test/helpers.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ std::string PreprocessorHelper::getcode(const Settings& settings, ErrorLogger& e
124124
return it->second;
125125
}
126126

127-
std::map<std::string, std::string> PreprocessorHelper::getcode(const Settings& settings, ErrorLogger& errorlogger, const char code[], std::size_t size, const std::string &filename, SuppressionList *inlineSuppression)
127+
std::map<std::string, std::string> PreprocessorHelper::getcode(const Settings& settings, ErrorLogger& errorlogger, const char* code, std::size_t size, const std::string &filename, SuppressionList *inlineSuppression)
128128
{
129129
return getcode(settings, errorlogger, code, size, {}, filename, inlineSuppression);
130130
}
131131

132-
std::map<std::string, std::string> PreprocessorHelper::getcode(const Settings& settings, ErrorLogger& errorlogger, const char code[], std::size_t size, std::set<std::string> cfgs, const std::string &filename, SuppressionList *inlineSuppression)
132+
std::map<std::string, std::string> PreprocessorHelper::getcode(const Settings& settings, ErrorLogger& errorlogger, const char* code, std::size_t size, std::set<std::string> cfgs, const std::string &filename, SuppressionList *inlineSuppression)
133133
{
134134
simplecpp::OutputList outputList;
135135
std::vector<std::string> files;

test/helpers.h

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,17 @@ class SimpleTokenizer : public Tokenizer {
6161
template<size_t size>
6262
bool tokenize(const char (&code)[size])
6363
{
64-
return tokenize(code, size-1, std::string(list.isCPP() ? "test.cpp" : "test.c"));
64+
return tokenize(code, size-1);
6565
}
6666

6767
bool tokenize(const std::string& code)
6868
{
69-
return tokenize(code.data(), code.size(), std::string(list.isCPP() ? "test.cpp" : "test.c"));
69+
return tokenize(code.data(), code.size());
70+
}
71+
72+
bool tokenize(const char* code, std::size_t size)
73+
{
74+
return tokenize(code, size, std::string(list.isCPP() ? "test.cpp" : "test.c"));
7075
}
7176

7277
private:
@@ -107,7 +112,7 @@ class SimpleTokenList
107112
explicit SimpleTokenList(const char (&code)[size], Standards::Language lang = Standards::Language::CPP)
108113
: list{settings, lang}
109114
{
110-
if (!list.createTokensFromBuffer(code, size-1))
115+
if (!list.createTokensFromString(code))
111116
throw std::runtime_error("creating tokens failed");
112117
}
113118

@@ -116,7 +121,7 @@ class SimpleTokenList
116121
: list{settings, lang}
117122
{
118123
list.appendFileIfNew(file0);
119-
if (!list.createTokensFromBuffer(code, size-1))
124+
if (!list.createTokensFromString(code))
120125
throw std::runtime_error("creating tokens failed");
121126
}
122127

@@ -184,8 +189,8 @@ class PreprocessorHelper
184189
}
185190

186191
private:
187-
static std::map<std::string, std::string> getcode(const Settings& settings, ErrorLogger& errorlogger, const char code[], std::size_t size, const std::string &filename = "file.c", SuppressionList *inlineSuppression = nullptr);
188-
static std::map<std::string, std::string> getcode(const Settings& settings, ErrorLogger& errorlogger, const char code[], std::size_t size, std::set<std::string> cfgs, const std::string &filename = "file.c", SuppressionList *inlineSuppression = nullptr);
192+
static std::map<std::string, std::string> getcode(const Settings& settings, ErrorLogger& errorlogger, const char* code, std::size_t size, const std::string &filename = "file.c", SuppressionList *inlineSuppression = nullptr);
193+
static std::map<std::string, std::string> getcode(const Settings& settings, ErrorLogger& errorlogger, const char* code, std::size_t size, std::set<std::string> cfgs, const std::string &filename = "file.c", SuppressionList *inlineSuppression = nullptr);
189194
};
190195

191196
namespace cppcheck {
@@ -262,13 +267,6 @@ class SimpleTokenizer2 : public Tokenizer {
262267
preprocess(code, size-1, mFiles, file0, *this, errorlogger);
263268
}
264269

265-
// TODO: get rid of this
266-
SimpleTokenizer2(const Settings &settings, ErrorLogger &errorlogger, const char* code, std::size_t size, const std::string& file0)
267-
: Tokenizer{TokenList{settings, Path::identify(file0, false)}, errorlogger}
268-
{
269-
preprocess(code, size, mFiles, file0, *this, errorlogger);
270-
}
271-
272270
private:
273271
static void preprocess(const char* code, std::size_t size, std::vector<std::string> &files, const std::string& file0, Tokenizer& tokenizer, ErrorLogger& errorlogger);
274272

@@ -279,16 +277,11 @@ struct TokenListHelper
279277
{
280278
template<size_t size>
281279
static bool createTokensFromString(TokenList& tokenlist, const char (&code)[size], const std::string& file)
282-
{
283-
return createTokensFromBuffer(tokenlist, code, size-1, file);
284-
}
285-
286-
static bool createTokensFromBuffer(TokenList& tokenlist, const char* data, size_t size, const std::string& file)
287280
{
288281
if (tokenlist.front())
289282
throw std::runtime_error("token list is not empty");
290283
tokenlist.appendFileIfNew(file);
291-
return tokenlist.createTokensFromBuffer(data, size);
284+
return tokenlist.createTokensFromString(code);
292285
}
293286
};
294287

test/testother.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11711,7 +11711,9 @@ class TestOther : public TestFixture {
1171111711
}
1171211712

1171311713
#define checkCustomSettings(...) checkCustomSettings_(__FILE__, __LINE__, __VA_ARGS__)
11714-
void checkCustomSettings_(const char* file, int line, const char code[], bool cpp = true, bool inconclusive = true, bool runSimpleChecks=true, bool verbose=false, Settings* settings = nullptr) {
11714+
// TODO: use options
11715+
template<size_t size>
11716+
void checkCustomSettings_(const char* file, int line, const char (&code)[size], bool cpp = true, bool inconclusive = true, bool runSimpleChecks=true, bool verbose=false, Settings* settings = nullptr) {
1171511717
if (!settings) {
1171611718
settings = &_settings;
1171711719
}
@@ -11728,7 +11730,8 @@ class TestOther : public TestFixture {
1172811730
(void)runSimpleChecks; // TODO Remove this
1172911731
}
1173011732

11731-
void checkCustomSettings_(const char* file, int line, const char code[], Settings *s) {
11733+
template<size_t size>
11734+
void checkCustomSettings_(const char* file, int line, const char (&code)[size], Settings *s) {
1173211735
checkCustomSettings_(file, line, code, true, true, true, false, s);
1173311736
}
1173411737

test/testsimplifytemplate.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5443,7 +5443,7 @@ class TestSimplifyTemplate : public TestFixture {
54435443
unsigned int templateParameters(const char (&data)[size]) {
54445444
TokenList tokenlist{settings, Standards::Language::CPP};
54455445
tokenlist.appendFileIfNew("test.cpp");
5446-
if (!tokenlist.createTokensFromBuffer(data, size-1))
5446+
if (!tokenlist.createTokensFromString(data))
54475447
return false;
54485448
Tokenizer tokenizer(std::move(tokenlist), *this);
54495449
tokenizer.createLinks();
@@ -5513,7 +5513,7 @@ class TestSimplifyTemplate : public TestFixture {
55135513
TokenList tokenlist{settings, Standards::Language::CPP};
55145514

55155515
tokenlist.appendFileIfNew("test.cpp");
5516-
if (!tokenlist.createTokensFromBuffer(data, size-1))
5516+
if (!tokenlist.createTokensFromString(data))
55175517
return false;
55185518
Tokenizer tokenizer(std::move(tokenlist), *this);
55195519
tokenizer.createLinks();
@@ -5584,7 +5584,7 @@ class TestSimplifyTemplate : public TestFixture {
55845584
template<size_t size>
55855585
bool findTemplateDeclarationEndHelper(const char (&data)[size], const char pattern[], unsigned offset = 0) {
55865586
TokenList tokenlist{settings, Standards::Language::CPP};
5587-
if (!TokenListHelper::createTokensFromBuffer(tokenlist, data, size-1, "test.cpp"))
5587+
if (!TokenListHelper::createTokensFromString(tokenlist, data, "test.cpp"))
55885588
return false;
55895589
Tokenizer tokenizer(std::move(tokenlist), *this);
55905590
tokenizer.createLinks();
@@ -5615,7 +5615,7 @@ class TestSimplifyTemplate : public TestFixture {
56155615
bool getTemplateParametersInDeclarationHelper(const char (&data)[size], const std::vector<std::string> & params) {
56165616
TokenList tokenlist{settings, Standards::Language::CPP};
56175617

5618-
if (!TokenListHelper::createTokensFromBuffer(tokenlist, data, size-1, "test.cpp"))
5618+
if (!TokenListHelper::createTokensFromString(tokenlist, data, "test.cpp"))
56195619
return false;
56205620
Tokenizer tokenizer(std::move(tokenlist), *this);
56215621
tokenizer.createLinks();

test/testsimplifytypedef.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ class TestSimplifyTypedef : public TestFixture {
279279
template<size_t size>
280280
std::string simplifyTypedef(const char (&data)[size]) {
281281
TokenList tokenlist{settings1, Standards::Language::CPP};
282-
if (!tokenlist.createTokensFromBuffer(data, size-1))
282+
if (!tokenlist.createTokensFromString(data))
283283
return "";
284284
Tokenizer tokenizer(std::move(tokenlist), *this);
285285
tokenizer.createLinks();
@@ -314,7 +314,7 @@ class TestSimplifyTypedef : public TestFixture {
314314
std::string simplifyTypedefC(const char (&data)[size]) {
315315
TokenList tokenlist{settings1, Standards::Language::C};
316316

317-
if (!TokenListHelper::createTokensFromBuffer(tokenlist, data, size-1, "file.c"))
317+
if (!TokenListHelper::createTokensFromString(tokenlist, data, "file.c"))
318318
return "";
319319
Tokenizer tokenizer(std::move(tokenlist), *this);
320320
tokenizer.createLinks();
@@ -330,7 +330,7 @@ class TestSimplifyTypedef : public TestFixture {
330330
template<size_t size>
331331
std::string dumpTypedefInfo(const char (&code)[size]) {
332332
TokenList tokenlist{settings1, Standards::Language::C};
333-
if (!TokenListHelper::createTokensFromBuffer(tokenlist, code, size-1, "file.c"))
333+
if (!TokenListHelper::createTokensFromString(tokenlist, code, "file.c"))
334334
return {};
335335
Tokenizer tokenizer(std::move(tokenlist), *this);
336336
tokenizer.createLinks();

test/testtokenize.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ class TestTokenizer : public TestFixture {
497497
}
498498

499499
#define tokenizeAndStringify(...) tokenizeAndStringify_(__FILE__, __LINE__, __VA_ARGS__)
500+
// TODO: use options
500501
template<size_t size>
501502
std::string tokenizeAndStringify_(const char* file, int linenr, const char (&code)[size], bool expand = true, Platform::Type platform = Platform::Type::Native,
502503
bool cpp = true, Standards::cppstd_t cppstd = Standards::CPP11, Standards::cstd_t cstd = Standards::C11) {
@@ -6209,7 +6210,7 @@ class TestTokenizer : public TestFixture {
62096210
// tokenize given code..
62106211
TokenList tokenlist{settings0, Standards::Language::CPP};
62116212
tokenlist.appendFileIfNew("test.cpp");
6212-
if (!tokenlist.createTokensFromBuffer(data, size-1))
6213+
if (!tokenlist.createTokensFromString(data))
62136214
return "ERROR";
62146215

62156216
Tokenizer tokenizer(std::move(tokenlist), *this);
@@ -8201,9 +8202,9 @@ class TestTokenizer : public TestFixture {
82018202

82028203
void cpp11init() {
82038204
#define testIsCpp11init(...) testIsCpp11init_(__FILE__, __LINE__, __VA_ARGS__)
8204-
auto testIsCpp11init_ = [this](const char* file, int line, const char* code, const char* find, TokenImpl::Cpp11init expected) {
8205+
auto testIsCpp11init_ = [this](const char* file, int line, const std::string& code, const char* find, TokenImpl::Cpp11init expected) {
82058206
SimpleTokenizer tokenizer(settingsDefault, *this);
8206-
ASSERT_LOC(tokenizer.tokenize(code), file, line);
8207+
ASSERT_LOC(tokenizer.tokenize(code.data(), code.size()), file, line);
82078208

82088209
const Token* tok = Token::findsimplematch(tokenizer.tokens(), find, strlen(find));
82098210
ASSERT_LOC(tok, file, line);
@@ -8627,7 +8628,7 @@ class TestTokenizer : public TestFixture {
86278628
}
86288629

86298630
void dumpFallthrough() {
8630-
const char * code = "void f(int n) {\n"
8631+
const char code[] = "void f(int n) {\n"
86318632
" void g(), h(), i();\n"
86328633
" switch (n) {\n"
86338634
" case 1:\n"
@@ -8651,9 +8652,9 @@ class TestTokenizer : public TestFixture {
86518652
}
86528653

86538654
void simplifyRedundantParentheses() {
8654-
const char *code = "int f(struct S s) {\n"
8655-
" return g(1, &(int){ s.i });\n"
8656-
"}\n";
8655+
const char code[] = "int f(struct S s) {\n"
8656+
" return g(1, &(int){ s.i });\n"
8657+
"}\n";
86578658
SimpleTokenizer tokenizer(settingsDefault, *this, false);
86588659
ASSERT_NO_THROW(tokenizer.tokenize(code));
86598660
}

test/testuninitvar.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5468,7 +5468,8 @@ class TestUninitVar : public TestFixture {
54685468
TODO_ASSERT_EQUALS("", "[test.c:4:14]: (error) Uninitialized variable: d [legacyUninitvar]\n", errout_str());
54695469
}
54705470

5471-
void valueFlowUninit_(const char* file, int line, const char code[], bool cpp = true)
5471+
template<size_t size>
5472+
void valueFlowUninit_(const char* file, int line, const char (&code)[size], bool cpp = true)
54725473
{
54735474
// Tokenize..
54745475
const Settings s = settingsBuilder(settings).debugwarnings(false).build();
@@ -7913,7 +7914,8 @@ class TestUninitVar : public TestFixture {
79137914
ASSERT_EQUALS("", errout_str());
79147915
}
79157916

7916-
void ctu_(const char* file, int line, const char code[]) {
7917+
template<size_t size>
7918+
void ctu_(const char* file, int line, const char (&code)[size]) {
79177919
// Tokenize..
79187920
SimpleTokenizer tokenizer(settings, *this);
79197921
ASSERT_LOC(tokenizer.tokenize(code), file, line);

test/testunusedvar.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ class TestUnusedVar : public TestFixture {
289289
};
290290

291291
#define checkStructMemberUsage(...) checkStructMemberUsage_(__FILE__, __LINE__, __VA_ARGS__)
292-
void checkStructMemberUsage_(const char* file, int line, const char code[], const CheckStructMemberUsageOptions& options = make_default_obj()) {
292+
template<size_t size>
293+
void checkStructMemberUsage_(const char* file, int line, const char (&code)[size], const CheckStructMemberUsageOptions& options = make_default_obj()) {
293294
// Tokenize..
294295
SimpleTokenizer tokenizer(settings, *this, options.cpp);
295296
if (options.directives)

0 commit comments

Comments
 (0)