fix #290 : Auto type induction for expression contains '[]' are not correct.

This commit is contained in:
Roy Qu 2024-03-21 16:55:32 +08:00
parent c68eea9463
commit d859894105
3 changed files with 53 additions and 30 deletions

View File

@ -69,6 +69,7 @@ Red Panda C++ Version 2.27
- enhancement: Tooltip info for the stacktrace table in the debug panel. - enhancement: Tooltip info for the stacktrace table in the debug panel.
- fix: '*=' is treadted as '*' when parsing. - fix: '*=' is treadted as '*' when parsing.
- fix: Can't correctly retrieve function parameters type. - fix: Can't correctly retrieve function parameters type.
- fix: Auto type induction for expression contains '[]' are not correct.
Red Panda C++ Version 2.26 Red Panda C++ Version 2.26
- enhancement: Code suggestion for embedded std::vectors. - enhancement: Code suggestion for embedded std::vectors.

View File

@ -4193,8 +4193,12 @@ void CppParser::handleVar(const QString& typePrefix,bool isExtern,bool isStatic)
&& !(addedVar->properties & StatementProperty::spFunctionPointer) && !(addedVar->properties & StatementProperty::spFunctionPointer)
&& AutoTypes.contains(addedVar->type)) { && AutoTypes.contains(addedVar->type)) {
//handle e.g.: for(auto x:vec) //handle e.g.: for(auto x:vec)
QStringList phraseExpression; int endIndex = indexOfNextRightParenthesis(mIndex+1);
phraseExpression.append(mTokenizer[mIndex+1]->text); QString expressionText;
for (int i=mIndex+1;i<endIndex;i++) {
expressionText+=mTokenizer[i]->text;
}
QStringList phraseExpression = splitExpression(expressionText);
int pos = 0; int pos = 0;
PEvalStatement aliasStatement = doEvalExpression(mCurrentFile, PEvalStatement aliasStatement = doEvalExpression(mCurrentFile,
phraseExpression, phraseExpression,
@ -4212,6 +4216,7 @@ void CppParser::handleVar(const QString& typePrefix,bool isExtern,bool isStatic)
addedVar->type = type; addedVar->type = type;
} }
} }
mIndex=endIndex;
} }
addedVar.reset(); addedVar.reset();
bool should_exit=false; bool should_exit=false;
@ -4249,21 +4254,11 @@ void CppParser::handleVar(const QString& typePrefix,bool isExtern,bool isStatic)
int pos = 0; int pos = 0;
int endIndex = skipAssignment(mIndex, tokenCount); int endIndex = skipAssignment(mIndex, tokenCount);
QStringList phraseExpression; QString expressionText;
for (int i=mIndex+1;i<endIndex;i++) { for (int i=mIndex+1;i<endIndex;i++) {
QString cmd = mTokenizer[i]->text; expressionText.append(mTokenizer[i]->text);
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);
} }
QStringList phraseExpression = splitExpression(expressionText);
PEvalStatement aliasStatement = doEvalExpression(mCurrentFile, PEvalStatement aliasStatement = doEvalExpression(mCurrentFile,
phraseExpression, phraseExpression,
pos, pos,
@ -4349,21 +4344,11 @@ void CppParser::handleVar(const QString& typePrefix,bool isExtern,bool isStatic)
&& AutoTypes.contains(addedVar->type)) { && AutoTypes.contains(addedVar->type)) {
int pos = 0; int pos = 0;
int endIndex = mTokenizer[mIndex]->matchIndex; int endIndex = mTokenizer[mIndex]->matchIndex;
QStringList phraseExpression; QString expressionText;
for (int i=mIndex+1;i<endIndex;i++) { for (int i=mIndex+1;i<endIndex;i++) {
QString cmd = mTokenizer[i]->text; expressionText.append(mTokenizer[i]->text);
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);
} }
QStringList phraseExpression = splitExpression(expressionText);
PEvalStatement aliasStatement = doEvalExpression(mCurrentFile, PEvalStatement aliasStatement = doEvalExpression(mCurrentFile,
phraseExpression, phraseExpression,
pos, pos,
@ -6516,6 +6501,24 @@ int CppParser::indexPassParenthesis(int index)
return index; return index;
} }
int CppParser::indexOfNextRightParenthesis(int index)
{
int tokenCount = mTokenizer.tokenCount();
while (index<tokenCount) {
QString s =mTokenizer[index]->text;
switch(s[0].unicode()) {
case ')':
return index;
case '(':
index = mTokenizer[index]->matchIndex+1;
break;
default:
index++;
}
}
return index;
}
//int CppParser::indexPassBraces(int index) //int CppParser::indexPassBraces(int index)
//{ //{
// int tokenCount = mTokenizer.tokenCount(); // int tokenCount = mTokenizer.tokenCount();
@ -6688,6 +6691,24 @@ QString CppParser::expandMacro(const QString &text) const
return mPreprocessor.expandMacros(text, usedMacros); 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;i<lines.length();i++) {
syntaxer.setLine(lines[i],i+1);
while(!syntaxer.eol()) {
if (syntaxer.getTokenAttribute()->tokenType()!=QSynedit::TokenType::Comment
&& syntaxer.getTokenAttribute()->tokenType()!=QSynedit::TokenType::Space)
result.append(syntaxer.getToken());
syntaxer.next();
}
}
return result;
}
const QSet<QString> &CppParser::projectFiles() const const QSet<QString> &CppParser::projectFiles() const
{ {
return mProjectFiles; return mProjectFiles;

View File

@ -673,6 +673,7 @@ private:
int indexOfNextColon(int index); int indexOfNextColon(int index);
int indexOfNextLeftBrace(int index); int indexOfNextLeftBrace(int index);
int indexPassParenthesis(int index); int indexPassParenthesis(int index);
int indexOfNextRightParenthesis(int index);
// int indexPassBraces(int index); // int indexPassBraces(int index);
int skipAssignment(int index, int endIndex); int skipAssignment(int index, int endIndex);
void skipNextSemicolon(int index); void skipNextSemicolon(int index);
@ -684,7 +685,7 @@ private:
QString& typeSuffix, QString& typeSuffix,
QString& args) const; QString& args) const;
QString expandMacro(const QString& text) const; QString expandMacro(const QString& text) const;
static QStringList splitExpression(const QString& expr);
private: private:
int mParserId; int mParserId;
ParserLanguage mLanguage; ParserLanguage mLanguage;