1+ # takes paths like this:
2+ # c/cert/src/rules/DCL39-C/InformationLeakageAcrossTrustBoundariesC.ql
3+ # c/common/test/rules/informationleakageacrossboundaries/InformationLeakageAcrossBoundaries.expected
4+ # c/common/test/rules/informationleakageacrossboundaries/InformationLeakageAcrossBoundaries.ql
5+ # c/common/test/rules/informationleakageacrossboundaries/arrays.c
6+ # c/common/test/rules/informationleakageacrossboundaries/interprocedural.c
7+ # c/common/test/rules/informationleakageacrossboundaries/multilayer.c
8+ # c/common/test/rules/informationleakageacrossboundaries/test.c
9+ # c/misra/src/rules/RULE-18-8/VariableLengthArrayTypesUsed.ql
10+ # c/misra/src/rules/RULE-8-12/ValueImplicitEnumerationConstantNotUnique.ql
11+ # c/misra/test/rules/RULE-18-8/VariableLengthArrayTypesUsed.expected
12+ # c/misra/test/rules/RULE-18-8/test.c
13+ # c/misra/test/rules/RULE-8-12/ValueImplicitEnumerationConstantNotUnique.expected
14+ # c/misra/test/rules/RULE-8-12/test.c
15+ # cpp/cert/src/rules/DCL55-CPP/InformationLeakageAcrossTrustBoundaries.ql
16+ # cpp/common/test/rules/informationleakageacrossboundaries/InformationLeakageAcrossBoundaries.expected
17+ # cpp/common/test/rules/informationleakageacrossboundaries/InformationLeakageAcrossBoundaries.ql
18+ # cpp/common/test/rules/informationleakageacrossboundaries/InformationLeakageAcrossTrustBoundaries.expected
19+ # cpp/common/test/rules/informationleakageacrossboundaries/arrays.cpp
20+ # cpp/common/test/rules/informationleakageacrossboundaries/inheritance.cpp
21+ # cpp/common/test/rules/informationleakageacrossboundaries/interprocedural.cpp
22+ # cpp/common/test/rules/informationleakageacrossboundaries/multilayer.cpp
23+ # cpp/common/test/rules/informationleakageacrossboundaries/test.cpp
24+
25+ # And produces one or more rules for it. It does this by loading every rule
26+ # and computing the test directory for it. This test directory is then
27+ # used to see if a) it is a substring of the supplied path or if b) it
28+ # is a substring of the path once the substitution `/src/` -> `/test/` is
29+ # applied
30+
31+ function Get-RuleForPath {
32+ param ([Parameter (Mandatory )]
33+ [string ]
34+ $Path ,
35+ [ValidateSet (' c' , ' cpp' )]
36+ [string ]
37+ $Language
38+ )
39+
40+ # load all the queries for all languages
41+ $allQueries = @ ()
42+ $queriesToCheck = @ ()
43+
44+ # load all the queries
45+ foreach ($s in $AVAILABLE_SUITES ) {
46+ $allQueries += Get-RulesInSuite - Suite $s - Language $Language
47+ }
48+
49+ $modifiedPathWithReplacement = Join-Path (Resolve-Path . - Relative) $Path
50+ # repalce "src" with "test" to make it match up
51+ $sep = [IO.Path ]::DirectorySeparatorChar
52+ $modifiedPathWithReplacement = $modifiedPathWithReplacement.Replace ( ($sep + " src" + $sep + " rules" ), ($sep + " test" + $sep + " rules" ))
53+ $modifiedPath = Join-Path (Resolve-Path . - Relative) $Path
54+
55+
56+ $matchingRules = @ ()
57+
58+ # for each query, create the test directory
59+ foreach ($q in $allQueries ){
60+
61+ # get test directory
62+ $testDirectory = (Get-TestDirectory - RuleObject $q - Language $Language )
63+ # resolve path to be compatible
64+ $testPath = Join-Path (Resolve-Path . - Relative) $testDirectory
65+
66+ # see if the TEST directory is a substring of the full path
67+ if ($modifiedPath.StartsWith ($testPath )){
68+ $matchingRules += $q
69+ continue
70+ }
71+
72+ if ($modifiedPathWithReplacement.StartsWith ($testPath )){
73+ $matchingRules += $q
74+ continue
75+ }
76+ }
77+
78+ if ($matchingRules.Count -gt 0 ){
79+ return $matchingRules
80+ }
81+
82+ throw " Path does not appear to be part of a rule."
83+ }
0 commit comments