blob: 53295324f1824ba63d0f70c2dc8e1a724023864b (
plain) (
blame)
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
77
78
79
80
81
82
|
/*******************************************************************************
* Copyright (c) 2012 Google, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Alex Ruiz - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.codan.internal.checkers.externaltool;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.cdt.codan.core.CodanRuntime;
import org.eclipse.cdt.codan.core.externaltool.AbstractOutputParser;
import org.eclipse.cdt.codan.core.externaltool.IProblemDisplay;
import org.eclipse.cdt.codan.core.externaltool.InvocationParameters;
import org.eclipse.cdt.codan.core.model.IProblemLocation;
import org.eclipse.cdt.codan.core.model.IProblemLocationFactory;
import org.eclipse.core.resources.IFile;
/**
* Parses the output of Cppcheck.
*
* @author alruiz@google.com (Alex Ruiz)
*
* @since 1.1
*/
public class CppcheckOutputParser extends AbstractOutputParser {
// sample line to parse:
//
// [/src/HelloWorld.cpp:19]: (style) The scope of the variable 'i' can be reduced
// ----------1--------- -2 --3-- ------------------4-------------------------
//
// groups:
// 1: file path and name
// 2: line where problem was found
// 3: problem severity
// 4: problem description
private static Pattern pattern = Pattern.compile("\\[(.*):(\\d+)\\]:\\s*\\((.*)\\)\\s*(.*)"); //$NON-NLS-1$
private final InvocationParameters parameters;
private final IProblemDisplay problemDisplay;
/**
* Constructor.
* @param parameters the parameters to pass to Cppcheck.
* @param problemDisplay displays any problems reported by Cppcheck as markers.
*/
public CppcheckOutputParser(InvocationParameters parameters, IProblemDisplay problemDisplay) {
this.parameters = parameters;
this.problemDisplay = problemDisplay;
}
@Override
public boolean parse(String line) {
Matcher matcher = pattern.matcher(line);
if (!matcher.matches()) {
return false;
}
String filePath = matcher.group(1);
if (parameters.getActualFilePath().equals(filePath)) {
int lineNumber = Integer.parseInt(matcher.group(2));
String severity = matcher.group(3);
String description = matcher.group(4);
IProblemLocation location = createProblemLocation(lineNumber);
problemDisplay.reportProblem(location, description, severity);
}
return true;
}
private IProblemLocation createProblemLocation(int lineNumber) {
IProblemLocationFactory factory = CodanRuntime.getInstance().getProblemLocationFactory();
IFile actualFile = (IFile) parameters.getActualFile();
return factory.createProblemLocation(actualFile, -1, -1, lineNumber);
}
@Override
public void reset() {}
}
|