Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 920947af762643456fccb17ea987f6bf3b4a78c5 (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
/*******************************************************************************
 * Copyright (c) 2007, 2011 Intel Corporation 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:
 * Intel Corporation - Initial API and implementation
 * IBM Corporation
 *******************************************************************************/
package org.eclipse.cdt.make.core.scannerconfig;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.cdt.internal.core.SafeStringInterner;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

/**
 * @noinstantiate This class is not intended to be instantiated by clients.
 */
public final class PathInfo {
	private static final Path[] EMPTY_PATH_ARRAY = new Path[0];
	public final static PathInfo EMPTY_INFO = new PathInfo(null, null, null, null, null);

	private static int EMPTY_CODE = 53;

	private IPath[] fIncludePaths;
	private IPath[] fQuoteIncludePaths;
	private HashMap<String, String> fSymbols;
	private IPath[] fIncludeFiles;
	private IPath[] fMacroFiles;
	private int fHash;

	public PathInfo(IPath[] includePaths, IPath[] quoteIncludePaths, Map<String, String> symbols, IPath[] includeFiles,
			IPath[] macroFiles) {
		fIncludePaths = includePaths != null && includePaths.length != 0 ? (IPath[]) includePaths.clone()
				: EMPTY_PATH_ARRAY;
		fQuoteIncludePaths = quoteIncludePaths != null && quoteIncludePaths.length != 0
				? (IPath[]) quoteIncludePaths.clone()
				: EMPTY_PATH_ARRAY;
		fSymbols = symbols != null && symbols.size() != 0 ? getInternedHashMap(symbols)
				: new HashMap<String, String>(0);
		fIncludeFiles = includeFiles != null && includeFiles.length != 0 ? (IPath[]) includeFiles.clone()
				: EMPTY_PATH_ARRAY;
		fMacroFiles = macroFiles != null && macroFiles.length != 0 ? (IPath[]) macroFiles.clone() : EMPTY_PATH_ARRAY;
	}

	/**
	 * Returns a new HashMap whereby all the strings used as keys and values are interned.
	 *
	 * @param oldMap
	 * @return HashMap<String, String>
	 */
	protected HashMap<String, String> getInternedHashMap(Map<String, String> oldMap) {
		if (oldMap == null)
			return null;

		if (oldMap.isEmpty())
			return new HashMap<String, String>(oldMap);

		HashMap<String, String> newMap = new HashMap<String, String>(oldMap.size());
		for (String key : oldMap.keySet()) {
			newMap.put(SafeStringInterner.safeIntern(key), SafeStringInterner.safeIntern(oldMap.get(key)));
		}

		return newMap;
	}

	/**
	 * Get include paths
	 */
	public IPath[] getIncludePaths() {
		return fIncludePaths.length != 0 ? (IPath[]) fIncludePaths.clone() : EMPTY_PATH_ARRAY;
	}

	/**
	 * Get quote include paths (for #include "...")
	 */
	public IPath[] getQuoteIncludePaths() {
		return fQuoteIncludePaths.length != 0 ? (IPath[]) fQuoteIncludePaths.clone() : EMPTY_PATH_ARRAY;
	}

	/**
	 * Get defined symbols
	 */
	@SuppressWarnings("unchecked")
	public Map<String, String> getSymbols() {
		return (Map<String, String>) fSymbols.clone();
	}

	/**
	 * Get include files (gcc option -include)
	 */
	public IPath[] getIncludeFiles() {
		return fIncludeFiles.length != 0 ? (IPath[]) fIncludeFiles.clone() : EMPTY_PATH_ARRAY;
	}

	/**
	 * Get macro files (gcc option -imacros)
	 */
	public IPath[] getMacroFiles() {
		return fMacroFiles.length != 0 ? (IPath[]) fMacroFiles.clone() : EMPTY_PATH_ARRAY;
	}

	/**
	 * Returns if there is any discovered scanner info
	 */
	public boolean isEmpty() {
		return fIncludePaths.length == 0 && fQuoteIncludePaths.length == 0 && fSymbols.size() == 0
				&& fIncludeFiles.length == 0 && fMacroFiles.length == 0;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;

		if (!(obj instanceof PathInfo))
			return false;

		PathInfo other = (PathInfo) obj;

		if (!Arrays.equals(fIncludePaths, other.fIncludePaths))
			return false;
		if (!Arrays.equals(fQuoteIncludePaths, other.fQuoteIncludePaths))
			return false;
		if (!fSymbols.equals(other.fSymbols))
			return false;
		if (!Arrays.equals(fIncludeFiles, other.fIncludeFiles))
			return false;
		if (!Arrays.equals(fMacroFiles, other.fMacroFiles))
			return false;

		return true;
	}

	@Override
	public int hashCode() {
		int hash = fHash;
		if (hash == 0) {
			hash = EMPTY_CODE;

			if (fIncludePaths.length != 0) {
				for (int i = 0; i < fIncludePaths.length; i++) {
					hash += fIncludePaths[i].hashCode();
				}
			}

			if (fQuoteIncludePaths.length != 0) {
				for (int i = 0; i < fQuoteIncludePaths.length; i++) {
					hash += fQuoteIncludePaths[i].hashCode();
				}
			}

			hash += fSymbols.hashCode();

			if (fIncludeFiles.length != 0) {
				for (int i = 0; i < fIncludeFiles.length; i++) {
					hash += fIncludeFiles[i].hashCode();
				}
			}

			if (fMacroFiles.length != 0) {
				for (int i = 0; i < fMacroFiles.length; i++) {
					hash += fMacroFiles[i].hashCode();
				}
			}

			fHash = hash;
		}
		return hash;
	}
}

Back to the top