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

Ron Ofir ron.ofir at gmail.com
Fri Aug 2 14:53:39 PDT 2013


  Fixed annoying CRLF and tab issues. Sorry for spamming...

http://llvm-reviews.chandlerc.com/D1271

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D1271?vs=3170&id=3171#toc

Files:
  utils/FileCheck/FileCheck.cpp

Index: utils/FileCheck/FileCheck.cpp
===================================================================
--- utils/FileCheck/FileCheck.cpp
+++ utils/FileCheck/FileCheck.cpp
@@ -704,9 +704,11 @@
 
     LineNumber += Buffer.substr(0, PrefixLoc).count('\n');
 
-    Buffer = Buffer.substr(PrefixLoc);
+    // Keep the charcter before our prefix so we can validate the 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:
@@ -713,8 +715,15 @@
     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()] == ':') {
+    } else if (Buffer[CheckPrefix.size()] == ':') {
       Buffer = Buffer.substr(CheckPrefix.size()+1);
     } else if (Buffer.size() > CheckPrefix.size()+6 &&
                memcmp(Buffer.data()+CheckPrefix.size(), "-NEXT:", 6) == 0) {
@@ -1026,11 +1035,25 @@
   return LastPos;
 }
 
+bool ValidateCheckPrefix() {
+  // First character must be a letter, and the string must contain only
+  // alphanumeric, hyphens and underscores.
+  Regex prefixValidator("^[a-zA-Z][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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1271.3.patch
Type: text/x-patch
Size: 2306 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130802/3d403dc2/attachment.bin>


More information about the llvm-commits mailing list