diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/InclusionTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/InclusionTests.java index e86e40256dc..f850b5328ac 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/InclusionTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/InclusionTests.java @@ -81,7 +81,7 @@ public class InclusionTests extends PreprocessorTestsBase { // #include "one.h" // #include "f1/two.h" // #include "f1/f2/three.h" - public void testIncludeVariables() throws Exception { + public void testIncludeVariables_69529() throws Exception { String content= getAboveComment(); IFolder f0 = importFolder(".framework"); @@ -232,4 +232,22 @@ public class InclusionTests extends PreprocessorTestsBase { validateEOF(); } + // #include + 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(); + } } diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/settings/model/DescriptionScannerInfoProvider.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/settings/model/DescriptionScannerInfoProvider.java index 8ade2ba87f7..bfac6f7ad24 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/settings/model/DescriptionScannerInfoProvider.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/settings/model/DescriptionScannerInfoProvider.java @@ -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 * are made available under the terms of the Eclipse Public License v1.0 * 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.ICResourceDescription; 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.IResource; import org.eclipse.core.resources.IWorkspaceRoot; @@ -44,7 +45,7 @@ public class DescriptionScannerInfoProvider implements IScannerInfoProvider, ICP private IProject fProject; private ICProjectDescription fProjDes; private ICConfigurationDescription fCfgDes; - private Map fIdToLanguageSettingsMap = Collections.synchronizedMap(new HashMap()); + private Map fIdToLanguageSettingsMap = Collections.synchronizedMap(new HashMap()); private String fCurrentFileDescriptionId; private IScannerInfo fCurrentFileScannerInfo; private static final ScannerInfo INEXISTENT_SCANNER_INFO = new ScannerInfo(); @@ -109,9 +110,9 @@ public class DescriptionScannerInfoProvider implements IScannerInfoProvider, ICP IScannerInfo info; if(useMap) - info = (IScannerInfo)fIdToLanguageSettingsMap.get(mapKey); + info = fIdToLanguageSettingsMap.get(mapKey); else { - if(fCurrentFileScannerInfo != null){ + if(fCurrentFileScannerInfo != null && rcDes != null){ if(rcDes.getId().equals(fCurrentFileDescriptionId)) info = fCurrentFileScannerInfo; else { @@ -127,7 +128,7 @@ public class DescriptionScannerInfoProvider implements IScannerInfoProvider, ICP info = createScannerInfo(ls); if(useMap) fIdToLanguageSettingsMap.put(mapKey, info); - else { + else if (rcDes != null){ fCurrentFileScannerInfo = info; fCurrentFileDescriptionId = rcDes.getId(); } @@ -144,7 +145,7 @@ public class DescriptionScannerInfoProvider implements IScannerInfoProvider, ICP } private static ICMacroEntry[] getMacroEntries(ICLanguageSetting ls){ - ICLanguageSettingEntry entries[] = ls.getResolvedSettingEntries(ICLanguageSettingEntry.MACRO); + ICLanguageSettingEntry entries[] = ls.getResolvedSettingEntries(ICSettingEntry.MACRO); ICMacroEntry macroEntries[] = new ICMacroEntry[entries.length]; System.arraycopy(entries, 0, macroEntries, 0, entries.length); @@ -154,37 +155,37 @@ public class DescriptionScannerInfoProvider implements IScannerInfoProvider, ICP private IScannerInfo createProjectScannerInfo(){ ICFolderDescription foDes = fCfgDes.getRootFolderDescription(); ICLanguageSetting[] lSettings = foDes.getLanguageSettings(); - ICLanguageSettingPathEntry pathEntries[] = getPathEntries(lSettings, ICLanguageSettingEntry.INCLUDE_PATH); + ICLanguageSettingPathEntry pathEntries[] = getPathEntries(lSettings, ICSettingEntry.INCLUDE_PATH); String incs[] = getValues(pathEntries); - pathEntries = getPathEntries(lSettings, ICLanguageSettingEntry.INCLUDE_FILE); + pathEntries = getPathEntries(lSettings, ICSettingEntry.INCLUDE_FILE); String incFiles[] = getValues(pathEntries); - pathEntries = getPathEntries(lSettings, ICLanguageSettingEntry.MACRO_FILE); + pathEntries = getPathEntries(lSettings, ICSettingEntry.MACRO_FILE); String macroFiles[] = getValues(pathEntries); ICMacroEntry macroEntries[] = getMacroEntries(lSettings); - Map macrosMap = getValues(macroEntries); + Map macrosMap = getValues(macroEntries); return new ExtendedScannerInfo(macrosMap, incs, macroFiles, incFiles); } private ICMacroEntry[] getMacroEntries(ICLanguageSetting[] settings){ - LinkedHashSet set = getEntriesSet(ICLanguageSettingEntry.MACRO, settings); - return (ICMacroEntry[])set.toArray(new ICMacroEntry[set.size()]); + LinkedHashSet set = getEntriesSet(ICSettingEntry.MACRO, settings); + return set.toArray(new ICMacroEntry[set.size()]); } private ICLanguageSettingPathEntry[] getPathEntries(ICLanguageSetting[] settings, int kind){ - LinkedHashSet set = getEntriesSet(kind, settings); - return (ICLanguageSettingPathEntry[])set.toArray(new ICLanguageSettingPathEntry[set.size()]); + LinkedHashSet set = getEntriesSet(kind, settings); + return set.toArray(new ICLanguageSettingPathEntry[set.size()]); } - private LinkedHashSet getEntriesSet(int kind, ICLanguageSetting[] settings){ - LinkedHashSet set = new LinkedHashSet(); + private LinkedHashSet getEntriesSet(int kind, ICLanguageSetting[] settings){ + LinkedHashSet set = new LinkedHashSet(); ICLanguageSettingEntry[] langEntries; - for(int i = 0; i < settings.length; i++){ - langEntries = settings[i].getResolvedSettingEntries(kind); + for (ICLanguageSetting setting : settings) { + langEntries = setting.getResolvedSettingEntries(kind); if(langEntries.length != 0){ set.addAll(Arrays.asList(langEntries)); } @@ -196,29 +197,29 @@ public class DescriptionScannerInfoProvider implements IScannerInfoProvider, ICP if(ls == null) return createProjectScannerInfo(); - ICLanguageSettingPathEntry pathEntries[] = getPathEntries(ls, ICLanguageSettingEntry.INCLUDE_PATH); + ICLanguageSettingPathEntry pathEntries[] = getPathEntries(ls, ICSettingEntry.INCLUDE_PATH); String incs[] = getValues(pathEntries); - pathEntries = getPathEntries(ls, ICLanguageSettingEntry.INCLUDE_FILE); + pathEntries = getPathEntries(ls, ICSettingEntry.INCLUDE_FILE); String incFiles[] = getValues(pathEntries); - pathEntries = getPathEntries(ls, ICLanguageSettingEntry.MACRO_FILE); + pathEntries = getPathEntries(ls, ICSettingEntry.MACRO_FILE); String macroFiles[] = getValues(pathEntries); ICMacroEntry macroEntries[] = getMacroEntries(ls); - Map macrosMap = getValues(macroEntries); + Map macrosMap = getValues(macroEntries); return new ExtendedScannerInfo(macrosMap, incs, macroFiles, incFiles); } - private Map getValues(ICMacroEntry macroEntries[]){ - Map macrosMap = new HashMap(macroEntries.length); + private Map getValues(ICMacroEntry macroEntries[]){ + Map macrosMap = new HashMap(macroEntries.length); String name; String value; - for(int i = 0; i < macroEntries.length; i++){ - name = macroEntries[i].getName(); - value = macroEntries[i].getValue(); + for (ICMacroEntry macroEntry : macroEntries) { + name = macroEntry.getName(); + value = macroEntry.getValue(); macrosMap.put(name, value); } return macrosMap; @@ -228,24 +229,22 @@ public class DescriptionScannerInfoProvider implements IScannerInfoProvider, ICP String values[] = new String[pathEntries.length]; IPath path; int num = 0; - for(int i = 0; i < pathEntries.length; i++){ - String p = pathEntries[i].getValue(); + for (ICLanguageSettingPathEntry pathEntry : pathEntries) { + String p = pathEntry.getValue(); if(p == null) continue; //TODO: obtain location from pathEntries when entries are resolved path = new Path(p);//pathEntries[i].getLocation(); - if(pathEntries[i].isValueWorkspacePath()){ + if(pathEntry.isValueWorkspacePath()){ IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); IResource rc = root.findMember(path); if(rc != null){ path = rc.getLocation(); } - } else if (!path.isAbsolute()) { - IPath projLocation = fProject != null ? fProject.getLocation() : null; - if(projLocation != null) - path = projLocation.append(path); - } - if(path != null) + } + // do not make paths absolute, that's the preprocessor's job and is done differently + // depending on the entry + if(path != null) values[num++] = path.toOSString(); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/CPreprocessor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/CPreprocessor.java index f21fa15c5a1..25aa218c4da 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/CPreprocessor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/CPreprocessor.java @@ -203,7 +203,6 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable { fKeywords= new CharArrayIntMap(40, -1); fPPKeywords= new CharArrayIntMap(40, -1); configureKeywords(language, configuration); - configureIncludeSearchPath(info); fExpressionEvaluator= new ExpressionEvaluator(); fMacroDefinitionParser= new MacroDefinitionParser(); @@ -213,9 +212,10 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable { fIncludeFileResolutionHeuristics= (IIncludeFileResolutionHeuristics) ((IAdaptable) readerFactory).getAdapter(IIncludeFileResolutionHeuristics.class); } + final String filePath= new String(reader.filename); + configureIncludeSearchPath(new File(filePath).getParentFile(), info); setupMacroDictionary(configuration, info, language); - final String filePath= new String(reader.filename); fAllIncludedFiles.add(filePath); ILocationCtx ctx= fLocationMap.pushTranslationUnit(filePath, reader.buffer); fCodeReaderFactory.reportTranslationUnitFile(filePath); @@ -313,7 +313,7 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable { return array == null ? EMPTY_CHAR_ARRAY : array; } - private void configureIncludeSearchPath(IScannerInfo info) { + private void configureIncludeSearchPath(File directory, IScannerInfo info) { String[] searchPath= info.getIncludePaths(); int idx= 0; if (info instanceof IExtendedScannerInfo) { @@ -322,7 +322,7 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable { if (quoteIncludeSearchPath != null && quoteIncludeSearchPath.length > 0) { fIncludeSearchPath= new IncludeSearchPathElement[quoteIncludeSearchPath.length + searchPath.length]; 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]; } 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 fMacroDictionary.put(__CDT_PARSER__.getNameCharArray(), __CDT_PARSER__); fMacroDictionary.put(__STDC__.getNameCharArray(), __STDC__);