From 6269c3cbbd03bff4a6f7c6cce1f4ad605d07f9b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 13 Dec 2008 20:22:45 +0000 Subject: [PATCH] tokenizer : updated simplifyKnownVariables --- testtokenize.cpp | 10 ++++++---- tokenize.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/testtokenize.cpp b/testtokenize.cpp index d26d6b445..8d41f8e54 100644 --- a/testtokenize.cpp +++ b/testtokenize.cpp @@ -233,13 +233,14 @@ private: std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); - tokenizer.simplifyTokenList(); + tokenizer.setVarId(); + tokenizer.simplifyKnownVariables(); std::ostringstream ostr; for (const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next()) ostr << " " << tok->str(); // TODO, the actual string should be " void f ( ) { int a ; a = 10 ; if ( true ) ; }" - ASSERT_EQUALS( std::string(" void f ( ) { int a ; a = 10 ; if ( a ) ; }"), ostr.str() ); + ASSERT_EQUALS( std::string(" void f ( ) { int a = 10 ; if ( 10 ) ; }"), ostr.str() ); } { @@ -255,12 +256,13 @@ private: std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); - tokenizer.simplifyTokenList(); + tokenizer.setVarId(); + tokenizer.simplifyKnownVariables(); std::ostringstream ostr; for (const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next()) ostr << " " << tok->str(); - ASSERT_EQUALS( std::string(" void f ( ) { int a ; a = 10 ; a = g ( ) ; if ( a ) ; }"), ostr.str() ); + ASSERT_EQUALS( std::string(" void f ( ) { int a = 10 ; a = g ( ) ; if ( a ) ; }"), ostr.str() ); } } diff --git a/tokenize.cpp b/tokenize.cpp index 951500a6e..3a40f537b 100644 --- a/tokenize.cpp +++ b/tokenize.cpp @@ -1182,11 +1182,51 @@ bool Tokenizer::simplifyKnownVariables() // TODO, this functions needs to be implemented. // TODO, test bool ret = false; - for ( const TOKEN *tok = tokens(); tok; tok = tok->next() ) + for ( TOKEN *tok = _tokens; tok; tok = tok->next() ) { - if ( TOKEN::Match(tok, "%var% = %num% ;") ) - { + // Search for a block of code + if ( ! TOKEN::Match(tok, ") const| {") ) + continue; + // parse the block of code.. + int indentlevel = 0; + for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next() ) + { + if ( tok2->str() == "{" ) + ++indentlevel; + + else if ( tok2->str() == "}" ) + { + --indentlevel; + if ( indentlevel <= 0 ) + continue; + } + + else if ( TOKEN::Match(tok2, "%var% = %num% ;") ) + { + unsigned int varid = tok2->varId(); + TOKEN *tok3 = tok2; + while ( tok3 ) + { + tok3 = tok3->next(); + + // Perhaps it's a loop => bail out + if ( TOKEN::Match(tok3, "[{}]") ) + break; + + // Variable is used somehow in a non-defined pattern => bail out + if ( tok3->varId() == varid ) + break; + + // Replace variable with numeric constant.. + if ( TOKEN::Match(tok3, "if ( %varid% )", 0, 0, varid) ) + { + tok3 = tok3->next()->next(); + tok3->setstr( tok2->strAt(2) ); + ret = true; + } + } + } } }