Skip to content

Commit

Permalink
[dlang] Add a simple standalone ddemangle program
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuclaw committed May 17, 2020
1 parent 4bfc176 commit ddb55d1
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions libiberty/d-demangle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1948,3 +1948,67 @@ dlang_demangle (const char *mangled, int option)
return demangled;
}

#ifdef STANDALONE_DEMANGLER

/* Main entry for a demangling filter executable. It will filter
stdin to stdout, replacing any recognized mangled D names with
their demangled equivalents. */

int
main (int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
{
string mangled;
char *s;

string_init (&mangled);

/* Read all of input. */
while (!feof (stdin))
{
/* Pile characters into mangled until we hit one that can't
occur in a mangled name. */
char c = getchar ();

while (!feof (stdin))
{
/* Stop if we encountered a character than cannot possibly occur
in a D mangled name. */
if (!ISIDNUM (c) && c != '.' && c != '$' && !(c & 0x80))
break;

string_appendn (&mangled, &c, 1);
c = getchar ();
}

if (string_length (&mangled) > 0)
{
/* Attempt to demangle. */
string_need (&mangled, 1);
*(mangled.p) = '\0';
s = dlang_demangle (mangled.b, 0);

/* If it worked, print the demangled name. The original text is
instead printed if it might not have been a mangled name. */
if (s != NULL)
{
fputs (s, stdout);
free (s);
}
else
fputs (mangled.b, stdout);

string_setlength (&mangled, 0);
}

/* If we haven't hit EOF yet, we've read one character that
can't occur in a mangled name, so print it out. */
if (!feof (stdin))
putchar (c);
}

string_delete (&mangled);

return 0;
}

#endif /* STANDALONE_DEMANGLER */

0 comments on commit ddb55d1

Please sign in to comment.