Skip to content

Commit c3136db

Browse files
authored
Refactor: The code to generate id string from pointer can be simplified (#5382)
1 parent 85332b2 commit c3136db

5 files changed

Lines changed: 62 additions & 125 deletions

File tree

lib/symboldatabase.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4043,7 +4043,7 @@ void SymbolDatabase::printXml(std::ostream &out) const
40434043
for (std::list<Scope>::const_iterator scope = scopeList.cbegin(); scope != scopeList.cend(); ++scope) {
40444044
outs += " <scope";
40454045
outs += " id=\"";
4046-
outs += ptr_to_string(&*scope);
4046+
outs += id_string(&*scope);
40474047
outs += "\"";
40484048
outs += " type=\"";
40494049
outs += scopeTypeToString(scope->type);
@@ -4055,27 +4055,27 @@ void SymbolDatabase::printXml(std::ostream &out) const
40554055
}
40564056
if (scope->bodyStart) {
40574057
outs += " bodyStart=\"";
4058-
outs += ptr_to_string(scope->bodyStart);
4058+
outs += id_string(scope->bodyStart);
40594059
outs += '\"';
40604060
}
40614061
if (scope->bodyEnd) {
40624062
outs += " bodyEnd=\"";
4063-
outs += ptr_to_string(scope->bodyEnd);
4063+
outs += id_string(scope->bodyEnd);
40644064
outs += '\"';
40654065
}
40664066
if (scope->nestedIn) {
40674067
outs += " nestedIn=\"";
4068-
outs += ptr_to_string(scope->nestedIn);
4068+
outs += id_string(scope->nestedIn);
40694069
outs += "\"";
40704070
}
40714071
if (scope->function) {
40724072
outs += " function=\"";
4073-
outs += ptr_to_string(scope->function);
4073+
outs += id_string(scope->function);
40744074
outs += "\"";
40754075
}
40764076
if (scope->definedType) {
40774077
outs += " definedType=\"";
4078-
outs += ptr_to_string(scope->definedType);
4078+
outs += id_string(scope->definedType);
40794079
outs += "\"";
40804080
}
40814081
if (scope->functionList.empty() && scope->varlist.empty())
@@ -4086,11 +4086,11 @@ void SymbolDatabase::printXml(std::ostream &out) const
40864086
outs += " <functionList>\n";
40874087
for (std::list<Function>::const_iterator function = scope->functionList.cbegin(); function != scope->functionList.cend(); ++function) {
40884088
outs += " <function id=\"";
4089-
outs += ptr_to_string(&*function);
4089+
outs += id_string(&*function);
40904090
outs += "\" token=\"";
4091-
outs += ptr_to_string(function->token);
4091+
outs += id_string(function->token);
40924092
outs += "\" tokenDef=\"";
4093-
outs += ptr_to_string(function->tokenDef);
4093+
outs += id_string(function->tokenDef);
40944094
outs += "\" name=\"";
40954095
outs += ErrorLogger::toxml(function->name());
40964096
outs += '\"';
@@ -4123,7 +4123,7 @@ void SymbolDatabase::printXml(std::ostream &out) const
41234123
outs += " isAttributeNoreturn=\"true\"";
41244124
if (const Function* overriddenFunction = function->getOverriddenFunction()) {
41254125
outs += " overriddenFunction=\"";
4126-
outs += ptr_to_string(overriddenFunction);
4126+
outs += id_string(overriddenFunction);
41274127
outs += "\"";
41284128
}
41294129
if (function->argCount() == 0U)
@@ -4135,7 +4135,7 @@ void SymbolDatabase::printXml(std::ostream &out) const
41354135
outs += " <arg nr=\"";
41364136
outs += std::to_string(argnr+1);
41374137
outs += "\" variable=\"";
4138-
outs += ptr_to_string(arg);
4138+
outs += id_string(arg);
41394139
outs += "\"/>\n";
41404140
variables.insert(arg);
41414141
}
@@ -4148,7 +4148,7 @@ void SymbolDatabase::printXml(std::ostream &out) const
41484148
outs += " <varlist>\n";
41494149
for (std::list<Variable>::const_iterator var = scope->varlist.cbegin(); var != scope->varlist.cend(); ++var) {
41504150
outs += " <var id=\"";
4151-
outs += ptr_to_string(&*var);
4151+
outs += id_string(&*var);
41524152
outs += "\"/>\n";
41534153
}
41544154
outs += " </varlist>\n";
@@ -4162,9 +4162,9 @@ void SymbolDatabase::printXml(std::ostream &out) const
41624162
outs += " <types>\n";
41634163
for (const Type& type:typeList) {
41644164
outs += " <type id=\"";
4165-
outs += ptr_to_string(&type);
4165+
outs += id_string(&type);
41664166
outs += "\" classScope=\"";
4167-
outs += ptr_to_string(type.classScope);
4167+
outs += id_string(type.classScope);
41684168
outs += "\"";
41694169
if (type.derivedFrom.empty()) {
41704170
outs += "/>\n";
@@ -4177,13 +4177,13 @@ void SymbolDatabase::printXml(std::ostream &out) const
41774177
outs += accessControlToString(baseInfo.access);
41784178
outs += "\"";
41794179
outs += " type=\"";
4180-
outs += ptr_to_string(baseInfo.type);
4180+
outs += id_string(baseInfo.type);
41814181
outs += "\"";
41824182
outs += " isVirtual=\"";
41834183
outs += bool_to_string(baseInfo.isVirtual);
41844184
outs += "\"";
41854185
outs += " nameTok=\"";
4186-
outs += ptr_to_string(baseInfo.nameTok);
4186+
outs += id_string(baseInfo.nameTok);
41874187
outs += "\"";
41884188
outs += "/>\n";
41894189
}
@@ -4200,22 +4200,22 @@ void SymbolDatabase::printXml(std::ostream &out) const
42004200
if (!var)
42014201
continue;
42024202
outs += " <var id=\"";
4203-
outs += ptr_to_string(var);
4203+
outs += id_string(var);
42044204
outs += '\"';
42054205
outs += " nameToken=\"";
4206-
outs += ptr_to_string(var->nameToken());
4206+
outs += id_string(var->nameToken());
42074207
outs += '\"';
42084208
outs += " typeStartToken=\"";
4209-
outs += ptr_to_string(var->typeStartToken());
4209+
outs += id_string(var->typeStartToken());
42104210
outs += '\"';
42114211
outs += " typeEndToken=\"";
4212-
outs += ptr_to_string(var->typeEndToken());
4212+
outs += id_string(var->typeEndToken());
42134213
outs += '\"';
42144214
outs += " access=\"";
42154215
outs += accessControlToString(var->mAccess);
42164216
outs += '\"';
42174217
outs += " scope=\"";
4218-
outs += ptr_to_string(var->scope());
4218+
outs += id_string(var->scope());
42194219
outs += '\"';
42204220
if (var->valueType()) {
42214221
outs += " constness=\"";
@@ -7591,7 +7591,7 @@ std::string ValueType::dump() const
75917591
case CONTAINER: {
75927592
ret += "valueType-type=\"container\"";
75937593
ret += " valueType-containerId=\"";
7594-
ret += ptr_to_string(container);
7594+
ret += id_string(container);
75957595
ret += "\"";
75967596
break;
75977597
}
@@ -7674,7 +7674,7 @@ std::string ValueType::dump() const
76747674

76757675
if (typeScope) {
76767676
ret += " valueType-typeScope=\"";
7677-
ret += ptr_to_string(typeScope);
7677+
ret += id_string(typeScope);
76787678
ret += '\"';
76797679
}
76807680

lib/token.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,7 +1710,7 @@ void Token::printValueFlow(bool xml, std::ostream &out) const
17101710
continue;
17111711
if (xml) {
17121712
outs += " <values id=\"";
1713-
outs += ptr_to_string(values);
1713+
outs += id_string(values);
17141714
outs += "\">";
17151715
outs += '\n';
17161716
}
@@ -1763,7 +1763,7 @@ void Token::printValueFlow(bool xml, std::ostream &out) const
17631763
break;
17641764
case ValueFlow::Value::ValueType::TOK:
17651765
outs += "tokvalue=\"";
1766-
outs += ptr_to_string(value.tokvalue);
1766+
outs += id_string(value.tokvalue);
17671767
outs += '\"';
17681768
break;
17691769
case ValueFlow::Value::ValueType::FLOAT:
@@ -1801,7 +1801,7 @@ void Token::printValueFlow(bool xml, std::ostream &out) const
18011801
break;
18021802
case ValueFlow::Value::ValueType::LIFETIME:
18031803
outs += "lifetime=\"";
1804-
outs += ptr_to_string(value.tokvalue);
1804+
outs += id_string(value.tokvalue);
18051805
outs += '\"';
18061806
outs += " lifetime-scope=\"";
18071807
outs += ValueFlow::Value::toString(value.lifetimeScope);
@@ -1812,7 +1812,7 @@ void Token::printValueFlow(bool xml, std::ostream &out) const
18121812
break;
18131813
case ValueFlow::Value::ValueType::SYMBOLIC:
18141814
outs += "symbolic=\"";
1815-
outs += ptr_to_string(value.tokvalue);
1815+
outs += id_string(value.tokvalue);
18161816
outs += '\"';
18171817
outs += " symbolic-delta=\"";
18181818
outs += std::to_string(value.intvalue);

lib/tokenize.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5806,7 +5806,7 @@ void Tokenizer::dump(std::ostream &out) const
58065806
outs += '\n';
58075807
for (const Token *tok = list.front(); tok; tok = tok->next()) {
58085808
outs += " <token id=\"";
5809-
outs += ptr_to_string(tok);
5809+
outs += id_string(tok);
58105810
outs += "\" file=\"";
58115811
outs += ErrorLogger::toxml(list.file(tok));
58125812
outs += "\" linenr=\"";
@@ -5820,7 +5820,7 @@ void Tokenizer::dump(std::ostream &out) const
58205820
outs += '\"';
58215821

58225822
outs += " scope=\"";
5823-
outs += ptr_to_string(tok->scope());
5823+
outs += id_string(tok->scope());
58245824
outs += '\"';
58255825
if (tok->isName()) {
58265826
outs += " type=\"name\"";
@@ -5880,7 +5880,7 @@ void Tokenizer::dump(std::ostream &out) const
58805880
outs += " isAttributeExport=\"true\"";
58815881
if (tok->link()) {
58825882
outs += " link=\"";
5883-
outs += ptr_to_string(tok->link());
5883+
outs += id_string(tok->link());
58845884
outs += '\"';
58855885
}
58865886
if (tok->varId() > 0) {
@@ -5895,37 +5895,37 @@ void Tokenizer::dump(std::ostream &out) const
58955895
}
58965896
if (tok->variable()) {
58975897
outs += " variable=\"";
5898-
outs += ptr_to_string(tok->variable());
5898+
outs += id_string(tok->variable());
58995899
outs += '\"';
59005900
}
59015901
if (tok->function()) {
59025902
outs += " function=\"";
5903-
outs += ptr_to_string(tok->function());
5903+
outs += id_string(tok->function());
59045904
outs += '\"';
59055905
}
59065906
if (!tok->values().empty()) {
59075907
outs += " values=\"";
5908-
outs += ptr_to_string(&tok->values());
5908+
outs += id_string(&tok->values());
59095909
outs += '\"';
59105910
}
59115911
if (tok->type()) {
59125912
outs += " type-scope=\"";
5913-
outs += ptr_to_string(tok->type()->classScope);
5913+
outs += id_string(tok->type()->classScope);
59145914
outs += '\"';
59155915
}
59165916
if (tok->astParent()) {
59175917
outs += " astParent=\"";
5918-
outs += ptr_to_string(tok->astParent());
5918+
outs += id_string(tok->astParent());
59195919
outs += '\"';
59205920
}
59215921
if (tok->astOperand1()) {
59225922
outs += " astOperand1=\"";
5923-
outs += ptr_to_string(tok->astOperand1());
5923+
outs += id_string(tok->astOperand1());
59245924
outs += '\"';
59255925
}
59265926
if (tok->astOperand2()) {
59275927
outs += " astOperand2=\"";
5928-
outs += ptr_to_string(tok->astOperand2());
5928+
outs += id_string(tok->astOperand2());
59295929
outs += '\"';
59305930
}
59315931
if (!tok->originalName().empty()) {
@@ -5963,7 +5963,7 @@ void Tokenizer::dump(std::ostream &out) const
59635963
outs += '\n';
59645964
for (const Library::Container* c: containers) {
59655965
outs += " <container id=\"";
5966-
outs += ptr_to_string(c);
5966+
outs += id_string(c);
59675967
outs += "\" array-like-index-op=\"";
59685968
outs += (c->arrayLike_indexOp ? "true" : "false");
59695969
outs += "\" ";

lib/utils.h

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -270,34 +270,26 @@ std::size_t getArrayLength(const T (& /*unused*/)[size])
270270
return size;
271271
}
272272

273-
/** this is meant as a replacement for when we cannot print a pointer via the stream insertion operator (operator<<) for performance reasons */
274-
// TODO: should give portable value / only used by Tokenizer::dump() and underlying functions - something deterministic would also help with comparing the output
275-
static inline std::string ptr_to_string(const void* p)
273+
/**
274+
* @brief get id string. i.e. for dump files
275+
* it will be a hexadecimal output.
276+
*/
277+
static inline std::string id_string_i(std::uintptr_t l)
276278
{
277-
#if (defined(__APPLE__) && defined(__MACH__))
278-
if (p == nullptr)
279-
return "0x0";
280-
#elif !defined(_WIN32) || defined(__MINGW32__)
281-
if (p == nullptr)
279+
if (!l)
282280
return "0";
283-
#endif
284281

285282
static constexpr int ptr_size = sizeof(void*);
286283

287-
#if defined(_WIN32) && !defined(__MINGW32__)
288284
// two characters of each byte / contains terminating \0
289285
static constexpr int buf_size = (ptr_size * 2) + 1;
290-
#else
291-
// two characters of each byte / contains 0x prefix and contains terminating \0
292-
static constexpr int buf_size = (ptr_size * 2) + 2 + 1;
293-
#endif
286+
294287
char buf[buf_size];
295288

296289
// needs to be signed so we don't underflow in padding loop
297-
int idx = sizeof(buf) - 1;
298-
buf[idx--] = '\0'; // terminate string
290+
int idx = buf_size - 1;
291+
buf[idx] = '\0';
299292

300-
uintptr_t l = reinterpret_cast<uintptr_t>(p);
301293
while (l != 0)
302294
{
303295
char c;
@@ -307,34 +299,19 @@ static inline std::string ptr_to_string(const void* p)
307299
c = '0' + temp;
308300
}
309301
else {
310-
#if !defined(_WIN32) || defined(__MINGW32__)
311302
// a-f
312303
c = 'a' + (temp - 10);
313-
#else
314-
// A-F
315-
c = 'A' + (temp - 10);
316-
#endif
317304
}
318-
buf[idx--] = c; // store in reverse order
305+
buf[--idx] = c; // store in reverse order
319306
l = l / 16;
320307
}
321308

322-
#if defined(_WIN32) && !defined(__MINGW32__)
323-
// pad address with 0
324-
while (idx >= 0) {
325-
buf[idx--] = '0';
326-
}
327-
328-
// 000000F0A61FF122 or 0230FB33
329-
return buf;
330-
#else
331-
// add 0x prefix
332-
buf[idx--] = 'x';
333-
buf[idx--] = '0';
309+
return &buf[idx];
310+
}
334311

335-
// 0x7ffc5aa334d8
336-
return &buf[idx+1];
337-
#endif
312+
static inline std::string id_string(const void* p)
313+
{
314+
return id_string_i(reinterpret_cast<uintptr_t>(p));
338315
}
339316

340317
static inline std::string bool_to_string(bool b)

0 commit comments

Comments
 (0)