1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-09-10 12:03:16 +02:00

codan - checker for comments

implement few code style rules for comments
- no line comments (sometimes enforced)
- no nested comments (no /* in block comment)

Change-Id: Ib4042a8ffab0dc1c5effd7b77ab6d2f51a1e6cae
This commit is contained in:
Alena Laskavaia 2015-02-24 08:56:02 -05:00 committed by Gerrit Code Review @ Eclipse.org
parent 0ba2118dc9
commit 396b946015
7 changed files with 251 additions and 16 deletions

View file

@ -398,5 +398,31 @@
name="%problem.name.ClassMembersInitialization">
</problem>
</checker>
<checker
class="org.eclipse.cdt.codan.internal.checkers.CommentChecker"
id="org.eclipse.cdt.codan.internal.checkers.CommentChecker"
name="Comment Checks">
<problem
category="org.eclipse.cdt.codan.core.categories.CodeStyle"
defaultEnabled="false"
defaultSeverity="Error"
description="This rule will flag usage of // style comments. Only applicable for C code, not C++"
id="org.eclipse.cdt.codan.checkers.nolinecomment"
markerType="org.eclipse.cdt.codan.core.codanProblem"
messagePattern="Line comments are not allowed"
multiple="false"
name="Line comments // are not allowed">
</problem>
<problem
category="org.eclipse.cdt.codan.core.categories.CodeStyle"
defaultEnabled="false"
defaultSeverity="Error"
description="This rule will flag usage of /* inside another comment."
id="org.eclipse.cdt.codan.checkers.nocommentinside"
markerType="org.eclipse.cdt.codan.core.codanProblem"
messagePattern="Sequence /* used inside the comment"
name="No nesting comments">
</problem>
</checker>
</extension>
</plugin>

View file

@ -0,0 +1,63 @@
/*******************************************************************************
* Copyright (c) 2015 QNX Software System 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:
* Elena Laskavaia (QNX Software System) - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.codan.internal.checkers;
import org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker;
import org.eclipse.cdt.codan.core.model.IProblemLocation;
import org.eclipse.cdt.codan.core.model.IProblemLocationFactory;
import org.eclipse.cdt.core.dom.ILinkage;
import org.eclipse.cdt.core.dom.ast.IASTComment;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
/**
* Checker for some specific code style violations in comments
*/
public class CommentChecker extends AbstractIndexAstChecker {
public static final String COMMENT_NO_LINE = "org.eclipse.cdt.codan.checkers.nolinecomment"; //$NON-NLS-1$
public static final String COMMENT_NO_START = "org.eclipse.cdt.codan.checkers.nocommentinside"; //$NON-NLS-1$
private boolean conly;
@Override
public void processAst(IASTTranslationUnit ast) {
IASTComment[] comments = ast.getComments();
if (comments == null)
return;
conly = ast.getLinkage().getLinkageID() == ILinkage.C_LINKAGE_ID;
if (!conly && shouldProduceProblem(getProblemById(COMMENT_NO_START, getFile()),
getFile().getFullPath())==false)
return; // c++ file and COMMENT_NO_START is disabled - optimize and bail
for (IASTComment comment : comments) {
processComment(comment);
}
}
protected void processComment(IASTComment comment) {
boolean blockComment = comment.isBlockComment();
if (blockComment) {
String commentStr = comment.getRawSignature();
int pos = commentStr.indexOf("/*", 2); //$NON-NLS-1$
if (pos >= 0) {
reportProblem(COMMENT_NO_START, getProblemLocation(comment.getFileLocation(), pos));
}
} else {
if (conly) {
reportProblem(COMMENT_NO_LINE, comment);
}
}
}
@SuppressWarnings("deprecation")
private IProblemLocation getProblemLocation(IASTFileLocation astLocation, int pos) {
IProblemLocationFactory locFactory = getRuntime().getProblemLocationFactory();
return locFactory.createProblemLocation(getFile(), astLocation.getNodeOffset() + pos, astLocation.getNodeOffset() + pos + 2);
}
}

View file

@ -0,0 +1,81 @@
/*******************************************************************************
* Copyright (c) 2015 QNX Software System 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:
* Elena Laskavaia (QNX Software System) - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.codan.core.internal.checkers;
import java.io.File;
import org.eclipse.cdt.codan.core.test.CheckerTestCase;
import org.eclipse.cdt.codan.internal.checkers.CommentChecker;
import org.junit.Test;
/**
* Tests for CommentChecker
*/
public class CommentCheckerLineTests extends CheckerTestCase {
@Override
public void setUp() throws Exception {
super.setUp();
enableProblems(CommentChecker.COMMENT_NO_LINE);
}
// void foo() {
// return; // error
// }
@Test
public void testLineComment() {
checkSampleAbove();
}
// void foo() {
// return;
// }
@Test
public void testNoLineComment() {
checkSampleAbove();
}
// char * foo() {
// return "// this is a string";
// }
@Test
public void testNoLineCommentInString() {
checkSampleAbove();
}
// void foo() {
// return; // not an error in c++
// }
@Test
public void testLineCommentCpp() {
checkSampleAboveCpp();
}
// #define AAA // error even in prepro
@Test
public void testLineCommentInPrepro() {
checkSampleAbove();
}
// @file:test.h
// int foo();// error too
// @file:test.c
// #include "test.h"
// int bar() {
// foo();
// }
public void testHeader() throws Exception {
loadcode(getContents(2));
runOnProject();
checkErrorLine(new File("test.h"), 1); //$NON-NLS-1$
}
}

View file

@ -0,0 +1,51 @@
/*******************************************************************************
* Copyright (c) 2015 QNX Software System 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:
* Elena Laskavaia (QNX Software System) - 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.CommentChecker;
import org.junit.Test;
/**
* Tests for CommentChecker
*/
public class CommentCheckerNestedTests extends CheckerTestCase {
@Override
public void setUp() throws Exception {
super.setUp();
enableProblems(CommentChecker.COMMENT_NO_START);
}
// void foo() {
// return; /* /* */ // error
// }
@Test
public void testLineComment() {
checkSampleAbove();
}
// void foo() {
// return; /*
// /* // error
// */
// }
@Test
public void testLineComment2() {
checkSampleAbove();
}
// void foo() {
// return; /* */
// }
@Test
public void testNoLineComment() {
checkSampleAbove();
}
}

View file

@ -21,6 +21,8 @@ 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.ClassMembersInitializationCheckerTest;
import org.eclipse.cdt.codan.core.internal.checkers.CommentCheckerLineTests;
import org.eclipse.cdt.codan.core.internal.checkers.CommentCheckerNestedTests;
import org.eclipse.cdt.codan.core.internal.checkers.FormatStringCheckerTest;
import org.eclipse.cdt.codan.core.internal.checkers.NonVirtualDestructorCheckerTest;
import org.eclipse.cdt.codan.core.internal.checkers.ProblemBindingCheckerTest;
@ -70,6 +72,8 @@ public class AutomatedIntegrationSuite extends TestSuite {
suite.addTestSuite(SuggestedParenthesisCheckerTest.class);
suite.addTestSuite(SuspiciousSemicolonCheckerTest.class);
suite.addTestSuite(UnusedSymbolInFileScopeCheckerTest.class);
suite.addTestSuite(CommentCheckerLineTests.class);
suite.addTestSuite(CommentCheckerNestedTests.class);
// framework
suite.addTest(CodanFastTestSuite.suite());
// quick fixes

View file

@ -220,6 +220,10 @@ public class CheckerTestCase extends CodanTestCase {
}
}
/**
* Enable given problems and disable the rest
* @param ids
*/
protected void enableProblems(String... ids) {
IProblemProfile profile = CodanRuntime.getInstance().getCheckersRegistry().getWorkspaceProfile();
IProblem[] problems = profile.getProblems();

View file

@ -48,7 +48,7 @@ public class CodanTestCase extends BaseTestCase {
protected File currentFile;
protected ICElement currentCElem;
protected IFile currentIFile;
protected ArrayList<Integer> errLines= new ArrayList<Integer>();
protected ArrayList<Integer> errLines = new ArrayList<Integer>();
/**
*
@ -99,13 +99,17 @@ public class CodanTestCase extends BaseTestCase {
* @throws CoreException
*/
private void removeLeftOverProjects() throws CoreException {
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
IProject[] projects = workspace.getRoot().getProjects();
for (int i = 0; i < projects.length; i++) {
IProject p = projects[i];
if (p.getName().startsWith("Codan")) {
p.delete(IResource.FORCE | IResource.ALWAYS_DELETE_PROJECT_CONTENT, new NullProgressMonitor());
try {
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
IProject[] projects = workspace.getRoot().getProjects();
for (int i = 0; i < projects.length; i++) {
IProject p = projects[i];
if (p.getName().startsWith("Codan")) {
p.delete(IResource.FORCE | IResource.ALWAYS_DELETE_PROJECT_CONTENT, new NullProgressMonitor());
}
}
} catch (Throwable e) {
// moving on...
}
}
@ -175,8 +179,7 @@ public class CodanTestCase extends BaseTestCase {
protected StringBuilder[] getContents(int sections) {
try {
return TestSourceReader.getContentsForTest(getPlugin().getBundle(), getSourcePrefix(),
getClass(), getName(), sections);
return TestSourceReader.getContentsForTest(getPlugin().getBundle(), getSourcePrefix(), getClass(), getName(), sections);
} catch (IOException e) {
fail(e.getMessage());
return null;
@ -215,11 +218,6 @@ public class CodanTestCase extends BaseTestCase {
return loadcode(code, testFile);
}
public File loadcode(String code, String filename) {
File testFile = new File(tmpDir, filename);
return loadcode(code, testFile);
}
private File loadcode(String code, File testFile) {
try {
tempFiles.add(testFile);
@ -246,14 +244,14 @@ public class CodanTestCase extends BaseTestCase {
return null;
}
}
private static Pattern COMMENT_TAG_PATTERN = Pattern.compile("//\\s*(err|ERR|ERROR|error)\\b");
private void loadErrorComments(String trim) {
String[] lines = trim.split("\n");
for (int i = 0; i < lines.length; i++) {
String string = lines[i];
if (COMMENT_TAG_PATTERN.matcher(string).find()) {
errLines.add(i+1);
errLines.add(i + 1);
}
}
}
@ -269,4 +267,12 @@ public class CodanTestCase extends BaseTestCase {
public File loadcode(String code) {
return loadcode(code, isCpp());
}
public File loadcode(CharSequence... more) {
File file = null;
for (CharSequence cseq : more) {
file = loadcode(cseq.toString(), isCpp());
}
return file;
}
}