1 /* 2 * Cppcheck - A tool for static C/C++ code analysis 3 * Copyright (C) 2007-2021 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 "token.h" 22 #include "tokenlist.h" 23 24 #include <string> 25 26 class TestTokenList : public TestFixture { 27 public: TestTokenList()28 TestTokenList() : TestFixture("TestTokenList") {} 29 30 private: 31 Settings settings; 32 run()33 void run() OVERRIDE { 34 TEST_CASE(testaddtoken1); 35 TEST_CASE(testaddtoken2); 36 TEST_CASE(inc); 37 TEST_CASE(isKeyword); 38 } 39 40 // inspired by #5895 testaddtoken1()41 void testaddtoken1() { 42 const std::string code = "0x89504e470d0a1a0a"; 43 TokenList tokenlist(&settings); 44 tokenlist.addtoken(code, 1, 1, false); 45 ASSERT_EQUALS("0x89504e470d0a1a0a", tokenlist.front()->str()); 46 } 47 testaddtoken2()48 void testaddtoken2() { 49 const std::string code = "0xF0000000"; 50 settings.int_bit = 32; 51 TokenList tokenlist(&settings); 52 tokenlist.addtoken(code, 1, 1, false); 53 ASSERT_EQUALS("0xF0000000", tokenlist.front()->str()); 54 } 55 inc() const56 void inc() const { 57 const char code[] = "a++1;1++b;"; 58 59 errout.str(""); 60 61 // tokenize.. 62 TokenList tokenlist(&settings); 63 std::istringstream istr(code); 64 tokenlist.createTokens(istr, "a.cpp"); 65 66 ASSERT(Token::simpleMatch(tokenlist.front(), "a + + 1 ; 1 + + b ;")); 67 } 68 isKeyword()69 void isKeyword() { 70 71 const char code[] = "for a int delete true"; 72 73 { 74 TokenList tokenlist(&settings); 75 std::istringstream istr(code); 76 tokenlist.createTokens(istr, "a.c"); 77 78 ASSERT_EQUALS(true, tokenlist.front()->isKeyword()); 79 ASSERT_EQUALS(true, tokenlist.front()->isControlFlowKeyword()); 80 ASSERT_EQUALS(false, tokenlist.front()->next()->isKeyword()); 81 ASSERT_EQUALS(false, tokenlist.front()->next()->isControlFlowKeyword()); 82 ASSERT_EQUALS(false, tokenlist.front()->tokAt(2)->isKeyword()); 83 ASSERT_EQUALS(true, tokenlist.front()->tokAt(2)->tokType() == Token::eType); 84 ASSERT_EQUALS(false, tokenlist.front()->tokAt(2)->isControlFlowKeyword()); 85 ASSERT_EQUALS(false, tokenlist.front()->tokAt(3)->isKeyword()); 86 ASSERT_EQUALS(false, tokenlist.front()->tokAt(3)->isControlFlowKeyword()); 87 ASSERT_EQUALS(false, tokenlist.front()->tokAt(4)->isKeyword()); 88 ASSERT_EQUALS(true, tokenlist.front()->tokAt(4)->isLiteral()); 89 ASSERT_EQUALS(false, tokenlist.front()->tokAt(4)->isControlFlowKeyword()); 90 } 91 { 92 TokenList tokenlist(&settings); 93 std::istringstream istr(code); 94 tokenlist.createTokens(istr, "a.cpp"); 95 96 ASSERT_EQUALS(true, tokenlist.front()->isKeyword()); 97 ASSERT_EQUALS(true, tokenlist.front()->isControlFlowKeyword()); 98 ASSERT_EQUALS(false, tokenlist.front()->next()->isKeyword()); 99 ASSERT_EQUALS(false, tokenlist.front()->next()->isControlFlowKeyword()); 100 ASSERT_EQUALS(false, tokenlist.front()->tokAt(2)->isKeyword()); 101 ASSERT_EQUALS(true, tokenlist.front()->tokAt(2)->tokType() == Token::eType); 102 ASSERT_EQUALS(false, tokenlist.front()->tokAt(2)->isControlFlowKeyword()); 103 ASSERT_EQUALS(true, tokenlist.front()->tokAt(3)->isKeyword()); 104 ASSERT_EQUALS(false, tokenlist.front()->tokAt(3)->isControlFlowKeyword()); 105 ASSERT_EQUALS(false, tokenlist.front()->tokAt(4)->isKeyword()); 106 ASSERT_EQUALS(true, tokenlist.front()->tokAt(4)->isLiteral()); 107 ASSERT_EQUALS(false, tokenlist.front()->tokAt(4)->isControlFlowKeyword()); 108 } 109 } 110 }; 111 112 REGISTER_TEST(TestTokenList) 113