From 951da02f8958e846f7bb508e476d6758ab3fa07d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 22 Jun 2012 15:59:41 +0200 Subject: [PATCH] Reviewed handling of unknown types in C files in checkunusedvar --- lib/checkunusedvar.cpp | 2 +- test/testunusedvar.cpp | 44 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index e7ab82e3e..09acaf715 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -608,7 +608,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const type = Variables::pointerPointer; else if (i->isPointer()) 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; if (type == Variables::none || isPartOfClassStructUnion(i->typeStartToken())) continue; diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index d2fdddee7..d41bd434b 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -366,7 +366,7 @@ private: 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.. errout.str(""); @@ -376,7 +376,7 @@ private: // Tokenize.. Tokenizer tokenizer(&settings, this); std::istringstream istr(code); - tokenizer.tokenize(istr, "test.cpp"); + tokenizer.tokenize(istr, filename); // Check for unused variables.. CheckUnusedVar checkUnusedVar(&tokenizer, &settings, this); @@ -484,6 +484,13 @@ private: "}\n"); 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" "{\n" " int i = undefined;\n" @@ -593,6 +600,14 @@ private: "}\n"); 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" "{\n" " undefined * i;\n" @@ -2357,6 +2372,15 @@ private: " ref[0] = 123;\n" "}"); 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 @@ -2367,6 +2391,15 @@ 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 localvarasm() { @@ -2430,6 +2463,13 @@ private: "}\n"); 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" "int foo() {\n" " A a;\n"