[FileCheck] Fix a bug that cause FileCheck to misidentify check-prefix

FileCheck should check to make sure the prefix was found, and not a word
containing it (e.g -check-prefix=BASEREL shouldn't match NOBASEREL).

Patch by Ron Ofir.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@188221 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/utils/FileCheck/FileCheck.cpp b/utils/FileCheck/FileCheck.cpp
index 8d5af58..82e8057 100644
--- a/utils/FileCheck/FileCheck.cpp
+++ b/utils/FileCheck/FileCheck.cpp
@@ -704,15 +704,26 @@
 
     LineNumber += Buffer.substr(0, PrefixLoc).count('\n');
 
-    Buffer = Buffer.substr(PrefixLoc);
+    // Keep the charcter before our prefix so we can validate that we have
+    // found our prefix, and account for cases when PrefixLoc is 0.
+    Buffer = Buffer.substr(std::min(PrefixLoc-1, PrefixLoc));
 
-    const char *CheckPrefixStart = Buffer.data();
+    const char *CheckPrefixStart = Buffer.data() + (PrefixLoc == 0 ? 0 : 1);
 
     // When we find a check prefix, keep track of whether we find CHECK: or
     // CHECK-NEXT:
     bool IsCheckNext = false, IsCheckNot = false, IsCheckDag = false,
          IsCheckLabel = false;
 
+    // Make sure we have actually found our prefix, and not a word containing
+    // our prefix.
+    if (PrefixLoc != 0 && (isalnum(Buffer[0]) ||
+                           Buffer[0] == '-'   ||
+                           Buffer[0] == '_')) {
+      Buffer = Buffer.substr(CheckPrefix.size());
+      continue;
+    }
+
     // Verify that the : is present after the prefix.
     if (Buffer[CheckPrefix.size()] == ':') {
       Buffer = Buffer.substr(CheckPrefix.size()+1);
@@ -1026,11 +1037,24 @@
   return LastPos;
 }
 
+bool ValidateCheckPrefix() {
+  // The check prefix must contain only alphanumeric, hyphens and underscores.
+  Regex prefixValidator("^[a-zA-Z0-9_-]*$");
+  return prefixValidator.match(CheckPrefix);
+}
+
 int main(int argc, char **argv) {
   sys::PrintStackTraceOnErrorSignal();
   PrettyStackTraceProgram X(argc, argv);
   cl::ParseCommandLineOptions(argc, argv);
 
+  if (!ValidateCheckPrefix()) {
+    errs() << "Supplied check-prefix is invalid! Prefixes must start with a "
+              "letter and contain only alphanumeric characters, hyphens and "
+              "underscores\n";
+    return 2;
+  }
+
   SourceMgr SM;
 
   // Read the expected strings from the check file.