Skip to content

Commit ea1fa72

Browse files
committed
new scripts
1 parent c37db4f commit ea1fa72

File tree

4 files changed

+108
-3
lines changed

4 files changed

+108
-3
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function Get-LanguageForPath {
2+
param([Parameter(Mandatory)]
3+
[string]
4+
$Path)
5+
6+
$parts = $Path -split '/'
7+
8+
$Language = $parts[0]
9+
10+
foreach($L in $AVAILABLE_LANGUAGES){
11+
if($Language -eq $L){
12+
return $L
13+
}
14+
}
15+
16+
throw "Unsupported Language: $Language"
17+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}

scripts/PSCodingStandards/Get-RulesInPackageAndSuite.ps1

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ function Get-RulesInPackageAndSuite {
66
$Package,
77
[Parameter(Mandatory)]
88
[string]
9-
$Suite
10-
)
9+
$Suite,
10+
[Parameter(Mandatory)]
11+
[ValidateSet('c', 'cpp')]
12+
[string]
13+
$Language
14+
)
1115

1216
$rulesInPackage = @()
1317

@@ -30,6 +34,7 @@ function Get-RulesInPackageAndSuite {
3034
$queries | Add-Member -NotePropertyName __memberof_suite -NotePropertyValue $Suite
3135
$queries | Add-Member -NotePropertyName __memberof_package -NotePropertyValue $Package.BaseName
3236
$queries | Add-Member -NotePropertyName __memberof_rule -NotePropertyValue $n.Name
37+
$queries | Add-Member -NotePropertyName __memberof_language -NotePropertyValue $Language
3338

3439
$rulesInPackage += $queries
3540
}

scripts/PSCodingStandards/Get-RulesInSuite.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function Get-RulesInSuite {
1313

1414
foreach ($p in Get-Packages -Language $Language) {
1515
Write-Host "Reading package: [$Language/$Suite/$($p.BaseName)]"
16-
$tmpQueries += Get-RulesInPackageAndSuite -Package $p -Suite $Suite
16+
$tmpQueries += Get-RulesInPackageAndSuite -Package $p -Suite $Suite -Language $Language
1717
}
1818

1919
return $tmpQueries

0 commit comments

Comments
 (0)