diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 4cdd86347a..8566c62632 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -1854,10 +1854,16 @@ proc genVarPrototype(m: BModule, n: PNode) = typ = ptrType(typ) if lfDynamicLib in sym.loc.flags: typ = ptrType(typ) - m.s[cfsVars].addVar(m, sym, - name = sym.loc.snippet, - typ = typ, - visibility = vis) + if sfCodegenDecl in sym.flags: + m.s[cfsVars].addDeclWithVisibility(vis): + m.s[cfsVars].addVar(m, sym, + name = sym.loc.snippet, + typ = typ) + else: + m.s[cfsVars].addVar(m, sym, + name = sym.loc.snippet, + typ = typ, + visibility = vis) if m.hcrOn: m.initProc.procSec(cpsLocals).add('\t') m.initProc.procSec(cpsLocals).addAssignment(sym.loc.snippet, diff --git a/tests/ccgbugs/mcodegendeclglobal.nim b/tests/ccgbugs/mcodegendeclglobal.nim new file mode 100644 index 0000000000..1f2ab904f4 --- /dev/null +++ b/tests/ccgbugs/mcodegendeclglobal.nim @@ -0,0 +1,4 @@ +var codegenDeclGlobal* {.codegenDecl: "$# /* custom declaration */ $#".} = 123 + +proc readCodegenDeclGlobal*(): int {.inline.} = + codegenDeclGlobal diff --git a/tests/ccgbugs/tcodegendeclglobal.nim b/tests/ccgbugs/tcodegendeclglobal.nim new file mode 100644 index 0000000000..b630d948c7 --- /dev/null +++ b/tests/ccgbugs/tcodegendeclglobal.nim @@ -0,0 +1,13 @@ +discard """ + output: ''' +123 +123 +''' + ccodecheck: "'extern NI /* custom declaration */ codegenDeclGlobal'" + targets: "c cpp" +""" + +import ./mcodegendeclglobal + +echo codegenDeclGlobal +echo readCodegenDeclGlobal()