From 4eca58b62af27c0b3b76f84324dff94e17e0a86d Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Tue, 24 Oct 2023 20:35:43 +0800 Subject: [PATCH] - enhancement: Basic support for parsing variadic macros. --- NEWS.md | 1 + RedPandaIDE/parser/cpppreprocessor.cpp | 19 ++++++++++++++++--- RedPandaIDE/parser/parserutils.h | 1 + 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index c553c4d1..26e82ac4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -14,6 +14,7 @@ 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. Red Panda C++ Version 2.25 diff --git a/RedPandaIDE/parser/cpppreprocessor.cpp b/RedPandaIDE/parser/cpppreprocessor.cpp index 1a1ff350..3dde9da7 100644 --- a/RedPandaIDE/parser/cpppreprocessor.cpp +++ b/RedPandaIDE/parser/cpppreprocessor.cpp @@ -946,6 +946,7 @@ 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 = ""; @@ -954,7 +955,12 @@ void CppPreprocessor::parseArgs(PDefine define) foreach (const PDefineArgToken& token, tokens) { switch(token->type) { case DefineArgTokenType::Identifier: - index = define->argList.indexOf(token->value); + if (token->value == "__VA_ARGS__") { + index = define->argList.indexOf("..."); + define->varArgIndex = index; + } else { + index = define->argList.indexOf(token->value); + } if (index>=0) { define->argUsed[index] = true; if (lastTokenType == DefineArgTokenType::Sharp) { @@ -1469,14 +1475,21 @@ QString CppPreprocessor::expandFunction(PDefine define, QString args) i++; } argValues.append(args.mid(lastSplit,i-lastSplit)); - if (argValues.length() == define->argList.length() + if (argValues.length() >= define->argList.length() && argValues.length()>0) { + QStringList varArgs; for (int i=0;iargUsed[i]) { + if (define->varArgIndex != -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()) + result=result.arg(varArgs.join(",")); } result.replace("%%","%"); diff --git a/RedPandaIDE/parser/parserutils.h b/RedPandaIDE/parser/parserutils.h index ccc4431d..6e8aae8c 100644 --- a/RedPandaIDE/parser/parserutils.h +++ b/RedPandaIDE/parser/parserutils.h @@ -52,6 +52,7 @@ struct Define { bool hardCoded;// if true, don't free memory (points to hard defines) QStringList argList; // args list to format values QList argUsed; + int varArgIndex; QString formatValue; // format template to format values };