Merge branch 'master' of github.com:danmar/cppcheck into wix

This commit is contained in:
Kimmo Varis 2010-03-09 16:58:25 +02:00
commit 05bd91b9fc
9 changed files with 58 additions and 49 deletions

View File

@ -1,4 +1,4 @@
CXXFLAGS=-Wall -Wextra -pedantic -g CXXFLAGS=-Wall -Wextra -pedantic -Wfloat-equal -Wcast-qual -Wlogical-op -g -D_GLIBCXX_DEBUG
CXX=g++ CXX=g++
BIN=${DESTDIR}/usr/bin BIN=${DESTDIR}/usr/bin

View File

@ -26,9 +26,8 @@ win32 {
} }
# Add more strict compiling flags for GCC # Add more strict compiling flags for GCC
# These flags are used in original (not generated by QMake) makefiles
contains(QMAKE_CXX, g++) { contains(QMAKE_CXX, g++) {
QMAKE_CXXFLAGS_WARN_ON += -Wextra -pedantic QMAKE_CXXFLAGS_WARN_ON += -Wextra -pedantic -Wfloat-equal -Wcast-qual -Wlogical-op
CONFIG(debug, debug|release) { CONFIG(debug, debug|release) {
# checked STL # checked STL

View File

@ -10,14 +10,29 @@
# - win_installer/productInfo.wxs # - win_installer/productInfo.wxs
# Tag to use # Tag to use
tag=1.41 tag=$1
# Name of release # Name of release
releasename=cppcheck-$tag releasename=cppcheck-$tag
# wget http://josefsson.org/git2cl/git2cl # wget http://josefsson.org/git2cl/git2cl
echo Update Changelog..
./git2cl > ChangeLog ./git2cl > ChangeLog
echo Update Makefile..
git tag -d $tag
g++ -o dmake tools/dmake.cpp lib/filelister.cpp
./dmake --release
git commit -a -m "Makefile: Set release mode"
git tag $tag
echo Create archives..
git archive --format=tar --prefix=$releasename/ $tag | gzip > ~/$releasename.tar.gz git archive --format=tar --prefix=$releasename/ $tag | gzip > ~/$releasename.tar.gz
git archive --format=tar --prefix=$releasename/ $tag | bzip2 > ~/$releasename.tar.bz2 git archive --format=tar --prefix=$releasename/ $tag | bzip2 > ~/$releasename.tar.bz2
git archive --format=zip -9 --prefix=$releasename/ $tag > ~/$releasename.zip git archive --format=zip -9 --prefix=$releasename/ $tag > ~/$releasename.zip
echo Restoring repository..
git tag -d $tag
git reset --hard HEAD^1
git pull

View File

@ -118,6 +118,10 @@ protected:
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList; std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
for (std::list<const Token *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it) for (std::list<const Token *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it)
{ {
// --errorlist can provide null values here
if (!(*it))
continue;
ErrorLogger::ErrorMessage::FileLocation loc; ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = (*it)->linenr(); loc.line = (*it)->linenr();
loc.file = _tokenizer->file(*it); loc.file = _tokenizer->file(*it);

View File

@ -182,11 +182,8 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
for (unsigned int i = 0; i < varname.size(); ++i) for (unsigned int i = 0; i < varname.size(); ++i)
varnames += (i == 0 ? "" : " . ") + varname[i]; varnames += (i == 0 ? "" : " . ") + varname[i];
unsigned int varc = varname.size(); const unsigned int varc(varname.empty() ? 0 : (varname.size() - 1) * 2);
if (varc == 0)
varc = 1;
varc = 2 * (varc - 1);
if (Token::Match(tok, "return")) if (Token::Match(tok, "return"))
{ {
tok = tok->next(); tok = tok->next();
@ -584,8 +581,8 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
if ((varid > 0 && Token::Match(tok, "strcpy|strcat ( %varid% , %str% )", varid)) || if ((varid > 0 && Token::Match(tok, "strcpy|strcat ( %varid% , %str% )", varid)) ||
(varid == 0 && Token::Match(tok, ("strcpy|strcat ( " + varnames + " , %str% )").c_str()))) (varid == 0 && Token::Match(tok, ("strcpy|strcat ( " + varnames + " , %str% )").c_str())))
{ {
size_t len = Token::getStrLength(tok->tokAt(varc + 4)); long len = Token::getStrLength(tok->tokAt(varc + 4));
if (len >= static_cast<size_t>(total_size)) if (len < 0 || len >= total_size)
{ {
bufferOverrun(tok, varid > 0 ? "" : varnames.c_str()); bufferOverrun(tok, varid > 0 ? "" : varnames.c_str());
continue; continue;
@ -597,8 +594,8 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
Token::Match(tok, "read|write ( %any% , %varid% , %num% )", varid) && Token::Match(tok, "read|write ( %any% , %varid% , %num% )", varid) &&
MathLib::isInt(tok->strAt(6))) MathLib::isInt(tok->strAt(6)))
{ {
size_t len = MathLib::toLongNumber(tok->strAt(6)); long len = MathLib::toLongNumber(tok->strAt(6));
if (len > static_cast<size_t>(total_size)) if (len < 0 || len > total_size)
{ {
bufferOverrun(tok); bufferOverrun(tok);
continue; continue;
@ -610,8 +607,8 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
Token::Match(tok, "fgets ( %varid% , %num% , %any% )", varid) && Token::Match(tok, "fgets ( %varid% , %num% , %any% )", varid) &&
MathLib::isInt(tok->strAt(4))) MathLib::isInt(tok->strAt(4)))
{ {
size_t len = MathLib::toLongNumber(tok->strAt(4)); long len = MathLib::toLongNumber(tok->strAt(4));
if (len > static_cast<size_t>(total_size)) if (len < 0 || len > total_size)
{ {
bufferOverrun(tok); bufferOverrun(tok);
continue; continue;
@ -622,7 +619,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
if (varid > 0 && Token::Match(tok, "strncat ( %varid% , %any% , %num% )", varid)) if (varid > 0 && Token::Match(tok, "strncat ( %varid% , %any% , %num% )", varid))
{ {
int n = MathLib::toLongNumber(tok->strAt(6)); int n = MathLib::toLongNumber(tok->strAt(6));
if (n >= total_size) if (n < 0 || n >= total_size)
strncatUsage(tok); strncatUsage(tok);
} }

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);
checkConstError2(found, tok2, classname, functionName); checkConstError(found, classname, functionName);
} }
} }
} }
@ -1785,14 +1785,6 @@ 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,7 +169,6 @@ 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()
{ {
@ -185,7 +184,6 @@ private:
operatorEqRetRefThisError(0); operatorEqRetRefThisError(0);
operatorEqToSelfError(0); operatorEqToSelfError(0);
checkConstError(0, "class", "function"); checkConstError(0, "class", "function");
checkConstError2(0, 0, "class", "function");
} }
std::string name() const std::string name() const

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] -> [test.cpp:3]: (style) The function 'Fred::getA' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (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] -> [test.cpp:2]: (style) The function 'Fred::foo' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (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] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (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] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (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] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (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] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (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] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (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] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n" ASSERT_EQUALS("[test.cpp:7]: (style) The function 'Fred::foo' can be const\n"
"[test.cpp:7] -> [test.cpp:4]: (style) The function 'Fred::foo' can be const\n", errout.str()); "[test.cpp:7]: (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] -> [test.cpp:3]: (style) The function 'Fred::foo1' can be const\n" ASSERT_EQUALS("[test.cpp:9]: (style) The function 'Fred::foo1' can be const\n"
"[test.cpp:10] -> [test.cpp:4]: (style) The function 'Fred::foo2' can be const\n" "[test.cpp:10]: (style) The function 'Fred::foo2' can be const\n"
"[test.cpp:11] -> [test.cpp:5]: (style) The function 'Fred::foo3' can be const\n" "[test.cpp:11]: (style) The function 'Fred::foo3' can be const\n"
"[test.cpp:12] -> [test.cpp:6]: (style) The function 'Fred::foo4' can be const\n" "[test.cpp:12]: (style) The function 'Fred::foo4' can be const\n"
"[test.cpp:13] -> [test.cpp:7]: (style) The function 'Fred::foo5' can be const\n", errout.str()); "[test.cpp:13]: (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] -> [test.cpp:4]: (style) The function 'Fred::A::getA' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:6]: (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] -> [test.cpp:4]: (style) The function 'Fred::A::getA' can be const\n", errout.str()); ASSERT_EQUALS("[test.cpp:7]: (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] -> [test.cpp:4]: (style) The function 'Fred::B::getB' can be const\n" ASSERT_EQUALS("[test.cpp:11]: (style) The function 'Fred::B::getB' can be const\n"
"[test.cpp:9] -> [test.cpp:7]: (style) The function 'Fred::B::A::getA' can be const\n", errout.str()); "[test.cpp:9]: (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] -> [test.cpp:4]: (style) The function 'Fred::B::getB' can be const\n" ASSERT_EQUALS("[test.cpp:12]: (style) The function 'Fred::B::getB' can be const\n"
"[test.cpp:11] -> [test.cpp:7]: (style) The function 'Fred::B::A::getA' can be const\n", errout.str()); "[test.cpp:11]: (style) The function 'Fred::B::A::getA' can be const\n", errout.str());
} }
// operator< can often be const // operator< can often be const

View File

@ -94,8 +94,10 @@ static void getCppFiles(std::vector<std::string> &files, const std::string &path
} }
} }
int main() int main(int argc, char **argv)
{ {
const bool release(argc >= 2 && std::string(argv[1]) == "--release");
// Get files.. // Get files..
std::vector<std::string> libfiles; std::vector<std::string> libfiles;
getCppFiles(libfiles, "lib/"); getCppFiles(libfiles, "lib/");
@ -138,8 +140,10 @@ int main()
std::ofstream fout("Makefile"); std::ofstream fout("Makefile");
// more warnings.. -Wfloat-equal -Wcast-qual -Wsign-conversion -Wlogical-op // Makefile settings..
fout << "CXXFLAGS=-Wall -Wextra -pedantic -g\n"; // TODO: add more compiler warnings. For example -Wsign-conversion
fout << "CXXFLAGS=-Wall -Wextra -pedantic -Wfloat-equal -Wcast-qual -Wlogical-op ";
fout << (release ? "-O2 -DNDEBUG" : "-g -D_GLIBCXX_DEBUG") << "\n";
fout << "CXX=g++\n"; fout << "CXX=g++\n";
fout << "BIN=${DESTDIR}/usr/bin\n\n"; fout << "BIN=${DESTDIR}/usr/bin\n\n";
fout << "# For 'make man': sudo apt-get install xsltproc docbook-xsl docbook-xml\n"; fout << "# For 'make man': sudo apt-get install xsltproc docbook-xsl docbook-xml\n";