From 467aab5559d4858e9e293c652f1860b77e362729 Mon Sep 17 00:00:00 2001 From: Marc-Andre Laperle Date: Thu, 27 Sep 2018 23:50:18 -0400 Subject: [PATCH] Bug 539307 - Set correct project nature in Codan tests to fix wrong content type When the lsp4e-cpp plugin is present, two content types are associated with .cpp extension. The lsp4e-cpp one is chosen because it happens to be first. Normally, the CDT C++ content type gets its priority boosted when the C++ project nature is present. Some Codan tests run on .cpp files but without the C++ project nature so the files are of wrong content type and the tests fail. Once the nature is fixed, some tests were failing because they should run on C files but loadCode was not loading them as C++ files because of the project nature. Change-Id: I61b77d409e81c3cf78e60adf0c77a9bf976ac9d5 Signed-off-by: Marc-Andre Laperle --- .../checkers/CaseBreakCheckerTest.java | 7 +++- .../checkers/CommentCheckerLineTests.java | 13 ++++--- .../internal/checkers/ReturnCheckerTest.java | 7 +++- .../StatementHasNoEffectCheckerTest.java | 35 +++++++++++-------- .../UnusedSymbolInFileScopeCheckerTest.java | 7 +++- .../cdt/codan/core/tests/CheckerTestCase.java | 10 ++++++ .../CaseBreakQuickFixCommentTest.java | 5 +++ ...BreakQuickFixFallthroughAttributeTest.java | 5 +++ .../quickfix/QuickFixSuppressProblemTest.java | 5 +++ 9 files changed, 72 insertions(+), 22 deletions(-) diff --git a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/CaseBreakCheckerTest.java b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/CaseBreakCheckerTest.java index e343c21372a..ff5f863adc5 100644 --- a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/CaseBreakCheckerTest.java +++ b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/CaseBreakCheckerTest.java @@ -29,6 +29,11 @@ public class CaseBreakCheckerTest extends CheckerTestCase { setLast(true); } + @Override + public boolean isCpp() { + return true; + } + // void foo(void) { // int a; // switch (a) { @@ -256,7 +261,7 @@ public class CaseBreakCheckerTest extends CheckerTestCase { // case 1: // b = 2; // /* no break */ - // bye(); + // foo(); // } // } public void testLastCaseBadCommentNotLast() throws Exception { diff --git a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/CommentCheckerLineTests.java b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/CommentCheckerLineTests.java index 59e438128b9..e57daa12009 100644 --- a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/CommentCheckerLineTests.java +++ b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/CommentCheckerLineTests.java @@ -26,12 +26,17 @@ public class CommentCheckerLineTests extends CheckerTestCase { enableProblems(CommentChecker.COMMENT_NO_LINE); } + @Override + public boolean isCpp() { + return true; + } + // void foo() { // return; // error // } @Test public void testLineComment() throws Exception { - checkSampleAbove(); + checkSampleAboveC(); } // void foo() { @@ -39,7 +44,7 @@ public class CommentCheckerLineTests extends CheckerTestCase { // } @Test public void testNoLineComment() throws Exception { - checkSampleAbove(); + checkSampleAboveC(); } // char * foo() { @@ -47,7 +52,7 @@ public class CommentCheckerLineTests extends CheckerTestCase { // } @Test public void testNoLineCommentInString() throws Exception { - checkSampleAbove(); + checkSampleAboveC(); } // void foo() { @@ -61,7 +66,7 @@ public class CommentCheckerLineTests extends CheckerTestCase { // #define AAA // error even in prepro @Test public void testLineCommentInPrepro() throws Exception { - checkSampleAbove(); + checkSampleAboveC(); } // @file:test.h diff --git a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/ReturnCheckerTest.java b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/ReturnCheckerTest.java index 69715f65a74..bc42a2cbf2b 100644 --- a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/ReturnCheckerTest.java +++ b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/ReturnCheckerTest.java @@ -26,7 +26,12 @@ public class ReturnCheckerTest extends CheckerTestCase { enableProblems(ReturnChecker.RET_NORET_ID,ReturnChecker.RET_ERR_VALUE_ID,ReturnChecker.RET_NO_VALUE_ID); } - // dummy() { + @Override + public boolean isCpp() { + return true; + } + + // void dummy() { // return; // no error here on line 2 // } public void testDummyFunction() throws Exception { diff --git a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/StatementHasNoEffectCheckerTest.java b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/StatementHasNoEffectCheckerTest.java index 4859975b9b3..491683cfc62 100644 --- a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/StatementHasNoEffectCheckerTest.java +++ b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/StatementHasNoEffectCheckerTest.java @@ -27,7 +27,12 @@ public class StatementHasNoEffectCheckerTest extends CheckerTestCase { enableProblems(StatementHasNoEffectChecker.ER_ID); } - // main() { + @Override + public boolean isCpp() { + return true; + } + + // int main() { // int a; // +a; // error here on line 3 // } @@ -35,7 +40,7 @@ public class StatementHasNoEffectCheckerTest extends CheckerTestCase { checkSampleAbove(); } - // main() { + // int main() { // int a,b; // // b+a; // error here on line 4 @@ -44,7 +49,7 @@ public class StatementHasNoEffectCheckerTest extends CheckerTestCase { checkSampleAbove(); } - // main() { + // int main() { // int a,b; // // a=b+a; // no error here @@ -53,7 +58,7 @@ public class StatementHasNoEffectCheckerTest extends CheckerTestCase { checkSampleAbove(); } - // main() { + // int main() { // int a,b; // // (a=b); // no errors here @@ -71,7 +76,7 @@ public class StatementHasNoEffectCheckerTest extends CheckerTestCase { checkSampleAbove(); } - // main() { + // int main() { // int a; // a; // error here on line 3 // } @@ -79,7 +84,7 @@ public class StatementHasNoEffectCheckerTest extends CheckerTestCase { checkSampleAbove(); } - // main() { + // int main() { // int a=({foo();a;}); // no error here on line 2 // char *p=({char s[]="Some string";&s[0];}); // no error here on line 3 // } @@ -87,19 +92,19 @@ public class StatementHasNoEffectCheckerTest extends CheckerTestCase { checkSampleAbove(); } - // main() { + // int main() { // int z=({int a=0; +a; a;}) // error here on line 2 // } public void testGNUExpressionCompoundStmtInside() throws Exception { checkSampleAbove(); } - // main() { + // int main() { // int a; // +a; // error here on line 3 // } - // foo() { + // void foo() { // int a; // // +a; // error here on line 4 @@ -114,7 +119,7 @@ public class StatementHasNoEffectCheckerTest extends CheckerTestCase { checkErrorLine(f2, 4); } - // main() { + // int main() { // for (a=b;a;a=a->next); // } public void testForTestExpression() throws Exception { @@ -131,7 +136,7 @@ public class StatementHasNoEffectCheckerTest extends CheckerTestCase { checkSampleAboveCpp(); } - // main() { + // int main() { // A a,b; // // b+=a; // no error here on line 4 @@ -141,7 +146,7 @@ public class StatementHasNoEffectCheckerTest extends CheckerTestCase { } //#define FUNC(a) a - // main() { + // int main() { // int a; // FUNC(a); // error by default // } @@ -152,7 +157,7 @@ public class StatementHasNoEffectCheckerTest extends CheckerTestCase { } //#define FUNC(a) a - // main() { + // int main() { // int x; // FUNC(x); // error // } @@ -163,7 +168,7 @@ public class StatementHasNoEffectCheckerTest extends CheckerTestCase { } //#define FUNC(a) a - // main() { + // int main() { // int a; // FUNC(a); // no error if macro exp turned off // } @@ -173,7 +178,7 @@ public class StatementHasNoEffectCheckerTest extends CheckerTestCase { checkSampleAbove(); } - // main() { + // int main() { // int a; // +a; // error here on line 3 // } diff --git a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/UnusedSymbolInFileScopeCheckerTest.java b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/UnusedSymbolInFileScopeCheckerTest.java index 39455f99973..693c06435f5 100644 --- a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/UnusedSymbolInFileScopeCheckerTest.java +++ b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/UnusedSymbolInFileScopeCheckerTest.java @@ -27,6 +27,11 @@ public class UnusedSymbolInFileScopeCheckerTest extends CheckerTestCase { UnusedSymbolInFileScopeChecker.ER_UNUSED_STATIC_FUNCTION_ID); } + @Override + public boolean isCpp() { + return true; + } + //////////////////////////////////////////////////////////////////////////// // extern function declarations //////////////////////////////////////////////////////////////////////////// @@ -314,7 +319,7 @@ public class UnusedSymbolInFileScopeCheckerTest extends CheckerTestCase { // static void f4() __attribute__((unused)) {} // static void __attribute__((unused)) f5(); public void testAttributeUnused() throws Exception { - loadCodeAndRun(getAboveComment()); + loadCodeAndRunC(getAboveComment()); checkNoErrors(); loadCodeAndRunCpp(getAboveComment()); diff --git a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/tests/CheckerTestCase.java b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/tests/CheckerTestCase.java index 2c5d488f3a7..9ef5d0d890c 100644 --- a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/tests/CheckerTestCase.java +++ b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/tests/CheckerTestCase.java @@ -159,6 +159,11 @@ public class CheckerTestCase extends CodanTestCase { runCodan(); } + public void loadCodeAndRunC(String code) throws CoreException { + loadcode(code, false); + runCodan(); + } + public void loadCodeAndRunCpp(String code) throws CoreException { loadcode(code, true); runCodan(); @@ -254,6 +259,11 @@ public class CheckerTestCase extends CodanTestCase { checkErrorComments(); } + protected void checkSampleAboveC() throws CoreException { + loadCodeAndRunC(getAboveComment()); + checkErrorComments(); + } + protected void checkSampleAboveCpp() throws CoreException { loadCodeAndRunCpp(getAboveComment()); checkErrorComments(); diff --git a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CaseBreakQuickFixCommentTest.java b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CaseBreakQuickFixCommentTest.java index d507fa7951e..c5f433bc084 100644 --- a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CaseBreakQuickFixCommentTest.java +++ b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CaseBreakQuickFixCommentTest.java @@ -17,6 +17,11 @@ public class CaseBreakQuickFixCommentTest extends QuickFixTestCase { return new CaseBreakQuickFixComment(); } + @Override + public boolean isCpp() { + return true; + } + //void hello() {} //void func() { // int a; diff --git a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CaseBreakQuickFixFallthroughAttributeTest.java b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CaseBreakQuickFixFallthroughAttributeTest.java index e507e722e9f..36b0ffe4c2c 100644 --- a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CaseBreakQuickFixFallthroughAttributeTest.java +++ b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CaseBreakQuickFixFallthroughAttributeTest.java @@ -28,6 +28,11 @@ public class CaseBreakQuickFixFallthroughAttributeTest extends QuickFixTestCase super.tearDown(); } + @Override + public boolean isCpp() { + return true; + } + @Override protected AbstractCodanCMarkerResolution createQuickFix() { return new CaseBreakQuickFixFallthroughAttribute(); diff --git a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixSuppressProblemTest.java b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixSuppressProblemTest.java index 3ab4b2afee0..372de364f42 100644 --- a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixSuppressProblemTest.java +++ b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixSuppressProblemTest.java @@ -23,6 +23,11 @@ public class QuickFixSuppressProblemTest extends QuickFixTestCase { return new QuickFixSuppressProblem(); } + @Override + public boolean isCpp() { + return true; + } + //struct s {}; //void func() { // try {