Various code cleanups

This commit is contained in:
PKEuS 2011-12-08 21:28:34 +01:00 committed by Daniel Marjamäki
parent e2dd085b60
commit 167a7e3e51
13 changed files with 65 additions and 94 deletions

View File

@ -159,7 +159,7 @@ void FileLister::recursiveAddFiles(std::vector<std::string> &filenames, std::map
oss << cleanedPath;
if (MyIsDirectory(cleanedPath.c_str())) {
if (MyIsDirectory(cleanedPath)) {
char c = cleanedPath[ cleanedPath.size()-1 ];
switch (c) {
case '\\':
@ -175,8 +175,7 @@ void FileLister::recursiveAddFiles(std::vector<std::string> &filenames, std::map
bdir << cleanedPath << '\\';
}
} else {
std::string::size_type pos;
pos = cleanedPath.find_last_of('\\');
std::string::size_type pos = cleanedPath.find_last_of('\\');
if (std::string::npos != pos) {
bdir << cleanedPath.substr(0, pos + 1);
}
@ -202,7 +201,7 @@ void FileLister::recursiveAddFiles(std::vector<std::string> &filenames, std::map
#endif // defined(UNICODE)
std::ostringstream fname;
fname << bdir.str().c_str() << ansiFfd;
fname << bdir.str() << ansiFfd;
if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
// File
@ -236,10 +235,7 @@ bool FileLister::isDirectory(const std::string &path)
bool FileLister::fileExists(const std::string &path)
{
if (MyFileExists(path) == TRUE)
return true;
else
return false;
return (MyFileExists(path) == TRUE);
}

View File

@ -46,9 +46,8 @@ namespace {
//---------------------------------------------------------------------------
void CheckBufferOverrun::arrayIndexOutOfBoundsError(const Token *tok, const ArrayInfo &arrayInfo, const std::vector<MathLib::bigint> &index)
static void makeArrayIndexOutOfBoundsError(std::ostream& oss, const CheckBufferOverrun::ArrayInfo &arrayInfo, const std::vector<MathLib::bigint> &index)
{
std::ostringstream oss;
oss << "Array '" << arrayInfo.varname();
for (unsigned int i = 0; i < arrayInfo.num().size(); ++i)
oss << "[" << arrayInfo.num(i) << "]";
@ -61,24 +60,18 @@ void CheckBufferOverrun::arrayIndexOutOfBoundsError(const Token *tok, const Arra
oss << "[" << index[i] << "]";
}
oss << " out of bounds";
}
void CheckBufferOverrun::arrayIndexOutOfBoundsError(const Token *tok, const ArrayInfo &arrayInfo, const std::vector<MathLib::bigint> &index)
{
std::ostringstream oss;
makeArrayIndexOutOfBoundsError(oss, arrayInfo, index);
reportError(tok, Severity::error, "arrayIndexOutOfBounds", oss.str());
}
void CheckBufferOverrun::arrayIndexOutOfBoundsError(const std::list<const Token *> &callstack, const ArrayInfo &arrayInfo, const std::vector<MathLib::bigint> &index)
{
std::ostringstream oss;
oss << "Array '" << arrayInfo.varname();
for (unsigned int i = 0; i < arrayInfo.num().size(); ++i)
oss << "[" << arrayInfo.num(i) << "]";
oss << "' index ";
if (index.size() == 1)
oss << index[0];
else {
oss << arrayInfo.varname();
for (unsigned int i = 0; i < index.size(); ++i)
oss << "[" << index[i] << "]";
}
oss << " out of bounds";
makeArrayIndexOutOfBoundsError(oss, arrayInfo, index);
reportError(callstack, Severity::error, "arrayIndexOutOfBounds", oss.str());
}
@ -542,7 +535,7 @@ void CheckBufferOverrun::checkFunctionParameter(const Token &tok, unsigned int p
total_size["fwrite"] = 1001; // parameter 2 * parameter 3
}
if (par == 2) {
else if (par == 2) {
total_size["read"] = 3;
total_size["pread"] = 3;
total_size["write"] = 3;
@ -928,7 +921,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
(varid == 0 && Token::Match(tok, ("strcpy|strcat ( " + varnames + " , %str% )").c_str()))) {
const std::size_t len = Token::getStrLength(tok->tokAt(varc + 4));
if (total_size > 0 && len >= (unsigned int)total_size) {
bufferOverrunError(tok, varid > 0 ? "" : varnames.c_str());
bufferOverrunError(tok, varid > 0 ? std::string("") : varnames);
continue;
}
} else if ((varid > 0 && Token::Match(tok, "strcpy|strcat ( %varid% , %var% )", varid)) ||
@ -1653,7 +1646,7 @@ void CheckBufferOverrun::checkSprintfCall(const Token *tok, const MathLib::bigin
std::list<const Token*> parameters;
const Token* vaArg = tok->tokAt(2)->nextArgument()->nextArgument();
while (vaArg) {
if (Token::Match(vaArg, "%any% [,)]")) {
if (Token::Match(vaArg->next(), "[,)]")) {
if (Token::Match(vaArg, "%str%"))
parameters.push_back(vaArg);

View File

@ -24,8 +24,6 @@
#include "errorlogger.h"
#include "symboldatabase.h"
#include <locale>
#include <cstring>
#include <string>
#include <sstream>
@ -487,7 +485,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<std::string>
else
break;
}
if (Token::Match(tok2, "%any% ="))
if (tok2 && tok2->strAt(1) == "=")
assignVar(ftok->str(), scope, usage);
}
@ -833,7 +831,7 @@ void CheckClass::checkReturnPtrThis(const Scope *scope, const Function *func, co
tok = tok->tokAt(4);
// check if a function is called
if (Token::Match(tok->next(), "%any% (") &&
if (tok->strAt(2) == "(" &&
tok->linkAt(2)->next()->str() == ";") {
std::list<Function>::const_iterator it;

View File

@ -24,7 +24,6 @@
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <set>
#include <stack>
@ -314,9 +313,9 @@ void CheckMemoryLeak::memoryLeak(const Token *tok, const std::string &varname, A
alloctype == CheckMemoryLeak::Pipe ||
alloctype == CheckMemoryLeak::Fd ||
alloctype == CheckMemoryLeak::Dir)
resourceLeakError(tok, varname.c_str());
resourceLeakError(tok, varname);
else
memleakError(tok, varname.c_str());
memleakError(tok, varname);
}
//---------------------------------------------------------------------------
@ -2553,7 +2552,7 @@ void CheckMemoryLeakInClass::variable(const Scope *scope, const Token *tokVarnam
// Function call .. possible deallocation
else if (Token::Match(tok->previous(), "[{};] %var% (")) {
if (!CheckMemoryLeakInFunction::test_white_list(tok->str().c_str())) {
if (!CheckMemoryLeakInFunction::test_white_list(tok->str())) {
return;
}
}
@ -2562,9 +2561,9 @@ void CheckMemoryLeakInClass::variable(const Scope *scope, const Token *tokVarnam
}
if (allocInConstructor && !deallocInDestructor) {
memoryLeak(tokVarname, (classname + "::" + varname).c_str(), Alloc);
memoryLeak(tokVarname, classname + "::" + varname, Alloc);
} else if (Alloc != CheckMemoryLeak::No && Dealloc == CheckMemoryLeak::No) {
memoryLeak(tokVarname, (classname + "::" + varname).c_str(), Alloc);
memoryLeak(tokVarname, classname + "::" + varname, Alloc);
}
}
@ -2707,7 +2706,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Token * const vartok
else if (tok3->str() == "}") {
if (indentlevel3 == 0) {
memoryLeak(tok3, (vartok->str() + "." + tok2->strAt(2)).c_str(), Malloc);
memoryLeak(tok3, vartok->str() + "." + tok2->strAt(2), Malloc);
break;
}
--indentlevel3;
@ -2739,7 +2738,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Token * const vartok
// Deallocating the struct..
else if (indentlevel2 == 0 && Token::Match(tok3, "free|kfree ( %varid% )", structid)) {
memoryLeak(tok3, (vartok->str() + "." + tok2->strAt(2)).c_str(), Malloc);
memoryLeak(tok3, vartok->str() + "." + tok2->strAt(2), Malloc);
break;
}
@ -2785,7 +2784,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Token * const vartok
// Returning from function without deallocating struct member?
if (!Token::Match(tok3, "return %varid% ;", structid) &&
!Token::Match(tok3, "return & %varid% .", structid)) {
memoryLeak(tok3, (vartok->str() + "." + tok2->strAt(2)).c_str(), Malloc);
memoryLeak(tok3, vartok->str() + "." + tok2->strAt(2), Malloc);
}
break;
}

View File

@ -789,14 +789,11 @@ bool CheckStl::isStlContainer(unsigned int varid)
if (Token::simpleMatch(type, "std ::"))
type = type->tokAt(2);
// all possible stl containers
static const char STL_CONTAINER_LIST[] = "bitset|deque|list|map|multimap|multiset|priority_queue|queue|set|stack|hash_map|hash_multimap|hash_set|vector";
// container template string
const std::string checkStr(std::string(STL_CONTAINER_LIST) + " <");
// all possible stl containers as a token
static const char STL_CONTAINER_LIST[] = "bitset|deque|list|map|multimap|multiset|priority_queue|queue|set|stack|hash_map|hash_multimap|hash_set|vector <";
// check if it's an stl template
if (Token::Match(type, checkStr.c_str()))
if (Token::Match(type, STL_CONTAINER_LIST))
return true;
}
@ -861,7 +858,7 @@ void CheckStl::size()
void CheckStl::sizeError(const Token *tok)
{
const std::string varname(tok ? tok->str().c_str() : "list");
const std::string varname(tok ? tok->str() : std::string("list"));
reportError(tok, Severity::performance, "stlSize",
"Possible inefficient checking for '" + varname + "' emptiness.\n"
"Checking for '" + varname + "' emptiness might be inefficient. "

View File

@ -417,7 +417,7 @@ static int doAssignment(Variables &variables, const Token *tok, bool dereference
if (Token::Match(tok->tokAt(start), "&| %var%") ||
Token::Match(tok->tokAt(start), "( const| struct|union| %type% *| ) &| %var%") ||
Token::Match(tok->tokAt(start), "( const| struct|union| %type% *| ) ( &| %var%") ||
Token::Match(tok->tokAt(start), "%any% < const| struct|union| %type% *| > ( &| %var%")) {
Token::Match(tok->tokAt(start+1), "< const| struct|union| %type% *| > ( &| %var%")) {
unsigned char offset = 0;
unsigned int varid2;
bool addressOf = false;

View File

@ -21,10 +21,8 @@
#include "mathlib.h"
#include "tokenize.h"
#include <fstream>
#include <string>
#include <cstring>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <cmath>
@ -70,7 +68,7 @@ double MathLib::toDoubleNumber(const std::string &str)
else if (isNullValue(str))
return 0.0;
// otherwise, convert to double
std::istringstream istr(str.c_str());
std::istringstream istr(str);
double ret;
istr >> ret;
return ret;

View File

@ -25,7 +25,6 @@
#include "settings.h"
#include <algorithm>
#include <stdexcept>
#include <sstream>
#include <fstream>
#include <cstdlib>
@ -590,7 +589,7 @@ std::string Preprocessor::removeParentheses(const std::string &str)
if (str.find("\n#if") == std::string::npos && str.compare(0, 3, "#if") != 0)
return str;
std::istringstream istr(str.c_str());
std::istringstream istr(str);
std::ostringstream ret;
std::string line;
while (std::getline(istr, line)) {
@ -836,7 +835,7 @@ void Preprocessor::preprocess(std::istream &srcCodeStream, std::string &processe
// Replace "defined A" with "defined(A)"
{
std::istringstream istr(processedFile.c_str());
std::istringstream istr(processedFile);
std::ostringstream ostr;
std::string line;
while (std::getline(istr, line)) {
@ -1183,7 +1182,7 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
if (s.find("&&") != std::string::npos) {
Tokenizer tokenizer(_settings, _errorLogger);
std::istringstream tempIstr(s.c_str());
std::istringstream tempIstr(s);
if (!tokenizer.tokenize(tempIstr, filename.c_str(), "", true)) {
std::ostringstream lineStream;
lineStream << __LINE__;
@ -1300,7 +1299,7 @@ void Preprocessor::simplifyCondition(const std::map<std::string, std::string> &c
{
Settings settings;
Tokenizer tokenizer(&settings, NULL);
std::istringstream istr(("(" + condition + ")").c_str());
std::istringstream istr("(" + condition + ")");
if (!tokenizer.tokenize(istr, "", "", true)) {
// If tokenize returns false, then there is syntax error in the
// code which we can't handle. So stop here.
@ -1519,7 +1518,7 @@ std::string Preprocessor::getcode(const std::string &filedata, const std::string
if (line.find("=") != std::string::npos) {
Tokenizer tokenizer(settings, NULL);
line.erase(0, sizeof("#pragma endasm"));
std::istringstream tempIstr(line.c_str());
std::istringstream tempIstr(line);
tokenizer.tokenize(tempIstr, "");
if (Token::Match(tokenizer.tokens(), "( %var% = %any% )")) {
ret << "asm(" << tokenizer.tokens()->strAt(1) << ");";
@ -2210,7 +2209,7 @@ public:
tokenizer.setSettings(&settings);
// Tokenize the macro to make it easier to handle
std::istringstream istr(macro.c_str());
std::istringstream istr(macro);
tokenizer.createTokens(istr);
// macro name..
@ -2481,7 +2480,7 @@ std::string Preprocessor::expandMacros(const std::string &code, std::string file
std::ostringstream ostr;
// read code..
std::istringstream istr(code.c_str());
std::istringstream istr(code);
std::string line;
while (getlines(istr, line)) {
// defining a macro..

View File

@ -129,7 +129,7 @@ void Tokenizer::addtoken(const char str[], const unsigned int lineno, const unsi
}
if (_tokensBack) {
_tokensBack->insertToken(str2.str().c_str());
_tokensBack->insertToken(str2.str());
} else {
_tokens = new Token(&_tokensBack);
_tokensBack = _tokens;
@ -145,20 +145,12 @@ void Tokenizer::addtoken(const Token * tok, const unsigned int lineno, const uns
if (tok == 0)
return;
// Replace hexadecimal value with decimal
std::ostringstream str2;
if (strncmp(tok->str().c_str(), "0x", 2) == 0) {
str2 << std::strtoul(tok->str().c_str() + 2, NULL, 16);
} else {
str2 << tok->str();
}
if (_tokensBack) {
_tokensBack->insertToken(str2.str().c_str());
_tokensBack->insertToken(tok->str());
} else {
_tokens = new Token(&_tokensBack);
_tokensBack = _tokens;
_tokensBack->str(str2.str());
_tokensBack->str(tok->str());
}
_tokensBack->linenr(lineno);
@ -330,7 +322,7 @@ void Tokenizer::createTokens(std::istream &code)
bool foundOurfile = false;
fileIndexes.push_back(FileIndex);
for (unsigned int i = 0; i < _files.size(); ++i) {
if (Path::sameFileName(_files[i].c_str(), line.c_str())) {
if (Path::sameFileName(_files[i], line)) {
// Use this index
foundOurfile = true;
FileIndex = i;
@ -759,7 +751,7 @@ static Token *splitDefinitionFromTypedef(Token *tok)
static unsigned int count = 0;
name = "Unnamed" + MathLib::toString<unsigned int>(count++);
}
tok->next()->insertToken(name.c_str());
tok->next()->insertToken(name);
} else
return NULL;
} else if (tok->strAt(3) == ":") {
@ -798,7 +790,7 @@ static Token *splitDefinitionFromTypedef(Token *tok)
}
tok1->insertToken(tok->next()->str()); // struct, union or enum
tok1 = tok1->next();
tok1->insertToken(name.c_str());
tok1->insertToken(name);
tok->deleteThis();
tok = tok3;
}
@ -1849,7 +1841,7 @@ void Tokenizer::simplifyTypedef()
}
}
void Tokenizer::simplifyMulAnd(void)
void Tokenizer::simplifyMulAnd()
{
for (Token *tok = _tokens; tok; tok = tok->next()) {
if (Token::Match(tok, "[;{}] *")) {
@ -5141,10 +5133,10 @@ void Tokenizer::simplifyConditionOperator()
while (pos1 != std::string::npos) {
std::string::size_type pos2 = str.find(" ", pos1);
if (pos2 == std::string::npos) {
tok->insertToken(str.substr(pos1).c_str());
tok->insertToken(str.substr(pos1));
pos1 = pos2;
} else {
tok->insertToken(str.substr(pos1, pos2 - pos1).c_str());
tok->insertToken(str.substr(pos1, pos2 - pos1));
pos1 = pos2 + 1;
}
tok = tok->next();
@ -5661,7 +5653,7 @@ bool Tokenizer::simplifyFunctionReturn()
static void incdec(std::string &value, const std::string &op)
{
int ivalue = 0;
std::istringstream istr(value.c_str());
std::istringstream istr(value);
istr >> ivalue;
if (op == "++")
++ivalue;
@ -6311,7 +6303,7 @@ void Tokenizer::simplifyIfNot()
// if( foo(x) == 0 )
if (Token::Match(tok->link()->tokAt(-2), "( %var%")) {
tok->deleteNext(2);
tok->link()->previous()->insertToken(tok->link()->previous()->str().c_str());
tok->link()->previous()->insertToken(tok->link()->previous()->str());
tok->link()->tokAt(-2)->str("!");
}
@ -7649,7 +7641,7 @@ void Tokenizer::simplifyGoto()
if (indentlevel == 1 && lev == 0)
ret2 = true;
}
token->insertToken(tok2->str().c_str());
token->insertToken(tok2->str());
token = token->next();
token->linenr(tok2->linenr());
token->varId(tok2->varId());
@ -8140,7 +8132,7 @@ void Tokenizer::simplifyEnum()
}
if (enumType) {
const std::string pattern(className.empty() ? "" : (className + " :: " + enumType->str()).c_str());
const std::string pattern(className.empty() ? std::string("") : (className + " :: " + enumType->str()));
// count { and } for tok2
int level = 0;
@ -9117,7 +9109,7 @@ void Tokenizer::simplifyStructDecl()
tok->insertToken("static");
tok = tok->next();
}
tok->insertToken(type->str().c_str());
tok->insertToken(type->str());
}
tok = restart;
@ -9138,11 +9130,11 @@ void Tokenizer::simplifyStructDecl()
name = "Anonymous" + MathLib::toString<unsigned int>(count++);
tok1->insertToken(name.c_str());
tok1->insertToken(name);
tok->insertToken(";");
tok = tok->next();
tok->insertToken(name.c_str());
tok->insertToken(name);
}
// unnamed anonymous struct/union so possibly remove it

View File

@ -49,7 +49,7 @@ private:
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code.c_str());
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();

View File

@ -30,7 +30,6 @@
#include <map>
#include <string>
#include <sstream>
#include <stdexcept>
extern std::ostringstream errout;
extern std::ostringstream output;
@ -2294,7 +2293,7 @@ private:
void unicodeInComment() {
const std::string filedata("//\xC8");
std::istringstream istr(filedata.c_str());
std::istringstream istr(filedata);
Settings settings;
Preprocessor preprocessor(&settings, this);
ASSERT_EQUALS("", preprocessor.read(istr, "test.cpp", 0));
@ -2302,7 +2301,7 @@ private:
void unicodeInString() {
const std::string filedata("\"\xC8\"");
std::istringstream istr(filedata.c_str());
std::istringstream istr(filedata);
Settings settings;
Preprocessor preprocessor(&settings, this);
ASSERT_EQUALS(filedata, preprocessor.read(istr, "test.cpp", 0));
@ -2716,7 +2715,7 @@ private:
void testPreprocessorRead1() {
const std::string filedata("/*\n*/ # /*\n*/ defi\\\nne FO\\\nO 10\\\n20");
std::istringstream istr(filedata.c_str());
std::istringstream istr(filedata);
Settings settings;
Preprocessor preprocessor(&settings, this);
ASSERT_EQUALS("#define FOO 1020", preprocessor.read(istr, "test.cpp", 0));
@ -2724,7 +2723,7 @@ private:
void testPreprocessorRead2() {
const std::string filedata("\"foo\\\\\nbar\"");
std::istringstream istr(filedata.c_str());
std::istringstream istr(filedata);
Settings settings;
Preprocessor preprocessor(&settings, this);
ASSERT_EQUALS("\"foo\\bar\"", preprocessor.read(istr, "test.cpp", 0));
@ -2732,7 +2731,7 @@ private:
void testPreprocessorRead3() {
const std::string filedata("#define A \" a \"\n\" b\"");
std::istringstream istr(filedata.c_str());
std::istringstream istr(filedata);
Settings settings;
Preprocessor preprocessor(&settings, this);
ASSERT_EQUALS(filedata, preprocessor.read(istr, "test.cpp", 0));
@ -2742,7 +2741,7 @@ private:
{
// test < \\> < > (unescaped)
const std::string filedata("#define A \" \\\\\"/*space*/ \" \"");
std::istringstream istr(filedata.c_str());
std::istringstream istr(filedata);
Settings settings;
Preprocessor preprocessor(&settings, this);
ASSERT_EQUALS("#define A \" \\\\\" \" \"", preprocessor.read(istr, "test.cpp", 0));
@ -2751,7 +2750,7 @@ private:
{
// test <" \\\" "> (unescaped)
const std::string filedata("#define A \" \\\\\\\" \"");
std::istringstream istr(filedata.c_str());
std::istringstream istr(filedata);
Settings settings;
Preprocessor preprocessor(&settings, this);
ASSERT_EQUALS("#define A \" \\\\\\\" \"", preprocessor.read(istr, "test.cpp", 0));
@ -2760,7 +2759,7 @@ private:
{
// test <" \\\\"> <" "> (unescaped)
const std::string filedata("#define A \" \\\\\\\\\"/*space*/ \" \"");
std::istringstream istr(filedata.c_str());
std::istringstream istr(filedata);
Settings settings;
Preprocessor preprocessor(&settings, this);
ASSERT_EQUALS("#define A \" \\\\\\\\\" \" \"", preprocessor.read(istr, "test.cpp", 0));
@ -2775,7 +2774,7 @@ private:
std::string processedFile;
std::list<std::string> cfg;
std::list<std::string> paths;
preprocessor.preprocess(src, processedFile, cfg, "", paths); // don't hang
preprocessor.preprocess(src, processedFile, cfg, "", paths); // don't hang
}
void missingInclude() {

View File

@ -127,7 +127,7 @@ private:
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code.c_str());
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();

View File

@ -5101,7 +5101,7 @@ private:
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code.c_str());
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
std::ostringstream ostr;