- Enhancement: Issue #213 Expands macro when finding function tips.

This commit is contained in:
Roy Qu 2024-03-06 19:35:35 +08:00
parent eff6beba0a
commit 0a11b4b6ea
5 changed files with 47 additions and 55 deletions

View File

@ -86,6 +86,8 @@ Red Panda C++ Version 2.26
- Enhancement: Improved Raw string support - Enhancement: Improved Raw string support
- Enhancement: New option for compiler set "Don't localize gcc output messages" - Enhancement: New option for compiler set "Don't localize gcc output messages"
- Enhancement: Optimization for drawing scrollbars. - Enhancement: Optimization for drawing scrollbars.
- Enhancement: Issue #213 Expands macro when finding function tips.
Red Panda C++ Version 2.25 Red Panda C++ Version 2.25

View File

@ -156,6 +156,14 @@ QList<PStatement> CppParser::getListOfFunctions(const QString &fileName, const Q
PStatement statement = doFindStatementOf(fileName,phrase, line); PStatement statement = doFindStatementOf(fileName,phrase, line);
if (!statement) if (!statement)
return result; 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; PStatement parentScope;
if (statement->kind == StatementKind::skClass) { if (statement->kind == StatementKind::skClass) {
parentScope = statement; 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<PStatement> CppParser::getListOfFunctions(const QString &fileName, int line, const PStatement &statement, const PStatement &scopeStatement) const QList<PStatement> CppParser::getListOfFunctions(const QString &fileName, int line, const PStatement &statement, const PStatement &scopeStatement) const
{ {
QList<PStatement> result; QList<PStatement> result;
StatementMap children = mStatementList.childrenStatements(scopeStatement); StatementMap children = mStatementList.childrenStatements(scopeStatement);
for (const PStatement& child:children) { for (const PStatement& child:children) {
if (( (statement->command == child->command) 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)) if (line < child->line && (child->fileName == fileName))
continue; continue;
result.append(child); result.append(child);
@ -6628,6 +6612,12 @@ void CppParser::parseCommandTypeAndArgs(QString &command, QString &typeSuffix, Q
} }
QString CppParser::expandMacro(const QString &text) const
{
QSet<QString> usedMacros;
return mPreprocessor.expandMacros(text, usedMacros);
}
const QSet<QString> &CppParser::projectFiles() const const QSet<QString> &CppParser::projectFiles() const
{ {
return mProjectFiles; return mProjectFiles;

View File

@ -274,10 +274,6 @@ private:
const QString& phrase, const QString& phrase,
int index, int index,
const PStatement& currentScope) const; const PStatement& currentScope) const;
void fillListOfFunctions(const QString& fileName, int line,
const PStatement& statement,
const PStatement& scopeStatement, QStringList& list);
QList<PStatement> getListOfFunctions(const QString& fileName, int line, QList<PStatement> getListOfFunctions(const QString& fileName, int line,
const PStatement& statement, const PStatement& statement,
const PStatement& scopeStatement) const; const PStatement& scopeStatement) const;
@ -685,6 +681,8 @@ private:
void parseCommandTypeAndArgs(QString& command, void parseCommandTypeAndArgs(QString& command,
QString& typeSuffix, QString& typeSuffix,
QString& args) const; QString& args) const;
QString expandMacro(const QString& text) const;
private: private:
int mParserId; int mParserId;
ParserLanguage mLanguage; ParserLanguage mLanguage;

View File

@ -515,29 +515,29 @@ void CppPreprocessor::handleUndefine(const QString &line)
} }
} }
QString CppPreprocessor::expandMacros(const QString &line, QSet<QString> usedMacros) QString CppPreprocessor::expandMacros(const QString &text, QSet<QString> usedMacros) const
{ {
QString word; QString word;
QString newLine; QString newLine;
int lenLine = line.length(); int lenLine = text.length();
int i=0; int i=0;
while (i< lenLine) { while (i< lenLine) {
QChar ch=line[i]; QChar ch=text[i];
if (isWordChar(ch)) { if (isWordChar(ch)) {
word += ch; word += ch;
} else { } else {
if (!word.isEmpty()) { if (!word.isEmpty()) {
expandMacro(line,newLine,word,i,usedMacros); expandMacro(text,newLine,word,i,usedMacros);
} }
word = ""; word = "";
if (i< lenLine) { if (i< lenLine) {
newLine += line[i]; newLine += text[i];
} }
} }
i++; i++;
} }
if (!word.isEmpty()) { if (!word.isEmpty()) {
expandMacro(line,newLine,word,i, usedMacros); expandMacro(text,newLine,word,i, usedMacros);
} }
return newLine; return newLine;
} }
@ -574,29 +574,29 @@ QString CppPreprocessor::expandMacros()
return newLine; return newLine;
} }
void CppPreprocessor::expandMacro(const QString &line, QString &newLine, QString &word, int &i, QSet<QString> usedMacros) void CppPreprocessor::expandMacro(const QString &text, QString &newText, const QString &word, int &i, QSet<QString> usedMacros) const
{ {
if (usedMacros.contains(word)) if (usedMacros.contains(word))
return; return;
int lenLine = line.length(); int lenLine = text.length();
PDefine define = getDefine(word); PDefine define = getDefine(word);
if (define && define->args=="" ) { if (define && define->args=="" ) {
usedMacros.insert(word); usedMacros.insert(word);
if (define->value != word ) { if (define->value != word ) {
newLine += expandMacros(define->value,usedMacros); newText += expandMacros(define->value,usedMacros);
} else } else
newLine += word; newText += word;
} else if (define && (define->args!="")) { } else if (define && (define->args!="")) {
while ((i<lenLine) && (line[i] == ' ' || line[i]=='\t')) while ((i<lenLine) && (text[i] == ' ' || text[i]=='\t'))
i++; i++;
int argStart=-1; int argStart=-1;
int argEnd=-1; int argEnd=-1;
if ((i<lenLine) && (line[i]=='(')) { if ((i<lenLine) && (text[i]=='(')) {
argStart =i+1; argStart =i+1;
int level=0; int level=0;
bool inString=false; bool inString=false;
while (i<lenLine) { while (i<lenLine) {
switch(line[i].unicode()) { switch(text[i].unicode()) {
case '\\': case '\\':
if (inString) if (inString)
i++; i++;
@ -618,19 +618,19 @@ void CppPreprocessor::expandMacro(const QString &line, QString &newLine, QString
} }
if (level==0) { if (level==0) {
argEnd = i-2; argEnd = i-2;
QString args = line.mid(argStart,argEnd-argStart+1).trimmed(); QString args = text.mid(argStart,argEnd-argStart+1).trimmed();
QString formattedValue = expandFunction(define,args); QString formattedValue = expandFunction(define,args);
usedMacros.insert(word); usedMacros.insert(word);
newLine += expandMacros(formattedValue,usedMacros); newText += expandMacros(formattedValue,usedMacros);
} }
} }
} else { } else {
newLine += word; newText += word;
} }
// } // }
} }
void CppPreprocessor::expandMacro(QString &newLine, QString &word, int &i, QSet<QString> usedMacros) void CppPreprocessor::expandMacro(QString &newLine, const QString &word, int &i, QSet<QString> usedMacros)
{ {
if (usedMacros.contains(word)) if (usedMacros.contains(word))
return; return;
@ -1462,7 +1462,7 @@ bool CppPreprocessor::skipParenthesis(const QString &line, int &index, int step)
return false; return false;
} }
QString CppPreprocessor::expandFunction(PDefine define, QString args) QString CppPreprocessor::expandFunction(PDefine define, const QString &args)
{ {
// Replace function by this string // Replace function by this string
QString result = define->formatValue; QString result = define->formatValue;
@ -1523,7 +1523,6 @@ QString CppPreprocessor::expandFunction(PDefine define, QString args)
|| (define->varArgIndex!=-1 && argValues.length() < define->argUsed.length()-1) || (define->varArgIndex!=-1 && argValues.length() < define->argUsed.length()-1)
) { ) {
qDebug()<<"*** Expand Macro error ***"; qDebug()<<"*** Expand Macro error ***";
qDebug()<<this->mFileName<<":"<<this->mIndex;
qDebug()<<"Macro: "<<define->name<<define->args; qDebug()<<"Macro: "<<define->name<<define->args;
qDebug()<<"Actual param: "<<args; qDebug()<<"Actual param: "<<args;
qDebug()<<"Params splitted: "<<argValues; qDebug()<<"Params splitted: "<<argValues;

View File

@ -80,6 +80,13 @@ public:
void clearProjectIncludePaths(); void clearProjectIncludePaths();
void removeScannedFile(const QString& filename); void removeScannedFile(const QString& filename);
PDefine getDefine(const QString& name) const{
return mDefines.value(name,PDefine());
}
QString expandMacros(const QString& text, QSet<QString> usedMacros) const;
void expandMacro(const QString &text, QString &newText, const QString &word, int &i, QSet<QString> usedMacros) const;
const QStringList& result() const{ const QStringList& result() const{
return mResult; return mResult;
}; };
@ -126,6 +133,7 @@ private:
parentIsFalse parentIsFalse
}; };
static QString expandFunction(PDefine define,const QString &args);
void preprocessBuffer(); void preprocessBuffer();
void skipToEndOfPreprocessor(); void skipToEndOfPreprocessor();
void skipToPreprocessor(); void skipToPreprocessor();
@ -135,15 +143,11 @@ private:
void handleInclude(const QString& line, bool fromNext=false); void handleInclude(const QString& line, bool fromNext=false);
void handlePreprocessor(const QString& value); void handlePreprocessor(const QString& value);
void handleUndefine(const QString& line); void handleUndefine(const QString& line);
QString expandMacros(const QString& line, QSet<QString> usedMacros);
QString expandMacros(); QString expandMacros();
void expandMacro(const QString& line, QString& newLine, QString& word, int& i, QSet<QString> usedMacros); void expandMacro(QString &newLine, const QString &word, int& i, QSet<QString> usedMacros);
void expandMacro(QString& newLine, QString& word, int& i, QSet<QString> usedMacros);
QString removeGCCAttributes(const QString& line); QString removeGCCAttributes(const QString& line);
void removeGCCAttribute(const QString&line, QString& newLine, int &i, const QString& word); 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 // current file stuff
PParsedFile getInclude(int index) const { PParsedFile getInclude(int index) const {
return mIncludes[index]; return mIncludes[index];
@ -235,7 +239,6 @@ static bool isNumberChar(const QChar& ch);
bool evaluateIf(const QString& line); bool evaluateIf(const QString& line);
QString expandDefines(QString line); QString expandDefines(QString line);
bool skipParenthesis(const QString&line, int& index, int step = 1); bool skipParenthesis(const QString&line, int& index, int step = 1);
QString expandFunction(PDefine define,QString args);
bool skipSpaces(const QString &expr, int& pos); bool skipSpaces(const QString &expr, int& pos);
bool evalNumber(const QString &expr, int& result, int& pos); bool evalNumber(const QString &expr, int& result, int& pos);
bool evalTerm(const QString &expr, int& result, int& pos); bool evalTerm(const QString &expr, int& result, int& pos);