- enhancement: Basic support for parsing variadic macros.

This commit is contained in:
Roy Qu 2023-10-24 20:35:43 +08:00
parent 1c23010408
commit 4eca58b62a
3 changed files with 18 additions and 3 deletions

View File

@ -14,6 +14,7 @@ Red Panda C++ Version 2.26
- fix: Should cd to working directory when debugging. - fix: Should cd to working directory when debugging.
- change: Ensure the line just below caret is visible while moving caret. - change: Ensure the line just below caret is visible while moving caret.
- change: Set mouse cursor to hand pointing when it's on gutter. - 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 Red Panda C++ Version 2.25

View File

@ -946,6 +946,7 @@ void CppPreprocessor::parseArgs(PDefine define)
define->argList[i]=define->argList[i].trimmed(); define->argList[i]=define->argList[i].trimmed();
define->argUsed.append(false); define->argUsed.append(false);
} }
define->varArgIndex=-1;
QList<PDefineArgToken> tokens = tokenizeValue(define->value); QList<PDefineArgToken> tokens = tokenizeValue(define->value);
QString formatStr = ""; QString formatStr = "";
@ -954,7 +955,12 @@ void CppPreprocessor::parseArgs(PDefine define)
foreach (const PDefineArgToken& token, tokens) { foreach (const PDefineArgToken& token, tokens) {
switch(token->type) { switch(token->type) {
case DefineArgTokenType::Identifier: 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) { if (index>=0) {
define->argUsed[index] = true; define->argUsed[index] = true;
if (lastTokenType == DefineArgTokenType::Sharp) { if (lastTokenType == DefineArgTokenType::Sharp) {
@ -1469,14 +1475,21 @@ QString CppPreprocessor::expandFunction(PDefine define, QString args)
i++; i++;
} }
argValues.append(args.mid(lastSplit,i-lastSplit)); argValues.append(args.mid(lastSplit,i-lastSplit));
if (argValues.length() == define->argList.length() if (argValues.length() >= define->argList.length()
&& argValues.length()>0) { && argValues.length()>0) {
QStringList varArgs;
for (int i=0;i<argValues.length();i++) { for (int i=0;i<argValues.length();i++) {
if (define->argUsed[i]) { if (define->varArgIndex != -1
&& i >= define->varArgIndex ) {
varArgs.append(argValues[i].trimmed());
} else if (i<define->argList.length()
&& define->argUsed[i]) {
QString argValue = argValues[i]; QString argValue = argValues[i];
result=result.arg(argValue.trimmed()); result=result.arg(argValue.trimmed());
} }
} }
if (!varArgs.isEmpty())
result=result.arg(varArgs.join(","));
} }
result.replace("%%","%"); result.replace("%%","%");

View File

@ -52,6 +52,7 @@ struct Define {
bool hardCoded;// if true, don't free memory (points to hard defines) bool hardCoded;// if true, don't free memory (points to hard defines)
QStringList argList; // args list to format values QStringList argList; // args list to format values
QList<bool> argUsed; QList<bool> argUsed;
int varArgIndex;
QString formatValue; // format template to format values QString formatValue; // format template to format values
}; };