Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7cb1f00b927df78ef4b30a6b71e1338d41591d28 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*******************************************************************************
 * Copyright (c) 2009, 2016 Alena Laskavaia
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Alena Laskavaia  - initial API and implementation
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.codan.core.cxx.model;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.cdt.codan.core.cxx.Activator;
import org.eclipse.cdt.codan.core.model.AbstractCheckerWithProblemPreferences;
import org.eclipse.cdt.codan.core.model.ICheckerInvocationContext;
import org.eclipse.cdt.codan.core.model.IProblem;
import org.eclipse.cdt.codan.core.model.IProblemLocation;
import org.eclipse.cdt.codan.core.model.IProblemLocationFactory;
import org.eclipse.cdt.codan.core.model.IRunnableInEditorChecker;
import org.eclipse.cdt.core.dom.ast.IASTComment;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTImageLocation;
import org.eclipse.cdt.core.dom.ast.IASTMacroExpansionLocation;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPSemantics;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.OperationCanceledException;

/**
 * Convenience implementation of checker that works on index-based AST of a
 * C/C++
 * program.
 *
 * Clients may extend this class.
 */
public abstract class AbstractIndexAstChecker extends AbstractCheckerWithProblemPreferences
		implements ICAstChecker, IRunnableInEditorChecker {
	private CxxModelsCache modelCache;

	@Override
	public synchronized boolean processResource(IResource resource) throws OperationCanceledException {
		if (!shouldProduceProblems(resource))
			return false;
		if (!(resource instanceof IFile))
			return true;
		processFile((IFile) resource);
		return false;
	}

	private void processFile(IFile file) throws OperationCanceledException {
		ICheckerInvocationContext context = getContext();
		synchronized (context) {
			modelCache = context.get(CxxModelsCache.class);
			if (modelCache == null) {
				ICElement celement = CoreModel.getDefault().create(file);
				if (!(celement instanceof ITranslationUnit)) {
					return;
				}
				modelCache = new CxxModelsCache((ITranslationUnit) celement);
				context.add(modelCache);
			}
		}
		try {
			// Run the checker only if the index is fully initialized. Otherwise it may produce
			// false positives.
			if (modelCache.getIndex().isFullyInitialized()) {
				IASTTranslationUnit ast = modelCache.getAST();
				if (ast != null) {
					synchronized (ast) {
						processAst(ast);
					}
				}
			}
		} catch (CoreException e) {
			Activator.log(e);
		} finally {
			modelCache = null;
		}
	}

	@Override
	public synchronized void processModel(Object model, ICheckerInvocationContext context) {
		if (model instanceof IASTTranslationUnit) {
			IASTTranslationUnit ast = (IASTTranslationUnit) model;
			// Run the checker only if the index was fully initialized when the file was parsed.
			// Otherwise the checker may produce false positives.
			if (ast.isBasedOnIncompleteIndex())
				return;
			setContext(context);
			synchronized (context) {
				modelCache = context.get(CxxModelsCache.class);
				if (modelCache == null) {
					modelCache = new CxxModelsCache(ast);
					context.add(modelCache);
				}
			}
			CPPSemantics.pushLookupPoint(ast);
			try {
				processAst(ast);
			} finally {
				modelCache = null;
				setContext(null);
				CPPSemantics.popLookupPoint();
			}
		}
	}

	@Override
	public boolean runInEditor() {
		return true;
	}

	public void reportProblem(String id, IASTNode astNode, Object... args) {
		IProblemLocation loc = getProblemLocation(astNode);
		if (loc != null)
			reportProblem(id, loc, args);
	}

	public void reportProblem(IProblem problem, IASTNode astNode, Object... args) {
		IProblemLocation loc = getProblemLocation(astNode);
		if (loc != null)
			reportProblem(problem, loc, args);
	}

	/**
	 * Checks if problem should be reported, in this case it will check line
	 * comments, later can add filters or what not.
	 *
	 * @param problem - problem kind
	 * @param loc - location
	 * @param args - arguments
	 * @since 3.4
	 */
	@Override
	protected boolean shouldProduceProblem(IProblem problem, IProblemLocation loc, Object... args) {
		String suppressionComment = (String) getSuppressionCommentPreference(problem).getValue();
		if (suppressionComment.isEmpty())
			return super.shouldProduceProblem(problem, loc, args);
		List<IASTComment> lineComments = getLineCommentsForLocation(loc);
		for (IASTComment astComment : lineComments) {
			if (astComment.getRawSignature().contains(suppressionComment))
				return false;
		}
		return super.shouldProduceProblem(problem, loc, args);
	}

	protected List<IASTComment> getLineCommentsForLocation(IProblemLocation loc) {
		ArrayList<IASTComment> lineComments = new ArrayList<>();
		try {
			IASTComment[] commentsArray = modelCache.getAST().getComments();
			for (IASTComment comm : commentsArray) {
				IASTFileLocation fileLocation = comm.getFileLocation();
				if (fileLocation.getStartingLineNumber() == loc.getLineNumber()) {
					String problemFile = loc.getFile().getLocation().toOSString();
					String commentFile = fileLocation.getFileName();
					if (problemFile.equals(commentFile)) {
						lineComments.add(comm);
					}
				}
			}
		} catch (OperationCanceledException | CoreException e) {
			Activator.log(e);
		}
		return lineComments;
	}

	protected IProblemLocation getProblemLocation(IASTNode astNode) {
		IASTFileLocation astLocation = astNode.getFileLocation();
		return getProblemLocation(astNode, astLocation);
	}

	private IProblemLocation getProblemLocation(IASTNode astNode, IASTFileLocation astLocation) {
		int line = astLocation.getStartingLineNumber();
		IProblemLocationFactory locFactory = getRuntime().getProblemLocationFactory();
		if (enclosedInMacroExpansion(astNode) && astNode instanceof IASTName) {
			IASTImageLocation imageLocation = ((IASTName) astNode).getImageLocation();
			if (imageLocation != null) {
				int start = imageLocation.getNodeOffset();
				int end = start + imageLocation.getNodeLength();
				return locFactory.createProblemLocation(getFile(), start, end, line);
			}
		}
		// If the raw signature has more than one line, we highlight only the code
		// related to the problem. However, if the problem is associated with a
		// node representing a class definition, do not highlight the entire class
		// definition, because that can result in many lines being highlighted.
		if (astNode instanceof IASTCompositeTypeSpecifier) {
			return locFactory.createProblemLocation(getFile(), line);
		}
		int start = astLocation.getNodeOffset();
		int end = start + astLocation.getNodeLength();
		return locFactory.createProblemLocation(getFile(), start, end, line);
	}

	protected static boolean enclosedInMacroExpansion(IASTNode node) {
		IASTNodeLocation[] nodeLocations = node.getNodeLocations();
		return nodeLocations.length == 1 && nodeLocations[0] instanceof IASTMacroExpansionLocation;
	}

	protected static boolean includesMacroExpansion(IASTNode node) {
		for (IASTNodeLocation nodeLocation : node.getNodeLocations()) {
			if (nodeLocation instanceof IASTMacroExpansionLocation)
				return true;
		}
		return false;
	}

	protected IFile getFile() {
		return modelCache.getFile();
	}

	protected IProject getProject() {
		IFile file = getFile();
		return file == null ? null : file.getProject();
	}

	protected CxxModelsCache getModelCache() {
		return modelCache;
	}

	protected ICodanCommentMap getCommentMap() {
		return modelCache.getCommentedNodeMap();
	}
}

Back to the top