1#!/usr/bin/env bash
2
3# Copyright 2018 The Fuchsia Authors
4#
5# Use of this source code is governed by a MIT-style
6# license that can be found in the LICENSE file or at
7# https://opensource.org/licenses/MIT
8
9# This script will run the checkers in the Fuchsia module of Clang-tidy on
10# Zircon. It requires either a prebuilt Clang toolchan or a Clang toolchain
11# built from official Clang repository. For instructions on how to obtain a
12# prebuilt toolchain or build the toolchain from scratch, please refer to
13# document at
14# https://fuchsia.googlesource.com/zircon/+/master/docs/getting_started.md
15
16set -eu
17
18# Path to toolchain
19TOOLCHAIN_PREFIX=""
20
21function func_help {
22  echo "USAGE: clang-tidy-zircon <options> [files ...]"
23  echo ""
24  echo "Make sure that you generate the compile_commands.json file with the "
25  echo "-c flag (or USE_CLANG=true if using make directly)."
26  echo "options:"
27  echo ""
28  echo "-p <toolchain prefix>     : path to the directory containing bin/clang"
29  echo "-h                        : for help"
30  exit 1
31}
32
33SOURCE="${BASH_SOURCE[0]}"
34while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
35  SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
36  SOURCE="$(readlink "$SOURCE")"
37  # if $SOURCE was a relative symlink, we need to resolve it relative to the
38  # path where the symlink file was located
39  [[ "$SOURCE" != /* ]] && SOURCE="$SCRIPT_DIR/$SOURCE"
40done
41SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
42ZIRCON_ROOT="$SCRIPT_DIR/.."
43
44# Read args from command line
45while getopts "p:t:hn" opt; do
46  case $opt in
47    p) TOOLCHAIN_PREFIX="$OPTARG";;
48    h) func_help;;
49    \?)
50      echo "Invalid option"
51      func_help
52  esac
53done
54shift $((OPTIND -1))
55
56# Determine the clang prefix
57if [ -z "$TOOLCHAIN_PREFIX" ]; then
58  # User did not provide toolchain prefix
59  # Assume user prefer prebuilt toolchain
60  PREBUILT_DIR="$ZIRCON_ROOT/prebuilt/downloads/clang"
61  if [ ! -d "$PREBUILT_DIR" ]; then
62    echo "Toolchain prefix is not defined and prebuilt toolchain has not yet been downloaded."
63    echo "Abort!"
64    exit -1
65  fi
66  TOOLCHAIN_PREFIX="$PREBUILT_DIR"
67fi
68
69# Change dir to zircon
70cd "$ZIRCON_ROOT"
71# Check the prebuilt clang-tidy binary
72CLANGTIDY="$TOOLCHAIN_PREFIX/bin/clang-tidy"
73if [ ! -f "$CLANGTIDY" ]; then
74  echo "$CLANGTIDY not found in prebuilts."
75  echo "Ensure the toolchain prefix includes the clang-tidy binary."
76  echo ""
77  exit 0
78fi
79
80cd "$ZIRCON_ROOT"
81# Check compile_commands.json
82COMPILE_COMMANDS="$ZIRCON_ROOT/compile_commands.json"
83if [ ! -f "$COMPILE_COMMANDS" ]; then
84  echo "compile_commands.json missing, see "
85  echo "https://fuchsia.googlesource.com/zircon/+/master/docs/editors.md for "
86  echo "instructions on how to generate it (make sure you use the "
87  echo "USE_CLANG=true option when building zircon)."
88  exit -1
89fi
90
91# Due to a bug in clang-tidy (https://bugs.llvm.org/show_bug.cgi?id=34900),
92# the checks have to be specified here instead of in the .clang-tidy file
93# if we're not running clang-analyzer-* checks.
94$CLANGTIDY -checks="-*,fuchsia-*" $@
95