Fixed #1486 (--errorlist shows two identical id values)

This commit is contained in:
Robert Reif 2010-03-10 07:47:01 +01:00 committed by Daniel Marjamäki
parent 525a027b77
commit 09ab841789
3 changed files with 30 additions and 21 deletions

View File

@ -1593,7 +1593,7 @@ void CheckClass::checkConst()
for (int k = nestInfo.size() - 2; k >= 0; k--) for (int k = nestInfo.size() - 2; k >= 0; k--)
classname = std::string(nestInfo[k].className + "::" + classname); classname = std::string(nestInfo[k].className + "::" + classname);
checkConstError(found, classname, functionName); checkConstError2(found, tok2, classname, functionName);
} }
} }
} }
@ -1785,6 +1785,14 @@ void CheckClass::checkConstError(const Token *tok, const std::string &classname,
reportError(tok, Severity::style, "functionConst", "The function '" + classname + "::" + funcname + "' can be const"); reportError(tok, Severity::style, "functionConst", "The function '" + classname + "::" + funcname + "' can be const");
} }
void CheckClass::checkConstError2(const Token *tok1, const Token *tok2, const std::string &classname, const std::string &funcname)
{
std::list<const Token *> toks;
toks.push_back(tok1);
toks.push_back(tok2);
reportError(toks, Severity::style, "functionConst", "The function '" + classname + "::" + funcname + "' can be const");
}
void CheckClass::noConstructorError(const Token *tok, const std::string &classname, bool isStruct) void CheckClass::noConstructorError(const Token *tok, const std::string &classname, bool isStruct)
{ {
reportError(tok, Severity::style, "noConstructor", "The " + std::string(isStruct ? "struct" : "class") + " '" + classname + "' has no constructor. Member variables not initialized."); reportError(tok, Severity::style, "noConstructor", "The " + std::string(isStruct ? "struct" : "class") + " '" + classname + "' has no constructor. Member variables not initialized.");

View File

@ -169,6 +169,7 @@ private:
void operatorEqToSelfError(const Token *tok); void operatorEqToSelfError(const Token *tok);
void checkConstError(const Token *tok, const std::string &classname, const std::string &funcname); void checkConstError(const Token *tok, const std::string &classname, const std::string &funcname);
void checkConstError2(const Token *tok1, const Token *tok2, const std::string &classname, const std::string &funcname);
void getErrorMessages() void getErrorMessages()
{ {

View File

@ -1778,20 +1778,20 @@ private:
" int getA();\n" " int getA();\n"
"};\n" "};\n"
"int Fred::getA() { return a; }"); "int Fred::getA() { return a; }");
ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::getA' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::getA' can be const\n", errout.str());
checkConst("class Fred {\n" checkConst("class Fred {\n"
" const std::string foo();\n" " const std::string foo();\n"
"};\n" "};\n"
"const std::string Fred::foo() { return ""; }"); "const std::string Fred::foo() { return ""; }");
ASSERT_EQUALS("[test.cpp:4]: (style) The function 'Fred::foo' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (style) The function 'Fred::foo' can be const\n", errout.str());
checkConst("class Fred {\n" checkConst("class Fred {\n"
" std::string s;\n" " std::string s;\n"
" const std::string & foo();\n" " const std::string & foo();\n"
"};\n" "};\n"
"const std::string & Fred::foo() { return ""; }"); "const std::string & Fred::foo() { return ""; }");
ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::foo' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str());
// constructors can't be const.. // constructors can't be const..
checkConst("class Fred {\n" checkConst("class Fred {\n"
@ -1843,7 +1843,7 @@ private:
" void foo(std::string & a);\n" " void foo(std::string & a);\n"
"};\n" "};\n"
"void Fred::foo(std::string & a) { a = s; }"); "void Fred::foo(std::string & a) { a = s; }");
ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::foo' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str());
// assignment to variable can't be const // assignment to variable can't be const
checkConst("class Fred {\n" checkConst("class Fred {\n"
@ -1859,7 +1859,7 @@ private:
" void foo(std::string & a, std::string & b);\n" " void foo(std::string & a, std::string & b);\n"
"};\n" "};\n"
"void Fred::foo(std::string & a, std::string & b) { a = s; b = s; }"); "void Fred::foo(std::string & a, std::string & b) { a = s; b = s; }");
ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::foo' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str());
// assignment to variable, can't be const // assignment to variable, can't be const
checkConst("class Fred {\n" checkConst("class Fred {\n"
@ -1891,7 +1891,7 @@ private:
" void foo(int * a);\n" " void foo(int * a);\n"
"};\n" "};\n"
"void Fred::foo(int * a) { *a = s; }"); "void Fred::foo(int * a) { *a = s; }");
ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::foo' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str());
// assignment to variable, can't be const // assignment to variable, can't be const
checkConst("class Fred {\n" checkConst("class Fred {\n"
@ -1907,7 +1907,7 @@ private:
" void foo(std::string * a, std::string * b);\n" " void foo(std::string * a, std::string * b);\n"
"};\n" "};\n"
"void Fred::foo(std::string * a, std::string * b) { *a = s; *b = s; }"); "void Fred::foo(std::string * a, std::string * b) { *a = s; *b = s; }");
ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::foo' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str());
// assignment to variable, can't be const // assignment to variable, can't be const
checkConst("class Fred {\n" checkConst("class Fred {\n"
@ -1943,8 +1943,8 @@ private:
"void Fred::foo() { }" "void Fred::foo() { }"
"void Fred::foo(std::string & a) { a = s; }" "void Fred::foo(std::string & a) { a = s; }"
"void Fred::foo(const std::string & a) { s = a; }"); "void Fred::foo(const std::string & a) { s = a; }");
ASSERT_EQUALS("[test.cpp:7]: (style) The function 'Fred::foo' can be const\n" ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n"
"[test.cpp:7]: (style) The function 'Fred::foo' can be const\n", errout.str()); "[test.cpp:7] -> [test.cpp:4]: (style) The function 'Fred::foo' can be const\n", errout.str());
// check functions with different or missing paramater names // check functions with different or missing paramater names
checkConst("class Fred {\n" checkConst("class Fred {\n"
@ -1960,11 +1960,11 @@ private:
"void Fred::foo3(int a, int b) { }\n" "void Fred::foo3(int a, int b) { }\n"
"void Fred::foo4(int a, int b) { }\n" "void Fred::foo4(int a, int b) { }\n"
"void Fred::foo5(int, int) { }"); "void Fred::foo5(int, int) { }");
ASSERT_EQUALS("[test.cpp:9]: (style) The function 'Fred::foo1' can be const\n" ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:3]: (style) The function 'Fred::foo1' can be const\n"
"[test.cpp:10]: (style) The function 'Fred::foo2' can be const\n" "[test.cpp:10] -> [test.cpp:4]: (style) The function 'Fred::foo2' can be const\n"
"[test.cpp:11]: (style) The function 'Fred::foo3' can be const\n" "[test.cpp:11] -> [test.cpp:5]: (style) The function 'Fred::foo3' can be const\n"
"[test.cpp:12]: (style) The function 'Fred::foo4' can be const\n" "[test.cpp:12] -> [test.cpp:6]: (style) The function 'Fred::foo4' can be const\n"
"[test.cpp:13]: (style) The function 'Fred::foo5' can be const\n", errout.str()); "[test.cpp:13] -> [test.cpp:7]: (style) The function 'Fred::foo5' can be const\n", errout.str());
// check nested classes // check nested classes
checkConst("class Fred {\n" checkConst("class Fred {\n"
@ -1982,7 +1982,7 @@ private:
" };\n" " };\n"
" int A::getA() { return a; }\n" " int A::getA() { return a; }\n"
"};"); "};");
ASSERT_EQUALS("[test.cpp:6]: (style) The function 'Fred::A::getA' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:4]: (style) The function 'Fred::A::getA' can be const\n", errout.str());
checkConst("class Fred {\n" checkConst("class Fred {\n"
" class A {\n" " class A {\n"
@ -1991,7 +1991,7 @@ private:
" };\n" " };\n"
"};\n" "};\n"
"int Fred::A::getA() { return a; }"); "int Fred::A::getA() { return a; }");
ASSERT_EQUALS("[test.cpp:7]: (style) The function 'Fred::A::getA' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:4]: (style) The function 'Fred::A::getA' can be const\n", errout.str());
// check deeply nested classes // check deeply nested classes
checkConst("class Fred {\n" checkConst("class Fred {\n"
@ -2019,8 +2019,8 @@ private:
" };\n" " };\n"
" int B::getB() { return b; }\n" " int B::getB() { return b; }\n"
"};"); "};");
ASSERT_EQUALS("[test.cpp:11]: (style) The function 'Fred::B::getB' can be const\n" ASSERT_EQUALS("[test.cpp:11] -> [test.cpp:4]: (style) The function 'Fred::B::getB' can be const\n"
"[test.cpp:9]: (style) The function 'Fred::B::A::getA' can be const\n", errout.str()); "[test.cpp:9] -> [test.cpp:7]: (style) The function 'Fred::B::A::getA' can be const\n", errout.str());
checkConst("class Fred {\n" checkConst("class Fred {\n"
" class B {\n" " class B {\n"
@ -2034,8 +2034,8 @@ private:
"};\n" "};\n"
"int Fred::B::A::getA() { return a; }\n" "int Fred::B::A::getA() { return a; }\n"
"int Fred::B::getB() { return b; }\n"); "int Fred::B::getB() { return b; }\n");
ASSERT_EQUALS("[test.cpp:12]: (style) The function 'Fred::B::getB' can be const\n" ASSERT_EQUALS("[test.cpp:12] -> [test.cpp:4]: (style) The function 'Fred::B::getB' can be const\n"
"[test.cpp:11]: (style) The function 'Fred::B::A::getA' can be const\n", errout.str()); "[test.cpp:11] -> [test.cpp:7]: (style) The function 'Fred::B::A::getA' can be const\n", errout.str());
} }
// operator< can often be const // operator< can often be const