Permalink
Browse files

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

  • Loading branch information...
2 parents b89adff + 9be2d1c commit 0415444e28355243eabcf2d8f4936bfa18ef2b8f @zblair zblair committed Jan 20, 2012
View
@@ -11,34 +11,73 @@
class SelectFilesModel : public QFileSystemModel {
private:
- QStringList selected;
- QStringList unselected;
+ /**
+ * paths that are user-checked. on the screen all children
+ * for these paths will appear to be checked too unless
+ * they are "unchecked".
+ */
+ QStringList checked;
+
+ /**
+ * paths that are user-unchecked.
+ */
+ QStringList unchecked;
/**
* Get index in stringlist where start of string matches. If
* many strings in the stringlist match then return the index
* for the longest string.
- * \param s stringlist with filepaths
+ * \param paths stringlist with filepaths
* \param filepath the filepath that is matched against the stringlist
*/
- int getindex(const QStringList &s, const QString &filepath) const {
+ int getindex(const QStringList &paths, const QString &filepath) const {
int matchlen = 0;
int matchindex = -1;
- for (int i = 0; i < s.size(); ++i) {
- if (filepath.startsWith(s[i])) {
+ for (int i = 0; i < paths.size(); ++i) {
+ if (filepath.startsWith(paths[i])) {
// not a real match of paths..
- if (s[i].size() < filepath.size() && filepath[s[i].size()] != '/')
+ if (paths[i].size() < filepath.size() && filepath[paths[i].size()] != '/')
continue;
// paths match. the return value is the index for the
// longest match
- if (s[i].size() > matchlen)
+ if (paths[i].size() > matchlen)
matchindex = i;
}
}
return matchindex;
}
+ /**
+ * Is filepath partially checked?
+ * \param filepath the filepath to investigate
+ * \param checkindex result from getindex(checked,filepath). If not given the getindex will be called.
+ * \return true if filepath is partially checked
+ */
+ bool partiallyChecked(const QString &filepath, int checkindex = -2) const {
+ const QString filepath2 = filepath.endsWith("/") ? filepath : (filepath + "/");
+
+ for (int i = 0; i < unchecked.size(); ++i) {
+ if (unchecked[i].startsWith(filepath2)) {
+ return true;
+ }
+ }
+
+ if (checkindex == -2)
+ checkindex = getindex(checked, filepath);
+
+
+ if (checkindex == -1) {
+ for (int i = 0; i < checked.size(); ++i) {
+ if (checked[i].startsWith(filepath2)) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
public:
SelectFilesModel() : QFileSystemModel() {
class FileLister : private FileList {
@@ -61,14 +100,21 @@ class SelectFilesModel : public QFileSystemModel {
QVariant data(const QModelIndex& index, int role=Qt::DisplayRole) const {
if (role == Qt::CheckStateRole) {
const QString filepath = filePath(index);
- int selindex = getindex(selected, filepath);
- int unselindex = getindex(unselected, filepath);
- if (selindex >= 0 && unselindex == -1)
+ const int checkindex = getindex(checked, filepath);
+ const int uncheckindex = getindex(unchecked, filepath);
+
+ // If some children are not checked then this item should be partially checked..
+ if (partiallyChecked(filepath, checkindex))
+ return Qt::PartiallyChecked;
+
+ // Is item selected but not unselected?
+ if (checkindex >= 0 && uncheckindex == -1)
return Qt::Checked;
- if (selindex >= 0 && unselindex >= 0 &&
- selected[selindex].size() > unselected[unselindex].size())
+ if (checkindex >= 0 && uncheckindex >= 0 &&
+ checked[checkindex].size() > unchecked[uncheckindex].size())
return Qt::Checked;
+ // Item is either not selected at all or else it is unselected
return Qt::Unchecked;
}
return QFileSystemModel::data(index, role);
@@ -77,35 +123,49 @@ class SelectFilesModel : public QFileSystemModel {
bool setData(const QModelIndex& index, const QVariant& value, int role) {
if (role == Qt::CheckStateRole) {
const QString filepath = filePath(index);
- if (unselected.indexOf(filepath) != -1) {
+
+ bool partiallychecked = partiallyChecked(filepath);
+
+ if (unchecked.indexOf(filepath) != -1) {
// remove unchecked path
- unselected.removeAll(filepath);
- } else if (selected.indexOf(filepath) != -1) {
+ unchecked.removeAll(filepath);
+ } else if (partiallychecked || checked.indexOf(filepath) != -1) {
// remove child selected paths
- for (int i = selected.size() - 1; i >= 0; --i) {
- if (selected[i].startsWith(filepath))
- selected.removeAt(i);
+ for (int i = checked.size() - 1; i >= 0; --i) {
+ if (checked[i].startsWith(filepath))
+ checked.removeAt(i);
}
// remove child unselected paths
- for (int i = unselected.size() - 1; i >= 0; --i) {
- if (unselected[i].startsWith(filepath))
- unselected.removeAt(i);
+ for (int i = unchecked.size() - 1; i >= 0; --i) {
+ if (unchecked[i].startsWith(filepath))
+ unchecked.removeAt(i);
}
+
+ // If partialChecked then select this item
+ if (partiallychecked)
+ checked.append(filepath);
} else {
- int selindex = getindex(selected, filepath);
- int unselindex = getindex(unselected, filepath);
- if (selindex == -1)
- selected.append(filepath);
- else if (unselindex >= 0 && selected[selindex].size() < unselected[unselindex].size())
- selected.append(filepath);
+ const int checkindex = getindex(checked, filepath);
+ const int uncheckindex = getindex(unchecked, filepath);
+ if (checkindex == -1)
+ checked.append(filepath);
+ else if (uncheckindex >= 0 && checked[checkindex].size() < unchecked[uncheckindex].size())
+ checked.append(filepath);
else
- unselected.append(filepath);
+ unchecked.append(filepath);
}
if (rowCount(index) > 0)
emit(dataChanged(index, index.child(rowCount(index)-1,0)));
+ // update parents
+ QModelIndex parent = index.parent();
+ while (parent != QModelIndex()) {
+ emit(dataChanged(parent,parent));
+ parent = parent.parent();
+ }
+
return true;
}
return QFileSystemModel::setData(index, value, role);
@@ -114,20 +174,20 @@ class SelectFilesModel : public QFileSystemModel {
QStringList getFiles() const {
QStringList ret;
- // List all files in "selected" folders..
+ // List all files in "checked" folders..
FileList fileLister;
- fileLister.AddPathList(selected);
+ fileLister.AddPathList(checked);
ret = fileLister.GetFileList();
- // Remove all items from ret that are unselected but not selected..
+ // Remove all items from ret that are unchecked but not checked..
for (int i = ret.size() - 1; i >= 0; i--) {
- int unselindex = getindex(unselected, ret[i]);
- if (unselindex == -1)
+ int uncheckindex = getindex(unchecked, ret[i]);
+ if (uncheckindex == -1)
continue;
- // both selected and unselected, check which to rely on
- int selindex = getindex(selected, ret[i]);
- if (selected[selindex].size() < unselected[unselindex].size())
+ // both checked and unchecked, check which to rely on
+ int checkindex = getindex(checked, ret[i]);
+ if (checked[checkindex].size() < unchecked[uncheckindex].size())
ret.removeAt(i);
}
View
@@ -442,15 +442,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<std::string>
// the function is external and it's neither friend nor inherited virtual function.
// assume all variables that are passed to it are initialized..
else {
- unsigned int indentlevel2 = 0;
- for (const Token *tok = ftok->tokAt(2); tok; tok = tok->next()) {
- if (tok->str() == "(")
- ++indentlevel2;
- else if (tok->str() == ")") {
- if (indentlevel2 == 0)
- break;
- --indentlevel2;
- }
+ for (const Token *tok = ftok->tokAt(2); tok && tok != ftok->next()->link(); tok = tok->next()) {
if (tok->isName()) {
assignVar(tok->str(), scope, usage);
}
View
@@ -434,23 +434,14 @@ void CheckNullPointer::nullPointerLinkedList()
const std::string varname(tok2->str());
// Check usage of dereferenced variable in the loop..
- unsigned int indentlevel3 = 0;
- for (const Token *tok3 = tok1->next()->link(); tok3; tok3 = tok3->next()) {
- if (tok3->str() == "{")
- ++indentlevel3;
- else if (tok3->str() == "}") {
- if (indentlevel3 <= 1)
- break;
- --indentlevel3;
- }
-
+ for (const Token *tok3 = i->classStart; tok3 && tok3 != i->classEnd; tok3 = tok3->next()) {
// TODO: are there false negatives for "while ( %varid% ||"
- else if (Token::Match(tok3, "while ( %varid% &&|)", varid)) {
+ if (Token::Match(tok3, "while ( %varid% &&|)", varid)) {
// Make sure there is a "break" or "return" inside the loop.
// Without the "break" a null pointer could be dereferenced in the
// for statement.
// indentlevel4 is a counter for { and }. When scanning the code with tok4
- unsigned int indentlevel4 = indentlevel3;
+ unsigned int indentlevel4 = 1;
for (const Token *tok4 = tok3->next()->link(); tok4; tok4 = tok4->next()) {
if (tok4->str() == "{")
++indentlevel4;
View
@@ -1212,7 +1212,8 @@ void CheckOther::invalidScanf()
}
else if (std::isalpha(formatstr[i])) {
- invalidScanfError(tok);
+ if (formatstr[i] != 'c') // #3490 - field width limits are not necessary for %c
+ invalidScanfError(tok);
format = false;
}
}
@@ -1747,8 +1748,7 @@ void CheckOther::checkVariableScope()
continue;
// Walk through all tokens..
- unsigned int indentlevel = 0;
- for (const Token *tok = scope->classStart; tok; tok = tok->next()) {
+ for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) {
// Skip function local class and struct declarations..
if ((tok->str() == "class") || (tok->str() == "struct") || (tok->str() == "union")) {
for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) {
@@ -1764,15 +1764,7 @@ void CheckOther::checkVariableScope()
break;
}
- else if (tok->str() == "{") {
- ++indentlevel;
- } else if (tok->str() == "}") {
- if (!indentlevel)
- break;
- --indentlevel;
- }
-
- if (indentlevel > 0 && Token::Match(tok, "[{};]")) {
+ if (Token::Match(tok, "[{};]")) {
// First token of statement..
const Token *tok1 = tok->next();
if (! tok1)
@@ -2004,7 +1996,7 @@ void CheckOther::checkCharVariable()
continue;
// This is an error..
- charBitOpError(tok);
+ charBitOpError(tok->tokAt(4));
}
else if (Token::Match(tok, "[;{}] %var% = %any% [&|] ( * %var% ) ;")) {
@@ -2021,7 +2013,7 @@ void CheckOther::checkCharVariable()
continue;
// This is an error..
- charBitOpError(tok);
+ charBitOpError(tok->tokAt(4));
}
}
}
View
@@ -67,6 +67,7 @@ class CheckOther : public Check {
checkOther.checkDuplicateExpression();
checkOther.checkUnreachableCode();
checkOther.checkSuspiciousSemicolon();
+ checkOther.checkWrongPrintfScanfArguments();
// information checks
checkOther.checkVariableScope();
@@ -91,7 +92,6 @@ class CheckOther : public Check {
checkOther.checkCCTypeFunctions();
checkOther.checkFflushOnInputStream();
checkOther.invalidScanf();
- checkOther.checkWrongPrintfScanfArguments();
checkOther.checkCoutCerrMisusage();
checkOther.checkIncorrectLogicOperator();
View
@@ -265,35 +265,16 @@ void CheckStl::stlOutOfBounds()
continue;
// check if the for loop condition is wrong
- unsigned int indent = 0;
- for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next()) {
- if (tok2->str() == "(")
- ++indent;
-
- else if (tok2->str() == ")") {
- if (indent == 0)
- break;
- --indent;
- }
-
+ for (const Token *tok2 = tok->tokAt(2); tok2 && tok2 != tok->next()->link(); tok2 = tok2->next()) {
if (Token::Match(tok2, "; %var% <= %var% . size ( ) ;")) {
- // Count { and } for tok3
- unsigned int indent3 = 0;
-
// variable id for loop variable.
unsigned int numId = tok2->next()->varId();
// variable id for the container variable
unsigned int varId = tok2->tokAt(3)->varId();
- for (const Token *tok3 = tok2->tokAt(8); tok3; tok3 = tok3->next()) {
- if (tok3->str() == "{")
- ++indent3;
- else if (tok3->str() == "}") {
- if (indent3 <= 1)
- break;
- --indent3;
- } else if (tok3->varId() == varId) {
+ for (const Token *tok3 = tok2->tokAt(8); tok3 && tok3 != i->classEnd; tok3 = tok3->next()) {
+ if (tok3->varId() == varId) {
if (Token::simpleMatch(tok3->next(), ". size ( )"))
break;
else if (Token::Match(tok3->next(), "[ %varid% ]", numId))
View
@@ -830,15 +830,7 @@ class UninitVar : public ExecutionPath {
// Check that the variable hasn't been initialized and
// that it isn't initialized in the body..
if (varid1.find(varid) == varid1.end()) {
- unsigned int indentlevel = 0;
- for (const Token *tok3 = tok2->tokAt(5); tok3; tok3 = tok3->next()) {
- if (tok3->str() == "{")
- ++indentlevel;
- else if (tok3->str() == "}") {
- if (indentlevel == 0)
- break;
- --indentlevel;
- }
+ for (const Token *tok3 = tok2->tokAt(5); tok3 && tok3 != tok2->linkAt(4); tok3 = tok3->next()) {
if (tok3->varId() == varid) {
varid = 0; // variable is used.. maybe it's initialized. clear the variable id.
break;
Oops, something went wrong.

0 comments on commit 0415444

Please sign in to comment.