- fix: foreach-loops are not correctly parsed.

This commit is contained in:
Roy Qu 2023-05-14 15:57:07 +08:00
parent 2114206e76
commit 7e3ee41546
3 changed files with 36 additions and 28 deletions

View File

@ -2,6 +2,7 @@ Red Panda C++ Version 2.22
- fix: Crash at startup when current problem in the problem set is connected with source file. - fix: Crash at startup when current problem in the problem set is connected with source file.
- fix: Double-clicking on touchpad can't select current word. - fix: Double-clicking on touchpad can't select current word.
- fix: foreach-loops are not correctly parsed.
Red Panda C++ Version 2.21 Red Panda C++ Version 2.21

View File

@ -2428,8 +2428,15 @@ void CppParser::handleForBlock()
mIndex++; // skip for/catch; mIndex++; // skip for/catch;
if (mIndex >= tokenCount) if (mIndex >= tokenCount)
return; return;
if (mTokenizer[mIndex]->text!="(")
return;
int i=indexOfNextSemicolon(mIndex); int i=indexOfNextSemicolon(mIndex);
int i2 = i+1; //skip over ';' (tokenizer have change for(;;) to for(;) int i2 = i+1; //skip over ';' (tokenizer have change for(;;) to for(;)
// for(int x:vec)
if (i2 > mTokenizer[mIndex]->matchIndex)
i2 = mTokenizer[mIndex]->matchIndex+1;
if (i2>=tokenCount) if (i2>=tokenCount)
return; return;
if (mTokenizer[i2]->text.startsWith('{')) { if (mTokenizer[i2]->text.startsWith('{')) {
@ -3955,7 +3962,7 @@ void CppParser::internalParse(const QString &fileName)
QStringList preprocessResult = mPreprocessor.result(); QStringList preprocessResult = mPreprocessor.result();
#ifdef QT_DEBUG #ifdef QT_DEBUG
// stringsToFile(mPreprocessor.result(),QString("r:\\preprocess-%1.txt").arg(extractFileName(fileName))); stringsToFile(mPreprocessor.result(),QString("r:\\preprocess-%1.txt").arg(extractFileName(fileName)));
// mPreprocessor.dumpDefinesTo("r:\\defines.txt"); // mPreprocessor.dumpDefinesTo("r:\\defines.txt");
// mPreprocessor.dumpIncludesListTo("r:\\includes.txt"); // mPreprocessor.dumpIncludesListTo("r:\\includes.txt");
#endif #endif
@ -3974,7 +3981,7 @@ void CppParser::internalParse(const QString &fileName)
if (mTokenizer.tokenCount() == 0) if (mTokenizer.tokenCount() == 0)
return; return;
#ifdef QT_DEBUG #ifdef QT_DEBUG
// mTokenizer.dumpTokens(QString("r:\\tokens-%1.txt").arg(extractFileName(fileName))); mTokenizer.dumpTokens(QString("r:\\tokens-%1.txt").arg(extractFileName(fileName)));
#endif #endif
#ifdef QT_DEBUG #ifdef QT_DEBUG
mLastIndex = -1; mLastIndex = -1;
@ -3987,8 +3994,8 @@ void CppParser::internalParse(const QString &fileName)
} }
// qDebug()<<"parse"<<timer.elapsed(); // qDebug()<<"parse"<<timer.elapsed();
#ifdef QT_DEBUG #ifdef QT_DEBUG
// mStatementList.dumpAll(QString("r:\\all-stats-%1.txt").arg(extractFileName(fileName))); mStatementList.dumpAll(QString("r:\\all-stats-%1.txt").arg(extractFileName(fileName)));
// mStatementList.dump(QString("r:\\stats-%1.txt").arg(extractFileName(fileName))); mStatementList.dump(QString("r:\\stats-%1.txt").arg(extractFileName(fileName)));
#endif #endif
//reduce memory usage //reduce memory usage
internalClear(); internalClear();

View File

@ -540,29 +540,29 @@ QString CppPreprocessor::expandMacros(const QString &line, int depth)
void CppPreprocessor::expandMacro(const QString &line, QString &newLine, QString &word, int &i, int depth) void CppPreprocessor::expandMacro(const QString &line, QString &newLine, QString &word, int &i, int depth)
{ {
int lenLine = line.length(); int lenLine = line.length();
if (word.startsWith("__") // if (word.startsWith("__")
&& word.endsWith("__")) { // && word.endsWith("__")) {
// if (word == "__attribute__") { //// if (word == "__attribute__") {
//skip gcc __attribute__ // //skip gcc __attribute__
while ((i<lenLine) && (line[i] == ' ' || line[i]=='\t')) // while ((i<lenLine) && (line[i] == ' ' || line[i]=='\t'))
i++; // i++;
if ((i<lenLine) && (line[i]=="(")) { // if ((i<lenLine) && (line[i]=="(")) {
int level=0; // int level=0;
while (i<lenLine) { // while (i<lenLine) {
switch(line[i].unicode()) { // switch(line[i].unicode()) {
case '(': // case '(':
level++; // level++;
break; // break;
case ')': // case ')':
level--; // level--;
break; // break;
} // }
i++; // i++;
if (level==0) // if (level==0)
break; // break;
} // }
} // }
} else { // } else {
PDefine define = getDefine(word); PDefine define = getDefine(word);
if (define && define->args=="" ) { if (define && define->args=="" ) {
if (define->value != word ) if (define->value != word )
@ -601,7 +601,7 @@ void CppPreprocessor::expandMacro(const QString &line, QString &newLine, QString
} else { } else {
newLine += word; newLine += word;
} }
} // }
} }
QString CppPreprocessor::removeGCCAttributes(const QString &line) QString CppPreprocessor::removeGCCAttributes(const QString &line)