From f0cc2ed42e4798326fe80f3a0241313d3bfd7adc Mon Sep 17 00:00:00 2001 From: David Nadlinger Date: Sat, 23 Apr 2011 17:43:25 +0200 Subject: [PATCH] Merged DMD commit c0d4f02e996e7913f729102a2c07eedcb015ba90: 4878 DDoc: Default arguments can break DDoc output Merged from D2 into D1 --- dmd/doc.c | 32 +++++++++++++++++++++++++++++++- dmd/doc.h | 1 + dmd/mtype.c | 6 ++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/dmd/doc.c b/dmd/doc.c index db0784bb..98705bc5 100644 --- a/dmd/doc.c +++ b/dmd/doc.c @@ -363,6 +363,36 @@ void Module::gendocfile() /**************************************************** * Having unmatched parentheses can hose the output of Ddoc, * as the macros depend on properly nested parentheses. + * This function replaces all ( with $(LPAREN) and ) with $(RPAREN) + * to preserve text literally. This also means macros in the + * text won't be expanded. + */ +void escapeDdocString(OutBuffer *buf, unsigned start) +{ + for (unsigned u = start; u < buf->offset; u++) + { + unsigned char c = buf->data[u]; + switch(c) + { + case '(': + buf->remove(u, 1); //remove the ( + buf->insert(u, "$(LPAREN)", 9); //insert this instead + u += 8; //skip over newly inserted macro + break; + + case ')': + buf->remove(u, 1); //remove the ) + buf->insert(u, "$(RPAREN)", 9); //insert this instead + u += 8; //skip over newly inserted macro + break; + } + } +} + +/**************************************************** + * Having unmatched parentheses can hose the output of Ddoc, + * as the macros depend on properly nested parentheses. + * Fix by replacing unmatched ( with $(LPAREN) and unmatched ) with $(RPAREN). */ void escapeStrayParenthesis(OutBuffer *buf, unsigned start, Loc loc) @@ -852,7 +882,7 @@ void FuncDeclaration::toDocBuffer(OutBuffer *buf) tp->toCBuffer(buf, &hgs); } buf->writeByte(')'); - Parameter::argsToCBuffer(buf, &hgs, tf->parameters, tf->varargs); + Parameter::argsToCBuffer(buf, &hgs, tf ? tf->parameters : NULL, tf ? tf->varargs : 0); buf->writestring(";\n"); highlightCode(NULL, this, buf, o); diff --git a/dmd/doc.h b/dmd/doc.h index 30b74b21..6fe5e99a 100644 --- a/dmd/doc.h +++ b/dmd/doc.h @@ -15,5 +15,6 @@ #pragma once #endif /* __DMC__ */ +void escapeDdocString(OutBuffer *buf, unsigned start); #endif diff --git a/dmd/mtype.c b/dmd/mtype.c index 20859168..f8a96163 100644 --- a/dmd/mtype.c +++ b/dmd/mtype.c @@ -52,6 +52,7 @@ #include "import.h" #include "aggregate.h" #include "hdrgen.h" +#include "doc.h" #if IN_LLVM //#include "gen/tollvm.h" @@ -5714,7 +5715,12 @@ void Parameter::argsToCBuffer(OutBuffer *buf, HdrGenState *hgs, Parameters *argu if (arg->defaultArg) { argbuf.writestring(" = "); + unsigned o = argbuf.offset; arg->defaultArg->toCBuffer(&argbuf, hgs); + if(hgs->ddoc) + { + escapeDdocString(&argbuf, o); + } } buf->write(&argbuf); }