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

Bug 534330 - Use the full path of the file when inventing names for anonymous types

Otherwise we can get clashes if two anonymous types happen to be
at the same offset in files with the same name but different paths.

Change-Id: Ia269a7c6fa1dc7e37d23d9333b245143d7c33e5d
This commit is contained in:
Nathan Ridge 2018-05-04 00:49:06 -04:00
parent cc23d88b54
commit 40133cceb4
2 changed files with 47 additions and 21 deletions

View file

@ -42,6 +42,8 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPQualifierType;
import org.eclipse.cdt.core.index.IIndexBinding;
import org.eclipse.cdt.core.index.IIndexFile;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.parser.GCCKeywords;
import org.eclipse.cdt.core.parser.Keywords;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
@ -60,7 +62,9 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPSemantics;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.TypeOfDependentExpression;
import org.eclipse.cdt.internal.core.resources.ResourceLookup;
import org.eclipse.cdt.utils.UNCPathConverter;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
@ -997,10 +1001,9 @@ public class ASTTypeUtil {
}
}
if (loc != null) {
char[] fname= loc.getFileName().toCharArray();
int fnamestart= findFileNameStart(fname);
char[] file = getWorkspaceRelativePath(loc.getFileName(), node).toCharArray();
buf.append('{');
buf.append(fname, fnamestart, fname.length - fnamestart);
buf.append(file);
if (includeOffset) {
buf.append(':');
buf.append(loc.getNodeOffset());
@ -1009,15 +1012,30 @@ public class ASTTypeUtil {
}
}
private static int findFileNameStart(char[] fname) {
for (int i= fname.length - 2; i >= 0; i--) {
switch (fname[i]) {
case '/':
case '\\':
return i+1;
/**
* Try to get a workspace-relative path for a filename.
* If that fails, just return the input path.
*/
private static String getWorkspaceRelativePath(String filename, IASTNode context) {
if (context == null) {
return filename;
}
IASTTranslationUnit ast = context.getTranslationUnit();
if (ast == null) {
return filename;
}
return 0;
ITranslationUnit tu = ast.getOriginatingTranslationUnit();
if (tu == null) {
return filename;
}
ICProject cproject = tu.getCProject();
if (cproject == null) {
return filename;
}
IPath path = new Path(filename);
IFile file = ResourceLookup.selectFileForLocation(path, cproject.getProject());
IPath workspaceRelative = file.getFullPath();
return workspaceRelative.toString();
}
/**

View file

@ -216,6 +216,8 @@ public class BasicCallHierarchyTest extends CallHierarchyBaseTest {
waitUntilFileIsIndexed(fIndex, file);
CEditor editor = openEditor(file);
String pathPrefix = "/" + fCProject.getElementName() + "/";
editor.selectAndReveal(content.indexOf("mem1"), 0);
openCallHierarchy(editor);
Tree tree = getCHTreeViewer().getTree();
@ -233,7 +235,7 @@ public class BasicCallHierarchyTest extends CallHierarchyBaseTest {
editor.selectAndReveal(content.indexOf("mem4"), 0);
openCallHierarchy(editor);
checkTreeNode(tree, 0, "s4::mem4 : {struct_member.c:129}");
checkTreeNode(tree, 0, "s4::mem4 : {" + pathPrefix + "struct_member.c:129}");
checkTreeNode(tree, 0, 0, "main() : void");
editor.selectAndReveal(content.indexOf("mem5"), 0);
@ -252,7 +254,7 @@ public class BasicCallHierarchyTest extends CallHierarchyBaseTest {
editor.selectAndReveal(content.indexOf("mem4."), 0);
openCallHierarchy(editor);
checkTreeNode(tree, 0, "s4::mem4 : {struct_member.c:129}");
checkTreeNode(tree, 0, "s4::mem4 : {" + pathPrefix + "struct_member.c:129}");
checkTreeNode(tree, 0, 0, "main() : void");
}
@ -262,6 +264,8 @@ public class BasicCallHierarchyTest extends CallHierarchyBaseTest {
waitUntilFileIsIndexed(fIndex, file);
CEditor editor = openEditor(file);
String pathPrefix = "/" + fCProject.getElementName() + "/";
editor.selectAndReveal(content.indexOf("mem1"), 0);
openCallHierarchy(editor);
Tree tree = getCHTreeViewer().getTree();
@ -279,7 +283,7 @@ public class BasicCallHierarchyTest extends CallHierarchyBaseTest {
editor.selectAndReveal(content.indexOf("mem4"), 0);
openCallHierarchy(editor);
checkTreeNode(tree, 0, "s4::mem4 : s4::{struct_member.cpp:129}");
checkTreeNode(tree, 0, "s4::mem4 : s4::{" + pathPrefix + "struct_member.cpp:129}");
checkTreeNode(tree, 0, 0, "main() : void");
editor.selectAndReveal(content.indexOf("mem5"), 0);
@ -298,7 +302,7 @@ public class BasicCallHierarchyTest extends CallHierarchyBaseTest {
editor.selectAndReveal(content.indexOf("mem4."), 0);
openCallHierarchy(editor);
checkTreeNode(tree, 0, "s4::mem4 : s4::{struct_member.cpp:129}");
checkTreeNode(tree, 0, "s4::mem4 : s4::{" + pathPrefix + "struct_member.cpp:129}");
checkTreeNode(tree, 0, 0, "main() : void");
}
@ -398,6 +402,8 @@ public class BasicCallHierarchyTest extends CallHierarchyBaseTest {
waitUntilFileIsIndexed(fIndex, file);
CEditor editor = openEditor(file);
String pathPrefix = "/" + fCProject.getElementName() + "/";
editor.selectAndReveal(content.indexOf("mem1"), 0);
openCallHierarchy(editor);
Tree tree = getCHTreeViewer().getTree();
@ -415,7 +421,7 @@ public class BasicCallHierarchyTest extends CallHierarchyBaseTest {
editor.selectAndReveal(content.indexOf("mem4"), 0);
openCallHierarchy(editor);
checkTreeNode(tree, 0, "u4::mem4 : {union_member.c:161}");
checkTreeNode(tree, 0, "u4::mem4 : {" + pathPrefix + "union_member.c:161}");
checkTreeNode(tree, 0, 0, "main() : void");
editor.selectAndReveal(content.indexOf("mem5"), 0);
@ -434,7 +440,7 @@ public class BasicCallHierarchyTest extends CallHierarchyBaseTest {
editor.selectAndReveal(content.indexOf("mem4."), 0);
openCallHierarchy(editor);
checkTreeNode(tree, 0, "u4::mem4 : {union_member.c:161}");
checkTreeNode(tree, 0, "u4::mem4 : {" + pathPrefix + "union_member.c:161}");
checkTreeNode(tree, 0, 0, "main() : void");
}
@ -444,6 +450,8 @@ public class BasicCallHierarchyTest extends CallHierarchyBaseTest {
waitUntilFileIsIndexed(fIndex, file);
CEditor editor = openEditor(file);
String pathPrefix = "/" + fCProject.getElementName() + "/";
editor.selectAndReveal(content.indexOf("mem1"), 0);
openCallHierarchy(editor);
Tree tree = getCHTreeViewer().getTree();
@ -461,7 +469,7 @@ public class BasicCallHierarchyTest extends CallHierarchyBaseTest {
editor.selectAndReveal(content.indexOf("mem4"), 0);
openCallHierarchy(editor);
checkTreeNode(tree, 0, "u4::mem4 : u4::{union_member.cpp:161}");
checkTreeNode(tree, 0, "u4::mem4 : u4::{" + pathPrefix + "union_member.cpp:161}");
checkTreeNode(tree, 0, 0, "main() : void");
editor.selectAndReveal(content.indexOf("mem5"), 0);
@ -480,7 +488,7 @@ public class BasicCallHierarchyTest extends CallHierarchyBaseTest {
editor.selectAndReveal(content.indexOf("mem4."), 0);
openCallHierarchy(editor);
checkTreeNode(tree, 0, "u4::mem4 : u4::{union_member.cpp:161}");
checkTreeNode(tree, 0, "u4::mem4 : u4::{" + pathPrefix + "union_member.cpp:161}");
checkTreeNode(tree, 0, 0, "main() : void");
}