Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1959,6 +1959,35 @@ static bool isLocal(const Token *tok)
return var && !var->isStatic() && var->isLocal();
}

static bool isc_strConcat(const Token* tok)
{
if (!Token::simpleMatch(tok, "+"))
return false;
const Token* cstr = nullptr;
for (const Token* op : { tok->astOperand1(), tok->astOperand2() }) {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upstream issue: llvm/llvm-project#185593

if (!Token::simpleMatch(op, "("))
continue;
const Token* dot = op->astOperand1();
if (!Token::simpleMatch(dot, "."))
continue;
if (!Token::Match(dot->astOperand1(), "%var%")) // TODO: function returning string
Comment thread
chrchr-github marked this conversation as resolved.
Outdated
continue;
const Variable* var = dot->astOperand1()->variable();
if (!var || !var->isStlStringType())
continue;
if (!Token::Match(dot->astOperand2(), "c_str|data ( )"))
continue;
cstr = op;
break;
}
if (!cstr)
return false;
const Token* strTok = (cstr == tok->astOperand1()) ? tok->astOperand2() : tok->astOperand1();
if (strTok->variable() && strTok->variable()->isStlStringType())
return true;
return strTok->valueType() && strTok->valueType()->container && strTok->valueType()->container->stdStringLike;
Comment thread
danmar marked this conversation as resolved.
Outdated
}

namespace {
const std::set<std::string> stl_string_stream = {
"istringstream", "ostringstream", "stringstream", "wstringstream"
Expand Down Expand Up @@ -2072,9 +2101,7 @@ void CheckStl::string_c_str()
tok->variable() && (tok->variable()->isStlStringType() || tok->variable()->isStlStringViewType()) &&
tok->tokAt(2)->variable() && tok->tokAt(2)->variable()->isStlStringType()) {
string_c_strConstructor(tok, tok->variable()->getTypeName());
} else if (printPerformance && tok->next() && tok->next()->variable() && tok->next()->variable()->isStlStringType() && tok->valueType() && tok->valueType()->type == ValueType::CONTAINER &&
((Token::Match(tok->previous(), "%var% + %var% . c_str|data ( )") && tok->previous()->variable() && tok->previous()->variable()->isStlStringType()) ||
(Token::Match(tok->tokAt(-5), "%var% . c_str|data ( ) + %var%") && tok->tokAt(-5)->variable() && tok->tokAt(-5)->variable()->isStlStringType()))) {
} else if (printPerformance && isc_strConcat(tok)) {
string_c_strConcat(tok);
} else if (printPerformance && Token::simpleMatch(tok, "<<") && tok->astOperand2() && Token::Match(tok->astOperand2()->astOperand1(), ". c_str|data ( )")) {
const Token* str = tok->astOperand2()->astOperand1()->astOperand1();
Expand Down
16 changes: 16 additions & 0 deletions test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4680,6 +4680,22 @@ class TestStl : public TestFixture {
" return s->x.c_str();\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("std::string f(const std::string& s) {\n" // #14533
" auto x = std::string(\"abc\") + s.c_str();\n"
" auto y = s.c_str() + std::string(\"def\");\n"
" return x + y;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:33]: (performance) Concatenating the result of c_str() and a std::string is slow and redundant. [stlcstrConcat]\n"
"[test.cpp:3:24]: (performance) Concatenating the result of c_str() and a std::string is slow and redundant. [stlcstrConcat]\n",
errout_str());

check("std::string get();\n"
"std::string f(const std::string& s) {\n"
" return get() + s.c_str();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:18]: (performance) Concatenating the result of c_str() and a std::string is slow and redundant. [stlcstrConcat]\n",
errout_str());
}

void uselessCalls() {
Expand Down
Loading