| 1 | #!/usr/bin/env bash
|
| 2 | #
|
| 3 | # Abandoned experiment. Might be useful later.
|
| 4 | #
|
| 5 | # Usage:
|
| 6 | # devtools/clang-ast.sh <function name>
|
| 7 |
|
| 8 | set -o nounset
|
| 9 | set -o pipefail
|
| 10 | set -o errexit
|
| 11 |
|
| 12 | REPO_ROOT=$(cd "$(dirname $0)/.."; pwd)
|
| 13 | source build/common.sh
|
| 14 |
|
| 15 | clang-action() {
|
| 16 | local action=$1
|
| 17 | shift
|
| 18 |
|
| 19 | log "*** $action"
|
| 20 |
|
| 21 | # Weird that -ast-list is an argument to -Xclang
|
| 22 | $CLANG_DIR/bin/clang++ -Xclang $action -fsyntax-only "$@"
|
| 23 | }
|
| 24 |
|
| 25 | demo() {
|
| 26 | # What's the difference with clang-query and clang-check?
|
| 27 | # CXX=$CLANG_DIR/bin/clang-query
|
| 28 |
|
| 29 | # These seem useful, but aren't really?
|
| 30 | # -dump-tokens
|
| 31 | # -dump-raw-tokens
|
| 32 |
|
| 33 | # -fno-color-diagnostics
|
| 34 |
|
| 35 | # Crashes?
|
| 36 | #CXX="$CLANG_DIR/bin/clang-check -syntax-tree-dump"
|
| 37 |
|
| 38 | local -a cxx_flags=(
|
| 39 | -std=c++11 -Wall -Wno-invalid-offsetof -D MARK_SWEEP
|
| 40 | -I $PWD # /home/andy/git/oilshell/oil
|
| 41 | )
|
| 42 |
|
| 43 | local out_dir=_tmp/clang-ast
|
| 44 | mkdir -p $out_dir
|
| 45 |
|
| 46 | local src=cpp/leaky_core.cc
|
| 47 | local out_prefix=$out_dir/$(basename $src)
|
| 48 |
|
| 49 |
|
| 50 | # List of function names
|
| 51 | time clang-action -ast-list ${cxx_flags[@]} -c $src > $out_prefix.txt
|
| 52 |
|
| 53 | # AST text
|
| 54 | time clang-action -ast-dump ${cxx_flags[@]} -c $src > $out_prefix.ast
|
| 55 |
|
| 56 | # AST json
|
| 57 | time clang-action -ast-dump=json ${cxx_flags[@]} -c $src > $out_prefix.json
|
| 58 |
|
| 59 | if false; then
|
| 60 | # Tokens go to stderr for some reason
|
| 61 | time clang-action -dump-tokens ${cxx_flags[@]} -c $src 2> $out_prefix.tokens.txt
|
| 62 |
|
| 63 | time clang-action -dump-raw-tokens ${cxx_flags[@]} -c $src 2> $out_prefix.raw-tokens.txt
|
| 64 | fi
|
| 65 |
|
| 66 | # Other flags:
|
| 67 | # -ast-dump-filter
|
| 68 |
|
| 69 | # - 600 KB for list of names
|
| 70 | # - 29M for AST text format
|
| 71 | # - 200M for AST JSON format
|
| 72 |
|
| 73 | ls -l --si $out_dir
|
| 74 |
|
| 75 | # 4.3M lines
|
| 76 | wc -l $out_prefix.json
|
| 77 | }
|
| 78 |
|
| 79 | filter() {
|
| 80 | # 200 MB of JSON takes 7 seconds
|
| 81 | #time jq '.' < _tmp/foo.json | wc -l
|
| 82 |
|
| 83 | # hm doesn't show anything, but takes 3 seconds
|
| 84 | time jq 'select(.kind == "FunctionDecl")' < _tmp/foo.json
|
| 85 | }
|
| 86 |
|
| 87 | help() {
|
| 88 | #$CLANG_DIR/bin/clang-query --help
|
| 89 | #$CLANG_DIR/bin/clang++ --help
|
| 90 |
|
| 91 | $CLANG_DIR/bin/clang++ -cc1 --help
|
| 92 |
|
| 93 | #$CLANG_DIR/bin/clang-check --help
|
| 94 | }
|
| 95 |
|
| 96 | "$@"
|