Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2008ab588c0e8dcdb47a1b3783345d5307ab092e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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);
			}
		}
	}

	private IProblemLocation getProblemLocation(IASTFileLocation astLocation, int pos) {
		IProblemLocationFactory locFactory = getRuntime().getProblemLocationFactory();
		int newPosition = astLocation.getNodeOffset() + pos;
		return locFactory.createProblemLocation(getFile(), newPosition, newPosition + 2, -1);
	}
}

Back to the top