• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import os
2import sys
3
4
5class TestingConfig:
6    """"
7    TestingConfig - Information on the tests inside a suite.
8    """
9
10    @staticmethod
11    def fromdefaults(litConfig):
12        """
13        fromdefaults(litConfig) -> TestingConfig
14
15        Create a TestingConfig object with default values.
16        """
17        # Set the environment based on the command line arguments.
18        environment = {
19            'PATH' : os.pathsep.join(litConfig.path +
20                                     [os.environ.get('PATH','')]),
21            'LLVM_DISABLE_CRASH_REPORT' : '1',
22            }
23
24        pass_vars = ['LIBRARY_PATH', 'LD_LIBRARY_PATH', 'SYSTEMROOT', 'TERM',
25                     'CLANG', 'LD_PRELOAD', 'ASAN_OPTIONS', 'UBSAN_OPTIONS',
26                     'LSAN_OPTIONS', 'ADB', 'ANDROID_SERIAL',
27                     'SANITIZER_IGNORE_CVE_2016_2143', 'TMPDIR', 'TMP', 'TEMP',
28                     'TEMPDIR', 'AVRLIT_BOARD', 'AVRLIT_PORT',
29                     'FILECHECK_DUMP_INPUT_ON_FAILURE']
30        for var in pass_vars:
31            val = os.environ.get(var, '')
32            # Check for empty string as some variables such as LD_PRELOAD cannot be empty
33            # ('') for OS's such as OpenBSD.
34            if val:
35                environment[var] = val
36
37        if sys.platform == 'win32':
38            environment.update({
39                    'INCLUDE' : os.environ.get('INCLUDE',''),
40                    'PATHEXT' : os.environ.get('PATHEXT',''),
41                    'PYTHONUNBUFFERED' : '1',
42                    'TEMP' : os.environ.get('TEMP',''),
43                    'TMP' : os.environ.get('TMP',''),
44                    })
45
46        # Set the default available features based on the LitConfig.
47        available_features = []
48        if litConfig.useValgrind:
49            available_features.append('valgrind')
50            if litConfig.valgrindLeakCheck:
51                available_features.append('vg_leak')
52
53        return TestingConfig(None,
54                             name = '<unnamed>',
55                             suffixes = set(),
56                             test_format = None,
57                             environment = environment,
58                             substitutions = [],
59                             unsupported = False,
60                             test_exec_root = None,
61                             test_source_root = None,
62                             excludes = [],
63                             available_features = available_features,
64                             pipefail = True)
65
66    def load_from_path(self, path, litConfig):
67        """
68        load_from_path(path, litConfig)
69
70        Load the configuration module at the provided path into the given config
71        object.
72        """
73
74        # Load the config script data.
75        data = None
76        f = open(path)
77        try:
78            data = f.read()
79        except:
80            litConfig.fatal('unable to load config file: %r' % (path,))
81        f.close()
82
83        # Execute the config script to initialize the object.
84        cfg_globals = dict(globals())
85        cfg_globals['config'] = self
86        cfg_globals['lit_config'] = litConfig
87        cfg_globals['__file__'] = path
88        try:
89            exec(compile(data, path, 'exec'), cfg_globals, None)
90            if litConfig.debug:
91                litConfig.note('... loaded config %r' % path)
92        except SystemExit:
93            e = sys.exc_info()[1]
94            # We allow normal system exit inside a config file to just
95            # return control without error.
96            if e.args:
97                raise
98        except:
99            import traceback
100            litConfig.fatal(
101                'unable to parse config file %r, traceback: %s' % (
102                    path, traceback.format_exc()))
103
104        self.finish(litConfig)
105
106    def __init__(self, parent, name, suffixes, test_format,
107                 environment, substitutions, unsupported,
108                 test_exec_root, test_source_root, excludes,
109                 available_features, pipefail, limit_to_features = [],
110                 is_early = False, parallelism_group = ""):
111        self.parent = parent
112        self.name = str(name)
113        self.suffixes = set(suffixes)
114        self.test_format = test_format
115        self.environment = dict(environment)
116        self.substitutions = list(substitutions)
117        self.unsupported = unsupported
118        self.test_exec_root = test_exec_root
119        self.test_source_root = test_source_root
120        self.excludes = set(excludes)
121        self.available_features = set(available_features)
122        self.pipefail = pipefail
123        # This list is used by TestRunner.py to restrict running only tests that
124        # require one of the features in this list if this list is non-empty.
125        # Configurations can set this list to restrict the set of tests to run.
126        self.limit_to_features = set(limit_to_features)
127        # Whether the suite should be tested early in a given run.
128        self.is_early = bool(is_early)
129        self.parallelism_group = parallelism_group
130
131    def finish(self, litConfig):
132        """finish() - Finish this config object, after loading is complete."""
133
134        self.name = str(self.name)
135        self.suffixes = set(self.suffixes)
136        self.environment = dict(self.environment)
137        self.substitutions = list(self.substitutions)
138        if self.test_exec_root is not None:
139            # FIXME: This should really only be suite in test suite config
140            # files. Should we distinguish them?
141            self.test_exec_root = str(self.test_exec_root)
142        if self.test_source_root is not None:
143            # FIXME: This should really only be suite in test suite config
144            # files. Should we distinguish them?
145            self.test_source_root = str(self.test_source_root)
146        self.excludes = set(self.excludes)
147
148    @property
149    def root(self):
150        """root attribute - The root configuration for the test suite."""
151        if self.parent is None:
152            return self
153        else:
154            return self.parent.root
155
156class SubstituteCaptures:
157    """
158    Helper class to indicate that the substitutions contains backreferences.
159
160    This can be used as the following in lit.cfg to mark subsitutions as having
161    back-references::
162
163        config.substutions.append(('\b[^ ]*.cpp', SubstituteCaptures('\0.txt')))
164
165    """
166    def __init__(self, substitution):
167        self.substitution = substitution
168
169    def replace(self, pattern, replacement):
170        return self.substitution
171
172    def __str__(self):
173        return self.substitution
174
175    def __len__(self):
176        return len(self.substitution)
177
178    def __getitem__(self, item):
179        return self.substitution.__getitem__(item)
180
181