1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
|
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2025 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "errorlogger.h"
#include "color.h"
#include "cppcheck.h"
#include "path.h"
#include "settings.h"
#include "suppressions.h"
#include "token.h"
#include "tokenlist.h"
#include "utils.h"
#include "checkers.h"
#include <algorithm>
#include <array>
#include <cassert>
#include <cctype>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <string>
#include <unordered_map>
#include <utility>
#include "xml.h"
const std::set<std::string> ErrorLogger::mCriticalErrorIds{
"cppcheckError",
"cppcheckLimit",
"internalAstError",
"instantiationError",
"internalError",
"premium-internalError",
"premium-invalidArgument",
"premium-invalidLicense",
"preprocessorErrorDirective",
"syntaxError",
"unknownMacro"
};
ErrorMessage::ErrorMessage()
: severity(Severity::none), cwe(0U), certainty(Certainty::normal), hash(0)
{}
// TODO: id and msg are swapped compared to other calls
ErrorMessage::ErrorMessage(std::list<FileLocation> callStack, std::string file1, Severity severity, const std::string &msg, std::string id, Certainty certainty) :
callStack(std::move(callStack)), // locations for this error message
id(std::move(id)), // set the message id
file0(std::move(file1)),
severity(severity), // severity for this error message
cwe(0U),
certainty(certainty),
hash(0)
{
// set the summary and verbose messages
setmsg(msg);
}
// TODO: id and msg are swapped compared to other calls
ErrorMessage::ErrorMessage(std::list<FileLocation> callStack, std::string file1, Severity severity, const std::string &msg, std::string id, const CWE &cwe, Certainty certainty) :
callStack(std::move(callStack)), // locations for this error message
id(std::move(id)), // set the message id
file0(std::move(file1)),
severity(severity), // severity for this error message
cwe(cwe.id),
certainty(certainty),
hash(0)
{
// set the summary and verbose messages
setmsg(msg);
}
ErrorMessage::ErrorMessage(const std::list<const Token*>& callstack, const TokenList* list, Severity severity, std::string id, const std::string& msg, Certainty certainty)
: id(std::move(id)), severity(severity), cwe(0U), certainty(certainty), hash(0)
{
// Format callstack
for (auto it = callstack.cbegin(); it != callstack.cend(); ++it) {
// --errorlist can provide null values here
if (!(*it))
continue;
callStack.emplace_back(*it, list);
}
if (list && !list->getFiles().empty())
file0 = list->getFiles()[0];
setmsg(msg);
}
ErrorMessage::ErrorMessage(const std::list<const Token*>& callstack, const TokenList* list, Severity severity, std::string id, const std::string& msg, const CWE &cwe, Certainty certainty)
: id(std::move(id)), severity(severity), cwe(cwe.id), certainty(certainty)
{
// Format callstack
for (const Token *tok: callstack) {
// --errorlist can provide null values here
if (!tok)
continue;
callStack.emplace_back(tok, list);
}
if (list && !list->getFiles().empty())
file0 = list->getFiles()[0];
setmsg(msg);
hash = 0; // calculateWarningHash(list, hashWarning.str());
}
ErrorMessage::ErrorMessage(const ErrorPath &errorPath, const TokenList *tokenList, Severity severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty)
: id(id), severity(severity), cwe(cwe.id), certainty(certainty)
{
// Format callstack
for (const ErrorPathItem& e: errorPath) {
const Token *tok = e.first;
// --errorlist can provide null values here
if (!tok)
continue;
const std::string& path_info = e.second;
std::string info;
if (startsWith(path_info,"$symbol:") && path_info.find('\n') < path_info.size()) {
const std::string::size_type pos = path_info.find('\n');
const std::string symbolName = path_info.substr(8, pos - 8);
info = replaceStr(path_info.substr(pos+1), "$symbol", symbolName);
}
else {
info = path_info;
}
callStack.emplace_back(tok, std::move(info), tokenList);
}
if (tokenList && !tokenList->getFiles().empty())
file0 = tokenList->getFiles()[0];
setmsg(msg);
hash = 0; // calculateWarningHash(tokenList, hashWarning.str());
}
ErrorMessage::ErrorMessage(const tinyxml2::XMLElement * const errmsg)
: severity(Severity::none),
cwe(0U),
certainty(Certainty::normal)
{
const char * const unknown = "<UNKNOWN>";
const char *attr = errmsg->Attribute("id");
id = attr ? attr : unknown;
attr = errmsg->Attribute("file0");
file0 = attr ? attr : "";
attr = errmsg->Attribute("severity");
severity = attr ? severityFromString(attr) : Severity::none;
attr = errmsg->Attribute("cwe");
// cppcheck-suppress templateInstantiation - TODO: fix this - see #11631
cwe.id = attr ? strToInt<unsigned short>(attr) : 0;
attr = errmsg->Attribute("inconclusive");
certainty = (attr && (std::strcmp(attr, "true") == 0)) ? Certainty::inconclusive : Certainty::normal;
attr = errmsg->Attribute("msg");
mShortMessage = attr ? attr : "";
attr = errmsg->Attribute("verbose");
mVerboseMessage = attr ? attr : "";
attr = errmsg->Attribute("hash");
hash = attr ? strToInt<std::size_t>(attr) : 0;
for (const tinyxml2::XMLElement *e = errmsg->FirstChildElement(); e; e = e->NextSiblingElement()) {
const char* name = e->Name();
if (std::strcmp(name,"location")==0) {
const char *strfile = e->Attribute("file");
const char *strinfo = e->Attribute("info");
const char *strline = e->Attribute("line");
const char *strcolumn = e->Attribute("column");
const char *file = strfile ? strfile : unknown;
const char *info = strinfo ? strinfo : "";
const int line = strline ? strToInt<int>(strline) : 0;
const int column = strcolumn ? strToInt<int>(strcolumn) : 0;
callStack.emplace_front(file, info, line, column);
} else if (std::strcmp(name,"symbol")==0) {
mSymbolNames += e->GetText();
}
}
}
void ErrorMessage::setmsg(const std::string &msg)
{
// If a message ends to a '\n' and contains only a one '\n'
// it will cause the mVerboseMessage to be empty which will show
// as an empty message to the user if --verbose is used.
// Even this doesn't cause problems with messages that have multiple
// lines, none of the error messages should end into it.
assert(!endsWith(msg,'\n'));
// The summary and verbose message are separated by a newline
// If there is no newline then both the summary and verbose messages
// are the given message
const std::string::size_type pos = msg.find('\n');
const std::string symbolName = mSymbolNames.empty() ? std::string() : mSymbolNames.substr(0, mSymbolNames.find('\n'));
if (pos == std::string::npos) {
mShortMessage = replaceStr(msg, "$symbol", symbolName);
mVerboseMessage = replaceStr(msg, "$symbol", symbolName);
} else if (startsWith(msg,"$symbol:")) {
mSymbolNames += msg.substr(8, pos-7);
setmsg(msg.substr(pos + 1));
} else {
mShortMessage = replaceStr(msg.substr(0, pos), "$symbol", symbolName);
mVerboseMessage = replaceStr(msg.substr(pos + 1), "$symbol", symbolName);
}
}
static void serializeString(std::string &oss, const std::string & str)
{
oss += std::to_string(str.length());
oss += " ";
oss += str;
}
ErrorMessage ErrorMessage::fromInternalError(const InternalError &internalError, const TokenList *tokenList, const std::string &filename, const std::string& msg)
{
if (internalError.token)
assert(tokenList != nullptr); // we need to make sure we can look up the provided token
std::list<ErrorMessage::FileLocation> locationList;
if (tokenList && internalError.token) {
locationList.emplace_back(internalError.token, tokenList);
} else {
locationList.emplace_back(filename, 0, 0);
if (tokenList && (filename != tokenList->getSourceFilePath())) {
locationList.emplace_back(tokenList->getSourceFilePath(), 0, 0);
}
}
ErrorMessage errmsg(std::move(locationList),
tokenList ? tokenList->getSourceFilePath() : filename,
Severity::error,
(msg.empty() ? "" : (msg + ": ")) + internalError.errorMessage,
internalError.id,
Certainty::normal);
// TODO: find a better way
if (!internalError.details.empty())
errmsg.mVerboseMessage = errmsg.mVerboseMessage + ": " + internalError.details;
return errmsg;
}
std::string ErrorMessage::serialize() const
{
// Serialize this message into a simple string
std::string oss;
serializeString(oss, id);
serializeString(oss, severityToString(severity));
serializeString(oss, std::to_string(cwe.id));
serializeString(oss, std::to_string(hash));
serializeString(oss, fixInvalidChars(remark));
serializeString(oss, file0);
serializeString(oss, (certainty == Certainty::inconclusive) ? "1" : "0");
const std::string saneShortMessage = fixInvalidChars(mShortMessage);
const std::string saneVerboseMessage = fixInvalidChars(mVerboseMessage);
serializeString(oss, saneShortMessage);
serializeString(oss, saneVerboseMessage);
serializeString(oss, mSymbolNames);
oss += std::to_string(callStack.size());
oss += " ";
for (auto loc = callStack.cbegin(); loc != callStack.cend(); ++loc) {
std::string frame;
frame += std::to_string(loc->line);
frame += '\t';
frame += std::to_string(loc->column);
frame += '\t';
frame += loc->getfile(false);
frame += '\t';
frame += loc->getOrigFile(false);
frame += '\t';
frame += loc->getinfo();
serializeString(oss, frame);
}
return oss;
}
void ErrorMessage::deserialize(const std::string &data)
{
// TODO: clear all fields
certainty = Certainty::normal;
callStack.clear();
std::istringstream iss(data);
std::array<std::string, 10> results;
std::size_t elem = 0;
while (iss.good() && elem < 10) {
unsigned int len = 0;
if (!(iss >> len))
throw InternalError(nullptr, "Internal Error: Deserialization of error message failed - invalid length");
if (iss.get() != ' ')
throw InternalError(nullptr, "Internal Error: Deserialization of error message failed - invalid separator");
if (!iss.good())
throw InternalError(nullptr, "Internal Error: Deserialization of error message failed - premature end of data");
std::string temp;
if (len > 0) {
temp.resize(len);
iss.read(&temp[0], len);
if (!iss.good())
throw InternalError(nullptr, "Internal Error: Deserialization of error message failed - premature end of data");
}
results[elem++] = std::move(temp);
}
if (!iss.good())
throw InternalError(nullptr, "Internal Error: Deserialization of error message failed - premature end of data");
if (elem != 10)
throw InternalError(nullptr, "Internal Error: Deserialization of error message failed - insufficient elements");
id = std::move(results[0]);
severity = severityFromString(results[1]);
cwe.id = 0;
if (!results[2].empty()) {
std::string err;
if (!strToInt(results[2], cwe.id, &err))
throw InternalError(nullptr, "Internal Error: Deserialization of error message failed - invalid CWE ID - " + err);
}
hash = 0;
if (!results[3].empty()) {
std::string err;
if (!strToInt(results[3], hash, &err))
throw InternalError(nullptr, "Internal Error: Deserialization of error message failed - invalid hash - " + err);
}
remark = std::move(results[4]);
file0 = std::move(results[5]);
if (results[6] == "1")
certainty = Certainty::inconclusive;
mShortMessage = std::move(results[7]);
mVerboseMessage = std::move(results[8]);
mSymbolNames = std::move(results[9]);
unsigned int stackSize = 0;
if (!(iss >> stackSize))
throw InternalError(nullptr, "Internal Error: Deserialization of error message failed - invalid stack size");
if (iss.get() != ' ')
throw InternalError(nullptr, "Internal Error: Deserialization of error message failed - invalid separator");
if (stackSize == 0)
return;
while (iss.good()) {
unsigned int len = 0;
if (!(iss >> len))
throw InternalError(nullptr, "Internal Error: Deserialization of error message failed - invalid length (stack)");
if (iss.get() != ' ')
throw InternalError(nullptr, "Internal Error: Deserialization of error message failed - invalid separator (stack)");
std::string temp;
if (len > 0) {
temp.resize(len);
iss.read(&temp[0], len);
if (!iss.good())
throw InternalError(nullptr, "Internal Error: Deserialization of error message failed - premature end of data (stack)");
}
std::vector<std::string> substrings;
substrings.reserve(5);
for (std::string::size_type pos = 0; pos < temp.size() && substrings.size() < 5; ++pos) {
if (substrings.size() == 4) {
substrings.push_back(temp.substr(pos));
break;
}
const std::string::size_type start = pos;
pos = temp.find('\t', pos);
if (pos == std::string::npos) {
substrings.push_back(temp.substr(start));
break;
}
substrings.push_back(temp.substr(start, pos - start));
}
if (substrings.size() < 4)
throw InternalError(nullptr, "Internal Error: Deserializing of error message failed");
// (*loc).line << '\t' << (*loc).column << '\t' << (*loc).getfile(false) << '\t' << loc->getOrigFile(false) << '\t' << loc->getinfo();
std::string info;
if (substrings.size() == 5)
info = std::move(substrings[4]);
ErrorMessage::FileLocation loc(substrings[3], std::move(info), strToInt<int>(substrings[0]), strToInt<unsigned int>(substrings[1]));
loc.setfile(std::move(substrings[2]));
callStack.push_back(std::move(loc));
if (callStack.size() >= stackSize)
break;
}
}
std::string ErrorMessage::getXMLHeader(std::string productName, int xmlVersion)
{
const auto nameAndVersion = Settings::getNameAndVersion(productName);
productName = nameAndVersion.first;
const std::string version = nameAndVersion.first.empty() ? CppCheck::version() : nameAndVersion.second;
tinyxml2::XMLPrinter printer;
// standard xml header
printer.PushDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
// header
printer.OpenElement("results", false);
printer.PushAttribute("version", xmlVersion);
printer.OpenElement("cppcheck", false);
if (!productName.empty())
printer.PushAttribute("product-name", productName.c_str());
printer.PushAttribute("version", version.c_str());
printer.CloseElement(false);
printer.OpenElement("errors", false);
return std::string(printer.CStr()) + '>';
}
std::string ErrorMessage::getXMLFooter(int xmlVersion)
{
return xmlVersion == 3 ? "</results>" : " </errors>\n</results>";
}
// There is no utf-8 support around but the strings should at least be safe for to tinyxml2.
// See #5300 "Invalid encoding in XML output" and #6431 "Invalid XML created - Invalid encoding of string literal "
std::string ErrorMessage::fixInvalidChars(const std::string& raw)
{
std::string result;
result.reserve(raw.length());
auto from=raw.cbegin();
while (from!=raw.cend()) {
if (std::isprint(static_cast<unsigned char>(*from))) {
result.push_back(*from);
} else {
std::ostringstream es;
// straight cast to (unsigned) doesn't work out.
const unsigned uFrom = (unsigned char)*from;
es << '\\' << std::setbase(8) << std::setw(3) << std::setfill('0') << uFrom;
result += es.str();
}
++from;
}
return result;
}
std::string ErrorMessage::toXML() const
{
tinyxml2::XMLPrinter printer(nullptr, false, 2);
printer.OpenElement("error", false);
printer.PushAttribute("id", id.c_str());
if (!guideline.empty())
printer.PushAttribute("guideline", guideline.c_str());
printer.PushAttribute("severity", severityToString(severity).c_str());
if (!classification.empty())
printer.PushAttribute("classification", classification.c_str());
printer.PushAttribute("msg", fixInvalidChars(mShortMessage).c_str());
printer.PushAttribute("verbose", fixInvalidChars(mVerboseMessage).c_str());
if (cwe.id)
printer.PushAttribute("cwe", cwe.id);
if (hash)
printer.PushAttribute("hash", std::to_string(hash).c_str());
if (certainty == Certainty::inconclusive)
printer.PushAttribute("inconclusive", "true");
if (!file0.empty())
printer.PushAttribute("file0", file0.c_str());
if (!remark.empty())
printer.PushAttribute("remark", fixInvalidChars(remark).c_str());
for (auto it = callStack.crbegin(); it != callStack.crend(); ++it) {
printer.OpenElement("location", false);
printer.PushAttribute("file", it->getfile().c_str());
printer.PushAttribute("line", std::max(it->line,0));
printer.PushAttribute("column", it->column);
if (!it->getinfo().empty())
printer.PushAttribute("info", fixInvalidChars(it->getinfo()).c_str());
printer.CloseElement(false);
}
for (std::string::size_type pos = 0; pos < mSymbolNames.size();) {
const std::string::size_type pos2 = mSymbolNames.find('\n', pos);
std::string symbolName;
if (pos2 == std::string::npos) {
symbolName = mSymbolNames.substr(pos);
pos = pos2;
} else {
symbolName = mSymbolNames.substr(pos, pos2-pos);
pos = pos2 + 1;
}
printer.OpenElement("symbol", false);
printer.PushText(symbolName.c_str());
printer.CloseElement(false);
}
printer.CloseElement(false);
return printer.CStr();
}
// TODO: read info from some shared resource instead?
static std::string readCode(const std::string &file, int linenr, int column, const char endl[])
{
std::ifstream fin(file);
std::string line;
while (linenr > 0 && std::getline(fin,line)) {
linenr--;
}
const std::string::size_type endPos = line.find_last_not_of("\r\n\t ");
if (endPos + 1 < line.size())
line.erase(endPos + 1);
std::string::size_type pos = 0;
while ((pos = line.find('\t', pos)) != std::string::npos)
line[pos] = ' ';
return line + endl + std::string((column>0 ? column-1 : 0), ' ') + '^';
}
static void replaceSpecialChars(std::string& source)
{
// Support a few special characters to allow to specific formatting, see http://sourceforge.net/apps/phpbb/cppcheck/viewtopic.php?f=4&t=494&sid=21715d362c0dbafd3791da4d9522f814
// Substitution should be done first so messages from cppcheck never get translated.
static const std::unordered_map<char, std::string> substitutionMap = {
{'b', "\b"},
{'n', "\n"},
{'r', "\r"},
{'t', "\t"}
};
std::string::size_type index = 0;
while ((index = source.find('\\', index)) != std::string::npos) {
const char searchFor = source[index+1];
const auto it = substitutionMap.find(searchFor);
if (it == substitutionMap.end()) {
index += 1;
continue;
}
const std::string& replaceWith = it->second;
source.replace(index, 2, replaceWith);
index += replaceWith.length();
}
}
static void replace(std::string& source, const std::unordered_map<std::string, std::string> &substitutionMap)
{
std::string::size_type index = 0;
while ((index = source.find('{', index)) != std::string::npos) {
const std::string::size_type end = source.find('}', index);
if (end == std::string::npos)
break;
const std::string searchFor = source.substr(index, end-index+1);
const auto it = substitutionMap.find(searchFor);
if (it == substitutionMap.end()) {
index += 1;
continue;
}
const std::string& replaceWith = it->second;
source.replace(index, searchFor.length(), replaceWith);
index += replaceWith.length();
}
}
static void replaceColors(std::string& source) {
// TODO: colors are not applied when either stdout or stderr is not a TTY because we resolve them before the stream usage
static const std::unordered_map<std::string, std::string> substitutionMap =
{
{"{reset}", ::toString(Color::Reset)},
{"{bold}", ::toString(Color::Bold)},
{"{dim}", ::toString(Color::Dim)},
{"{red}", ::toString(Color::FgRed)},
{"{green}", ::toString(Color::FgGreen)},
{"{blue}", ::toString(Color::FgBlue)},
{"{magenta}", ::toString(Color::FgMagenta)},
{"{default}", ::toString(Color::FgDefault)},
};
replace(source, substitutionMap);
}
// TODO: remove default parameters
std::string ErrorMessage::toString(bool verbose, const std::string &templateFormat, const std::string &templateLocation) const
{
// Save this ErrorMessage in plain text.
// TODO: should never happen - remove this
// No template is given
// (not 100%) equivalent templateFormat: {callstack} ({severity}{inconclusive:, inconclusive}) {message}
if (templateFormat.empty()) {
std::string text;
if (!callStack.empty()) {
text += ErrorLogger::callStackToString(callStack);
text += ": ";
}
if (severity != Severity::none) {
text += '(';
text += severityToString(severity);
if (certainty == Certainty::inconclusive)
text += ", inconclusive";
text += ") ";
}
text += (verbose ? mVerboseMessage : mShortMessage);
return text;
}
// template is given. Reformat the output according to it
std::string result = templateFormat;
// replace id with guideline if present
// replace severity with classification if present
const std::string idStr = guideline.empty() ? id : guideline;
const std::string severityStr = classification.empty() ? severityToString(severity) : classification;
findAndReplace(result, "{id}", idStr);
std::string::size_type pos1 = result.find("{inconclusive:");
while (pos1 != std::string::npos) {
const std::string::size_type pos2 = result.find('}', pos1+1);
const std::string replaceFrom = result.substr(pos1,pos2-pos1+1);
const std::string replaceWith = (certainty == Certainty::inconclusive) ? result.substr(pos1+14, pos2-pos1-14) : std::string();
findAndReplace(result, replaceFrom, replaceWith);
pos1 = result.find("{inconclusive:", pos1);
}
findAndReplace(result, "{severity}", severityStr);
findAndReplace(result, "{cwe}", std::to_string(cwe.id));
findAndReplace(result, "{message}", verbose ? mVerboseMessage : mShortMessage);
findAndReplace(result, "{remark}", remark);
if (!callStack.empty()) {
if (result.find("{callstack}") != std::string::npos)
findAndReplace(result, "{callstack}", ErrorLogger::callStackToString(callStack));
findAndReplace(result, "{file}", callStack.back().getfile());
findAndReplace(result, "{line}", std::to_string(callStack.back().line));
findAndReplace(result, "{column}", std::to_string(callStack.back().column));
if (result.find("{code}") != std::string::npos) {
const std::string::size_type pos = result.find('\r');
const char *endl;
if (pos == std::string::npos)
endl = "\n";
else if (pos+1 < result.size() && result[pos+1] == '\n')
endl = "\r\n";
else
endl = "\r";
findAndReplace(result, "{code}", readCode(callStack.back().getOrigFile(), callStack.back().line, callStack.back().column, endl));
}
} else {
static const std::unordered_map<std::string, std::string> callStackSubstitutionMap =
{
{"{callstack}", ""},
{"{file}", "nofile"},
{"{line}", "0"},
{"{column}", "0"},
{"{code}", ""}
};
replace(result, callStackSubstitutionMap);
}
if (!templateLocation.empty() && callStack.size() >= 2U) {
for (const FileLocation &fileLocation : callStack) {
std::string text = templateLocation;
findAndReplace(text, "{file}", fileLocation.getfile());
findAndReplace(text, "{line}", std::to_string(fileLocation.line));
findAndReplace(text, "{column}", std::to_string(fileLocation.column));
findAndReplace(text, "{info}", fileLocation.getinfo().empty() ? mShortMessage : fileLocation.getinfo());
if (text.find("{code}") != std::string::npos) {
const std::string::size_type pos = text.find('\r');
const char *endl;
if (pos == std::string::npos)
endl = "\n";
else if (pos+1 < text.size() && text[pos+1] == '\n')
endl = "\r\n";
else
endl = "\r";
findAndReplace(text, "{code}", readCode(fileLocation.getOrigFile(), fileLocation.line, fileLocation.column, endl));
}
result += '\n' + text;
}
}
return result;
}
std::string ErrorLogger::callStackToString(const std::list<ErrorMessage::FileLocation> &callStack)
{
std::string str;
for (auto tok = callStack.cbegin(); tok != callStack.cend(); ++tok) {
str += (tok == callStack.cbegin() ? "" : " -> ");
str += tok->stringify();
}
return str;
}
ErrorMessage::FileLocation::FileLocation(const Token* tok, const TokenList* tokenList)
: fileIndex(tok->fileIndex()), line(tok->linenr()), column(tok->column()), mOrigFileName(tokenList->getOrigFile(tok)), mFileName(tokenList->file(tok))
{}
ErrorMessage::FileLocation::FileLocation(const Token* tok, std::string info, const TokenList* tokenList)
: fileIndex(tok->fileIndex()), line(tok->linenr()), column(tok->column()), mOrigFileName(tokenList->getOrigFile(tok)), mFileName(tokenList->file(tok)), mInfo(std::move(info))
{}
std::string ErrorMessage::FileLocation::getfile(bool convert) const
{
if (convert)
return Path::toNativeSeparators(mFileName);
return mFileName;
}
std::string ErrorMessage::FileLocation::getOrigFile(bool convert) const
{
if (convert)
return Path::toNativeSeparators(mOrigFileName);
return mOrigFileName;
}
void ErrorMessage::FileLocation::setfile(std::string file)
{
mFileName = Path::simplifyPath(std::move(file));
}
std::string ErrorMessage::FileLocation::stringify() const
{
std::string str;
str += '[';
str += Path::toNativeSeparators(mFileName);
if (line != SuppressionList::Suppression::NO_LINE) {
str += ':';
str += std::to_string(line);
}
str += ']';
return str;
}
std::string ErrorLogger::toxml(const std::string &str)
{
std::string xml;
for (const unsigned char c : str) {
switch (c) {
case '<':
xml += "<";
break;
case '>':
xml += ">";
break;
case '&':
xml += "&";
break;
case '\"':
xml += """;
break;
case '\'':
xml += "'";
break;
case '\0':
xml += "\\0";
break;
default:
if (c >= ' ' && c <= 0x7f)
xml += c;
else
xml += 'x';
break;
}
}
return xml;
}
std::string ErrorLogger::plistHeader(const std::string &version, const std::vector<std::string> &files)
{
std::ostringstream ostr;
ostr << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
<< "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\r\n"
<< "<plist version=\"1.0\">\r\n"
<< "<dict>\r\n"
<< " <key>clang_version</key>\r\n"
<< "<string>cppcheck version " << version << "</string>\r\n"
<< " <key>files</key>\r\n"
<< " <array>\r\n";
for (const std::string & file : files)
ostr << " <string>" << ErrorLogger::toxml(file) << "</string>\r\n";
ostr << " </array>\r\n"
<< " <key>diagnostics</key>\r\n"
<< " <array>\r\n";
return ostr.str();
}
static std::string plistLoc(const char indent[], const ErrorMessage::FileLocation &loc)
{
std::ostringstream ostr;
ostr << indent << "<dict>\r\n"
<< indent << ' ' << "<key>line</key><integer>" << loc.line << "</integer>\r\n"
<< indent << ' ' << "<key>col</key><integer>" << loc.column << "</integer>\r\n"
<< indent << ' ' << "<key>file</key><integer>" << loc.fileIndex << "</integer>\r\n"
<< indent << "</dict>\r\n";
return ostr.str();
}
std::string ErrorLogger::plistData(const ErrorMessage &msg)
{
std::ostringstream plist;
plist << " <dict>\r\n"
<< " <key>path</key>\r\n"
<< " <array>\r\n";
auto prev = msg.callStack.cbegin();
for (auto it = msg.callStack.cbegin(); it != msg.callStack.cend(); ++it) {
if (prev != it) {
plist << " <dict>\r\n"
<< " <key>kind</key><string>control</string>\r\n"
<< " <key>edges</key>\r\n"
<< " <array>\r\n"
<< " <dict>\r\n"
<< " <key>start</key>\r\n"
<< " <array>\r\n"
<< plistLoc(" ", *prev)
<< plistLoc(" ", *prev)
<< " </array>\r\n"
<< " <key>end</key>\r\n"
<< " <array>\r\n"
<< plistLoc(" ", *it)
<< plistLoc(" ", *it)
<< " </array>\r\n"
<< " </dict>\r\n"
<< " </array>\r\n"
<< " </dict>\r\n";
prev = it;
}
auto next = it;
++next;
const std::string message = (it->getinfo().empty() && next == msg.callStack.cend() ? msg.shortMessage() : it->getinfo());
plist << " <dict>\r\n"
<< " <key>kind</key><string>event</string>\r\n"
<< " <key>location</key>\r\n"
<< plistLoc(" ", *it)
<< " <key>ranges</key>\r\n"
<< " <array>\r\n"
<< " <array>\r\n"
<< plistLoc(" ", *it)
<< plistLoc(" ", *it)
<< " </array>\r\n"
<< " </array>\r\n"
<< " <key>depth</key><integer>0</integer>\r\n"
<< " <key>extended_message</key>\r\n"
<< " <string>" << ErrorLogger::toxml(message) << "</string>\r\n"
<< " <key>message</key>\r\n"
<< " <string>" << ErrorLogger::toxml(message) << "</string>\r\n"
<< " </dict>\r\n";
}
plist << " </array>\r\n"
<< " <key>description</key><string>" << ErrorLogger::toxml(msg.shortMessage()) << "</string>\r\n"
<< " <key>category</key><string>" << severityToString(msg.severity) << "</string>\r\n"
<< " <key>type</key><string>" << ErrorLogger::toxml(msg.shortMessage()) << "</string>\r\n"
<< " <key>check_name</key><string>" << msg.id << "</string>\r\n"
<< " <!-- This hash is experimental and going to change! -->\r\n"
<< " <key>issue_hash_content_of_line_in_context</key><string>" << 0 << "</string>\r\n"
<< " <key>issue_context_kind</key><string></string>\r\n"
<< " <key>issue_context</key><string></string>\r\n"
<< " <key>issue_hash_function_offset</key><string></string>\r\n"
<< " <key>location</key>\r\n"
<< plistLoc(" ", msg.callStack.back())
<< " </dict>\r\n";
return plist.str();
}
std::string replaceStr(std::string s, const std::string &from, const std::string &to)
{
std::string::size_type pos1 = 0;
while (pos1 < s.size()) {
pos1 = s.find(from, pos1);
if (pos1 == std::string::npos)
return s;
if (pos1 > 0 && (s[pos1-1] == '_' || std::isalnum(s[pos1-1]))) {
pos1++;
continue;
}
const std::string::size_type pos2 = pos1 + from.size();
if (pos2 >= s.size())
return s.substr(0,pos1) + to;
if (s[pos2] == '_' || std::isalnum(s[pos2])) {
pos1++;
continue;
}
s.replace(pos1, from.size(), to);
pos1 += to.size();
}
return s;
}
void substituteTemplateFormatStatic(std::string& templateFormat)
{
replaceSpecialChars(templateFormat);
replaceColors(templateFormat);
}
void substituteTemplateLocationStatic(std::string& templateLocation)
{
replaceSpecialChars(templateLocation);
replaceColors(templateLocation);
}
std::string getClassification(const std::string &guideline, ReportType reportType) {
if (guideline.empty())
return "";
const auto getClassification = [](const std::vector<checkers::Info> &info, const std::string &guideline) -> std::string {
const auto it = std::find_if(info.cbegin(), info.cend(), [&](const checkers::Info &i) {
return caseInsensitiveStringCompare(i.guideline, guideline) == 0;
});
if (it == info.cend())
return "";
return it->classification;
};
switch (reportType) {
case ReportType::autosar:
return getClassification(checkers::autosarInfo, guideline);
case ReportType::certC:
return getClassification(checkers::certCInfo, guideline);
case ReportType::certCpp:
return getClassification(checkers::certCppInfo, guideline);
case ReportType::misraC:
{
auto components = splitString(guideline, '.');
if (components.size() != 2)
return "";
const int a = std::stoi(components[0]);
const int b = std::stoi(components[1]);
const std::vector<checkers::MisraInfo> &info = checkers::misraC2012Rules;
const auto it = std::find_if(info.cbegin(), info.cend(), [&](const checkers::MisraInfo &i) {
return i.a == a && i.b == b;
});
if (it == info.cend())
return "";
return it->str;
}
case ReportType::misraCpp2008:
case ReportType::misraCpp2023:
{
char delim;
const std::vector<checkers::MisraCppInfo> *info;
if (reportType == ReportType::misraCpp2008) {
delim = '-';
info = &checkers::misraCpp2008Rules;
} else {
delim = '.';
info = &checkers::misraCpp2023Rules;
}
auto components = splitString(guideline, delim);
if (components.size() != 3)
return "";
const int a = std::stoi(components[0]);
const int b = std::stoi(components[1]);
const int c = std::stoi(components[2]);
const auto it = std::find_if(info->cbegin(), info->cend(), [&](const checkers::MisraCppInfo &i) {
return i.a == a && i.b == b && i.c == c;
});
if (it == info->cend())
return "";
return it->classification;
}
default:
return "";
}
}
std::string getGuideline(const std::string &errId, ReportType reportType,
const std::map<std::string, std::string> &guidelineMapping,
Severity severity)
{
std::string guideline;
switch (reportType) {
case ReportType::autosar:
if (errId.rfind("premium-autosar-", 0) == 0) {
guideline = errId.substr(16);
break;
}
if (errId.rfind("premium-misra-cpp-2008-", 0) == 0)
guideline = "M" + errId.substr(23);
break;
case ReportType::certC:
case ReportType::certCpp:
if (errId.rfind("premium-cert-", 0) == 0) {
guideline = errId.substr(13);
std::transform(guideline.begin(), guideline.end(),
guideline.begin(), static_cast<int (*)(int)>(std::toupper));
}
break;
case ReportType::misraC:
if (errId.rfind("misra-c20", 0) == 0)
guideline = errId.substr(errId.rfind('-') + 1);
break;
case ReportType::misraCpp2008:
if (errId.rfind("misra-cpp-2008-", 0) == 0)
guideline = errId.substr(15);
break;
case ReportType::misraCpp2023:
if (errId.rfind("misra-cpp-2023-", 0) == 0)
guideline = errId.substr(15);
break;
default:
break;
}
if (!guideline.empty())
return guideline;
auto it = guidelineMapping.find(errId);
if (it != guidelineMapping.cend())
return it->second;
if (severity == Severity::error || severity == Severity::warning) {
it = guidelineMapping.find("error");
if (it != guidelineMapping.cend())
return it->second;
}
return "";
}
std::map<std::string, std::string> createGuidelineMapping(ReportType reportType) {
std::map<std::string, std::string> guidelineMapping;
const std::vector<checkers::IdMapping> *idMapping1 = nullptr;
const std::vector<checkers::IdMapping> *idMapping2 = nullptr;
std::string ext1, ext2;
switch (reportType) {
case ReportType::autosar:
idMapping1 = &checkers::idMappingAutosar;
break;
case ReportType::certCpp:
idMapping2 = &checkers::idMappingCertCpp;
ext2 = "-CPP";
FALLTHROUGH;
case ReportType::certC:
idMapping1 = &checkers::idMappingCertC;
ext1 = "-C";
break;
case ReportType::misraC:
idMapping1 = &checkers::idMappingMisraC;
break;
case ReportType::misraCpp2008:
idMapping1 = &checkers::idMappingMisraCpp2008;
break;
case ReportType::misraCpp2023:
idMapping1 = &checkers::idMappingMisraCpp2023;
break;
default:
break;
}
if (idMapping1) {
for (const auto &i : *idMapping1)
for (const std::string &cppcheckId : splitString(i.cppcheckId, ','))
guidelineMapping[cppcheckId] = i.guideline + ext1;
}
if (idMapping2) {
for (const auto &i : *idMapping2)
for (const std::string &cppcheckId : splitString(i.cppcheckId, ','))
guidelineMapping[cppcheckId] = i.guideline + ext2;
}
return guidelineMapping;
}
|