#6077: Don't warn about memcpy/memmove on class containing floats.

This commit is contained in:
PKEuS 2014-08-20 15:12:53 +02:00
parent c678937538
commit f01d7543f6
3 changed files with 18 additions and 9 deletions

View File

@ -1037,8 +1037,8 @@ void CheckClass::checkMemsetType(const Scope *start, const Token *tok, const Sco
checkMemsetType(start, tok, typeScope, allocation, parsedTypes);
// check for float
else if (var->isFloatingType() && _settings->isEnabled("portability"))
memsetErrorFloat(tok, tok->str(), type->classDef->str());
else if (tok->str() == "memset" && var->isFloatingType() && _settings->isEnabled("portability"))
memsetErrorFloat(tok, type->classDef->str());
}
}
}
@ -1079,10 +1079,10 @@ void CheckClass::memsetErrorReference(const Token *tok, const std::string &memfu
reportError(tok, Severity::error, "memsetClassReference", "Using '" + memfunc + "' on " + type + " that contains a reference.");
}
void CheckClass::memsetErrorFloat(const Token *tok, const std::string &memfunc, const std::string &type)
void CheckClass::memsetErrorFloat(const Token *tok, const std::string &type)
{
reportError(tok, Severity::portability, "memsetClassFloat", "Using '" + memfunc + "' on " + type + " which contains a floating point number.\n"
"Using '" + memfunc + "' on " + type + " which contains a floating point number. This is not portable because memset() sets each byte of a block of memory to a specific value and"
reportError(tok, Severity::portability, "memsetClassFloat", "Using memset() on " + type + " which contains a floating point number.\n"
"Using memset() on " + type + " which contains a floating point number. This is not portable because memset() sets each byte of a block of memory to a specific value and"
" the actual representation of a floating-point value is implementation defined.");
}

View File

@ -144,7 +144,7 @@ private:
void unusedPrivateFunctionError(const Token *tok, const std::string &classname, const std::string &funcname);
void memsetError(const Token *tok, const std::string &memfunc, const std::string &classname, const std::string &type);
void memsetErrorReference(const Token *tok, const std::string &memfunc, const std::string &type);
void memsetErrorFloat(const Token *tok, const std::string &memfunc, const std::string &type);
void memsetErrorFloat(const Token *tok, const std::string &type);
void mallocOnClassError(const Token* tok, const std::string &memfunc, const Token* classTok, const std::string &classname);
void mallocOnClassWarning(const Token* tok, const std::string &memfunc, const Token* classTok);
void operatorEqReturnError(const Token *tok, const std::string &className);
@ -171,7 +171,7 @@ private:
c.unusedPrivateFunctionError(0, "classname", "funcname");
c.memsetError(0, "memfunc", "classname", "class");
c.memsetErrorReference(0, "memfunc", "class");
c.memsetErrorFloat(0, "memfunc", "class");
c.memsetErrorFloat(0, "class");
c.mallocOnClassWarning(0, "malloc", 0);
c.mallocOnClassError(0, "malloc", 0, "std::string");
c.operatorEqReturnError(0, "class");

View File

@ -2481,7 +2481,7 @@ private:
" A a;\n"
" memset(&a, 0, sizeof(A));\n"
"}", false, true);
ASSERT_EQUALS("[test.cpp:6]: (portability) Using 'memset' on struct which contains a floating point number.\n", errout.str());
ASSERT_EQUALS("[test.cpp:6]: (portability) Using memset() on struct which contains a floating point number.\n", errout.str());
checkNoMemset("struct A {\n"
" float f[4];\n"
@ -2490,7 +2490,16 @@ private:
" A a;\n"
" memset(&a, 0, sizeof(A));\n"
"}", false, true);
ASSERT_EQUALS("[test.cpp:6]: (portability) Using 'memset' on struct which contains a floating point number.\n", errout.str());
ASSERT_EQUALS("[test.cpp:6]: (portability) Using memset() on struct which contains a floating point number.\n", errout.str());
checkNoMemset("struct A {\n"
" float f[4];\n"
"};\n"
"void f(const A& b) {\n"
" A a;\n"
" memcpy(&a, &b, sizeof(A));\n"
"}", false, true);
ASSERT_EQUALS("", errout.str());
checkNoMemset("struct A {\n"
" float* f;\n"