1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-22 06:02:11 +02:00

Bug 562452: Avoid using .C for C++ files when calculating specs

Change-Id: I0fe24a8343e73d501ae09e8bf3721e3d310a696d
This commit is contained in:
Jonah Graham 2020-04-25 12:16:25 -04:00
parent 5947786365
commit eeebe5234c
3 changed files with 34 additions and 16 deletions

View file

@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2 Bundle-ManifestVersion: 2
Bundle-Name: %pluginName Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.cdt.managedbuilder.core; singleton:=true Bundle-SymbolicName: org.eclipse.cdt.managedbuilder.core; singleton:=true
Bundle-Version: 8.8.0.qualifier Bundle-Version: 8.9.0.qualifier
Bundle-Activator: org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin Bundle-Activator: org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin
Bundle-Vendor: %providerName Bundle-Vendor: %providerName
Bundle-Localization: plugin Bundle-Localization: plugin

View file

@ -26,6 +26,7 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.CCorePlugin;
@ -894,23 +895,41 @@ public abstract class AbstractBuiltinSpecsDetector extends AbstractLanguageSetti
* @return file extension associated with the language or {@code null} if not found. * @return file extension associated with the language or {@code null} if not found.
*/ */
protected String getSpecFileExtension(String languageId) { protected String getSpecFileExtension(String languageId) {
String ext = null; Optional<String> extension = Optional.empty();
ILanguageDescriptor langDescriptor = LanguageManager.getInstance().getLanguageDescriptor(languageId); ILanguageDescriptor langDescriptor = LanguageManager.getInstance().getLanguageDescriptor(languageId);
if (langDescriptor != null) { if (langDescriptor != null) {
IContentType[] contentTypes = langDescriptor.getContentTypes(); IContentType[] contentTypes = langDescriptor.getContentTypes();
if (contentTypes != null && contentTypes.length > 0) { if (contentTypes != null && contentTypes.length > 0) {
String[] fileExtensions = contentTypes[0].getFileSpecs(IContentType.FILE_EXTENSION_SPEC); String[] fileExtensions = contentTypes[0].getFileSpecs(IContentType.FILE_EXTENSION_SPEC);
if (fileExtensions != null && fileExtensions.length > 0) { if (fileExtensions != null) {
ext = fileExtensions[0]; List<String> extensions = Arrays.asList(fileExtensions);
extension = selectBestSpecFileExtension(extensions);
} }
} }
} }
if (ext == null) { if (!extension.isPresent()) {
ManagedBuilderCorePlugin.log(new Status(IStatus.ERROR, ManagedBuilderCorePlugin.PLUGIN_ID, ManagedBuilderCorePlugin.log(new Status(IStatus.ERROR, ManagedBuilderCorePlugin.PLUGIN_ID,
"Unable to find file extension for language " + languageId)); //$NON-NLS-1$ "Unable to find file extension for language " + languageId)); //$NON-NLS-1$
return null;
} }
return ext; return extension.get();
}
/**
* Return the best extension to use for calculating spec file from a list of extensions.
* @param extensions list of possible extensions to choose from
* @return one of the extensions deemed the best one to use from the list
* @since 8.9
*/
protected Optional<String> selectBestSpecFileExtension(List<String> extensions) {
return extensions.stream().filter(s -> s != null && !s.isEmpty()).findFirst().map(ext -> {
// Bug 562452: Special case where we prefer not to use .C for c++ files.
if ("C".equals(ext) && extensions.contains("cpp")) { //$NON-NLS-1$//$NON-NLS-2$
return "cpp"; //$NON-NLS-1$
}
return ext;
});
} }
/** /**

View file

@ -16,6 +16,7 @@
package org.eclipse.cdt.managedbuilder.language.settings.providers; package org.eclipse.cdt.managedbuilder.language.settings.providers;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -139,19 +140,17 @@ public abstract class ToolchainBuiltinSpecsDetector extends AbstractBuiltinSpecs
@Override @Override
protected String getSpecFileExtension(String languageId) { protected String getSpecFileExtension(String languageId) {
Optional<String> found = languageTool(languageId)// Optional<String[]> optionalExtensions = languageTool(languageId)
.flatMap(t -> Optional.of(t.getAllInputExtensions()))// .flatMap(t -> Optional.of(t.getAllInputExtensions()));
.flatMap(e -> Arrays.asList(e).stream().findFirst()); List<String> extensions = optionalExtensions.map(Arrays::asList).orElseGet(() -> Collections.emptyList());
if (!found.isPresent()) { Optional<String> extension = selectBestSpecFileExtension(extensions);
if (!extension.isPresent()) {
//this looks like either invalid configuration settings or API issue
ManagedBuilderCorePlugin.error(NLS.bind("Unable to find file extension for language {0}", languageId)); //$NON-NLS-1$ ManagedBuilderCorePlugin.error(NLS.bind("Unable to find file extension for language {0}", languageId)); //$NON-NLS-1$
return null; return null;
} }
String firstInput = found.get(); return extension.get();
if (firstInput == null || firstInput.isEmpty()) {
//this looks like either invalid configuration settings or API issue
ManagedBuilderCorePlugin.error(NLS.bind("Unable to find file extension for language {0}", languageId)); //$NON-NLS-1$
}
return firstInput;
} }
@Override @Override