Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFelix Morgner2017-05-16 12:44:53 +0000
committerNathan Ridge2017-10-04 15:13:54 +0000
commitd47cb6c4ea7bdf38d4db9d10efd18a02959b6e31 (patch)
tree5bb7b0fde77e2f5fdee66a0bf69a1381eedf58e3 /codan/org.eclipse.cdt.codan.checkers.ui
parentbf73bb58bc179f143190f52762d908f6a4851eb7 (diff)
downloadorg.eclipse.cdt-d47cb6c4ea7bdf38d4db9d10efd18a02959b6e31.tar.gz
org.eclipse.cdt-d47cb6c4ea7bdf38d4db9d10efd18a02959b6e31.tar.xz
org.eclipse.cdt-d47cb6c4ea7bdf38d4db9d10efd18a02959b6e31.zip
Bug 495842: Add a quick fix for codan problems that ignores them by adding a @suppress comment
Change-Id: I760d2d435010f219bdb94273a6450e4163fe472e Signed-off-by: Felix Morgner <fmorgner@hsr.ch>
Diffstat (limited to 'codan/org.eclipse.cdt.codan.checkers.ui')
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml3
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.java1
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.properties3
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixSuppressProblem.java75
4 files changed, 81 insertions, 1 deletions
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml b/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml
index 9a20fafef4d..8be4554c32f 100644
--- a/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml
@@ -79,5 +79,8 @@
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.CaseBreakQuickFixFallthroughAttribute"
problemId="org.eclipse.cdt.codan.internal.checkers.CaseBreakProblem">
</resolution>
+ <universalResolution
+ class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixSuppressProblem">
+ </universalResolution>
</extension>
</plugin>
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.java
index 9daaa139337..ccd08163251 100644
--- a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.java
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.java
@@ -25,6 +25,7 @@ public class QuickFixMessages extends NLS {
public static String QuickFixUsePointer_replace_dot;
public static String QuickFixUseDotOperator_replace_ptr;
public static String QuickFixForFixit_apply_fixit;
+ public static String QuickFixSuppressProblem_Label;
static {
NLS.initializeMessages(QuickFixMessages.class.getName(), QuickFixMessages.class);
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.properties b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.properties
index 7ca12d59bf9..e9eff2f7a67 100644
--- a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.properties
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.properties
@@ -19,4 +19,5 @@ QuickFixRenameMember_rename_member=Rename to suggested member
QuickFixAddSemicolon_add_semicolon=Add semicolon
QuickFixUsePointer_replace_dot=Replace '.' with '->'
QuickFixUseDotOperator_replace_ptr=Replace '->' with '.'
-QuickFixForFixit_apply_fixit=Apply compiler recommended fix-it \ No newline at end of file
+QuickFixForFixit_apply_fixit=Apply compiler recommended fix-it
+QuickFixSuppressProblem_Label=Suppress problem "%s" \ No newline at end of file
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixSuppressProblem.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixSuppressProblem.java
new file mode 100644
index 00000000000..8045c926385
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixSuppressProblem.java
@@ -0,0 +1,75 @@
+/*******************************************************************************
+ * Copyright (c) 2017 Institute for Software.
+ * 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:
+ * Felix Morgner <fmorgner@hsr.ch> - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.cdt.codan.internal.checkers.ui.quickfix;
+
+import org.eclipse.cdt.codan.core.model.IProblem;
+import org.eclipse.cdt.codan.core.param.IProblemPreference;
+import org.eclipse.cdt.codan.core.param.RootProblemPreference;
+import org.eclipse.cdt.codan.core.param.SuppressionCommentProblemPreference;
+import org.eclipse.cdt.codan.internal.checkers.ui.CheckersUiActivator;
+import org.eclipse.cdt.codan.ui.AbstractAstRewriteQuickFix;
+import org.eclipse.cdt.codan.ui.ICodanMarkerResolutionExtension;
+import org.eclipse.cdt.core.index.IIndex;
+import org.eclipse.core.resources.IMarker;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.text.edits.InsertEdit;
+
+public class QuickFixSuppressProblem extends AbstractAstRewriteQuickFix implements ICodanMarkerResolutionExtension {
+ private static final String COMMENT_TEMPLATE = " // %s"; //$NON-NLS-1$
+ private String problemName;
+
+ @Override
+ public String getLabel() {
+ return String.format(QuickFixMessages.QuickFixSuppressProblem_Label, problemName);
+ }
+
+ @Override
+ public void modifyAST(IIndex index, IMarker marker) {
+ String supressionComment = getSupressionComment(getProblem(marker));
+ if (supressionComment == null) {
+ return;
+ }
+ try {
+ int line = marker.getAttribute(IMarker.LINE_NUMBER, 0);
+ IRegion lineInformation = getDocument().getLineInformation(line - 1);
+ int offset = lineInformation.getOffset() + lineInformation.getLength();
+ String commentString = String.format(COMMENT_TEMPLATE, supressionComment);
+ InsertEdit edit = new InsertEdit(offset, commentString);
+ edit.apply(getDocument());
+ } catch (BadLocationException e) {
+ CheckersUiActivator.log(e);
+ }
+ }
+
+ private String getSupressionComment(IProblem problem) {
+ IProblemPreference preference = problem.getPreference();
+ if (preference instanceof RootProblemPreference) {
+ RootProblemPreference root = (RootProblemPreference) preference;
+ Object value = root.getChildValue(SuppressionCommentProblemPreference.KEY);
+ if (value instanceof String && ((String) value).trim().length() > 0) {
+ return (String) value;
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public void prepareFor(IMarker marker) {
+ problemName = getProblem(marker).getName();
+ }
+
+ @Override
+ public boolean isApplicable(IMarker marker) {
+ return getProblem(marker) != null;
+ }
+
+} \ No newline at end of file

Back to the top