kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/docs/schema/example-cxx.sh (about) 1 #!/bin/bash -e 2 set -o pipefail 3 4 # Copyright 2016 The Kythe Authors. All rights reserved. 5 # 6 # Licensed under the Apache License, Version 2.0 (the "License"); 7 # you may not use this file except in compliance with the License. 8 # You may obtain a copy of the License at 9 # 10 # http://www.apache.org/licenses/LICENSE-2.0 11 # 12 # Unless required by applicable law or agreed to in writing, software 13 # distributed under the License is distributed on an "AS IS" BASIS, 14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 # See the License for the specific language governing permissions and 16 # limitations under the License. 17 18 # This script verifies and formats a single Kythe example, which is expected 19 # to be piped in on standard input from example.sh. 20 # 21 # The script assumes its working directory is the schema output directory and 22 # requires the following environment variables: 23 # TMP 24 # LANGUAGE 25 # LABEL 26 # CXX_INDEXER_BIN 27 # VERIFIER_BIN 28 # SHASUM_TOOL 29 # SHOWGRAPH 30 # VERIFIER_ARGS 31 32 SRCS="$TMP/example" 33 mkdir "$SRCS" 34 ARGS_FILE="$TMP/args" 35 touch "$ARGS_FILE" 36 37 # This filter assumes that its stdin is a full C++ source file which will be 38 # placed into $TEST_MAIN for compilation/verification. Optionally, after the 39 # main source text, more files can be specified with header lines formatted like 40 # "#example filename". The lines proceeding these header lines will be placed 41 # next to test.cc in "$SRCS/filename". 42 export TEST_MAIN="$SRCS/test.cc" 43 44 # The raw filter input will be placed into this file for later syntax highlighting 45 RAW_EXAMPLE="$TMP/raw.hcc" 46 47 # Test entries will be dropped here. 48 TEST_ENTRIES="$TMP/test.entries" 49 50 # Example filter input: 51 # #include "test.h" 52 # //- @C completes Decl1 53 # //- @C completes Decl2 54 # //- @C defines Defn 55 # class C { }; 56 # 57 # #example test.h 58 # //- @C defines Decl1 59 # class C; 60 # //- @C defines Decl2 61 # class C; 62 # 63 # The above input will generate/verify two files: test.cc and test.h 64 65 # Split collected_files.hcc into files via "#example file.name" delimiter lines. 66 { echo "#example test.cc"; 67 tee "$RAW_EXAMPLE"; 68 } | awk -v argsfile="$ARGS_FILE" -v root="$SRCS/" ' 69 /#example .*/ { 70 x=root $2; 71 next; 72 } 73 74 /#arguments / { 75 $1 = ""; 76 print > argsfile; 77 next; 78 } 79 80 {print > x;}' 81 82 CXX_ARGS="-std=c++1y $(cat "$ARGS_FILE")" 83 84 for TEST_CC in "${SRCS}"/*.cc 85 do 86 # shellcheck disable=SC2086 87 "$CXX_INDEXER_BIN" --ignore_unimplemented=false \ 88 --record_call_directness=true \ 89 -i "${TEST_CC}" -- $CXX_ARGS \ 90 >> "${TEST_ENTRIES}" 91 done 92 "$VERIFIER_BIN" "${VERIFIER_ARGS}" --ignore_dups "${SRCS}"/* < "${TEST_ENTRIES}" 93 94 trap 'error FORMAT' ERR 95 EXAMPLE_ID=$($SHASUM_TOOL "$RAW_EXAMPLE" | cut -c 1-64) 96 97 if [[ -n "${DIV_STYLE}" ]]; then 98 echo "<div style=\"${DIV_STYLE}\">" 99 else 100 echo "<div>" 101 fi 102 103 echo "<h5 id=\"_${LABEL}\">${LABEL}" 104 105 if [[ "${SHOWGRAPH}" == 1 ]]; then 106 "$VERIFIER_BIN" "${VERIFIER_ARGS}" --ignore_dups --graphviz < "${TEST_ENTRIES}" > "$TMP/${EXAMPLE_ID}.dot" 107 dot -Tsvg -o "$EXAMPLE_ID.svg" "$TMP/${EXAMPLE_ID}.dot" 108 echo "(<a href=\"${EXAMPLE_ID}.svg\" target=\"_blank\">${LANGUAGE}</a>)</h5>" 109 else 110 echo " (${LANGUAGE})</h5>" 111 fi 112 113 source-highlight --failsafe --output=STDOUT --src-lang cpp -i "$RAW_EXAMPLE" 114 echo "</div>"