1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-24 17:35:35 +02:00

Bug 45203. Added a test for indirect header export and fixed a bug

uncovered by the test.
This commit is contained in:
Sergey Prigogin 2013-08-15 10:37:04 -07:00
parent 76122eab0d
commit 6b95df5d67
3 changed files with 49 additions and 4 deletions

View file

@ -24,7 +24,9 @@ import org.eclipse.text.edits.TextEdit;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.ui.PreferenceConstants;
import org.eclipse.cdt.internal.ui.refactoring.includes.HeaderSubstitutionMap;
import org.eclipse.cdt.internal.ui.refactoring.includes.IHeaderChooser;
import org.eclipse.cdt.internal.ui.refactoring.includes.IncludeMap;
import org.eclipse.cdt.internal.ui.refactoring.includes.IncludeOrganizer;
import org.eclipse.cdt.internal.ui.refactoring.includes.IncludePreferences;
import org.eclipse.cdt.internal.ui.refactoring.includes.IncludePreferences.UnusedStatementDisposition;
@ -58,6 +60,7 @@ public class IncludeOrganizerTest extends IncludesTestBase {
preferenceStore.setToDefault(PreferenceConstants.FORWARD_DECLARE_TEMPLATES);
preferenceStore.setToDefault(PreferenceConstants.FORWARD_DECLARE_NAMESPACE_ELEMENTS);
preferenceStore.setToDefault(PreferenceConstants.INCLUDES_ALLOW_REORDERING);
preferenceStore.setToDefault(IncludePreferences.INCLUDES_HEADER_SUBSTITUTION);
preferenceStore.setToDefault(IncludePreferences.INCLUDES_SYMBOL_EXPORTING_HEADERS);
}
@ -338,6 +341,33 @@ public class IncludeOrganizerTest extends IncludesTestBase {
assertExpectedResults();
}
//h1.h
//class A {};
//h2.h
//#include "h1.h" // IWYU pragma: export
//class B {};
//h3.h
//#include "h2.h"
//source.cpp
//A a;
//B b;
//====================
//#include "h3.h"
//
//A a;
//B b;
public void testIndirectHeaderExport() throws Exception {
HeaderSubstitutionMap headerMap = new HeaderSubstitutionMap("Test", false,
new IncludeMap(true, new String[] { "h2.h", "h3.h"}),
new IncludeMap(false));
getPreferenceStore().setValue(IncludePreferences.INCLUDES_HEADER_SUBSTITUTION,
HeaderSubstitutionMap.serializeMaps(Collections.singletonList(headerMap)));
assertExpectedResults();
}
//h1.h
//#define M2(t, p) t p

View file

@ -59,7 +59,7 @@ public class IncludeCreationContext extends InclusionContext {
}
/**
* Removes headers that are exported by other headers that will be included
* Removes headers that are exported by other headers that will be included.
*/
public void removeExportedHeaders() throws CoreException {
// Index files keyed by their absolute paths.
@ -69,8 +69,14 @@ public class IncludeCreationContext extends InclusionContext {
filesByPath.put(path, file);
}
Set<IPath> exportedHeaders = new HashSet<IPath>();
for (IPath path : fHeadersToInclude) {
removeExportedHeaders(fHeadersAlreadyIncluded, filesByPath);
removeExportedHeaders(fHeadersToInclude, filesByPath);
}
private void removeExportedHeaders(Set<IPath> exportingHeaders,
Map<IPath, IIndexFile> filesByPath) throws CoreException {
Set<IPath> exportedHeaders = new HashSet<IPath>();
for (IPath path : exportingHeaders) {
if (!exportedHeaders.contains(path)) {
IIndexFile file = filesByPath.get(path);
if (file != null) { // file can be null if the header was not indexed.
@ -91,7 +97,7 @@ public class IncludeCreationContext extends InclusionContext {
}
}
fHeadersToInclude.removeAll(exportedHeaders);
}
}
private static IPath getPath(IIndexFile file) throws CoreException {
return IndexLocationFactory.getAbsolutePath(file.getLocation());
@ -103,6 +109,7 @@ public class IncludeCreationContext extends InclusionContext {
public final void addHeaderToInclude(IPath header) {
fHeadersToInclude.add(header);
fHeadersAlreadyIncluded.add(header);
}
public final boolean isToBeIncluded(IPath header) {

View file

@ -746,6 +746,7 @@ public class IncludeOrganizer {
if (!request.isResolved() && !isExportedBinding(request, headerSubstitutor)) {
List<IPath> candidatePaths = request.getCandidatePaths();
Set<IPath> representativeHeaders = new HashSet<IPath>();
Set<IPath> representedHeaders = new HashSet<IPath>();
boolean allRepresented = true;
for (IPath path : candidatePaths) {
if (fContext.isIncluded(path)) {
@ -759,6 +760,7 @@ public class IncludeOrganizer {
IPath header = headerSubstitutor.getUniqueRepresentativeHeader(path);
if (header != null) {
representativeHeaders.add(header);
representedHeaders.add(path);
} else {
allRepresented = false;
}
@ -772,6 +774,10 @@ public class IncludeOrganizer {
System.out.println(request.toString() + " (unique representative)"); //$NON-NLS-1$
if (!fContext.isAlreadyIncluded(path))
fContext.addHeaderToInclude(path);
for (IPath header : representedHeaders) {
if (!header.equals(path))
fContext.addHeaderAlreadyIncluded(header);
}
}
}
}
@ -799,6 +805,8 @@ public class IncludeOrganizer {
}
if (!fContext.isAlreadyIncluded(header))
fContext.addHeaderToInclude(header);
if (!header.equals(path))
fContext.addHeaderAlreadyIncluded(path);
}
}
}