scan-build: Determine the location of the clang binary before printing help.

We need the clang executable to print the list of available checkers.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@163925 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/scan-build/scan-build b/tools/scan-build/scan-build
index 0b7025b..a13b235 100755
--- a/tools/scan-build/scan-build
+++ b/tools/scan-build/scan-build
@@ -1135,73 +1135,74 @@
 }
 
 # Query clang for complete list of checkers.
-pipe(FROM_CHILD, TO_PARENT);
-my $pid = fork();
-if ($pid == 0) {
-  close FROM_CHILD;
-  open(STDOUT,">&", \*TO_PARENT);
-  open(STDERR,">&", \*TO_PARENT);
-  exec $Clang, ('-cc1', @PluginsToLoad , '-analyzer-checker-help');
-}
-close(TO_PARENT);
-my $foundCheckers = 0;
-while(<FROM_CHILD>) {
-  if (/CHECKERS:/) {
-    $foundCheckers = 1;
-    last;
+if (defined $Clang && -x $Clang) {
+  pipe(FROM_CHILD, TO_PARENT);
+  my $pid = fork();
+  if ($pid == 0) {
+    close FROM_CHILD;
+    open(STDOUT,">&", \*TO_PARENT);
+    open(STDERR,">&", \*TO_PARENT);
+    exec $Clang, ('-cc1', @PluginsToLoad , '-analyzer-checker-help');
   }
-}
-if (!$foundCheckers) {
-  print "  *** Could not query Clang for the list of available checkers.";
-}
-else {
-  print("\nAVAILABLE CHECKERS:\n\n");
-  my $skip = 0;
+  close(TO_PARENT);
+  my $foundCheckers = 0;
   while(<FROM_CHILD>) {
-    if (/experimental/) {
-      $skip = 1;
-      next;
+    if (/CHECKERS:/) {
+      $foundCheckers = 1;
+      last;
     }
-    if ($skip) {
-      next if (!/^\s\s[^\s]/);
-      $skip = 0;
-    }
-    s/^\s\s//;
-    if (/^([^\s]+)/) {
-      # Is the checker enabled?
-      my $checker = $1;
-      my $enabled = 0;
-      my $aggregate = "";
-      foreach my $domain (split /\./, $checker) {
-        $aggregate .= $domain;
-        if ($EnabledCheckers{$aggregate}) {
-          $enabled =1;
-          last;
-        }
-        # append a dot, if an additional domain is added in the next iteration
-        $aggregate .= ".";
+  }
+  if (!$foundCheckers) {
+    print "  *** Could not query Clang for the list of available checkers.";
+  }
+  else {
+    print("\nAVAILABLE CHECKERS:\n\n");
+    my $skip = 0;
+    while(<FROM_CHILD>) {
+      if (/experimental/) {
+        $skip = 1;
+        next;
       }
+      if ($skip) {
+        next if (!/^\s\s[^\s]/);
+        $skip = 0;
+      }
+      s/^\s\s//;
+      if (/^([^\s]+)/) {
+        # Is the checker enabled?
+        my $checker = $1;
+        my $enabled = 0;
+        my $aggregate = "";
+        foreach my $domain (split /\./, $checker) {
+          $aggregate .= $domain;
+          if ($EnabledCheckers{$aggregate}) {
+            $enabled =1;
+            last;
+          }
+          # append a dot, if an additional domain is added in the next iteration
+          $aggregate .= ".";
+        }
       
-      if ($enabled) {
-        print " + ";
+        if ($enabled) {
+          print " + ";
+        }
+        else {
+          print "   ";
+        }
       }
       else {
         print "   ";
       }
+      print $_;
     }
-    else {
-      print "   ";
-    }
-    print $_;
-  } 
+    print "\nNOTE: \"+\" indicates that an analysis is enabled by default.\n"
+  }
+  waitpid($pid,0);
+  close(FROM_CHILD);
 }
-waitpid($pid,0);
-close(FROM_CHILD);
 
 print <<ENDTEXT
 
- NOTE: "+" indicates that an analysis is enabled by default.
-
 BUILD OPTIONS
 
  You can specify any build option acceptable to the build command.
@@ -1456,11 +1457,6 @@
   $displayHelp = 1;
 }
 
-if ($displayHelp) {
-  DisplayHelp();
-  exit 1;
-}
-
 # Find 'clang'
 if (!defined $AnalyzerDiscoveryMethod) {
   $Clang = Cwd::realpath("$RealBin/bin/clang");
@@ -1468,8 +1464,10 @@
     $Clang = Cwd::realpath("$RealBin/clang");
   }
   if (!defined $Clang || ! -x $Clang) {
-    DieDiag("error: Cannot find an executable 'clang' relative to scan-build." .
-   	        "  Consider using --use-analyzer to pick a version of 'clang' to use for static analysis.\n");
+    if (!$displayHelp) {
+      DieDiag("error: Cannot find an executable 'clang' relative to scan-build." .
+   	          "  Consider using --use-analyzer to pick a version of 'clang' to use for static analysis.\n");
+    }
   }
 } 
 else {  
@@ -1493,6 +1491,11 @@
   }
 }
 
+if ($displayHelp) {
+  DisplayHelp();
+  exit 1;
+}
+
 $ClangCXX = $Clang;
 $ClangCXX =~ s/\-\d+\.\d+$//;
 $ClangCXX .= "++";