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

Method toString

lib/errorlogger.cpp:638–717  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

636}
637
638std::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;

Callers

nothing calls this directly

Calls 9

severityToStringFunction · 0.85
findAndReplaceFunction · 0.85
readCodeFunction · 0.85
replaceFunction · 0.85
getfileMethod · 0.80
emptyMethod · 0.45
findMethod · 0.45
sizeMethod · 0.45
getOrigFileMethod · 0.45

Tested by

no test coverage detected