Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #3587 (Wrong tokenizer simplification - variable assigned by op…

…erator>>)
  • Loading branch information...
commit bbb707c460578839a9127ef9391f2061b0210f86 1 parent 47c7e34
@danmar authored
Showing with 37 additions and 0 deletions.
  1. +10 −0 lib/tokenize.cpp
  2. +27 −0 test/testtokenize.cpp
View
10 lib/tokenize.cpp
@@ -6504,6 +6504,16 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
ret = true;
}
+ // The >> operator is sometimes used to assign a variable in C++
+ if (isCPP() && Token::Match(tok3, (">> " + structname + " %varid%").c_str(), varid)) {
+ // bailout for such code: ; std :: cin >> i ;
+ const Token *prev = tok3->previous();
+ while (prev && prev->str() != "return" && (prev->isName() || prev->str() == "::"))
+ prev = prev->previous();
+ if (Token::Match(prev, "[;{}]"))
+ break;
+ }
+
// Variable is used in calculation..
if (((tok3->previous()->varId() > 0) && Token::Match(tok3, ("& " + structname + " %varid%").c_str(), varid)) ||
Token::Match(tok3, ("[=+-*/%^|[] " + structname + " %varid% [=?+-*/%^|;])]").c_str(), varid) ||
View
27 test/testtokenize.cpp
@@ -146,6 +146,7 @@ class TestTokenizer : public TestFixture {
TEST_CASE(simplifyKnownVariables43);
TEST_CASE(simplifyKnownVariables44); // ticket #3117 - don't simplify static variables
TEST_CASE(simplifyKnownVariables45); // ticket #3281 - static constant variable not simplified
+ TEST_CASE(simplifyKnownVariables46); // ticket #3587 - >>
TEST_CASE(simplifyKnownVariablesBailOutAssign1);
TEST_CASE(simplifyKnownVariablesBailOutAssign2);
TEST_CASE(simplifyKnownVariablesBailOutFor1);
@@ -2264,6 +2265,32 @@ class TestTokenizer : public TestFixture {
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
+ void simplifyKnownVariables46() {
+ const char code[] = "void f() {\n"
+ " int x = 0;\n"
+ " cin >> x;\n"
+ " return x;\n"
+ "}";
+
+ {
+ const char expected[] = "void f ( ) {\n"
+ "int x ; x = 0 ;\n"
+ "cin >> x ;\n"
+ "return x ;\n"
+ "}";
+ ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Unspecified, "test.cpp"));
+ }
+
+ {
+ const char expected[] = "void f ( ) {\n"
+ "\n"
+ "cin >> 0 ;\n"
+ "return 0 ;\n"
+ "}";
+ ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Unspecified, "test.c"));
+ }
+ }
+
void simplifyKnownVariablesBailOutAssign1() {
const char code[] = "int foo() {\n"
" int i; i = 0;\n"
Please sign in to comment.
Something went wrong with that request. Please try again.