Skip to content

Commit 00d32fe

Browse files
committed
Token: do not rely on TokenImpl in interface
1 parent f916df0 commit 00d32fe

7 files changed

Lines changed: 51 additions & 47 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: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ 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+
enum class Cpp11init : std::uint8_t { UNKNOWN, CPP11INIT, NOINIT };
69+
6670
struct TokenImpl {
6771
nonneg int mVarId{};
6872
nonneg int mFileIndex{};
@@ -114,7 +118,6 @@ struct TokenImpl {
114118

115119
// ValueFlow
116120
std::list<ValueFlow::Value>* mValues{};
117-
static const std::list<ValueFlow::Value> mEmptyValueList;
118121

119122
// Pointer to a template in the template simplifier
120123
std::set<TemplateSimplifier::TokenAndName*>* mTemplateSimplifierPointers{};
@@ -124,7 +127,7 @@ struct TokenImpl {
124127

125128
// __cppcheck_in_range__
126129
struct CppcheckAttributes {
127-
enum Type : std::uint8_t { LOW, HIGH } type = LOW;
130+
CppcheckAttributesType type{LOW};
128131
MathLib::bigint value{};
129132
CppcheckAttributes* next{};
130133
};
@@ -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
@@ -7158,8 +7158,8 @@ static void valueFlowSafeFunctions(const TokenList& tokenlist, const SymbolDatab
71587158
}
71597159

71607160
MathLib::bigint low, high;
7161-
bool isLow = arg.nameToken()->getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::LOW, low);
7162-
bool isHigh = arg.nameToken()->getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::HIGH, high);
7161+
bool isLow = arg.nameToken()->getCppcheckAttribute(CppcheckAttributesType::LOW, low);
7162+
bool isHigh = arg.nameToken()->getCppcheckAttribute(CppcheckAttributesType::HIGH, high);
71637163

71647164
if (!isLow && !isHigh && !all)
71657165
continue;

test/testtokenize.cpp

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

82048204
void cpp11init() {
82058205
#define testIsCpp11init(...) testIsCpp11init_(__FILE__, __LINE__, __VA_ARGS__)
8206-
auto testIsCpp11init_ = [this](const char* file, int line, const std::string& code, const char* find, TokenImpl::Cpp11init expected) {
8206+
auto testIsCpp11init_ = [this](const char* file, int line, const std::string& code, const char* find, Cpp11init expected) {
82078207
SimpleTokenizer tokenizer(settingsDefault, *this);
82088208
ASSERT_LOC(tokenizer.tokenize(code.data(), code.size()), file, line);
82098209

@@ -8214,60 +8214,60 @@ class TestTokenizer : public TestFixture {
82148214

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

82198219
testIsCpp11init("auto f() -> void {}",
82208220
"void {",
8221-
TokenImpl::Cpp11init::NOINIT);
8221+
Cpp11init::NOINIT);
82228222
testIsCpp11init("auto f() & -> void {}",
82238223
"void {",
8224-
TokenImpl::Cpp11init::NOINIT);
8224+
Cpp11init::NOINIT);
82258225
testIsCpp11init("auto f() const noexcept(false) -> void {}",
82268226
"void {",
8227-
TokenImpl::Cpp11init::NOINIT);
8227+
Cpp11init::NOINIT);
82288228
testIsCpp11init("auto f() -> std::vector<int> { return {}; }",
82298229
"{ return",
8230-
TokenImpl::Cpp11init::NOINIT);
8230+
Cpp11init::NOINIT);
82318231
testIsCpp11init("auto f() -> std::vector<int> { return {}; }",
82328232
"vector",
8233-
TokenImpl::Cpp11init::NOINIT);
8233+
Cpp11init::NOINIT);
82348234
testIsCpp11init("auto f() -> std::vector<int> { return {}; }",
82358235
"std ::",
8236-
TokenImpl::Cpp11init::NOINIT);
8236+
Cpp11init::NOINIT);
82378237

82388238
testIsCpp11init("class X{};",
82398239
"{ }",
8240-
TokenImpl::Cpp11init::NOINIT);
8240+
Cpp11init::NOINIT);
82418241
testIsCpp11init("class X{}", // forgotten ; so not properly recognized as a class
82428242
"{ }",
8243-
TokenImpl::Cpp11init::CPP11INIT);
8243+
Cpp11init::CPP11INIT);
82448244

82458245
testIsCpp11init("namespace abc::def { TEST(a, b) {} }",
82468246
"{ TEST",
8247-
TokenImpl::Cpp11init::NOINIT);
8247+
Cpp11init::NOINIT);
82488248
testIsCpp11init("namespace { TEST(a, b) {} }", // anonymous namespace
82498249
"{ TEST",
8250-
TokenImpl::Cpp11init::NOINIT);
8250+
Cpp11init::NOINIT);
82518251

82528252
testIsCpp11init("enum { e = decltype(s)::i };",
82538253
"{ e",
8254-
TokenImpl::Cpp11init::NOINIT);
8254+
Cpp11init::NOINIT);
82558255

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

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

82728272
testIsCpp11init("using namespace std;\n"
82738273
"namespace internal {\n"
@@ -8277,21 +8277,21 @@ class TestTokenizer : public TestFixture {
82778277
" S::S() {}\n"
82788278
"}\n",
82798279
"{ } }",
8280-
TokenImpl::Cpp11init::NOINIT);
8280+
Cpp11init::NOINIT);
82818281

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

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

82968296
testIsCpp11init("struct S {};\n"
82978297
"template<class... Args>\n"
@@ -8301,21 +8301,21 @@ class TestTokenizer : public TestFixture {
83018301
" void operator()(Args...) {}\n"
83028302
"};\n",
83038303
"{ void",
8304-
TokenImpl::Cpp11init::NOINIT);
8304+
Cpp11init::NOINIT);
83058305

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

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

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

0 commit comments

Comments
 (0)