MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / mismatchingContainerIterator

Method mismatchingContainerIterator

lib/checkstl.cpp:869–918  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

867}
868
869void CheckStlImpl::mismatchingContainerIterator()
870{
871 logChecker("CheckStl::misMatchingContainerIterator");
872
873 // Check if different containers are used in various calls of standard functions
874 const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
875 for (const Scope * scope : symbolDatabase->functionScopes) {
876 for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
877 if (!astIsContainer(tok))
878 continue;
879 if (!astIsLHS(tok))
880 continue;
881 if (!Token::Match(tok->astParent(), ". %name% ( !!)"))
882 continue;
883 const Token* const ftok = tok->astParent()->next();
884 const std::vector<const Token *> args = getArguments(ftok);
885
886 const Library::Container * c = tok->valueType()->container;
887 const Library::Container::Action action = c->getAction(tok->strAt(2));
888 const Token* iterTok = nullptr;
889 if (action == Library::Container::Action::INSERT && args.size() == 2) {
890 // Skip if iterator pair
891 if (astIsIterator(args.back()))
892 continue;
893 if (!astIsIterator(args.front()))
894 continue;
895 iterTok = args.front();
896 } else if (action == Library::Container::Action::ERASE) {
897 if (!astIsIterator(args.front()))
898 continue;
899 iterTok = args.front();
900 } else {
901 continue;
902 }
903
904 ValueFlow::Value val = getLifetimeIteratorValue(iterTok);
905 if (!val.tokvalue)
906 continue;
907 if (!val.isKnown() && Token::simpleMatch(val.tokvalue->astParent(), ":"))
908 continue;
909 if (val.lifetimeKind != ValueFlow::Value::LifetimeKind::Iterator)
910 continue;
911 if (iterTok->str() == "*" && iterTok->astOperand1()->valueType() && iterTok->astOperand1()->valueType()->type == ValueType::ITERATOR)
912 continue;
913 if (isSameIteratorContainerExpression(tok, val.tokvalue, mSettings))
914 continue;
915 mismatchingContainerIteratorError(tok, iterTok, val.tokvalue);
916 }
917 }
918}
919
920static const Token* getInvalidMethod(const Token* tok)
921{

Callers 1

runChecksMethod · 0.45

Calls 15

astIsContainerFunction · 0.85
astIsLHSFunction · 0.85
astIsIteratorFunction · 0.85
getLifetimeIteratorValueFunction · 0.85
nextMethod · 0.80
astParentMethod · 0.80
getActionMethod · 0.80
frontMethod · 0.80
isKnownMethod · 0.80
astOperand1Method · 0.80
getArgumentsFunction · 0.70

Tested by

no test coverage detected