From 9bccca7417e64b7eef56bd0867484e701cb75d45 Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Tue, 7 Oct 2008 14:52:24 +0000 Subject: [PATCH] Don't include directories, bug 243682. --- .../internal/index/tests/IndexBugsTests.java | 24 ++++++++ ...tandaloneIndexerFallbackReaderFactory.java | 4 +- .../StandaloneIndexerInputAdapter.java | 6 +- .../core/parser/InternalParserUtil.java | 3 +- .../core/pdom/indexer/FileExistsCache.java | 58 ++++++++++++++----- .../indexer/ProjectIndexerInputAdapter.java | 6 +- 6 files changed, 75 insertions(+), 26 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBugsTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBugsTests.java index ce5c3f7f2f0..fdb2c2fbe52 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBugsTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBugsTests.java @@ -1399,4 +1399,28 @@ public class IndexBugsTests extends BaseTestCase { fIndex.releaseReadLock(); } } + + + // #include "dir" + // #include "header.h" + public void testIncludsionOfFolders_Bug243682() throws Exception { + String contents= getContentsForTest(1)[0]; + final IIndexManager indexManager = CCorePlugin.getIndexManager(); + IFile sol= TestSourceReader.createFile(fCProject.getProject(), "f1/header.h", ""); + TestSourceReader.createFile(fCProject.getProject(), "dir/dummy.h", ""); + TestSourceReader.createFile(fCProject.getProject(), "header.h/dummy.h", ""); + IFile f1= TestSourceReader.createFile(fCProject.getProject(), "source1.cpp", contents); + indexManager.reindex(fCProject); + waitForIndexer(); + fIndex.acquireReadLock(); + try { + IIndexFile f= fIndex.getFile(ILinkage.CPP_LINKAGE_ID, IndexLocationFactory.getWorkspaceIFL(f1)); + IIndexInclude[] is= f.getIncludes(); + assertFalse(is[0].isResolved()); + assertTrue(is[1].isResolvedByHeuristics()); + assertEquals(sol.getFullPath().toString(), is[1].getIncludesLocation().getFullPath()); + } finally { + fIndex.releaseReadLock(); + } + } } \ No newline at end of file diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/indexer/StandaloneIndexerFallbackReaderFactory.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/indexer/StandaloneIndexerFallbackReaderFactory.java index 405df73eb4f..3a75a85e7b8 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/indexer/StandaloneIndexerFallbackReaderFactory.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/indexer/StandaloneIndexerFallbackReaderFactory.java @@ -34,7 +34,7 @@ public class StandaloneIndexerFallbackReaderFactory implements ICodeReaderFactor public CodeReader createCodeReaderForInclusion(String path) { try { - if (!new File(path).exists()) + if (!new File(path).isFile()) return null; return new CodeReader(path); } catch (IOException e) { @@ -44,7 +44,7 @@ public class StandaloneIndexerFallbackReaderFactory implements ICodeReaderFactor public CodeReader createCodeReaderForTranslationUnit(String path) { try { - if (!new File(path).exists()) + if (!new File(path).isFile()) return null; return new CodeReader(path); } catch (IOException e) { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/indexer/StandaloneIndexerInputAdapter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/indexer/StandaloneIndexerInputAdapter.java index a084bf5beda..63dd9d9c41a 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/indexer/StandaloneIndexerInputAdapter.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/indexer/StandaloneIndexerInputAdapter.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007 Wind River Systems, Inc. and others. + * Copyright (c) 2007, 2008 Wind River Systems, Inc. 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 @@ -82,7 +82,7 @@ public class StandaloneIndexerInputAdapter extends IndexerInputAdapter { @Override public boolean doesIncludeFileExist(String includePath) { - return new File(includePath).exists(); + return new File(includePath).isFile(); } @Override @@ -95,7 +95,7 @@ public class StandaloneIndexerInputAdapter extends IndexerInputAdapter { } catch (IOException e) { // use the original } - //Standalone indexing stores the absolute paths of files being indexed + //Stand-alone indexing stores the absolute paths of files being indexed result = new IndexFileLocation(URIUtil.toURI(includePath),includePath); fIflCache.put(includePath, result); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/InternalParserUtil.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/InternalParserUtil.java index 6d18f6984c9..b88385f8844 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/InternalParserUtil.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/InternalParserUtil.java @@ -23,8 +23,7 @@ public class InternalParserUtil extends ParserFactory { public static CodeReader createFileReader(String finalPath) { File includeFile = new File(finalPath); - if (includeFile.exists() && includeFile.isFile()) - { + if (includeFile.isFile()) { try { //use the canonical path so that in case of non-case-sensitive OSs //the CodeReader always has the same name as the file on disk with diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/FileExistsCache.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/FileExistsCache.java index c0db9350d0c..c10fd56afe1 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/FileExistsCache.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/FileExistsCache.java @@ -14,6 +14,7 @@ import java.io.File; import java.lang.ref.Reference; import java.lang.ref.SoftReference; import java.util.Arrays; +import java.util.BitSet; import java.util.HashMap; import java.util.Map; @@ -24,20 +25,28 @@ import java.util.Map; * @since 5.0 */ public final class FileExistsCache { - private static final String[] EMPTY_STRING_ARRAY= {}; + private static final Content EMPTY_STRING_ARRAY= new Content(new String[0]); private static final boolean CASE_INSENSITIVE = new File("a").equals(new File("A")); //$NON-NLS-1$ //$NON-NLS-2$ private static boolean BYPASS_CACHE= Boolean.getBoolean("CDT_INDEXER_BYPASS_FILE_EXISTS_CACHE"); //$NON-NLS-1$ - private Reference> fCache= null; + private static class Content { + public Content(String[] names) { + fNames= names; + fIsFile= new BitSet(names.length*2); + } + public String[] fNames; + public BitSet fIsFile; + } + private Reference> fCache= null; public FileExistsCache() { - fCache= new SoftReference>(new HashMap()); // before running out of memory the entire map will be thrown away. + fCache= new SoftReference>(new HashMap()); // before running out of memory the entire map will be thrown away. } - public boolean exists(String path) { + public boolean isFile(String path) { File file= new File(path); if (BYPASS_CACHE) { - return file.exists(); + return file.isFile(); } String parent= file.getParent(); @@ -45,30 +54,47 @@ public final class FileExistsCache { if (CASE_INSENSITIVE) name= name.toUpperCase(); - String[] avail= getExistsCache().get(parent); + Content avail= getExistsCache().get(parent); if (avail == null) { - avail= new File(parent).list(); - if (avail == null || avail.length == 0) { + String[] files= new File(parent).list(); + if (files == null || files.length == 0) { avail= EMPTY_STRING_ARRAY; } else { if (CASE_INSENSITIVE) { - for (int i = 0; i < avail.length; i++) { - avail[i]= avail[i].toUpperCase(); + for (int i = 0; i < files.length; i++) { + files[i]= files[i].toUpperCase(); } } - Arrays.sort(avail); + Arrays.sort(files); + avail= new Content(files); } getExistsCache().put(parent, avail); } - return Arrays.binarySearch(avail, name) >= 0; + int idx= Arrays.binarySearch(avail.fNames, name); + if (idx < 0) + return false; + idx *= 2; + + final BitSet isFileBitset = avail.fIsFile; + if (isFileBitset.get(idx)) + return true; + if (isFileBitset.get(idx+1)) + return false; + + if (file.isFile()) { + isFileBitset.set(idx); + return true; + } + isFileBitset.set(idx+1); + return false; } - private Map getExistsCache() { - Map cache= fCache.get(); + private Map getExistsCache() { + Map cache= fCache.get(); if (cache == null) { - cache= new HashMap(); - fCache= new SoftReference>(cache); // before running out of memory the entire map will be thrown away. + cache= new HashMap(); + fCache= new SoftReference>(cache); // before running out of memory the entire map will be thrown away. } return cache; } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ProjectIndexerInputAdapter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ProjectIndexerInputAdapter.java index bc5b71e63dd..9a6bd3c4cba 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ProjectIndexerInputAdapter.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ProjectIndexerInputAdapter.java @@ -81,7 +81,7 @@ public class ProjectIndexerInputAdapter extends IndexerInputAdapter { if (fIflCache == null) { return doResolveASTPath(includePath); } - if (!fExistsCache.exists(includePath)) { + if (!fExistsCache.isFile(includePath)) { return null; } IIndexFileLocation result= fIflCache.get(includePath); @@ -108,9 +108,9 @@ public class ProjectIndexerInputAdapter extends IndexerInputAdapter { @Override public boolean doesIncludeFileExist(String includePath) { if (fExistsCache != null) { - return fExistsCache.exists(includePath); + return fExistsCache.isFile(includePath); } - return new File(includePath).exists(); + return new File(includePath).isFile(); } @Override