- 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: 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

View File

@ -156,6 +156,14 @@ QList<PStatement> 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<PStatement> CppParser::getListOfFunctions(const QString &fileName, int line, const PStatement &statement, const PStatement &scopeStatement) const
{
QList<PStatement> 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<QString> usedMacros;
return mPreprocessor.expandMacros(text, usedMacros);
}
const QSet<QString> &CppParser::projectFiles() const
{
return mProjectFiles;

View File

@ -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<PStatement> 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;

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 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<QString> usedMacros)
void CppPreprocessor::expandMacro(const QString &text, QString &newText, const QString &word, int &i, QSet<QString> 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<lenLine) && (line[i] == ' ' || line[i]=='\t'))
while ((i<lenLine) && (text[i] == ' ' || text[i]=='\t'))
i++;
int argStart=-1;
int argEnd=-1;
if ((i<lenLine) && (line[i]=='(')) {
if ((i<lenLine) && (text[i]=='(')) {
argStart =i+1;
int level=0;
bool inString=false;
while (i<lenLine) {
switch(line[i].unicode()) {
switch(text[i].unicode()) {
case '\\':
if (inString)
i++;
@ -618,19 +618,19 @@ void CppPreprocessor::expandMacro(const QString &line, QString &newLine, QString
}
if (level==0) {
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);
usedMacros.insert(word);
newLine += expandMacros(formattedValue,usedMacros);
newText += expandMacros(formattedValue,usedMacros);
}
}
} 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))
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()<<this->mFileName<<":"<<this->mIndex;
qDebug()<<"Macro: "<<define->name<<define->args;
qDebug()<<"Actual param: "<<args;
qDebug()<<"Params splitted: "<<argValues;

View File

@ -80,6 +80,13 @@ public:
void clearProjectIncludePaths();
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{
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<QString> usedMacros);
QString expandMacros();
void expandMacro(const QString& line, QString& newLine, QString& word, int& i, QSet<QString> usedMacros);
void expandMacro(QString& newLine, QString& word, int& i, QSet<QString> usedMacros);
void expandMacro(QString &newLine, const QString &word, int& i, QSet<QString> 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);