1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-17 21:25:58 +02:00

Relative include search paths, bug 243170.

This commit is contained in:
Markus Schorn 2009-07-10 08:37:57 +00:00
parent 031bf4556a
commit 88c5aa9a43
3 changed files with 67 additions and 43 deletions

View file

@ -81,7 +81,7 @@ public class InclusionTests extends PreprocessorTestsBase {
// #include "one.h" // #include "one.h"
// #include "f1/two.h" // #include "f1/two.h"
// #include "f1/f2/three.h" // #include "f1/f2/three.h"
public void testIncludeVariables() throws Exception { public void testIncludeVariables_69529() throws Exception {
String content= getAboveComment(); String content= getAboveComment();
IFolder f0 = importFolder(".framework"); IFolder f0 = importFolder(".framework");
@ -232,4 +232,22 @@ public class InclusionTests extends PreprocessorTestsBase {
validateEOF(); validateEOF();
} }
// #include <inc/test.h>
public void testRelativeIncludes_243170() throws Exception {
String content= getAboveComment();
IFolder f0 = importFolder("f1");
importFolder("f1/f2");
importFolder("f1/f2/inc");
importFile("f1/f2/inc/test.h", "1");
IFile base = importFile("f1/base.cpp", getAboveComment());
String[] path = {"f2"}; // relative include
IScannerInfo scannerInfo = new ExtendedScannerInfo(Collections.EMPTY_MAP, path, new String[]{}, null);
CodeReader reader= new CodeReader(base.getLocation().toString());
initializeScanner(reader, ParserLanguage.C, ParserMode.COMPLETE_PARSE, scannerInfo);
validateInteger("1");
validateEOF();
}
} }

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2007 Intel Corporation and others. * Copyright (c) 2007, 2009 Intel Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -33,6 +33,7 @@ import org.eclipse.cdt.core.settings.model.ICProjectDescription;
import org.eclipse.cdt.core.settings.model.ICProjectDescriptionListener; import org.eclipse.cdt.core.settings.model.ICProjectDescriptionListener;
import org.eclipse.cdt.core.settings.model.ICResourceDescription; import org.eclipse.cdt.core.settings.model.ICResourceDescription;
import org.eclipse.cdt.core.settings.model.ICSettingBase; import org.eclipse.cdt.core.settings.model.ICSettingBase;
import org.eclipse.cdt.core.settings.model.ICSettingEntry;
import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot; import org.eclipse.core.resources.IWorkspaceRoot;
@ -44,7 +45,7 @@ public class DescriptionScannerInfoProvider implements IScannerInfoProvider, ICP
private IProject fProject; private IProject fProject;
private ICProjectDescription fProjDes; private ICProjectDescription fProjDes;
private ICConfigurationDescription fCfgDes; private ICConfigurationDescription fCfgDes;
private Map fIdToLanguageSettingsMap = Collections.synchronizedMap(new HashMap()); private Map<Object, IScannerInfo> fIdToLanguageSettingsMap = Collections.synchronizedMap(new HashMap<Object, IScannerInfo>());
private String fCurrentFileDescriptionId; private String fCurrentFileDescriptionId;
private IScannerInfo fCurrentFileScannerInfo; private IScannerInfo fCurrentFileScannerInfo;
private static final ScannerInfo INEXISTENT_SCANNER_INFO = new ScannerInfo(); private static final ScannerInfo INEXISTENT_SCANNER_INFO = new ScannerInfo();
@ -109,9 +110,9 @@ public class DescriptionScannerInfoProvider implements IScannerInfoProvider, ICP
IScannerInfo info; IScannerInfo info;
if(useMap) if(useMap)
info = (IScannerInfo)fIdToLanguageSettingsMap.get(mapKey); info = fIdToLanguageSettingsMap.get(mapKey);
else { else {
if(fCurrentFileScannerInfo != null){ if(fCurrentFileScannerInfo != null && rcDes != null){
if(rcDes.getId().equals(fCurrentFileDescriptionId)) if(rcDes.getId().equals(fCurrentFileDescriptionId))
info = fCurrentFileScannerInfo; info = fCurrentFileScannerInfo;
else { else {
@ -127,7 +128,7 @@ public class DescriptionScannerInfoProvider implements IScannerInfoProvider, ICP
info = createScannerInfo(ls); info = createScannerInfo(ls);
if(useMap) if(useMap)
fIdToLanguageSettingsMap.put(mapKey, info); fIdToLanguageSettingsMap.put(mapKey, info);
else { else if (rcDes != null){
fCurrentFileScannerInfo = info; fCurrentFileScannerInfo = info;
fCurrentFileDescriptionId = rcDes.getId(); fCurrentFileDescriptionId = rcDes.getId();
} }
@ -144,7 +145,7 @@ public class DescriptionScannerInfoProvider implements IScannerInfoProvider, ICP
} }
private static ICMacroEntry[] getMacroEntries(ICLanguageSetting ls){ private static ICMacroEntry[] getMacroEntries(ICLanguageSetting ls){
ICLanguageSettingEntry entries[] = ls.getResolvedSettingEntries(ICLanguageSettingEntry.MACRO); ICLanguageSettingEntry entries[] = ls.getResolvedSettingEntries(ICSettingEntry.MACRO);
ICMacroEntry macroEntries[] = new ICMacroEntry[entries.length]; ICMacroEntry macroEntries[] = new ICMacroEntry[entries.length];
System.arraycopy(entries, 0, macroEntries, 0, entries.length); System.arraycopy(entries, 0, macroEntries, 0, entries.length);
@ -154,37 +155,37 @@ public class DescriptionScannerInfoProvider implements IScannerInfoProvider, ICP
private IScannerInfo createProjectScannerInfo(){ private IScannerInfo createProjectScannerInfo(){
ICFolderDescription foDes = fCfgDes.getRootFolderDescription(); ICFolderDescription foDes = fCfgDes.getRootFolderDescription();
ICLanguageSetting[] lSettings = foDes.getLanguageSettings(); ICLanguageSetting[] lSettings = foDes.getLanguageSettings();
ICLanguageSettingPathEntry pathEntries[] = getPathEntries(lSettings, ICLanguageSettingEntry.INCLUDE_PATH); ICLanguageSettingPathEntry pathEntries[] = getPathEntries(lSettings, ICSettingEntry.INCLUDE_PATH);
String incs[] = getValues(pathEntries); String incs[] = getValues(pathEntries);
pathEntries = getPathEntries(lSettings, ICLanguageSettingEntry.INCLUDE_FILE); pathEntries = getPathEntries(lSettings, ICSettingEntry.INCLUDE_FILE);
String incFiles[] = getValues(pathEntries); String incFiles[] = getValues(pathEntries);
pathEntries = getPathEntries(lSettings, ICLanguageSettingEntry.MACRO_FILE); pathEntries = getPathEntries(lSettings, ICSettingEntry.MACRO_FILE);
String macroFiles[] = getValues(pathEntries); String macroFiles[] = getValues(pathEntries);
ICMacroEntry macroEntries[] = getMacroEntries(lSettings); ICMacroEntry macroEntries[] = getMacroEntries(lSettings);
Map macrosMap = getValues(macroEntries); Map<String, String> macrosMap = getValues(macroEntries);
return new ExtendedScannerInfo(macrosMap, incs, macroFiles, incFiles); return new ExtendedScannerInfo(macrosMap, incs, macroFiles, incFiles);
} }
private ICMacroEntry[] getMacroEntries(ICLanguageSetting[] settings){ private ICMacroEntry[] getMacroEntries(ICLanguageSetting[] settings){
LinkedHashSet set = getEntriesSet(ICLanguageSettingEntry.MACRO, settings); LinkedHashSet<ICLanguageSettingEntry> set = getEntriesSet(ICSettingEntry.MACRO, settings);
return (ICMacroEntry[])set.toArray(new ICMacroEntry[set.size()]); return set.toArray(new ICMacroEntry[set.size()]);
} }
private ICLanguageSettingPathEntry[] getPathEntries(ICLanguageSetting[] settings, int kind){ private ICLanguageSettingPathEntry[] getPathEntries(ICLanguageSetting[] settings, int kind){
LinkedHashSet set = getEntriesSet(kind, settings); LinkedHashSet<ICLanguageSettingEntry> set = getEntriesSet(kind, settings);
return (ICLanguageSettingPathEntry[])set.toArray(new ICLanguageSettingPathEntry[set.size()]); return set.toArray(new ICLanguageSettingPathEntry[set.size()]);
} }
private LinkedHashSet getEntriesSet(int kind, ICLanguageSetting[] settings){ private LinkedHashSet<ICLanguageSettingEntry> getEntriesSet(int kind, ICLanguageSetting[] settings){
LinkedHashSet set = new LinkedHashSet(); LinkedHashSet<ICLanguageSettingEntry> set = new LinkedHashSet<ICLanguageSettingEntry>();
ICLanguageSettingEntry[] langEntries; ICLanguageSettingEntry[] langEntries;
for(int i = 0; i < settings.length; i++){ for (ICLanguageSetting setting : settings) {
langEntries = settings[i].getResolvedSettingEntries(kind); langEntries = setting.getResolvedSettingEntries(kind);
if(langEntries.length != 0){ if(langEntries.length != 0){
set.addAll(Arrays.asList(langEntries)); set.addAll(Arrays.asList(langEntries));
} }
@ -196,29 +197,29 @@ public class DescriptionScannerInfoProvider implements IScannerInfoProvider, ICP
if(ls == null) if(ls == null)
return createProjectScannerInfo(); return createProjectScannerInfo();
ICLanguageSettingPathEntry pathEntries[] = getPathEntries(ls, ICLanguageSettingEntry.INCLUDE_PATH); ICLanguageSettingPathEntry pathEntries[] = getPathEntries(ls, ICSettingEntry.INCLUDE_PATH);
String incs[] = getValues(pathEntries); String incs[] = getValues(pathEntries);
pathEntries = getPathEntries(ls, ICLanguageSettingEntry.INCLUDE_FILE); pathEntries = getPathEntries(ls, ICSettingEntry.INCLUDE_FILE);
String incFiles[] = getValues(pathEntries); String incFiles[] = getValues(pathEntries);
pathEntries = getPathEntries(ls, ICLanguageSettingEntry.MACRO_FILE); pathEntries = getPathEntries(ls, ICSettingEntry.MACRO_FILE);
String macroFiles[] = getValues(pathEntries); String macroFiles[] = getValues(pathEntries);
ICMacroEntry macroEntries[] = getMacroEntries(ls); ICMacroEntry macroEntries[] = getMacroEntries(ls);
Map macrosMap = getValues(macroEntries); Map<String, String> macrosMap = getValues(macroEntries);
return new ExtendedScannerInfo(macrosMap, incs, macroFiles, incFiles); return new ExtendedScannerInfo(macrosMap, incs, macroFiles, incFiles);
} }
private Map getValues(ICMacroEntry macroEntries[]){ private Map<String, String> getValues(ICMacroEntry macroEntries[]){
Map macrosMap = new HashMap(macroEntries.length); Map<String, String> macrosMap = new HashMap<String, String>(macroEntries.length);
String name; String name;
String value; String value;
for(int i = 0; i < macroEntries.length; i++){ for (ICMacroEntry macroEntry : macroEntries) {
name = macroEntries[i].getName(); name = macroEntry.getName();
value = macroEntries[i].getValue(); value = macroEntry.getValue();
macrosMap.put(name, value); macrosMap.put(name, value);
} }
return macrosMap; return macrosMap;
@ -228,23 +229,21 @@ public class DescriptionScannerInfoProvider implements IScannerInfoProvider, ICP
String values[] = new String[pathEntries.length]; String values[] = new String[pathEntries.length];
IPath path; IPath path;
int num = 0; int num = 0;
for(int i = 0; i < pathEntries.length; i++){ for (ICLanguageSettingPathEntry pathEntry : pathEntries) {
String p = pathEntries[i].getValue(); String p = pathEntry.getValue();
if(p == null) if(p == null)
continue; continue;
//TODO: obtain location from pathEntries when entries are resolved //TODO: obtain location from pathEntries when entries are resolved
path = new Path(p);//pathEntries[i].getLocation(); path = new Path(p);//pathEntries[i].getLocation();
if(pathEntries[i].isValueWorkspacePath()){ if(pathEntry.isValueWorkspacePath()){
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IResource rc = root.findMember(path); IResource rc = root.findMember(path);
if(rc != null){ if(rc != null){
path = rc.getLocation(); path = rc.getLocation();
} }
} else if (!path.isAbsolute()) {
IPath projLocation = fProject != null ? fProject.getLocation() : null;
if(projLocation != null)
path = projLocation.append(path);
} }
// do not make paths absolute, that's the preprocessor's job and is done differently
// depending on the entry
if(path != null) if(path != null)
values[num++] = path.toOSString(); values[num++] = path.toOSString();
} }

View file

@ -203,7 +203,6 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable {
fKeywords= new CharArrayIntMap(40, -1); fKeywords= new CharArrayIntMap(40, -1);
fPPKeywords= new CharArrayIntMap(40, -1); fPPKeywords= new CharArrayIntMap(40, -1);
configureKeywords(language, configuration); configureKeywords(language, configuration);
configureIncludeSearchPath(info);
fExpressionEvaluator= new ExpressionEvaluator(); fExpressionEvaluator= new ExpressionEvaluator();
fMacroDefinitionParser= new MacroDefinitionParser(); fMacroDefinitionParser= new MacroDefinitionParser();
@ -213,9 +212,10 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable {
fIncludeFileResolutionHeuristics= (IIncludeFileResolutionHeuristics) ((IAdaptable) readerFactory).getAdapter(IIncludeFileResolutionHeuristics.class); fIncludeFileResolutionHeuristics= (IIncludeFileResolutionHeuristics) ((IAdaptable) readerFactory).getAdapter(IIncludeFileResolutionHeuristics.class);
} }
final String filePath= new String(reader.filename);
configureIncludeSearchPath(new File(filePath).getParentFile(), info);
setupMacroDictionary(configuration, info, language); setupMacroDictionary(configuration, info, language);
final String filePath= new String(reader.filename);
fAllIncludedFiles.add(filePath); fAllIncludedFiles.add(filePath);
ILocationCtx ctx= fLocationMap.pushTranslationUnit(filePath, reader.buffer); ILocationCtx ctx= fLocationMap.pushTranslationUnit(filePath, reader.buffer);
fCodeReaderFactory.reportTranslationUnitFile(filePath); fCodeReaderFactory.reportTranslationUnitFile(filePath);
@ -313,7 +313,7 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable {
return array == null ? EMPTY_CHAR_ARRAY : array; return array == null ? EMPTY_CHAR_ARRAY : array;
} }
private void configureIncludeSearchPath(IScannerInfo info) { private void configureIncludeSearchPath(File directory, IScannerInfo info) {
String[] searchPath= info.getIncludePaths(); String[] searchPath= info.getIncludePaths();
int idx= 0; int idx= 0;
if (info instanceof IExtendedScannerInfo) { if (info instanceof IExtendedScannerInfo) {
@ -322,7 +322,7 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable {
if (quoteIncludeSearchPath != null && quoteIncludeSearchPath.length > 0) { if (quoteIncludeSearchPath != null && quoteIncludeSearchPath.length > 0) {
fIncludeSearchPath= new IncludeSearchPathElement[quoteIncludeSearchPath.length + searchPath.length]; fIncludeSearchPath= new IncludeSearchPathElement[quoteIncludeSearchPath.length + searchPath.length];
for (String qip : quoteIncludeSearchPath) { for (String qip : quoteIncludeSearchPath) {
fIncludeSearchPath[idx++]= new IncludeSearchPathElement(qip, true); fIncludeSearchPath[idx++]= new IncludeSearchPathElement(makeAbsolute(directory, qip), true);
} }
} }
} }
@ -330,11 +330,18 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable {
fIncludeSearchPath= new IncludeSearchPathElement[searchPath.length]; fIncludeSearchPath= new IncludeSearchPathElement[searchPath.length];
} }
for (String path : searchPath) { for (String path : searchPath) {
fIncludeSearchPath[idx++]= new IncludeSearchPathElement(path, false); fIncludeSearchPath[idx++]= new IncludeSearchPathElement(makeAbsolute(directory, path), false);
} }
} }
private void setupMacroDictionary(IScannerExtensionConfiguration config, IScannerInfo info, ParserLanguage lang) { private String makeAbsolute(File directory, String inlcudePath) {
if (directory == null || new File(inlcudePath).isAbsolute()) {
return inlcudePath;
}
return ScannerUtility.createReconciledPath(directory.getAbsolutePath(), inlcudePath);
}
private void setupMacroDictionary(IScannerExtensionConfiguration config, IScannerInfo info, ParserLanguage lang) {
// built in macros // built in macros
fMacroDictionary.put(__CDT_PARSER__.getNameCharArray(), __CDT_PARSER__); fMacroDictionary.put(__CDT_PARSER__.getNameCharArray(), __CDT_PARSER__);
fMacroDictionary.put(__STDC__.getNameCharArray(), __STDC__); fMacroDictionary.put(__STDC__.getNameCharArray(), __STDC__);