From 37b1f7c296cc8594667d334ab071340ddc5cdf5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 6 Jan 2011 16:27:22 +0100 Subject: [PATCH 1/9] memsetZeroBytes: improved error message. ticket: #2421 --- lib/checkother.cpp | 6 +++--- test/testother.cpp | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index cf3529de2..6e9101893 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2819,7 +2819,7 @@ void CheckOther::catchExceptionByValueError(const Token *tok) void CheckOther::memsetZeroBytesError(const Token *tok, const std::string &varname) { - reportError(tok, Severity::warning, - "memsetZeroBytes", "memset() called to fill 0 bytes of \"" + varname + "\"" - ". Second and third arguments might be inverted."); + const std::string summary("memset() called to fill 0 bytes of \'" + varname + "\'"); + const std::string verbose(summary + ". Second and third arguments might be inverted."); + reportError(tok, Severity::warning, "memsetZeroBytes", summary + "\n" + verbose); } diff --git a/test/testother.cpp b/test/testother.cpp index 5284aa522..7992a2849 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1625,7 +1625,7 @@ private: "}\n" ); ASSERT_EQUALS("[test.cpp:2]: (warning) memset() called to fill 0" - " bytes of \"p\". Second and third arguments might be inverted.\n", errout.str()); + " bytes of \'p\'\n", errout.str()); check("void f() {\n" " memset(p, sizeof(p), 0)\n" @@ -1633,6 +1633,7 @@ private: ); TODO_ASSERT_EQUALS("[test.cpp:2]: (warning) memset() called to fill 0" " bytes of \"p\". Second and third arguments might be inverted.\n", errout.str()); + ASSERT_EQUALS("", errout.str()); } }; From 2a93aa9fce34e705c034027dd3e74085ad173175 Mon Sep 17 00:00:00 2001 From: Kimmo Varis Date: Thu, 6 Jan 2011 18:32:04 +0200 Subject: [PATCH 2/9] GUI: Convert path to native separators before copying it. Ticket #2424 (Windows GUI: "Copy full path" doesn't copy the visible full path if it has been changed in preferences) We keep paths internally with / separator and only convert to native separators (for Windows) when showing them. Conversion was missing from path copying function. --- gui/resultstree.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gui/resultstree.cpp b/gui/resultstree.cpp index 78b0bb4c2..a46bedd59 100644 --- a/gui/resultstree.cpp +++ b/gui/resultstree.cpp @@ -719,7 +719,7 @@ void ResultsTree::CopyPath(QStandardItem *target, bool fullPath) //Replace (file) with filename QString file = data["file"].toString(); - pathStr = file; + pathStr = QDir::toNativeSeparators(file); if (!fullPath) { QFileInfo fi(pathStr); From 6e0f6c5aec0302ba939c53ce441b9c0e485996b3 Mon Sep 17 00:00:00 2001 From: Kimmo Varis Date: Thu, 6 Jan 2011 20:45:07 +0200 Subject: [PATCH 3/9] GUI: Add comment about storing paths. --- gui/erroritem.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gui/erroritem.h b/gui/erroritem.h index 68729ebac..6053aea1a 100644 --- a/gui/erroritem.h +++ b/gui/erroritem.h @@ -30,6 +30,11 @@ class ErrorLine; /** * @brief A class containing error data for one error. +* +* The paths are stored with internal ("/") separators. Only when we show the +* path or copy if for user (to clipboard) we convert to native separators. +* Full path is stored instead of relative path for flexibility. It is easy +* to get the relative path from full path when needed. */ class ErrorItem { From a7835c405454e7f18fccb56488f3c661097735c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 6 Jan 2011 20:01:09 +0100 Subject: [PATCH 4/9] Preprocessor: Reverted fix for #2131, it didn't work well so a better fix is needed --- lib/preprocessor.cpp | 54 ++++++++++++++++++++++++-------------------- lib/preprocessor.h | 10 +------- 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 14e6b80a4..f95c4286c 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -699,8 +699,7 @@ void Preprocessor::preprocess(std::istream &srcCodeStream, std::string &processe processedFile = ostr.str(); } - std::set systemIncludes; - handleIncludes(processedFile, filename, includePaths, systemIncludes); + handleIncludes(processedFile, filename, includePaths); processedFile = replaceIfDefined(processedFile); @@ -1490,13 +1489,17 @@ static int tolowerWrapper(int c) } -void Preprocessor::handleIncludes(std::string &code, - const std::string &filePath, - const std::list &includePaths, - std::set &systemIncludes, - std::set handledFiles) +void Preprocessor::handleIncludes(std::string &code, const std::string &filePath, const std::list &includePaths) { + std::list paths; + std::string path; + path = filePath; + path.erase(1 + path.find_last_of("\\/")); + paths.push_back(path); std::string::size_type pos = 0; + std::string::size_type endfilePos = 0; + std::set handledFiles; + endfilePos = pos; while ((pos = code.find("#include", pos)) != std::string::npos) { // Accept only includes that are at the start of a line @@ -1506,6 +1509,15 @@ void Preprocessor::handleIncludes(std::string &code, continue; } + // If endfile is encountered, we have moved to a next file in our stack, + // so remove last path in our list. + while ((endfilePos = code.find("\n#endfile", endfilePos)) != std::string::npos && endfilePos < pos) + { + paths.pop_back(); + endfilePos += 9; // size of #endfile + } + + endfilePos = pos; std::string::size_type end = code.find("\n", pos); std::string filename = code.substr(pos, end - pos); @@ -1539,17 +1551,11 @@ void Preprocessor::handleIncludes(std::string &code, if (headerType == UserHeader && !fileOpened) { - if (filePath.find_first_of("\\/") != std::string::npos) + fin.open((paths.back() + filename).c_str()); + if (fin.is_open()) { - std::string path(filePath); - path.erase(1 + path.find_last_of("\\/")); - - fin.open((path + filename).c_str()); - if (fin.is_open()) - { - filename = path + filename; - fileOpened = true; - } + filename = paths.back() + filename; + fileOpened = true; } } @@ -1558,8 +1564,7 @@ void Preprocessor::handleIncludes(std::string &code, filename = Path::simplifyPath(filename.c_str()); std::string tempFile = filename; std::transform(tempFile.begin(), tempFile.end(), tempFile.begin(), tolowerWrapper); - if (handledFiles.find(tempFile) != handledFiles.end() || - (headerType == SystemHeader && systemIncludes.find(tempFile) != systemIncludes.end())) + if (handledFiles.find(tempFile) != handledFiles.end()) { // We have processed this file already once, skip // it this time to avoid eternal loop. @@ -1567,10 +1572,7 @@ void Preprocessor::handleIncludes(std::string &code, continue; } - if (headerType == SystemHeader) - systemIncludes.insert(tempFile); - else - handledFiles.insert(tempFile); + handledFiles.insert(tempFile); processedFile = Preprocessor::read(fin, filename, _settings); fin.close(); } @@ -1586,10 +1588,12 @@ void Preprocessor::handleIncludes(std::string &code, // Remove space characters that are after or before new line character processedFile = removeSpaceNearNL(processedFile); - handleIncludes(processedFile, filename, includePaths, systemIncludes, handledFiles); processedFile = "#file \"" + filename + "\"\n" + processedFile + "\n#endfile"; code.insert(pos, processedFile); - pos += processedFile.size(); + + path = filename; + path.erase(1 + path.find_last_of("\\/")); + paths.push_back(path); } else if (!fileOpened) { diff --git a/lib/preprocessor.h b/lib/preprocessor.h index 8c31568b6..6cdaebb5f 100644 --- a/lib/preprocessor.h +++ b/lib/preprocessor.h @@ -25,7 +25,6 @@ #include #include #include -#include class ErrorLogger; class Settings; @@ -213,16 +212,9 @@ private: * There must be a path separator at the end. Default parameter is empty list. * Note that if path from given filename is also extracted and that is used as * a last include path if include file was not found from earlier paths. - * @param systemIncludes System includes - * @param handledFiles used in the recursive handling. Should be empty unless - * a recursive call is made. * @return modified source code */ - void handleIncludes(std::string &code, - const std::string &filePath, - const std::list &includePaths, - std::set &systemIncludes, - std::set handledFiles = std::set()); + void handleIncludes(std::string &code, const std::string &filePath, const std::list &includePaths); Settings *_settings; ErrorLogger *_errorLogger; From 1b3a5ed0be34c8a86484a17825f816240448a6be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 6 Jan 2011 20:16:14 +0100 Subject: [PATCH 5/9] Tokenizer: simplify NULL to 0 in the normal token list --- lib/tokenize.cpp | 34 ++++++++++++++++++---------------- test/testtokenize.cpp | 12 ++++++------ 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 21c1519a0..8cca03e7d 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2029,6 +2029,22 @@ bool Tokenizer::tokenize(std::istream &code, } } + // Replace NULL with 0.. + for (Token *tok = _tokens; tok; tok = tok->next()) + { + if (tok->str() == "NULL" || tok->str() == "'\\0'" || tok->str() == "'\\x0'") + { + tok->str("0"); + } + else if (tok->isNumber() && + MathLib::isInt(tok->str()) && + MathLib::toLongNumber(tok->str()) == 0) + { + tok->str("0"); + } + } + + // remove inline SQL (Oracle PRO*C). Ticket: #1959 for (Token *tok = _tokens; tok; tok = tok->next()) { @@ -2422,6 +2438,8 @@ bool Tokenizer::tokenize(std::istream &code, // Change initialisation of variable to assignment simplifyInitVar(); + + if (!preprocessorCondition) { setVarId(); @@ -3987,22 +4005,6 @@ bool Tokenizer::simplifyTokenList() tok->deleteNext(); } - - // Replace NULL with 0.. - for (Token *tok = _tokens; tok; tok = tok->next()) - { - if (tok->str() == "NULL" || tok->str() == "'\\0'" || tok->str() == "'\\x0'") - { - tok->str("0"); - } - else if (tok->isNumber() && - MathLib::isInt(tok->str()) && - MathLib::toLongNumber(tok->str()) == 0) - { - tok->str("0"); - } - } - // simplify references simplifyReference(); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 82460bbf6..18db0f6fb 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -3776,19 +3776,19 @@ private: // ticket #346 const char code1[] = "void *p = NULL;"; - const char res1[] = "void * p ; p = NULL ;"; + const char res1[] = "void * p ; p = 0 ;"; ASSERT_EQUALS(res1, tokenizeAndStringify(code1)); const char code2[] = "const void *p = NULL;"; - const char res2[] = "const void * p ; p = NULL ;"; + const char res2[] = "const void * p ; p = 0 ;"; ASSERT_EQUALS(res2, tokenizeAndStringify(code2)); const char code3[] = "void * const p = NULL;"; - const char res3[] = "void * const p ; p = NULL ;"; + const char res3[] = "void * const p ; p = 0 ;"; ASSERT_EQUALS(res3, tokenizeAndStringify(code3)); const char code4[] = "const void * const p = NULL;"; - const char res4[] = "const void * const p ; p = NULL ;"; + const char res4[] = "const void * const p ; p = 0 ;"; ASSERT_EQUALS(res4, tokenizeAndStringify(code4)); } @@ -3909,7 +3909,7 @@ private: { // ticket #696 const char code[] = "char a[10]={'\\0'}, b[10]={'\\0'};"; - const char res[] = "char a [ 10 ] = { '\\0' } ; char b [ 10 ] = { '\\0' } ;"; + const char res[] = "char a [ 10 ] = { 0 } ; char b [ 10 ] = { 0 } ;"; ASSERT_EQUALS(res, tokenizeAndStringify(code)); } @@ -3917,7 +3917,7 @@ private: void vardecl9() { const char code[] = "char a[2] = {'A', '\\0'}, b[2] = {'B', '\\0'};"; - const char res[] = "char a [ 2 ] = { 'A' , '\\0' } ; char b [ 2 ] = { 'B' , '\\0' } ;"; + const char res[] = "char a [ 2 ] = { 'A' , 0 } ; char b [ 2 ] = { 'B' , 0 } ;"; ASSERT_EQUALS(res, tokenizeAndStringify(code)); } From 5eeb18dcd7f0b8e0083e4e20aad900cc87780e81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 6 Jan 2011 20:53:22 +0100 Subject: [PATCH 6/9] scripts: update 'comment.pl' so it understand /* --- scripts/comment.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/comment.pl b/scripts/comment.pl index 4c2a011c9..57a310e8f 100755 --- a/scripts/comment.pl +++ b/scripts/comment.pl @@ -27,7 +27,7 @@ sub checkfile } # set comment variable - if ($line =~ /\/\//) + if (($line =~ /\/\//) || ($line =~ /\/\*/)) { $comment = 1; } From 56ffde402f753f331f97f1b652127786acd3d04f Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Fri, 7 Jan 2011 07:42:00 +0100 Subject: [PATCH 7/9] Fixed #2425 (segmentation fault of cppcheck) --- lib/symboldatabase.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 6f9cf95a5..375c5df6a 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -1574,7 +1574,8 @@ void SymbolDatabase::SpaceInfo::initializeVarList(const Func &func, std::listprevious()->str() != "::") { // check if member function exists std::list::const_iterator it; From bfc95e01c115723dee104343984380a73061d621 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Fri, 7 Jan 2011 08:02:47 +0100 Subject: [PATCH 8/9] Fixed #2426 (### Internal error in Cppcheck. Please report it.) --- lib/tokenize.cpp | 21 +++++++++++++++++++-- test/testsimplifytokens.cpp | 9 +++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 8cca03e7d..6c963dbec 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -665,8 +665,16 @@ void Tokenizer::unsupportedTypedef(const Token *tok) const std::ostringstream str; const Token *tok1 = tok; - while (tok && tok->str() != ";") + int level = 0; + while (tok) { + if (level == 0 && tok->str() == ";") + break; + else if (tok->str() == "{") + level++; + else if (tok->str() == "}") + level--; + if (tok != tok1) str << " "; str << tok->str(); @@ -694,10 +702,19 @@ void Tokenizer::unsupportedTypedef(const Token *tok) const Token * Tokenizer::deleteInvalidTypedef(Token *typeDef) { Token *tok = NULL; + int level = 0; // remove typedef but leave ; - while (typeDef->next() && typeDef->next()->str() != ";") + while (typeDef->next()) + { + if (level == 0 && typeDef->next()->str() == ";") + break; + else if (typeDef->next()->str() == "{") + level++; + else if (typeDef->next()->str() == "}") + level--; typeDef->deleteNext(); + } if (typeDef != _tokens) { diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 0efd8f757..dc19bb1ab 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -232,6 +232,7 @@ private: TEST_CASE(simplifyTypedef72); // ticket #2375 TEST_CASE(simplifyTypedef73); // ticket #2412 TEST_CASE(simplifyTypedef74); // ticket #2414 + TEST_CASE(simplifyTypedef75); // ticket #2426 TEST_CASE(simplifyTypedefFunction1); TEST_CASE(simplifyTypedefFunction2); // ticket #1685 @@ -4786,6 +4787,14 @@ private: ASSERT_EQUALS("", errout.str()); } + void simplifyTypedef75() // ticket #2426 + { + const char code[] = "typedef _Packed struct S { long l; }; \n"; + const std::string expected = ";"; + ASSERT_EQUALS(expected, sizeof_(code)); + ASSERT_EQUALS("", errout.str()); + } + void simplifyTypedefFunction1() { { From 9b66f7a754e53c3118d949320ae29d2d0cd172f7 Mon Sep 17 00:00:00 2001 From: Ettl Martin Date: Fri, 7 Jan 2011 12:27:12 +0100 Subject: [PATCH 9/9] #ticket 2429: added a test to the mathlib to ensure the used floating point number is recognized correctly --- lib/filelister_win32.cpp | 2 +- test/testmathlib.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/filelister_win32.cpp b/lib/filelister_win32.cpp index 565bd579b..a9a8f895d 100644 --- a/lib/filelister_win32.cpp +++ b/lib/filelister_win32.cpp @@ -88,7 +88,7 @@ static BOOL MyIsDirectory(std::string path) return (GetFileAttributes(path.c_str()) & FILE_ATTRIBUTE_DIRECTORY); #else // See http://msdn.microsoft.com/en-us/library/bb773621(VS.85).aspx -return PathIsDirectory(path.c_str()); + return PathIsDirectory(path.c_str()); #endif } diff --git a/test/testmathlib.cpp b/test/testmathlib.cpp index 4e0f2d3d2..155dec47c 100644 --- a/test/testmathlib.cpp +++ b/test/testmathlib.cpp @@ -262,6 +262,7 @@ private: ASSERT_EQUALS(false , MathLib::isFloat("+1E+10000")); ASSERT_EQUALS(false , MathLib::isFloat("-1E+1")); ASSERT_EQUALS(false , MathLib::isFloat("-1E+10000")); + ASSERT_EQUALS(true , MathLib::isFloat(".1250E+04")); ASSERT_EQUALS(true , MathLib::isFloat("-1E-1")); ASSERT_EQUALS(true , MathLib::isFloat("-1E-10000"));