Skip to content

Commit edf0104

Browse files
authored
Token: allow the compiler to inline *At() calls (#6479)
1 parent dfa928f commit edf0104

File tree

2 files changed

+46
-55
lines changed

2 files changed

+46
-55
lines changed

lib/token.cpp

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -376,56 +376,6 @@ void Token::replace(Token *replaceThis, Token *start, Token *end)
376376
delete replaceThis;
377377
}
378378

379-
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
380-
static T *tokAtImpl(T *tok, int index)
381-
{
382-
while (index > 0 && tok) {
383-
tok = tok->next();
384-
--index;
385-
}
386-
while (index < 0 && tok) {
387-
tok = tok->previous();
388-
++index;
389-
}
390-
return tok;
391-
}
392-
393-
const Token *Token::tokAt(int index) const
394-
{
395-
return tokAtImpl(this, index);
396-
}
397-
398-
Token *Token::tokAt(int index)
399-
{
400-
return tokAtImpl(this, index);
401-
}
402-
403-
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
404-
static T *linkAtImpl(T *thisTok, int index)
405-
{
406-
T *tok = thisTok->tokAt(index);
407-
if (!tok) {
408-
throw InternalError(thisTok, "Internal error. Token::linkAt called with index outside the tokens range.");
409-
}
410-
return tok->link();
411-
}
412-
413-
const Token *Token::linkAt(int index) const
414-
{
415-
return linkAtImpl(this, index);
416-
}
417-
418-
Token *Token::linkAt(int index)
419-
{
420-
return linkAtImpl(this, index);
421-
}
422-
423-
const std::string &Token::strAt(int index) const
424-
{
425-
const Token *tok = this->tokAt(index);
426-
return tok ? tok->mStr : emptyString;
427-
}
428-
429379
static
430380
#if defined(__GNUC__)
431381
// GCC does not inline this by itself

lib/token.h

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
//---------------------------------------------------------------------------
2323

2424
#include "config.h"
25+
#include "errortypes.h"
2526
#include "mathlib.h"
2627
#include "templatesimplifier.h"
2728
#include "utils.h"
@@ -213,21 +214,37 @@ class CPPCHECKLIB Token {
213214
* For example index 1 would return next token, and 2
214215
* would return next from that one.
215216
*/
216-
const Token *tokAt(int index) const;
217-
Token *tokAt(int index);
217+
const Token *tokAt(int index) const
218+
{
219+
return tokAtImpl(this, index);
220+
}
221+
Token *tokAt(int index)
222+
{
223+
return tokAtImpl(this, index);
224+
}
218225

219226
/**
220227
* @return the link to the token in given index, related to this token.
221228
* For example index 1 would return the link to next token.
222229
*/
223-
const Token *linkAt(int index) const;
224-
Token *linkAt(int index);
230+
const Token *linkAt(int index) const
231+
{
232+
return linkAtImpl(this, index);
233+
}
234+
Token *linkAt(int index)
235+
{
236+
return linkAtImpl(this, index);
237+
}
225238

226239
/**
227240
* @return String of the token in given index, related to this token.
228241
* If that token does not exist, an empty string is being returned.
229242
*/
230-
const std::string &strAt(int index) const;
243+
const std::string &strAt(int index) const
244+
{
245+
const Token *tok = this->tokAt(index);
246+
return tok ? tok->mStr : emptyString;
247+
}
231248

232249
/**
233250
* Match given token (or list of tokens) to a pattern list.
@@ -784,6 +801,30 @@ class CPPCHECKLIB Token {
784801
static Token *findmatch(Token * const startTok, const char pattern[], const Token * const end, const nonneg int varId = 0);
785802

786803
private:
804+
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
805+
static T *tokAtImpl(T *tok, int index)
806+
{
807+
while (index > 0 && tok) {
808+
tok = tok->next();
809+
--index;
810+
}
811+
while (index < 0 && tok) {
812+
tok = tok->previous();
813+
++index;
814+
}
815+
return tok;
816+
}
817+
818+
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
819+
static T *linkAtImpl(T *thisTok, int index)
820+
{
821+
T *tok = thisTok->tokAt(index);
822+
if (!tok) {
823+
throw InternalError(thisTok, "Internal error. Token::linkAt called with index outside the tokens range.");
824+
}
825+
return tok->link();
826+
}
827+
787828
/**
788829
* Needle is build from multiple alternatives. If one of
789830
* them is equal to haystack, return value is 1. If there

0 commit comments

Comments
 (0)