forked from jenkinsci/analysis-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreFastParser.java
executable file
·76 lines (70 loc) · 2.5 KB
/
PreFastParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package edu.hm.hafner.analysis.parser;
import java.util.Optional;
import java.util.regex.Matcher;
import edu.hm.hafner.analysis.Issue;
import edu.hm.hafner.analysis.IssueBuilder;
import edu.hm.hafner.analysis.RegexpLineParser;
/**
* A parser for Microsoft PREfast (aka Code Analysis for C/C++) XML files.
*
* @author Charles Chan
* @see <a href="http://msdn.microsoft.com/en-us/library/ms173498.aspx" ></a>
*/
public class PreFastParser extends RegexpLineParser {
private static final long serialVersionUID = 1409381677034028504L;
/*
* Microsoft PREfast static code analyzer produces XML files with the
* following schema.
*
* <?xml version="1.0" encoding="UTF-8"?>
* <DEFECTS>
* <DEFECT _seq="1">
* <SFA>
* <FILEPATH>d:\myproject\</FILEPATH>
* <FILENAME>filename.c</FILENAME>
* <LINE>102</LINE>
* <COLUMN>9</COLUMN>
* </SFA>
* <DEFECTCODE>28101</DEFECTCODE>
* <DESCRIPTION>A long message</DESCRIPTION>
* <FUNCTION>DriverEntry</FUNCTION>
* <DECORATED>DriverEntry@8</DECORATED>
* <FUNCLINE>102</FUNCLINE>
* <PATH/>
* </DEFECT>
* <DEFECT>
* ...
* </DEFECT>
* </DEFECTS>
*
* The following regular expression performs the following matches:
* <DEFECT.*?> ... </DEFECT>
* - the tag containing 1 violation (seq number ignored)
* .*?
* - zero or more characters
* <FILENAME>(.+?)</FILENAME>
* - capture group 1 to get the filename
* <LINE>(.+?)</LINE>
* - capture group 2 to get the line number
* <DEFECTCODE>(.+?)</DEFECTCODE>
* - capture group 3 to get the error code
* <DESCRIPTION>(.+?)</DESCRIPTION>
* - capture group 4 to get the description
*/
private static final String PREFAST_PATTERN_WARNING = "<DEFECT.*?>.*?<FILENAME>(.+?)</FILENAME>.*?<LINE>(.+?)"
+ "</LINE>.*?<DEFECTCODE>(.+?)</DEFECTCODE>.*?<DESCRIPTION>(.+?)</DESCRIPTION>.*?</DEFECT>";
/**
* Creates a new instance of {@link PreFastParser}.
*/
public PreFastParser() {
super(PREFAST_PATTERN_WARNING);
}
@Override
protected Optional<Issue> createIssue(final Matcher matcher, final IssueBuilder builder) {
return builder.setFileName(matcher.group(1))
.setLineStart(matcher.group(2))
.setCategory(matcher.group(3))
.setMessage(matcher.group(4))
.buildOptional();
}
}