CheckOther: Use Variable::valueType() instead of Variable::typeStartToken

This commit is contained in:
Daniel Marjamäki 2018-06-23 16:42:36 +02:00
parent ee335e5151
commit 2a8296879c
3 changed files with 26 additions and 17 deletions

View File

@ -1425,11 +1425,7 @@ void CheckOther::checkPassByReference()
bool inconclusive = false; bool inconclusive = false;
const Token* const tok = var->typeStartToken(); if (var->valueType()->type == ValueType::Type::CONTAINER) {
if (var->isStlStringType()) {
;
} else if (var->isStlType() && Token::Match(tok, "std :: %type% <") && !Token::simpleMatch(tok->linkAt(3), "> ::") && !Token::Match(tok->tokAt(2), "initializer_list|weak_ptr|auto_ptr|unique_ptr|shared_ptr|function|pair")) {
;
} else if (var->type() && !var->type()->isEnumType()) { // Check if type is a struct or class. } else if (var->type() && !var->type()->isEnumType()) { // Check if type is a struct or class.
// Ensure that it is a large object. // Ensure that it is a large object.
if (!var->type()->classScope) if (!var->type()->classScope)
@ -1444,7 +1440,7 @@ void CheckOther::checkPassByReference()
const bool isConst = var->isConst(); const bool isConst = var->isConst();
if (isConst) { if (isConst) {
passedByValueError(tok, var->name(), inconclusive); passedByValueError(var->nameToken(), var->name(), inconclusive);
continue; continue;
} }
@ -1453,7 +1449,7 @@ void CheckOther::checkPassByReference()
continue; continue;
if (canBeConst(var)) { if (canBeConst(var)) {
passedByValueError(tok, var->name(), inconclusive); passedByValueError(var->nameToken(), var->name(), inconclusive);
} }
} }
} }

View File

@ -904,17 +904,21 @@ const Library::Container* Library::detectContainer(const Token* typeStart, bool
if (container.startPattern.empty()) if (container.startPattern.empty())
continue; continue;
if (Token::Match(typeStart, container.startPattern.c_str())) { if (!endsWith(container.startPattern, '<')) {
if (!iterator && container.endPattern.empty()) // If endPattern is undefined, it will always match, but itEndPattern has to be defined. if (!Token::Match(typeStart, (container.startPattern + " !!::").c_str()))
return &container; continue;
} else if (!Token::Match(typeStart, (container.startPattern + " !!::").c_str()))
continue;
for (const Token* tok = typeStart; tok && !tok->varId(); tok = tok->next()) { if (!iterator && container.endPattern.empty()) // If endPattern is undefined, it will always match, but itEndPattern has to be defined.
if (tok->link()) { return &container;
const std::string& endPattern = iterator ? container.itEndPattern : container.endPattern;
if (Token::Match(tok->link(), endPattern.c_str())) for (const Token* tok = typeStart; tok && !tok->varId(); tok = tok->next()) {
return &container; if (tok->link()) {
break; const std::string& endPattern = iterator ? container.itEndPattern : container.endPattern;
} if (Token::Match(tok->link(), endPattern.c_str()))
return &container;
break;
} }
} }
} }

View File

@ -148,6 +148,7 @@ private:
TEST_CASE(VariableValueType1); TEST_CASE(VariableValueType1);
TEST_CASE(VariableValueType2); TEST_CASE(VariableValueType2);
TEST_CASE(VariableValueType3);
TEST_CASE(findVariableType1); TEST_CASE(findVariableType1);
TEST_CASE(findVariableType2); TEST_CASE(findVariableType2);
@ -808,6 +809,14 @@ private:
ASSERT(x->valueType()->isIntegral()); ASSERT(x->valueType()->isIntegral());
} }
void VariableValueType3() {
GET_SYMBOL_DB("void f(std::string::size_type x);\n");
const Variable* x = db->getVariableFromVarId(1);
ASSERT_EQUALS("x", x->name());
// TODO: Configure std::string::size_type somehow.
TODO_ASSERT_EQUALS(ValueType::Type::LONGLONG, ValueType::Type::UNKNOWN_TYPE, x->valueType()->type);
}
void findVariableType1() { void findVariableType1() {
GET_SYMBOL_DB("class A {\n" GET_SYMBOL_DB("class A {\n"
"public:\n" "public:\n"