Tell if it's struct or union

This commit is contained in:
Dmitry-Me 2015-09-22 17:38:23 +03:00
parent a0b97d7676
commit 0b991f5560
3 changed files with 21 additions and 9 deletions

View File

@ -1253,15 +1253,16 @@ void CheckUnusedVar::checkStructMemberUsage()
} }
if (!used) { if (!used) {
unusedStructMemberError(tok->next(), structname, *varname); unusedStructMemberError(tok->next(), structname, *varname, tok->scope()->type == Scope::eUnion);
} }
} }
} }
} }
void CheckUnusedVar::unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname) void CheckUnusedVar::unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname, bool isUnion)
{ {
reportError(tok, Severity::style, "unusedStructMember", "struct or union member '" + structname + "::" + varname + "' is never used."); const char* prefix = isUnion ? "union member '" : "struct member '";
reportError(tok, Severity::style, "unusedStructMember", std::string(prefix) + structname + "::" + varname + "' is never used.");
} }
bool CheckUnusedVar::isRecordTypeWithoutSideEffects(const Type* type) bool CheckUnusedVar::isRecordTypeWithoutSideEffects(const Type* type)

View File

@ -75,7 +75,7 @@ private:
bool isEmptyType(const Type* type); bool isEmptyType(const Type* type);
// Error messages.. // Error messages..
void unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname); void unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname, bool isUnion = false);
void unusedVariableError(const Token *tok, const std::string &varname); void unusedVariableError(const Token *tok, const std::string &varname);
void allocatedButUnusedVariableError(const Token *tok, const std::string &varname); void allocatedButUnusedVariableError(const Token *tok, const std::string &varname);
void unreadVariableError(const Token *tok, const std::string &varname); void unreadVariableError(const Token *tok, const std::string &varname);

View File

@ -208,9 +208,20 @@ private:
" int b;\n" " int b;\n"
" int c;\n" " int c;\n"
"};"); "};");
ASSERT_EQUALS("[test.cpp:3]: (style) struct or union member 'abc::a' is never used.\n" ASSERT_EQUALS("[test.cpp:3]: (style) struct member 'abc::a' is never used.\n"
"[test.cpp:4]: (style) struct or union member 'abc::b' is never used.\n" "[test.cpp:4]: (style) struct member 'abc::b' is never used.\n"
"[test.cpp:5]: (style) struct or union member 'abc::c' is never used.\n", errout.str()); "[test.cpp:5]: (style) struct member 'abc::c' is never used.\n", errout.str());
checkStructMemberUsage("union abc\n"
"{\n"
" int a;\n"
" int b;\n"
" int c;\n"
"};");
ASSERT_EQUALS("[test.cpp:3]: (style) union member 'abc::a' is never used.\n"
"[test.cpp:4]: (style) union member 'abc::b' is never used.\n"
"[test.cpp:5]: (style) union member 'abc::c' is never used.\n", errout.str());
} }
void structmember2() { void structmember2() {
@ -418,7 +429,7 @@ private:
"{\n" "{\n"
" ab.b = 0;\n" " ab.b = 0;\n"
"}"); "}");
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 member 'AB::a' is never used.\n", errout.str());
} }
void functionVariableUsage(const char code[], const char filename[]="test.cpp") { void functionVariableUsage(const char code[], const char filename[]="test.cpp") {