Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get clang to dump the AST without color?

Using clang-check to dump a source code's AST, can be done with the following command:

$ clang-check -ast-dump file.c --

However, the output of this command will appear colorful within the terminal.
When I direct the output to a file, I'm stuck with all of the color escape codes:

$ clang-check -ast-dump file.c -- > out.txt  

example:

[0;1;32mTranslationUnitDecl[0m[0;33m 0x227c5c0[0m <[0;33m<invalid sloc>[0m> [0;33m<invalid sloc>[0m
[0;34m|-[0m[0;1;32mTypedefDecl[0m[0;33m 0x227cac0[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;1;32mTypedefDecl[0m[0;33m 0x227cb20[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;1;32mTypedefDecl[0m[0;33m 0x227ce70[0m <[0;33m<invalid sloc>[0m> [0;33m<invalid sloc>[0m implicit[0;1;36m __builtin_va_list[0m [0;32m'__va_list_tag [1]'[0m
...

Is there a flag to disable colors in clang-check?
I tried adding the following flag, but it did not work:

--extra-arg="--no-color-diagnostics"
like image 990
Trevor Hickey Avatar asked Sep 08 '15 00:09

Trevor Hickey


People also ask

What is AST in clang?

Clang's AST nodes are modeled on a class hierarchy that does not have a common ancestor. Instead, there are multiple larger hierarchies for basic node types like Decl and Stmt. Many important AST nodes derive from Type, Decl, DeclContext or Stmt, with some classes deriving from both Decl and DeclContext.

What is AST dump?

The ast. dump() method returns a formatted string of the tree structure in a tree. The visit method available to the visitor object visits all the nodes in the tree structure.

What is clang check?

ClangCheck is a small wrapper around LibTooling which can be used to do basic error checking and AST dumping.

What is C++ AST?

An AST is essentially the same thing, a tree-like diagram of the meaningful content of a program. The part of the compiler responsible for producing the AST is called the front-end. That's where all the grammatical rules of C++ are interpreted and applied to the incoming source code.


1 Answers

You are almost correct. Try

$ clang-check -ast-dump test.c --extra-arg="-fno-color-diagnostics" -- 

Additionally, -fno-diagnostics-color and -fdiagnostics-color=never also seems to work

Reference: http://clang.llvm.org/docs/UsersManual.html#formatting-of-diagnostics

like image 53
ymonad Avatar answered Oct 18 '22 05:10

ymonad