-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fix #14533, #14536 FN stlcstrConcat, stlcstrAssignment, stlcstrConstructor #8261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chrchr-github
wants to merge
9
commits into
danmar:main
Choose a base branch
from
chrchr-github:chr_14533
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+87
−11
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5f7d92b
Update checkstl.cpp
chrchr-github 0ec9223
Update teststl.cpp
chrchr-github d4269dd
Update checkstl.cpp
chrchr-github 4614417
Fix #14536 FN stlcstrConcat with function returning string
web-flow a8bdf5f
Check operand
web-flow 6559527
Update checkstl.cpp
chrchr-github 1eafc89
Update teststl.cpp
chrchr-github 82b2a0f
Update teststl.cpp
chrchr-github f6b00d6
Update librarydialog.cpp
chrchr-github File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1959,6 +1959,55 @@ static bool isLocal(const Token *tok) | |
| return var && !var->isStatic() && var->isLocal(); | ||
| } | ||
|
|
||
| static bool isc_strCall(const Token* tok) | ||
| { | ||
| if (!Token::simpleMatch(tok, "(")) | ||
| return false; | ||
| const Token* dot = tok->astOperand1(); | ||
| if (!Token::simpleMatch(dot, ".")) | ||
| return false; | ||
| const Token* obj = dot->astOperand1(); | ||
| if (!obj || !obj->valueType() || !obj->valueType()->container || !obj->valueType()->container->stdStringLike) | ||
| return false; | ||
| return Token::Match(dot->astOperand2(), "c_str|data ( )"); | ||
| } | ||
|
|
||
| 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() }) { | ||
| if (isc_strCall(op)) { | ||
| cstr = op; | ||
| break; | ||
| } | ||
| } | ||
| if (!cstr) | ||
| return false; | ||
| const Token* strTok = (cstr == tok->astOperand1()) ? tok->astOperand2() : tok->astOperand1(); | ||
| return strTok->valueType() && strTok->valueType()->container && strTok->valueType()->container->stdStringLike; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. gcc does not complain about this weird code: |
||
| } | ||
|
|
||
| static bool isc_strAssignment(const Token* tok) | ||
| { | ||
| if (!Token::simpleMatch(tok, "=")) | ||
| return false; | ||
| if (!isc_strCall(tok->astOperand2())) | ||
| return false; | ||
| const Token* strTok = tok->astOperand1(); | ||
| return strTok && strTok->valueType() && strTok->valueType()->container && strTok->valueType()->container->stdStringLike; | ||
| } | ||
|
|
||
| static bool isc_strConstructor(const Token* tok) | ||
| { | ||
| if (!Token::Match(tok, "%var% (|{")) | ||
| return false; | ||
| if (!isc_strCall(tok->tokAt(1)->astOperand2())) | ||
| return false; | ||
| return tok->valueType() && tok->valueType()->container && tok->valueType()->container->stdStringLike; | ||
| } | ||
|
|
||
| namespace { | ||
| const std::set<std::string> stl_string_stream = { | ||
| "istringstream", "ostringstream", "stringstream", "wstringstream" | ||
|
|
@@ -2027,16 +2076,14 @@ void CheckStl::string_c_str() | |
| const Variable* var2 = tok->tokAt(2)->variable(); | ||
| if (var->isPointer() && var2 && var2->isStlType(stl_string_stream)) | ||
| string_c_strError(tok); | ||
| } else if (printPerformance && isc_strAssignment(tok->tokAt(1))) { | ||
| string_c_strAssignment(tok, tok->variable()->getTypeName()); | ||
| } else if (Token::Match(tok->tokAt(2), "%name% (") && | ||
| Token::Match(tok->linkAt(3), ") . c_str|data ( ) ;") && | ||
| tok->tokAt(2)->function() && Token::Match(tok->tokAt(2)->function()->retDef, "std :: string|wstring %name%")) { | ||
| const Variable* var = tok->variable(); | ||
| if (var->isPointer()) | ||
| string_c_strError(tok); | ||
| } else if (printPerformance && tok->tokAt(1)->astOperand2() && Token::Match(tok->tokAt(1)->astOperand2()->tokAt(-3), "%var% . c_str|data ( ) ;")) { | ||
| const Token* vartok = tok->tokAt(1)->astOperand2()->tokAt(-3); | ||
| if ((tok->variable()->isStlStringType() || tok->variable()->isStlStringViewType()) && vartok->variable() && vartok->variable()->isStlStringType()) | ||
| string_c_strAssignment(tok, tok->variable()->getTypeName()); | ||
| } | ||
| } else if (printPerformance && tok->function() && Token::Match(tok, "%name% ( !!)") && tok->str() != scope.className) { | ||
| const auto range = c_strFuncParam.equal_range(tok->function()); | ||
|
|
@@ -2068,13 +2115,9 @@ void CheckStl::string_c_str() | |
| } | ||
| } | ||
| } | ||
| } else if (printPerformance && Token::Match(tok, "%var% (|{ %var% . c_str|data ( ) !!,") && | ||
| tok->variable() && (tok->variable()->isStlStringType() || tok->variable()->isStlStringViewType()) && | ||
| tok->tokAt(2)->variable() && tok->tokAt(2)->variable()->isStlStringType()) { | ||
| } else if (printPerformance && isc_strConstructor(tok)) { | ||
| 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(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have
stdStringLikestrings in qt.cfg, sfml.cfg and wxwidgets.cfg, which all have conversion functions to/fromstd::string. So it should be OK to warn if the types don't match. I guess converting e.g. aQStringto awxStringis pretty rare.