- refactor: for/catch processing

- fix: Link in the project options dialog / precompiled header pages is not clickable.
This commit is contained in:
Roy Qu 2023-10-22 18:33:53 +08:00
parent 30cabe2faa
commit c3352ad661
6 changed files with 136 additions and 155 deletions

View File

@ -9,6 +9,7 @@ Red Panda C++ Version 2.26
- fix: Code suggestions in namespace. - fix: Code suggestions in namespace.
- enhancement: Code suggestions for namespace alias. - enhancement: Code suggestions for namespace alias.
- fix: Correctly handle statements like 'using xxx::operator()'. - fix: Correctly handle statements like 'using xxx::operator()'.
- fix: Link in the project options dialog / precompiled header pages is not clickable.
Red Panda C++ Version 2.25 Red Panda C++ Version 2.25

View File

@ -1091,8 +1091,8 @@ void CppParser::resetParser()
mStatementList.clear(); mStatementList.clear();
mProjectFiles.clear(); mProjectFiles.clear();
mBlockBeginSkips.clear(); //list of for/catch block begin token index; // mBlockBeginSkips.clear(); //list of for/catch block begin token index;
mBlockEndSkips.clear(); //list of for/catch block end token index; // mBlockEndSkips.clear(); //list of for/catch block end token index;
mInlineNamespaceEndSkips.clear(); // list for inline namespace end token index; mInlineNamespaceEndSkips.clear(); // list for inline namespace end token index;
mFilesToScan.clear(); // list of base files to scan mFilesToScan.clear(); // list of base files to scan
mNamespaces.clear(); // namespace and the statements in its scope mNamespaces.clear(); // namespace and the statements in its scope
@ -1717,8 +1717,8 @@ void CppParser::internalClear()
mMemberAccessibilities.clear(); mMemberAccessibilities.clear();
mIndex = 0; mIndex = 0;
mCurrentMemberAccessibility = StatementAccessibility::None; mCurrentMemberAccessibility = StatementAccessibility::None;
mBlockBeginSkips.clear(); // mBlockBeginSkips.clear();
mBlockEndSkips.clear(); // mBlockEndSkips.clear();
mInlineNamespaceEndSkips.clear(); mInlineNamespaceEndSkips.clear();
} }
@ -2406,19 +2406,19 @@ QString CppParser::doFindTemplateParamOf(const QString &fileName, const QString
return getTemplateParam(statement,fileName, phrase,index, currentScope); return getTemplateParam(statement,fileName, phrase,index, currentScope);
} }
int CppParser::getCurrentBlockEndSkip() const //int CppParser::getCurrentBlockEndSkip() const
{ //{
if (mBlockEndSkips.isEmpty()) // if (mBlockEndSkips.isEmpty())
return mTokenizer.tokenCount()+1; // return mTokenizer.tokenCount()+1;
return mBlockEndSkips.back(); // return mBlockEndSkips.back();
} //}
int CppParser::getCurrentBlockBeginSkip() const //int CppParser::getCurrentBlockBeginSkip() const
{ //{
if (mBlockBeginSkips.isEmpty()) // if (mBlockBeginSkips.isEmpty())
return mTokenizer.tokenCount()+1; // return mTokenizer.tokenCount()+1;
return mBlockBeginSkips.back(); // return mBlockBeginSkips.back();
} //}
int CppParser::getCurrentInlineNamespaceEndSkip() const int CppParser::getCurrentInlineNamespaceEndSkip() const
{ {
@ -2541,43 +2541,43 @@ PStatement CppParser::getTypeDef(const PStatement& statement,
return PStatement(); return PStatement();
} }
void CppParser::handleCatchBlock() //void CppParser::handleCatchBlock()
{ //{
int tokenCount = mTokenizer.tokenCount(); // int tokenCount = mTokenizer.tokenCount();
int startLine= mTokenizer[mIndex]->line; // int startLine= mTokenizer[mIndex]->line;
mIndex++; // skip for/catch; // mIndex++; // skip for/catch;
if (!((mIndex < tokenCount) && (mTokenizer[mIndex]->text.startsWith('(')))) // if (!((mIndex < tokenCount) && (mTokenizer[mIndex]->text.startsWith('('))))
return; // return;
//skip params // //skip params
int i2=mTokenizer[mIndex]->matchIndex+1; // int i2=mTokenizer[mIndex]->matchIndex+1;
if (i2>=tokenCount) // if (i2>=tokenCount)
return; // return;
if (mTokenizer[i2]->text.startsWith('{')) { // if (mTokenizer[i2]->text.startsWith('{')) {
mBlockBeginSkips.append(i2); // mBlockBeginSkips.append(i2);
int i = indexOfMatchingBrace(i2); // int i = indexOfMatchingBrace(i2);
mBlockEndSkips.append(i); // mBlockEndSkips.append(i);
} else { // } else {
int i=indexOfNextSemicolon(i2); // int i=indexOfNextSemicolon(i2);
mBlockEndSkips.append(i); // mBlockEndSkips.append(i);
} // }
// add a block // // add a block
PStatement block = addStatement( // PStatement block = addStatement(
getCurrentScope(), // getCurrentScope(),
mCurrentFile, // mCurrentFile,
"", // "",
"", // "",
"", // "",
"", // "",
"", // "",
startLine, // startLine,
StatementKind::skBlock, // StatementKind::skBlock,
getScope(), // getScope(),
mCurrentMemberAccessibility, // mCurrentMemberAccessibility,
StatementProperty::spHasDefinition); // StatementProperty::spHasDefinition);
addSoloScopeLevel(block,startLine); // addSoloScopeLevel(block,startLine);
scanMethodArgs(block,mIndex); // scanMethodArgs(block,mIndex);
mIndex=mTokenizer[mIndex]->matchIndex+1; // mIndex=mTokenizer[mIndex]->matchIndex+1;
} //}
void CppParser::handleConcept() void CppParser::handleConcept()
{ {
@ -2807,45 +2807,23 @@ void CppParser::handleForBlock()
mIndex++; // skip for/catch; mIndex++; // skip for/catch;
if (mIndex >= tokenCount) if (mIndex >= tokenCount)
return; return;
if (mTokenizer[mIndex]->text!="(") if (mTokenizer[mIndex]->text!='(')
return; return;
int i=indexOfNextSemicolon(mIndex); int i=mTokenizer[mIndex]->matchIndex; //")"
int i2 = i+1; //skip over ';' (tokenizer have change for(;;) to for(;) int i2 = i+1;
// 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=='{') {
mBlockBeginSkips.append(i2); mTokenizer[mIndex]->text="{";
i=indexOfMatchingBrace(i2); mTokenizer[mIndex]->matchIndex = mTokenizer[i2]->matchIndex;
// tokenizer will handle unbalanced braces, no need check here mTokenizer[mTokenizer[mIndex]->matchIndex]->matchIndex = mIndex;
// if (i==i2) mTokenizer[i]->text=";";
// mBlockEndSkips.append(mTokenizer.tokenCount()); mTokenizer[i2]->text=";";
// else
mBlockEndSkips.append(i);
} else { } else {
i=indexOfNextSemicolon(i2); mTokenizer[mIndex]->text=";";
mBlockEndSkips.append(i); mTokenizer[i]->text=";";
mIndex++; //skip ';'
} }
// add a block
PStatement block = addStatement(
getCurrentScope(),
mCurrentFile,
"",
"",
"",
"",
"",
startLine,
StatementKind::skBlock,
getScope(),
mCurrentMemberAccessibility,
StatementProperty::spHasDefinition);
addSoloScopeLevel(block,startLine);
} }
void CppParser::handleKeyword(KeywordType skipType) void CppParser::handleKeyword(KeywordType skipType)
@ -3535,8 +3513,8 @@ void CppParser::handleAccessibilitySpecifiers(KeywordType keywordType)
bool CppParser::handleStatement() bool CppParser::handleStatement()
{ {
QString funcType,funcName; QString funcType,funcName;
int idx=getCurrentBlockEndSkip(); // int idx=getCurrentBlockEndSkip();
int idx2=getCurrentBlockBeginSkip(); // int idx2=getCurrentBlockBeginSkip();
int idx3=getCurrentInlineNamespaceEndSkip(); int idx3=getCurrentInlineNamespaceEndSkip();
KeywordType keywordType; KeywordType keywordType;
#ifdef QT_DEBUG #ifdef QT_DEBUG
@ -3546,21 +3524,22 @@ bool CppParser::handleStatement()
#endif #endif
int tokenCount = mTokenizer.tokenCount(); int tokenCount = mTokenizer.tokenCount();
if (mIndex >= idx2) { // if (mIndex >= idx2) {
//skip (previous handled) block begin // //skip (previous handled) block begin
mBlockBeginSkips.pop_back(); // mBlockBeginSkips.pop_back();
if (mIndex == idx2) // if (mIndex == idx2)
mIndex++; // mIndex++;
else if (mIndex<tokenCount) //error happens, but we must remove an (error) added scope // else if (mIndex<tokenCount) //error happens, but we must remove an (error) added scope
removeScopeLevel(mTokenizer[mIndex]->line); // removeScopeLevel(mTokenizer[mIndex]->line);
} else if (mIndex >= idx) { // } else if (mIndex >= idx) {
//skip (previous handled) block end // //skip (previous handled) block end
mBlockEndSkips.pop_back(); // mBlockEndSkips.pop_back();
if (idx+1 < tokenCount) // if (idx+1 < tokenCount)
removeScopeLevel(mTokenizer[idx+1]->line); // removeScopeLevel(mTokenizer[idx+1]->line);
if (mIndex == idx) // if (mIndex == idx)
mIndex++; // mIndex++;
} else if (mIndex >= idx3) { // } else
if (mIndex >= idx3) {
//skip (previous handled) inline name space end //skip (previous handled) inline name space end
mInlineNamespaceEndSkips.pop_back(); mInlineNamespaceEndSkips.pop_back();
if (mIndex == idx3) if (mIndex == idx3)
@ -3619,10 +3598,8 @@ bool CppParser::handleStatement()
handleConcept(); handleConcept();
} else if (keywordType==KeywordType::Requires) { } else if (keywordType==KeywordType::Requires) {
skipRequires(); skipRequires();
} else if (keywordType==KeywordType::For) { // (for/catch) } else if (keywordType==KeywordType::For || keywordType==KeywordType::Catch) { // (for/catch)
handleForBlock(); handleForBlock();
} else if (keywordType==KeywordType::Catch) { // (for/catch)
handleCatchBlock();
} else if (checkForAccessibilitySpecifiers(keywordType)) { // public /private/proteced } else if (checkForAccessibilitySpecifiers(keywordType)) { // public /private/proteced
handleAccessibilitySpecifiers(keywordType); handleAccessibilitySpecifiers(keywordType);
} else if (keywordType==KeywordType::Enum) { } else if (keywordType==KeywordType::Enum) {
@ -4388,17 +4365,17 @@ void CppParser::skipRequires()
mIndex++; // skip '::'; mIndex++; // skip '::';
} }
} }
if (mIndex>=tokenCount) if (mIndex+1>=tokenCount)
return; return;
if (mTokenizer[mIndex]->text!="&&") if (mTokenizer[mIndex]->text!="&" || mTokenizer[mIndex+1]->text!="&")
break; break;
mIndex++; // skip '&&'; mIndex+=2; // skip '&&';
} }
if (mIndex>=tokenCount) if (mIndex+1>=tokenCount)
return; return;
if (mTokenizer[mIndex]->text!="||") if (mTokenizer[mIndex]->text!="|" || mTokenizer[mIndex+1]->text!="|")
break; break;
mIndex++; // skip '||'; mIndex+=2; // skip '||';
} }
} }
@ -4422,9 +4399,9 @@ 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
//qDebug()<<"preprocess"<<timer.elapsed(); //qDebug()<<"preprocess"<<timer.elapsed();
//reduce memory usage //reduce memory usage
@ -4441,7 +4418,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;
@ -4454,8 +4431,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

@ -462,8 +462,8 @@ private:
int getBracketEnd(const QString& s, int startAt) const; int getBracketEnd(const QString& s, int startAt) const;
StatementAccessibility getClassMemberAccessibility(const QString& text) const; StatementAccessibility getClassMemberAccessibility(const QString& text) const;
StatementAccessibility getClassMemberAccessibility(KeywordType keywordType) const; StatementAccessibility getClassMemberAccessibility(KeywordType keywordType) const;
int getCurrentBlockBeginSkip() const; // int getCurrentBlockBeginSkip() const;
int getCurrentBlockEndSkip() const; // int getCurrentBlockEndSkip() const;
int getCurrentInlineNamespaceEndSkip() const; int getCurrentInlineNamespaceEndSkip() const;
PStatement getCurrentScope() const; // gets last item from last level PStatement getCurrentScope() const; // gets last item from last level
QString getTemplateParam(const PStatement& statement, const QString& filename, QString getTemplateParam(const PStatement& statement, const QString& filename,
@ -488,7 +488,7 @@ private:
const QString& sNoNameArgs) const; const QString& sNoNameArgs) const;
PStatement getTypeDef(const PStatement& statement, PStatement getTypeDef(const PStatement& statement,
const QString& fileName, const QString& aType) const; const QString& fileName, const QString& aType) const;
void handleCatchBlock(); // void handleCatchBlock();
void handleConcept(); void handleConcept();
void handleEnum(bool isTypedef); void handleEnum(bool isTypedef);
void handleForBlock(); void handleForBlock();
@ -705,8 +705,8 @@ private:
CppTokenizer mTokenizer; CppTokenizer mTokenizer;
CppPreprocessor mPreprocessor; CppPreprocessor mPreprocessor;
QSet<QString> mProjectFiles; QSet<QString> mProjectFiles;
QVector<int> mBlockBeginSkips; //list of for/catch block begin token index; // QVector<int> mBlockBeginSkips; //list of for/catch block begin token index;
QVector<int> mBlockEndSkips; //list of for/catch block end token index; // QVector<int> mBlockEndSkips; //list of for/catch block end token index;
QVector<int> mInlineNamespaceEndSkips; // list for inline namespace end token index; QVector<int> mInlineNamespaceEndSkips; // list for inline namespace end token index;
QSet<QString> mFilesToScan; // list of base files to scan QSet<QString> mFilesToScan; // list of base files to scan
int mFilesScannedCount; // count of files that have been scanned int mFilesScannedCount; // count of files that have been scanned

View File

@ -158,30 +158,30 @@ void CppTokenizer::countLines()
} }
} }
QString CppTokenizer::getForInit() //QString CppTokenizer::getForInit()
{ //{
QChar* startOffset = mCurrent; // QChar* startOffset = mCurrent;
// Step into the init statement // // Step into the init statement
mCurrent++; // mCurrent++;
TokenType tokenType; // TokenType tokenType;
// Process until ; or end of file // // Process until ; or end of file
while (true) { // while (true) {
QString s = getNextToken(&tokenType); // QString s = getNextToken(&tokenType);
simplify(s); // simplify(s);
if (!s.isEmpty()) // if (!s.isEmpty())
addToken(s,mCurrentLine,tokenType); // addToken(s,mCurrentLine,tokenType);
if ( (s == "") || (s == ";") || (s==")") || (s=="(")) // if ( (s == "") || (s == ";") || (s==")") || (s=="("))
break; // break;
// : is used in for-each loop // // : is used in for-each loop
} // }
// Skip to end of for loop // // Skip to end of for loop
mCurrent = startOffset; // mCurrent = startOffset;
skipPair('(', ')'); // skipPair('(', ')');
return ""; // return "";
} //}
QString CppTokenizer::getNextToken(TokenType *pTokenType) QString CppTokenizer::getNextToken(TokenType *pTokenType)
{ {
@ -203,10 +203,10 @@ QString CppTokenizer::getNextToken(TokenType *pTokenType)
} }
} }
done = (result != ""); done = (result != "");
} else if (isForInit()) { // } else if (isForInit()) {
countLines(); // countLines();
result = getForInit(); // result = getForInit();
done = (result != ""); // done = (result != "");
// } else if (isArguments()) { // } else if (isArguments()) {
// countLines(); // countLines();
// result = getArguments(); // result = getArguments();
@ -526,10 +526,10 @@ bool CppTokenizer::isArguments()
return *mCurrent == '('; return *mCurrent == '(';
} }
bool CppTokenizer::isForInit() //bool CppTokenizer::isForInit()
{ //{
return (*mCurrent == '(') && (mLastToken == "for"); // return (*mCurrent == '(') && (mLastToken == "for");
} //}
bool CppTokenizer::isNumber() bool CppTokenizer::isNumber()
{ {

View File

@ -75,14 +75,14 @@ private:
void countLines(); void countLines();
PToken getToken(int index); PToken getToken(int index);
QString getForInit(); // QString getForInit();
QString getNextToken( QString getNextToken(
TokenType *pTokenType); TokenType *pTokenType);
QString getNumber(); QString getNumber();
QString getPreprocessor(); QString getPreprocessor();
QString getWord(); QString getWord();
bool isArguments(); bool isArguments();
bool isForInit(); // bool isForInit();
bool isNumber(); bool isNumber();
bool isPreprocessor(); bool isPreprocessor();
bool isWord(); bool isWord();

View File

@ -78,6 +78,9 @@
<property name="alignment"> <property name="alignment">
<set>Qt::AlignCenter</set> <set>Qt::AlignCenter</set>
</property> </property>
<property name="openExternalLinks">
<bool>true</bool>
</property>
</widget> </widget>
</item> </item>
</layout> </layout>