void FunctionArgsOrderChecker::preCheckNode(const ast::Exp & e, SLintContext & context, SLintResult & result) { const ast::CallExp & ce = static_cast<const ast::CallExp &>(e); if (ce.getName().isSimpleVar()) { ast::exps_t args = ce.getArgs(); std::map<symbol::Symbol, unsigned int> assignments; unsigned int pos = 1; for (const auto arg : args) { if (arg->isAssignExp()) { const ast::AssignExp & ae = *static_cast<const ast::AssignExp *>(arg); if (ae.getLeftExp().isSimpleVar()) { assignments.emplace(static_cast<ast::SimpleVar &>(ae.getLeftExp()).getSymbol(), pos); } } else if (!assignments.empty()) { result.report(context, e.getLocation(), *this, _("Argument at position %d must be an assignment."), pos); } ++pos; } if (!assignments.empty()) { const std::wstring & name = static_cast<const ast::SimpleVar &>(ce.getName()).getSymbol().getName(); const ast::FunctionDec * fundec = context.getPublicFunction(name); if (!fundec) { fundec = context.getPrivateFunction(name); } if (fundec) { const ast::exps_t & funargs = fundec->getArgs().getVars(); pos = 1; for (const auto arg : funargs) { const symbol::Symbol & sym = static_cast<const ast::SimpleVar *>(arg)->getSymbol(); auto i = assignments.find(sym); if (i != assignments.end()) { if (pos != i->second) { result.report(context, e.getLocation(), *this, _("Argument %s declared at position %d and assigned at position %d."), sym.getName(), pos, i->second); } } ++pos; } } } } }
void SLintXmlResult::handleMessage(SLintContext & context, const Location & loc, const SLintChecker & checker, const std::wstring & msg) { if (context.getSciFile().get() != current.get()) { if (current.get()) { (*out) << " </File>\n"; } current = context.getSciFile(); print(current); } print(loc, checker, msg); }
void CNESCsvResult::handleMessage(SLintContext & context, const Location & loc, const SLintChecker & checker, const unsigned sub, const std::wstring & msg) { if (context.getSciFile().get() != current.get()) { printRes(); current = context.getSciFile(); } if (const ast::FunctionDec * fd = context.topFn()) { res[checker.getId(sub)].emplace_back(loc, msg, fd->getSymbol().getName()); } else { res[checker.getId(sub)].emplace_back(loc, msg, L""); } }
void UselessRetChecker::preCheckNode(const ast::Exp & e, SLintContext & context, SLintResult & result) { if (e.isFunctionDec()) { const ast::exps_t & rets = static_cast<const ast::FunctionDec &>(e).getReturns().getVars(); useless.push(std::map<symbol::Symbol, Location>()); std::map<symbol::Symbol, Location> & map = useless.top(); for (const auto ret : rets) { const ast::SimpleVar & var = *static_cast<const ast::SimpleVar *>(ret); map.emplace(var.getSymbol(), var.getLocation()); } } else if (e.isSimpleVar()) { const ast::SimpleVar & var = static_cast<const ast::SimpleVar &>(e); if (context.isFunOut(var.getSymbol()) && context.isAssignedVar(var)) { useless.top().erase(var.getSymbol()); } } }
void VariablesChecker::postCheckNode(const ast::Exp & e, SLintContext & context, SLintResult & result) { if (e.isFunctionDec()) { const ast::FunctionDec & fd = static_cast<const ast::FunctionDec &>(e); auto & map = assigned.top(); const auto & out = context.getFunOut(); for (const auto & n : out) { map.erase(n); } map.erase(fd.getSymbol().getName()); for (const auto & p : map) { result.report(context, p.second.first, *this, _("Declared variable and might be unused: %s."), p.first); } assigned.pop(); used.pop(); } }
void SLintScilabOut::handleMessage(SLintContext & context, const Location & loc, const SLintChecker & checker, const std::wstring & msg) { results[context.getFilename()][checker.getId()].emplace_back(loc, msg); }
void SLintScilabResult::handleMessage(SLintContext & context, const Location & loc, const SLintChecker & checker, const std::wstring & msg) { auto & mmap = results[context.getFilename()]; mmap.emplace(loc, checker.getId() + L": " + msg); }
void FunctionTestReturnChecker::preCheckNode(const ast::Exp & e, SLintContext & context, SLintResult & result) { const ast::CallExp & ce = static_cast<const ast::CallExp &>(e); if (ce.getName().isSimpleVar()) { const std::wstring & name = static_cast<const ast::SimpleVar &>(ce.getName()).getSymbol().getName(); auto i = funs.find(name); if (i != funs.end()) { const std::vector<unsigned int> & positions = i->second; if (!positions.empty()) { const ast::AssignListExp * ale = nullptr; const ast::AssignExp * ae = context.getAssignExp(); if (ae) { if (ae->getLeftExp().isAssignListExp()) { ale = &static_cast<const ast::AssignListExp &>(ae->getLeftExp()); } // syms will contain the symbols associated to returned error code std::set<symbol::Symbol> syms; if (ale) { const ast::exps_t & exps = ale->getExps(); for (const auto pos : positions) { if (pos > exps.size()) { result.report(context, e.getLocation(), *this, _("Function %s requires an error checking just after call."), name); return; } else { if (exps[pos - 1] && exps[pos - 1]->isSimpleVar()) { syms.emplace(static_cast<ast::SimpleVar *>(exps[pos - 1])->getSymbol()); } } } } else if (ae->getLeftExp().isSimpleVar()) { if (positions.size() > 1 || positions.back() != 1) { result.report(context, e.getLocation(), *this, _("Function %s requires an error checking just after call."), name); return; } syms.emplace(static_cast<ast::SimpleVar &>(ae->getLeftExp()).getSymbol()); } if (const ast::Exp * next = context.getNextRelevantExp()) { if (next->isIfExp()) { const ast::IfExp * ie = static_cast<const ast::IfExp *>(next); // We look for syms in the test: found syms are removed from syms FindSymVisitor(syms, ie->getTest()); if (!syms.empty()) { result.report(context, e.getLocation(), *this, _("Function %s requires an error checking just after call."), name); return; } } } } else { result.report(context, e.getLocation(), *this, _("Function %s requires an error checking just after call."), name); return; } } } } }
void VariablesChecker::preCheckNode(const ast::Exp & e, SLintContext & context, SLintResult & result) { if (e.isFunctionDec()) { const ast::FunctionDec & fd = static_cast<const ast::FunctionDec &>(e); if (!assigned.empty()) { // we declare the function in the current scope std::pair<Location, ast::AssignListExp *> p = { e.getLocation(), nullptr }; assigned.top().emplace(fd.getSymbol().getName(), p); } assigned.emplace(std::unordered_map<std::wstring, std::pair<Location, ast::AssignListExp *>>()); used.emplace(std::unordered_map<std::wstring, const ast::Exp *>()); // a function cans refer to itself std::pair<Location, ast::AssignListExp *> p = { e.getLocation(), nullptr }; assigned.top().emplace(fd.getSymbol().getName(), p); } else { if (!used.empty()) { if (e.isSimpleVar()) { const ast::SimpleVar & var = static_cast<const ast::SimpleVar &>(e); if (context.isAssignedVar(var)) { // if we are not in the context on a nested assignment in a function call (foo(a,b=2)) if (!var.getParent()->getParent() || !var.getParent()->getParent()->isCallExp()) { const std::wstring & name = var.getSymbol().getName(); auto i = used.top().find(name); if (!context.topLoop() || (i == used.top().end()) || !isParentOf(context.topLoop(), i->second)) { // the variable has never been used or the last time it wasn't in a loop if (i != used.top().end()) { used.top().erase(i); } if (var.getParent() == context.getLHSExp() && var.getParent()->isAssignListExp()) { // we have something like [lhs, rhs] = argn(0) // if rhs is used and lhs is not used then this not an "error": // we are obliged to define lhs just to get rhs. // So when rhs is used lhs is "used" too. std::pair<Location, ast::AssignListExp *> p = { var.getLocation(), static_cast<ast::AssignListExp *>(var.getParent()) }; assigned.top().emplace(name, p); } else { std::pair<Location, ast::AssignListExp *> p = { var.getLocation(), nullptr }; assigned.top().emplace(name, p); } } else /*if (context.topLoop() && i != used.top.end() && isParentOf(context.topLoop(), i->second))*/ { // Just to remember... // Here the variable has already been used in the current loop and we reassign it // something like: // while (...) // ... use i // ... // i = ... // end // In general the loop will looping so the assignment is implicitly destroyed by the use in the next iteration // so we don't add this assignment ! } } } else if (!e.getParent()->isFieldExp() || static_cast<const ast::FieldExp *>(e.getParent())->getTail() != &e) { const symbol::Symbol & sym = var.getSymbol(); const std::wstring & name = sym.getName(); if (used.top().find(name) == used.top().end()) { used.top().emplace(name, context.topLoop()); auto i = assigned.top().find(name); if (i == assigned.top().end()) { if (!context.isFunIn(name) && !SLintChecker::isScilabConstant(name)) { types::InternalType * pIT = symbol::Context::getInstance()->get(sym); if (pIT) { if (!pIT->isFunction() && !pIT->isMacroFile() && !pIT->isMacro()) { result.report(context, e.getLocation(), *this, _("Use of non-initialized variable \'%s\' may have any side-effects."), name); } } else if (!context.isPrivateFunction(sym)) { /* The symbol doesn't correspond to a private function: function tata() titi() end function titi() ... end titi is private but usable in tata. */ std::wstring fname; if (context.isExternPrivateFunction(sym, fname)) { result.report(context, e.getLocation(), *this, _("Use of a private macro \'%s\' defined in an other file %s."), name, fname); } else if (!context.getPublicFunction(fname)) { // The macro has not been declared somewhere in the project result.report(context, e.getLocation(), *this, _("Use of non-initialized variable \'%s\' may have any side-effects."), name); } } } } else { if (ast::AssignListExp * ale = i->second.second) { // the variable is in an AssignListExp // so we must "use" the variables which preceed it too for (auto e : ale->getExps()) { if (e->isSimpleVar()) { const std::wstring & prevName = static_cast<ast::SimpleVar *>(e)->getSymbol().getName(); assigned.top().erase(prevName); if (prevName == name) { break; } else { used.top().emplace(prevName, context.topLoop()); } } } } else { assigned.top().erase(i); } } } } } // for i=1:10... end even if i is not used, i is useful to make the loop /*else if (e.isVarDec()) { const ast::VarDec & vd = static_cast<const ast::VarDec &>(e); const std::wstring & name = vd.getSymbol().getName(); std::pair<Location, ast::AssignListExp *> p = { vd.getLocation(), nullptr }; assigned.top().emplace(name, p); used.top().erase(name); }*/ } } }