Fix #11717 Warn when C++ code is scanned as C (prevent FPs) (#5064)

* Fix #11717 Warn when C++ code is scanned as C (prevent FPs)

* Don't throw for operators

* Detect reference variables

* Fix tests

* Fix compilation

* Fix test

* Fix another test

* Use strAt()
This commit is contained in:
chrchr-github 2023-05-21 13:59:49 +02:00 committed by GitHub
parent f72db74817
commit ba57e15cb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 8 additions and 19 deletions

View File

@ -4139,8 +4139,6 @@ static bool setVarIdParseDeclaration(Token** tok, const VariableMap& variableMap
return false;
}
} else if (Token::Match(tok2, "&|&&")) {
if (c)
return false;
ref = !bracket;
} else if (singleNameCount >= 1 && Token::Match(tok2, "( [*&]") && Token::Match(tok2->link(), ") (|[")) {
for (const Token* tok3 = tok2->tokAt(2); Token::Match(tok3, "!!)"); tok3 = tok3->next()) {
@ -4667,6 +4665,8 @@ void Tokenizer::setVarIdPass1()
decl = false;
if (decl) {
if (isC() && Token::Match(prev2->previous(), "&|&&"))
syntaxErrorC(prev2, prev2->strAt(-2) + prev2->strAt(-1) + " " + prev2->str());
variableMap.addVariable(prev2->str(), scopeStack.size() <= 1);
if (Token::simpleMatch(tok->previous(), "for (") && Token::Match(prev2, "%name% [=,]")) {

View File

@ -368,8 +368,8 @@ private:
" char * &ref = p;\n"
" p = malloc(10);\n"
" free(ref);\n"
"}");
TODO_ASSERT_EQUALS("", "[test.c:6]: (error) Memory leak: p\n", errout.str());
"}", /*cpp*/ true);
TODO_ASSERT_EQUALS("", "[test.cpp:6]: (error) Memory leak: p\n", errout.str());
}
void assign14() {
@ -2329,7 +2329,7 @@ private:
void test1() { // 3809
check("void f(double*&p) {\n"
" p = malloc(0x100);\n"
"}");
"}", /*cpp*/ true);
ASSERT_EQUALS("", errout.str());
}
@ -2345,7 +2345,7 @@ private:
check("void f() {\n"
" char *&p = x();\n"
" p = malloc(10);\n"
"};");
"};", /*cpp*/ true);
ASSERT_EQUALS("", errout.str());
}

View File

@ -4788,15 +4788,6 @@ private:
" *x = 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
functionVariableUsage("void foo(Foo &foo)\n"
"{\n"
" Foo &ref = foo;\n"
" int *x = &ref.x;\n"
" *x = 0;\n"
"}",
"test.c");
ASSERT_EQUALS("", errout.str());
}
void localvaralias11() { // #4423 - iterator

View File

@ -897,14 +897,12 @@ private:
const char code1[] = "union evt; void f(const evt & event);";
ASSERT_EQUALS("1: union evt ; void f ( const evt & event@1 ) ;\n",
tokenize(code1));
ASSERT_EQUALS("1: union evt ; void f ( const evt & event ) ;\n",
tokenize(code1, "test.c"));
ASSERT_THROW(tokenize(code1, "test.c"), InternalError);
const char code2[] = "struct evt; void f(const evt & event);";
ASSERT_EQUALS("1: struct evt ; void f ( const evt & event@1 ) ;\n",
tokenize(code2));
ASSERT_EQUALS("1: struct evt ; void f ( const evt & event ) ;\n",
tokenize(code2, "test.c"));
ASSERT_THROW(tokenize(code2, "test.c"), InternalError);
}
void varid42() {