| 636 | } |
| 637 | |
| 638 | std::string ErrorMessage::toString(bool verbose, const std::string &templateFormat, const std::string &templateLocation) const |
| 639 | { |
| 640 | assert(!templateFormat.empty()); |
| 641 | |
| 642 | // template is given. Reformat the output according to it |
| 643 | std::string result = templateFormat; |
| 644 | |
| 645 | // replace id with guideline if present |
| 646 | // replace severity with classification if present |
| 647 | const std::string idStr = guideline.empty() ? id : guideline; |
| 648 | const std::string severityStr = classification.empty() ? severityToString(severity) : classification; |
| 649 | |
| 650 | findAndReplace(result, "{id}", idStr); |
| 651 | |
| 652 | std::string::size_type pos1 = result.find("{inconclusive:"); |
| 653 | while (pos1 != std::string::npos) { |
| 654 | const std::string::size_type pos2 = result.find('}', pos1+1); |
| 655 | const std::string replaceFrom = result.substr(pos1,pos2-pos1+1); |
| 656 | const std::string replaceWith = (certainty == Certainty::inconclusive) ? result.substr(pos1+14, pos2-pos1-14) : std::string(); |
| 657 | findAndReplace(result, replaceFrom, replaceWith); |
| 658 | pos1 = result.find("{inconclusive:", pos1); |
| 659 | } |
| 660 | findAndReplace(result, "{severity}", severityStr); |
| 661 | findAndReplace(result, "{cwe}", std::to_string(cwe.id)); |
| 662 | findAndReplace(result, "{message}", verbose ? mVerboseMessage : mShortMessage); |
| 663 | findAndReplace(result, "{remark}", remark); |
| 664 | if (!callStack.empty()) { |
| 665 | if (result.find("{callstack}") != std::string::npos) |
| 666 | findAndReplace(result, "{callstack}", ErrorLogger::callStackToString(callStack)); |
| 667 | findAndReplace(result, "{file}", callStack.back().getfile()); |
| 668 | findAndReplace(result, "{line}", std::to_string(callStack.back().line)); |
| 669 | findAndReplace(result, "{column}", std::to_string(callStack.back().column)); |
| 670 | if (result.find("{code}") != std::string::npos) { |
| 671 | const std::string::size_type pos = result.find('\r'); |
| 672 | const char *endl; |
| 673 | if (pos == std::string::npos) |
| 674 | endl = "\n"; |
| 675 | else if (pos+1 < result.size() && result[pos+1] == '\n') |
| 676 | endl = "\r\n"; |
| 677 | else |
| 678 | endl = "\r"; |
| 679 | findAndReplace(result, "{code}", readCode(callStack.back().getOrigFile(), callStack.back().line, callStack.back().column, endl)); |
| 680 | } |
| 681 | } else { |
| 682 | static const std::unordered_map<std::string, std::string> callStackSubstitutionMap = |
| 683 | { |
| 684 | {"{callstack}", ""}, |
| 685 | {"{file}", "nofile"}, |
| 686 | {"{line}", "0"}, |
| 687 | {"{column}", "0"}, |
| 688 | {"{code}", ""} |
| 689 | }; |
| 690 | replace(result, callStackSubstitutionMap); |
| 691 | } |
| 692 | |
| 693 | if (!templateLocation.empty() && callStack.size() >= 2U) { |
| 694 | for (const FileLocation &fileLocation : callStack) { |
| 695 | std::string text = templateLocation; |
nothing calls this directly
no test coverage detected