• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2#
3# Copyright (c) 2011 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6#
7# Hacky, primitive testing: This runs the style plugin for a set of input files
8# and compares the output with golden result files.
9
10E_BADARGS=65
11E_FAILEDTEST=1
12
13failed_any_test=
14
15# Prints usage information.
16usage() {
17  echo "Usage: $(basename "${0}")" \
18    "<Path to the llvm build dir, usually Release+Asserts>"
19  echo ""
20  echo "  Runs all the libFindBadConstructs unit tests"
21  echo ""
22}
23
24# Runs a single test case.
25do_testcase() {
26  local flags=""
27  if [ -e "${3}" ]; then
28    flags="$(cat "${3}")"
29  fi
30
31  if [ "$(uname -s)" = "Darwin" ]; then
32    flags="${flags} -isysroot $(xcrun --show-sdk-path) -stdlib=libstdc++"
33  fi
34
35  local output="$("${CLANG_DIR}"/bin/clang -c -Wno-c++11-extensions \
36      -Xclang -load -Xclang "${CLANG_DIR}"/lib/libFindBadConstructs.${LIB} \
37      -Xclang -add-plugin -Xclang find-bad-constructs ${flags} ${1} 2>&1)"
38  local diffout="$(echo "${output}" | diff - "${2}")"
39  if [ "${diffout}" = "" ]; then
40    echo "PASS: ${1}"
41  else
42    failed_any_test=yes
43    echo "FAIL: ${1}"
44    echo "Output of compiler:"
45    echo "${output}"
46    echo "Expected output:"
47    cat "${2}"
48    echo
49  fi
50}
51
52# Validate input to the script.
53if [[ -z "${1}" ]]; then
54  usage
55  exit ${E_BADARGS}
56elif [[ ! -d "${1}" ]]; then
57  echo "${1} is not a directory."
58  usage
59  exit ${E_BADARGS}
60else
61  export CLANG_DIR="${PWD}/${1}"
62  echo "Using clang directory ${CLANG_DIR}..."
63
64  # The golden files assume that the cwd is this directory. To make the script
65  # work no matter what the cwd is, explicitly cd to there.
66  cd "$(dirname "${0}")"
67
68  if [ "$(uname -s)" = "Linux" ]; then
69    export LIB=so
70  elif [ "$(uname -s)" = "Darwin" ]; then
71    export LIB=dylib
72  fi
73fi
74
75for input in *.cpp; do
76  do_testcase "${input}" "${input%cpp}txt" "${input%cpp}flags"
77done
78
79for input in *.c; do
80  do_testcase "${input}" "${input%c}txt" "${input%c}flags"
81done
82
83if [[ "${failed_any_test}" ]]; then
84  exit ${E_FAILEDTEST}
85fi
86