kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/docs/schema/example-objc.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  #   VERIFIER_ARGS
    29  #   SHASUM_TOOL
    30  #   SHOWGRAPH
    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 ObjC 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.m in "$SRCS/filename".
    42  export TEST_MAIN="$SRCS/test.m"
    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 for C++.
    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 C++ 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.m";
    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="-fblocks $(cat "$ARGS_FILE")"
    83  
    84  for TEST_M in "${SRCS}"/*.m
    85  do
    86    # CXX_ARGS is intentionally unquoted
    87    # shellcheck disable=SC2086
    88    "$CXX_INDEXER_BIN" --ignore_unimplemented=false -i "${TEST_M}" -- $CXX_ARGS \
    89        >> "${TEST_ENTRIES}"
    90  done
    91  "$VERIFIER_BIN" "${VERIFIER_ARGS}" --ignore_dups "${SRCS}"/* < "${TEST_ENTRIES}"
    92  
    93  trap 'error FORMAT' ERR
    94  EXAMPLE_ID=$($SHASUM_TOOL "$RAW_EXAMPLE" | cut -c 1-64)
    95  
    96  if [[ -n "${DIV_STYLE}" ]]; then
    97    echo "<div style=\"${DIV_STYLE}\">"
    98  else
    99    echo "<div>"
   100  fi
   101  
   102  echo "<h5 id=\"_${LABEL}\">${LABEL}"
   103  
   104  if [[ "${SHOWGRAPH}" == 1 ]]; then
   105    "$VERIFIER_BIN" --ignore_dups --graphviz < "${TEST_ENTRIES}" > "$TMP/EXAMPLE_ID.dot"
   106    dot -Tsvg -o "$EXAMPLE_ID.svg" "$TMP/EXAMPLE_ID.dot"
   107    echo "(<a href=\"${EXAMPLE_ID}.svg\" target=\"_blank\">${LANGUAGE}</a>)</h5>"
   108  else
   109    echo " (${LANGUAGE})</h5>"
   110  fi
   111  
   112  # I guess cpp?? Objective C doesn't seem to be in the list of possible
   113  # langauges.
   114  source-highlight --failsafe --output=STDOUT --src-lang cpp -i "$RAW_EXAMPLE"
   115  echo "</div>"