Debian Codegraph @

c++filt.go

raw
1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build ignore
6 // +build ignore
7
8 // This is a program that works like the GNU c++filt program.
9 // It's here for testing purposes and as an example.
10
11 package main
12
13 import (
14 "bufio"
15 "flag"
16 "fmt"
17 "io"
18 "os"
19 "strings"
20 "unicode"
21
22 "github.com/ianlancetaylor/demangle"
23 )
24
25 func flagUsage() {
26 usage(os.Stderr, 2)
27 }
28
29 func usage(w io.Writer, status int) {
30 fmt.Fprintf(w, "Usage: %s [options] [mangled names]\n", os.Args[0])
31 flag.CommandLine.SetOutput(w)
32 flag.PrintDefaults()
33 fmt.Fprintln(w, `Demangled names are displayed to stdout
34 If a name cannot be demangled it is just echoed to stdout.
35 If no names are provided on the command line, stdin is read.`)
36 os.Exit(status)
37 }
38
39 var stripUnderscore = flag.Bool("_", false, "Ignore first leading underscore")
40 var noParams = flag.Bool("p", false, "Do not display function argument types")
41 var noTemplateParams = flag.Bool("T", false, "Do not display template parameters")
42 var noEnclosingParams = flag.Bool("e", false, "Do not display enclosing parameters")
43 var noVerbose = flag.Bool("i", false, "Do not show implementation details (if any)")
44 var help = flag.Bool("h", false, "Display help information")
45 var debug = flag.Bool("d", false, "Display debugging information for strings on command line")
46 var llvm = flag.Bool("llvm", false, "Demangle strings in LLVM style")
47 var maxLen = flag.Int("m", 0, "Maximum length as power of 2, between 1 and 30")
48
49 // Unimplemented c++filt flags:
50 // -n (opposite of -_)
51 // -t (demangle types)
52 // -s (set demangling style)
53 // -V (print version information)
54
55 // Characters considered to be part of a symbol.
56 const symbolChars = "_$."
57
58 func main() {
59 flag.Usage = func() { usage(os.Stderr, 1) }
60 flag.Parse()
61
62 if *help {
63 usage(os.Stdout, 0)
64 }
65
66 out := bufio.NewWriter(os.Stdout)
67
68 if flag.NArg() > 0 {
69 for _, f := range flag.Args() {
70 if *debug {
71 a, err := demangle.ToAST(f, options()...)
72 if err != nil {
73 fmt.Fprintf(os.Stderr, "%s: %v\n", f, err)
74 } else {
75 fmt.Fprintf(out, "%#v\n", a)
76 }
77 } else {
78 doDemangle(out, f)
79 }
80 out.WriteByte('\n')
81 }
82 if err := out.Flush(); err != nil {
83 fmt.Fprintln(os.Stderr, err)
84 os.Exit(2)
85 }
86 return
87 }
88
89 scanner := bufio.NewScanner(bufio.NewReader(os.Stdin))
90 scanner.Buffer(nil, 1<<30)
91 for scanner.Scan() {
92 line := scanner.Text()
93 start := -1
94 for i, c := range line {
95 if unicode.IsLetter(c) || unicode.IsNumber(c) || strings.ContainsRune(symbolChars, c) {
96 if start < 0 {
97 start = i
98 }
99 } else {
100 if start >= 0 {
101 doDemangle(out, line[start:i])
102 }
103 out.WriteRune(c)
104 start = -1
105 }
106 }
107 if start >= 0 {
108 doDemangle(out, line[start:])
109 start = -1
110 }
111 out.WriteByte('\n')
112 if err := out.Flush(); err != nil {
113 fmt.Fprintln(os.Stderr, err)
114 os.Exit(2)
115 }
116 }
117 if err := scanner.Err(); err != nil {
118 fmt.Fprintln(os.Stderr, err)
119 os.Exit(2)
120 }
121 }
122
123 // Demangle a string just as the GNU c++filt program does.
124 func doDemangle(out *bufio.Writer, name string) {
125 skip := 0
126 if name[0] == '.' || name[0] == '$' {
127 skip++
128 }
129 if *stripUnderscore && name[skip] == '_' {
130 skip++
131 }
132 result := demangle.Filter(name[skip:], options()...)
133 if result == name[skip:] {
134 out.WriteString(name)
135 } else {
136 if name[0] == '.' {
137 out.WriteByte('.')
138 }
139 out.WriteString(result)
140 }
141 }
142
143 // options returns the demangling options to use based on the command
144 // line flags.
145 func options() []demangle.Option {
146 var options []demangle.Option
147 if *noParams {
148 options = append(options, demangle.NoParams)
149 }
150 if *noTemplateParams {
151 options = append(options, demangle.NoTemplateParams)
152 }
153 if *noEnclosingParams {
154 options = append(options, demangle.NoEnclosingParams)
155 }
156 if !*noVerbose {
157 options = append(options, demangle.Verbose)
158 }
159 if *llvm {
160 options = append(options, demangle.LLVMStyle)
161 }
162 if *maxLen > 0 {
163 options = append(options, demangle.MaxLength(*maxLen))
164 }
165 return options
166 }