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 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
|
/* -*- C++ -*-
* 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/>.
*/
//---------------------------------------------------------------------------
#ifndef tokenH
#define tokenH
//---------------------------------------------------------------------------
#include "config.h"
#include "errortypes.h"
#include "mathlib.h"
#include "templatesimplifier.h"
#include "utils.h"
#include "vfvalue.h"
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <cstddef>
#include <functional>
#include <list>
#include <memory>
#include <ostream>
#include <set>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
struct Enumerator;
class Function;
class Scope;
class Settings;
class Type;
class ValueType;
class Variable;
class ConstTokenRange;
class Token;
struct TokensFrontBack;
struct ScopeInfo2 {
ScopeInfo2(std::string name_, const Token *bodyEnd_, std::set<std::string> usingNamespaces_ = std::set<std::string>()) : name(std::move(name_)), bodyEnd(bodyEnd_), usingNamespaces(std::move(usingNamespaces_)) {}
std::string name;
const Token* const bodyEnd{};
std::set<std::string> usingNamespaces;
};
enum class TokenDebug : std::uint8_t { None, ValueFlow, ValueType };
struct TokenImpl {
nonneg int mVarId{};
nonneg int mFileIndex{};
nonneg int mLineNumber{};
nonneg int mColumn{};
nonneg int mExprId{};
/**
* A value from 0-100 that provides a rough idea about where in the token
* list this token is located.
*/
nonneg int mProgressValue{};
/**
* Token index. Position in token list
*/
nonneg int mIndex{};
/** Bitfield bit count. */
unsigned char mBits{};
// AST..
Token* mAstOperand1{};
Token* mAstOperand2{};
Token* mAstParent{};
// symbol database information
const Scope* mScope{};
union {
const Function *mFunction;
const Variable *mVariable;
const ::Type* mType;
const Enumerator *mEnumerator;
};
// original name like size_t
std::string* mOriginalName{};
// If this token came from a macro replacement list, this is the name of that macro
std::string mMacroName;
// ValueType
ValueType* mValueType{};
// ValueFlow
std::list<ValueFlow::Value>* mValues{};
static const std::list<ValueFlow::Value> mEmptyValueList;
// Pointer to a template in the template simplifier
std::set<TemplateSimplifier::TokenAndName*>* mTemplateSimplifierPointers{};
// Pointer to the object representing this token's scope
std::shared_ptr<ScopeInfo2> mScopeInfo;
// __cppcheck_in_range__
struct CppcheckAttributes {
enum Type : std::uint8_t { LOW, HIGH } type = LOW;
MathLib::bigint value{};
CppcheckAttributes* next{};
};
CppcheckAttributes* mCppcheckAttributes{};
// alignas expressions
std::unique_ptr<std::vector<std::string>> mAttributeAlignas;
void addAttributeAlignas(const std::string& a) {
if (!mAttributeAlignas)
mAttributeAlignas = std::unique_ptr<std::vector<std::string>>(new std::vector<std::string>());
if (std::find(mAttributeAlignas->cbegin(), mAttributeAlignas->cend(), a) == mAttributeAlignas->cend())
mAttributeAlignas->push_back(a);
}
std::string mAttributeCleanup;
// For memoization, to speed up parsing of huge arrays #8897
enum class Cpp11init : std::uint8_t { UNKNOWN, CPP11INIT, NOINIT } mCpp11init = Cpp11init::UNKNOWN;
TokenDebug mDebug{};
void setCppcheckAttribute(CppcheckAttributes::Type type, MathLib::bigint value);
bool getCppcheckAttribute(CppcheckAttributes::Type type, MathLib::bigint &value) const;
TokenImpl() : mFunction(nullptr) {}
~TokenImpl();
};
/// @addtogroup Core
/// @{
/**
* @brief The token list that the TokenList generates is a linked-list of this class.
*
* Tokens are stored as strings. The "if", "while", etc are stored in plain text.
* The reason the Token class is needed (instead of using the string class) is that some extra functionality is also needed for tokens:
* - location of the token is stored (fileIndex, linenr, column)
* - functions for classifying the token (isName, isNumber, isBoolean, isStandardType)
*
* The Token class also has other functions for management of token list, matching tokens, etc.
*/
class CPPCHECKLIB Token {
friend class TestToken;
private:
TokensFrontBack& mTokensFrontBack;
static const std::string mEmptyString;
public:
Token(const Token &) = delete;
Token& operator=(const Token &) = delete;
enum Type : std::uint8_t {
eVariable, eType, eFunction, eKeyword, eName, // Names: Variable (varId), Type (typeId, later), Function (FuncId, later), Language keyword, Name (unknown identifier)
eNumber, eString, eChar, eBoolean, eLiteral, eEnumerator, // Literals: Number, String, Character, Boolean, User defined literal (C++11), Enumerator
eArithmeticalOp, eComparisonOp, eAssignmentOp, eLogicalOp, eBitOp, eIncDecOp, eExtendedOp, // Operators: Arithmetical, Comparison, Assignment, Logical, Bitwise, ++/--, Extended
eBracket, // {, }, <, >: < and > only if link() is set. Otherwise they are comparison operators.
eLambda, // A function without a name
eEllipsis, // "..."
eOther,
eNone
};
explicit Token(TokensFrontBack &tokensFrontBack);
// for usage in CheckIO::ArgumentInfo only
explicit Token(const Token *tok);
~Token();
ConstTokenRange until(const Token * t) const;
template<typename T>
void str(T&& s) {
mStr = s;
mImpl->mVarId = 0;
update_property_info();
}
/**
* Concatenate two (quoted) strings. Automatically cuts of the last/first character.
* Example: "hello ""world" -> "hello world". Used by the token simplifier.
*/
void concatStr(std::string const& b);
const std::string &str() const {
return mStr;
}
/**
* Unlink and delete the next 'count' tokens.
*/
void deleteNext(nonneg int count = 1);
/**
* Unlink and delete the previous 'count' tokens.
*/
void deletePrevious(nonneg int count = 1);
/**
* Swap the contents of this token with the next token.
*/
void swapWithNext();
/**
* @return token in given index, related to this token.
* For example index 1 would return next token, and 2
* would return next from that one.
*/
const Token *tokAt(int index) const
{
return tokAtImpl(this, index);
}
Token *tokAt(int index)
{
return tokAtImpl(this, index);
}
/**
* @return the link to the token in given index, related to this token.
* For example index 1 would return the link to next token.
*/
const Token *linkAt(int index) const
{
return linkAtImpl(this, index);
}
Token *linkAt(int index)
{
return linkAtImpl(this, index);
}
/**
* @return String of the token in given index, related to this token.
* If that token does not exist, an empty string is being returned.
*/
const std::string &strAt(int index) const
{
const Token *tok = this->tokAt(index);
return tok ? tok->mStr : mEmptyString;
}
/**
* Match given token (or list of tokens) to a pattern list.
*
* Possible patterns
* "someRandomText" If token contains "someRandomText".
* @note Use Match() if you want to use flags in patterns
*
* The patterns can be also combined to compare to multiple tokens at once
* by separating tokens with a space, e.g.
* ") void {" will return true if first token is ')' next token
* is "void" and token after that is '{'. If even one of the tokens does
* not match its pattern, false is returned.
*
* @param tok List of tokens to be compared to the pattern
* @param pattern The pattern against which the tokens are compared,
* e.g. "const" or ") void {".
* @return true if given token matches with given pattern
* false if given token does not match with given pattern
*/
template<size_t count>
static bool simpleMatch(const Token *tok, const char (&pattern)[count]) {
return simpleMatch(tok, pattern, count-1);
}
static bool simpleMatch(const Token *tok, const char pattern[], size_t pattern_len);
/**
* Match given token (or list of tokens) to a pattern list.
*
* Possible patterns
* - "%any%" any token
* - "%assign%" a assignment operand
* - "%bool%" true or false
* - "%char%" Any token enclosed in '-character.
* - "%comp%" Any token such that isComparisonOp() returns true.
* - "%cop%" Any token such that isConstOp() returns true.
* - "%name%" any token which is a name, variable or type e.g. "hello" or "int". Also matches keywords.
* - "%num%" Any numeric token, e.g. "23"
* - "%op%" Any token such that isOp() returns true.
* - "%or%" A bitwise-or operator '|'
* - "%oror%" A logical-or operator '||'
* - "%type%" Anything that can be a variable type, e.g. "int". Also matches keywords.
* - "%str%" Any token starting with "-character (C-string).
* - "%var%" Match with token with varId > 0
* - "%varid%" Match with parameter varid
* - "[abc]" Any of the characters 'a' or 'b' or 'c'
* - "int|void|char" Any of the strings, int, void or char
* - "int|void|char|" Any of the strings, int, void or char or no token
* - "!!else" No tokens or any token that is not "else".
* - "someRandomText" If token contains "someRandomText".
*
* multi-compare patterns such as "int|void|char" can contain %%or%, %%oror% and %%op%
* it is recommended to put such an %%cmd% as the first pattern.
* For example: "%var%|%num%|)" means yes to a variable, a number or ')'.
*
* The patterns can be also combined to compare to multiple tokens at once
* by separating tokens with a space, e.g.
* ") const|void {" will return true if first token is ')' next token is either
* "const" or "void" and token after that is '{'. If even one of the tokens does not
* match its pattern, false is returned.
*
* @param tok List of tokens to be compared to the pattern
* @param pattern The pattern against which the tokens are compared,
* e.g. "const" or ") const|volatile| {".
* @param varid if %%varid% is given in the pattern the Token::varId
* will be matched against this argument
* @return true if given token matches with given pattern
* false if given token does not match with given pattern
*/
static bool Match(const Token *tok, const char pattern[], nonneg int varid = 0);
/**
* @return length of C-string.
*
* Should be called for %%str%% tokens only.
*
* @param tok token with C-string
**/
static nonneg int getStrLength(const Token *tok);
/**
* @return array length of C-string.
*
* Should be called for %%str%% tokens only.
*
* @param tok token with C-string
**/
static nonneg int getStrArraySize(const Token *tok);
/**
* @return sizeof of C-string.
*
* Should be called for %%str%% tokens only.
*
* @param tok token with C-string
* @param settings Settings
**/
static nonneg int getStrSize(const Token *tok, const Settings & settings);
const ValueType *valueType() const {
return mImpl->mValueType;
}
void setValueType(ValueType *vt);
const ValueType *argumentType() const;
Token::Type tokType() const {
return mTokType;
}
void tokType(Token::Type t) {
mTokType = t;
const bool memoizedIsName = (mTokType == eName || mTokType == eType || mTokType == eVariable ||
mTokType == eFunction || mTokType == eKeyword || mTokType == eBoolean ||
mTokType == eEnumerator); // TODO: "true"/"false" aren't really a name...
setFlag(fIsName, memoizedIsName);
const bool memoizedIsLiteral = (mTokType == eNumber || mTokType == eString || mTokType == eChar ||
mTokType == eBoolean || mTokType == eLiteral || mTokType == eEnumerator);
setFlag(fIsLiteral, memoizedIsLiteral);
}
bool isKeyword() const {
return mTokType == eKeyword;
}
bool isName() const {
return getFlag(fIsName);
}
bool isNameOnly() const {
return mFlags == fIsName && mTokType == eName;
}
bool isUpperCaseName() const;
bool isLiteral() const {
return getFlag(fIsLiteral);
}
bool isNumber() const {
return mTokType == eNumber;
}
bool isEnumerator() const {
return mTokType == eEnumerator;
}
bool isVariable() const {
return mTokType == eVariable;
}
bool isOp() const {
return (isConstOp() ||
isAssignmentOp() ||
mTokType == eIncDecOp);
}
bool isConstOp() const {
return (isArithmeticalOp() ||
mTokType == eLogicalOp ||
mTokType == eComparisonOp ||
mTokType == eBitOp);
}
bool isExtendedOp() const {
return isConstOp() ||
mTokType == eExtendedOp;
}
bool isArithmeticalOp() const {
return mTokType == eArithmeticalOp;
}
bool isComparisonOp() const {
return mTokType == eComparisonOp;
}
bool isAssignmentOp() const {
return mTokType == eAssignmentOp;
}
bool isBoolean() const {
return mTokType == eBoolean;
}
bool isIncDecOp() const {
return mTokType == eIncDecOp;
}
bool isBinaryOp() const {
return astOperand1() != nullptr && astOperand2() != nullptr;
}
bool isUnaryOp(const std::string &s) const {
return s == mStr && astOperand1() != nullptr && astOperand2() == nullptr;
}
bool isUnaryPreOp() const;
uint64_t flags() const {
return mFlags;
}
void flags(const uint64_t flags_) {
mFlags = flags_;
}
bool isUnsigned() const {
return getFlag(fIsUnsigned);
}
void isUnsigned(const bool sign) {
setFlag(fIsUnsigned, sign);
}
bool isSigned() const {
return getFlag(fIsSigned);
}
void isSigned(const bool sign) {
setFlag(fIsSigned, sign);
}
// cppcheck-suppress unusedFunction
bool isPointerCompare() const {
return getFlag(fIsPointerCompare);
}
void isPointerCompare(const bool b) {
setFlag(fIsPointerCompare, b);
}
bool isLong() const {
return getFlag(fIsLong);
}
void isLong(bool size) {
setFlag(fIsLong, size);
}
bool isStandardType() const {
return getFlag(fIsStandardType);
}
void isStandardType(const bool b) {
setFlag(fIsStandardType, b);
}
bool isExpandedMacro() const {
return !mImpl->mMacroName.empty();
}
bool isCast() const {
return getFlag(fIsCast);
}
void isCast(bool c) {
setFlag(fIsCast, c);
}
bool isAttributeConstructor() const {
return getFlag(fIsAttributeConstructor);
}
void isAttributeConstructor(const bool ac) {
setFlag(fIsAttributeConstructor, ac);
}
bool isAttributeDestructor() const {
return getFlag(fIsAttributeDestructor);
}
void isAttributeDestructor(const bool value) {
setFlag(fIsAttributeDestructor, value);
}
bool isAttributeUnused() const {
return getFlag(fIsAttributeUnused);
}
void isAttributeUnused(bool unused) {
setFlag(fIsAttributeUnused, unused);
}
bool isAttributeUsed() const {
return getFlag(fIsAttributeUsed);
}
void isAttributeUsed(const bool unused) {
setFlag(fIsAttributeUsed, unused);
}
bool isAttributePure() const {
return getFlag(fIsAttributePure);
}
void isAttributePure(const bool value) {
setFlag(fIsAttributePure, value);
}
bool isAttributeConst() const {
return getFlag(fIsAttributeConst);
}
void isAttributeConst(bool value) {
setFlag(fIsAttributeConst, value);
}
bool isAttributeNoreturn() const {
return getFlag(fIsAttributeNoreturn);
}
void isAttributeNoreturn(const bool value) {
setFlag(fIsAttributeNoreturn, value);
}
bool isAttributeNothrow() const {
return getFlag(fIsAttributeNothrow);
}
void isAttributeNothrow(const bool value) {
setFlag(fIsAttributeNothrow, value);
}
bool isAttributeExport() const {
return getFlag(fIsAttributeExport);
}
void isAttributeExport(const bool value) {
setFlag(fIsAttributeExport, value);
}
bool isAttributePacked() const {
return getFlag(fIsAttributePacked);
}
void isAttributePacked(const bool value) {
setFlag(fIsAttributePacked, value);
}
bool isAttributeNodiscard() const {
return getFlag(fIsAttributeNodiscard);
}
void isAttributeNodiscard(const bool value) {
setFlag(fIsAttributeNodiscard, value);
}
bool isAttributeMaybeUnused() const {
return getFlag(fIsAttributeMaybeUnused);
}
void isAttributeMaybeUnused(const bool value) {
setFlag(fIsAttributeMaybeUnused, value);
}
std::vector<std::string> getAttributeAlignas() const {
return mImpl->mAttributeAlignas ? *mImpl->mAttributeAlignas : std::vector<std::string>();
}
bool hasAttributeAlignas() const {
return !!mImpl->mAttributeAlignas;
}
void addAttributeAlignas(const std::string& a) {
mImpl->addAttributeAlignas(a);
}
void addAttributeCleanup(const std::string& funcname) {
mImpl->mAttributeCleanup = funcname;
}
const std::string& getAttributeCleanup() const {
return mImpl->mAttributeCleanup;
}
bool hasAttributeCleanup() const {
return !mImpl->mAttributeCleanup.empty();
}
void setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type type, MathLib::bigint value) {
mImpl->setCppcheckAttribute(type, value);
}
bool getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type type, MathLib::bigint &value) const {
return mImpl->getCppcheckAttribute(type, value);
}
// cppcheck-suppress unusedFunction
bool hasCppcheckAttributes() const {
return nullptr != mImpl->mCppcheckAttributes;
}
bool isControlFlowKeyword() const {
return getFlag(fIsControlFlowKeyword);
}
bool isOperatorKeyword() const {
return getFlag(fIsOperatorKeyword);
}
void isOperatorKeyword(const bool value) {
setFlag(fIsOperatorKeyword, value);
}
bool isComplex() const {
return getFlag(fIsComplex);
}
void isComplex(const bool value) {
setFlag(fIsComplex, value);
}
bool isEnumType() const {
return getFlag(fIsEnumType);
}
void isEnumType(const bool value) {
setFlag(fIsEnumType, value);
}
bool isAtAddress() const {
return getFlag(fAtAddress);
}
void isAtAddress(bool b) {
setFlag(fAtAddress, b);
}
bool isIncompleteVar() const {
return getFlag(fIncompleteVar);
}
void isIncompleteVar(bool b) {
setFlag(fIncompleteVar, b);
}
bool isSimplifiedTypedef() const {
return getFlag(fIsSimplifiedTypedef);
}
void isSimplifiedTypedef(bool b) {
setFlag(fIsSimplifiedTypedef, b);
}
bool isIncompleteConstant() const {
return getFlag(fIsIncompleteConstant);
}
void isIncompleteConstant(bool b) {
setFlag(fIsIncompleteConstant, b);
}
bool isConstexpr() const {
return getFlag(fConstexpr);
}
void isConstexpr(bool b) {
setFlag(fConstexpr, b);
}
bool isExternC() const {
return getFlag(fExternC);
}
void isExternC(bool b) {
setFlag(fExternC, b);
}
bool isSplittedVarDeclComma() const {
return getFlag(fIsSplitVarDeclComma);
}
void isSplittedVarDeclComma(bool b) {
setFlag(fIsSplitVarDeclComma, b);
}
bool isSplittedVarDeclEq() const {
return getFlag(fIsSplitVarDeclEq);
}
void isSplittedVarDeclEq(bool b) {
setFlag(fIsSplitVarDeclEq, b);
}
bool isImplicitInt() const {
return getFlag(fIsImplicitInt);
}
void isImplicitInt(bool b) {
setFlag(fIsImplicitInt, b);
}
bool isInline() const {
return getFlag(fIsInline);
}
void isInline(bool b) {
setFlag(fIsInline, b);
}
bool isAtomic() const {
return getFlag(fIsAtomic);
}
void isAtomic(bool b) {
setFlag(fIsAtomic, b);
}
bool isRestrict() const {
return getFlag(fIsRestrict);
}
void isRestrict(bool b) {
setFlag(fIsRestrict, b);
}
bool isRemovedVoidParameter() const {
return getFlag(fIsRemovedVoidParameter);
}
void setRemovedVoidParameter(bool b) {
setFlag(fIsRemovedVoidParameter, b);
}
bool isTemplate() const {
return getFlag(fIsTemplate);
}
void isTemplate(bool b) {
setFlag(fIsTemplate, b);
}
bool isSimplifiedScope() const {
return getFlag(fIsSimplifedScope);
}
void isSimplifiedScope(bool b) {
setFlag(fIsSimplifedScope, b);
}
bool isFinalType() const {
return getFlag(fIsFinalType);
}
void isFinalType(bool b) {
setFlag(fIsFinalType, b);
}
bool isInitComma() const {
return getFlag(fIsInitComma);
}
void isInitComma(bool b) {
setFlag(fIsInitComma, b);
}
bool isInitBracket() const {
return getFlag(fIsInitBracket);
}
void isInitBracket(bool b) {
setFlag(fIsInitBracket, b);
}
// cppcheck-suppress unusedFunction
bool isBitfield() const {
return mImpl->mBits > 0;
}
unsigned char bits() const {
return mImpl->mBits;
}
const std::set<TemplateSimplifier::TokenAndName*>* templateSimplifierPointers() const {
return mImpl->mTemplateSimplifierPointers;
}
std::set<TemplateSimplifier::TokenAndName*>* templateSimplifierPointers() {
return mImpl->mTemplateSimplifierPointers;
}
void templateSimplifierPointer(TemplateSimplifier::TokenAndName* tokenAndName) {
if (!mImpl->mTemplateSimplifierPointers)
mImpl->mTemplateSimplifierPointers = new std::set<TemplateSimplifier::TokenAndName*>;
mImpl->mTemplateSimplifierPointers->insert(tokenAndName);
}
void setBits(const unsigned char b) {
mImpl->mBits = b;
}
bool isUtf8() const {
return (((mTokType == eString) && isPrefixStringCharLiteral(mStr, '"', "u8")) ||
((mTokType == eChar) && isPrefixStringCharLiteral(mStr, '\'', "u8")));
}
bool isUtf16() const {
return (((mTokType == eString) && isPrefixStringCharLiteral(mStr, '"', "u")) ||
((mTokType == eChar) && isPrefixStringCharLiteral(mStr, '\'', "u")));
}
bool isUtf32() const {
return (((mTokType == eString) && isPrefixStringCharLiteral(mStr, '"', "U")) ||
((mTokType == eChar) && isPrefixStringCharLiteral(mStr, '\'', "U")));
}
bool isCChar() const {
return (((mTokType == eString) && isPrefixStringCharLiteral(mStr, '"', emptyString)) ||
((mTokType == eChar) && isPrefixStringCharLiteral(mStr, '\'', emptyString) && (replaceEscapeSequences(getCharLiteral(mStr)).size() == 1)));
}
bool isCMultiChar() const {
return (mTokType == eChar) && isPrefixStringCharLiteral(mStr, '\'', emptyString) && (replaceEscapeSequences(getCharLiteral(mStr)).size() > 1);
}
/**
* @brief Is current token a template argument?
*
* Original code:
*
* template<class C> struct S {
* C x;
* };
* S<int> s;
*
* Resulting code:
*
* struct S<int> {
* int x ; // <- "int" is a template argument
* }
* S<int> s;
*/
bool isTemplateArg() const {
return getFlag(fIsTemplateArg);
}
void isTemplateArg(const bool value) {
setFlag(fIsTemplateArg, value);
}
const std::string& getMacroName() const {
return mImpl->mMacroName;
}
void setMacroName(std::string name) {
mImpl->mMacroName = std::move(name);
}
template<size_t count>
static const Token *findsimplematch(const Token * const startTok, const char (&pattern)[count]) {
return findsimplematch(startTok, pattern, count-1);
}
static const Token *findsimplematch(const Token * startTok, const char pattern[], size_t pattern_len);
template<size_t count>
static const Token *findsimplematch(const Token * const startTok, const char (&pattern)[count], const Token * const end) {
return findsimplematch(startTok, pattern, count-1, end);
}
static const Token *findsimplematch(const Token * startTok, const char pattern[], size_t pattern_len, const Token * end);
static const Token *findmatch(const Token * startTok, const char pattern[], nonneg int varId = 0);
static const Token *findmatch(const Token * startTok, const char pattern[], const Token * end, nonneg int varId = 0);
template<size_t count>
static Token *findsimplematch(Token * const startTok, const char (&pattern)[count]) {
return findsimplematch(startTok, pattern, count-1);
}
static Token *findsimplematch(Token * startTok, const char pattern[], size_t pattern_len);
template<size_t count>
static Token *findsimplematch(Token * const startTok, const char (&pattern)[count], const Token * const end) {
return findsimplematch(startTok, pattern, count-1, end);
}
static Token *findsimplematch(Token * startTok, const char pattern[], size_t pattern_len, const Token * end);
static Token *findmatch(Token * startTok, const char pattern[], nonneg int varId = 0);
static Token *findmatch(Token * startTok, const char pattern[], const Token * end, nonneg int varId = 0);
private:
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
static T *tokAtImpl(T *tok, int index)
{
while (index > 0 && tok) {
tok = tok->next();
--index;
}
while (index < 0 && tok) {
tok = tok->previous();
++index;
}
return tok;
}
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
static T *linkAtImpl(T *thisTok, int index)
{
T *tok = thisTok->tokAt(index);
if (!tok) {
throw InternalError(thisTok, "Internal error. Token::linkAt called with index outside the tokens range.");
}
return tok->link();
}
/**
* Needle is build from multiple alternatives. If one of
* them is equal to haystack, return value is 1. If there
* are no matches, but one alternative to needle is empty
* string, return value is 0. If needle was not found, return
* value is -1.
*
* @param tok Current token (needle)
* @param haystack e.g. "one|two" or "|one|two"
* @param varid optional varid of token
* @return 1 if needle is found from the haystack
* 0 if needle was empty string
* -1 if needle was not found
*/
static int multiCompare(const Token *tok, const char *haystack, nonneg int varid);
public:
const std::string& fileName() const;
nonneg int fileIndex() const {
return mImpl->mFileIndex;
}
void fileIndex(nonneg int indexOfFile) {
mImpl->mFileIndex = indexOfFile;
}
nonneg int linenr() const {
return mImpl->mLineNumber;
}
void linenr(nonneg int lineNumber) {
mImpl->mLineNumber = lineNumber;
}
nonneg int column() const {
return mImpl->mColumn;
}
void column(nonneg int c) {
mImpl->mColumn = c;
}
Token* next() {
return mNext;
}
const Token* next() const {
return mNext;
}
/**
* Delete tokens between begin and end. E.g. if begin = 1
* and end = 5, tokens 2,3 and 4 would be erased.
*
* @param begin Tokens after this will be erased.
* @param end Tokens before this will be erased.
*/
static void eraseTokens(Token *begin, const Token *end);
/**
* Insert new token after this token. This function will handle
* relations between next and previous token also.
* @param tokenStr String for the new token.
* @param originalNameStr String used for Token::originalName().
* @param prepend Insert the new token before this token when it's not
* the first one on the tokens list.
*/
RET_NONNULL Token* insertToken(const std::string& tokenStr, const std::string& originalNameStr = emptyString, const std::string& macroNameStr = emptyString, bool prepend = false);
RET_NONNULL Token* insertTokenBefore(const std::string& tokenStr, const std::string& originalNameStr = emptyString, const std::string& macroNameStr = emptyString)
{
return insertToken(tokenStr, originalNameStr, macroNameStr, true);
}
Token* previous() {
return mPrevious;
}
const Token* previous() const {
return mPrevious;
}
nonneg int varId() const {
return mImpl->mVarId;
}
void varId(nonneg int id) {
mImpl->mVarId = id;
if (id != 0) {
tokType(eVariable);
isStandardType(false);
} else {
update_property_info();
}
}
nonneg int exprId() const {
if (mImpl->mExprId)
return mImpl->mExprId;
return mImpl->mVarId;
}
void exprId(nonneg int id) {
mImpl->mExprId = id;
}
void setUniqueExprId()
{
assert(mImpl->mExprId > 0);
mImpl->mExprId |= 1 << efIsUnique;
}
bool isUniqueExprId() const
{
return (mImpl->mExprId & (1 << efIsUnique)) != 0;
}
/**
* For debugging purposes, prints token and all tokens followed by it.
*/
void printOut() const;
/**
* For debugging purposes, prints token and all tokens followed by it.
* @param title Title for the printout or use default parameter or 0
* for no title.
*/
void printOut(std::ostream& out, const char *title = nullptr) const;
/**
* For debugging purposes, prints token and all tokens followed by it.
* @param xml print in XML format
* @param title Title for the printout or use default parameter or 0
* for no title.
* @param fileNames Prints out file name instead of file index.
* File index should match the index of the string in this vector.
*/
void printOut(std::ostream& out, bool xml, const char *title, const std::vector<std::string> &fileNames) const;
/**
* print out tokens - used for debugging
*/
void printLines(std::ostream& out, int lines=5) const;
/**
* Replace token replaceThis with tokens between start and end,
* including start and end. The replaceThis token is deleted.
* @param replaceThis This token will be deleted.
* @param start This will be in the place of replaceThis
* @param end This is also in the place of replaceThis
*/
static void replace(Token *replaceThis, Token *start, Token *end);
struct stringifyOptions {
bool varid = false;
bool exprid = false;
bool idtype = false; // distinguish varid / exprid
bool attributes = false;
bool macro = false;
bool linenumbers = false;
bool linebreaks = false;
bool files = false;
static stringifyOptions forDebug() {
stringifyOptions options;
options.attributes = true;
options.macro = true;
options.linenumbers = true;
options.linebreaks = true;
options.files = true;
return options;
}
// cppcheck-suppress unusedFunction
static stringifyOptions forDebugVarId() {
stringifyOptions options = forDebug();
options.varid = true;
return options;
}
static stringifyOptions forDebugExprId() {
stringifyOptions options = forDebug();
options.exprid = true;
return options;
}
static stringifyOptions forPrintOut() {
stringifyOptions options = forDebug();
options.exprid = true;
options.varid = true;
options.idtype = true;
return options;
}
};
std::string stringify(const stringifyOptions& options) const;
/**
* Stringify a token
* @param varid Print varids. (Style: "varname\@id")
* @param attributes Print attributes of tokens like "unsigned" in front of it.
* @param macro Prints $ in front of the token if it was expanded from a macro.
*/
std::string stringify(bool varid, bool attributes, bool macro) const;
std::string stringifyList(const stringifyOptions& options, const std::vector<std::string>* fileNames = nullptr, const Token* end = nullptr) const;
std::string stringifyList(const Token* end, bool attributes = true) const;
std::string stringifyList(bool varid = false) const;
/**
* Stringify a list of token, from current instance on.
* @param varid Print varids. (Style: "varname\@id")
* @param attributes Print attributes of tokens like "unsigned" in front of it.
* @param linenumbers Print line number in front of each line
* @param linebreaks Insert "\\n" into string when line number changes
* @param files print Files as numbers or as names (if fileNames is given)
* @param fileNames Vector of filenames. Used (if given) to print filenames as strings instead of numbers.
* @param end Stringification ends before this token is reached. 0 to stringify until end of list.
* @return Stringified token list as a string
*/
std::string stringifyList(bool varid, bool attributes, bool linenumbers, bool linebreaks, bool files, const std::vector<std::string>* fileNames = nullptr, const Token* end = nullptr) const;
/**
* Remove the contents for this token from the token list.
*
* The contents are replaced with the contents of the next token and
* the next token is unlinked and deleted from the token list.
*
* So this token will still be valid after the 'deleteThis()'.
*/
void deleteThis();
/**
* Create link to given token
* @param linkToToken The token where this token should link
* to.
*/
void link(Token *linkToToken) {
mLink = linkToToken;
if (mStr == "<" || mStr == ">")
update_property_info();
}
/**
* Return token where this token links to.
* Supported links are:
* "{" <-> "}"
* "(" <-> ")"
* "[" <-> "]"
*
* @return The token where this token links to.
*/
const Token* link() const {
return mLink;
}
Token* link() {
return mLink;
}
/**
* Associate this token with given scope
* @param s Scope to be associated
*/
void scope(const Scope *s) {
mImpl->mScope = s;
}
/**
* @return a pointer to the scope containing this token.
*/
const Scope *scope() const {
return mImpl->mScope;
}
/**
* Associate this token with given function
* @param f Function to be associated
*/
void function(const Function *f);
/**
* @return a pointer to the Function associated with this token.
*/
const Function *function() const {
return mTokType == eFunction || mTokType == eLambda ? mImpl->mFunction : nullptr;
}
/**
* Associate this token with given variable
* @param v Variable to be associated
*/
void variable(const Variable *v) {
mImpl->mVariable = v;
if (v || mImpl->mVarId)
tokType(eVariable);
else if (mTokType == eVariable)
tokType(eName);
}
/**
* @return a pointer to the variable associated with this token.
*/
const Variable *variable() const {
return mTokType == eVariable ? mImpl->mVariable : nullptr;
}
/**
* Associate this token with given type
* @param t Type to be associated
*/
void type(const ::Type *t);
/**
* @return a pointer to the type associated with this token.
*/
const ::Type *type() const {
return mTokType == eType ? mImpl->mType : nullptr;
}
static const ::Type* typeOf(const Token* tok, const Token** typeTok = nullptr);
/**
* @return the declaration associated with this token.
* @param pointedToType return the pointed-to type?
*/
static std::pair<const Token*, const Token*> typeDecl(const Token* tok, bool pointedToType = false);
static std::string typeStr(const Token* tok);
static bool isStandardType(const std::string& str);
/**
* @return a pointer to the Enumerator associated with this token.
*/
const Enumerator *enumerator() const {
return mTokType == eEnumerator ? mImpl->mEnumerator : nullptr;
}
/**
* Associate this token with given enumerator
* @param e Enumerator to be associated
*/
void enumerator(const Enumerator *e) {
mImpl->mEnumerator = e;
if (e)
tokType(eEnumerator);
else if (mTokType == eEnumerator)
tokType(eName);
}
/**
* Links two elements against each other.
**/
static void createMutualLinks(Token *begin, Token *end);
/**
* This can be called only for tokens that are strings, else
* the assert() is called. If Token is e.g. '"hello"', this will return
* 'hello' (removing the double quotes).
* @return String value
*/
std::string strValue() const;
/**
* Move srcStart and srcEnd tokens and all tokens between them
* into new a location. Only links between tokens are changed.
* @param srcStart This is the first token to be moved
* @param srcEnd The last token to be moved
* @param newLocation srcStart will be placed after this token.
*/
static void move(Token *srcStart, Token *srcEnd, Token *newLocation);
/** Get progressValue (0 - 100) */
nonneg int progressValue() const {
return mImpl->mProgressValue;
}
/** Calculate progress values for all tokens */
static void assignProgressValues(Token *tok);
/**
* @return the first token of the next argument. Does only work on argument
* lists. Requires that Tokenizer::createLinks2() has been called before.
* Returns nullptr, if there is no next argument.
*/
const Token* nextArgument() const;
Token *nextArgument();
/**
* @return the first token of the next argument. Does only work on argument
* lists. Should be used only before Tokenizer::createLinks2() was called.
* Returns nullptr, if there is no next argument.
*/
const Token* nextArgumentBeforeCreateLinks2() const;
/**
* @return the first token of the next template argument. Does only work on template argument
* lists. Requires that Tokenizer::createLinks2() has been called before.
* Returns nullptr, if there is no next argument.
*/
const Token* nextTemplateArgument() const;
/**
* Returns the closing bracket of opening '<'. Should only be used if link()
* is unavailable.
* @return closing '>', ')', ']' or '}'. if no closing bracket is found, NULL is returned
*/
const Token* findClosingBracket() const;
Token* findClosingBracket();
const Token* findOpeningBracket() const;
Token* findOpeningBracket();
/**
* @return the original name.
*/
const std::string & originalName() const {
return mImpl->mOriginalName ? *mImpl->mOriginalName : mEmptyString;
}
const std::list<ValueFlow::Value>& values() const {
return mImpl->mValues ? *mImpl->mValues : TokenImpl::mEmptyValueList;
}
/**
* Sets the original name.
*/
template<typename T>
void originalName(T&& name) {
if (!mImpl->mOriginalName)
mImpl->mOriginalName = new std::string(name);
else
*mImpl->mOriginalName = name;
}
bool hasKnownIntValue() const;
bool hasKnownValue() const;
bool hasKnownValue(ValueFlow::Value::ValueType t) const;
bool hasKnownSymbolicValue(const Token* tok) const;
const ValueFlow::Value* getKnownValue(ValueFlow::Value::ValueType t) const;
MathLib::bigint getKnownIntValue() const {
return mImpl->mValues->front().intvalue;
}
const ValueFlow::Value* getValue(MathLib::bigint val) const;
const ValueFlow::Value* getMaxValue(bool condition, MathLib::bigint path = 0) const;
const ValueFlow::Value* getMinValue(bool condition, MathLib::bigint path = 0) const;
const ValueFlow::Value* getMovedValue() const;
const ValueFlow::Value * getValueLE(MathLib::bigint val, const Settings &settings) const;
const ValueFlow::Value * getValueGE(MathLib::bigint val, const Settings &settings) const;
const ValueFlow::Value * getValueNE(MathLib::bigint val) const;
const ValueFlow::Value * getInvalidValue(const Token *ftok, nonneg int argnr, const Settings &settings) const;
const ValueFlow::Value* getContainerSizeValue(MathLib::bigint val) const;
const Token *getValueTokenMaxStrLength() const;
const Token *getValueTokenMinStrSize(const Settings &settings, MathLib::bigint* path = nullptr) const;
/** Add token value. Return true if value is added. */
bool addValue(const ValueFlow::Value &value);
void removeValues(std::function<bool(const ValueFlow::Value &)> pred) {
if (mImpl->mValues)
mImpl->mValues->remove_if(std::move(pred));
}
nonneg int index() const {
return mImpl->mIndex;
}
void assignIndexes();
private:
void next(Token *nextToken) {
mNext = nextToken;
}
void previous(Token *previousToken) {
mPrevious = previousToken;
}
/** used by deleteThis() to take data from token to delete */
void takeData(Token *fromToken);
/**
* Works almost like strcmp() except returns only true or false and
* if str has empty space ' ' character, that character is handled
* as if it were '\\0'
*/
static bool firstWordEquals(const char *str, const char *word);
/**
* Works almost like strchr() except
* if str has empty space ' ' character, that character is handled
* as if it were '\\0'
*/
static const char *chrInFirstWord(const char *str, char c);
std::string mStr;
Token* mNext{};
Token* mPrevious{};
Token* mLink{};
enum : uint64_t {
fIsUnsigned = (1ULL << 0),
fIsSigned = (1ULL << 1),
fIsPointerCompare = (1ULL << 2),
fIsLong = (1ULL << 3),
fIsStandardType = (1ULL << 4),
//fIsExpandedMacro = (1ULL << 5),
fIsCast = (1ULL << 6),
fIsAttributeConstructor = (1ULL << 7), // __attribute__((constructor)) __attribute__((constructor(priority)))
fIsAttributeDestructor = (1ULL << 8), // __attribute__((destructor)) __attribute__((destructor(priority)))
fIsAttributeUnused = (1ULL << 9), // __attribute__((unused))
fIsAttributePure = (1ULL << 10), // __attribute__((pure))
fIsAttributeConst = (1ULL << 11), // __attribute__((const))
fIsAttributeNoreturn = (1ULL << 12), // __attribute__((noreturn)), __declspec(noreturn)
fIsAttributeNothrow = (1ULL << 13), // __attribute__((nothrow)), __declspec(nothrow)
fIsAttributeUsed = (1ULL << 14), // __attribute__((used))
fIsAttributePacked = (1ULL << 15), // __attribute__((packed))
fIsAttributeExport = (1ULL << 16), // __attribute__((__visibility__("default"))), __declspec(dllexport)
fIsAttributeMaybeUnused = (1ULL << 17), // [[maybe_unused]]
fIsAttributeNodiscard = (1ULL << 18), // __attribute__ ((warn_unused_result)), [[nodiscard]]
fIsControlFlowKeyword = (1ULL << 19), // if/switch/while/...
fIsOperatorKeyword = (1ULL << 20), // operator=, etc
fIsComplex = (1ULL << 21), // complex/_Complex type
fIsEnumType = (1ULL << 22), // enumeration type
fIsName = (1ULL << 23),
fIsLiteral = (1ULL << 24),
fIsTemplateArg = (1ULL << 25),
fAtAddress = (1ULL << 26), // @ 0x4000
fIncompleteVar = (1ULL << 27),
fConstexpr = (1ULL << 28),
fExternC = (1ULL << 29),
fIsSplitVarDeclComma = (1ULL << 30), // set to true when variable declarations are split up ('int a,b;' => 'int a; int b;')
fIsSplitVarDeclEq = (1ULL << 31), // set to true when variable declaration with initialization is split up ('int a=5;' => 'int a; a=5;')
fIsImplicitInt = (1ULL << 32), // Is "int" token implicitly added?
fIsInline = (1ULL << 33), // Is this a inline type
fIsTemplate = (1ULL << 34),
fIsSimplifedScope = (1ULL << 35), // scope added when simplifying e.g. if (int i = ...; ...)
fIsRemovedVoidParameter = (1ULL << 36), // A void function parameter has been removed
fIsIncompleteConstant = (1ULL << 37),
fIsRestrict = (1ULL << 38), // Is this a restrict pointer type
fIsAtomic = (1ULL << 39), // Is this a _Atomic declaration
fIsSimplifiedTypedef = (1ULL << 40),
fIsFinalType = (1ULL << 41), // Is this a type with final specifier
fIsInitComma = (1ULL << 42), // Is this comma located inside some {..}. i.e: {1,2,3,4}
fIsInitBracket = (1ULL << 43), // Is this bracket used as a part of variable initialization i.e: int a{5}, b(2);
};
enum : std::uint8_t {
efMaxSize = sizeof(nonneg int) * 8,
efIsUnique = efMaxSize - 2,
};
Token::Type mTokType = eNone;
uint64_t mFlags{};
TokenImpl* mImpl{};
/**
* Get specified flag state.
* @param flag_ flag to get state of
* @return true if flag set or false in flag not set
*/
bool getFlag(uint64_t flag_) const {
return ((mFlags & flag_) != 0);
}
/**
* Set specified flag state.
* @param flag_ flag to set state
* @param state_ new state of flag
*/
void setFlag(uint64_t flag_, bool state_) {
mFlags = state_ ? mFlags | flag_ : mFlags & ~flag_;
}
/** Updates internal property cache like _isName or _isBoolean.
Called after any mStr() modification. */
void update_property_info();
/** Update internal property cache about isStandardType() */
void update_property_isStandardType();
/** Internal helper function to avoid excessive string allocations */
void astStringVerboseRecursive(std::string& ret, nonneg int indent1 = 0, nonneg int indent2 = 0) const;
// cppcheck-suppress premium-misra-cpp-2023-12.2.1
bool mIsC : 1;
bool mIsCpp : 1;
public:
void astOperand1(Token *tok);
void astOperand2(Token *tok);
void astParent(Token* tok);
Token * astOperand1() {
return mImpl->mAstOperand1;
}
const Token * astOperand1() const {
return mImpl->mAstOperand1;
}
Token * astOperand2() {
return mImpl->mAstOperand2;
}
const Token * astOperand2() const {
return mImpl->mAstOperand2;
}
Token * astParent() {
return mImpl->mAstParent;
}
const Token * astParent() const {
return mImpl->mAstParent;
}
Token * astSibling() {
if (!astParent())
return nullptr;
if (this == astParent()->astOperand1())
return astParent()->astOperand2();
if (this == astParent()->astOperand2())
return astParent()->astOperand1();
return nullptr;
}
const Token * astSibling() const {
if (!astParent())
return nullptr;
if (this == astParent()->astOperand1())
return astParent()->astOperand2();
if (this == astParent()->astOperand2())
return astParent()->astOperand1();
return nullptr;
}
RET_NONNULL Token *astTop() {
Token *ret = this;
while (ret->mImpl->mAstParent)
ret = ret->mImpl->mAstParent;
return ret;
}
RET_NONNULL const Token *astTop() const {
const Token *ret = this;
while (ret->mImpl->mAstParent)
ret = ret->mImpl->mAstParent;
return ret;
}
std::pair<const Token *, const Token *> findExpressionStartEndTokens() const;
/**
* Is current token a calculation? Only true for operands.
* For '*' and '&' tokens it is looked up if this is a
* dereference or address-of. A dereference or address-of is not
* counted as a calculation.
* @return returns true if current token is a calculation
*/
bool isCalculation() const;
void clearValueFlow() {
delete mImpl->mValues;
mImpl->mValues = nullptr;
}
std::string astString(const char *sep = "") const {
std::string ret;
if (mImpl->mAstOperand1)
ret = mImpl->mAstOperand1->astString(sep);
if (mImpl->mAstOperand2)
ret += mImpl->mAstOperand2->astString(sep);
return ret + sep + mStr;
}
std::string astStringVerbose() const;
std::string astStringZ3() const;
std::string expressionString() const;
void printAst(bool verbose, bool xml, const std::vector<std::string> &fileNames, std::ostream &out) const;
void printValueFlow(bool xml, std::ostream &out) const;
void scopeInfo(std::shared_ptr<ScopeInfo2> newScopeInfo);
std::shared_ptr<ScopeInfo2> scopeInfo() const;
void setCpp11init(bool cpp11init) const {
mImpl->mCpp11init=cpp11init ? TokenImpl::Cpp11init::CPP11INIT : TokenImpl::Cpp11init::NOINIT;
}
TokenImpl::Cpp11init isCpp11init() const {
return mImpl->mCpp11init;
}
TokenDebug getTokenDebug() const {
return mImpl->mDebug;
}
void setTokenDebug(TokenDebug td) {
mImpl->mDebug = td;
}
bool isCpp() const
{
return mIsCpp;
}
bool isC() const
{
return mIsC;
}
};
Token* findTypeEnd(Token* tok);
Token* findLambdaEndScope(Token* tok);
const Token* findLambdaEndScope(const Token* tok);
/// @}
//---------------------------------------------------------------------------
#endif // tokenH
|