xref: /freebsd-src/contrib/llvm-project/llvm/lib/Demangle/DLangDemangle.cpp (revision 5e801ac66d24704442eba426ed13c3effb8a34e7)
1 //===--- DLangDemangle.cpp ------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file defines a demangler for the D programming language as specified
11 /// in the ABI specification, available at:
12 /// https://dlang.org/spec/abi.html#name_mangling
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/Demangle/Demangle.h"
17 #include "llvm/Demangle/Utility.h"
18 
19 #include <cstring>
20 
21 using namespace llvm;
22 using llvm::itanium_demangle::OutputBuffer;
23 
24 char *llvm::dlangDemangle(const char *MangledName) {
25   if (MangledName == nullptr || strncmp(MangledName, "_D", 2) != 0)
26     return nullptr;
27 
28   OutputBuffer Demangled;
29   if (!initializeOutputBuffer(nullptr, nullptr, Demangled, 1024))
30     return nullptr;
31 
32   if (strcmp(MangledName, "_Dmain") == 0)
33     Demangled << "D main";
34 
35   // OutputBuffer's internal buffer is not null terminated and therefore we need
36   // to add it to comply with C null terminated strings.
37   if (Demangled.getCurrentPosition() > 0) {
38     Demangled << '\0';
39     Demangled.setCurrentPosition(Demangled.getCurrentPosition() - 1);
40     return Demangled.getBuffer();
41   }
42 
43   free(Demangled.getBuffer());
44   return nullptr;
45 }
46