Skip to content

Commit

Permalink
[FileCheck] introduce CHECK-COUNT-<num> repetition directive
Browse files Browse the repository at this point in the history
In some cases it is desirable to match the same pattern repeatedly
many times. Currently the only way to do it is to copy the same
check pattern as many times as needed. And that gets pretty unwieldy
when its more than count is big.

Introducing CHECK-COUNT-<num> directive which acts like a plain CHECK
directive yet matches the same pattern exactly <num> times.

Extended FileCheckType to a struct to add Count there.
Changed some parsing routines to handle non-fixed length of directive
(all currently existing directives were fixed-length).

The code is generic enough to allow future support for COUNT in more
than just PlainCheck directives.

See motivating example for this feature in reviews.llvm.org/D54223.

Reviewed By: chandlerc, dblaikie
Differential Revision: https://reviews.llvm.org/D54336

llvm-svn: 346722
  • Loading branch information
Fedor Sergeev committed Nov 13, 2018
1 parent 1069463 commit 6c9e19b
Show file tree
Hide file tree
Showing 4 changed files with 293 additions and 105 deletions.
23 changes: 23 additions & 0 deletions llvm/docs/CommandGuide/FileCheck.rst
Expand Up @@ -311,6 +311,29 @@ can be used:
; CHECK: ret i8
}
The "CHECK-COUNT:" directive
~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you need to match multiple lines with the same pattern over and over again
you can repeat a plain ``CHECK:`` as many times as needed. If that looks too
boring you can instead use a counted check "``CHECK-COUNT-<num>:``", where
``<num>`` is a positive decimal number. It will match the pattern exactly
``<num>`` times, no more and no less. If you specified a custom check prefix,
just use "``<PREFIX>-COUNT-<num>:``" for the same effect.
Here is a simple example:

.. code-block:: llvm
Loop at depth 1
Loop at depth 1
Loop at depth 1
Loop at depth 1
Loop at depth 2
Loop at depth 3
; CHECK-COUNT-6: Loop at depth {{[0-9]+}}
; CHECK-NOT: Loop at depth {{[0-9]+}}
The "CHECK-DAG:" directive
~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
26 changes: 24 additions & 2 deletions llvm/include/llvm/Support/FileCheck.h
Expand Up @@ -43,7 +43,8 @@ struct FileCheckRequest {
//===----------------------------------------------------------------------===//

namespace Check {
enum FileCheckType {

enum FileCheckKind {
CheckNone = 0,
CheckPlain,
CheckNext,
Expand All @@ -58,7 +59,26 @@ enum FileCheckType {
CheckEOF,

/// Marks when parsing found a -NOT check combined with another CHECK suffix.
CheckBadNot
CheckBadNot,

/// Marks when parsing found a -COUNT directive with invalid count value.
CheckBadCount
};

class FileCheckType {
FileCheckKind Kind;
int Count; //< optional Count for some checks

public:
FileCheckType(FileCheckKind Kind = CheckNone) : Kind(Kind), Count(1) {}
FileCheckType(const FileCheckType &) = default;

operator FileCheckKind() const { return Kind; }

int getCount() const { return Count; }
FileCheckType &setCount(int C);

std::string getDescription(StringRef Prefix) const;
};
}

Expand Down Expand Up @@ -113,6 +133,8 @@ class FileCheckPattern {

Check::FileCheckType getCheckTy() const { return CheckTy; }

int getCount() const { return CheckTy.getCount(); }

private:
bool AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM);
void AddBackrefToRegEx(unsigned BackrefNum);
Expand Down

0 comments on commit 6c9e19b

Please sign in to comment.