[MD settings] moving attached() code
[chromium-blink-merge.git] / tools / clang / plugins / tests / test.sh
blob61bee99f82f229c44a58696e0c9537450ec11a81
1 #!/bin/bash
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.
7 # Hacky, primitive testing: This runs the style plugin for a set of input files
8 # and compares the output with golden result files.
10 E_BADARGS=65
11 E_FAILEDTEST=1
13 failed_any_test=
15 THIS_DIR="$(dirname "${0}")"
17 # Prints usage information.
18 usage() {
19 echo "Usage: $(basename "${0}")" \
20 "<path to clang>" \
21 "<path to plugin>"
22 echo ""
23 echo " Runs all the libFindBadConstructs unit tests"
24 echo ""
27 # Runs a single test case.
28 do_testcase() {
29 local flags=""
30 if [ -e "${3}" ]; then
31 flags="$(cat "${3}")"
34 # TODO(thakis): Remove once the tests are standalone, http://crbug.com/486559
35 if [[ "$(uname -s)" == "Darwin" ]]; then
36 flags="${flags} -isysroot $(xcrun --show-sdk-path)"
38 if [[ "$(uname -s)" == "Darwin" && "${flags}" != *-target* ]]; then
39 flags="${flags} -stdlib=libstdc++"
42 flags="${flags} -Xclang -plugin-arg-find-bad-constructs \
43 -Xclang with-ast-visitor"
45 local output="$("${CLANG_PATH}" -fsyntax-only -Wno-c++11-extensions \
46 -Wno-inconsistent-missing-override \
47 -isystem ${THIS_DIR}/system \
48 -Xclang -load -Xclang "${PLUGIN_PATH}" \
49 -Xclang -add-plugin -Xclang find-bad-constructs ${flags} ${1} 2>&1)"
50 local diffout="$(echo "${output}" | diff - "${2}")"
51 if [ "${diffout}" = "" ]; then
52 echo "PASS: ${1}"
53 else
54 failed_any_test=yes
55 echo "FAIL: ${1}"
56 echo "Output of compiler:"
57 echo "${output}"
58 cat > ${2}-actual << EOF
59 ${output}
60 EOF
62 echo "Expected output:"
63 cat "${2}"
64 echo
68 # Validate input to the script.
69 if [[ -z "${1}" ]]; then
70 usage
71 exit ${E_BADARGS}
72 elif [[ -z "${2}" ]]; then
73 usage
74 exit ${E_BADARGS}
75 elif [[ ! -x "${1}" ]]; then
76 echo "${1} is not an executable"
77 usage
78 exit ${E_BADARGS}
79 elif [[ ! -f "${2}" ]]; then
80 echo "${2} could not be found"
81 usage
82 exit ${E_BADARGS}
83 else
84 export CLANG_PATH="${1}"
85 export PLUGIN_PATH="${2}"
86 echo "Using clang ${CLANG_PATH}..."
87 echo "Using plugin ${PLUGIN_PATH}..."
89 # The golden files assume that the cwd is this directory. To make the script
90 # work no matter what the cwd is, explicitly cd to there.
91 cd "${THIS_DIR}"
94 for input in *.cpp; do
95 do_testcase "${input}" "${input%cpp}txt" "${input%cpp}flags"
96 done
98 for input in *.c; do
99 do_testcase "${input}" "${input%c}txt" "${input%c}flags"
100 done
102 if [[ "${failed_any_test}" ]]; then
103 exit ${E_FAILEDTEST}