diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ProblemBindingChecker.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ProblemBindingChecker.java index 71629d975bf..01c1041c03e 100644 --- a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ProblemBindingChecker.java +++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ProblemBindingChecker.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010 Marc-Andre Laperle and others. + * Copyright (c) 2010, 2011 Marc-Andre Laperle 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 @@ -210,7 +210,13 @@ public class ProblemBindingChecker extends AbstractIndexAstChecker { private void handleMemberProblem(IASTName name, IASTNode parentNode, IProblemBinding problemBinding, String contextFlagsString) throws DOMException { IASTNode parentParentNode = parentNode.getParent(); - if (parentParentNode instanceof IASTFunctionCallExpression) { + + // An IASTFieldReference corresponds to a method if it's the first child in the parent function call expression + // For example, + // func(x.y()); the field reference is first in the x.y() function call expression -> y is a method + // func(x.y); the field reference is second in the func(x.y) function call expression, func is first as a Id Expression -> y is a field + boolean isMethod = parentParentNode instanceof IASTFunctionCallExpression && parentParentNode.getChildren()[0] == parentNode; + if (isMethod) { if (problemBinding.getCandidateBindings().length == 0) { reportProblem(ERR_ID_MethodResolutionProblem, name.getLastName(), name.getRawSignature(), contextFlagsString); } else { diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ProblemBindingCheckerTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ProblemBindingCheckerTest.java new file mode 100644 index 00000000000..173c85c6641 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ProblemBindingCheckerTest.java @@ -0,0 +1,78 @@ +/******************************************************************************* + * Copyright (c) 2011 Marc-Andre Laperle 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: + * Marc-Andre Laperle - Initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.core.internal.checkers; + +import org.eclipse.cdt.codan.core.test.CheckerTestCase; +import org.eclipse.cdt.codan.internal.checkers.ProblemBindingChecker; + +public class ProblemBindingCheckerTest extends CheckerTestCase { + // @Override + @Override + public boolean isCpp() { + return true; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.cdt.codan.core.test.CodanTestCase#setUp() + */ + @Override + public void setUp() throws Exception { + super.setUp(); + enableProblems(ProblemBindingChecker.ERR_ID_AmbiguousProblem, ProblemBindingChecker.ERR_ID_Candidates, + ProblemBindingChecker.ERR_ID_CircularReferenceProblem, ProblemBindingChecker.ERR_ID_FieldResolutionProblem, + ProblemBindingChecker.ERR_ID_FunctionResolutionProblem, ProblemBindingChecker.ERR_ID_InvalidArguments, + ProblemBindingChecker.ERR_ID_InvalidTemplateArgumentsProblem, ProblemBindingChecker.ERR_ID_LabelStatementNotFoundProblem, + ProblemBindingChecker.ERR_ID_MemberDeclarationNotFoundProblem, ProblemBindingChecker.ERR_ID_MethodResolutionProblem, + ProblemBindingChecker.ERR_ID_OverloadProblem, ProblemBindingChecker.ERR_ID_RedeclarationProblem, + ProblemBindingChecker.ERR_ID_RedefinitionProblem, ProblemBindingChecker.ERR_ID_TypeResolutionProblem, + ProblemBindingChecker.ERR_ID_VariableResolutionProblem); + } + + // int main () { + // struct X {} x; + // fun(x.y); + // } + public void testBug338683FieldInFunctionCall() { + loadCodeAndRun(getAboveComment()); + checkErrorLine(3, ProblemBindingChecker.ERR_ID_FieldResolutionProblem); + } + + // int main () { + // struct X {} x; + // x.b( + // x.y(), + // x.y( + // x.y), + // x.y( + // x.y( + // a, + // fun( + // x.b(), + // x.y, + // a.b())))); + // } + public void testBug338683VariousFieldMethodCombinations() { + loadCodeAndRun(getAboveComment()); + checkErrorLine(3, ProblemBindingChecker.ERR_ID_MethodResolutionProblem); + checkErrorLine(4, ProblemBindingChecker.ERR_ID_MethodResolutionProblem); + checkErrorLine(5, ProblemBindingChecker.ERR_ID_MethodResolutionProblem); + checkErrorLine(6, ProblemBindingChecker.ERR_ID_FieldResolutionProblem); + checkErrorLine(7, ProblemBindingChecker.ERR_ID_MethodResolutionProblem); + checkErrorLine(8, ProblemBindingChecker.ERR_ID_MethodResolutionProblem); + checkErrorLine(9, ProblemBindingChecker.ERR_ID_VariableResolutionProblem); + checkErrorLine(10, ProblemBindingChecker.ERR_ID_FunctionResolutionProblem); + checkErrorLine(11, ProblemBindingChecker.ERR_ID_MethodResolutionProblem); + checkErrorLine(12, ProblemBindingChecker.ERR_ID_FieldResolutionProblem); + checkErrorLine(13, ProblemBindingChecker.ERR_ID_MethodResolutionProblem); + } +} 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 e9d9e12c1be..1070b82d7cb 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 @@ -20,6 +20,7 @@ import org.eclipse.cdt.codan.core.internal.checkers.AssignmentToItselfCheckerTes import org.eclipse.cdt.codan.core.internal.checkers.CaseBreakCheckerTest; import org.eclipse.cdt.codan.core.internal.checkers.CatchByReferenceTest; import org.eclipse.cdt.codan.core.internal.checkers.FormatStringCheckerTest; +import org.eclipse.cdt.codan.core.internal.checkers.ProblemBindingCheckerTest; import org.eclipse.cdt.codan.core.internal.checkers.ReturnCheckerTest; import org.eclipse.cdt.codan.core.internal.checkers.ReturnStyleCheckerTest; import org.eclipse.cdt.codan.core.internal.checkers.StatementHasNoEffectCheckerTest; @@ -56,6 +57,7 @@ public class AutomatedIntegrationSuite extends TestSuite { suite.addTestSuite(SuspiciousSemicolonCheckerTest.class); suite.addTestSuite(CaseBreakCheckerTest.class); suite.addTestSuite(FormatStringCheckerTest.class); + suite.addTestSuite(ProblemBindingCheckerTest.class); // framework suite.addTest(CodanFastTestSuite.suite()); // quick fixes diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CheckerTestCase.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CheckerTestCase.java index beb283f3395..0ce02c3d229 100644 --- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CheckerTestCase.java +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CheckerTestCase.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2010 Alena Laskavaia + * Copyright (c) 2009, 2011 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 @@ -14,11 +14,13 @@ import java.io.File; import java.io.IOException; import org.eclipse.cdt.codan.core.CodanRuntime; +import org.eclipse.cdt.codan.core.model.CheckerLaunchMode; import org.eclipse.cdt.codan.core.model.IProblem; import org.eclipse.cdt.codan.core.model.IProblemProfile; import org.eclipse.cdt.codan.core.model.IProblemReporter; import org.eclipse.cdt.codan.core.param.IProblemPreference; import org.eclipse.cdt.codan.core.param.MapProblemPreference; +import org.eclipse.cdt.codan.core.param.RootProblemPreference; import org.eclipse.cdt.codan.internal.core.model.CodanProblem; import org.eclipse.cdt.codan.internal.core.model.CodanProblemMarker; import org.eclipse.core.resources.IMarker; @@ -187,14 +189,22 @@ public class CheckerTestCase extends CodanTestCase { IProblem[] problems = profile.getProblems(); for (int i = 0; i < problems.length; i++) { IProblem p = problems[i]; + boolean enabled = false; for (int j = 0; j < ids.length; j++) { String pid = ids[j]; if (p.getId().equals(pid)) { - ((CodanProblem) p).setEnabled(true); - } else { - ((CodanProblem) p).setEnabled(false); + enabled = true; + // Force the launch mode to FULL_BUILD to make sure we can test the problem even if by default it + // is not set to run on FULL_BUILD + IProblemPreference preference = p.getPreference(); + if (preference instanceof RootProblemPreference) { + RootProblemPreference rootProblemPreference = (RootProblemPreference) preference; + rootProblemPreference.getLaunchModePreference().enableInLaunchModes(CheckerLaunchMode.RUN_ON_FULL_BUILD); + } + break; } } + ((CodanProblem) p).setEnabled(enabled); } CodanRuntime.getInstance().getCheckersRegistry().updateProfile(cproject.getProject(), profile); return;