blob: 03124333fe7bf71b8c70a3d20c007ed1d68cd306 [file] [log] [blame]
Reid Kleckner2ae37c12017-04-05 18:56:48 +00001import lit.formats
2import lit.TestRunner
3
4# Custom format class for static analyzer tests
5class AnalyzerTest(lit.formats.ShTest):
6
George Karpenkov2997a302018-08-13 23:12:43 +00007 def __init__(self, execute_external, use_z3_solver=False):
8 super(AnalyzerTest, self).__init__(execute_external)
9 self.use_z3_solver = use_z3_solver
10
Reid Kleckner2ae37c12017-04-05 18:56:48 +000011 def execute(self, test, litConfig):
Dominic Chenf94c1082017-06-15 17:05:07 +000012 results = []
Reid Kleckner2ae37c12017-04-05 18:56:48 +000013
Dominic Chenf94c1082017-06-15 17:05:07 +000014 # Parse any test requirements ('REQUIRES: ')
Dominic Chenc0402c62017-07-12 21:43:42 +000015 saved_test = test
Dominic Chenf94c1082017-06-15 17:05:07 +000016 lit.TestRunner.parseIntegratedTestScript(test)
17
18 if 'z3' not in test.requires:
19 results.append(self.executeWithAnalyzeSubstitution(
Dominic Chenc0402c62017-07-12 21:43:42 +000020 saved_test, litConfig, '-analyzer-constraints=range'))
Dominic Chenf94c1082017-06-15 17:05:07 +000021
Dominic Chenc0402c62017-07-12 21:43:42 +000022 if results[-1].code == lit.Test.FAIL:
Dominic Chenf94c1082017-06-15 17:05:07 +000023 return results[-1]
Reid Kleckner2ae37c12017-04-05 18:56:48 +000024
25 # If z3 backend available, add an additional run line for it
George Karpenkov2997a302018-08-13 23:12:43 +000026 if self.use_z3_solver == '1':
27 assert(test.config.clang_staticanalyzer_z3 == '1')
Dominic Chenf94c1082017-06-15 17:05:07 +000028 results.append(self.executeWithAnalyzeSubstitution(
29 saved_test, litConfig, '-analyzer-constraints=z3 -DANALYZER_CM_Z3'))
Reid Kleckner2ae37c12017-04-05 18:56:48 +000030
Dominic Chenf94c1082017-06-15 17:05:07 +000031 # Combine all result outputs into the last element
32 for x in results:
33 if x != results[-1]:
34 results[-1].output = x.output + results[-1].output
35
36 if results:
37 return results[-1]
38 return lit.Test.Result(lit.Test.UNSUPPORTED,
39 "Test requires the following unavailable features: z3")
Reid Kleckner2ae37c12017-04-05 18:56:48 +000040
41 def executeWithAnalyzeSubstitution(self, test, litConfig, substitution):
42 saved_substitutions = list(test.config.substitutions)
43 test.config.substitutions.append(('%analyze', substitution))
44 result = lit.TestRunner.executeShTest(test, litConfig,
Dominic Chenf94c1082017-06-15 17:05:07 +000045 self.execute_external)
Reid Kleckner2ae37c12017-04-05 18:56:48 +000046 test.config.substitutions = saved_substitutions
47
48 return result