Skip to content

Commit da92ed8

Browse files
committed
[Demangle] Add a few more options to the microsoft demangler
This corresponds to commonly used options to UnDecorateSymbolName within llvm. Add them as hidden options in llvm-undname. MS undname.exe takes numeric flags, corresponding to the UNDNAME_* constants, but instead of hardcoding in mappings for those numbers, just add textual options instead, as it the use of them here is primarily intended for testing. Differential Revision: https://reviews.llvm.org/D68917 llvm-svn: 374865
1 parent bbb8ead commit da92ed8

File tree

6 files changed

+86
-21
lines changed

6 files changed

+86
-21
lines changed

llvm/include/llvm/Demangle/Demangle.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ char *itaniumDemangle(const char *mangled_name, char *buf, size_t *n,
3232
int *status);
3333

3434

35-
enum MSDemangleFlags { MSDF_None = 0, MSDF_DumpBackrefs = 1 << 0 };
35+
enum MSDemangleFlags {
36+
MSDF_None = 0,
37+
MSDF_DumpBackrefs = 1 << 0,
38+
MSDF_NoAccessSpecifier = 1 << 1,
39+
MSDF_NoCallingConvention = 1 << 2,
40+
MSDF_NoReturnType = 1 << 3,
41+
MSDF_NoMemberType = 1 << 4,
42+
};
3643
char *microsoftDemangle(const char *mangled_name, char *buf, size_t *n,
3744
int *status, MSDemangleFlags Flags = MSDF_None);
3845

llvm/include/llvm/Demangle/MicrosoftDemangleNodes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ enum OutputFlags {
7575
OF_Default = 0,
7676
OF_NoCallingConvention = 1,
7777
OF_NoTagSpecifier = 2,
78+
OF_NoAccessSpecifier = 4,
79+
OF_NoMemberType = 8,
80+
OF_NoReturnType = 16,
7881
};
7982

8083
// Types

llvm/lib/Demangle/MicrosoftDemangle.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2346,12 +2346,22 @@ char *llvm::microsoftDemangle(const char *MangledName, char *Buf, size_t *N,
23462346
if (Flags & MSDF_DumpBackrefs)
23472347
D.dumpBackReferences();
23482348

2349+
OutputFlags OF = OF_Default;
2350+
if (Flags & MSDF_NoCallingConvention)
2351+
OF = OutputFlags(OF | OF_NoCallingConvention);
2352+
if (Flags & MSDF_NoAccessSpecifier)
2353+
OF = OutputFlags(OF | OF_NoAccessSpecifier);
2354+
if (Flags & MSDF_NoReturnType)
2355+
OF = OutputFlags(OF | OF_NoReturnType);
2356+
if (Flags & MSDF_NoMemberType)
2357+
OF = OutputFlags(OF | OF_NoMemberType);
2358+
23492359
if (D.Error)
23502360
InternalStatus = demangle_invalid_mangled_name;
23512361
else if (!initializeOutputStream(Buf, N, S, 1024))
23522362
InternalStatus = demangle_memory_alloc_failure;
23532363
else {
2354-
AST->output(S, OF_Default);
2364+
AST->output(S, OF);
23552365
S += '\0';
23562366
if (N != nullptr)
23572367
*N = S.getCurrentPosition();

llvm/lib/Demangle/MicrosoftDemangleNodes.cpp

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -378,24 +378,28 @@ void LiteralOperatorIdentifierNode::output(OutputStream &OS,
378378

379379
void FunctionSignatureNode::outputPre(OutputStream &OS,
380380
OutputFlags Flags) const {
381-
if (FunctionClass & FC_Public)
382-
OS << "public: ";
383-
if (FunctionClass & FC_Protected)
384-
OS << "protected: ";
385-
if (FunctionClass & FC_Private)
386-
OS << "private: ";
387-
388-
if (!(FunctionClass & FC_Global)) {
389-
if (FunctionClass & FC_Static)
390-
OS << "static ";
381+
if (!(Flags & OF_NoAccessSpecifier)) {
382+
if (FunctionClass & FC_Public)
383+
OS << "public: ";
384+
if (FunctionClass & FC_Protected)
385+
OS << "protected: ";
386+
if (FunctionClass & FC_Private)
387+
OS << "private: ";
391388
}
392-
if (FunctionClass & FC_Virtual)
393-
OS << "virtual ";
394389

395-
if (FunctionClass & FC_ExternC)
396-
OS << "extern \"C\" ";
390+
if (!(Flags & OF_NoMemberType)) {
391+
if (!(FunctionClass & FC_Global)) {
392+
if (FunctionClass & FC_Static)
393+
OS << "static ";
394+
}
395+
if (FunctionClass & FC_Virtual)
396+
OS << "virtual ";
397+
398+
if (FunctionClass & FC_ExternC)
399+
OS << "extern \"C\" ";
400+
}
397401

398-
if (ReturnType) {
402+
if (!(Flags & OF_NoReturnType) && ReturnType) {
399403
ReturnType->outputPre(OS, Flags);
400404
OS << " ";
401405
}
@@ -438,7 +442,7 @@ void FunctionSignatureNode::outputPost(OutputStream &OS,
438442
else if (RefQualifier == FunctionRefQualifier::RValueReference)
439443
OS << " &&";
440444

441-
if (ReturnType)
445+
if (!(Flags & OF_NoReturnType) && ReturnType)
442446
ReturnType->outputPost(OS, Flags);
443447
}
444448

@@ -580,19 +584,26 @@ void FunctionSymbolNode::output(OutputStream &OS, OutputFlags Flags) const {
580584
}
581585

582586
void VariableSymbolNode::output(OutputStream &OS, OutputFlags Flags) const {
587+
const char *AccessSpec = nullptr;
588+
bool IsStatic = true;
583589
switch (SC) {
584590
case StorageClass::PrivateStatic:
585-
OS << "private: static ";
591+
AccessSpec = "private";
586592
break;
587593
case StorageClass::PublicStatic:
588-
OS << "public: static ";
594+
AccessSpec = "public";
589595
break;
590596
case StorageClass::ProtectedStatic:
591-
OS << "protected: static ";
597+
AccessSpec = "protected";
592598
break;
593599
default:
600+
IsStatic = false;
594601
break;
595602
}
603+
if (!(Flags & OF_NoAccessSpecifier) && AccessSpec)
604+
OS << AccessSpec << ": ";
605+
if (!(Flags & OF_NoMemberType) && IsStatic)
606+
OS << "static ";
596607

597608
if (Type) {
598609
Type->outputPre(OS, Flags);

llvm/test/Demangle/ms-options.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
; RUN: llvm-undname < %s | FileCheck %s
2+
; RUN: llvm-undname --no-calling-convention < %s | FileCheck %s --check-prefix=CHECK-NO-CALLING-CONV
3+
; RUN: llvm-undname --no-return-type < %s | FileCheck %s --check-prefix=CHECK-NO-RETURN
4+
; RUN: llvm-undname --no-access-specifier < %s | FileCheck %s --check-prefix=CHECK-NO-ACCESS
5+
; RUN: llvm-undname --no-member-type < %s | FileCheck %s --check-prefix=CHECK-NO-MEMBER-TYPE
6+
; RUN: llvm-undname --no-calling-convention --no-return-type --no-access-specifier --no-member-type < %s | FileCheck %s --check-prefix=CHECK-NO-ALL
7+
8+
?func@MyClass@@UEAAHHH@Z
9+
; CHECK: public: virtual int __cdecl MyClass::func(int, int)
10+
; CHECK-NO-CALLING-CONV: public: virtual int MyClass::func(int, int)
11+
; CHECK-NO-RETURN: public: virtual __cdecl MyClass::func(int, int)
12+
; CHECK-NO-ACCESS: {{^}}virtual int __cdecl MyClass::func(int, int)
13+
; CHECK-NO-MEMBER-TYPE: public: int __cdecl MyClass::func(int, int)
14+
; CHECK-NO-ALL: {{^}}MyClass::func(int, int)

llvm/tools/llvm-undname/llvm-undname.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ using namespace llvm;
3131
cl::opt<bool> DumpBackReferences("backrefs", cl::Optional,
3232
cl::desc("dump backreferences"), cl::Hidden,
3333
cl::init(false));
34+
cl::opt<bool> NoAccessSpecifier("no-access-specifier", cl::Optional,
35+
cl::desc("skip access specifiers"), cl::Hidden,
36+
cl::init(false));
37+
cl::opt<bool> NoCallingConvention("no-calling-convention", cl::Optional,
38+
cl::desc("skip calling convention"),
39+
cl::Hidden, cl::init(false));
40+
cl::opt<bool> NoReturnType("no-return-type", cl::Optional,
41+
cl::desc("skip return types"), cl::Hidden,
42+
cl::init(false));
43+
cl::opt<bool> NoMemberType("no-member-type", cl::Optional,
44+
cl::desc("skip member types"), cl::Hidden,
45+
cl::init(false));
3446
cl::opt<std::string> RawFile("raw-file", cl::Optional,
3547
cl::desc("for fuzzer data"), cl::Hidden);
3648
cl::list<std::string> Symbols(cl::Positional, cl::desc("<input symbols>"),
@@ -41,6 +53,14 @@ static bool msDemangle(const std::string &S) {
4153
MSDemangleFlags Flags = MSDF_None;
4254
if (DumpBackReferences)
4355
Flags = MSDemangleFlags(Flags | MSDF_DumpBackrefs);
56+
if (NoAccessSpecifier)
57+
Flags = MSDemangleFlags(Flags | MSDF_NoAccessSpecifier);
58+
if (NoCallingConvention)
59+
Flags = MSDemangleFlags(Flags | MSDF_NoCallingConvention);
60+
if (NoReturnType)
61+
Flags = MSDemangleFlags(Flags | MSDF_NoReturnType);
62+
if (NoMemberType)
63+
Flags = MSDemangleFlags(Flags | MSDF_NoMemberType);
4464

4565
char *ResultBuf =
4666
microsoftDemangle(S.c_str(), nullptr, nullptr, &Status, Flags);

0 commit comments

Comments
 (0)