On OS X, use xcrun (if present) to find the clang to use for static analysis if
no clang can be found relative to the location of scan-build.

Fixes <rdar://problem/11691794>

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@162535 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/scan-build/scan-build b/tools/scan-build/scan-build
index 65c4893..e2efa44 100755
--- a/tools/scan-build/scan-build
+++ b/tools/scan-build/scan-build
@@ -102,12 +102,26 @@
   $ClangSB = Cwd::realpath("$RealBin/clang");
 }
 my $Clang;
+my $howFound = "from path";
 if (!defined $ClangSB || ! -x $ClangSB) {
-  # Default to looking for 'clang' in the path.
-  $Clang = `which clang`;
-  chomp $Clang;
-  if ($Clang eq "") {
-    DieDiag("No 'clang' executable found in path.\n");
+  # Default to looking for 'clang' in the path, or xcrun
+  # on OS X.
+  my $xcrun = `which xcrun`;
+  chomp $xcrun;
+  if ($xcrun ne "") {
+    $Clang = `$xcrun -toolchain XcodeDefault -find clang`;
+    chomp $Clang;  
+    if ($Clang eq "") {
+      DieDiag("No 'clang' executable found by 'xcrun'\n");
+    }
+    $howFound = "found using 'xcrun'";
+  }
+  else {
+    $Clang = `which clang`;
+    chomp $Clang;
+    if ($Clang eq "") {
+      DieDiag("No 'clang' executable found in path\n");
+    }
   }
 }
 else {
@@ -1459,7 +1473,7 @@
 
 if (!defined $ClangSB || ! -x $ClangSB) {
   Diag("'clang' executable not found in '$RealBin/bin'.\n");
-  Diag("Using 'clang' from path: $Clang\n");
+  Diag("Using 'clang' $howFound: $Clang\n");
 }
 
 SetHtmlEnv(\@ARGV, $HtmlDir);