diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml b/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml
index 97c3d5ea3e8..839147f898d 100644
--- a/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml
@@ -9,6 +9,10 @@
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.CatchByReferenceQuickFix"
problemId="org.eclipse.cdt.codan.internal.checkers.CatchByReference">
+
+
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/Messages.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/Messages.java
index d56e2074674..ef0fa2ce6e4 100644
--- a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/Messages.java
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/Messages.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2009,2010 Alena Laskavaia
+ * Copyright (c) 2009, 2011 Alena Laskavaia and others.
* 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
@@ -17,6 +17,7 @@ import org.eclipse.osgi.util.NLS;
*/
public class Messages extends NLS {
public static String CatchByReferenceQuickFix_Message;
+ public static String CatchByConstReferenceQuickFix_Message;
public static String QuickFixAssignmentInCondition_Message;
public static String SuggestedParenthesisQuickFix_Message;
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/Messages.properties b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/Messages.properties
index da262e0d768..720c604fae0 100644
--- a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/Messages.properties
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/Messages.properties
@@ -1,5 +1,5 @@
###############################################################################
-# Copyright (c) 2010 Alena Laskavaia and others.
+# Copyright (c) 2010, 2011 Alena Laskavaia and others.
# 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
@@ -9,5 +9,6 @@
# Alena Laskavaia - initial API and implementation
###############################################################################
CatchByReferenceQuickFix_Message=Change to use '&'
+CatchByConstReferenceQuickFix_Message=Change to use 'const &'
QuickFixAssignmentInCondition_Message=Change to '=='
SuggestedParenthesisQuickFix_Message=Surround with '()'
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CatchByConstReferenceQuickFix.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CatchByConstReferenceQuickFix.java
new file mode 100644
index 00000000000..26bb1321a57
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CatchByConstReferenceQuickFix.java
@@ -0,0 +1,30 @@
+/*******************************************************************************
+ * Copyright (c) 2010, 2011 Alena Laskavaia and others.
+ * 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 - original API and implementation in CatchByReferenceQuickFix
+ * Tomasz Wesolowski - modified for const &
+ *******************************************************************************/
+package org.eclipse.cdt.codan.internal.checkers.ui.quickfix;
+
+import org.eclipse.cdt.codan.internal.checkers.ui.Messages;
+import org.eclipse.cdt.codan.ui.AbstractCodanCMarkerResolution;
+import org.eclipse.core.resources.IMarker;
+import org.eclipse.jface.text.IDocument;
+
+/**
+ * quick fix for catch by value
+ */
+public class CatchByConstReferenceQuickFix extends AbstractCodanCMarkerResolution {
+ public String getLabel() {
+ return Messages.CatchByConstReferenceQuickFix_Message;
+ }
+
+ public void apply(IMarker marker, IDocument document) {
+ CatchByReferenceQuickFix.applyCatchByReferenceQuickFix(marker, document, true);
+ }
+}
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CatchByReferenceQuickFix.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CatchByReferenceQuickFix.java
index 4c845aa1246..cff7d0ceea8 100644
--- a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CatchByReferenceQuickFix.java
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CatchByReferenceQuickFix.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2010 Alena Laskavaia
+ * Copyright (c) 2010, 2011 Alena Laskavaia and others.
* 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
@@ -15,7 +15,6 @@ import org.eclipse.cdt.codan.internal.checkers.ui.Messages;
import org.eclipse.cdt.codan.ui.AbstractCodanCMarkerResolution;
import org.eclipse.core.resources.IMarker;
import org.eclipse.jface.text.BadLocationException;
-import org.eclipse.jface.text.FindReplaceDocumentAdapter;
import org.eclipse.jface.text.IDocument;
/**
@@ -27,15 +26,47 @@ public class CatchByReferenceQuickFix extends AbstractCodanCMarkerResolution {
}
public void apply(IMarker marker, IDocument document) {
- FindReplaceDocumentAdapter dad = new FindReplaceDocumentAdapter(document);
+ applyCatchByReferenceQuickFix(marker, document, false);
+ }
+
+ static void applyCatchByReferenceQuickFix(IMarker marker, IDocument document, boolean addConst) {
try {
- int pos = getOffset(marker, document);
- dad.find(pos, " ", /* forwardSearch *///$NON-NLS-1$
- true, /* caseSensitive */false,
- /* wholeWord */false, /* regExSearch */false);
- dad.replace(" & ", /* regExReplace */false); //$NON-NLS-1$
+ int left = marker.getAttribute(IMarker.CHAR_START, -1);
+ int right = marker.getAttribute(IMarker.CHAR_END, -1);
+ String inStr = document.get(left, right - left);
+ document.replace(left, right - left, getCatchByReferenceString(inStr, addConst));
} catch (BadLocationException e) {
CheckersUiActivator.log(e);
}
}
+
+ /**
+ * Returns a catch by reference string from a catch by value string
+ */
+ static private String getCatchByReferenceString(String inStr, boolean addConst) {
+ StringBuilder stringBuilder = new StringBuilder(inStr.length() + 10);
+ if (addConst) {
+ stringBuilder.append("const "); //$NON-NLS-1$
+ }
+
+ String typename;
+ int space = inStr.lastIndexOf(' ');
+ boolean hasDeclName = space != -1;
+ if (hasDeclName) {
+ typename = inStr.substring(0,space);
+ } else {
+ typename = inStr;
+ }
+ stringBuilder.append(typename);
+
+ stringBuilder.append(" &"); //$NON-NLS-1$
+
+ if (hasDeclName) {
+ stringBuilder.append(" "); //$NON-NLS-1$
+ String declname = inStr.substring(space+1);
+ stringBuilder.append(declname);
+ }
+
+ return stringBuilder.toString();
+ }
}
diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java
index b70376f252f..b832bcd40fe 100644
--- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java
+++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java
@@ -30,6 +30,7 @@ import org.eclipse.cdt.codan.core.internal.checkers.StatementHasNoEffectCheckerT
import org.eclipse.cdt.codan.core.internal.checkers.SuggestedParenthesisCheckerTest;
import org.eclipse.cdt.codan.core.internal.checkers.SuspiciousSemicolonCheckerTest;
import org.eclipse.cdt.codan.core.internal.checkers.UnusedSymbolInFileScopeCheckerTest;
+import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.CatchByReferenceQuickFixTest;
import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.CreateLocalVariableQuickFixTest;
import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.SuggestedParenthesisQuickFixTest;
@@ -72,6 +73,7 @@ public class AutomatedIntegrationSuite extends TestSuite {
// quick fixes
suite.addTestSuite(CreateLocalVariableQuickFixTest.class);
suite.addTestSuite(SuggestedParenthesisQuickFixTest.class);
+ suite.addTestSuite(CatchByReferenceQuickFixTest.class);
return suite;
}
}
diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CatchByReferenceQuickFixTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CatchByReferenceQuickFixTest.java
new file mode 100644
index 00000000000..9849aaea806
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CatchByReferenceQuickFixTest.java
@@ -0,0 +1,97 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Tomasz Wesolowski and others
+ * 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:
+ * Tomasz Wesolowski - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.cdt.codan.internal.checkers.ui.quickfix;
+
+import org.eclipse.cdt.codan.internal.checkers.CatchByReference;
+import org.eclipse.cdt.codan.ui.AbstractCodanCMarkerResolution;
+
+/**
+ * @author Tomasz Wesolowski
+ */
+public class CatchByReferenceQuickFixTest extends QuickFixTestCase {
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ enableProblems(CatchByReference.ER_ID);
+ }
+
+ @Override
+ public boolean isCpp() {
+ return true;
+ }
+
+ @Override
+ protected AbstractCodanCMarkerResolution createQuickFix() {
+ return null; // quick fix to be chosen per test
+ }
+
+ // struct C {
+ // };
+ // void foo() {
+ // try {
+ // } catch (C exception) {
+ // }
+ // }
+ @SuppressWarnings("restriction")
+ public void testCatchByReference() throws Exception {
+ setQuickFix(new CatchByReferenceQuickFix());
+ loadcode(getAboveComment());
+ String result = runQuickFixOneFile();
+ assertContainedIn("catch (C & exception)", result); //$NON-NLS-1$
+ }
+
+ // struct C {
+ // };
+ // void foo() {
+ // try {
+ // } catch (C) {
+ // }
+ // }
+ @SuppressWarnings("restriction")
+ public void testCatchByReferenceNoDeclName() throws Exception {
+ setQuickFix(new CatchByReferenceQuickFix());
+ loadcode(getAboveComment());
+ String result = runQuickFixOneFile();
+ assertContainedIn("catch (C &)", result); //$NON-NLS-1$
+ }
+
+ // struct C {
+ // };
+ // void foo() {
+ // try {
+ // } catch (C exception) {
+ // }
+ // }
+ @SuppressWarnings("restriction")
+ public void testCatchByConstReference() throws Exception {
+ setQuickFix(new CatchByConstReferenceQuickFix());
+ loadcode(getAboveComment());
+ String result = runQuickFixOneFile();
+ assertContainedIn("catch (const C & exception)", result); //$NON-NLS-1$
+ }
+
+ // struct C {
+ // };
+ // void foo() {
+ // try {
+ // } catch (C) {
+ // }
+ // }
+ @SuppressWarnings("restriction")
+ public void testCatchByConstReferenceNoDeclName() throws Exception {
+ setQuickFix(new CatchByConstReferenceQuickFix());
+ loadcode(getAboveComment());
+ String result = runQuickFixOneFile();
+ assertContainedIn("catch (const C &)", result); //$NON-NLS-1$
+ }
+
+}
diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixTestCase.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixTestCase.java
index 0782edc5472..ba15bee64cd 100644
--- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixTestCase.java
+++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixTestCase.java
@@ -146,4 +146,12 @@ public abstract class QuickFixTestCase extends CheckerTestCase {
public void assertContainedIn(String expected, String result) {
assertTrue("Text <" + expected + "> not found in <" + result + ">", result.contains(expected)); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
}
+
+ /**
+ * Changes the quick fix to be used
+ * @param quickFix
+ */
+ public void setQuickFix(AbstractCodanCMarkerResolution quickFix) {
+ this.quickFix = quickFix;
+ }
}