| 1422 | } |
| 1423 | |
| 1424 | void CppCheck::executeRules(const std::string &tokenlist, const TokenList &list) |
| 1425 | { |
| 1426 | // There is no rule to execute |
| 1427 | if (!hasRule(tokenlist)) |
| 1428 | return; |
| 1429 | |
| 1430 | // Write all tokens in a string that can be parsed by pcre |
| 1431 | std::string str; |
| 1432 | for (const Token *tok = list.front(); tok; tok = tok->next()) { |
| 1433 | str += " "; |
| 1434 | str += tok->str(); |
| 1435 | } |
| 1436 | |
| 1437 | for (const Rule &rule : mSettings.rules) { |
| 1438 | if (rule.tokenlist != tokenlist) |
| 1439 | continue; |
| 1440 | |
| 1441 | if (!mSettings.quiet) { |
| 1442 | mErrorLogger.reportOut("Processing rule: " + rule.pattern, Color::FgGreen); |
| 1443 | } |
| 1444 | |
| 1445 | auto f = [&](int pos1, int pos2) { |
| 1446 | // determine location.. |
| 1447 | int fileIndex = 0; |
| 1448 | int line = 0; |
| 1449 | |
| 1450 | std::size_t len = 0; |
| 1451 | for (const Token *tok = list.front(); tok; tok = tok->next()) { |
| 1452 | len = len + 1U + tok->str().size(); |
| 1453 | if (len > pos1) { |
| 1454 | fileIndex = tok->fileIndex(); |
| 1455 | line = tok->linenr(); |
| 1456 | break; |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | const std::string& file = list.getFiles()[fileIndex]; |
| 1461 | |
| 1462 | ErrorMessage::FileLocation loc(file, line, 0); |
| 1463 | |
| 1464 | // Create error message |
| 1465 | const ErrorMessage errmsg({std::move(loc)}, |
| 1466 | list.getSourceFilePath(), |
| 1467 | rule.severity, |
| 1468 | !rule.summary.empty() ? rule.summary : "found '" + str.substr(pos1, pos2 - pos1) + "'", |
| 1469 | rule.id, |
| 1470 | Certainty::normal); |
| 1471 | |
| 1472 | // Report error |
| 1473 | mErrorLogger.reportErr(errmsg); |
| 1474 | }; |
| 1475 | |
| 1476 | const std::string err = rule.regex->match(str, f); |
| 1477 | if (!err.empty()) { |
| 1478 | const ErrorMessage errmsg(std::list<ErrorMessage::FileLocation>(), |
| 1479 | "", |
| 1480 | Severity::error, |
| 1481 | err, |