From 3619552e38a3ccd4482e5ca5638950f0ea1b014c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 8 Dec 2008 17:28:44 +0000 Subject: [PATCH] Variable Id : First simple implementation --- testtokenize.cpp | 37 +++++++++++++++++++++++++++++++++- token.h | 3 +++ tokenize.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ tokenize.h | 5 ++++- 4 files changed, 95 insertions(+), 2 deletions(-) diff --git a/testtokenize.cpp b/testtokenize.cpp index 985597540..cd65aab3e 100644 --- a/testtokenize.cpp +++ b/testtokenize.cpp @@ -50,7 +50,9 @@ private: TEST_CASE( multi_compare ); - TEST_CASE( match1 ); + TEST_CASE( match1 ); + + TEST_CASE( varid1 ); } @@ -259,6 +261,39 @@ private: // Match.. ASSERT_EQUALS( true, TOKEN::Match(tokenizer.tokens(), "%var% || %var%") ); } + } + + + void varid1() + { + const std::string code(";static int i = 1;\n" + "void f()\n" + "{\n" + " int i = 2;\n" + " for (int i = 0; i < 10; ++i)\n" + " i = 3;\n" + " i = 4;\n" + "}\n" ); + + // tokenize.. + Tokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + tokenizer.setVarId(); + + for ( const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next ) + { + if ( tok->str() != "i" ) + ASSERT_EQUALS( 0, tok->varId ); + else if ( TOKEN::Match(tok, "i = 1") ) + ASSERT_EQUALS( 1, tok->varId ); + else if ( TOKEN::Match(tok, "i = 2") ) + ASSERT_EQUALS( 2, tok->varId ); + else if ( TOKEN::Match(tok, "i = 3") ) + ASSERT_EQUALS( 3, tok->varId ); + else if ( TOKEN::Match(tok, "i = 4") ) + ASSERT_EQUALS( 2, tok->varId ); + } } }; diff --git a/token.h b/token.h index c099bd1ce..b5be39699 100644 --- a/token.h +++ b/token.h @@ -113,6 +113,9 @@ public: unsigned int FileIndex; unsigned int linenr; + + unsigned int varId; + TOKEN *next; private: diff --git a/tokenize.cpp b/tokenize.cpp index 16e15ec4f..78eff4c8d 100644 --- a/tokenize.cpp +++ b/tokenize.cpp @@ -618,6 +618,58 @@ void Tokenizer::tokenizeCode(std::istream &code, const unsigned int FileIndex) } //--------------------------------------------------------------------------- + + +void Tokenizer::setVarId() +{ + // Clear all variable ids + for ( TOKEN *tok = _tokens; tok; tok = tok->next ) + tok->varId = 0; + + // Set variable ids.. + unsigned int _varId = 0; + for ( TOKEN *tok = _tokens; tok; tok = tok->next ) + { + if ( ! TOKEN::Match(tok, "[;{}(] %type% %var%") ) + continue; + + // Determine name of declared variable.. + const char *varname = 0; + TOKEN *tok2 = tok->next; + while ( ! TOKEN::Match( tok2, "[;[=(]" ) ) + { + if ( tok2->isName() ) + varname = tok2->strAt(0); + else if ( tok2->str() != "*" ) + break; + tok2 = tok2->next; + } + + // Variable declaration found => Set variable ids + if ( TOKEN::Match(tok2, "[;[=]") && varname ) + { + ++_varId; + int indentlevel = 0; + int parlevel = 0; + for ( tok2 = tok->next; tok2 && indentlevel >= 0; tok2 = tok2->next ) + { + if ( tok2->str() == varname ) + tok2->varId = _varId; + else if ( tok2->str() == "{" ) + ++indentlevel; + else if ( tok2->str() == "}" ) + --indentlevel; + else if ( tok2->str() == "(" ) + ++parlevel; + else if ( tok2->str() == ")" ) + --parlevel; + else if ( parlevel < 0 && tok2->str() == ";" ) + break; + } + } + } +} + //--------------------------------------------------------------------------- // Simplify token list diff --git a/tokenize.h b/tokenize.h index 39f3f4fa5..44b3a1d5d 100644 --- a/tokenize.h +++ b/tokenize.h @@ -49,7 +49,10 @@ public: * @param code input stream for code * @param FileName The filename */ - void tokenize(std::istream &code, const char FileName[]); + void tokenize(std::istream &code, const char FileName[]); + + /** Set variable id */ + void setVarId(); /** Simplify tokenlist */ void simplifyTokenList();