Skip to content

Commit 2845080

Browse files
committed
Fixes issue #60. Support suppression of whole directories.
1 parent 124d5ce commit 2845080

File tree

8 files changed

+109
-43
lines changed

8 files changed

+109
-43
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.googlecode.cppcheclipse.core;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.io.File;
6+
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
import com.googlecode.cppcheclipse.tests.MockPreferenceStore;
11+
12+
public class SuppressionProfileTest {
13+
14+
private SuppressionProfile profile;
15+
16+
@Before
17+
public void setup() {
18+
profile = new SuppressionProfile(new MockPreferenceStore(), null);
19+
}
20+
21+
@Test
22+
public void testFileSuppression() {
23+
File file = new File("/test/testfile");
24+
profile.addFileSuppression(file);
25+
assertTrue(profile.isFileSuppressed(file));
26+
}
27+
28+
@Test
29+
public void testFolderSuppression() {
30+
File file = new File("/test/testfolder");
31+
profile.addFileSuppression(file);
32+
assertTrue(profile.isFileSuppressed(new File("/test/testfolder/testfile")));
33+
profile.addFileSuppression(file);
34+
assertFalse(profile.isFileSuppressed(new File("/test/testfolder2/testfile")));
35+
}
36+
37+
@Test
38+
public void testLineSuppression() {
39+
File file = new File("/test/testfile");
40+
profile.addProblemInLineSuppression(file, "id1", 12);
41+
assertTrue(profile.isProblemInLineSuppressed(file, "id1", 12));
42+
}
43+
44+
@Test
45+
public void testProblemSuppression() {
46+
File file = new File("/test/testfile");
47+
profile.addProblemSuppression(file, "id1");
48+
assertTrue(profile.isProblemInLineSuppressed(file, "id1", 12));
49+
}
50+
51+
@Test
52+
public void testProblemSuppressionForSuppressedFile() {
53+
File file = new File("/test/testfolder");
54+
profile.addFileSuppression(file);
55+
assertTrue(profile.isProblemInLineSuppressed(new File("/test/testfolder"), "id1", 12));
56+
}
57+
}

com.googlecode.cppcheclipse.core/src/com/googlecode/cppcheclipse/core/Suppression.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public boolean isFileSuppression() {
6666
return problemId.equals(PROBLEM_ID_ALL);
6767
}
6868

69+
public boolean isSuppression(File absoluteFile, IProject project) {
70+
return isSuppression(absoluteFile, PROBLEM_ID_ALL, LINE_ALL, project);
71+
}
72+
6973
public boolean isAllLines() {
7074
return line == LINE_ALL;
7175
}
@@ -78,7 +82,7 @@ public boolean isAllLines() {
7882
* @return true if the given problem should be suppressed
7983
*/
8084
public boolean isSuppression(File file, String problemId, int line, IProject project) {
81-
if (!file.equals(getFile(project)))
85+
if (!file.equals(getFile(project)) && !file.toString().startsWith(getFile(project).toString() + File.separator))
8286
return false;
8387

8488
if (isFileSuppression())

com.googlecode.cppcheclipse.core/src/com/googlecode/cppcheclipse/core/SuppressionProfile.java

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.IOException;
55
import java.util.Collection;
66
import java.util.Iterator;
7+
import java.util.LinkedList;
78
import java.util.List;
89
import java.util.StringTokenizer;
910

@@ -16,16 +17,16 @@
1617

1718
public class SuppressionProfile implements TableModel<Suppression> {
1819
private static final String DELIMITER = "!";
19-
20+
2021
// contains the suppressions per file
21-
private final ListMultimap<File, Suppression> suppressionList;
22+
private final List<Suppression> suppressionList;
2223
private final IPreferenceStore projectPreferences;
2324
private final IProject project;
2425

2526
public SuppressionProfile(IPreferenceStore projectPreferences,
2627
IProject project) {
2728
this.projectPreferences = projectPreferences;
28-
this.suppressionList = LinkedListMultimap.create();
29+
this.suppressionList = new LinkedList<Suppression>();
2930
this.project = project;
3031
load();
3132
}
@@ -47,9 +48,7 @@ private void load() {
4748

4849
public void save() throws IOException {
4950
StringBuffer suppressions = new StringBuffer();
50-
Iterator<?> iterator = suppressionList.values().iterator();
51-
while (iterator.hasNext()) {
52-
Suppression suppression = (Suppression) iterator.next();
51+
for (Suppression suppression : suppressionList) {
5352
suppressions.append(suppression.serialize()).append(DELIMITER);
5453
}
5554

@@ -62,7 +61,7 @@ public void save() throws IOException {
6261
}
6362

6463
private void addSuppression(Suppression suppression) {
65-
suppressionList.put(suppression.getFile(project), suppression);
64+
suppressionList.add(suppression);
6665
}
6766

6867
public Suppression addFileSuppression(File file) {
@@ -81,7 +80,7 @@ public void addProblemInLineSuppression(File file, String problemId,
8180
}
8281

8382
public void remove(Suppression suppression) {
84-
suppressionList.remove(suppression.getFile(project), suppression);
83+
suppressionList.remove(suppression);
8584
}
8685

8786
public void removeAll() {
@@ -96,11 +95,11 @@ private File makeAbsoluteFile(File file) {
9695
}
9796

9897
public boolean isFileSuppressed(File file) {
99-
List<Suppression> suppressions = suppressionList
100-
.get(makeAbsoluteFile(file));
101-
for (Suppression suppression : suppressions) {
102-
if (suppression.isFileSuppression())
98+
File absoluteFile = makeAbsoluteFile(file);
99+
for (Suppression suppression : suppressionList) {
100+
if (suppression.isSuppression(absoluteFile, project)) {
103101
return true;
102+
}
104103
}
105104
return false;
106105
}
@@ -111,28 +110,25 @@ public boolean isProblemInLineSuppressed(File file, String problemId,
111110
if (file == null) {
112111
return false;
113112
}
114-
List<Suppression> suppressions = suppressionList
115-
.get(makeAbsoluteFile(file));
116-
for (Suppression suppression : suppressions) {
117-
if (suppression.isFileSuppression()) {
118-
return true;
119-
}
120-
if (suppression.isSuppression(file, problemId, line, project)) {
113+
File absoluteFile = makeAbsoluteFile(file);
114+
for (Suppression suppression : suppressionList) {
115+
if (suppression.isSuppression(absoluteFile, problemId, line,
116+
project)) {
121117
return true;
122118
}
123119
}
124120
return false;
125121
}
126122

127-
public Collection<Suppression> getSuppressions() {
128-
return suppressionList.values();
123+
public final Collection<Suppression> getSuppressions() {
124+
return suppressionList;
129125
}
130-
126+
131127
public Iterator<Suppression> iterator() {
132-
return suppressionList.values().iterator();
128+
return suppressionList.iterator();
133129
}
134-
130+
135131
public Suppression[] toArray() {
136-
return (Suppression[]) suppressionList.values().toArray(new Suppression[0]);
132+
return (Suppression[]) suppressionList.toArray(new Suppression[0]);
137133
}
138134
}

com.googlecode.cppcheclipse.ui/src/com/googlecode/cppcheclipse/ui/Messages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public class Messages extends NLS {
7979
public static String SuppressionsTable_FileSelection;
8080
public static String TableEditor_FileSelectionErrorExactlyOne;
8181
public static String TableEditor_FileSelectionErrorFile;
82+
public static String TableEditor_FileSelectionErrorFileFolder;
8283
public static String SuppressionsTable_FileSelectionMessage;
8384
public static String SuppressProblemInLineResolution_Label;
8485
public static String SuppressProblemResolution_Label;

com.googlecode.cppcheclipse.ui/src/com/googlecode/cppcheclipse/ui/messages.properties

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,22 @@ SuppressionsTable_AllProblems=All problems
9090
SuppressionsTable_ColumnFilename=Filename
9191
SuppressionsTable_ColumnLine=Line
9292
SuppressionsTable_ColumnProblem=Problem
93-
SuppressionsTable_FileSelection=File Selection
94-
SuppressionsTable_FileSelectionMessage=Select a file which you want to exclude from future runs of cppcheck:
93+
SuppressionsTable_FileSelection=File/Folder Selection
94+
SuppressionsTable_FileSelectionMessage=Select a file or folder which you want to exclude from future runs of cppcheck:
9595

9696
AppendagePropertyPage_AppendageLabel=Appendage of implementation files
9797
AppendagePropertyPage_Description=Add all files, which should be appended to each check (--append=<file>).
9898
AppendageTable_ColumnFile=File
9999
AppendageTable_FileSelection=File Selection
100100
AppendageTable_FileSelectionMessage=Select a file which you want to append to each checked file.
101101

102-
TableEditor_Add=Add File
103-
TableEditor_AddExternal=Add External File
102+
TableEditor_Add=Add
103+
TableEditor_AddExternal=Add External
104104
TableEditor_Remove=Remove
105105
TableEditor_RemoveAll=Remove All
106106
TableEditor_FileSelectionErrorExactlyOne=You must select exactly one item\!
107107
TableEditor_FileSelectionErrorFile=You must select a file\!
108+
TableEditor_FileSelectionErrorFileFolder=You must select a file or folder\!
108109
AdvancedSettingsPropertyPage_AdvancedArguments=Advanced command line arguments
109110
AdvancedSettingsPropertyPage_Description=Only use this if you are experienced with the command line version of cppcheck.
110111

com.googlecode.cppcheclipse.ui/src/com/googlecode/cppcheclipse/ui/preferences/AppendageTable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import java.io.File;
44

5-
import org.eclipse.core.resources.IFile;
65
import org.eclipse.core.resources.IProject;
6+
import org.eclipse.core.resources.IResource;
77
import org.eclipse.jface.viewers.ILabelProviderListener;
88
import org.eclipse.jface.viewers.ITableLabelProvider;
99
import org.eclipse.swt.SWT;
@@ -99,7 +99,7 @@ public void widgetSelected(SelectionEvent e) {
9999
}
100100

101101
protected void addPressed() {
102-
IFile resource = openProjectFile(Messages.AppendageTable_FileSelection, Messages.AppendageTable_FileSelectionMessage, project);
102+
IResource resource = openProjectFile(Messages.AppendageTable_FileSelection, Messages.AppendageTable_FileSelectionMessage, project, false);
103103
if (resource != null) {
104104
File file = resource.getProjectRelativePath().toFile();
105105
getModel().add(file);

com.googlecode.cppcheclipse.ui/src/com/googlecode/cppcheclipse/ui/preferences/SuppressionsTable.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import java.io.File;
44

5-
import org.eclipse.core.resources.IFile;
65
import org.eclipse.core.resources.IProject;
6+
import org.eclipse.core.resources.IResource;
77
import org.eclipse.core.runtime.CoreException;
88
import org.eclipse.jface.viewers.ILabelProviderListener;
99
import org.eclipse.jface.viewers.ITableLabelProvider;
@@ -128,8 +128,8 @@ public void widgetSelected(SelectionEvent e) {
128128
}
129129

130130
protected void addPressed() {
131-
IFile file = openProjectFile(Messages.SuppressionsTable_FileSelection,
132-
Messages.SuppressionsTable_FileSelectionMessage, project);
131+
IResource file = openProjectFile(Messages.SuppressionsTable_FileSelection,
132+
Messages.SuppressionsTable_FileSelectionMessage, project, true);
133133
if (file != null) {
134134
SuppressionProfile profile = getModel();
135135
Suppression suppression = profile.addFileSuppression(file

com.googlecode.cppcheclipse.ui/src/com/googlecode/cppcheclipse/ui/preferences/TableEditor.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.eclipse.core.resources.IFile;
88
import org.eclipse.core.resources.IFolder;
99
import org.eclipse.core.resources.IProject;
10+
import org.eclipse.core.resources.IResource;
1011
import org.eclipse.core.runtime.IStatus;
1112
import org.eclipse.core.runtime.Status;
1213
import org.eclipse.jface.dialogs.Dialog;
@@ -472,8 +473,8 @@ protected Model getModel() {
472473
return (Model) getTableViewer().getInput();
473474
}
474475

475-
protected IFile openProjectFile(String title, String message,
476-
IProject project) {
476+
protected IResource openProjectFile(String title, String message,
477+
IProject project, final boolean allowFolders) {
477478
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
478479
.getShell();
479480
ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
@@ -504,17 +505,23 @@ public IStatus validate(Object[] selection) {
504505
Messages.TableEditor_FileSelectionErrorExactlyOne);
505506
}
506507
Object element = selection[0];
507-
if (!(element instanceof IFile)) {
508-
return new Status(Status.ERROR, CppcheclipsePlugin.getId(),
509-
Messages.TableEditor_FileSelectionErrorFile);
508+
if ((element instanceof IFile) || (allowFolders && element instanceof IFolder)) {
509+
return new Status(Status.OK, CppcheclipsePlugin.getId(), ""); //$NON-NLS-1$
510+
510511
}
511-
512-
return new Status(Status.OK, CppcheclipsePlugin.getId(), ""); //$NON-NLS-1$
512+
513+
final String errorMessage;
514+
if (allowFolders) {
515+
errorMessage = Messages.TableEditor_FileSelectionErrorFileFolder;
516+
} else {
517+
errorMessage = Messages.TableEditor_FileSelectionErrorFile;
518+
}
519+
return new Status(Status.ERROR, CppcheclipsePlugin.getId(), errorMessage);
513520
}
514521
});
515522

516523
if (dialog.open() == Dialog.OK) {
517-
return (IFile) dialog.getFirstResult();
524+
return (IResource) dialog.getFirstResult();
518525
}
519526
return null;
520527
}

0 commit comments

Comments
 (0)