|
22 | 22 | //---------------------------------------------------------------------------
|
23 | 23 |
|
24 | 24 | #include "config.h"
|
| 25 | +#include "errortypes.h" |
25 | 26 | #include "mathlib.h"
|
26 | 27 | #include "templatesimplifier.h"
|
27 | 28 | #include "utils.h"
|
@@ -213,21 +214,37 @@ class CPPCHECKLIB Token {
|
213 | 214 | * For example index 1 would return next token, and 2
|
214 | 215 | * would return next from that one.
|
215 | 216 | */
|
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 | + } |
218 | 225 |
|
219 | 226 | /**
|
220 | 227 | * @return the link to the token in given index, related to this token.
|
221 | 228 | * For example index 1 would return the link to next token.
|
222 | 229 | */
|
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 | + } |
225 | 238 |
|
226 | 239 | /**
|
227 | 240 | * @return String of the token in given index, related to this token.
|
228 | 241 | * If that token does not exist, an empty string is being returned.
|
229 | 242 | */
|
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 | + } |
231 | 248 |
|
232 | 249 | /**
|
233 | 250 | * Match given token (or list of tokens) to a pattern list.
|
@@ -784,6 +801,30 @@ class CPPCHECKLIB Token {
|
784 | 801 | static Token *findmatch(Token * const startTok, const char pattern[], const Token * const end, const nonneg int varId = 0);
|
785 | 802 |
|
786 | 803 | 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 | + |
787 | 828 | /**
|
788 | 829 | * Needle is build from multiple alternatives. If one of
|
789 | 830 | * them is equal to haystack, return value is 1. If there
|
|
0 commit comments