Skip to content

Commit 41740eb

Browse files
Add 3c-regtest.py and migrate one example test to use it.
1 parent 9a8a7b2 commit 41740eb

File tree

2 files changed

+102
-6
lines changed

2 files changed

+102
-6
lines changed

clang/test/3C/3c-regtest.py

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/env python
2+
#
3+
# Usage: 3c-regtest -t TMPNAME [options...] SRC_FILE
4+
#
5+
# TMPNAME corresponds to %t and SRC_FILE corresponds to %s. Both are required.
6+
#
7+
# --subst can be used for things such as %clang, for example (assuming single
8+
# --quotes are removed by the shell):
9+
#
10+
# --subst %clang 'clang -some-flag'
11+
#
12+
# (Note: A literal % has to be represented as %% in a RUN line. If we instead
13+
# established the convention of automatically prepending the % here, then the
14+
# RUN line would trip the "Do not use 'clang' in tests, use '%clang'." error.)
15+
#
16+
# Example RUN line:
17+
#
18+
# // RUN: %S/3c-regtest.py -t %t --subst %%clang '%clang' %s
19+
#
20+
# Soon, we'll add options for different kinds of 3C regression tests.
21+
22+
# TODO: Add Windows compatibility code once we have an easy way to test on Windows.
23+
24+
import sys
25+
import os
26+
import platform
27+
import argparse
28+
29+
sys.path.insert(0, os.path.dirname(__file__) + '/../../../llvm/utils/lit')
30+
import lit.TestRunner
31+
32+
print "NOTICE: cwd is %s" % os.getcwd()
33+
34+
def die(msg):
35+
sys.stderr.write('Error: %s\n' % msg)
36+
sys.exit(1)
37+
38+
parser = argparse.ArgumentParser(description='Run a 3C regression test.')
39+
# TODO: Add help
40+
parser.add_argument('test_file')
41+
parser.add_argument('-t', required=True)
42+
parser.add_argument('--subst', action='append', nargs=2, default=[])
43+
args = parser.parse_args()
44+
45+
test_dir = os.path.dirname(args.test_file)
46+
if test_dir == '':
47+
test_dir = '.'
48+
49+
tmpName = args.t
50+
tmpNameSuffix = '.tmp'
51+
if tmpName.endswith(tmpNameSuffix):
52+
tmpBase = tmpName[:-len(tmpNameSuffix)]
53+
else:
54+
die('-t argument %s does not end with %s' % (tmpName, tmpNameSuffix))
55+
56+
substitutions = [
57+
('%%', '#_MARKER_#'),
58+
('%s', args.test_file),
59+
('%S', test_dir),
60+
('%t', tmpName),
61+
]
62+
substitutions.extend(args.subst)
63+
substitutions.append(('#_MARKER_#', '%'))
64+
65+
# Starting with processor.py because it's always the same.
66+
commands = [
67+
# FIXME: 'foo.c' + 'hecked.c' is a terrible hack; find the right way to do this.
68+
'3c -alltypes -addcr %s -- | FileCheck -match-full-lines -check-prefixes="CHECK_ALL","CHECK" %s',
69+
'3c -addcr %s -- | FileCheck -match-full-lines -check-prefixes="CHECK_NOALL","CHECK" %s',
70+
'3c -addcr %s -- | %clang -c -fcheckedc-extension -x c -o /dev/null -',
71+
'3c -output-postfix=checked -alltypes %s',
72+
'3c -alltypes %shecked.c -- | count 0',
73+
'rm %shecked.c',
74+
]
75+
commands = lit.TestRunner.applySubstitutions(commands, substitutions)
76+
77+
class FakeTestConfig:
78+
def __init__(self):
79+
self.pipefail = True # Is this always OK?
80+
self.environment = dict(os.environ)
81+
82+
class FakeTest:
83+
def __init__(self):
84+
self.config = FakeTestConfig()
85+
86+
class FakeLitConfig:
87+
def __init__(self):
88+
self.isWindows = platform.system() == 'windows'
89+
# Let the calling `lit` handle any timeout.
90+
self.maxIndividualTestTime = 0
91+
92+
res = lit.TestRunner.executeScriptInternal(
93+
FakeTest(), FakeLitConfig(), tmpBase, commands, os.getcwd())
94+
if isinstance(res, lit.Test.Result):
95+
die('Error: executeScriptInternal returned unexpected Result(%s, %r)' %
96+
(res.code.name, res.output))
97+
98+
out, err, exitCode, timeoutInfo = res
99+
sys.stdout.write(out)
100+
sys.stderr.write(err)
101+
sys.exit(exitCode)

clang/test/3C/b9_allsafestructp.c

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
// RUN: 3c -alltypes -addcr %s -- | FileCheck -match-full-lines -check-prefixes="CHECK_ALL","CHECK" %s
2-
// RUN: 3c -addcr %s -- | FileCheck -match-full-lines -check-prefixes="CHECK_NOALL","CHECK" %s
3-
// RUN: 3c -addcr %s -- | %clang -c -fcheckedc-extension -x c -o /dev/null -
4-
// RUN: 3c -output-postfix=checked -alltypes %s
5-
// RUN: 3c -alltypes %S/b9_allsafestructp.checked.c -- | count 0
6-
// RUN: rm %S/b9_allsafestructp.checked.c
1+
// RUN: %S/3c-regtest.py -t %t --subst %%clang '%clang' %s
72
#include <stddef.h>
83
#include <stddef.h>
94
extern _Itype_for_any(T) void *calloc(size_t nmemb, size_t size) : itype(_Array_ptr<T>) byte_count(nmemb * size);

0 commit comments

Comments
 (0)