Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

ddemangle #2

Merged
merged 7 commits into from

5 participants

@michelf

Reads stdin, demangles mangled D symbols, send result to stdout. Useful to filter output from the linker.

@andralex

Use KeepTerminator.yes here and replace writeln with write? That way you don't produce a spurious \n at end of file if it didn't have one.

@michelf

Fixed: now using File.KeepTerminator.yes and write instead of writeln. Also removed tabs from ddemangle.d.

@klickverbot
Collaborator

Does that handle symbols terminated by end-of-line correctly? I have been using this for quite some time now: https://gist.github.com/1069843 (with KeepTerminator.yes edited in right now having read Andrei's comment above).

@Trass3r

Why does it check for ' ', '"' and '\'' to end the search?
Anything that isn't alphanumeric or _ should terminate it, shouldn't it?
E.g. objdump gives stuff like <_D3std5stdio4File14__T5writeTAyaZ5writeMFAyaZv+0x7a>

@Trass3r

btw, since I didn't know about this pull request before, I implemented yet another demangle tool which reads stdin char-wise, supports demangling names passed on the cmd line and --help.
I think one could also add that cmd line arguments are checked if they are filenames and then process that file instead.

https://github.com/Trass3r/tools/blob/master/dfilt/dfilt.d

@michelf

I added a --help switch, and fixed scanning for end of symbol. I could obviously add more features (demangling names passed as arguments, or reading files), but I suggest we leave that to future pull requests.

@andralex

stderr

@andralex

stderr

@andralex

Use DeMorgan to simplify: if (args.length != 2 || args[1] != "--help" && args[1] != "-h")

Collaborator

I'd rather not remove the braces, imho relying on && binding stronger than || is confusing for other people to read.

@andralex

aren't you looking for isalnum here?

@mrmonday

Once this is merged it'd be cool for rdmd to use it if it's available.

@andralex
Owner

ping?

@michelf

I'll look at this this weekend. Been busy with other things lately.

@michelf

Should be ready for merge now.

@andralex
Owner

This is not about making code to my liking. I think the proposed changes improve the code and its behavior. If you don't think so, you are free to argue your point. Generally you shouldn't feel you're spending time on changes that don't add value.

For one thing, I think you're right about writing help text to stderr, it was worth changing.

For the rest, I wouldn't have made those changes, but they don't bother me either. I'd be a waste of time to start arguing over such triviality.

@andralex andralex merged commit 72dc823 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 78 additions and 0 deletions.
  1. +78 −0 ddemangle.d
View
78 ddemangle.d
@@ -0,0 +1,78 @@
+/**
+ * Demangler filter for D symbols: demangle the first D mangled symbol
+ * found on each line (if any) from standard input and send the
+ * result to standard output.
+ *
+ * Copyright: 2011 Michel Fortin
+ * License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
+ * Author: Michel Fortin
+ */
+/* Copyright Michel Fortin 2011.
+ * Distributed under the Boost Software License, Version 1.0.
+ * (See accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ */
+module ddemangle;
+
+import std.stdio;
+import std.ascii;
+import core.demangle;
+
+int main(string[] args)
+{
+ if (args.length != 1)
+ { // this takes care of the --help / -h case too!
+ stderr.writeln("Usage: ", args[0], " [-h|--help]");
+ stderr.writeln("Demangler filter for D symbols: demangle the first D mangled symbol
+found on each line (if any) from standard input and send the result
+to standard output.");
+ if (args.length != 2 || (args[1] != "--help" && args[1] != "-h"))
+ return 1; // invalid arguments
+ return 0; // help called normally
+ }
+
+ foreach (line; stdin.byLine(File.KeepTerminator.yes))
+ {
+ size_t beginIdx, endIdx;
+
+ enum State { searching_, searchingD, searchingEnd, done }
+ State state;
+ foreach (i, char c; line)
+ {
+ switch (state)
+ {
+ case State.searching_:
+ if (c == '_')
+ {
+ beginIdx = i;
+ state = State.searchingD;
+ }
+ break;
+ case State.searchingD:
+ if (c == 'D')
+ state = State.searchingEnd;
+ else if (c != '_')
+ state = State.searching_;
+ break;
+ case State.searchingEnd:
+ if (!isAlphaNum(c) && c != '_')
+ {
+ endIdx = i;
+ state = State.done;
+ }
+ break;
+ default:
+ break;
+ }
+ if (state == State.done)
+ break;
+ }
+
+ if (endIdx > beginIdx)
+ write(line[0..beginIdx], demangle(line[beginIdx..endIdx]), line[endIdx..$]);
+ else
+ write(line);
+ }
+ return 0;
+}
+
Something went wrong with that request. Please try again.