Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlena Laskavaia2016-01-07 23:12:51 -0500
committerElena Laskavaia2016-01-11 12:49:09 -0500
commit05daa126a0b6c09907dbe5f842156ec9d15d9d44 (patch)
treec5b260c579b4b556a7963f0bddfd518a196dcc52 /codan/org.eclipse.cdt.codan.core
parent12843ef1d7d1f60504f58e79c11693210fd8b735 (diff)
downloadorg.eclipse.cdt-05daa126a0b6c09907dbe5f842156ec9d15d9d44.tar.gz
org.eclipse.cdt-05daa126a0b6c09907dbe5f842156ec9d15d9d44.tar.xz
org.eclipse.cdt-05daa126a0b6c09907dbe5f842156ec9d15d9d44.zip
Bug 383576 - Ability to ignore codan errors using line comments
Change-Id: I806e1787fb6cc3be8865cee1d397d581ae4acd8e Signed-off-by: Alena Laskavaia <elaskavaia.cdt@gmail.com>
Diffstat (limited to 'codan/org.eclipse.cdt.codan.core')
-rw-r--r--codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/AbstractChecker.java4
-rw-r--r--codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/AbstractCheckerWithProblemPreferences.java35
-rw-r--r--codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/Messages.java1
-rw-r--r--codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/Messages.properties1
-rw-r--r--codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/RootProblemPreference.java17
-rw-r--r--codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/SuppressionCommentProblemPreference.java34
6 files changed, 79 insertions, 13 deletions
diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/AbstractChecker.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/AbstractChecker.java
index 871611809b..eaf81ce1a2 100644
--- a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/AbstractChecker.java
+++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/AbstractChecker.java
@@ -50,7 +50,7 @@ public abstract class AbstractChecker implements IChecker {
* internationalization)
*/
public void reportProblem(String id, IFile file, int lineNumber, Object... args) {
- getProblemReporter().reportProblem(id, createProblemLocation(file, lineNumber), args);
+ reportProblem(id, createProblemLocation(file, lineNumber), args);
}
/**
@@ -103,7 +103,7 @@ public abstract class AbstractChecker implements IChecker {
* - line
*/
public void reportProblem(String id, IFile file, int lineNumber) {
- getProblemReporter().reportProblem(id, createProblemLocation(file, lineNumber), EMPTY_OBJECT_ARRAY);
+ reportProblem(id, createProblemLocation(file, lineNumber), EMPTY_OBJECT_ARRAY);
}
/**
diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/AbstractCheckerWithProblemPreferences.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/AbstractCheckerWithProblemPreferences.java
index c18a124ade..7518bb52b3 100644
--- a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/AbstractCheckerWithProblemPreferences.java
+++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/AbstractCheckerWithProblemPreferences.java
@@ -22,6 +22,7 @@ import org.eclipse.cdt.codan.core.param.LaunchModeProblemPreference;
import org.eclipse.cdt.codan.core.param.ListProblemPreference;
import org.eclipse.cdt.codan.core.param.MapProblemPreference;
import org.eclipse.cdt.codan.core.param.RootProblemPreference;
+import org.eclipse.cdt.codan.core.param.SuppressionCommentProblemPreference;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
@@ -43,6 +44,7 @@ public abstract class AbstractCheckerWithProblemPreferences extends AbstractChec
CheckerLaunchMode.RUN_ON_DEMAND,
CheckerLaunchMode.RUN_ON_FULL_BUILD,
CheckerLaunchMode.RUN_ON_INC_BUILD);
+ getSuppressionCommentPreference(problem).setValue(SuppressionCommentProblemPreference.generateDefaultComment(problem));
}
/**
@@ -66,9 +68,18 @@ public abstract class AbstractCheckerWithProblemPreferences extends AbstractChec
}
/**
+ * @param problem - problem for which preference is extracted
+ * @return suppression comment preference
+ * @since 4.0
+ */
+ public SuppressionCommentProblemPreference getSuppressionCommentPreference(IProblem problem) {
+ return getTopLevelPreference(problem).getSuppressionCommentPreference();
+ }
+
+ /**
* User can scope out some resources for this checker. Checker can use this
* call to test if it should run on this resource at all or not. Test should
- * be done within processResource method not in enabledInContext.
+ * be done within processResource method.
* This test uses user "scope" preference for the all problems that this
* checker can produce.
*
@@ -109,10 +120,7 @@ public abstract class AbstractCheckerWithProblemPreferences extends AbstractChec
@Override
public void reportProblem(String problemId, IProblemLocation loc, Object... args) {
- if (shouldProduceProblem(getProblemById(problemId, loc.getFile()),
- loc.getFile().getFullPath())) {
- super.reportProblem(problemId, loc, args);
- }
+ reportProblem(getProblemById(problemId, loc.getFile()), loc, args);
}
/**
@@ -125,11 +133,26 @@ public abstract class AbstractCheckerWithProblemPreferences extends AbstractChec
* @since 2.0
*/
public void reportProblem(IProblem pr, IProblemLocation loc, Object... args) {
- if (shouldProduceProblem(pr, loc.getFile().getFullPath()))
+ if (shouldProduceProblem(pr, loc, args))
super.reportProblem(pr.getId(), loc, args);
}
/**
+ * Checks if problem should be reported, this implementation only checks
+ * suppression by scope, but subclass should override,
+ * to implement any other filtering, such as suppression by filter or by comment.
+ * Call super to check for scope suppression as well.
+ *
+ * @param problem - problem kind
+ * @param loc - location
+ * @param args - arguments
+ * @since 4.0
+ */
+ protected boolean shouldProduceProblem(IProblem problem, IProblemLocation loc, Object... args) {
+ return shouldProduceProblem(problem, loc.getFile().getFullPath());
+ }
+
+ /**
* Adds a parameter
*
* @param problem
diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/Messages.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/Messages.java
index 4591d28854..bf8a1982f1 100644
--- a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/Messages.java
+++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/Messages.java
@@ -18,6 +18,7 @@ import org.eclipse.osgi.util.NLS;
*/
class Messages extends NLS {
public static String FileScopeProblemPreference_Label;
+ public static String SuppressionCommentProblemPreference_Label;
static {
NLS.initializeMessages(Messages.class.getName(), Messages.class);
diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/Messages.properties b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/Messages.properties
index 04c64faf74..c6675dda2b 100644
--- a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/Messages.properties
+++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/Messages.properties
@@ -10,3 +10,4 @@
# IBM Corporation
###############################################################################
FileScopeProblemPreference_Label=Exclusion and Inclusion
+SuppressionCommentProblemPreference_Label=Suppression Comment
diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/RootProblemPreference.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/RootProblemPreference.java
index 7dcf296513..bab72b6ce7 100644
--- a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/RootProblemPreference.java
+++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/RootProblemPreference.java
@@ -10,7 +10,6 @@
*******************************************************************************/
package org.eclipse.cdt.codan.core.param;
-
/**
* Common problem preference root for most of the codan problems
*
@@ -24,26 +23,34 @@ public class RootProblemPreference extends MapProblemPreference {
public static final String KEY = PARAM;
/**
- * Default constructor
+ * Default constructor
*/
public RootProblemPreference() {
super(KEY, ""); //$NON-NLS-1$
addChildDescriptor(new FileScopeProblemPreference());
addChildDescriptor(new LaunchModeProblemPreference());
+ addChildDescriptor(new SuppressionCommentProblemPreference());
}
/**
* @return scope preference
*/
public FileScopeProblemPreference getScopePreference() {
- return (FileScopeProblemPreference) getChildDescriptor(
- FileScopeProblemPreference.KEY);
+ return (FileScopeProblemPreference) getChildDescriptor(FileScopeProblemPreference.KEY);
}
+
/**
- * @return launch mode
+ * @return launch mode preference
*/
public LaunchModeProblemPreference getLaunchModePreference() {
return (LaunchModeProblemPreference) getChildDescriptor(LaunchModeProblemPreference.KEY);
+ }
+ /**
+ * @return suppression comment preference
+ * @since 4.0
+ */
+ public SuppressionCommentProblemPreference getSuppressionCommentPreference() {
+ return (SuppressionCommentProblemPreference) getChildDescriptor(SuppressionCommentProblemPreference.KEY);
}
}
diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/SuppressionCommentProblemPreference.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/SuppressionCommentProblemPreference.java
new file mode 100644
index 0000000000..8506b974cd
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/SuppressionCommentProblemPreference.java
@@ -0,0 +1,34 @@
+/*******************************************************************************
+ * Copyright (c) 2016 Alena Laskavaia
+ * 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:
+ * Alena Laskavaia - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.cdt.codan.core.param;
+
+import java.text.MessageFormat;
+
+import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy;
+
+/**
+ * Preference for suppressing a problem using code comments. Automatically added to all problems.
+ * @since 4.0
+ */
+public class SuppressionCommentProblemPreference extends BasicProblemPreference implements IProblemPreference {
+ public static final String KEY = "suppression_comment"; //$NON-NLS-1$;
+ // Even if using English name it is really a keyword, so no externalizable.
+ public static final String KEYWORD = "@suppress(\"{0}\")"; //$NON-NLS-1$;
+
+ public SuppressionCommentProblemPreference() {
+ super(KEY, Messages.SuppressionCommentProblemPreference_Label, PreferenceType.TYPE_STRING);
+ }
+
+ public static String generateDefaultComment(IProblemWorkingCopy problem) {
+ String name = problem.getName();
+ return MessageFormat.format(KEYWORD, name);
+ }
+}

Back to the top