check_prefast_error.ps1 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #-------------------------------------------------------------------------------------------------------
  2. # Copyright (C) Microsoft. All rights reserved.
  3. # Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. #-------------------------------------------------------------------------------------------------------
  5. param (
  6. [string]$directory,
  7. [string]$logFile = ""
  8. )
  9. . "$PSScriptRoot\util.ps1"
  10. if ((Test-Path $directory) -eq 0) {
  11. Write-Error ("ERROR: Directory {0} does not exist. Cannot scan for prefast defects" -f $directory);
  12. Exit(-1)
  13. }
  14. if (Test-Path -Path $logFile) {
  15. Remove-Item $logFile -Force
  16. }
  17. # load rules
  18. $rulefilter = @{};
  19. if ($env:VS140COMNTOOLS -ne "") {
  20. [xml]$ruleset = Get-Content "$env:VS140COMNTOOLS\..\..\Team Tools\Static Analysis Tools\Rule Sets\NativeRecommendedRules.ruleset"
  21. foreach ($r in $ruleset.RuleSet.Rules.Rule) {
  22. $rulefilter[$r.Id] = $r.Action;
  23. }
  24. } else {
  25. WriteMessage "WARNING: No ruleset found. No filter applied."
  26. }
  27. # load all prefast results
  28. $files = Get-ChildItem -recurse ("{0}\vc.nativecodeanalysis.all.xml" -f $directory)
  29. $filecount = 0;
  30. $count = 0;
  31. foreach ($file in $files) {
  32. $filecount++;
  33. [xml]$x = Get-Content $file
  34. foreach ($d in $x.DEFECTS.DEFECT) {
  35. if ($rulefilter.Count -eq 0 -or $rulefilter[("C{0}" -f $d.DEFECTCODE)] -eq "Warning") {
  36. WriteErrorMessage ("{0}{1}({2}): warning C{3}: {4}" -f $d.SFA.FILEPATH, $d.SFA.FILENAME, $d.SFA.LINE, $d.DEFECTCODE, $d.DESCRIPTION)
  37. $count++;
  38. }
  39. }
  40. }
  41. if ($count -ne 0) {
  42. WriteErrorMessage ("ERROR: {0} prefast warning detected" -f $count)
  43. } elseif ($filecount -ne 0) {
  44. WriteMessage "No prefast warning detected"
  45. } else {
  46. WriteMessage "No prefast result found"
  47. }
  48. exit $count