Skip to content

Commit 1d08c4e

Browse files
authored
fixes #25263; provides a new switch mangle:nim/cpp for debug name mangling (#25264)
fixes #25263 - [x] documentation and changelogs
1 parent cfefd1d commit 1d08c4e

File tree

7 files changed

+215
-3
lines changed

7 files changed

+215
-3
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ errors.
2727

2828
- With `-d:nimPreviewDuplicateModuleError`, importing two modules that share the same name becomes a compile-time error. This includes importing the same module more than once. Use `import foo as foo1` (or other aliases) to avoid collisions.
2929

30+
- Adds the switch `--mangle:nim|cpp`, which selects `nim` or `cpp` style name mangling when used with `debuginfo` on, defaults to `nim`. The default is changed from `cpp` to `nim`.
31+
3032
## Standard library additions and changes
3133

3234
[//]: # "Additions:"

compiler/ccgtypes.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ proc mangleProc(m: BModule; s: PSym; makeUnique: bool): string =
7575
proc fillBackendName(m: BModule; s: PSym) =
7676
if s.loc.snippet == "":
7777
var result: Rope
78-
if not m.compileToCpp and s.kind in routineKinds and optCDebug in m.g.config.globalOptions and
78+
if s.kind in routineKinds and {optCDebug, optItaniumMangle} * m.g.config.globalOptions == {optCDebug, optItaniumMangle} and
7979
m.g.config.symbolFiles == disabledSf:
8080
result = mangleProc(m, s, false).rope
8181
else:

compiler/commands.nim

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ proc testCompileOption*(conf: ConfigRef; switch: string, info: TLineInfo): bool
364364
result = false
365365
of "panics": result = contains(conf.globalOptions, optPanics)
366366
of "jsbigint64": result = contains(conf.globalOptions, optJsBigInt64)
367+
of "mangle": result = contains(conf.globalOptions, optItaniumMangle)
367368
else:
368369
result = false
369370
invalidCmdLineOption(conf, passCmd1, switch, info)
@@ -762,6 +763,14 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
762763
conf.globalOptions.excl optCDebug
763764
else:
764765
localError(conf, info, "expected native|gdb|on|off but found " & arg)
766+
of "mangle":
767+
case arg.normalize
768+
of "nim":
769+
conf.globalOptions.excl optItaniumMangle
770+
of "cpp":
771+
conf.globalOptions.incl optItaniumMangle
772+
else:
773+
localError(conf, info, "expected nim|cpp but found " & arg)
765774
of "g": # alias for --debugger:native
766775
conf.globalOptions.incl optCDebug
767776
conf.options.incl optLineDir

compiler/options.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ type # please make sure we have under 32 options
110110
optEnableDeepCopy # ORC specific: enable 'deepcopy' for all types.
111111
optShowNonExportedFields # for documentation: show fields that are not exported
112112
optJsBigInt64 # use bigints for 64-bit integers in JS
113+
optItaniumMangle # mangling follows the Itanium spec
113114

114115
TGlobalOptions* = set[TGlobalOption]
115116

doc/advopt.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Advanced options:
9191
--os:SYMBOL set the target operating system (cross-compilation)
9292
--cpu:SYMBOL set the target processor (cross-compilation)
9393
--debuginfo:on|off enables debug information
94+
--mangle:nim|cpp selects `nim` or `cpp` style name mangling, defaults to `nim`
9495
-t, --passC:OPTION pass an option to the C compiler
9596
-l, --passL:OPTION pass an option to the linker
9697
--cc:SYMBOL specify the C compiler

tests/codegen/titaniummangle.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
discard """
2-
targets: "c"
3-
matrix: "--debugger:native"
2+
targets: "c cpp"
3+
matrix: "--debugger:native --mangle:cpp"
44
ccodecheck: "'_ZN14titaniummangle8testFuncE'"
55
ccodecheck: "'_ZN14titaniummangle8testFuncE6stringN14titaniummangle3FooE'"
66
ccodecheck: "'_ZN14titaniummangle8testFuncE3int7varargsI6stringE'"
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
discard """
2+
targets: "c"
3+
matrix: "--debugger:native --mangle:nim; --debugger:native"
4+
ccodecheck: "'testFunc__titaniummangle95nim_u1316'"
5+
ccodecheck: "'testFunc__titaniummangle95nim_u156'"
6+
ccodecheck: "'testFunc__titaniummangle95nim_u1305'"
7+
ccodecheck: "'testFunc__titaniummangle95nim_u241'"
8+
ccodecheck: "'testFunc__titaniummangle95nim_u1357'"
9+
ccodecheck: "'testFunc__titaniummangle95nim_u292'"
10+
ccodecheck: "'testFunc__titaniummangle95nim_u38'"
11+
ccodecheck: "'testFunc__titaniummangle95nim_u175'"
12+
ccodecheck: "'testFunc__titaniummangle95nim_u1302'"
13+
ccodecheck: "'testFunc__titaniummangle95nim_u1305'"
14+
ccodecheck: "'testFunc__titaniummangle95nim_u535'"
15+
ccodecheck: "'testFunc__titaniummangle95nim_u1294'"
16+
ccodecheck: "'testFunc__titaniummangle95nim_u336'"
17+
ccodecheck: "'testFunc__titaniummangle95nim_u425'"
18+
ccodecheck: "'testFunc__titaniummangle95nim_u308'"
19+
ccodecheck: "'testFunc__titaniummangle95nim_u129'"
20+
ccodecheck: "'testFunc__titaniummangle95nim_u320'"
21+
ccodecheck: "'testFunc__titaniummangle95nim_u223'"
22+
ccodecheck: "'testFunc__titaniummangle95nim_u545'"
23+
ccodecheck: "'testFunc__titaniummangle95nim_u543'"
24+
ccodecheck: "'testFunc__titaniummangle95nim_u895'"
25+
ccodecheck: "'testFunc__titaniummangle95nim_u1104'"
26+
ccodecheck: "'testFunc__titaniummangle95nim_u1155'"
27+
ccodecheck: "'testFunc__titaniummangle95nim_u636'"
28+
ccodecheck: "'testFunc__titaniummangle95nim_u705'"
29+
ccodecheck: "'testFunc__titaniummangle95nim_u800'"
30+
ccodecheck: "'new__titaniummangle95nim_u1320'"
31+
ccodecheck: "'xxx__titaniummangle95nim_u1391'"
32+
ccodecheck: "'xxx__titaniummangle95nim_u1394'"
33+
"""
34+
35+
#When debugging this notice that if one check fails, it can be due to any of the above.
36+
37+
type
38+
Comparable = concept x, y
39+
(x < y) is bool
40+
41+
Foo = object
42+
a: int32
43+
b: int32
44+
45+
FooTuple = tuple
46+
a: int
47+
b: int
48+
49+
Container[T] = object
50+
data: T
51+
52+
Container2[T, T2] = object
53+
data: T
54+
data2: T2
55+
56+
Boo = distinct Foo
57+
58+
Coo = Foo
59+
60+
Doo = Boo | Foo
61+
62+
TestProc = proc(a:string): string
63+
64+
type EnumSample = enum
65+
a, b, c
66+
67+
type EnumAnotherSample = enum
68+
a, b, c
69+
70+
proc testFunc(a: set[EnumSample]) =
71+
echo $a
72+
73+
proc testFunc(a: typedesc) =
74+
echo $a
75+
76+
proc testFunc(a: ptr Foo) =
77+
echo repr a
78+
79+
proc testFunc(s: string, a: Coo) =
80+
echo repr a
81+
82+
proc testFunc(s: int, a: Comparable) =
83+
echo repr a
84+
85+
proc testFunc(a: TestProc) =
86+
let b = ""
87+
echo repr a("")
88+
89+
proc testFunc(a: ref Foo) =
90+
echo repr a
91+
92+
proc testFunc(b: Boo) =
93+
echo repr b
94+
95+
proc testFunc(a: ptr UncheckedArray[int]) =
96+
echo repr a
97+
98+
proc testFunc(a: ptr int) =
99+
echo repr a
100+
101+
proc testFunc(a: ptr ptr int) =
102+
echo repr a
103+
104+
proc testFunc(e: FooTuple, str: cstring) =
105+
echo e
106+
107+
proc testFunc(e: (float, float)) =
108+
echo e
109+
110+
proc testFunc(e: EnumSample) =
111+
echo e
112+
113+
proc testFunc(e: var int) =
114+
echo e
115+
116+
proc testFunc(e: var Foo, a, b: int32, refFoo: ref Foo) =
117+
echo e
118+
119+
proc testFunc(xs: Container[int]) =
120+
let a = 2
121+
echo xs
122+
123+
proc testFunc(xs: Container2[int32, int32]) =
124+
let a = 2
125+
echo xs
126+
127+
proc testFunc(xs: Container[Container2[int32, int32]]) =
128+
let a = 2
129+
echo xs
130+
131+
proc testFunc(xs: seq[int]) =
132+
let a = 2
133+
echo xs
134+
135+
proc testFunc(xs: openArray[string]) =
136+
let a = 2
137+
echo xs
138+
139+
proc testFunc(xs: array[2, int]) =
140+
let a = 2
141+
echo xs
142+
143+
proc testFunc(e: EnumAnotherSample) =
144+
echo e
145+
146+
proc testFunc(a, b: int) =
147+
echo "hola"
148+
discard
149+
150+
proc testFunc(a: int, xs: varargs[string]) =
151+
let a = 10
152+
for x in xs:
153+
echo x
154+
155+
proc xxx(v: static int) =
156+
echo v
157+
158+
proc testFunc() =
159+
var a = 2
160+
var aPtr = a.addr
161+
var foo = Foo()
162+
let refFoo : ref Foo = new(Foo)
163+
let b = Foo().Boo()
164+
let d: Doo = Foo()
165+
testFunc("", Coo())
166+
testFunc(1, )
167+
testFunc(b)
168+
testFunc(EnumAnotherSample)
169+
var t = [1, 2]
170+
let uArr = cast[ptr UncheckedArray[int]](t.addr)
171+
testFunc(uArr)
172+
testFunc({})
173+
testFunc(proc(s:string): string = "test")
174+
testFunc(20, a.int32)
175+
testFunc(20, 2)
176+
testFunc(EnumSample.c)
177+
testFunc(EnumAnotherSample.c)
178+
testFunc((2, 1), "adios")
179+
testFunc((22.1, 1.2))
180+
testFunc(a.addr)
181+
testFunc(foo.addr)
182+
testFunc(aPtr.addr)
183+
testFunc(refFoo)
184+
testFunc(foo, 2, 1, refFoo)
185+
testFunc(a)
186+
testFunc(@[2, 1, 2])
187+
testFunc(@["hola"])
188+
testFunc(2, "hola", "adios")
189+
let arr: array[2, int] = [2, 1]
190+
testFunc(arr)
191+
testFunc(Container[int](data: 10))
192+
let c2 = Container2[int32, int32](data: 10, data2: 20)
193+
testFunc(c2)
194+
testFunc(Container[Container2[int32, int32]](data: c2))
195+
xxx(10)
196+
xxx(20)
197+
198+
199+
testFunc()

0 commit comments

Comments
 (0)