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
|
/*
* 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 "preprocessor.h"
#include "errorlogger.h"
#include "errortypes.h"
#include "library.h"
#include "path.h"
#include "platform.h"
#include "settings.h"
#include "standards.h"
#include "suppressions.h"
#include "utils.h"
#include <algorithm>
#include <array>
#include <cstddef>
#include <iterator>
#include <sstream>
#include <utility>
#include <simplecpp.h>
static bool sameline(const simplecpp::Token *tok1, const simplecpp::Token *tok2)
{
return tok1 && tok2 && tok1->location.sameline(tok2->location);
}
Directive::Directive(const simplecpp::Location & _loc, std::string _str) :
file(_loc.file()),
linenr(_loc.line),
str(std::move(_str))
{}
Directive::Directive(std::string _file, const int _linenr, std::string _str) :
file(std::move(_file)),
linenr(_linenr),
str(std::move(_str))
{}
Directive::DirectiveToken::DirectiveToken(const simplecpp::Token & _tok) :
line(_tok.location.line),
column(_tok.location.col),
tokStr(_tok.str())
{}
char Preprocessor::macroChar = char(1);
Preprocessor::Preprocessor(const Settings& settings, ErrorLogger &errorLogger) : mSettings(settings), mErrorLogger(errorLogger)
{}
Preprocessor::~Preprocessor()
{
for (const std::pair<const std::string, simplecpp::TokenList*>& tokenList : mTokenLists)
delete tokenList.second;
}
namespace {
struct BadInlineSuppression {
BadInlineSuppression(std::string file, const int line, std::string msg) : file(std::move(file)), line(line), errmsg(std::move(msg)) {}
std::string file;
int line;
std::string errmsg;
};
}
static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std::list<SuppressionList::Suppression> &inlineSuppressions, std::list<BadInlineSuppression> &bad)
{
const std::string cppchecksuppress("cppcheck-suppress");
const std::string &comment = tok->str();
if (comment.size() < cppchecksuppress.size())
return false;
const std::string::size_type pos1 = comment.find_first_not_of("/* \t");
if (pos1 == std::string::npos)
return false;
if (pos1 + cppchecksuppress.size() >= comment.size())
return false;
if (comment.substr(pos1, cppchecksuppress.size()) != cppchecksuppress)
return false;
// check if it has a prefix
const std::string::size_type posEndComment = comment.find_first_of(" [", pos1+cppchecksuppress.size());
// skip spaces after "cppcheck-suppress" and its possible prefix
const std::string::size_type pos2 = comment.find_first_not_of(' ', posEndComment);
if (pos2 == std::string::npos)
return false;
SuppressionList::Type errorType = SuppressionList::Type::unique;
// determine prefix if specified
if (posEndComment >= (pos1 + cppchecksuppress.size() + 1)) {
if (comment.at(pos1 + cppchecksuppress.size()) != '-')
return false;
const unsigned int argumentLength =
posEndComment - (pos1 + cppchecksuppress.size() + 1);
const std::string suppressTypeString =
comment.substr(pos1 + cppchecksuppress.size() + 1, argumentLength);
if ("file" == suppressTypeString)
errorType = SuppressionList::Type::file;
else if ("begin" == suppressTypeString)
errorType = SuppressionList::Type::blockBegin;
else if ("end" == suppressTypeString)
errorType = SuppressionList::Type::blockEnd;
else if ("macro" == suppressTypeString)
errorType = SuppressionList::Type::macro;
else
return false;
}
if (comment[pos2] == '[') {
// multi suppress format
std::string errmsg;
std::vector<SuppressionList::Suppression> suppressions = SuppressionList::parseMultiSuppressComment(comment, &errmsg);
for (SuppressionList::Suppression &s : suppressions) {
s.type = errorType;
s.lineNumber = tok->location.line;
}
if (!errmsg.empty())
bad.emplace_back(tok->location.file(), tok->location.line, std::move(errmsg));
std::copy_if(suppressions.cbegin(), suppressions.cend(), std::back_inserter(inlineSuppressions), [](const SuppressionList::Suppression& s) {
return !s.errorId.empty();
});
} else {
//single suppress format
std::string errmsg;
SuppressionList::Suppression s;
if (!s.parseComment(comment, &errmsg))
return false;
s.type = errorType;
s.lineNumber = tok->location.line;
if (!s.errorId.empty())
inlineSuppressions.push_back(std::move(s));
if (!errmsg.empty())
bad.emplace_back(tok->location.file(), tok->location.line, std::move(errmsg));
}
return true;
}
static std::string getRelativeFilename(const simplecpp::Token* tok, const Settings &settings) {
std::string relativeFilename(tok->location.file());
if (settings.relativePaths) {
for (const std::string & basePath : settings.basePaths) {
const std::string bp = basePath + "/";
if (relativeFilename.compare(0,bp.size(),bp)==0) {
relativeFilename = relativeFilename.substr(bp.size());
}
}
}
return Path::simplifyPath(std::move(relativeFilename));
}
static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Settings &settings, SuppressionList &suppressions, std::list<BadInlineSuppression> &bad)
{
std::list<SuppressionList::Suppression> inlineSuppressionsBlockBegin;
bool onlyComments = true;
for (const simplecpp::Token *tok = tokens.cfront(); tok; tok = tok->next) {
if (!tok->comment) {
onlyComments = false;
continue;
}
std::list<SuppressionList::Suppression> inlineSuppressions;
if (!parseInlineSuppressionCommentToken(tok, inlineSuppressions, bad))
continue;
if (!sameline(tok->previous, tok)) {
// find code after comment..
if (tok->next) {
tok = tok->next;
while (tok->comment) {
parseInlineSuppressionCommentToken(tok, inlineSuppressions, bad);
if (tok->next) {
tok = tok->next;
} else {
break;
}
}
}
}
if (inlineSuppressions.empty())
continue;
// It should never happen
if (!tok)
continue;
// Relative filename
const std::string relativeFilename = getRelativeFilename(tok, settings);
// Macro name
std::string macroName;
if (tok->str() == "#" && tok->next && tok->next->str() == "define") {
const simplecpp::Token *macroNameTok = tok->next->next;
if (sameline(tok, macroNameTok) && macroNameTok->name) {
macroName = macroNameTok->str();
}
}
// Add the suppressions.
for (SuppressionList::Suppression &suppr : inlineSuppressions) {
suppr.fileName = relativeFilename;
if (SuppressionList::Type::blockBegin == suppr.type)
{
inlineSuppressionsBlockBegin.push_back(std::move(suppr));
} else if (SuppressionList::Type::blockEnd == suppr.type) {
bool throwError = true;
if (!inlineSuppressionsBlockBegin.empty()) {
const SuppressionList::Suppression lastBeginSuppression = inlineSuppressionsBlockBegin.back();
auto supprBegin = inlineSuppressionsBlockBegin.begin();
while (supprBegin != inlineSuppressionsBlockBegin.end())
{
if (lastBeginSuppression.lineNumber != supprBegin->lineNumber) {
++supprBegin;
continue;
}
if (suppr.symbolName == supprBegin->symbolName && suppr.lineNumber > supprBegin->lineNumber) {
suppr.lineBegin = supprBegin->lineNumber;
suppr.lineEnd = suppr.lineNumber;
suppr.lineNumber = supprBegin->lineNumber;
suppr.type = SuppressionList::Type::block;
inlineSuppressionsBlockBegin.erase(supprBegin);
suppressions.addSuppression(std::move(suppr)); // TODO: check result
throwError = false;
break;
}
++supprBegin;
}
}
if (throwError) {
// NOLINTNEXTLINE(bugprone-use-after-move) - moved only when thrownError is false
bad.emplace_back(suppr.fileName, suppr.lineNumber, "Suppress End: No matching begin");
}
} else if (SuppressionList::Type::unique == suppr.type || suppr.type == SuppressionList::Type::macro) {
// special handling when suppressing { warnings for backwards compatibility
const bool thisAndNextLine = tok->previous &&
tok->previous->previous &&
tok->next &&
!sameline(tok->previous->previous, tok->previous) &&
tok->location.line + 1 == tok->next->location.line &&
tok->location.fileIndex == tok->next->location.fileIndex &&
tok->previous->str() == "{";
suppr.thisAndNextLine = thisAndNextLine;
suppr.lineNumber = tok->location.line;
suppr.macroName = macroName;
suppressions.addSuppression(std::move(suppr)); // TODO: check result
} else if (SuppressionList::Type::file == suppr.type) {
if (onlyComments)
suppressions.addSuppression(std::move(suppr)); // TODO: check result
else
bad.emplace_back(suppr.fileName, suppr.lineNumber, "File suppression should be at the top of the file");
}
}
}
for (const SuppressionList::Suppression & suppr: inlineSuppressionsBlockBegin)
// cppcheck-suppress useStlAlgorithm
bad.emplace_back(suppr.fileName, suppr.lineNumber, "Suppress Begin: No matching end");
}
void Preprocessor::inlineSuppressions(const simplecpp::TokenList &tokens, SuppressionList &suppressions)
{
if (!mSettings.inlineSuppressions)
return;
std::list<BadInlineSuppression> err;
::addInlineSuppressions(tokens, mSettings, suppressions, err);
for (auto it = mTokenLists.cbegin(); it != mTokenLists.cend(); ++it) {
if (it->second)
::addInlineSuppressions(*it->second, mSettings, suppressions, err);
}
for (const BadInlineSuppression &bad : err) {
error(bad.file, bad.line, bad.errmsg);
}
}
std::vector<RemarkComment> Preprocessor::getRemarkComments(const simplecpp::TokenList &tokens) const
{
std::vector<RemarkComment> ret;
addRemarkComments(tokens, ret);
for (auto it = mTokenLists.cbegin(); it != mTokenLists.cend(); ++it) {
if (it->second)
addRemarkComments(*it->second, ret);
}
return ret;
}
std::list<Directive> Preprocessor::createDirectives(const simplecpp::TokenList &tokens) const
{
// directive list..
std::list<Directive> directives;
std::vector<const simplecpp::TokenList *> list;
list.reserve(1U + mTokenLists.size());
list.push_back(&tokens);
for (auto it = mTokenLists.cbegin(); it != mTokenLists.cend(); ++it) {
list.push_back(it->second);
}
for (const simplecpp::TokenList *tokenList : list) {
for (const simplecpp::Token *tok = tokenList->cfront(); tok; tok = tok->next) {
if ((tok->op != '#') || (tok->previous && tok->previous->location.line == tok->location.line))
continue;
if (tok->next && tok->next->str() == "endfile")
continue;
Directive directive(tok->location, "");
for (const simplecpp::Token *tok2 = tok; tok2 && tok2->location.line == directive.linenr; tok2 = tok2->next) {
if (tok2->comment)
continue;
if (!directive.str.empty() && (tok2->location.col > tok2->previous->location.col + tok2->previous->str().size()))
directive.str += ' ';
if (directive.str == "#" && tok2->str() == "file")
directive.str += "include";
else
directive.str += tok2->str();
directive.strTokens.emplace_back(*tok2);
}
directives.push_back(std::move(directive));
}
}
return directives;
}
static std::string readcondition(const simplecpp::Token *iftok, const std::set<std::string> &defined, const std::set<std::string> &undefined)
{
const simplecpp::Token *cond = iftok->next;
if (!sameline(iftok,cond))
return "";
const simplecpp::Token *next1 = cond->next;
const simplecpp::Token *next2 = next1 ? next1->next : nullptr;
const simplecpp::Token *next3 = next2 ? next2->next : nullptr;
unsigned int len = 1;
if (sameline(iftok,next1))
len = 2;
if (sameline(iftok,next2))
len = 3;
if (sameline(iftok,next3))
len = 4;
if (len == 1 && cond->str() == "0")
return "0";
if (len == 1 && cond->name) {
if (defined.find(cond->str()) == defined.end())
return cond->str();
}
if (len == 2 && cond->op == '!' && next1->name) {
if (defined.find(next1->str()) == defined.end())
return next1->str() + "=0";
}
if (len == 3 && cond->op == '(' && next1->name && next2->op == ')') {
if (defined.find(next1->str()) == defined.end() && undefined.find(next1->str()) == undefined.end())
return next1->str();
}
if (len == 3 && cond->name && next1->str() == "==" && next2->number) {
if (defined.find(cond->str()) == defined.end())
return cond->str() + '=' + cond->next->next->str();
}
std::set<std::string> configset;
for (; sameline(iftok,cond); cond = cond->next) {
if (cond->op == '!') {
if (!sameline(iftok,cond->next) || !cond->next->name)
break;
if (cond->next->str() == "defined")
continue;
configset.insert(cond->next->str() + "=0");
continue;
}
if (cond->str() != "defined")
continue;
const simplecpp::Token *dtok = cond->next;
if (!dtok)
break;
if (dtok->op == '(')
dtok = dtok->next;
if (sameline(iftok,dtok) && dtok->name && defined.find(dtok->str()) == defined.end() && undefined.find(dtok->str()) == undefined.end())
configset.insert(dtok->str());
}
std::string cfgStr;
for (const std::string &s : configset) {
if (!cfgStr.empty())
cfgStr += ';';
cfgStr += s;
}
return cfgStr;
}
static bool hasDefine(const std::string &userDefines, const std::string &cfg)
{
if (cfg.empty()) {
return false;
}
std::string::size_type pos = 0;
while (pos < userDefines.size()) {
pos = userDefines.find(cfg, pos);
if (pos == std::string::npos)
break;
const std::string::size_type pos2 = pos + cfg.size();
if ((pos == 0 || userDefines[pos-1U] == ';') && (pos2 == userDefines.size() || userDefines[pos2] == '='))
return true;
pos = pos2;
}
return false;
}
static std::string cfg(const std::vector<std::string> &configs, const std::string &userDefines)
{
std::set<std::string> configs2(configs.cbegin(), configs.cend());
std::string ret;
for (const std::string &c : configs2) {
if (c.empty())
continue;
if (c == "0")
return "";
if (hasDefine(userDefines, c))
continue;
if (!ret.empty())
ret += ';';
ret += c;
}
return ret;
}
static bool isUndefined(const std::string &cfg, const std::set<std::string> &undefined)
{
for (std::string::size_type pos1 = 0U; pos1 < cfg.size();) {
const std::string::size_type pos2 = cfg.find(';',pos1);
const std::string def = (pos2 == std::string::npos) ? cfg.substr(pos1) : cfg.substr(pos1, pos2 - pos1);
const std::string::size_type eq = def.find('=');
if (eq == std::string::npos && undefined.find(def) != undefined.end())
return true;
if (eq != std::string::npos && undefined.find(def.substr(0,eq)) != undefined.end() && def.substr(eq) != "=0")
return true;
pos1 = (pos2 == std::string::npos) ? pos2 : pos2 + 1U;
}
return false;
}
static bool getConfigsElseIsFalse(const std::vector<std::string> &configs_if, const std::string &userDefines)
{
return std::any_of(configs_if.cbegin(), configs_if.cend(),
[&](const std::string &cfg) {
return hasDefine(userDefines, cfg);
});
}
static const simplecpp::Token *gotoEndIf(const simplecpp::Token *cmdtok)
{
int level = 0;
while (nullptr != (cmdtok = cmdtok->next)) {
if (cmdtok->op == '#' && !sameline(cmdtok->previous,cmdtok) && sameline(cmdtok, cmdtok->next)) {
if (startsWith(cmdtok->next->str(),"if"))
++level;
else if (cmdtok->next->str() == "endif") {
--level;
if (level < 0)
return cmdtok;
}
}
}
return nullptr;
}
static void getConfigs(const simplecpp::TokenList &tokens, std::set<std::string> &defined, const std::string &userDefines, const std::set<std::string> &undefined, std::set<std::string> &ret)
{
std::vector<std::string> configs_if;
std::vector<std::string> configs_ifndef;
std::string elseError;
for (const simplecpp::Token *tok = tokens.cfront(); tok; tok = tok->next) {
if (tok->op != '#' || sameline(tok->previous, tok))
continue;
const simplecpp::Token *cmdtok = tok->next;
if (!sameline(tok, cmdtok))
continue;
if (cmdtok->str() == "ifdef" || cmdtok->str() == "ifndef" || cmdtok->str() == "if") {
std::string config;
if (cmdtok->str() == "ifdef" || cmdtok->str() == "ifndef") {
const simplecpp::Token *expr1 = cmdtok->next;
if (sameline(tok,expr1) && expr1->name && !sameline(tok,expr1->next))
config = expr1->str();
if (defined.find(config) != defined.end())
config.clear();
} else if (cmdtok->str() == "if") {
config = readcondition(cmdtok, defined, undefined);
}
// skip undefined configurations..
if (isUndefined(config, undefined))
config.clear();
bool ifndef = false;
if (cmdtok->str() == "ifndef")
ifndef = true;
else {
const std::array<std::string, 6> match{"if", "!", "defined", "(", config, ")"};
std::size_t i = 0;
ifndef = true;
for (const simplecpp::Token *t = cmdtok; i < match.size(); t = t->next) {
if (!t || t->str() != match[i++]) {
ifndef = false;
break;
}
}
}
// include guard..
if (ifndef && tok->location.fileIndex > 0) {
bool includeGuard = true;
for (const simplecpp::Token *t = tok->previous; t; t = t->previous) {
if (t->location.fileIndex == tok->location.fileIndex) {
includeGuard = false;
break;
}
}
if (includeGuard) {
configs_if.emplace_back(/*std::string()*/);
configs_ifndef.emplace_back(/*std::string()*/);
continue;
}
}
configs_if.push_back((cmdtok->str() == "ifndef") ? std::string() : config);
configs_ifndef.push_back((cmdtok->str() == "ifndef") ? std::move(config) : std::string());
ret.insert(cfg(configs_if,userDefines));
} else if (cmdtok->str() == "elif" || cmdtok->str() == "else") {
if (getConfigsElseIsFalse(configs_if,userDefines)) {
tok = gotoEndIf(tok);
if (!tok)
break;
tok = tok->previous;
continue;
}
if (cmdtok->str() == "else" &&
cmdtok->next &&
!sameline(cmdtok,cmdtok->next) &&
sameline(cmdtok->next, cmdtok->next->next) &&
cmdtok->next->op == '#' &&
cmdtok->next->next->str() == "error") {
const std::string &ifcfg = cfg(configs_if, userDefines);
if (!ifcfg.empty()) {
if (!elseError.empty())
elseError += ';';
elseError += ifcfg;
}
}
if (!configs_if.empty())
configs_if.pop_back();
if (cmdtok->str() == "elif") {
std::string config = readcondition(cmdtok, defined, undefined);
if (isUndefined(config,undefined))
config.clear();
configs_if.push_back(std::move(config));
ret.insert(cfg(configs_if, userDefines));
} else if (!configs_ifndef.empty()) {
configs_if.push_back(configs_ifndef.back());
ret.insert(cfg(configs_if, userDefines));
}
} else if (cmdtok->str() == "endif" && !sameline(tok, cmdtok->next)) {
if (!configs_if.empty())
configs_if.pop_back();
if (!configs_ifndef.empty())
configs_ifndef.pop_back();
} else if (cmdtok->str() == "error") {
if (!configs_ifndef.empty() && !configs_ifndef.back().empty()) {
if (configs_ifndef.size() == 1U)
ret.erase(emptyString);
std::vector<std::string> configs(configs_if);
configs.push_back(configs_ifndef.back());
ret.erase(cfg(configs, userDefines));
std::set<std::string> temp;
temp.swap(ret);
for (const std::string &c: temp) {
if (c.find(configs_ifndef.back()) != std::string::npos)
ret.insert(c);
else if (c.empty())
ret.insert("");
else
ret.insert(c + ";" + configs_ifndef.back());
}
if (!elseError.empty())
elseError += ';';
elseError += cfg(configs_ifndef, userDefines);
}
if (!configs_if.empty() && !configs_if.back().empty()) {
const std::string &last = configs_if.back();
if (last.size() > 2U && last.compare(last.size()-2U,2,"=0") == 0) {
std::vector<std::string> configs(configs_if);
ret.erase(cfg(configs, userDefines));
configs[configs.size() - 1U] = last.substr(0,last.size()-2U);
if (configs.size() == 1U)
ret.erase("");
if (!elseError.empty())
elseError += ';';
elseError += cfg(configs, userDefines);
}
}
} else if (cmdtok->str() == "define" && sameline(tok, cmdtok->next) && cmdtok->next->name) {
defined.insert(cmdtok->next->str());
}
}
if (!elseError.empty())
ret.insert(std::move(elseError));
}
std::set<std::string> Preprocessor::getConfigs(const simplecpp::TokenList &tokens) const
{
std::set<std::string> ret = { "" };
if (!tokens.cfront())
return ret;
std::set<std::string> defined = { "__cplusplus" };
::getConfigs(tokens, defined, mSettings.userDefines, mSettings.userUndefs, ret);
for (auto it = mTokenLists.cbegin(); it != mTokenLists.cend(); ++it) {
if (!mSettings.configurationExcluded(it->first))
::getConfigs(*(it->second), defined, mSettings.userDefines, mSettings.userUndefs, ret);
}
return ret;
}
static void splitcfg(const std::string &cfg, std::list<std::string> &defines, const std::string &defaultValue)
{
for (std::string::size_type defineStartPos = 0U; defineStartPos < cfg.size();) {
const std::string::size_type defineEndPos = cfg.find(';', defineStartPos);
std::string def = (defineEndPos == std::string::npos) ? cfg.substr(defineStartPos) : cfg.substr(defineStartPos, defineEndPos - defineStartPos);
if (!defaultValue.empty() && def.find('=') == std::string::npos)
def += '=' + defaultValue;
defines.push_back(std::move(def));
if (defineEndPos == std::string::npos)
break;
defineStartPos = defineEndPos + 1U;
}
}
static simplecpp::DUI createDUI(const Settings &mSettings, const std::string &cfg, const std::string &filename)
{
// TODO: make it possible to specify platform-dependent sizes
simplecpp::DUI dui;
splitcfg(mSettings.userDefines, dui.defines, "1");
if (!cfg.empty())
splitcfg(cfg, dui.defines, "");
for (const std::string &def : mSettings.library.defines()) {
const std::string::size_type pos = def.find_first_of(" (");
if (pos == std::string::npos) {
dui.defines.push_back(def);
continue;
}
std::string s = def;
if (s[pos] == ' ') {
s[pos] = '=';
} else {
s[s.find(')')+1] = '=';
}
dui.defines.push_back(std::move(s));
}
dui.undefined = mSettings.userUndefs; // -U
dui.includePaths = mSettings.includePaths; // -I
dui.includes = mSettings.userIncludes; // --include
// TODO: use mSettings.standards.stdValue instead
// TODO: error out on unknown language?
const Standards::Language lang = Path::identify(filename, mSettings.cppHeaderProbe);
if (lang == Standards::Language::CPP) {
dui.std = mSettings.standards.getCPP();
splitcfg(mSettings.platform.getLimitsDefines(Standards::getCPP(dui.std)), dui.defines, "");
}
else if (lang == Standards::Language::C) {
dui.std = mSettings.standards.getC();
splitcfg(mSettings.platform.getLimitsDefines(Standards::getC(dui.std)), dui.defines, "");
}
dui.clearIncludeCache = mSettings.clearIncludeCache;
return dui;
}
bool Preprocessor::hasErrors(const simplecpp::Output &output)
{
switch (output.type) {
case simplecpp::Output::ERROR:
case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
case simplecpp::Output::SYNTAX_ERROR:
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
case simplecpp::Output::FILE_NOT_FOUND:
case simplecpp::Output::DUI_ERROR:
return true;
case simplecpp::Output::WARNING:
case simplecpp::Output::MISSING_HEADER:
case simplecpp::Output::PORTABILITY_BACKSLASH:
break;
}
return false;
}
bool Preprocessor::hasErrors(const simplecpp::OutputList &outputList)
{
const auto it = std::find_if(outputList.cbegin(), outputList.cend(), [](const simplecpp::Output &output) {
return hasErrors(output);
});
return it != outputList.cend();
}
void Preprocessor::handleErrors(const simplecpp::OutputList& outputList, bool throwError)
{
const bool showerror = (!mSettings.userDefines.empty() && !mSettings.force);
reportOutput(outputList, showerror);
if (throwError) {
const auto it = std::find_if(outputList.cbegin(), outputList.cend(), [](const simplecpp::Output &output){
return hasErrors(output);
});
if (it != outputList.cend()) {
throw *it;
}
}
}
bool Preprocessor::loadFiles(const simplecpp::TokenList &rawtokens, std::vector<std::string> &files)
{
const simplecpp::DUI dui = createDUI(mSettings, "", files[0]);
simplecpp::OutputList outputList;
mTokenLists = simplecpp::load(rawtokens, files, dui, &outputList);
handleErrors(outputList, false);
return !hasErrors(outputList);
}
void Preprocessor::removeComments(simplecpp::TokenList &tokens)
{
tokens.removeComments();
for (std::pair<const std::string, simplecpp::TokenList*>& tokenList : mTokenLists) {
if (tokenList.second)
tokenList.second->removeComments();
}
}
void Preprocessor::setPlatformInfo(simplecpp::TokenList &tokens, const Settings& settings)
{
tokens.sizeOfType["bool"] = settings.platform.sizeof_bool;
tokens.sizeOfType["short"] = settings.platform.sizeof_short;
tokens.sizeOfType["int"] = settings.platform.sizeof_int;
tokens.sizeOfType["long"] = settings.platform.sizeof_long;
tokens.sizeOfType["long long"] = settings.platform.sizeof_long_long;
tokens.sizeOfType["float"] = settings.platform.sizeof_float;
tokens.sizeOfType["double"] = settings.platform.sizeof_double;
tokens.sizeOfType["long double"] = settings.platform.sizeof_long_double;
tokens.sizeOfType["bool *"] = settings.platform.sizeof_pointer;
tokens.sizeOfType["short *"] = settings.platform.sizeof_pointer;
tokens.sizeOfType["int *"] = settings.platform.sizeof_pointer;
tokens.sizeOfType["long *"] = settings.platform.sizeof_pointer;
tokens.sizeOfType["long long *"] = settings.platform.sizeof_pointer;
tokens.sizeOfType["float *"] = settings.platform.sizeof_pointer;
tokens.sizeOfType["double *"] = settings.platform.sizeof_pointer;
tokens.sizeOfType["long double *"] = settings.platform.sizeof_pointer;
}
simplecpp::TokenList Preprocessor::preprocess(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector<std::string> &files, bool throwError)
{
const simplecpp::DUI dui = createDUI(mSettings, cfg, files[0]);
simplecpp::OutputList outputList;
std::list<simplecpp::MacroUsage> macroUsage;
std::list<simplecpp::IfCond> ifCond;
simplecpp::TokenList tokens2(files);
simplecpp::preprocess(tokens2, tokens1, files, mTokenLists, dui, &outputList, ¯oUsage, &ifCond);
mMacroUsage = std::move(macroUsage);
mIfCond = std::move(ifCond);
handleErrors(outputList, throwError);
tokens2.removeComments();
return tokens2;
}
std::string Preprocessor::getcode(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector<std::string> &files, const bool writeLocations)
{
simplecpp::TokenList tokens2 = preprocess(tokens1, cfg, files, false);
unsigned int prevfile = 0;
unsigned int line = 1;
std::ostringstream ret;
for (const simplecpp::Token *tok = tokens2.cfront(); tok; tok = tok->next) {
if (writeLocations && tok->location.fileIndex != prevfile) {
ret << "\n#line " << tok->location.line << " \"" << tok->location.file() << "\"\n";
prevfile = tok->location.fileIndex;
line = tok->location.line;
}
if (tok->previous && line >= tok->location.line) // #7912
ret << ' ';
while (tok->location.line > line) {
ret << '\n';
line++;
}
if (!tok->macro.empty())
ret << Preprocessor::macroChar;
ret << tok->str();
}
return ret.str();
}
void Preprocessor::reportOutput(const simplecpp::OutputList &outputList, bool showerror)
{
for (const simplecpp::Output &out : outputList) {
switch (out.type) {
case simplecpp::Output::ERROR:
if (!startsWith(out.msg,"#error") || showerror)
error(out.location.file(), out.location.line, out.msg);
break;
case simplecpp::Output::WARNING:
case simplecpp::Output::PORTABILITY_BACKSLASH:
break;
case simplecpp::Output::MISSING_HEADER: {
const std::string::size_type pos1 = out.msg.find_first_of("<\"");
const std::string::size_type pos2 = out.msg.find_first_of(">\"", pos1 + 1U);
if (pos1 < pos2 && pos2 != std::string::npos)
missingInclude(out.location.file(), out.location.line, out.msg.substr(pos1+1, pos2-pos1-1), out.msg[pos1] == '\"' ? UserHeader : SystemHeader);
}
break;
case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
case simplecpp::Output::SYNTAX_ERROR:
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
error(out.location.file(), out.location.line, out.msg);
break;
case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
case simplecpp::Output::FILE_NOT_FOUND:
case simplecpp::Output::DUI_ERROR:
error("", 0, out.msg);
break;
}
}
}
void Preprocessor::error(const std::string &filename, unsigned int linenr, const std::string &msg)
{
std::list<ErrorMessage::FileLocation> locationList;
if (!filename.empty()) {
std::string file = Path::fromNativeSeparators(filename);
if (mSettings.relativePaths)
file = Path::getRelativePath(file, mSettings.basePaths);
locationList.emplace_back(file, linenr, 0);
}
mErrorLogger.reportErr(ErrorMessage(std::move(locationList),
mFile0,
Severity::error,
msg,
"preprocessorErrorDirective",
Certainty::normal));
}
// Report that include is missing
void Preprocessor::missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, HeaderTypes headerType)
{
if (!mSettings.checks.isEnabled(Checks::missingInclude))
return;
std::list<ErrorMessage::FileLocation> locationList;
if (!filename.empty()) {
locationList.emplace_back(filename, linenr, 0);
}
ErrorMessage errmsg(std::move(locationList), mFile0, Severity::information,
(headerType==SystemHeader) ?
"Include file: <" + header + "> not found. Please note: Cppcheck does not need standard library headers to get proper results." :
"Include file: \"" + header + "\" not found.",
(headerType==SystemHeader) ? "missingIncludeSystem" : "missingInclude",
Certainty::normal);
mErrorLogger.reportErr(errmsg);
}
void Preprocessor::getErrorMessages(ErrorLogger &errorLogger, const Settings &settings)
{
Preprocessor preprocessor(settings, errorLogger);
preprocessor.missingInclude("", 1, "", UserHeader);
preprocessor.missingInclude("", 1, "", SystemHeader);
preprocessor.error("", 1, "#error message"); // #error ..
}
void Preprocessor::dump(std::ostream &out) const
{
// Create a xml dump.
if (!mMacroUsage.empty()) {
out << " <macro-usage>" << std::endl;
for (const simplecpp::MacroUsage ¯oUsage: mMacroUsage) {
out << " <macro"
<< " name=\"" << macroUsage.macroName << "\""
<< " file=\"" << ErrorLogger::toxml(macroUsage.macroLocation.file()) << "\""
<< " line=\"" << macroUsage.macroLocation.line << "\""
<< " column=\"" << macroUsage.macroLocation.col << "\""
<< " usefile=\"" << ErrorLogger::toxml(macroUsage.useLocation.file()) << "\""
<< " useline=\"" << macroUsage.useLocation.line << "\""
<< " usecolumn=\"" << macroUsage.useLocation.col << "\""
<< " is-known-value=\"" << bool_to_string(macroUsage.macroValueKnown) << "\""
<< "/>" << std::endl;
}
out << " </macro-usage>" << std::endl;
}
if (!mIfCond.empty()) {
out << " <simplecpp-if-cond>" << std::endl;
for (const simplecpp::IfCond &ifCond: mIfCond) {
out << " <if-cond"
<< " file=\"" << ErrorLogger::toxml(ifCond.location.file()) << "\""
<< " line=\"" << ifCond.location.line << "\""
<< " column=\"" << ifCond.location.col << "\""
<< " E=\"" << ErrorLogger::toxml(ifCond.E) << "\""
<< " result=\"" << ifCond.result << "\""
<< "/>" << std::endl;
}
out << " </simplecpp-if-cond>" << std::endl;
}
}
std::size_t Preprocessor::calculateHash(const simplecpp::TokenList &tokens1, const std::string &toolinfo) const
{
std::string hashData = toolinfo;
for (const simplecpp::Token *tok = tokens1.cfront(); tok; tok = tok->next) {
if (!tok->comment) {
hashData += tok->str();
hashData += static_cast<char>(tok->location.line);
hashData += static_cast<char>(tok->location.col);
}
}
for (auto it = mTokenLists.cbegin(); it != mTokenLists.cend(); ++it) {
for (const simplecpp::Token *tok = it->second->cfront(); tok; tok = tok->next) {
if (!tok->comment) {
hashData += tok->str();
hashData += static_cast<char>(tok->location.line);
hashData += static_cast<char>(tok->location.col);
}
}
}
return (std::hash<std::string>{})(hashData);
}
void Preprocessor::simplifyPragmaAsm(simplecpp::TokenList &tokenList) const
{
Preprocessor::simplifyPragmaAsmPrivate(tokenList);
for (const std::pair<const std::string, simplecpp::TokenList*>& list : mTokenLists) {
Preprocessor::simplifyPragmaAsmPrivate(*list.second);
}
}
void Preprocessor::simplifyPragmaAsmPrivate(simplecpp::TokenList &tokenList)
{
// assembler code..
for (simplecpp::Token *tok = tokenList.front(); tok; tok = tok->next) {
if (tok->op != '#')
continue;
if (sameline(tok, tok->previousSkipComments()))
continue;
const simplecpp::Token * const tok2 = tok->nextSkipComments();
if (!tok2 || !sameline(tok, tok2) || tok2->str() != "pragma")
continue;
const simplecpp::Token * const tok3 = tok2->nextSkipComments();
if (!tok3 || !sameline(tok, tok3) || tok3->str() != "asm")
continue;
const simplecpp::Token *endasm = tok3;
while ((endasm = endasm->next) != nullptr) {
if (endasm->op != '#' || sameline(endasm,endasm->previousSkipComments()))
continue;
const simplecpp::Token * const endasm2 = endasm->nextSkipComments();
if (!endasm2 || !sameline(endasm, endasm2) || endasm2->str() != "pragma")
continue;
const simplecpp::Token * const endasm3 = endasm2->nextSkipComments();
if (!endasm3 || !sameline(endasm2, endasm3) || endasm3->str() != "endasm")
continue;
while (sameline(endasm,endasm3))
endasm = endasm->next;
break;
}
const simplecpp::Token * const tok4 = tok3->next;
tok->setstr("asm");
const_cast<simplecpp::Token *>(tok2)->setstr("(");
const_cast<simplecpp::Token *>(tok3)->setstr(")");
const_cast<simplecpp::Token *>(tok4)->setstr(";");
while (tok4->next != endasm)
tokenList.deleteToken(tok4->next);
}
}
void Preprocessor::addRemarkComments(const simplecpp::TokenList &tokens, std::vector<RemarkComment> &remarkComments) const
{
for (const simplecpp::Token *tok = tokens.cfront(); tok; tok = tok->next) {
if (!tok->comment)
continue;
const std::string& comment = tok->str();
// is it a remark comment?
const std::string::size_type pos1 = comment.find_first_not_of("/* \t");
if (pos1 == std::string::npos)
continue;
const std::string::size_type pos2 = comment.find_first_of(": \t", pos1);
if (pos2 != pos1 + 6 || comment.compare(pos1, 6, "REMARK") != 0)
continue;
const std::string::size_type pos3 = comment.find_first_not_of(": \t", pos2);
if (pos3 == std::string::npos)
continue;
if (comment.compare(0,2,"/*") == 0 && pos3 + 2 >= tok->str().size())
continue;
const std::string::size_type pos4 = (comment.compare(0,2,"/*") == 0) ? comment.size()-2 : comment.size();
const std::string remarkText = comment.substr(pos3, pos4-pos3);
// Get remarked token
const simplecpp::Token* remarkedToken = nullptr;
for (const simplecpp::Token* after = tok->next; after; after = after->next) {
if (after->comment)
continue;
remarkedToken = after;
break;
}
for (const simplecpp::Token* prev = tok->previous; prev; prev = prev->previous) {
if (prev->comment)
continue;
if (sameline(prev, tok))
remarkedToken = prev;
break;
}
// Relative filename
const std::string relativeFilename = getRelativeFilename(remarkedToken, mSettings);
// Add the suppressions.
remarkComments.emplace_back(relativeFilename, remarkedToken->location.line, remarkText);
}
}
|