Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 609d72722b38732f16f72e01500d20b6268fec62 (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
/*******************************************************************************
 * Copyright (c) 2007, 2012 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
 *******************************************************************************/
package org.eclipse.cdt.build.internal.core.scannerconfig;

import java.util.Set;
import java.util.TreeSet;

import org.eclipse.cdt.build.core.scannerconfig.CfgInfoContext;
import org.eclipse.cdt.managedbuilder.core.IInputType;
import org.eclipse.cdt.managedbuilder.core.IResourceInfo;
import org.eclipse.cdt.managedbuilder.core.ITool;
import org.eclipse.cdt.managedbuilder.core.IToolChain;
import org.eclipse.cdt.managedbuilder.internal.core.Configuration;
import org.eclipse.cdt.managedbuilder.internal.core.FolderInfo;
import org.eclipse.cdt.managedbuilder.internal.core.InputType;
import org.eclipse.cdt.managedbuilder.internal.core.ManagedMakeMessages;
import org.eclipse.cdt.managedbuilder.internal.core.ResourceConfiguration;
import org.eclipse.cdt.managedbuilder.internal.core.Tool;
import org.eclipse.cdt.managedbuilder.internal.core.ToolChain;
import org.eclipse.core.runtime.Assert;

import com.ibm.icu.text.MessageFormat;

public class CfgScannerConfigUtil {
	public static CfgInfoContext adjustPerRcTypeContext(CfgInfoContext context) {
		if (((Configuration) context.getConfiguration()).isPreference())
			return context;
		Tool tool = (Tool) context.getTool();
		IResourceInfo rcInfo = context.getResourceInfo();
		IInputType inType = context.getInputType();
		boolean adjust = false;
		CfgInfoContext newContext = context;

		if (tool != null) {
			if (inType != null) {
				if (!tool.hasScannerConfigSettings(inType)) {
					//	        			tool = null;
					inType = null;
					adjust = true;
				}
			}
			if (inType == null) {
				if (!tool.hasScannerConfigSettings(null)) {
					tool = null;
					adjust = true;
				}
			}
		}
		if (tool == null) {
			if (inType != null) {
				inType = null;
				adjust = true;
			}

			if (rcInfo != null) {
				ToolChain tc = getToolChain(rcInfo);

				if (tc != null) {
					if (!tc.hasScannerConfigSettings()) {
						adjust = true;
						rcInfo = null;
					}
				}
			}
		}
		//        } else {
		//        	if(tool != null){
		//        		tool = null;
		//        		adjust = true;
		//        	}
		//        	if(rcInfo != null){
		//        		rcInfo = null;
		//        		adjust = true;
		//        	}
		//        	if(inType != null){
		//        		inType = null;
		//        		adjust = true;
		//        	}
		//        }

		if (adjust) {
			if (rcInfo == null)
				newContext = new CfgInfoContext(context.getConfiguration());
			else
				newContext = new CfgInfoContext(rcInfo, tool, inType);
		}

		return newContext;
	}

	private static ToolChain getToolChain(IResourceInfo rcInfo) {
		return rcInfo instanceof FolderInfo ? (ToolChain) ((FolderInfo) rcInfo).getToolChain()
				: (ToolChain) ((ResourceConfiguration) rcInfo).getBaseToolChain();
	}

	public static String getDefaultProfileId(CfgInfoContext context, boolean searchFirstIfNone) {
		String id = null;
		if (context.getInputType() != null)
			id = context.getInputType().getDiscoveryProfileId(context.getTool());
		if (id == null && context.getTool() != null)
			id = ((Tool) context.getTool()).getDiscoveryProfileId();
		if (id == null && context.getResourceInfo() != null) {
			ToolChain tCh = getToolChain(context.getResourceInfo());
			if (tCh != null)
				id = tCh.getScannerConfigDiscoveryProfileId();
		}
		if (id == null) {
			id = ((Configuration) context.getConfiguration()).getDiscoveryProfileId();
		}

		if (id == null && searchFirstIfNone) {
			id = getFirstProfileId(context.getConfiguration().getFilteredTools());
		}
		return id;
	}

	public static String getFirstProfileId(ITool[] tools) {
		String id = null;
		for (int i = 0; i < tools.length; i++) {
			ITool tool = tools[i];
			IInputType[] types = tool.getInputTypes();

			if (types.length != 0) {
				for (int k = 0; k < types.length; k++) {
					id = types[k].getDiscoveryProfileId(tool);
					if (id != null)
						break;
				}
			} else {
				id = ((Tool) tool).getDiscoveryProfileId();
			}

			if (id != null)
				break;
		}

		return id;
	}

	/**
	 * Search for toolchain's discovery profiles. Discovery profiles could be
	 * specified on toolchain level, input types level or in their super-classes.
	 *
	 * @param toolchain - toolchain to search for scanner discovery profiles.
	 * @return all available discovery profiles in given toolchain
	 */
	public static Set<String> getAllScannerDiscoveryProfileIds(IToolChain toolchain) {
		Assert.isNotNull(toolchain);

		Set<String> profiles = new TreeSet<String>();

		if (toolchain != null) {
			String toolchainProfileId = null;
			if (toolchain instanceof ToolChain) {
				// still allow a user a choice to select any legacy profiles
				toolchainProfileId = ((ToolChain) toolchain).getLegacyScannerConfigDiscoveryProfileId();
			} else {
				toolchainProfileId = toolchain.getScannerConfigDiscoveryProfileId();
			}
			if (toolchainProfileId != null && toolchainProfileId.length() > 0) {
				profiles.add(toolchainProfileId);
			}
			ITool[] tools = toolchain.getTools();
			for (ITool tool : tools) {
				profiles.addAll(getAllScannerDiscoveryProfileIds(tool));
			}
			IToolChain superClass = toolchain.getSuperClass();
			if (superClass != null) {
				profiles.addAll(getAllScannerDiscoveryProfileIds(superClass));
			}
		}

		return profiles;
	}

	/**
	 * Search for tool's discovery profiles. Discovery profiles could be retrieved
	 * from tool/input type super-class. Input type could hold list of profiles
	 * separated by pipe character '|'.
	 *
	 * @param tool - tool to search for scanner discovery profiles
	 * @return all available discovery profiles in given configuration
	 */
	public static Set<String> getAllScannerDiscoveryProfileIds(ITool tool) {
		Assert.isNotNull(tool);

		if (!(tool instanceof Tool)) {
			String msg = MessageFormat.format(ManagedMakeMessages.getString("CfgScannerConfigUtil_ErrorNotSupported"), //$NON-NLS-1$
					new Object[] { Tool.class.getName() });
			throw new UnsupportedOperationException(msg);
		}

		Set<String> profiles = new TreeSet<String>();

		for (IInputType inputType : ((Tool) tool).getAllInputTypes()) {
			for (String profileId : getAllScannerDiscoveryProfileIds(inputType)) {
				profiles.add(profileId);
			}
		}

		ITool superClass = tool.getSuperClass();
		if (superClass != null) {
			profiles.addAll(getAllScannerDiscoveryProfileIds(superClass));
		}
		return profiles;
	}

	/**
	 * Search for input type's discovery profiles. Discovery profiles could be specified
	 * on input type super-class. Input type could hold list of profiles
	 * separated by pipe character '|'.
	 *
	 * @param inputType - input type to search for scanner discovery profiles
	 * @return all available discovery profiles in given configuration
	 */
	private static Set<String> getAllScannerDiscoveryProfileIds(IInputType inputType) {
		Assert.isNotNull(inputType);

		if (!(inputType instanceof InputType)) {
			String msg = MessageFormat.format(ManagedMakeMessages.getString("CfgScannerConfigUtil_ErrorNotSupported"), //$NON-NLS-1$
					new Object[] { InputType.class.getName() });
			throw new UnsupportedOperationException(msg);
		}

		Set<String> profiles = new TreeSet<String>();

		String attribute = ((InputType) inputType).getLegacyDiscoveryProfileIdAttribute();
		if (attribute != null) {
			// FIXME: temporary; we should add new method to IInputType instead of that
			for (String profileId : attribute.split("\\|")) { //$NON-NLS-1$
				profiles.add(profileId);
			}
		}
		IInputType superClass = inputType.getSuperClass();
		if (superClass != null) {
			profiles.addAll(getAllScannerDiscoveryProfileIds(superClass));
		}

		return profiles;
	}
}

Back to the top