From 40133cceb4616d0eee2f8d68f15d4faa3c04db8f Mon Sep 17 00:00:00 2001 From: Nathan Ridge Date: Fri, 4 May 2018 00:49:06 -0400 Subject: [PATCH] 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 --- .../eclipse/cdt/core/dom/ast/ASTTypeUtil.java | 42 +++++++++++++------ .../callhierarchy/BasicCallHierarchyTest.java | 26 ++++++++---- 2 files changed, 47 insertions(+), 21 deletions(-) diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTTypeUtil.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTTypeUtil.java index c508ecf7ea2..b256f7aab65 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTTypeUtil.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTTypeUtil.java @@ -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,17 +1012,32 @@ 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; } - return 0; + IASTTranslationUnit ast = context.getTranslationUnit(); + if (ast == null) { + return filename; + } + 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(); } - + /** * @noreference This method is not intended to be referenced by clients. * @deprecated This method is no longer used and is scheduled for removal in 10.0. diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/callhierarchy/BasicCallHierarchyTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/callhierarchy/BasicCallHierarchyTest.java index 3de4f61be97..a27221a9464 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/callhierarchy/BasicCallHierarchyTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/callhierarchy/BasicCallHierarchyTest.java @@ -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"); } @@ -261,7 +263,9 @@ public class BasicCallHierarchyTest extends CallHierarchyBaseTest { IFile file= createFile(getProject(), "struct_member.cpp", content); 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"); }