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

Function createTokenFromExpression

lib/programmemory.cpp:1830–1875  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1828}
1829
1830static std::shared_ptr<Token> createTokenFromExpression(const std::string& returnValue,
1831 const Settings& settings,
1832 bool cpp,
1833 std::unordered_map<nonneg int, const Token*>& lookupVarId)
1834{
1835 std::shared_ptr<TokenList> tokenList = std::make_shared<TokenList>(settings, cpp ? Standards::Language::CPP : Standards::Language::C);
1836 {
1837 const std::string code = "return " + returnValue + ";";
1838 if (!tokenList->createTokensFromBuffer(code.data(), code.size()))
1839 return nullptr;
1840 }
1841
1842 // TODO: put in a helper?
1843 // combine operators, set links, etc..
1844 std::stack<Token*> lpar;
1845 for (Token* tok2 = tokenList->front(); tok2; tok2 = tok2->next()) {
1846 if (Token::Match(tok2, "[!<>=] =")) {
1847 tok2->str(tok2->str() + "=");
1848 tok2->deleteNext();
1849 } else if (tok2->str() == "(")
1850 lpar.push(tok2);
1851 else if (tok2->str() == ")") {
1852 if (lpar.empty())
1853 return nullptr;
1854 Token::createMutualLinks(lpar.top(), tok2);
1855 lpar.pop();
1856 }
1857 }
1858 if (!lpar.empty())
1859 return nullptr;
1860
1861 // set varids
1862 for (Token* tok2 = tokenList->front(); tok2; tok2 = tok2->next()) {
1863 if (!startsWith(tok2->str(), "arg"))
1864 continue;
1865 nonneg int const id = strToInt<nonneg int>(tok2->str().c_str() + 3);
1866 tok2->varId(id);
1867 lookupVarId[id] = tok2;
1868 }
1869
1870 // Evaluate expression
1871 tokenList->createAst();
1872 Token* expr = tokenList->front()->astOperand1();
1873 ValueFlow::valueFlowConstantFoldAST(expr, settings);
1874 return {tokenList, expr};
1875}
1876
1877ValueFlow::Value evaluateLibraryFunction(const std::unordered_map<nonneg int, ValueFlow::Value>& args,
1878 const std::string& returnValue,

Callers 1

evaluateLibraryFunctionFunction · 0.85

Calls 11

frontMethod · 0.80
nextMethod · 0.80
deleteNextMethod · 0.80
createAstMethod · 0.80
astOperand1Method · 0.80
startsWithFunction · 0.70
dataMethod · 0.45
sizeMethod · 0.45
strMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected