From: hama Date: Tue, 29 Dec 2015 23:24:30 +0000 (+0100) Subject: recform: formatting var decl X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=705b6628354a43b2e03b8ce3ed5b61504fc2d698;p=reqt recform: formatting var decl --- diff --git a/appl/recform/CFormatter.cpp b/appl/recform/CFormatter.cpp index 10affda..fd14e84 100644 --- a/appl/recform/CFormatter.cpp +++ b/appl/recform/CFormatter.cpp @@ -114,6 +114,43 @@ void CFormatter::findTypeLists() } } } + // Search for variable definition: + // examples: char* const x; String* x[8*10]; List z = 1+2; + int maxIx = count - 1; + CppOperator op; + for (int ix = 0; ix < count; ix++){ + const FormatToken& item = m_logicalLine.constData()[ix]; + if (item.isOperator(OP_ASSIGN)){ + maxIx = ix - 1; + break; + } + if (item.isTokenType(TOKEN_OPERATOR)){ + op = (CppOperator) item.id(); + if (op == OP_LBRACKET){ + while(++ix < count){ + const FormatToken& item2 = m_logicalLine.constData()[maxIx]; + if (item2.isOperator(OP_RBRACKET)) + break; + } + } else if (! (op == OP_STAR || op == OP_BIT_AND || op == OP_LT + || op == OP_GT)){ + maxIx = -1; + break; + } + } + } + while(--maxIx >= 0){ + FormatToken const& item = m_logicalLine.constData()[maxIx]; + if (item.isOperator(OP_RBRACKET)){ + while(--maxIx >= 0){ + FormatToken const& item2 = m_logicalLine.constData()[maxIx]; + if (item2.isOperator(OP_LBRACKET)) + break; + } + } else { + m_isInTypeList[maxIx] = true; + } + } } /** @@ -138,6 +175,7 @@ void CFormatter::flush(bool isPart) m_lexer->textOfToken(item, buffer); lastItem = item; } + ReStringUtils::chomp(buffer, ' '); m_writer->writeLine(buffer); m_logicalLine.clear(); } @@ -259,6 +297,8 @@ bool CFormatter::needsBlank(ReToken* first, ReToken* second, bool isDeclaration) case TOKEN_ID: if ( (op = (CppOperator) first->id()) == OP_RPARENTH) rc = true; + else if (isDeclaration && op == OP_GT) + rc = false; else rc = needsTrailingBlank((CppOperator) first->id()); break; diff --git a/appl/recform/cuReCFormatter.cpp b/appl/recform/cuReCFormatter.cpp index 03964a4..9d3af70 100644 --- a/appl/recform/cuReCFormatter.cpp +++ b/appl/recform/cuReCFormatter.cpp @@ -51,6 +51,12 @@ public: line = "const char* ptr = (const char*) abc(1 + 2) * 3 / 4;\n"; setTokens(line); m_writer.buffer().clear(); + m_formatter.flush(true); + checkEqu(line, m_writer.buffer()); + + line = "String ptr[3+(4/8)] = x(z[ix++ - 1] + 1 - 2 * 5);\n"; + setTokens(line); + m_writer.buffer().clear(); m_formatter.setLastDeclToken(4); m_formatter.flush(true); checkEqu(line, m_writer.buffer()); diff --git a/base/ReTest.cpp b/base/ReTest.cpp index 0c708e3..831279c 100644 --- a/base/ReTest.cpp +++ b/base/ReTest.cpp @@ -168,6 +168,8 @@ bool ReTest::assertEquals(const ReString& expected, const ReString& current, */ bool ReTest::assertEquals(const char* expected, const char* current, const char* file, int lineNo) { + int len1 = strlen(expected); + int len2 = strlen(current); bool equal = strcmp(expected, current) == 0; if (!equal) { if (strchr(expected, '\n') != NULL || strchr(current, '\n')) { @@ -186,11 +188,15 @@ bool ReTest::assertEquals(const char* expected, const char* current, *ptr++ = '^'; *ptr = '\0'; if (ix < 10) - error("%s-%d: error: diff at index %d\n%s\n%s\n%s", file, - lineNo, ix, expected, current, pointer); + error("%s-%d: error: diff at index %d (%c/%c 0x%x/0x%x)\n%s\n%s\n%s", file, + lineNo, ix, printChar(expected[ix]), printChar(current[ix]), + expected[ix], current[ix], + expected, current, pointer); else - error("%s-%d: error: diff at index %d\n%s\n...%s\n...%s\n%s", - file, lineNo, ix, current, expected + ix - 10 + 3, + error("%s-%d: error: diff at index %d (%c/%c 0x%x/0x%x)\n%s\n...%s\n...%s\n%s", + file, lineNo, ix, printChar(expected[ix]), printChar(current[ix]), + expected[ix], current[ix], + current, expected + ix - 10 + 3, current + ix - 10 + 3, pointer); } } diff --git a/base/ReTest.hpp b/base/ReTest.hpp index f76e7c3..cb96c22 100644 --- a/base/ReTest.hpp +++ b/base/ReTest.hpp @@ -64,6 +64,10 @@ public: bool deleteIfExists = true); bool logContains(const char* pattern); virtual void runTests(void) = 0; +public: + inline static char printChar(char cc){ + return cc < ' ' ? '.' : cc; + } protected: int m_errors; diff --git a/expr/ReSource.cpp b/expr/ReSource.cpp index 33c4c36..ce925ee 100644 --- a/expr/ReSource.cpp +++ b/expr/ReSource.cpp @@ -506,8 +506,7 @@ const ReSourcePosition* ReSource::newPosition(int colNo) { unsigned offset = m_countPositionBlock * sizeof(ReSourcePosition); m_countPositionBlock++; char* posInBlock = &m_sourcePositionBlock->m_positions[offset]; - ReSourceUnit* unit = dynamic_cast(m_currentReader - ->currentSourceUnit()); + ReSourceUnit* unit = m_currentReader->currentSourceUnit(); ReSourcePosition* rc = new (posInBlock) ReSourcePosition(unit, unit->lineNo(), colNo); return rc; @@ -706,6 +705,7 @@ void ReStringReader::replaceSource(ReSourceUnitName name, unit->setCurrentPosition(0); if (source().currentReader() == NULL) source().addReader(this); + m_currentSourceUnit = unit; } }