Skip to content

Commit

Permalink
pass a char buffer to simplecpp instead of a stream
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed May 3, 2024
1 parent b51a458 commit b4790e4
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 71 deletions.
2 changes: 1 addition & 1 deletion lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ unsigned int CppCheck::checkInternal(const std::string& filename, const std::str
TokenList tokenlist(&mSettings);
std::istringstream istr2(code);
// TODO: asserts when file has unknown extension
tokenlist.createTokens(istr2, Path::identify(*files.begin())); // TODO: check result?
tokenlist.createTokens(code.data(), code.size(), Path::identify(*files.begin())); // TODO: check result?
executeRules("define", tokenlist);
}
#endif
Expand Down
8 changes: 3 additions & 5 deletions lib/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include <cstring>
#include <list>
#include <memory>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
Expand All @@ -55,8 +54,8 @@ static std::vector<std::string> getnames(const char *names)

static void gettokenlistfromvalid(const std::string& valid, bool cpp, TokenList& tokenList)
{
std::istringstream istr(valid + ',');
tokenList.createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
const std::string str(valid + ',');
tokenList.createTokens(str.data(), str.size(), cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
for (Token *tok = tokenList.front(); tok; tok = tok->next()) {
if (Token::Match(tok,"- %num%")) {
tok->str("-" + tok->strAt(1));
Expand Down Expand Up @@ -1769,8 +1768,7 @@ std::shared_ptr<Token> createTokenFromExpression(const std::string& returnValue,
std::shared_ptr<TokenList> tokenList = std::make_shared<TokenList>(&settings);
{
const std::string code = "return " + returnValue + ";";
std::istringstream istr(code);
if (!tokenList->createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C))
if (!tokenList->createTokens(code.data(), code.size(), cpp ? Standards::Language::CPP : Standards::Language::C))
return nullptr;
}

Expand Down
8 changes: 4 additions & 4 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7558,8 +7558,8 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
if (!typestr.empty()) {
ValueType valuetype;
TokenList tokenList(&mSettings);
std::istringstream istr(typestr+";");
tokenList.createTokens(istr, tok->isCpp() ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
const std::string str(typestr+";");
tokenList.createTokens(str.data(), str.size(), tok->isCpp() ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
tokenList.simplifyStdType();
if (parsedecl(tokenList.front(), &valuetype, mDefaultSignedness, mSettings)) {
valuetype.originalTypeName = typestr;
Expand Down Expand Up @@ -7648,8 +7648,8 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
continue;
}
TokenList tokenList(&mSettings);
std::istringstream istr(typestr+";");
if (tokenList.createTokens(istr, tok->isCpp() ? Standards::Language::CPP : Standards::Language::C)) {
const std::string str(typestr+";");
if (tokenList.createTokens(str.data(), str.size(), tok->isCpp() ? Standards::Language::CPP : Standards::Language::C)) {
ValueType vt;
tokenList.simplifyPlatformTypes();
tokenList.simplifyStdType();
Expand Down
8 changes: 3 additions & 5 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@
#include <memory>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <type_traits>
#include <unordered_map>
Expand Down Expand Up @@ -3871,8 +3870,7 @@ static bool isNotEqual(std::pair<const Token*, const Token*> x, std::pair<const
static bool isNotEqual(std::pair<const Token*, const Token*> x, const std::string& y, bool cpp)
{
TokenList tokenList(nullptr);
std::istringstream istr(y);
tokenList.createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
tokenList.createTokens(y.data(), y.size(), cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
return isNotEqual(x, std::make_pair(tokenList.front(), tokenList.back()));
}
static bool isNotEqual(std::pair<const Token*, const Token*> x, const ValueType* y, bool cpp)
Expand Down Expand Up @@ -9250,8 +9248,8 @@ static bool getMinMaxValues(const ValueType *vt, const Platform &platform, MathL
static bool getMinMaxValues(const std::string &typestr, const Settings &settings, bool cpp, MathLib::bigint &minvalue, MathLib::bigint &maxvalue)
{
TokenList typeTokens(&settings);
std::istringstream istr(typestr+";");
if (!typeTokens.createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C))
const std::string str(typestr+";");
if (!typeTokens.createTokens(str.data(), str.size(), cpp ? Standards::Language::CPP : Standards::Language::C))
return false;
typeTokens.simplifyPlatformTypes();
typeTokens.simplifyStdType();
Expand Down
7 changes: 3 additions & 4 deletions test/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,10 @@ class SimpleTokenizer : public Tokenizer {
class SimpleTokenList
{
public:

explicit SimpleTokenList(const char code[], Standards::Language lang = Standards::Language::CPP)
template<size_t size>
SimpleTokenList(const char (&data)[size], Standards::Language lang = Standards::Language::CPP)
{
std::istringstream iss(code);
if (!list.createTokens(iss, lang))
if (!list.createTokens(data, size-1, lang))
throw std::runtime_error("creating tokens failed");
}

Expand Down
20 changes: 10 additions & 10 deletions test/testlibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ class TestLibrary : public TestFixture {
"</def>";

TokenList tokenList(&settings);
std::istringstream istr("foo();"); // <- too few arguments, not library function
ASSERT(tokenList.createTokens(istr, Standards::Language::CPP));
const char code[] = "foo();"; // <- too few arguments, not library function
ASSERT(tokenList.createTokens(code, Standards::Language::CPP));
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
tokenList.createAst();

Expand All @@ -184,35 +184,35 @@ class TestLibrary : public TestFixture {

{
TokenList tokenList(&settings);
std::istringstream istr("foo();"); // <- too few arguments, not library function
ASSERT(tokenList.createTokens(istr, Standards::Language::CPP));
const char code[] = "foo();"; // <- too few arguments, not library function
ASSERT(tokenList.createTokens(code, Standards::Language::CPP));
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
tokenList.createAst();

ASSERT(library.isNotLibraryFunction(tokenList.front()));
}
{
TokenList tokenList(&settings);
std::istringstream istr("foo(a);"); // <- library function
ASSERT(tokenList.createTokens(istr, Standards::Language::CPP));
const char code[] = "foo(a);"; // <- library function
ASSERT(tokenList.createTokens(code, Standards::Language::CPP));
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
tokenList.createAst();

ASSERT(!library.isNotLibraryFunction(tokenList.front()));
}
{
TokenList tokenList(&settings);
std::istringstream istr("foo(a, b);"); // <- library function
ASSERT(tokenList.createTokens(istr, Standards::Language::CPP));
const char code[] = "foo(a, b);"; // <- library function
ASSERT(tokenList.createTokens(code, Standards::Language::CPP));
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
tokenList.createAst();

ASSERT(!library.isNotLibraryFunction(tokenList.front()));
}
{
TokenList tokenList(&settings);
std::istringstream istr("foo(a, b, c);"); // <- too much arguments, not library function
ASSERT(tokenList.createTokens(istr, Standards::Language::CPP));
const char code[] = "foo(a, b, c);"; // <- too much arguments, not library function
ASSERT(tokenList.createTokens(code, Standards::Language::CPP));
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
tokenList.createAst();

Expand Down
24 changes: 12 additions & 12 deletions test/testsimplifytemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5301,11 +5301,11 @@ class TestSimplifyTemplate : public TestFixture {
"C<B<int>> y;"));
}

unsigned int templateParameters(const char code[]) {
template<size_t size>
unsigned int templateParameters(const char (&data)[size]) {
Tokenizer tokenizer(settings, *this);

std::istringstream istr(code);
if (!tokenizer.list.createTokens(istr, "test.cpp"))
if (!tokenizer.list.createTokens(data, size-1, "test.cpp"))
return false;
tokenizer.createLinks();
tokenizer.splitTemplateRightAngleBrackets(false);
Expand Down Expand Up @@ -5369,11 +5369,11 @@ class TestSimplifyTemplate : public TestFixture {
}

// Helper function to unit test TemplateSimplifier::getTemplateNamePosition
int templateNamePositionHelper(const char code[], unsigned offset = 0) {
template<size_t size>
int templateNamePositionHelper(const char (&data)[size], unsigned offset = 0) {
Tokenizer tokenizer(settings, *this);

std::istringstream istr(code);
if (!tokenizer.list.createTokens(istr, "test.cpp"))
if (!tokenizer.list.createTokens(data, size-1, "test.cpp"))
return false;
tokenizer.createLinks();
tokenizer.splitTemplateRightAngleBrackets(false);
Expand Down Expand Up @@ -5440,11 +5440,11 @@ class TestSimplifyTemplate : public TestFixture {
}

// Helper function to unit test TemplateSimplifier::findTemplateDeclarationEnd
bool findTemplateDeclarationEndHelper(const char code[], const char pattern[], unsigned offset = 0) {
template<size_t size>
bool findTemplateDeclarationEndHelper(const char (&data)[size], const char pattern[], unsigned offset = 0) {
Tokenizer tokenizer(settings, *this);

std::istringstream istr(code);
if (!tokenizer.list.createTokens(istr, "test.cpp"))
if (!tokenizer.list.createTokens(data, size-1, "test.cpp"))
return false;
tokenizer.createLinks();
tokenizer.splitTemplateRightAngleBrackets(false);
Expand All @@ -5470,11 +5470,11 @@ class TestSimplifyTemplate : public TestFixture {
}

// Helper function to unit test TemplateSimplifier::getTemplateParametersInDeclaration
bool getTemplateParametersInDeclarationHelper(const char code[], const std::vector<std::string> & params) {
template<size_t size>
bool getTemplateParametersInDeclarationHelper(const char (&data)[size], const std::vector<std::string> & params) {
Tokenizer tokenizer(settings, *this);

std::istringstream istr(code);
if (!tokenizer.list.createTokens(istr, "test.cpp"))
if (!tokenizer.list.createTokens(data, size-1, "test.cpp"))
return false;
tokenizer.createLinks();
tokenizer.splitTemplateRightAngleBrackets(false);
Expand Down
37 changes: 17 additions & 20 deletions test/testsimplifytypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,11 @@ class TestSimplifyTypedef : public TestFixture {
return tokenizer.tokens()->stringifyList(nullptr, !simplify);
}

std::string simplifyTypedef(const char code[]) {
template<size_t size>
std::string simplifyTypedef(const char (&data)[size]) {
Tokenizer tokenizer(settings1, *this);

std::istringstream istr(code);
if (!tokenizer.list.createTokens(istr, Standards::Language::CPP))
if (!tokenizer.list.createTokens(data, size-1, Standards::Language::CPP))
return "";
tokenizer.createLinks();
tokenizer.simplifyTypedef();
Expand Down Expand Up @@ -284,11 +284,11 @@ class TestSimplifyTypedef : public TestFixture {
}


std::string simplifyTypedefC(const char code[]) {
template<size_t size>
std::string simplifyTypedefC(const char (&data)[size]) {
Tokenizer tokenizer(settings1, *this);

std::istringstream istr(code);
if (!tokenizer.list.createTokens(istr, "file.c"))
if (!tokenizer.list.createTokens(data, size-1, "file.c"))
return "";
tokenizer.createLinks();
tokenizer.simplifyTypedef();
Expand Down Expand Up @@ -465,23 +465,21 @@ class TestSimplifyTypedef : public TestFixture {
}

void carray3() {
const char* code{};
code = "typedef int a[256];\n" // #11689
"typedef a b[256];\n"
"b* p;\n";
const char code[] = "typedef int a[256];\n" // #11689
"typedef a b[256];\n"
"b* p;\n";
ASSERT_EQUALS("int ( * p ) [ 256 ] [ 256 ] ;", simplifyTypedef(code));

code = "typedef int a[1];\n"
"typedef a b[2];\n"
"typedef b c[3];\n"
"c* p;\n";
ASSERT_EQUALS("int ( * p ) [ 3 ] [ 2 ] [ 1 ] ;", simplifyTypedef(code));
const char code1[] = "typedef int a[1];\n"
"typedef a b[2];\n"
"typedef b c[3];\n"
"c* p;\n";
ASSERT_EQUALS("int ( * p ) [ 3 ] [ 2 ] [ 1 ] ;", simplifyTypedef(code1));
}

void carray4() {
const char* code{};
code = "typedef int arr[12];\n" // #12019
"void foo() { arr temp = {0}; }\n";
const char code[] = "typedef int arr[12];\n" // #12019
"void foo() { arr temp = {0}; }\n";
ASSERT_EQUALS("void foo ( ) { int temp [ 12 ] = { 0 } ; }", tok(code));
}

Expand Down Expand Up @@ -4222,8 +4220,7 @@ class TestSimplifyTypedef : public TestFixture {
"void test(rFunctionPointer_fp functionPointer);";

Tokenizer tokenizer(settings1, *this);
std::istringstream istr(code);
ASSERT(tokenizer.list.createTokens(istr, "file.c"));
ASSERT(tokenizer.list.createTokens(code, "file.c"));
tokenizer.createLinks();
tokenizer.simplifyTypedef();

Expand Down
4 changes: 2 additions & 2 deletions test/testsuppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ class TestSuppressions : public TestFixture {
settings.exitCode = 1;

const char code[] = "int f() { int a; return a; }";
ASSERT_EQUALS(0, cppCheck.check("test.c", code)); // <- no unsuppressed error is seen
ASSERT_EQUALS(0, cppCheck.check("test.c", reinterpret_cast<const uint8_t*>(code), sizeof(code))); // <- no unsuppressed error is seen
ASSERT_EQUALS("[test.c:1]: (error) Uninitialized variable: a\n", errout_str()); // <- report error so ThreadExecutor can suppress it and make sure the global suppression is matched.
}

Expand Down Expand Up @@ -1226,7 +1226,7 @@ class TestSuppressions : public TestFixture {
" // cppcheck-suppress unusedStructMember\n"
" int y;\n"
"};";
ASSERT_EQUALS(0, cppCheck.check("/somewhere/test.cpp", code));
ASSERT_EQUALS(0, cppCheck.check("/somewhere/test.cpp", reinterpret_cast<const uint8_t*>(code), sizeof(code)));
ASSERT_EQUALS("",errout_str());
}

Expand Down
6 changes: 3 additions & 3 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5925,11 +5925,11 @@ class TestTokenizer : public TestFixture {
Z3
};

std::string testAst(const char code[], AstStyle style = AstStyle::Simple) {
template<size_t size>
std::string testAst(const char (&data)[size], AstStyle style = AstStyle::Simple) {
// tokenize given code..
Tokenizer tokenizer(settings0, *this);
std::istringstream istr(code);
if (!tokenizer.list.createTokens(istr,"test.cpp"))
if (!tokenizer.list.createTokens(data, size-1,"test.cpp"))
return "ERROR";

tokenizer.combineStringAndCharLiterals();
Expand Down
7 changes: 2 additions & 5 deletions test/testtokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "token.h"
#include "tokenlist.h"

#include <sstream>
#include <string>

#include <simplecpp.h>
Expand Down Expand Up @@ -122,8 +121,7 @@ class TestTokenList : public TestFixture {
const char code2[] = "_Generic"; // C11 keyword
const Settings s = settingsBuilder().c(Standards::C89).build();
TokenList tokenlist(&s);
std::istringstream istr(code2);
ASSERT(tokenlist.createTokens(istr, "a.c"));
ASSERT(tokenlist.createTokens(code2, "a.c"));
ASSERT_EQUALS(false, tokenlist.front()->isKeyword());
}

Expand All @@ -143,8 +141,7 @@ class TestTokenList : public TestFixture {
const char code2[] = "noexcept"; // C++11 keyword
const Settings s = settingsBuilder().cpp(Standards::CPP03).build();
TokenList tokenlist(&s);
std::istringstream istr(code2);
ASSERT(tokenlist.createTokens(istr, "a.cpp"));
ASSERT(tokenlist.createTokens(code2, "a.cpp"));
ASSERT_EQUALS(false, tokenlist.front()->isKeyword());
}
}
Expand Down

0 comments on commit b4790e4

Please sign in to comment.