Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0d78aa1dc89e032d865dec21d19aca4ff840357d (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*******************************************************************************
 * Copyright (c) 2008, 2012 Wind River Systems, Inc. and others.
 *
 * 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:
 *     Markus Schorn - initial API and implementation
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.parser.scanner;

import java.util.List;

import org.eclipse.cdt.core.dom.ast.cpp.ICPPUsingDirective;
import org.eclipse.cdt.core.index.IIndexFile;
import org.eclipse.cdt.core.index.IIndexMacro;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.parser.FileContent;
import org.eclipse.cdt.core.parser.ISignificantMacros;

/**
 * Instructs the preprocessor on how to handle a file-inclusion.
 */
public class InternalFileContent extends FileContent {
	public enum InclusionKind {
		/**
		 * Instruct the preprocessor to skip this inclusion.
		 */
		SKIP_FILE,
		/**
		 * The file and its dependents are indexed, required information is read
		 * from there.
		 */
		FOUND_IN_INDEX,
		/**
		 * The file has to be scanned, source is provided.
		 */
		USE_SOURCE
	}

	public static class FileVersion {
		public final String fPath;
		public final ISignificantMacros fSigMacros;

		public FileVersion(String path, ISignificantMacros sig) {
			fPath = path;
			fSigMacros = sig;
		}
	}

	private final InclusionKind fKind;
	private final AbstractCharArray fSource;
	private final List<IIndexMacro> fMacroDefinitions;
	private final List<ICPPUsingDirective> fUsingDirectives;
	private final String fFileLocation;
	private final List<FileVersion> fNonPragmaOnceFiles;
	private boolean fHeuristic;
	private boolean fIsSource;
	private ITranslationUnit fTranslationUnit;
	private List<IIndexFile> fFiles;
	private IncludeSearchPathElement fFoundOnPath;
	private final long fTimestamp;
	private final long fFileSize;
	private final long fReadTime;

	/**
	 * For skipping include files.
	 * @param fileLocation the location of the file.
	 * @param kind must be {@link InclusionKind#SKIP_FILE}.
	 * @throws IllegalArgumentException if fileLocation is <code>null</code> or the kind value is illegal for
	 * this constructor.
	 */
	public InternalFileContent(String fileLocation, InclusionKind kind) throws IllegalArgumentException {
		if (fileLocation == null || kind != InclusionKind.SKIP_FILE) {
			throw new IllegalArgumentException();
		}
		fKind = kind;
		fFileLocation = fileLocation;
		fMacroDefinitions = null;
		fUsingDirectives = null;
		fSource = null;
		fNonPragmaOnceFiles = null;
		fTimestamp = NULL_TIMESTAMP;
		fFileSize = NULL_FILE_SIZE;
		fReadTime = 0;
	}

	/**
	 * For reading include files from disk.
	 * @throws IllegalArgumentException in case the codeReader or its location is <code>null</code>.
	 */
	public InternalFileContent(String filePath, AbstractCharArray content, long timestamp, long fileSize,
			long fileReadTime) throws IllegalArgumentException {
		if (content == null) {
			throw new IllegalArgumentException();
		}
		fKind = InclusionKind.USE_SOURCE;
		fFileLocation = filePath;
		fSource = content;
		fMacroDefinitions = null;
		fUsingDirectives = null;
		fNonPragmaOnceFiles = null;
		if (fFileLocation == null) {
			throw new IllegalArgumentException();
		}
		fTimestamp = timestamp;
		fFileSize = fileSize;
		fReadTime = fileReadTime;
	}

	/**
	 * For reading in-memory buffers.
	 * @throws IllegalArgumentException in case the codeReader or its location is <code>null</code>.
	 */
	public InternalFileContent(String filePath, CharArray content) throws IllegalArgumentException {
		if (content == null) {
			throw new IllegalArgumentException();
		}
		fKind = InclusionKind.USE_SOURCE;
		fFileLocation = filePath;
		fSource = content;
		fMacroDefinitions = null;
		fUsingDirectives = null;
		fNonPragmaOnceFiles = null;
		if (fFileLocation == null) {
			throw new IllegalArgumentException();
		}
		fTimestamp = NULL_TIMESTAMP;
		fFileSize = NULL_FILE_SIZE;
		fReadTime = 0;
	}

	/**
	 * For using information about an include file from the index.
	 * @param fileLocation the location of the file
	 * @param macroDefinitions a list of macro definitions
	 * @param files
	 * @throws IllegalArgumentException in case the fileLocation or the macroDefinitions are <code>null</code>.
	 */
	public InternalFileContent(String fileLocation, List<IIndexMacro> macroDefinitions,
			List<ICPPUsingDirective> usingDirectives, List<IIndexFile> files, List<FileVersion> nonPragmaOnceVersions) {
		fKind = InclusionKind.FOUND_IN_INDEX;
		fFileLocation = fileLocation;
		fSource = null;
		fUsingDirectives = usingDirectives;
		fMacroDefinitions = macroDefinitions;
		fFiles = files;
		fNonPragmaOnceFiles = nonPragmaOnceVersions;
		fTimestamp = NULL_TIMESTAMP;
		fFileSize = NULL_FILE_SIZE;
		fReadTime = 0;
	}

	/**
	 * @return the kind
	 */
	public InclusionKind getKind() {
		return fKind;
	}

	/**
	 * Returns the location of the file to be included.
	 */
	@Override
	public String getFileLocation() {
		return fFileLocation;
	}

	@Override
	public long getTimestamp() {
		return fTimestamp;
	}

	@Override
	public long getReadTime() {
		return fReadTime;
	}

	@Override
	public long getFileSize() {
		return fFileSize;
	}

	@Override
	public long getContentsHash() {
		return fSource != null ? fSource.getContentsHash() : 0;
	}

	@Override
	public boolean hasError() {
		return fSource != null && fSource.hasError();
	}

	/**
	 * Valid with {@link InclusionKind#USE_SOURCE}.
	 * @return the codeReader or <code>null</code> if kind is different to {@link InclusionKind#USE_SOURCE}.
	 */
	public AbstractCharArray getSource() {
		return fSource;
	}

	/**
	 * Valid with {@link InclusionKind#FOUND_IN_INDEX}.
	 * @return the macroDefinitions or <code>null</code> if kind is different to {@link InclusionKind#FOUND_IN_INDEX}.
	 */
	public List<IIndexMacro> getMacroDefinitions() {
		return fMacroDefinitions;
	}

	/**
	 * Valid with {@link InclusionKind#FOUND_IN_INDEX}.
	 * @return the usingDirectives or <code>null</code> if kind is different to {@link InclusionKind#FOUND_IN_INDEX}.
	 */
	public List<ICPPUsingDirective> getUsingDirectives() {
		return fUsingDirectives;
	}

	/**
	 * Valid with {@link InclusionKind#FOUND_IN_INDEX}.
	 * @return the files included or <code>null</code> if kind is different to {@link InclusionKind#FOUND_IN_INDEX}.
	 */
	public List<IIndexFile> getFilesIncluded() {
		return fFiles;
	}

	public List<FileVersion> getNonPragmaOnceVersions() {
		return fNonPragmaOnceFiles;
	}

	/**
	 * Returns whether this inclusion was found by a heuristics.
	 */
	public boolean isFoundByHeuristics() {
		return fHeuristic;
	}

	public void setFoundByHeuristics(boolean val) {
		fHeuristic = val;
	}

	public boolean isSource() {
		return fIsSource;
	}

	public void setIsSource(boolean isSource) {
		fIsSource = isSource;
	}

	public ITranslationUnit getTranslationUnit() {
		return fTranslationUnit;
	}

	public void setTranslationUnit(ITranslationUnit tu) {
		fTranslationUnit = tu;
	}

	public IncludeSearchPathElement getFoundOnPath() {
		return fFoundOnPath;
	}

	public void setFoundOnPath(IncludeSearchPathElement isp) {
		fFoundOnPath = isp;
	}

	/**
	 * This method is slow. Use only for debugging.
	 */
	@Override
	public String toString() {
		return getSource().toString();
	}
}

Back to the top