aboutsummaryrefslogtreecommitdiff
path: root/llvm
diff options
context:
space:
mode:
authorJoel E. Denny <jdenny.ornl@gmail.com>2020-05-04 18:05:55 -0400
committerJoel E. Denny <jdenny.ornl@gmail.com>2020-05-11 14:53:48 -0400
commit9a9a5f9893c8db05cebc8818eb8485bff61f7c74 (patch)
tree1f54cdee60d0bb5ae801679e0463decdaab041f0 /llvm
parenta78e13745d4ee4a42e41ebbe698159f651515fc5 (diff)
downloadllvm-9a9a5f9893c8db05cebc8818eb8485bff61f7c74.zip
llvm-9a9a5f9893c8db05cebc8818eb8485bff61f7c74.tar.gz
llvm-9a9a5f9893c8db05cebc8818eb8485bff61f7c74.tar.bz2
[FileCheck] Support comment directives
Sometimes you want to disable a FileCheck directive without removing it entirely, or you want to write comments that mention a directive by name. The `COM:` directive makes it easy to do this. For example, you might have: ``` ; X32: pinsrd_1: ; X32: pinsrd $1, 4(%esp), %xmm0 ; COM: FIXME: X64 isn't working correctly yet for this part of codegen, but ; COM: X64 will have something similar to X32: ; COM: ; COM: X64: pinsrd_1: ; COM: X64: pinsrd $1, %edi, %xmm0 ``` Without this patch, you need to use some combination of rewording and directive syntax mangling to prevent FileCheck from recognizing the commented occurrences of `X32:` and `X64:` above as directives. Moreover, FileCheck diagnostics have been proposed that might complain about the occurrences of `X64` that don't have the trailing `:` because they look like directive typos: <http://lists.llvm.org/pipermail/llvm-dev/2020-April/140610.html> I think dodging all these problems can prove tedious for test authors, and directive syntax mangling already makes the purpose of existing test code unclear. `COM:` can avoid all these problems. This patch also updates the small set of existing tests that define `COM` as a check prefix: - clang/test/CodeGen/default-address-space.c - clang/test/CodeGenOpenCL/addr-space-struct-arg.cl - clang/test/Driver/hip-device-libs.hip - llvm/test/Assembler/drop-debug-info-nonzero-alloca.ll I think lit should support `COM:` as well. Perhaps `clang -verify` should too. Reviewed By: jhenderson, thopre Differential Revision: https://reviews.llvm.org/D79276
Diffstat (limited to 'llvm')
-rw-r--r--llvm/docs/CommandGuide/FileCheck.rst73
-rw-r--r--llvm/include/llvm/Support/FileCheck.h2
-rw-r--r--llvm/lib/Support/FileCheck.cpp89
-rw-r--r--llvm/test/Assembler/drop-debug-info-nonzero-alloca.ll10
-rw-r--r--llvm/test/FileCheck/comment/after-words.txt16
-rw-r--r--llvm/test/FileCheck/comment/bad-comment-prefix.txt48
-rw-r--r--llvm/test/FileCheck/comment/blank-comments.txt9
-rw-r--r--llvm/test/FileCheck/comment/suffixes.txt22
-rw-r--r--llvm/test/FileCheck/comment/suppresses-checks.txt33
-rw-r--r--llvm/test/FileCheck/comment/unused-check-prefixes.txt8
-rw-r--r--llvm/test/FileCheck/comment/unused-comment-prefixes.txt16
-rw-r--r--llvm/test/FileCheck/comment/within-checks.txt8
-rw-r--r--llvm/test/FileCheck/first-character-match.txt2
-rw-r--r--llvm/test/FileCheck/validate-check-prefix.txt2
-rw-r--r--llvm/utils/FileCheck/FileCheck.cpp13
15 files changed, 320 insertions, 31 deletions
diff --git a/llvm/docs/CommandGuide/FileCheck.rst b/llvm/docs/CommandGuide/FileCheck.rst
index 55fc3f0..7d9a69e 100644
--- a/llvm/docs/CommandGuide/FileCheck.rst
+++ b/llvm/docs/CommandGuide/FileCheck.rst
@@ -39,15 +39,31 @@ and from the command line.
match. By default, these patterns are prefixed with "``CHECK:``".
If you'd like to use a different prefix (e.g. because the same input
file is checking multiple different tool or options), the
- :option:`--check-prefix` argument allows you to specify one or more
- prefixes to match. Multiple prefixes are useful for tests which might
- change for different run options, but most lines remain the same.
+ :option:`--check-prefix` argument allows you to specify (without the trailing
+ "``:``") one or more prefixes to match. Multiple prefixes are useful for tests
+ which might change for different run options, but most lines remain the same.
+
+ FileCheck does not permit duplicate prefixes, even if one is a check prefix
+ and one is a comment prefix (see :option:`--comment-prefixes` below).
.. option:: --check-prefixes prefix1,prefix2,...
An alias of :option:`--check-prefix` that allows multiple prefixes to be
specified as a comma separated list.
+.. option:: --comment-prefixes prefix1,prefix2,...
+
+ By default, FileCheck ignores any occurrence in ``match-filename`` of any check
+ prefix if it is preceded on the same line by "``COM:``" or "``RUN:``". See the
+ section `The "COM:" directive`_ for usage details.
+
+ These default comment prefixes can be overridden by
+ :option:`--comment-prefixes` if they are not appropriate for your testing
+ environment. However, doing so is not recommended in LLVM's LIT-based test
+ suites, which should be easier to maintain if they all follow a consistent
+ comment style. In that case, consider proposing a change to the default
+ comment prefixes instead.
+
.. option:: --input-file filename
File to check (defaults to stdin).
@@ -236,6 +252,57 @@ circumstances, for example, testing different architectural variants with
In this case, we're testing that we get the expected code generation with
both 32-bit and 64-bit code generation.
+The "COM:" directive
+~~~~~~~~~~~~~~~~~~~~
+
+Sometimes you want to disable a FileCheck directive without removing it
+entirely, or you want to write comments that mention a directive by name. The
+"``COM:``" directive makes it easy to do this. For example, you might have:
+
+.. code-block:: llvm
+
+ ; X32: pinsrd_1:
+ ; X32: pinsrd $1, 4(%esp), %xmm0
+
+ ; COM: FIXME: X64 isn't working correctly yet for this part of codegen, but
+ ; COM: X64 will have something similar to X32:
+ ; COM:
+ ; COM: X64: pinsrd_1:
+ ; COM: X64: pinsrd $1, %edi, %xmm0
+
+Without "``COM:``", you would need to use some combination of rewording and
+directive syntax mangling to prevent FileCheck from recognizing the commented
+occurrences of "``X32:``" and "``X64:``" above as directives. Moreover,
+FileCheck diagnostics have been proposed that might complain about the above
+occurrences of "``X64``" that don't have the trailing "``:``" because they look
+like directive typos. Dodging all these problems can be tedious for a test
+author, and directive syntax mangling can make the purpose of test code unclear.
+"``COM:``" avoids all these problems.
+
+A few important usage notes:
+
+* "``COM:``" within another directive's pattern does *not* comment out the
+ remainder of the pattern. For example:
+
+ .. code-block:: llvm
+
+ ; X32: pinsrd $1, 4(%esp), %xmm0 COM: This is part of the X32 pattern!
+
+ If you need to temporarily comment out part of a directive's pattern, move it
+ to another line. The reason is that FileCheck parses "``COM:``" in the same
+ manner as any other directive: only the first directive on the line is
+ recognized as a directive.
+
+* For the sake of LIT, FileCheck treats "``RUN:``" just like "``COM:``". If this
+ is not suitable for your test environment, see :option:`--comment-prefixes`.
+
+* FileCheck does not recognize "``COM``", "``RUN``", or any user-defined comment
+ prefix as a comment directive if it's combined with one of the usual check
+ directive suffixes, such as "``-NEXT:``" or "``-NOT:``", discussed below.
+ FileCheck treats such a combination as plain text instead. If it needs to act
+ as a comment directive for your test environment, define it as such with
+ :option:`--comment-prefixes`.
+
The "CHECK-NEXT:" directive
~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/llvm/include/llvm/Support/FileCheck.h b/llvm/include/llvm/Support/FileCheck.h
index 0d1f92b..2f0e641 100644
--- a/llvm/include/llvm/Support/FileCheck.h
+++ b/llvm/include/llvm/Support/FileCheck.h
@@ -25,6 +25,7 @@ namespace llvm {
/// Contains info about various FileCheck options.
struct FileCheckRequest {
std::vector<StringRef> CheckPrefixes;
+ std::vector<StringRef> CommentPrefixes;
bool NoCanonicalizeWhiteSpace = false;
std::vector<StringRef> ImplicitCheckNot;
std::vector<StringRef> GlobalDefines;
@@ -53,6 +54,7 @@ enum FileCheckKind {
CheckDAG,
CheckLabel,
CheckEmpty,
+ CheckComment,
/// Indicates the pattern only matches the end of file. This is used for
/// trailing CHECK-NOTs.
diff --git a/llvm/lib/Support/FileCheck.cpp b/llvm/lib/Support/FileCheck.cpp
index 369b6df..9cdb14c 100644
--- a/llvm/lib/Support/FileCheck.cpp
+++ b/llvm/lib/Support/FileCheck.cpp
@@ -1110,6 +1110,8 @@ std::string Check::FileCheckType::getDescription(StringRef Prefix) const {
return Prefix.str() + "-LABEL";
case Check::CheckEmpty:
return Prefix.str() + "-EMPTY";
+ case Check::CheckComment:
+ return std::string(Prefix);
case Check::CheckEOF:
return "implicit EOF";
case Check::CheckBadNot:
@@ -1121,13 +1123,24 @@ std::string Check::FileCheckType::getDescription(StringRef Prefix) const {
}
static std::pair<Check::FileCheckType, StringRef>
-FindCheckType(StringRef Buffer, StringRef Prefix) {
+FindCheckType(const FileCheckRequest &Req, StringRef Buffer, StringRef Prefix) {
if (Buffer.size() <= Prefix.size())
return {Check::CheckNone, StringRef()};
char NextChar = Buffer[Prefix.size()];
StringRef Rest = Buffer.drop_front(Prefix.size() + 1);
+
+ // Check for comment.
+ if (Req.CommentPrefixes.end() != std::find(Req.CommentPrefixes.begin(),
+ Req.CommentPrefixes.end(),
+ Prefix)) {
+ if (NextChar == ':')
+ return {Check::CheckComment, Rest};
+ // Ignore a comment prefix if it has a suffix like "-NOT".
+ return {Check::CheckNone, StringRef()};
+ }
+
// Verify that the : is present after the prefix.
if (NextChar == ':')
return {Check::CheckPlain, Rest};
@@ -1206,8 +1219,9 @@ static size_t SkipWord(StringRef Str, size_t Loc) {
/// If no valid prefix is found, the state of Buffer, LineNumber, and CheckTy
/// is unspecified.
static std::pair<StringRef, StringRef>
-FindFirstMatchingPrefix(Regex &PrefixRE, StringRef &Buffer,
- unsigned &LineNumber, Check::FileCheckType &CheckTy) {
+FindFirstMatchingPrefix(const FileCheckRequest &Req, Regex &PrefixRE,
+ StringRef &Buffer, unsigned &LineNumber,
+ Check::FileCheckType &CheckTy) {
SmallVector<StringRef, 2> Matches;
while (!Buffer.empty()) {
@@ -1235,7 +1249,7 @@ FindFirstMatchingPrefix(Regex &PrefixRE, StringRef &Buffer,
if (Skipped.empty() || !IsPartOfWord(Skipped.back())) {
// Now extract the type.
StringRef AfterSuffix;
- std::tie(CheckTy, AfterSuffix) = FindCheckType(Buffer, Prefix);
+ std::tie(CheckTy, AfterSuffix) = FindCheckType(Req, Buffer, Prefix);
// If we've found a valid check type for this prefix, we're done.
if (CheckTy != Check::CheckNone)
@@ -1316,7 +1330,7 @@ bool FileCheck::readCheckFile(
// found.
unsigned LineNumber = 1;
- bool FoundUsedPrefix = false;
+ bool FoundUsedCheckPrefix = false;
while (1) {
Check::FileCheckType CheckTy;
@@ -1324,10 +1338,11 @@ bool FileCheck::readCheckFile(
StringRef UsedPrefix;
StringRef AfterSuffix;
std::tie(UsedPrefix, AfterSuffix) =
- FindFirstMatchingPrefix(PrefixRE, Buffer, LineNumber, CheckTy);
+ FindFirstMatchingPrefix(Req, PrefixRE, Buffer, LineNumber, CheckTy);
if (UsedPrefix.empty())
break;
- FoundUsedPrefix = true;
+ if (CheckTy != Check::CheckComment)
+ FoundUsedCheckPrefix = true;
assert(UsedPrefix.data() == Buffer.data() &&
"Failed to move Buffer's start forward, or pointed prefix outside "
@@ -1370,9 +1385,17 @@ bool FileCheck::readCheckFile(
// Remember the location of the start of the pattern, for diagnostics.
SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data());
+ // Extract the pattern from the buffer.
+ StringRef PatternBuffer = Buffer.substr(0, EOL);
+ Buffer = Buffer.substr(EOL);
+
+ // If this is a comment, we're done.
+ if (CheckTy == Check::CheckComment)
+ continue;
+
// Parse the pattern.
Pattern P(CheckTy, PatternContext.get(), LineNumber);
- if (P.parsePattern(Buffer.substr(0, EOL), UsedPrefix, SM, Req))
+ if (P.parsePattern(PatternBuffer, UsedPrefix, SM, Req))
return true;
// Verify that CHECK-LABEL lines do not define or use variables
@@ -1384,8 +1407,6 @@ bool FileCheck::readCheckFile(
return true;
}
- Buffer = Buffer.substr(EOL);
-
// Verify that CHECK-NEXT/SAME/EMPTY lines have at least one CHECK line before them.
if ((CheckTy == Check::CheckNext || CheckTy == Check::CheckSame ||
CheckTy == Check::CheckEmpty) &&
@@ -1414,7 +1435,7 @@ bool FileCheck::readCheckFile(
// When there are no used prefixes we report an error except in the case that
// no prefix is specified explicitly but -implicit-check-not is specified.
- if (!FoundUsedPrefix &&
+ if (!FoundUsedCheckPrefix &&
(ImplicitNegativeChecks.empty() || !Req.IsDefaultCheckPrefix)) {
errs() << "error: no check strings found with prefix"
<< (Req.CheckPrefixes.size() > 1 ? "es " : " ");
@@ -1874,50 +1895,76 @@ size_t FileCheckString::CheckDag(const SourceMgr &SM, StringRef Buffer,
return StartPos;
}
-static bool ValidatePrefixes(StringSet<> &UniquePrefixes,
+static bool ValidatePrefixes(StringRef Kind, StringSet<> &UniquePrefixes,
ArrayRef<StringRef> SuppliedPrefixes) {
for (StringRef Prefix : SuppliedPrefixes) {
if (Prefix.empty()) {
- errs() << "error: supplied check prefix must not be the empty string\n";
+ errs() << "error: supplied " << Kind << " prefix must not be the empty "
+ << "string\n";
return false;
}
static const Regex Validator("^[a-zA-Z0-9_-]*$");
if (!Validator.match(Prefix)) {
- errs() << "error: supplied check prefix must start with a letter and "
- << "contain only alphanumeric characters, hyphens, and "
+ errs() << "error: supplied " << Kind << " prefix must start with a "
+ << "letter and contain only alphanumeric characters, hyphens, and "
<< "underscores: '" << Prefix << "'\n";
return false;
}
if (!UniquePrefixes.insert(Prefix).second) {
- errs() << "error: supplied check prefix must be unique among check "
- << "prefixes: '" << Prefix << "'\n";
+ errs() << "error: supplied " << Kind << " prefix must be unique among "
+ << "check and comment prefixes: '" << Prefix << "'\n";
return false;
}
}
return true;
}
+static const char *DefaultCheckPrefixes[] = {"CHECK"};
+static const char *DefaultCommentPrefixes[] = {"COM", "RUN"};
+
bool FileCheck::ValidateCheckPrefixes() {
StringSet<> UniquePrefixes;
- if (!ValidatePrefixes(UniquePrefixes, Req.CheckPrefixes))
+ // Add default prefixes to catch user-supplied duplicates of them below.
+ if (Req.CheckPrefixes.empty()) {
+ for (const char *Prefix : DefaultCheckPrefixes)
+ UniquePrefixes.insert(Prefix);
+ }
+ if (Req.CommentPrefixes.empty()) {
+ for (const char *Prefix : DefaultCommentPrefixes)
+ UniquePrefixes.insert(Prefix);
+ }
+ // Do not validate the default prefixes, or diagnostics about duplicates might
+ // incorrectly indicate that they were supplied by the user.
+ if (!ValidatePrefixes("check", UniquePrefixes, Req.CheckPrefixes))
+ return false;
+ if (!ValidatePrefixes("comment", UniquePrefixes, Req.CommentPrefixes))
return false;
return true;
}
Regex FileCheck::buildCheckPrefixRegex() {
if (Req.CheckPrefixes.empty()) {
- Req.CheckPrefixes.push_back("CHECK");
+ for (const char *Prefix : DefaultCheckPrefixes)
+ Req.CheckPrefixes.push_back(Prefix);
Req.IsDefaultCheckPrefix = true;
}
+ if (Req.CommentPrefixes.empty()) {
+ for (const char *Prefix : DefaultCommentPrefixes)
+ Req.CommentPrefixes.push_back(Prefix);
+ }
- // We already validated the contents of CheckPrefixes so just concatenate
- // them as alternatives.
+ // We already validated the contents of CheckPrefixes and CommentPrefixes so
+ // just concatenate them as alternatives.
SmallString<32> PrefixRegexStr;
for (size_t I = 0, E = Req.CheckPrefixes.size(); I != E; ++I) {
if (I != 0)
PrefixRegexStr.push_back('|');
PrefixRegexStr.append(Req.CheckPrefixes[I]);
}
+ for (StringRef Prefix : Req.CommentPrefixes) {
+ PrefixRegexStr.push_back('|');
+ PrefixRegexStr.append(Prefix);
+ }
return Regex(PrefixRegexStr);
}
diff --git a/llvm/test/Assembler/drop-debug-info-nonzero-alloca.ll b/llvm/test/Assembler/drop-debug-info-nonzero-alloca.ll
index 739edba..3e2aefb 100644
--- a/llvm/test/Assembler/drop-debug-info-nonzero-alloca.ll
+++ b/llvm/test/Assembler/drop-debug-info-nonzero-alloca.ll
@@ -1,7 +1,7 @@
-; RUN: llvm-as < %s -o %t.bc -data-layout=A5 2>&1 | FileCheck -check-prefixes=COM,AS %s
-; RUN: llvm-dis < %t.bc | FileCheck -check-prefixes=COM,DIS %s
-; RUN: opt < %s -S -data-layout=A5 2>&1 | FileCheck -check-prefixes=COM,AS %s
-; RUN: opt < %t.bc -S | FileCheck -check-prefixes=COM,DIS %s
+; RUN: llvm-as < %s -o %t.bc -data-layout=A5 2>&1 | FileCheck -check-prefixes=ALL,AS %s
+; RUN: llvm-dis < %t.bc | FileCheck -check-prefixes=ALL,DIS %s
+; RUN: opt < %s -S -data-layout=A5 2>&1 | FileCheck -check-prefixes=ALL,AS %s
+; RUN: opt < %t.bc -S | FileCheck -check-prefixes=ALL,DIS %s
define void @foo() {
entry:
@@ -12,7 +12,7 @@ entry:
metadata i8* undef,
metadata !DILocalVariable(scope: !1),
metadata !DIExpression())
-; COM-NOT: Allocation instruction pointer not in the stack address space!
+; ALL-NOT: Allocation instruction pointer not in the stack address space!
; AS: llvm.dbg.value intrinsic requires a !dbg attachment
; AS: warning: ignoring invalid debug info in <stdin>
ret void
diff --git a/llvm/test/FileCheck/comment/after-words.txt b/llvm/test/FileCheck/comment/after-words.txt
new file mode 100644
index 0000000..46eeb65
--- /dev/null
+++ b/llvm/test/FileCheck/comment/after-words.txt
@@ -0,0 +1,16 @@
+# Comment prefixes are not recognized at ends of words.
+
+RUN: echo 'foo' > %t.in
+RUN: echo 'bar' >> %t.in
+RUN: echo 'foo' >> %t.in
+RUN: echo 'bar' >> %t.in
+RUN: echo 'FOO-COM: CHECK: foo' > %t.chk
+RUN: echo 'RUN_COM: CHECK: bar' >> %t.chk
+RUN: echo 'RUN3COM: CHECK: foo' >> %t.chk
+RUN: echo ' COMRUN: CHECK: bar' >> %t.chk
+RUN: %ProtectFileCheckOutput FileCheck -vv %t.chk < %t.in 2>&1 | FileCheck %s
+
+CHECK: .chk:1:17: remark: CHECK: expected string found in input
+CHECK: .chk:2:17: remark: CHECK: expected string found in input
+CHECK: .chk:3:17: remark: CHECK: expected string found in input
+CHECK: .chk:4:17: remark: CHECK: expected string found in input
diff --git a/llvm/test/FileCheck/comment/bad-comment-prefix.txt b/llvm/test/FileCheck/comment/bad-comment-prefix.txt
new file mode 100644
index 0000000..58a8873
--- /dev/null
+++ b/llvm/test/FileCheck/comment/bad-comment-prefix.txt
@@ -0,0 +1,48 @@
+# Bad comment prefixes are diagnosed.
+
+# Check empty comment prefix.
+RUN: %ProtectFileCheckOutput not FileCheck /dev/null < /dev/null 2>&1 \
+RUN: -comment-prefixes= | \
+RUN: FileCheck -check-prefix=PREFIX-EMPTY %s
+RUN: %ProtectFileCheckOutput not FileCheck /dev/null < /dev/null 2>&1 \
+RUN: -comment-prefixes=,FOO | \
+RUN: FileCheck -check-prefix=PREFIX-EMPTY %s
+RUN: %ProtectFileCheckOutput not FileCheck /dev/null < /dev/null 2>&1 \
+RUN: -comment-prefixes=FOO, | \
+RUN: FileCheck -check-prefix=PREFIX-EMPTY %s
+RUN: %ProtectFileCheckOutput not FileCheck /dev/null < /dev/null 2>&1 \
+RUN: -comment-prefixes=FOO,,BAR | \
+RUN: FileCheck -check-prefix=PREFIX-EMPTY %s
+PREFIX-EMPTY: error: supplied comment prefix must not be the empty string
+
+# Check invalid characters in comment prefix.
+RUN: %ProtectFileCheckOutput not FileCheck /dev/null < /dev/null 2>&1 \
+RUN: -comment-prefixes=. | \
+RUN: FileCheck -check-prefix=PREFIX-BAD-CHAR1 %s
+RUN: %ProtectFileCheckOutput not FileCheck /dev/null < /dev/null 2>&1 \
+RUN: -comment-prefixes='foo ' | \
+RUN: FileCheck -check-prefix=PREFIX-BAD-CHAR2 %s
+PREFIX-BAD-CHAR1: error: supplied comment prefix must start with a letter and contain only alphanumeric characters, hyphens, and underscores: '.'
+PREFIX-BAD-CHAR2: error: supplied comment prefix must start with a letter and contain only alphanumeric characters, hyphens, and underscores: 'foo '
+
+# Check duplicate comment prefixes.
+RUN: %ProtectFileCheckOutput not FileCheck /dev/null < /dev/null 2>&1 \
+RUN: -comment-prefixes=FOO,BAR,BAZ,BAR | \
+RUN: FileCheck -check-prefix=COMMENT-PREFIX-DUP %s
+COMMENT-PREFIX-DUP: error: supplied comment prefix must be unique among check and comment prefixes: 'BAR'
+
+# Check user-supplied check prefix that duplicates a default comment prefix.
+RUN: %ProtectFileCheckOutput not FileCheck /dev/null < /dev/null 2>&1 \
+RUN: -check-prefixes=FOO,COM | \
+RUN: FileCheck -check-prefix=CHECK-PREFIX-DUP-COM %s
+RUN: %ProtectFileCheckOutput not FileCheck /dev/null < /dev/null 2>&1 \
+RUN: -check-prefixes=RUN,FOO | \
+RUN: FileCheck -check-prefix=CHECK-PREFIX-DUP-RUN_ %s
+CHECK-PREFIX-DUP-COM: error: supplied check prefix must be unique among check and comment prefixes: 'COM'
+CHECK-PREFIX-DUP-RUN_: error: supplied check prefix must be unique among check and comment prefixes: 'RUN'
+
+# Check user-supplied comment prefix that duplicates default check prefixes.
+RUN: %ProtectFileCheckOutput not FileCheck /dev/null < /dev/null 2>&1 \
+RUN: -comment-prefixes=CHECK | \
+RUN: FileCheck -check-prefix=COMMENT-PREFIX-DUP-CHECK %s
+COMMENT-PREFIX-DUP-CHECK: error: supplied comment prefix must be unique among check and comment prefixes: 'CHECK'
diff --git a/llvm/test/FileCheck/comment/blank-comments.txt b/llvm/test/FileCheck/comment/blank-comments.txt
new file mode 100644
index 0000000..b035ddd
--- /dev/null
+++ b/llvm/test/FileCheck/comment/blank-comments.txt
@@ -0,0 +1,9 @@
+# Comment directives with blank contents are fine.
+
+RUN: echo 'foo' > %t.in
+RUN: echo 'COM:' > %t.chk
+RUN: echo 'CHECK: foo' >> %t.chk
+RUN: echo ' COM: ' >> %t.chk
+RUN: %ProtectFileCheckOutput FileCheck -vv %t.chk < %t.in 2>&1 | FileCheck %s
+
+CHECK: .chk:2:8: remark: CHECK: expected string found in input
diff --git a/llvm/test/FileCheck/comment/suffixes.txt b/llvm/test/FileCheck/comment/suffixes.txt
new file mode 100644
index 0000000..47805b4
--- /dev/null
+++ b/llvm/test/FileCheck/comment/suffixes.txt
@@ -0,0 +1,22 @@
+# Comment prefixes plus check directive suffixes are not comment directives
+# and are treated as plain text.
+
+RUN: echo foo > %t.in
+RUN: echo bar >> %t.in
+RUN: echo 'COM-NEXT: CHECK: foo' > %t.chk
+RUN: echo 'RUN-NOT: CHECK: bar' >> %t.chk
+
+RUN: %ProtectFileCheckOutput FileCheck -vv %t.chk < %t.in 2>&1 | \
+RUN: FileCheck -check-prefix=CHECK1 %s
+
+CHECK1: .chk:1:18: remark: CHECK: expected string found in input
+CHECK1: .chk:2:17: remark: CHECK: expected string found in input
+
+# But we can define them as comment prefixes.
+
+RUN: %ProtectFileCheckOutput \
+RUN: FileCheck -vv -comment-prefixes=COM,RUN,RUN-NOT %t.chk < %t.in 2>&1 | \
+RUN: FileCheck -check-prefix=CHECK2 %s
+
+CHECK2: .chk:1:18: remark: CHECK: expected string found in input
+CHECK2-NOT: .chk:2
diff --git a/llvm/test/FileCheck/comment/suppresses-checks.txt b/llvm/test/FileCheck/comment/suppresses-checks.txt
new file mode 100644
index 0000000..98f0181
--- /dev/null
+++ b/llvm/test/FileCheck/comment/suppresses-checks.txt
@@ -0,0 +1,33 @@
+# Comment directives successfully comment out check directives.
+
+# Check all default comment prefixes.
+# Check that a comment directive at the beginning/end of the file is handled.
+# Check that the preceding/following line's check directive is not affected.
+RUN: echo 'foo' > %t-1.in
+RUN: echo 'COM: CHECK: bar' > %t-1.chk
+RUN: echo 'CHECK: foo' >> %t-1.chk
+RUN: echo 'RUN: echo "CHECK: baz"' >> %t-1.chk
+RUN: %ProtectFileCheckOutput FileCheck -vv %t-1.chk < %t-1.in 2>&1 | \
+RUN: FileCheck -DCHECK_LINE=2 %s
+
+# Check the case of one user-specified comment prefix.
+# Check that a comment directive not at the beginning of a line is handled.
+RUN: echo 'foo' > %t-2.in
+RUN: echo 'CHECK: foo' > %t-2.chk
+RUN: echo 'letters then space MY-PREFIX: CHECK: bar' >> %t-2.chk
+RUN: %ProtectFileCheckOutput \
+RUN: FileCheck -vv %t-2.chk -comment-prefixes=MY-PREFIX < %t-2.in 2>&1 | \
+RUN: FileCheck -DCHECK_LINE=1 %s
+
+# Check the case of multiple user-specified comment prefixes.
+RUN: echo 'foo' > %t-3.in
+RUN: echo 'Bar_2: CHECK: Bar' > %t-3.chk
+RUN: echo 'CHECK: foo' >> %t-3.chk
+RUN: echo 'Foo_1: CHECK: Foo' >> %t-3.chk
+RUN: echo 'Baz_3: CHECK: Baz' >> %t-3.chk
+RUN: %ProtectFileCheckOutput \
+RUN: FileCheck -vv %t-3.chk -comment-prefixes=Foo_1,Bar_2 \
+RUN: -comment-prefixes=Baz_3 < %t-3.in 2>&1 | \
+RUN: FileCheck -DCHECK_LINE=2 %s
+
+CHECK: .chk:[[CHECK_LINE]]:8: remark: CHECK: expected string found in input
diff --git a/llvm/test/FileCheck/comment/unused-check-prefixes.txt b/llvm/test/FileCheck/comment/unused-check-prefixes.txt
new file mode 100644
index 0000000..6c3f830
--- /dev/null
+++ b/llvm/test/FileCheck/comment/unused-check-prefixes.txt
@@ -0,0 +1,8 @@
+# Comment directives do not suppress diagnostics for unused check prefixes.
+
+RUN: echo 'foo' > %t.in
+RUN: echo 'COM: foo' > %t.chk
+RUN: echo 'RUN: echo' >> %t.chk
+RUN: %ProtectFileCheckOutput not FileCheck %t.chk < %t.in 2>&1 | FileCheck %s
+
+CHECK: error: no check strings found with prefix 'CHECK:'
diff --git a/llvm/test/FileCheck/comment/unused-comment-prefixes.txt b/llvm/test/FileCheck/comment/unused-comment-prefixes.txt
new file mode 100644
index 0000000..29212ec
--- /dev/null
+++ b/llvm/test/FileCheck/comment/unused-comment-prefixes.txt
@@ -0,0 +1,16 @@
+# Not using comment directives is always fine.
+
+RUN: echo 'foo' > %t.in
+RUN: echo 'CHECK: foo' > %t.chk
+
+# Check the case of default comment prefixes.
+RUN: %ProtectFileCheckOutput \
+RUN: FileCheck -vv %t.chk < %t.in 2>&1 | FileCheck %s
+
+# Specifying non-default comment prefixes doesn't mean you have to use them.
+# For example, they might be applied to an entire test suite via
+# FILECHECK_OPTS or via a wrapper command or substitution.
+RUN: %ProtectFileCheckOutput \
+RUN: FileCheck -vv -comment-prefixes=FOO %t.chk < %t.in 2>&1 | FileCheck %s
+
+CHECK: .chk:1:8: remark: CHECK: expected string found in input
diff --git a/llvm/test/FileCheck/comment/within-checks.txt b/llvm/test/FileCheck/comment/within-checks.txt
new file mode 100644
index 0000000..b488edb
--- /dev/null
+++ b/llvm/test/FileCheck/comment/within-checks.txt
@@ -0,0 +1,8 @@
+# A comment directive is not recognized within a check directive's pattern and
+# thus does not comment out the remainder of the pattern.
+
+RUN: echo 'foo' > %t.in
+RUN: echo 'CHECK: foo COM: bar' > %t.chk
+RUN: %ProtectFileCheckOutput not FileCheck %t.chk < %t.in 2>&1 | FileCheck %s
+
+CHECK: .chk:1:8: error: CHECK: expected string not found in input
diff --git a/llvm/test/FileCheck/first-character-match.txt b/llvm/test/FileCheck/first-character-match.txt
index 4b09c21..3c8b153 100644
--- a/llvm/test/FileCheck/first-character-match.txt
+++ b/llvm/test/FileCheck/first-character-match.txt
@@ -1,2 +1,2 @@
-RUN: FileCheck -check-prefix=RUN -input-file %s %s
+RUN: FileCheck -check-prefix=RUN --comment-prefixes=COM -input-file %s %s
// Prefix is at the first character in the file. The run line then matches itself.
diff --git a/llvm/test/FileCheck/validate-check-prefix.txt b/llvm/test/FileCheck/validate-check-prefix.txt
index 29f352c..af43afd 100644
--- a/llvm/test/FileCheck/validate-check-prefix.txt
+++ b/llvm/test/FileCheck/validate-check-prefix.txt
@@ -8,6 +8,6 @@ foobar
; BAD_PREFIX: supplied check prefix must start with a letter and contain only alphanumeric characters, hyphens, and underscores: 'A!'
-; DUPLICATE_PREFIX: error: supplied check prefix must be unique among check prefixes: 'REPEAT'
+; DUPLICATE_PREFIX: error: supplied check prefix must be unique among check and comment prefixes: 'REPEAT'
; EMPTY_PREFIX: error: supplied check prefix must not be the empty string
diff --git a/llvm/utils/FileCheck/FileCheck.cpp b/llvm/utils/FileCheck/FileCheck.cpp
index 46821ec..3ee7c5a 100644
--- a/llvm/utils/FileCheck/FileCheck.cpp
+++ b/llvm/utils/FileCheck/FileCheck.cpp
@@ -44,6 +44,14 @@ static cl::alias CheckPrefixesAlias(
cl::desc(
"Alias for -check-prefix permitting multiple comma separated values"));
+static cl::list<std::string> CommentPrefixes(
+ "comment-prefixes", cl::CommaSeparated, cl::Hidden,
+ cl::desc("Comma-separated list of comment prefixes to use from check file\n"
+ "(defaults to 'COM,RUN'). Please avoid using this feature in\n"
+ "LLVM's LIT-based test suites, which should be easier to\n"
+ "maintain if they all follow a consistent comment style. This\n"
+ "feature is meant for non-LIT test suites using FileCheck."));
+
static cl::opt<bool> NoCanonicalizeWhiteSpace(
"strict-whitespace",
cl::desc("Do not treat all horizontal whitespace as equivalent"));
@@ -279,6 +287,8 @@ std::string GetCheckTypeAbbreviation(Check::FileCheckType Ty) {
return "label";
case Check::CheckEmpty:
return "empty";
+ case Check::CheckComment:
+ return "com";
case Check::CheckEOF:
return "eof";
case Check::CheckBadNot:
@@ -565,6 +575,9 @@ int main(int argc, char **argv) {
for (StringRef Prefix : CheckPrefixes)
Req.CheckPrefixes.push_back(Prefix);
+ for (StringRef Prefix : CommentPrefixes)
+ Req.CommentPrefixes.push_back(Prefix);
+
for (StringRef CheckNot : ImplicitCheckNot)
Req.ImplicitCheckNot.push_back(CheckNot);