|
| 1 | +/* |
| 2 | + * Cppcheck - A tool for static C/C++ code analysis |
| 3 | + * Copyright (C) 2007-2020 Cppcheck team. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "settings.h" |
| 20 | +#include "testsuite.h" |
| 21 | +#include "testutils.h" |
| 22 | +#include "token.h" |
| 23 | +#include "tokenize.h" |
| 24 | +#include "tokenlist.h" |
| 25 | +#include "tokenrange.h" |
| 26 | +#include "symboldatabase.h" |
| 27 | + |
| 28 | +#include <string> |
| 29 | +#include <vector> |
| 30 | + |
| 31 | +struct InternalError; |
| 32 | + |
| 33 | + |
| 34 | +class TestTokenRange : public TestFixture { |
| 35 | +public: |
| 36 | + TestTokenRange() : TestFixture("TestTokenRange") { |
| 37 | + } |
| 38 | + |
| 39 | +private: |
| 40 | + void run() OVERRIDE { |
| 41 | + TEST_CASE(enumerationToEnd); |
| 42 | + TEST_CASE(untilHelperToEnd); |
| 43 | + TEST_CASE(untilHelperPartWay); |
| 44 | + TEST_CASE(partialEnumeration); |
| 45 | + TEST_CASE(scopeExample); |
| 46 | + TEST_CASE(exampleAlgorithms); |
| 47 | + } |
| 48 | + |
| 49 | + std::string testTokenRange(ConstTokenRange range, const Token* start, const Token* end) const |
| 50 | + { |
| 51 | + auto tokenToString = [](const Token* t) { return t ? t->str() : "<null>"; }; |
| 52 | + int index = 0; |
| 53 | + const Token* expected = start; |
| 54 | + for (const Token* t : range) |
| 55 | + { |
| 56 | + if (expected != t) { |
| 57 | + std::ostringstream message; |
| 58 | + message << "Failed to match token " << tokenToString(expected) << " at position " << index << ". Got " << tokenToString(t) << " instead"; |
| 59 | + return message.str(); |
| 60 | + } |
| 61 | + index++; |
| 62 | + expected = expected->next(); |
| 63 | + } |
| 64 | + if (expected != end) { |
| 65 | + std::ostringstream message; |
| 66 | + message << "Failed to terminate on " << tokenToString(end) << ". Instead terminated on " << tokenToString(expected) << " at position " << index << "."; |
| 67 | + return message.str(); |
| 68 | + } |
| 69 | + return ""; |
| 70 | + } |
| 71 | + |
| 72 | + void enumerationToEnd() const { |
| 73 | + std::istringstream istr("void a(){} void main(){ if(true){a();} }"); |
| 74 | + TokenList tokenList(nullptr); |
| 75 | + tokenList.createTokens(istr, "test.cpp"); |
| 76 | + ASSERT_EQUALS("", testTokenRange(ConstTokenRange{ tokenList.front(), nullptr }, tokenList.front(), nullptr)); |
| 77 | + } |
| 78 | + |
| 79 | + void untilHelperToEnd() const { |
| 80 | + std::istringstream istr("void a(){} void main(){ if(true){a();} }"); |
| 81 | + TokenList tokenList(nullptr); |
| 82 | + tokenList.createTokens(istr, "test.cpp"); |
| 83 | + ASSERT_EQUALS("", testTokenRange(tokenList.front()->until(nullptr), tokenList.front(), nullptr)); |
| 84 | + } |
| 85 | + |
| 86 | + void untilHelperPartWay() const { |
| 87 | + std::istringstream istr("void a(){} void main(){ if(true){a();} }"); |
| 88 | + TokenList tokenList(nullptr); |
| 89 | + tokenList.createTokens(istr, "test.cpp"); |
| 90 | + const Token* start = tokenList.front()->tokAt(4); |
| 91 | + const Token* end = start->tokAt(8); |
| 92 | + ASSERT_EQUALS("", testTokenRange(start->until(end), start, end)); |
| 93 | + } |
| 94 | + |
| 95 | + void partialEnumeration() const { |
| 96 | + std::istringstream istr("void a(){} void main(){ if(true){a();} }"); |
| 97 | + TokenList tokenList(nullptr); |
| 98 | + tokenList.createTokens(istr, "test.cpp"); |
| 99 | + const Token* start = tokenList.front()->tokAt(4); |
| 100 | + const Token* end = tokenList.front()->tokAt(10); |
| 101 | + ASSERT_EQUALS("", testTokenRange(ConstTokenRange{ start, end }, start, end)); |
| 102 | + } |
| 103 | + |
| 104 | + void scopeExample() const { |
| 105 | + Settings settings; |
| 106 | + Tokenizer tokenizer{ &settings, nullptr }; |
| 107 | + std::istringstream sample("void a(){} void main(){ if(true){a();} }"); |
| 108 | + tokenizer.tokenize(sample, "test.cpp"); |
| 109 | + |
| 110 | + const SymbolDatabase* sd = tokenizer.getSymbolDatabase(); |
| 111 | + const Scope& scope = *std::next(sd->scopeList.begin(), 3); //The scope of the if block |
| 112 | + |
| 113 | + std::ostringstream contents; |
| 114 | + for (const Token* t : ConstTokenRange{ scope.bodyStart->next(), scope.bodyEnd }) |
| 115 | + { |
| 116 | + contents << t->str(); |
| 117 | + } |
| 118 | + ASSERT_EQUALS("a();", contents.str()); |
| 119 | + } |
| 120 | + |
| 121 | + void exampleAlgorithms() const { |
| 122 | + std::istringstream istr("void a(){} void main(){ if(true){a();} }"); |
| 123 | + TokenList tokenList(nullptr); |
| 124 | + tokenList.createTokens(istr, "test.cpp"); |
| 125 | + ConstTokenRange range{ tokenList.front(), nullptr }; |
| 126 | + ASSERT_EQUALS(true, std::all_of(range.begin(), range.end(), [](const Token*) {return true;})); |
| 127 | + ASSERT_EQUALS(true, std::any_of(range.begin(), range.end(), [](const Token* t) {return t->str() == "true";})); |
| 128 | + ASSERT_EQUALS("true", (*std::find_if(range.begin(), range.end(), [](const Token* t) {return t->str() == "true";}))->str()); |
| 129 | + ASSERT_EQUALS(3, std::count_if(range.begin(), range.end(), [](const Token* t) {return t->str() == "{";})); |
| 130 | + } |
| 131 | +}; |
| 132 | + |
| 133 | +REGISTER_TEST(TestTokenRange) |
0 commit comments