- fix: function pointers not correctly handle in code parser;

- fix: var assignment not correctly handled in code parser;
  - fix: function args not correctly handled in code parser;
This commit is contained in:
Roy Qu 2022-11-09 22:22:33 +08:00
parent faf25f4f1d
commit bcc1b8dc09
3 changed files with 151 additions and 75 deletions

View File

@ -10,6 +10,9 @@ Red Panda C++ Version 2.4
- enhancement: Rename filenames in bookmarks/breakpoints after a file is save-ased. - enhancement: Rename filenames in bookmarks/breakpoints after a file is save-ased.
- fix: Can't goto definition of classes and namespaces displayed in the class browser on whole project mode. - fix: Can't goto definition of classes and namespaces displayed in the class browser on whole project mode.
- fix: macro defines parsed before not correctly applied in the succeeding parse. - fix: macro defines parsed before not correctly applied in the succeeding parse.
- fix: function pointers not correctly handle in code parser;
- fix: var assignment not correctly handled in code parser;
- fix: function args not correctly handled in code parser;
Red Panda C++ Version 2.3 Red Panda C++ Version 2.3

View File

@ -1293,7 +1293,7 @@ PStatement CppParser::addStatement(const PStatement &parent, const QString &file
if (this->isIdentChar(ch)) { if (this->isIdentChar(ch)) {
QString spaces=(i>argStart)?" ":""; QString spaces=(i>argStart)?" ":"";
args+=spaces; args+=spaces;
word += mTokenizer[i]->text[0]; word += mTokenizer[i]->text;
if (!typeGetted) { if (!typeGetted) {
noNameArgs+=spaces+word; noNameArgs+=spaces+word;
if (mCppTypeKeywords.contains(word) || !isCppKeyword(word)) if (mCppTypeKeywords.contains(word) || !isCppKeyword(word))
@ -1900,7 +1900,7 @@ void CppParser::checkAndHandleMethodOrVar(KeywordType keywordType)
} }
// function call, skip it // function call, skip it
mIndex=moveToNextBraceOrSkipNextSemicolon(mIndex,true); mIndex=moveToEndOfStatement(mIndex,true);
} }
} else if (mTokenizer[mIndex]->text.startsWith('*') } else if (mTokenizer[mIndex]->text.startsWith('*')
|| mTokenizer[mIndex]->text.startsWith('&') || mTokenizer[mIndex]->text.startsWith('&')
@ -2573,11 +2573,7 @@ void CppParser::handleLambda(int index, int endIndex)
} }
} }
i=moveToNextBraceOrSkipNextSemicolon(i, true, bodyEnd); i=moveToEndOfStatement(i, true, bodyEnd);
if (i<bodyEnd && mTokenizer[i]->text=='{') {
//skip '}'
i=mTokenizer[i]->matchIndex+1;
}
} }
removeScopeLevel(mTokenizer[bodyEnd]->line); removeScopeLevel(mTokenizer[bodyEnd]->line);
} }
@ -3095,13 +3091,14 @@ bool CppParser::handleStatement()
handleMethod(StatementKind::skDestructor, "", '~'+mTokenizer[mIndex]->text, mIndex+1, false, false); handleMethod(StatementKind::skDestructor, "", '~'+mTokenizer[mIndex]->text, mIndex+1, false, false);
} else { } else {
//error //error
mIndex=moveToNextBraceOrSkipNextSemicolon(mIndex,false); mIndex=moveToEndOfStatement(mIndex,false);
} }
} else if (!isIdentChar(mTokenizer[mIndex]->text[0])) { } else if (!isIdentChar(mTokenizer[mIndex]->text[0])) {
mIndex=moveToNextBraceOrSkipNextSemicolon(mIndex,false); mIndex=moveToEndOfStatement(mIndex,true);
} else if (mTokenizer[mIndex]->text.endsWith('.') } else if (mTokenizer[mIndex]->text.endsWith('.')
|| mTokenizer[mIndex]->text.endsWith("->")) { || mTokenizer[mIndex]->text.endsWith("->")) {
mIndex=moveToNextBraceOrSkipNextSemicolon(mIndex,true); mIndex=moveToEndOfStatement(mIndex,true);
} else if (checkForKeyword(keywordType)) { // includes template now } else if (checkForKeyword(keywordType)) { // includes template now
handleKeyword(keywordType); handleKeyword(keywordType);
} else if (keywordType==KeywordType::For) { // (for/catch) } else if (keywordType==KeywordType::For) { // (for/catch)
@ -3581,7 +3578,6 @@ void CppParser::handleVar(const QString& typePrefix,bool isExtern,bool isStatic)
// return false; // return false;
// } // }
bool varAdded = false;
QString tempType; QString tempType;
while(mIndex<mTokenizer.tokenCount()) { while(mIndex<mTokenizer.tokenCount()) {
// Skip bit identifiers, // Skip bit identifiers,
@ -3600,20 +3596,23 @@ void CppParser::handleVar(const QString& typePrefix,bool isExtern,bool isStatic)
mIndex++; mIndex++;
} else if (mTokenizer[mIndex]->text==';') { } else if (mTokenizer[mIndex]->text==';') {
break; break;
} else if (mTokenizer[mIndex]->text=='(' } else if (isWordChar(mTokenizer[mIndex]->text[0])) {
&& mTokenizer[mIndex]->matchIndex+1<mTokenizer.tokenCount()
&& mTokenizer[mTokenizer[mIndex]->matchIndex+1]->text=='(') {
//function pointer
QString cmd=mTokenizer[mIndex]->text; QString cmd=mTokenizer[mIndex]->text;
int argStart=mTokenizer[mIndex]->matchIndex+1; if (mTokenizer[mIndex+1]->text=='('
&& mTokenizer[mIndex+1]->matchIndex+1<mTokenizer.tokenCount()
&& mTokenizer[mTokenizer[mIndex+1]->matchIndex+1]->text=='(') {
//function pointer
cmd = mTokenizer[mIndex+2]->text;
int argStart=mTokenizer[mIndex+1]->matchIndex+1;
int argEnd=mTokenizer[argStart]->matchIndex; int argEnd=mTokenizer[argStart]->matchIndex;
if (cmd.startsWith('*')) if (cmd.startsWith("*"))
cmd=cmd.mid(1); cmd = cmd.mid(1);
if (!cmd.isEmpty()) { if (!cmd.isEmpty()) {
QString type=lastType;
addChildStatement( addChildStatement(
getCurrentScope(), getCurrentScope(),
mCurrentFile, mCurrentFile,
lastType, (lastType+" "+tempType+" "+mTokenizer[mIndex]->text).trimmed(),
cmd, cmd,
mergeArgs(argStart,argEnd), mergeArgs(argStart,argEnd),
"", "",
@ -3624,13 +3623,12 @@ void CppParser::handleVar(const QString& typePrefix,bool isExtern,bool isStatic)
mClassScope, mClassScope,
//True, //True,
!isExtern, !isExtern,
isStatic); // TODO: not supported to pass list isStatic);
varAdded = true;
tempType="";
} }
mIndex=argEnd+1; tempType="";
} else if (isWordChar(mTokenizer[mIndex]->text[0])) { mIndex=indexOfNextSemicolonOrLeftBrace(argEnd+1);
QString cmd=mTokenizer[mIndex]->text; } else {
//normal var
while (cmd.startsWith('*')) { while (cmd.startsWith('*')) {
cmd=cmd.mid(1); cmd=cmd.mid(1);
} }
@ -3656,12 +3654,12 @@ void CppParser::handleVar(const QString& typePrefix,bool isExtern,bool isStatic)
mClassScope, mClassScope,
//True, //True,
!isExtern, !isExtern,
isStatic); // TODO: not supported to pass list isStatic);
varAdded = true;
tempType=""; tempType="";
} }
} }
mIndex++; mIndex++;
}
} else if (mTokenizer[mIndex]->text=='(') { } else if (mTokenizer[mIndex]->text=='(') {
mIndex=mTokenizer[mIndex]->matchIndex+1; mIndex=mTokenizer[mIndex]->matchIndex+1;
} else if (mTokenizer[mIndex]->text=='=') { } else if (mTokenizer[mIndex]->text=='=') {
@ -5073,6 +5071,25 @@ int CppParser::indexOfNextSemicolon(int index, int endIndex)
return index; return index;
} }
int CppParser::indexOfNextPeriodOrSemicolon(int index, int endIndex)
{
if (endIndex<0)
endIndex=mTokenizer.tokenCount();
while (index<endIndex) {
switch(mTokenizer[index]->text[0].unicode()) {
case ';':
case ',':
return index;
case '(':
index = mTokenizer[index]->matchIndex+1;
break;
default:
index++;
}
}
return index;
}
int CppParser::indexOfNextSemicolonOrLeftBrace(int index) int CppParser::indexOfNextSemicolonOrLeftBrace(int index)
{ {
while (index<mTokenizer.tokenCount()) { while (index<mTokenizer.tokenCount()) {
@ -5169,19 +5186,70 @@ void CppParser::skipNextSemicolon(int index)
} }
} }
int CppParser::moveToNextBraceOrSkipNextSemicolon(int index, bool checkLambda, int endIndex) //int CppParser::moveToEndOfStatement(int index, bool checkLambda, int endIndex)
//{
// int startIndex=index;
// if (endIndex<0)
// endIndex=mTokenizer.tokenCount();
// bool stop=false;
// while (index<endIndex && !stop) {
// switch(mTokenizer[index]->text[0].unicode()) {
// case ';':
// index++;
// stop=true;
// break;
// case '{':
// //skip '}'
// index = mTokenizer[index]->matchIndex+1;
// stop = true;
// break;
// case '(':
// index = mTokenizer[index]->matchIndex+1;
// break;
// default:
// index++;
// }
// }
// if (stop && checkLambda) {
// while (mTokenizer.lambdasCount()>0 && mTokenizer.indexOfFirstLambda()<index) {
// int i=mTokenizer.indexOfFirstLambda();
// mTokenizer.removeFirstLambda();
// if (i>=startIndex) {
// handleLambda(i,index);
// }
// }
// }
// return index;
//}
int CppParser::moveToEndOfStatement(int index, bool checkLambda, int endIndex)
{ {
int startIndex=index; int startIndex=index;
if (endIndex<0) if (endIndex<0)
endIndex=mTokenizer.tokenCount(); endIndex=mTokenizer.tokenCount();
bool stop=false; if (index>=endIndex)
return index;
index--; // compensate for the first loop
bool skip=true;
do {
index++;
bool stop = false;
while (index<endIndex && !stop) { while (index<endIndex && !stop) {
switch(mTokenizer[index]->text[0].unicode()) { switch(mTokenizer[index]->text[0].unicode()) {
case ';': case ';':
index++;
stop=true; stop=true;
break; break;
case '=':
stop=true;
break;
case '}':
stop=true;
skip=false; //don't skip the orphan '}' that we found
break;
case '{': case '{':
//move to '}'
index=mTokenizer[index]->matchIndex;
stop=true; stop=true;
break; break;
case '(': case '(':
@ -5191,7 +5259,8 @@ int CppParser::moveToNextBraceOrSkipNextSemicolon(int index, bool checkLambda, i
index++; index++;
} }
} }
if (stop && checkLambda) { } while (index<mTokenizer.tokenCount() && mTokenizer[index]->text=='=');
if (index<endIndex && checkLambda) {
while (mTokenizer.lambdasCount()>0 && mTokenizer.indexOfFirstLambda()<index) { while (mTokenizer.lambdasCount()>0 && mTokenizer.indexOfFirstLambda()<index) {
int i=mTokenizer.indexOfFirstLambda(); int i=mTokenizer.indexOfFirstLambda();
mTokenizer.removeFirstLambda(); mTokenizer.removeFirstLambda();
@ -5200,6 +5269,8 @@ int CppParser::moveToNextBraceOrSkipNextSemicolon(int index, bool checkLambda, i
} }
} }
} }
if (skip)
index++;
return index; return index;
} }

View File

@ -589,6 +589,7 @@ private:
void updateSerialId(); void updateSerialId();
int indexOfNextSemicolon(int index, int endIndex=-1); int indexOfNextSemicolon(int index, int endIndex=-1);
int indexOfNextPeriodOrSemicolon(int index, int endIndex=-1);
int indexOfNextSemicolonOrLeftBrace(int index); int indexOfNextSemicolonOrLeftBrace(int index);
int indexOfNextColon(int index); int indexOfNextColon(int index);
int indexOfNextLeftBrace(int index); int indexOfNextLeftBrace(int index);
@ -596,7 +597,8 @@ private:
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);
int moveToNextBraceOrSkipNextSemicolon(int index, bool checkLambda, int endIndex=-1); int moveToEndOfStatement(int index, bool checkLambda, int endIndex=-1);
// int moveToNextAssignmentOrEndOfStatement(int index, bool checkLambda, int endIndex=-1);
void skipParenthesis(int index); void skipParenthesis(int index);
QString mergeArgs(int startIndex, int endIndex); QString mergeArgs(int startIndex, int endIndex);
void parseCommandTypeAndArgs(QString& command, void parseCommandTypeAndArgs(QString& command,