From 146c41dde36634571b97db69ad67259f4c63dc99 Mon Sep 17 00:00:00 2001 From: Walter Bright Date: Thu, 26 Jan 2023 23:02:41 -0800 Subject: [PATCH] fix Issue 23658 - .di generation of variables should turn them into declarations --- compiler/src/dmd/frontend.h | 5 ++++- compiler/src/dmd/hdrgen.d | 19 +++++++++++++++++-- .../test/compilable/extra-files/header1.di | 2 +- .../test/compilable/extra-files/header1i.di | 2 +- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/compiler/src/dmd/frontend.h b/compiler/src/dmd/frontend.h index 5cc035758b8..83beb984dcd 100644 --- a/compiler/src/dmd/frontend.h +++ b/compiler/src/dmd/frontend.h @@ -2812,6 +2812,7 @@ struct HdrGenState final int32_t autoMember; int32_t forStmtInit; int32_t insideFuncBody; + int32_t insideAggregate; bool declstring; EnumDeclaration* inEnumDecl; HdrGenState() : @@ -2823,11 +2824,12 @@ struct HdrGenState final autoMember(), forStmtInit(), insideFuncBody(), + insideAggregate(), declstring(), inEnumDecl() { } - HdrGenState(bool hdrgen, bool ddoc = false, bool fullDump = false, bool fullQual = false, int32_t tpltMember = 0, int32_t autoMember = 0, int32_t forStmtInit = 0, int32_t insideFuncBody = 0, bool declstring = false, EnumDeclaration* inEnumDecl = nullptr) : + HdrGenState(bool hdrgen, bool ddoc = false, bool fullDump = false, bool fullQual = false, int32_t tpltMember = 0, int32_t autoMember = 0, int32_t forStmtInit = 0, int32_t insideFuncBody = 0, int32_t insideAggregate = 0, bool declstring = false, EnumDeclaration* inEnumDecl = nullptr) : hdrgen(hdrgen), ddoc(ddoc), fullDump(fullDump), @@ -2836,6 +2838,7 @@ struct HdrGenState final autoMember(autoMember), forStmtInit(forStmtInit), insideFuncBody(insideFuncBody), + insideAggregate(insideAggregate), declstring(declstring), inEnumDecl(inEnumDecl) {} diff --git a/compiler/src/dmd/hdrgen.d b/compiler/src/dmd/hdrgen.d index 68670d929e5..f670d1f7a0b 100644 --- a/compiler/src/dmd/hdrgen.d +++ b/compiler/src/dmd/hdrgen.d @@ -65,6 +65,7 @@ struct HdrGenState int autoMember; int forStmtInit; int insideFuncBody; + int insideAggregate; bool declstring; // set while declaring alias for string,wstring or dstring EnumDeclaration inEnumDecl; @@ -1406,8 +1407,10 @@ public: buf.writeByte('{'); buf.writenl(); buf.level++; + hgs.insideAggregate++; foreach (s; *d.members) s.accept(this); + hgs.insideAggregate--; buf.level--; buf.writeByte('}'); buf.writenl(); @@ -1428,8 +1431,10 @@ public: buf.writeByte('{'); buf.writenl(); buf.level++; + hgs.insideAggregate++; foreach (s; *d.members) s.accept(this); + hgs.insideAggregate--; buf.level--; buf.writeByte('}'); } @@ -1521,6 +1526,13 @@ public: void visitVarDecl(VarDeclaration v, bool anywritten) { + const bool isextern = hgs.hdrgen && + !hgs.insideFuncBody && + !hgs.tpltMember && + !hgs.insideAggregate && + !(v.storage_class & (STC.manifest | STC.auto_)) && + v.type; + if (anywritten) { buf.writestring(", "); @@ -1528,14 +1540,17 @@ public: } else { - if (stcToBuffer(buf, v.storage_class)) + auto stc = v.storage_class; + if (isextern) + stc |= STC.extern_; + if (stcToBuffer(buf, stc)) buf.writeByte(' '); if (v.type) typeToBuffer(v.type, v.ident, buf, hgs); else buf.writestring(v.ident.toString()); } - if (v._init) + if (v._init && !isextern) { buf.writestring(" = "); auto ie = v._init.isExpInitializer(); diff --git a/compiler/test/compilable/extra-files/header1.di b/compiler/test/compilable/extra-files/header1.di index fa4eb937715..4b29f2c3bbe 100644 --- a/compiler/test/compilable/extra-files/header1.di +++ b/compiler/test/compilable/extra-files/header1.di @@ -277,7 +277,7 @@ void templ(T)(T val) { pragma (msg, "Invalid destination type."); } -static char[] charArray = ['"', '\'']; +static extern char[] charArray; class Point { auto x = 10; diff --git a/compiler/test/compilable/extra-files/header1i.di b/compiler/test/compilable/extra-files/header1i.di index f1b371304f5..0663c3b901c 100644 --- a/compiler/test/compilable/extra-files/header1i.di +++ b/compiler/test/compilable/extra-files/header1i.di @@ -357,7 +357,7 @@ void templ(T)(T val) { pragma (msg, "Invalid destination type."); } -static char[] charArray = ['"', '\'']; +static extern char[] charArray; class Point { auto x = 10;