From d859894105c9ca3ddad4d6a3051c91ebe15c4da5 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Thu, 21 Mar 2024 16:55:32 +0800 Subject: [PATCH] fix #290 : Auto type induction for expression contains '[]' are not correct. --- NEWS.md | 3 +- RedPandaIDE/parser/cppparser.cpp | 77 ++++++++++++++++++++------------ RedPandaIDE/parser/cppparser.h | 3 +- 3 files changed, 53 insertions(+), 30 deletions(-) diff --git a/NEWS.md b/NEWS.md index 927ff3ee..853c18f2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -69,7 +69,8 @@ Red Panda C++ Version 2.27 - enhancement: Tooltip info for the stacktrace table in the debug panel. - fix: '*=' is treadted as '*' when parsing. - fix: Can't correctly retrieve function parameters type. - + - fix: Auto type induction for expression contains '[]' are not correct. + Red Panda C++ Version 2.26 - enhancement: Code suggestion for embedded std::vectors. - change: Use ctrl+mouseMove event to highlight jumpable symbols (instead of ctrl+tooltip). diff --git a/RedPandaIDE/parser/cppparser.cpp b/RedPandaIDE/parser/cppparser.cpp index df79da2c..20545d47 100644 --- a/RedPandaIDE/parser/cppparser.cpp +++ b/RedPandaIDE/parser/cppparser.cpp @@ -4193,8 +4193,12 @@ void CppParser::handleVar(const QString& typePrefix,bool isExtern,bool isStatic) && !(addedVar->properties & StatementProperty::spFunctionPointer) && AutoTypes.contains(addedVar->type)) { //handle e.g.: for(auto x:vec) - QStringList phraseExpression; - phraseExpression.append(mTokenizer[mIndex+1]->text); + int endIndex = indexOfNextRightParenthesis(mIndex+1); + QString expressionText; + for (int i=mIndex+1;itext; + } + QStringList phraseExpression = splitExpression(expressionText); int pos = 0; PEvalStatement aliasStatement = doEvalExpression(mCurrentFile, phraseExpression, @@ -4212,6 +4216,7 @@ void CppParser::handleVar(const QString& typePrefix,bool isExtern,bool isStatic) addedVar->type = type; } } + mIndex=endIndex; } addedVar.reset(); bool should_exit=false; @@ -4249,21 +4254,11 @@ void CppParser::handleVar(const QString& typePrefix,bool isExtern,bool isStatic) int pos = 0; int endIndex = skipAssignment(mIndex, tokenCount); - QStringList phraseExpression; + QString expressionText; for (int i=mIndex+1;itext; - if (cmd.length()>1 && cmd.endsWith(".")) { - phraseExpression.append(cmd.left(cmd.length()-1)); - phraseExpression.append("."); - } else if (cmd.length()>2 && cmd.endsWith("->")) { - phraseExpression.append(cmd.left(cmd.length()-2)); - phraseExpression.append("->"); - } else if (cmd.length()>2 && cmd.endsWith("::")) { - phraseExpression.append(cmd.left(cmd.length()-2)); - phraseExpression.append("::"); - } else - phraseExpression.append(cmd); + expressionText.append(mTokenizer[i]->text); } + QStringList phraseExpression = splitExpression(expressionText); PEvalStatement aliasStatement = doEvalExpression(mCurrentFile, phraseExpression, pos, @@ -4349,21 +4344,11 @@ void CppParser::handleVar(const QString& typePrefix,bool isExtern,bool isStatic) && AutoTypes.contains(addedVar->type)) { int pos = 0; int endIndex = mTokenizer[mIndex]->matchIndex; - QStringList phraseExpression; + QString expressionText; for (int i=mIndex+1;itext; - if (cmd.length()>1 && cmd.endsWith(".")) { - phraseExpression.append(cmd.left(cmd.length()-1)); - phraseExpression.append("."); - } else if (cmd.length()>2 && cmd.endsWith("->")) { - phraseExpression.append(cmd.left(cmd.length()-2)); - phraseExpression.append("->"); - } else if (cmd.length()>2 && cmd.endsWith("::")) { - phraseExpression.append(cmd.left(cmd.length()-2)); - phraseExpression.append("::"); - } else - phraseExpression.append(cmd); + expressionText.append(mTokenizer[i]->text); } + QStringList phraseExpression = splitExpression(expressionText); PEvalStatement aliasStatement = doEvalExpression(mCurrentFile, phraseExpression, pos, @@ -6516,6 +6501,24 @@ int CppParser::indexPassParenthesis(int index) return index; } +int CppParser::indexOfNextRightParenthesis(int index) +{ + int tokenCount = mTokenizer.tokenCount(); + while (indextext; + switch(s[0].unicode()) { + case ')': + return index; + case '(': + index = mTokenizer[index]->matchIndex+1; + break; + default: + index++; + } + } + return index; +} + //int CppParser::indexPassBraces(int index) //{ // int tokenCount = mTokenizer.tokenCount(); @@ -6688,6 +6691,24 @@ QString CppParser::expandMacro(const QString &text) const return mPreprocessor.expandMacros(text, usedMacros); } +QStringList CppParser::splitExpression(const QString &expr) +{ + QStringList result; + QSynedit::CppSyntaxer syntaxer; + syntaxer.resetState(); + QStringList lines = textToLines(expr); + for(int i=0;itokenType()!=QSynedit::TokenType::Comment + && syntaxer.getTokenAttribute()->tokenType()!=QSynedit::TokenType::Space) + result.append(syntaxer.getToken()); + syntaxer.next(); + } + } + return result; +} + const QSet &CppParser::projectFiles() const { return mProjectFiles; diff --git a/RedPandaIDE/parser/cppparser.h b/RedPandaIDE/parser/cppparser.h index b217c911..853d5488 100644 --- a/RedPandaIDE/parser/cppparser.h +++ b/RedPandaIDE/parser/cppparser.h @@ -673,6 +673,7 @@ private: int indexOfNextColon(int index); int indexOfNextLeftBrace(int index); int indexPassParenthesis(int index); + int indexOfNextRightParenthesis(int index); // int indexPassBraces(int index); int skipAssignment(int index, int endIndex); void skipNextSemicolon(int index); @@ -684,7 +685,7 @@ private: QString& typeSuffix, QString& args) const; QString expandMacro(const QString& text) const; - + static QStringList splitExpression(const QString& expr); private: int mParserId; ParserLanguage mLanguage;