I am trying to save the output of a command to a file. The command is:
clang -Xclang -ast-dump -fsyntax-only main.cpp > output.txt
However the resulting output.txt file when opened (by gedit and jedit on ubuntu) gives me this:
[0;1;32mTranslationUnitDecl[0m[0;33m 0x4192020[0m <[0;33m<invalid sloc>[0m> [0;33m<invalid sloc>[0m
[0;34m|-[0m[0;1;32mTypedefDecl[0m[0;33m 0x4192558[0m <[0;33m<invalid sloc>[0m> [0;33m<invalid sloc>[0m implicit[0;1;36m __int128_t[0m [0;32m'__int128'[0m
[0;34m| `-[0m[0;32mBuiltinType[0m[0;33m 0x4192270[0m [0;32m'__int128'[0m
[0;34m|-[0m[0;1;32mTypedefDecl[0m[0;33m 0x41925b8[0m <[0;33m<invalid sloc>[0m> [0;33m<invalid sloc>[0m implicit[0;1;36m __uint128_t[0m [0;32m'unsigned __int128'[0m
[0;34m| `-[0m[0;32mBuiltinType[0m[0;33m 0x4192290[0m [0;32m'unsigned __int128'[0m
...
When it should really look like this:
TranslationUnitDecl 0x4e46020 <<invalid sloc>> <invalid sloc>
|-TypedefDecl 0x4e46558 <<invalid sloc>> <invalid sloc> implicit __int128_t '__int128'
| `-BuiltinType 0x4e46270 '__int128'
|-TypedefDecl 0x4e465b8 <<invalid sloc>> <invalid sloc> implicit __uint128_t 'unsigned __int128'
| `-BuiltinType 0x4e46290 'unsigned __int128'
...
I thought it might be a problem of encoding, I checked the encoding of the file, file -bi output.txt
which outputs text/plain; charset=us-ascii
.
I thought maybe if I change the encoding to utf-8 the problem would be fixed so I tried this:
clang -Xclang -ast-dump -fsyntax-only main.cpp | iconv -f us-ascii -t UTF-8 > output.txt
but it didn't make a difference.
What can I do to solve this problem?
The problem isn't that I'm trying to view the syntax-highlighted version (I didn't have a problem viewing it in the first place). I need to save the AST generated by clang to a file and then parse it, which would be difficult with the colour information left in.
>
does not generate output, it simply designates to the shell that you wish to put the output of yourclang
command in the given file, rather than then terminal. After that you're viewing it in a manner that does not permit color codes in the same way. If you were tocat
the file it would work as the terminal would take over, and you can makeless
do the same with the-R
flag.column
command confused by ANSI color escapes, Prevent ANSI escape sequences in terminal output, and Why doescat /dev/urandom
break your terminal?