From 0bc361e1f9a97abd0533541cabbfc7bfaa27147d Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Wed, 25 Oct 2023 00:19:05 +0800 Subject: [PATCH] - enhancement: Better support for expanding macros with complex parameters. --- NEWS.md | 3 +- RedPandaIDE/debugger.cpp | 2 +- RedPandaIDE/parser/cppparser.cpp | 10 ++-- RedPandaIDE/parser/cpppreprocessor.cpp | 83 +++++++++++++++++--------- 4 files changed, 62 insertions(+), 36 deletions(-) diff --git a/NEWS.md b/NEWS.md index 26e82ac4..57f7b0bf 100644 --- a/NEWS.md +++ b/NEWS.md @@ -14,7 +14,8 @@ Red Panda C++ Version 2.26 - fix: Should cd to working directory when debugging. - change: Ensure the line just below caret is visible while moving caret. - change: Set mouse cursor to hand pointing when it's on gutter. - - enhancement: Basic support for parsing variadic macros. + - enhancement: Basic support for parsing variadic macros(macros that use __VA_ARGS__). + - enhancement: Better support for expanding macros with complex parameters. Red Panda C++ Version 2.25 diff --git a/RedPandaIDE/debugger.cpp b/RedPandaIDE/debugger.cpp index bedfdeb2..68f5e4ac 100644 --- a/RedPandaIDE/debugger.cpp +++ b/RedPandaIDE/debugger.cpp @@ -1114,7 +1114,7 @@ void DebugReader::processLogOutput(const QByteArray &line) int p=newLine.lastIndexOf(':'); if (p>0) { newLine=newLine.left(p); - qDebug()<argList; define->formatValue = value; define->hardCoded = hardCoded; + define->varArgIndex = -1; if (!args.isEmpty()) parseArgs(define); if (hardCoded) { @@ -946,7 +947,6 @@ void CppPreprocessor::parseArgs(PDefine define) define->argList[i]=define->argList[i].trimmed(); define->argUsed.append(false); } - define->varArgIndex=-1; QList tokens = tokenizeValue(define->value); QString formatStr = ""; @@ -1448,48 +1448,73 @@ QString CppPreprocessor::expandFunction(PDefine define, QString args) { // Replace function by this string QString result = define->formatValue; - if (args.startsWith('(') && args.endsWith(')')) { - args = args.mid(1,args.length()-2); - } +// if (args.startsWith('(') && args.endsWith(')')) { +// qDebug()<name<argList.length()==0) { + // do nothing + } else if (define->argList.length()==1) { + result=result.arg(args); + } else { + QStringList argValues; + int i=0; + bool inString = false; + bool inChar = false; + int lastSplit=0; + int level=0; + while (i= define->argList.length() - && argValues.length()>0) { - QStringList varArgs; - for (int i=0;ivarArgIndex != -1 - && i >= define->varArgIndex ) { - varArgs.append(argValues[i].trimmed()); - } else if (iargList.length() - && define->argUsed[i]) { - QString argValue = argValues[i]; - result=result.arg(argValue.trimmed()); + argValues.append(args.mid(lastSplit,i-lastSplit)); + if (argValues.length() >= define->argList.length() + && argValues.length()>0) { + QStringList varArgs; + for (int i=0;ivarArgIndex != -1 + && i >= define->varArgIndex ) { + varArgs.append(argValues[i].trimmed()); + } else if (iargList.length() + && define->argUsed[i]) { + QString argValue = argValues[i]; + result=result.arg(argValue.trimmed()); + } + } + if (!varArgs.isEmpty() && define->varArgIndex != -1) { + result=result.arg(varArgs.join(",")); } } - if (!varArgs.isEmpty()) - result=result.arg(varArgs.join(",")); } result.replace("%%","%");