From 0a11b4b6ead2004e9dbfe1fc52bb7a4963033621 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Wed, 6 Mar 2024 19:35:35 +0800 Subject: [PATCH] - Enhancement: Issue #213 Expands macro when finding function tips. --- NEWS.md | 2 ++ RedPandaIDE/parser/cppparser.cpp | 40 ++++++++++---------------- RedPandaIDE/parser/cppparser.h | 6 ++-- RedPandaIDE/parser/cpppreprocessor.cpp | 37 ++++++++++++------------ RedPandaIDE/parser/cpppreprocessor.h | 17 ++++++----- 5 files changed, 47 insertions(+), 55 deletions(-) diff --git a/NEWS.md b/NEWS.md index d3a79073..c3e63ea4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -86,6 +86,8 @@ Red Panda C++ Version 2.26 - Enhancement: Improved Raw string support - Enhancement: New option for compiler set "Don't localize gcc output messages" - Enhancement: Optimization for drawing scrollbars. + - Enhancement: Issue #213 Expands macro when finding function tips. + Red Panda C++ Version 2.25 diff --git a/RedPandaIDE/parser/cppparser.cpp b/RedPandaIDE/parser/cppparser.cpp index 9f6226ec..b536d66f 100644 --- a/RedPandaIDE/parser/cppparser.cpp +++ b/RedPandaIDE/parser/cppparser.cpp @@ -156,6 +156,14 @@ QList CppParser::getListOfFunctions(const QString &fileName, const Q PStatement statement = doFindStatementOf(fileName,phrase, line); if (!statement) return result; + if (statement->kind == StatementKind::skPreprocessor) { + if (statement->args.isEmpty()) { + QString name = expandMacro(statement->value); + statement = doFindStatementOf(fileName, name ,line); + if (!statement) + return result; + } + } PStatement parentScope; if (statement->kind == StatementKind::skClass) { parentScope = statement; @@ -4560,36 +4568,12 @@ void CppParser::inheritClassStatement(const PStatement& derived, bool isStruct, } } -void CppParser::fillListOfFunctions(const QString& fileName, int line, - const PStatement& statement, - const PStatement& scopeStatement, QStringList &list) -{ - StatementMap children = mStatementList.childrenStatements(scopeStatement); - for (const PStatement& child:children) { - if ((statement->command == child->command) -#ifdef Q_OS_WIN - || (statement->command +'A' == child->command) - || (statement->command +'W' == child->command) -#endif - ) { - if (line < child->line && (child->fileName == fileName)) - continue; - list.append(prettyPrintStatement(child,child->fileName,child->line)); - } - } -} - QList CppParser::getListOfFunctions(const QString &fileName, int line, const PStatement &statement, const PStatement &scopeStatement) const { QList result; StatementMap children = mStatementList.childrenStatements(scopeStatement); for (const PStatement& child:children) { - if (( (statement->command == child->command) -#ifdef Q_OS_WIN - || (statement->command +'A' == child->command) - || (statement->command +'W' == child->command) -#endif - ) ) { + if (statement->command == child->command) { if (line < child->line && (child->fileName == fileName)) continue; result.append(child); @@ -6628,6 +6612,12 @@ void CppParser::parseCommandTypeAndArgs(QString &command, QString &typeSuffix, Q } +QString CppParser::expandMacro(const QString &text) const +{ + QSet usedMacros; + return mPreprocessor.expandMacros(text, usedMacros); +} + const QSet &CppParser::projectFiles() const { return mProjectFiles; diff --git a/RedPandaIDE/parser/cppparser.h b/RedPandaIDE/parser/cppparser.h index ca05ced4..6ae3d799 100644 --- a/RedPandaIDE/parser/cppparser.h +++ b/RedPandaIDE/parser/cppparser.h @@ -274,10 +274,6 @@ private: const QString& phrase, int index, const PStatement& currentScope) const; - - void fillListOfFunctions(const QString& fileName, int line, - const PStatement& statement, - const PStatement& scopeStatement, QStringList& list); QList getListOfFunctions(const QString& fileName, int line, const PStatement& statement, const PStatement& scopeStatement) const; @@ -685,6 +681,8 @@ private: void parseCommandTypeAndArgs(QString& command, QString& typeSuffix, QString& args) const; + QString expandMacro(const QString& text) const; + private: int mParserId; ParserLanguage mLanguage; diff --git a/RedPandaIDE/parser/cpppreprocessor.cpp b/RedPandaIDE/parser/cpppreprocessor.cpp index 1bfe1213..09aadb6c 100644 --- a/RedPandaIDE/parser/cpppreprocessor.cpp +++ b/RedPandaIDE/parser/cpppreprocessor.cpp @@ -515,29 +515,29 @@ void CppPreprocessor::handleUndefine(const QString &line) } } -QString CppPreprocessor::expandMacros(const QString &line, QSet usedMacros) +QString CppPreprocessor::expandMacros(const QString &text, QSet usedMacros) const { QString word; QString newLine; - int lenLine = line.length(); + int lenLine = text.length(); int i=0; while (i< lenLine) { - QChar ch=line[i]; + QChar ch=text[i]; if (isWordChar(ch)) { word += ch; } else { if (!word.isEmpty()) { - expandMacro(line,newLine,word,i,usedMacros); + expandMacro(text,newLine,word,i,usedMacros); } word = ""; if (i< lenLine) { - newLine += line[i]; + newLine += text[i]; } } i++; } if (!word.isEmpty()) { - expandMacro(line,newLine,word,i, usedMacros); + expandMacro(text,newLine,word,i, usedMacros); } return newLine; } @@ -574,29 +574,29 @@ QString CppPreprocessor::expandMacros() return newLine; } -void CppPreprocessor::expandMacro(const QString &line, QString &newLine, QString &word, int &i, QSet usedMacros) +void CppPreprocessor::expandMacro(const QString &text, QString &newText, const QString &word, int &i, QSet usedMacros) const { if (usedMacros.contains(word)) return; - int lenLine = line.length(); + int lenLine = text.length(); PDefine define = getDefine(word); if (define && define->args=="" ) { usedMacros.insert(word); if (define->value != word ) { - newLine += expandMacros(define->value,usedMacros); + newText += expandMacros(define->value,usedMacros); } else - newLine += word; + newText += word; } else if (define && (define->args!="")) { - while ((i usedMacros) +void CppPreprocessor::expandMacro(QString &newLine, const QString &word, int &i, QSet usedMacros) { if (usedMacros.contains(word)) return; @@ -1462,7 +1462,7 @@ bool CppPreprocessor::skipParenthesis(const QString &line, int &index, int step) return false; } -QString CppPreprocessor::expandFunction(PDefine define, QString args) +QString CppPreprocessor::expandFunction(PDefine define, const QString &args) { // Replace function by this string QString result = define->formatValue; @@ -1523,7 +1523,6 @@ QString CppPreprocessor::expandFunction(PDefine define, QString args) || (define->varArgIndex!=-1 && argValues.length() < define->argUsed.length()-1) ) { qDebug()<<"*** Expand Macro error ***"; - qDebug()<mFileName<<":"<mIndex; qDebug()<<"Macro: "<name<args; qDebug()<<"Actual param: "< usedMacros) const; + void expandMacro(const QString &text, QString &newText, const QString &word, int &i, QSet usedMacros) const; + const QStringList& result() const{ return mResult; }; @@ -126,6 +133,7 @@ private: parentIsFalse }; + static QString expandFunction(PDefine define,const QString &args); void preprocessBuffer(); void skipToEndOfPreprocessor(); void skipToPreprocessor(); @@ -135,15 +143,11 @@ private: void handleInclude(const QString& line, bool fromNext=false); void handlePreprocessor(const QString& value); void handleUndefine(const QString& line); - QString expandMacros(const QString& line, QSet usedMacros); QString expandMacros(); - void expandMacro(const QString& line, QString& newLine, QString& word, int& i, QSet usedMacros); - void expandMacro(QString& newLine, QString& word, int& i, QSet usedMacros); + void expandMacro(QString &newLine, const QString &word, int& i, QSet usedMacros); QString removeGCCAttributes(const QString& line); void removeGCCAttribute(const QString&line, QString& newLine, int &i, const QString& word); - PDefine getDefine(const QString& name) const{ - return mDefines.value(name,PDefine()); - } + // current file stuff PParsedFile getInclude(int index) const { return mIncludes[index]; @@ -235,7 +239,6 @@ static bool isNumberChar(const QChar& ch); bool evaluateIf(const QString& line); QString expandDefines(QString line); bool skipParenthesis(const QString&line, int& index, int step = 1); - QString expandFunction(PDefine define,QString args); bool skipSpaces(const QString &expr, int& pos); bool evalNumber(const QString &expr, int& result, int& pos); bool evalTerm(const QString &expr, int& result, int& pos);