Reviewed handling of unknown types in C files in checkunusedvar
This commit is contained in:
parent
bc9e419615
commit
951da02f89
|
@ -608,7 +608,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
|
||||||
type = Variables::pointerPointer;
|
type = Variables::pointerPointer;
|
||||||
else if (i->isPointer())
|
else if (i->isPointer())
|
||||||
type = Variables::pointer;
|
type = Variables::pointer;
|
||||||
else if (i->typeEndToken()->isStandardType() || isRecordTypeWithoutSideEffects(*i) || Token::simpleMatch(i->nameToken()->tokAt(-3), "std :: string"))
|
else if (_tokenizer->isC() || i->typeEndToken()->isStandardType() || isRecordTypeWithoutSideEffects(*i) || Token::simpleMatch(i->nameToken()->tokAt(-3), "std :: string"))
|
||||||
type = Variables::standard;
|
type = Variables::standard;
|
||||||
if (type == Variables::none || isPartOfClassStructUnion(i->typeStartToken()))
|
if (type == Variables::none || isPartOfClassStructUnion(i->typeStartToken()))
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -366,7 +366,7 @@ private:
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (style) struct or union member 'AB::a' is never used\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (style) struct or union member 'AB::a' is never used\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void functionVariableUsage(const char code[]) {
|
void functionVariableUsage(const char code[], const char filename[]="test.cpp") {
|
||||||
// Clear the error buffer..
|
// Clear the error buffer..
|
||||||
errout.str("");
|
errout.str("");
|
||||||
|
|
||||||
|
@ -376,7 +376,7 @@ private:
|
||||||
// Tokenize..
|
// Tokenize..
|
||||||
Tokenizer tokenizer(&settings, this);
|
Tokenizer tokenizer(&settings, this);
|
||||||
std::istringstream istr(code);
|
std::istringstream istr(code);
|
||||||
tokenizer.tokenize(istr, "test.cpp");
|
tokenizer.tokenize(istr, filename);
|
||||||
|
|
||||||
// Check for unused variables..
|
// Check for unused variables..
|
||||||
CheckUnusedVar checkUnusedVar(&tokenizer, &settings, this);
|
CheckUnusedVar checkUnusedVar(&tokenizer, &settings, this);
|
||||||
|
@ -484,6 +484,13 @@ private:
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
functionVariableUsage("undefined foo()\n"
|
||||||
|
"{\n"
|
||||||
|
" undefined i = 0;\n"
|
||||||
|
"}\n",
|
||||||
|
"test.c");
|
||||||
|
ASSERT_EQUALS("[test.c:3]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
|
||||||
|
|
||||||
functionVariableUsage("void foo()\n"
|
functionVariableUsage("void foo()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" int i = undefined;\n"
|
" int i = undefined;\n"
|
||||||
|
@ -593,6 +600,14 @@ private:
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
functionVariableUsage("undefined foo()\n"
|
||||||
|
"{\n"
|
||||||
|
" undefined i;\n"
|
||||||
|
" return i;\n"
|
||||||
|
"}\n",
|
||||||
|
"test.c");
|
||||||
|
ASSERT_EQUALS("[test.c:3]: (style) Variable 'i' is not assigned a value\n", errout.str());
|
||||||
|
|
||||||
functionVariableUsage("undefined *foo()\n"
|
functionVariableUsage("undefined *foo()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" undefined * i;\n"
|
" undefined * i;\n"
|
||||||
|
@ -2357,6 +2372,15 @@ private:
|
||||||
" ref[0] = 123;\n"
|
" ref[0] = 123;\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
functionVariableUsage("void foo()\n"
|
||||||
|
"{\n"
|
||||||
|
" Foo foo;\n"
|
||||||
|
" Foo &ref = foo;\n"
|
||||||
|
" ref[0] = 123;\n"
|
||||||
|
"}",
|
||||||
|
"test.c");
|
||||||
|
ASSERT_EQUALS("[test.c:3]: (style) Variable 'foo' is assigned a value that is never used\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void localvaralias10() { // ticket 2004
|
void localvaralias10() { // ticket 2004
|
||||||
|
@ -2367,6 +2391,15 @@ private:
|
||||||
" *x = 0;\n"
|
" *x = 0;\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("", errout.str());
|
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 localvarasm() {
|
void localvarasm() {
|
||||||
|
@ -2430,6 +2463,13 @@ private:
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
functionVariableUsage("int foo() {\n"
|
||||||
|
" A a;\n"
|
||||||
|
" return 0;\n"
|
||||||
|
"}\n",
|
||||||
|
"test.c");
|
||||||
|
ASSERT_EQUALS("[test.c:2]: (style) Unused variable: a\n", errout.str());
|
||||||
|
|
||||||
functionVariableUsage("struct A { int i; };\n"
|
functionVariableUsage("struct A { int i; };\n"
|
||||||
"int foo() {\n"
|
"int foo() {\n"
|
||||||
" A a;\n"
|
" A a;\n"
|
||||||
|
|
Loading…
Reference in New Issue