From 01c4750bab227191b69dd6723caf8f462e1a9dc9 Mon Sep 17 00:00:00 2001 From: John Camelon Date: Tue, 5 Apr 2005 17:57:32 +0000 Subject: [PATCH] Fixed 88802 - [DOM AST] IASTTranslationUnit needs a method to return include directives in order in which they were encountered --- .../tests/ast2/DOMLocationInclusionTests.java | 5 +++ .../core/parser/scanner2/DependencyTree.java | 40 +++++++++++++++++++ .../parser/scanner2/IDependencyNodeHost.java | 19 +++++++++ .../core/parser/scanner2/InclusionNode.java | 40 +++++++++++++++++++ .../core/parser/scanner2/LocationMap.java | 25 +++++++++++- 5 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/DependencyTree.java create mode 100644 core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/IDependencyNodeHost.java create mode 100644 core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/InclusionNode.java diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationInclusionTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationInclusionTests.java index 79445ed51e9..4952e97c4e0 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationInclusionTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationInclusionTests.java @@ -155,6 +155,7 @@ public class DOMLocationInclusionTests extends FileBasePluginTest { assertSoleFileLocation( incs[0], "code.cpp", code.indexOf("#inc"), code.indexOf(".h\"\n") + ".h\"".length() - code.indexOf("#inc")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ + } } @@ -254,6 +255,10 @@ public class DOMLocationInclusionTests extends FileBasePluginTest { "source.c", cpp_code.indexOf("#include \"header2.h\""), "#include \"header2.h\"".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ assertSoleFileLocation(declarations[2], "source.c", cpp_code.indexOf("int z;"), "int z;".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + + IASTTranslationUnit.IDependencyTree tree = tu.getDependencyTree(); + assertEquals( tree.getInclusions().length, 2 ); + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/DependencyTree.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/DependencyTree.java new file mode 100644 index 00000000000..3ed10b5db03 --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/DependencyTree.java @@ -0,0 +1,40 @@ +/********************************************************************** + * Copyright (c) 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM - Initial API and implementation + **********************************************************************/ +package org.eclipse.cdt.internal.core.parser.scanner2; + +import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; +import org.eclipse.cdt.core.parser.util.ArrayUtil; + +public class DependencyTree implements IASTTranslationUnit.IDependencyTree, IDependencyNodeHost { + + private final String tu_path; + + public DependencyTree( String path ) + { + tu_path = path; + } + + public String getTranslationUnitPath() { + return tu_path; + } + + private IASTInclusionNode [] incs = new IASTInclusionNode[2]; + + public IASTInclusionNode[] getInclusions() { + incs = (IASTInclusionNode[]) ArrayUtil.removeNulls( IASTInclusionNode.class, incs ); + return incs; + } + + public void addInclusionNode(IASTInclusionNode node) { + incs = (IASTInclusionNode[]) ArrayUtil.append( IASTInclusionNode.class, incs, node ); + } + +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/IDependencyNodeHost.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/IDependencyNodeHost.java new file mode 100644 index 00000000000..41040ac063a --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/IDependencyNodeHost.java @@ -0,0 +1,19 @@ +/********************************************************************** + * Copyright (c) 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM - Initial API and implementation + **********************************************************************/ +package org.eclipse.cdt.internal.core.parser.scanner2; + +import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit.IDependencyTree.IASTInclusionNode; + +public interface IDependencyNodeHost { + + public void addInclusionNode( IASTInclusionNode node ); + +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/InclusionNode.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/InclusionNode.java new file mode 100644 index 00000000000..d066e67bb26 --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/InclusionNode.java @@ -0,0 +1,40 @@ +/********************************************************************** + * Copyright (c) 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM - Initial API and implementation + **********************************************************************/ +package org.eclipse.cdt.internal.core.parser.scanner2; + +import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement; +import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit.IDependencyTree.IASTInclusionNode; +import org.eclipse.cdt.core.parser.util.ArrayUtil; + +public class InclusionNode implements IASTInclusionNode, IDependencyNodeHost { + + private final IASTPreprocessorIncludeStatement stmt; + + public InclusionNode(IASTPreprocessorIncludeStatement stmt) { + this.stmt = stmt; + } + + public IASTPreprocessorIncludeStatement getIncludeDirective() { + return stmt; + } + + private IASTInclusionNode [] incs = new IASTInclusionNode[2]; + + public IASTInclusionNode[] getNestedInclusions() { + incs = (IASTInclusionNode[]) ArrayUtil.removeNulls( IASTInclusionNode.class, incs ); + return incs; + } + + public void addInclusionNode(IASTInclusionNode node) { + incs = (IASTInclusionNode[]) ArrayUtil.append( IASTInclusionNode.class, incs, node ); + } + +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java index 2a24815d5a8..555ded7fa52 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java @@ -37,6 +37,7 @@ import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IMacroBinding; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit.IDependencyTree; +import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit.IDependencyTree.IASTInclusionNode; import org.eclipse.cdt.core.parser.CodeReader; import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.CharArrayUtils; @@ -2363,8 +2364,28 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { } public IDependencyTree getDependencyTree() { - // TODO Auto-generated method stub - return null; + DependencyTree result = new DependencyTree(getTranslationUnitPath()); + buildDependencyTree( result, tu ); + return result; + } + + protected void buildDependencyTree(IDependencyNodeHost result, _CompositeFileContext context) { + _Context [] subs = context.getSubContexts(); + for( int i = 0; i < subs.length; ++i ) + { + if( subs[i] instanceof _Inclusion ) + { + IASTTranslationUnit.IDependencyTree.IASTInclusionNode node = createDepTreeNode( (_Inclusion)subs[i] ); + result.addInclusionNode( node ); + } + } + } + + private IASTInclusionNode createDepTreeNode(_Inclusion inclusion) { + IASTPreprocessorIncludeStatement stmt = createASTInclusion( inclusion ); + InclusionNode node = new InclusionNode( stmt ); + buildDependencyTree(node, inclusion); + return node; } public IMacroDefinition registerBuiltinObjectStyleMacro(ObjectStyleMacro macro) {