From 63da926ed2c4fc1a701a0c7b4465988152fc89f2 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Sat, 9 May 2009 22:32:29 +0300 Subject: [PATCH] Fix ticket #294 (### Error: Invalid number of character () http://apps.sourceforge.net/trac/cppcheck/ticket/294 --- src/preprocessor.cpp | 2 +- src/tokenize.cpp | 14 ++++++++++---- src/tokenize.h | 6 ++++++ test/testpreprocessor.cpp | 21 +++++++++++++++++++++ 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/preprocessor.cpp b/src/preprocessor.cpp index f48a7bbb7..27e8c04a4 100644 --- a/src/preprocessor.cpp +++ b/src/preprocessor.cpp @@ -599,7 +599,7 @@ public: { // Tokenize the macro to make it easier to handle std::istringstream istr(macro.c_str()); - tokenizer.tokenize(istr, ""); + tokenizer.createTokens(istr); // macro name.. if (tokens() && tokens()->isName()) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index c2a077f2f..fbef6745f 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -153,11 +153,8 @@ void Tokenizer::InsertTokens(Token *dest, Token *src, unsigned int n) // Tokenize - tokenizes a given file. //--------------------------------------------------------------------------- -bool Tokenizer::tokenize(std::istream &code, const char FileName[]) +void Tokenizer::createTokens(std::istream &code) { - // The "_files" vector remembers what files have been tokenized.. - _files.push_back(FileLister::simplifyPath(FileName)); - // line number in parsed code unsigned int lineno = 1; @@ -336,6 +333,15 @@ bool Tokenizer::tokenize(std::istream &code, const char FileName[]) } addtoken(CurrentToken.c_str(), lineno, FileIndex); +} + +bool Tokenizer::tokenize(std::istream &code, const char FileName[]) +{ + // The "_files" vector remembers what files have been tokenized.. + _files.push_back(FileLister::simplifyPath(FileName)); + + createTokens(code); + if (!createLinks()) { // Source has syntax errors, can't proceed diff --git a/src/tokenize.h b/src/tokenize.h index 5afbf83f6..1a6e41dea 100644 --- a/src/tokenize.h +++ b/src/tokenize.h @@ -49,6 +49,12 @@ public: */ bool tokenize(std::istream &code, const char FileName[]); + /** + * Create tokens from code. + * @param code input stream for code + */ + void createTokens(std::istream &code); + /** Set variable id */ void setVarId(); diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 38be78970..5074d2ec4 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -120,6 +120,7 @@ private: TEST_CASE(missing_doublequote); TEST_CASE(unicode1); + TEST_CASE(define_part_of_func); } @@ -888,6 +889,26 @@ private: std::istringstream istr(filedata); ASSERT_THROW(Preprocessor::read(istr), std::runtime_error); } + + void define_part_of_func() + { + const char filedata[] = "#define A g(\n" + "void f() {\n" + " A );\n" + " }\n"; + + // Preprocess => actual result.. + std::istringstream istr(filedata); + std::map actual; + Preprocessor preprocessor; + preprocessor.preprocess(istr, actual, "file.c"); + + // Compare results.. + ASSERT_EQUALS(1, static_cast(actual.size())); + ASSERT_EQUALS("\nvoid f() {\ng( );\n}\n", actual[""]); + ASSERT_EQUALS("", errout.str()); + } + }; REGISTER_TEST(TestPreprocessor)