From edf0104c6a4dc70d924fcd250a9fb30ea9f014c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Mon, 3 Jun 2024 00:25:52 +0200 Subject: [PATCH] Token: allow the compiler to inline `*At()` calls (#6479) --- lib/token.cpp | 50 -------------------------------------------------- lib/token.h | 51 ++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 46 insertions(+), 55 deletions(-) diff --git a/lib/token.cpp b/lib/token.cpp index fbdf7c5efb9..e7e925f421b 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -376,56 +376,6 @@ void Token::replace(Token *replaceThis, Token *start, Token *end) delete replaceThis; } -template )> -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; -} - -const Token *Token::tokAt(int index) const -{ - return tokAtImpl(this, index); -} - -Token *Token::tokAt(int index) -{ - return tokAtImpl(this, index); -} - -template )> -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(); -} - -const Token *Token::linkAt(int index) const -{ - return linkAtImpl(this, index); -} - -Token *Token::linkAt(int index) -{ - return linkAtImpl(this, index); -} - -const std::string &Token::strAt(int index) const -{ - const Token *tok = this->tokAt(index); - return tok ? tok->mStr : emptyString; -} - static #if defined(__GNUC__) // GCC does not inline this by itself diff --git a/lib/token.h b/lib/token.h index 9e557376996..2f054d96946 100644 --- a/lib/token.h +++ b/lib/token.h @@ -22,6 +22,7 @@ //--------------------------------------------------------------------------- #include "config.h" +#include "errortypes.h" #include "mathlib.h" #include "templatesimplifier.h" #include "utils.h" @@ -213,21 +214,37 @@ class CPPCHECKLIB Token { * For example index 1 would return next token, and 2 * would return next from that one. */ - const Token *tokAt(int index) const; - Token *tokAt(int index); + 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; - Token *linkAt(int index); + 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 std::string &strAt(int index) const + { + const Token *tok = this->tokAt(index); + return tok ? tok->mStr : emptyString; + } /** * Match given token (or list of tokens) to a pattern list. @@ -784,6 +801,30 @@ class CPPCHECKLIB Token { static Token *findmatch(Token * const startTok, const char pattern[], const Token * const end, const nonneg int varId = 0); private: + template )> + 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 )> + 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