Skip to content

Commit 5f34d3e

Browse files
committed
Token: do not rely on TokenImpl in interface
1 parent 3e169d6 commit 5f34d3e

7 files changed

Lines changed: 55 additions & 51 deletions

File tree

lib/checkleakautovar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
396396
}
397397
}
398398

399-
if (tok->isCpp11init() == TokenImpl::Cpp11init::CPP11INIT) {
399+
if (tok->isCpp11init() == Cpp11init::CPP11INIT) {
400400
const Token *newTok = tok->astOperand1();
401401
const Token *oldTok = tok->astOperand2();
402402
if (newTok && newTok->varId() && oldTok && oldTok->varId()) {

lib/token.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace {
5858
};
5959
}
6060

61-
const std::list<ValueFlow::Value> TokenImpl::mEmptyValueList;
61+
const std::list<ValueFlow::Value> Token::mEmptyValueList;
6262
const std::string Token::mEmptyString;
6363

6464
Token::Token(const TokenList& tokenlist, std::shared_ptr<TokensFrontBack> tokensFrontBack)
@@ -2663,7 +2663,7 @@ TokenImpl::~TokenImpl()
26632663
}
26642664
}
26652665

2666-
void TokenImpl::setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type type, MathLib::bigint value)
2666+
void TokenImpl::setCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint value)
26672667
{
26682668
CppcheckAttributes *attr = mCppcheckAttributes;
26692669
while (attr && attr->type != type)
@@ -2679,7 +2679,7 @@ void TokenImpl::setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type type, M
26792679
}
26802680
}
26812681

2682-
bool TokenImpl::getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type type, MathLib::bigint &value) const
2682+
bool TokenImpl::getCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint &value) const
26832683
{
26842684
CppcheckAttributes *attr = mCppcheckAttributes;
26852685
while (attr && attr->type != type)

lib/token.h

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ struct ScopeInfo2 {
6363

6464
enum class TokenDebug : std::uint8_t { None, ValueFlow, ValueType };
6565

66+
// TODO: move these to Token when TokenImpl has been moved into the implementation
67+
enum CppcheckAttributesType : std::uint8_t { LOW, HIGH };
68+
struct CppcheckAttributes {
69+
CppcheckAttributesType type{LOW};
70+
MathLib::bigint value{};
71+
CppcheckAttributes* next{};
72+
};
73+
enum class Cpp11init : std::uint8_t { UNKNOWN, CPP11INIT, NOINIT };
74+
6675
struct TokenImpl {
6776
nonneg int mVarId{};
6877
nonneg int mFileIndex{};
@@ -114,7 +123,6 @@ struct TokenImpl {
114123

115124
// ValueFlow
116125
std::list<ValueFlow::Value>* mValues{};
117-
static const std::list<ValueFlow::Value> mEmptyValueList;
118126

119127
// Pointer to a template in the template simplifier
120128
std::set<TemplateSimplifier::TokenAndName*>* mTemplateSimplifierPointers{};
@@ -123,11 +131,6 @@ struct TokenImpl {
123131
std::shared_ptr<ScopeInfo2> mScopeInfo;
124132

125133
// __cppcheck_in_range__
126-
struct CppcheckAttributes {
127-
enum Type : std::uint8_t { LOW, HIGH } type = LOW;
128-
MathLib::bigint value{};
129-
CppcheckAttributes* next{};
130-
};
131134
CppcheckAttributes* mCppcheckAttributes{};
132135

133136
// alignas expressions
@@ -142,12 +145,12 @@ struct TokenImpl {
142145
std::string mAttributeCleanup;
143146

144147
// For memoization, to speed up parsing of huge arrays #8897
145-
enum class Cpp11init : std::uint8_t { UNKNOWN, CPP11INIT, NOINIT } mCpp11init = Cpp11init::UNKNOWN;
148+
Cpp11init mCpp11init{Cpp11init::UNKNOWN};
146149

147150
TokenDebug mDebug{};
148151

149-
void setCppcheckAttribute(CppcheckAttributes::Type type, MathLib::bigint value);
150-
bool getCppcheckAttribute(CppcheckAttributes::Type type, MathLib::bigint &value) const;
152+
void setCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint value);
153+
bool getCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint &value) const;
151154

152155
TokenImpl() : mFunction(nullptr) {}
153156

@@ -594,10 +597,10 @@ class CPPCHECKLIB Token {
594597
bool hasAttributeCleanup() const {
595598
return !mImpl->mAttributeCleanup.empty();
596599
}
597-
void setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type type, MathLib::bigint value) {
600+
void setCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint value) {
598601
mImpl->setCppcheckAttribute(type, value);
599602
}
600-
bool getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type type, MathLib::bigint &value) const {
603+
bool getCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint &value) const {
601604
return mImpl->getCppcheckAttribute(type, value);
602605
}
603606
// cppcheck-suppress unusedFunction
@@ -1346,7 +1349,7 @@ class CPPCHECKLIB Token {
13461349
}
13471350

13481351
const std::list<ValueFlow::Value>& values() const {
1349-
return mImpl->mValues ? *mImpl->mValues : TokenImpl::mEmptyValueList;
1352+
return mImpl->mValues ? *mImpl->mValues : mEmptyValueList;
13501353
}
13511354

13521355
/**
@@ -1406,6 +1409,7 @@ class CPPCHECKLIB Token {
14061409
void assignIndexes();
14071410

14081411
private:
1412+
static const std::list<ValueFlow::Value> mEmptyValueList;
14091413

14101414
void next(Token *nextToken) {
14111415
mNext = nextToken;
@@ -1630,9 +1634,9 @@ class CPPCHECKLIB Token {
16301634
std::shared_ptr<ScopeInfo2> scopeInfo() const;
16311635

16321636
void setCpp11init(bool cpp11init) const {
1633-
mImpl->mCpp11init=cpp11init ? TokenImpl::Cpp11init::CPP11INIT : TokenImpl::Cpp11init::NOINIT;
1637+
mImpl->mCpp11init=cpp11init ? Cpp11init::CPP11INIT : Cpp11init::NOINIT;
16341638
}
1635-
TokenImpl::Cpp11init isCpp11init() const {
1639+
Cpp11init isCpp11init() const {
16361640
return mImpl->mCpp11init;
16371641
}
16381642

lib/tokenize.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9471,10 +9471,10 @@ void Tokenizer::simplifyCppcheckAttribute()
94719471

94729472
if (vartok->isName()) {
94739473
if (Token::Match(tok->previous(), "__cppcheck_low__ ( %num% )"))
9474-
vartok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::LOW,
9474+
vartok->setCppcheckAttribute(CppcheckAttributesType::LOW,
94759475
MathLib::toBigNumber(tok->tokAt(1)));
94769476
else if (Token::Match(tok->previous(), "__cppcheck_high__ ( %num% )"))
9477-
vartok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::HIGH,
9477+
vartok->setCppcheckAttribute(CppcheckAttributesType::HIGH,
94789478
MathLib::toBigNumber(tok->tokAt(1)));
94799479
}
94809480

@@ -9562,16 +9562,16 @@ void Tokenizer::simplifyCPPAttribute()
95629562
}
95639563
if (argtok && argtok->str() == vartok->str()) {
95649564
if (vartok->strAt(1) == ">=")
9565-
argtok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::LOW,
9565+
argtok->setCppcheckAttribute(CppcheckAttributesType::LOW,
95669566
MathLib::toBigNumber(vartok->tokAt(2)));
95679567
else if (vartok->strAt(1) == ">")
9568-
argtok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::LOW,
9568+
argtok->setCppcheckAttribute(CppcheckAttributesType::LOW,
95699569
MathLib::toBigNumber(vartok->tokAt(2)) + 1);
95709570
else if (vartok->strAt(1) == "<=")
9571-
argtok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::HIGH,
9571+
argtok->setCppcheckAttribute(CppcheckAttributesType::HIGH,
95729572
MathLib::toBigNumber(vartok->tokAt(2)));
95739573
else if (vartok->strAt(1) == "<")
9574-
argtok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::HIGH,
9574+
argtok->setCppcheckAttribute(CppcheckAttributesType::HIGH,
95759575
MathLib::toBigNumber(vartok->tokAt(2)) - 1);
95769576
}
95779577
}

lib/tokenlist.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,9 @@ static Token * findCppTypeInitPar(Token *tok)
559559
static bool iscpp11init_impl(const Token * tok);
560560
static bool iscpp11init(const Token * const tok)
561561
{
562-
if (tok->isCpp11init() == TokenImpl::Cpp11init::UNKNOWN)
562+
if (tok->isCpp11init() == Cpp11init::UNKNOWN)
563563
tok->setCpp11init(iscpp11init_impl(tok));
564-
return tok->isCpp11init() == TokenImpl::Cpp11init::CPP11INIT;
564+
return tok->isCpp11init() == Cpp11init::CPP11INIT;
565565
}
566566

567567
static bool iscpp11init_impl(const Token * const tok)
@@ -570,8 +570,8 @@ static bool iscpp11init_impl(const Token * const tok)
570570
return false;
571571
const Token *nameToken = tok;
572572
while (nameToken && nameToken->str() == "{") {
573-
if (nameToken->isCpp11init() != TokenImpl::Cpp11init::UNKNOWN)
574-
return nameToken->isCpp11init() == TokenImpl::Cpp11init::CPP11INIT;
573+
if (nameToken->isCpp11init() != Cpp11init::UNKNOWN)
574+
return nameToken->isCpp11init() == Cpp11init::CPP11INIT;
575575
nameToken = nameToken->previous();
576576
if (nameToken && nameToken->str() == "," && Token::simpleMatch(nameToken->previous(), "} ,"))
577577
nameToken = nameToken->linkAt(-1);

lib/valueflow.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7160,8 +7160,8 @@ static void valueFlowSafeFunctions(const TokenList& tokenlist, const SymbolDatab
71607160
}
71617161

71627162
MathLib::bigint low, high;
7163-
bool isLow = arg.nameToken()->getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::LOW, low);
7164-
bool isHigh = arg.nameToken()->getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::HIGH, high);
7163+
bool isLow = arg.nameToken()->getCppcheckAttribute(CppcheckAttributesType::LOW, low);
7164+
bool isHigh = arg.nameToken()->getCppcheckAttribute(CppcheckAttributesType::HIGH, high);
71657165

71667166
if (!isLow && !isHigh && !all)
71677167
continue;

test/testtokenize.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8201,7 +8201,7 @@ class TestTokenizer : public TestFixture {
82018201

82028202
void cpp11init() {
82038203
#define testIsCpp11init(...) testIsCpp11init_(__FILE__, __LINE__, __VA_ARGS__)
8204-
auto testIsCpp11init_ = [this](const char* file, int line, const char* code, const char* find, TokenImpl::Cpp11init expected) {
8204+
auto testIsCpp11init_ = [this](const char* file, int line, const char* code, const char* find, Cpp11init expected) {
82058205
SimpleTokenizer tokenizer(settingsDefault, *this);
82068206
ASSERT_LOC(tokenizer.tokenize(code), file, line);
82078207

@@ -8212,60 +8212,60 @@ class TestTokenizer : public TestFixture {
82128212

82138213
testIsCpp11init("class X : public A<int>, C::D {};",
82148214
"D {",
8215-
TokenImpl::Cpp11init::NOINIT);
8215+
Cpp11init::NOINIT);
82168216

82178217
testIsCpp11init("auto f() -> void {}",
82188218
"void {",
8219-
TokenImpl::Cpp11init::NOINIT);
8219+
Cpp11init::NOINIT);
82208220
testIsCpp11init("auto f() & -> void {}",
82218221
"void {",
8222-
TokenImpl::Cpp11init::NOINIT);
8222+
Cpp11init::NOINIT);
82238223
testIsCpp11init("auto f() const noexcept(false) -> void {}",
82248224
"void {",
8225-
TokenImpl::Cpp11init::NOINIT);
8225+
Cpp11init::NOINIT);
82268226
testIsCpp11init("auto f() -> std::vector<int> { return {}; }",
82278227
"{ return",
8228-
TokenImpl::Cpp11init::NOINIT);
8228+
Cpp11init::NOINIT);
82298229
testIsCpp11init("auto f() -> std::vector<int> { return {}; }",
82308230
"vector",
8231-
TokenImpl::Cpp11init::NOINIT);
8231+
Cpp11init::NOINIT);
82328232
testIsCpp11init("auto f() -> std::vector<int> { return {}; }",
82338233
"std ::",
8234-
TokenImpl::Cpp11init::NOINIT);
8234+
Cpp11init::NOINIT);
82358235

82368236
testIsCpp11init("class X{};",
82378237
"{ }",
8238-
TokenImpl::Cpp11init::NOINIT);
8238+
Cpp11init::NOINIT);
82398239
testIsCpp11init("class X{}", // forgotten ; so not properly recognized as a class
82408240
"{ }",
8241-
TokenImpl::Cpp11init::CPP11INIT);
8241+
Cpp11init::CPP11INIT);
82428242

82438243
testIsCpp11init("namespace abc::def { TEST(a, b) {} }",
82448244
"{ TEST",
8245-
TokenImpl::Cpp11init::NOINIT);
8245+
Cpp11init::NOINIT);
82468246
testIsCpp11init("namespace { TEST(a, b) {} }", // anonymous namespace
82478247
"{ TEST",
8248-
TokenImpl::Cpp11init::NOINIT);
8248+
Cpp11init::NOINIT);
82498249

82508250
testIsCpp11init("enum { e = decltype(s)::i };",
82518251
"{ e",
8252-
TokenImpl::Cpp11init::NOINIT);
8252+
Cpp11init::NOINIT);
82538253

82548254
testIsCpp11init("template <typename T>\n" // #11378
82558255
"class D<M<T, 1>> : public B<M<T, 1>, T> {\n"
82568256
"public:\n"
82578257
" D(int x) : B<M<T, 1>, T>(x) {}\n"
82588258
"};\n",
82598259
"{ public:",
8260-
TokenImpl::Cpp11init::NOINIT);
8260+
Cpp11init::NOINIT);
82618261

82628262
testIsCpp11init("template <typename T>\n"
82638263
"class D<M<T, 1>> : B<M<T, 1>, T> {\n"
82648264
"public:\n"
82658265
" D(int x) : B<M<T, 1>, T>(x) {}\n"
82668266
"};\n",
82678267
"{ public:",
8268-
TokenImpl::Cpp11init::NOINIT);
8268+
Cpp11init::NOINIT);
82698269

82708270
testIsCpp11init("using namespace std;\n"
82718271
"namespace internal {\n"
@@ -8275,21 +8275,21 @@ class TestTokenizer : public TestFixture {
82758275
" S::S() {}\n"
82768276
"}\n",
82778277
"{ } }",
8278-
TokenImpl::Cpp11init::NOINIT);
8278+
Cpp11init::NOINIT);
82798279

82808280
testIsCpp11init("template <std::size_t N>\n"
82818281
"struct C : public C<N - 1>, public B {\n"
82828282
" ~C() {}\n"
82838283
"};\n",
82848284
"{ } }",
8285-
TokenImpl::Cpp11init::NOINIT);
8285+
Cpp11init::NOINIT);
82868286

82878287
testIsCpp11init("struct S { int i; } s;\n"
82888288
"struct T : decltype (s) {\n"
82898289
" T() : decltype(s) ({ 0 }) { }\n"
82908290
"};\n",
82918291
"{ } }",
8292-
TokenImpl::Cpp11init::NOINIT);
8292+
Cpp11init::NOINIT);
82938293

82948294
testIsCpp11init("struct S {};\n"
82958295
"template<class... Args>\n"
@@ -8299,21 +8299,21 @@ class TestTokenizer : public TestFixture {
82998299
" void operator()(Args...) {}\n"
83008300
"};\n",
83018301
"{ void",
8302-
TokenImpl::Cpp11init::NOINIT);
8302+
Cpp11init::NOINIT);
83038303

83048304
testIsCpp11init("struct S {\n"
83058305
" std::uint8_t* p;\n"
83068306
" S() : p{ new std::uint8_t[1]{} } {}\n"
83078307
"};\n",
83088308
"{ } } {",
8309-
TokenImpl::Cpp11init::CPP11INIT);
8309+
Cpp11init::CPP11INIT);
83108310

83118311
testIsCpp11init("struct S {\n"
83128312
" S() : p{new (malloc(4)) int{}} {}\n"
83138313
" int* p;\n"
83148314
"};\n",
83158315
"{ } } {",
8316-
TokenImpl::Cpp11init::CPP11INIT);
8316+
Cpp11init::CPP11INIT);
83178317

83188318
ASSERT_NO_THROW(tokenizeAndStringify("template<typename U> struct X {};\n" // don't crash
83198319
"template<typename T> auto f(T t) -> X<decltype(t + 1)> {}\n"));

0 commit comments

Comments
 (0)