Skip to content

Commit ec1bd42

Browse files
committed
Refactorizations optimizing std::string usage:
1) Added global static const std::string emptyString; object: -> Replaces some static variables in functions which might be not threadsafe -> Avoids constructor call (std::string::string("")) -> Even functions that return an empty string in some branches can return by reference now. Added to config.h to ensure that it is available everywhere 2) Added overloads for TestFixture::assertEquals for the most common use cases: -> Moves conversion from const char[] to std::string into a function, reducing code duplication in binary.
1 parent feefa4c commit ec1bd42

15 files changed

Lines changed: 43 additions & 31 deletions

lib/checkbufferoverrun.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
10691069
(declarationId == 0 && Token::Match(tok, ("strcpy|strcat ( " + varnames + " , %str% )").c_str()))) {
10701070
const std::size_t len = Token::getStrLength(tok->tokAt(varcount + 4));
10711071
if (total_size > 0 && len >= (unsigned int)total_size) {
1072-
bufferOverrunError(tok, declarationId > 0 ? std::string() : varnames);
1072+
bufferOverrunError(tok, declarationId > 0 ? emptyString : varnames);
10731073
continue;
10741074
}
10751075
} else if ((declarationId > 0 && Token::Match(tok, "strcpy|strcat ( %varid% , %var% )", declarationId)) ||

lib/checkbufferoverrun.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ class CPPCHECKLIB CheckBufferOverrun : public Check {
210210

211211
static bool isArrayOfStruct(const Token* tok, int &position);
212212
void arrayIndexOutOfBoundsError(const std::list<const Token *> &callstack, const ArrayInfo &arrayInfo, const std::vector<MathLib::bigint> &index);
213-
void bufferOverrunError(const Token *tok, const std::string &varnames = "");
214-
void bufferOverrunError(const std::list<const Token *> &callstack, const std::string &varnames = "");
213+
void bufferOverrunError(const Token *tok, const std::string &varnames = emptyString);
214+
void bufferOverrunError(const std::list<const Token *> &callstack, const std::string &varnames = emptyString);
215215
void strncatUsageError(const Token *tok);
216216
void negativeMemoryAllocationSizeError(const Token *tok); // provide a negative value to memory allocation function
217217
void outOfBoundsError(const Token *tok, const std::string &what, const bool show_size_info, const MathLib::bigint &supplied_size, const MathLib::bigint &actual_size);

lib/config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@
1919
# include <crtdbg.h>
2020
#endif
2121

22+
#include <string>
23+
static const std::string emptyString;
24+
2225
#endif // configH

lib/errorlogger.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ class CPPCHECKLIB ErrorLogger {
217217
* @param outputFormat Empty string to use default output format
218218
* or template to be used. E.g. "{file}:{line},{severity},{id},{message}"
219219
* @return formatted string
220-
*/
221-
std::string toString(bool verbose, const std::string &outputFormat = "") const;
220+
*/
221+
std::string toString(bool verbose, const std::string &outputFormat = emptyString) const;
222222

223223
std::string serialize() const;
224224
bool deserialize(const std::string &data);

lib/library.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ class CPPCHECKLIB Library {
184184

185185
bool isargvalid(const std::string &functionName, int argnr, const MathLib::bigint argvalue) const;
186186

187-
std::string validarg(const std::string &functionName, int argnr) const {
187+
const std::string& validarg(const std::string &functionName, int argnr) const {
188188
const ArgumentChecks *arg = getarg(functionName, argnr);
189-
return arg ? arg->valid : std::string("");
189+
return arg ? arg->valid : emptyString;
190190
}
191191

192192
bool markupFile(const std::string &path) const {
@@ -227,24 +227,24 @@ class CPPCHECKLIB Library {
227227
return offset;
228228
}
229229

230-
std::string blockstart(const std::string &file) const {
230+
const std::string& blockstart(const std::string &file) const {
231231
const std::map<std::string, CodeBlock>::const_iterator map_it
232232
= _executableblocks.find(Path::getFilenameExtensionInLowerCase(file));
233233

234234
if (map_it != _executableblocks.end()) {
235235
return map_it->second.start();
236236
}
237-
return std::string();
237+
return emptyString;
238238
}
239239

240-
std::string blockend(const std::string &file) const {
240+
const std::string& blockend(const std::string &file) const {
241241
const std::map<std::string, CodeBlock>::const_iterator map_it
242242
= _executableblocks.find(Path::getFilenameExtensionInLowerCase(file));
243243

244244
if (map_it != _executableblocks.end()) {
245245
return map_it->second.end();
246246
}
247-
return std::string();
247+
return emptyString;
248248
}
249249

250250
bool iskeyword(const std::string &file, const std::string &keyword) const {

lib/preprocessor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ void Preprocessor::preprocess(std::istream &srcCodeStream, std::string &processe
10401040
processedFile = ostr.str();
10411041
}
10421042

1043-
std::map<std::string, std::string> defs(getcfgmap(_settings ? _settings->userDefines : std::string(""), _settings, filename));
1043+
std::map<std::string, std::string> defs(getcfgmap(_settings ? _settings->userDefines : emptyString, _settings, filename));
10441044

10451045
if (_settings && _settings->_maxConfigs == 1U) {
10461046
std::set<std::string> pragmaOnce;

lib/suppressions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class CPPCHECKLIB Suppressions {
9999
* @param line number, e.g. "123"
100100
* @return error message. empty upon success
101101
*/
102-
std::string addSuppression(const std::string &errorId, const std::string &file = "", unsigned int line = 0);
102+
std::string addSuppression(const std::string &errorId, const std::string &file = emptyString, unsigned int line = 0);
103103

104104
/**
105105
* @brief Returns true if this message should not be shown to the user.

lib/symboldatabase.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ class CPPCHECKLIB Type {
104104
}
105105

106106
const std::string& name() const {
107-
static const std::string empty;
108-
return classDef->next()->isName() ? classDef->strAt(1) : empty;
107+
return classDef->next()->isName() ? classDef->strAt(1) : emptyString;
109108
}
110109

111110
const Token *initBaseInfo(const Token *tok, const Token *tok1);
@@ -215,13 +214,11 @@ class CPPCHECKLIB Variable {
215214
* @return name string
216215
*/
217216
const std::string &name() const {
218-
static const std::string noname;
219-
220217
// name may not exist for function arguments
221218
if (_name)
222219
return _name->str();
223220

224-
return noname;
221+
return emptyString;
225222
}
226223

227224
/**

lib/token.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,8 @@ const Token *Token::linkAt(int index) const
266266

267267
const std::string &Token::strAt(int index) const
268268
{
269-
static const std::string empty_str;
270-
271269
const Token *tok = this->tokAt(index);
272-
return tok ? tok->_str : empty_str;
270+
return tok ? tok->_str : emptyString;
273271
}
274272

275273
static int multiComparePercent(const Token *tok, const char ** haystack_p,

lib/tokenize.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class CPPCHECKLIB Tokenizer {
9090
*/
9191
bool tokenize(std::istream &code,
9292
const char FileName[],
93-
const std::string &configuration = "",
93+
const std::string &configuration = emptyString,
9494
bool noSymbolDB_AST = false);
9595
/**
9696
* tokenize condition and run simple simplifications on it

0 commit comments

Comments
 (0)