From 820b901b693d283a8c2d94e06cde9fc4502691c2 Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Tue, 24 Jun 2014 12:10:47 -0700 Subject: [PATCH] Cosmetics. --- .../core/parser/tests/ast2/AST2CPPTests.java | 106 +++++++++--------- .../cdt/core/parser/util/HashTable.java | 41 +++---- .../core/dom/parser/cpp/CPPFunction.java | 6 +- .../dom/parser/cpp/CPPFunctionTemplate.java | 6 +- .../dom/parser/cpp/semantics/CPPVisitor.java | 31 ++--- 5 files changed, 92 insertions(+), 98 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java index fecbd68f63b..8c11b2bc41f 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java @@ -1120,7 +1120,7 @@ public class AST2CPPTests extends AST2TestBase { // X::f(); // X::g(); // } - public void testUsingDeclaration_1() throws Exception { + public void testUsingDeclaration() throws Exception { IASTTranslationUnit tu = parse(getAboveComment(), CPP); NameCollector collector = new NameCollector(); tu.accept(collector); @@ -1143,6 +1143,39 @@ public class AST2CPPTests extends AST2TestBase { assertInstances(collector, using_g.getDelegates()[0], 3); // decl + using-decl + ref } + // namespace A { + // void f(int); + // } + // using A::f; + // namespace A { + // void f(char); + // } + // void foo() { + // f('i'); + // } + // void bar() { + // using A::f; + // f('c'); + // } + public void testUsingDeclaration_86368() throws Exception { + BindingAssertionHelper bh = getAssertionHelper(); + IFunction f1= bh.assertNonProblem("f(int)", 1); + IFunction f2= bh.assertNonProblem("f('i')", 1); + assertSame(f1, f2); + IFunction g1= bh.assertNonProblem("f(char)", 1); + IFunction g2= bh.assertNonProblem("f('c')", 1); + assertSame(g1, g2); + + // Alternative binding resolution order. + bh = getAssertionHelper(); + f2= bh.assertNonProblem("f('i')", 1); + f1= bh.assertNonProblem("f(int)", 1); + assertSame(f1, f2); + g2= bh.assertNonProblem("f('c')", 1); + g1= bh.assertNonProblem("f(char)", 1); + assertSame(g1, g2); + } + // typedef int Int; // void f(int i); // void f(const int); @@ -6827,23 +6860,20 @@ public class AST2CPPTests extends AST2TestBase { assertInstance(((IFunctionType) s2.getType()).getParameterTypes()[0], IBasicType.class); } - // namespace A { - // class X { - // friend void f(int); - // class Y { - // friend void g(int); - // }; - // }; - // void test() { - // f(1); - // g(1); - // } - // } - public void testFriendFunctionResolution_86368_1() throws Exception { - final String code= getAboveComment(); - parseAndCheckBindings(code); - - BindingAssertionHelper bh= new BindingAssertionHelper(code, true); + // namespace A { + // class X { + // friend void f(int); + // class Y { + // friend void g(int); + // }; + // }; + // void test() { + // f(1); + // g(1); + // } + // } + public void testFriendFunctionResolution_86368() throws Exception { + BindingAssertionHelper bh = getAssertionHelper(); IFunction f1= bh.assertNonProblem("f(int)", 1); IFunction f2= bh.assertNonProblem("f(1)", 1); assertSame(f1, f2); @@ -6851,7 +6881,8 @@ public class AST2CPPTests extends AST2TestBase { IFunction g2= bh.assertNonProblem("g(1)", 1); assertSame(g1, g2); - bh= new BindingAssertionHelper(code, true); + // Alternative binding resolution order. + bh = getAssertionHelper(); f2= bh.assertNonProblem("f(1)", 1); f1= bh.assertNonProblem("f(int)", 1); assertSame(f1, f2); @@ -6860,41 +6891,6 @@ public class AST2CPPTests extends AST2TestBase { assertSame(g1, g2); } - // namespace A { - // void f(int); - // } - // using A::f; - // namespace A { - // void f(char); // openReferences fails - // } - // void foo() { - // f('i'); - // } - // void bar() { - // using A::f; - // f('c'); - // } - public void testFriendFunctionResolution_86368_2() throws Exception { - final String code= getAboveComment(); - parseAndCheckBindings(code); - - BindingAssertionHelper bh= new BindingAssertionHelper(code, true); - IFunction f1= bh.assertNonProblem("f(int)", 1); - IFunction f2= bh.assertNonProblem("f('i')", 1); - assertSame(f1, f2); - IFunction g1= bh.assertNonProblem("f(char)", 1); - IFunction g2= bh.assertNonProblem("f('c')", 1); - assertSame(g1, g2); - - bh= new BindingAssertionHelper(code, true); - f2= bh.assertNonProblem("f('i')", 1); - f1= bh.assertNonProblem("f(int)", 1); - assertSame(f1, f2); - g2= bh.assertNonProblem("f('c')", 1); - g1= bh.assertNonProblem("f(char)", 1); - assertSame(g1, g2); - } - // class A { // public: // void foo() const volatile; @@ -10646,7 +10642,6 @@ public class AST2CPPTests extends AST2TestBase { assertEquals(5, waldo.getInitialValue().numericalValue().longValue()); } - // constexpr int naive_fibonacci(int x) { // return x == 0 ? 0 // : x == 1 ? 1 @@ -10664,7 +10659,6 @@ public class AST2CPPTests extends AST2TestBase { assertNull(waldo.getInitialValue().numericalValue()); } - // constexpr int foo(int a = 42) { // return a; // } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/HashTable.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/HashTable.java index 7484ecee46f..796f7f74d4d 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/HashTable.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/HashTable.java @@ -17,19 +17,19 @@ import java.util.Comparator; */ public class HashTable implements Cloneable { protected static final int minHashSize = 2; + protected int currEntry = -1; + protected int[] hashTable; + protected int[] nextTable; public boolean isEmpty() { - return currEntry == -1; + return currEntry < 0; } public final int size() { return currEntry + 1; } - protected int[] hashTable; - protected int[] nextTable; - public HashTable(int initialSize) { int size = 1; while (size < initialSize) @@ -50,7 +50,7 @@ public class HashTable implements Cloneable { try { newTable = (HashTable) super.clone(); } catch (CloneNotSupportedException e) { - //shouldn't happen because object supports clone. + // Shouldn't happen because object supports clone. return null; } @@ -73,7 +73,7 @@ public class HashTable implements Cloneable { public void clear() { currEntry = -1; - // clear the table + // Clear the table. if (hashTable == null) return; @@ -83,27 +83,29 @@ public class HashTable implements Cloneable { nextTable[i] = 0; } } + protected void rehash() { if (nextTable == null) return; - // clear the table (don't call clear() or else the subclasses stuff will be cleared too) + // Clear the table (don't call clear() or else the subclasses stuff will be cleared too). for (int i = 0; i < capacity(); i++) { hashTable[2 * i] = 0; hashTable[2 * i + 1] = 0; nextTable[i] = 0; } - // Need to rehash everything + // Need to rehash everything. for (int i = 0; i <= currEntry; ++i) { linkIntoHashTable(i, hash(i)); } } + protected void resize(int size) { if (size > minHashSize) { hashTable = new int[size * 2]; nextTable = new int[size]; - // Need to rehash everything + // Need to rehash everything. for (int i = 0; i <= currEntry; ++i) { linkIntoHashTable(i, hash(i)); } @@ -111,7 +113,7 @@ public class HashTable implements Cloneable { } protected int hash(int pos) { - // return the hash value of the element in the key table + // Return the hash value of the element in the key table. throw new UnsupportedOperationException(); } @@ -122,7 +124,7 @@ public class HashTable implements Cloneable { if (hashTable[hash] == 0) { hashTable[hash] = i + 1; } else { - // need to link + // Need to link. int j = hashTable[hash] - 1; while (nextTable[j] != 0) { // if (nextTable[j] - 1 == j) { @@ -134,7 +136,7 @@ public class HashTable implements Cloneable { } } - final public int capacity() { + public final int capacity() { if (nextTable == null) return minHashSize; return nextTable.length; @@ -146,11 +148,11 @@ public class HashTable implements Cloneable { return; } - // Remove the hash entry + // Remove the hash entry. if (hashTable[hash] == i + 1) { hashTable[hash] = nextTable[i]; } else { - // find entry pointing to me + // Find entry pointing to me. int j = hashTable[hash] - 1; while (nextTable[j] != 0 && nextTable[j] != i + 1) j = nextTable[j] - 1; @@ -158,10 +160,10 @@ public class HashTable implements Cloneable { } if (i < currEntry) { - // shift everything over + // Shift everything over. System.arraycopy(nextTable, i + 1, nextTable, i, currEntry - i); - // adjust hash and next entries for things that moved + // Adjust hash and next entries for things that moved. for (int j = 0; j < hashTable.length; ++j) { if (hashTable[j] > i + 1) --hashTable[j]; @@ -173,18 +175,19 @@ public class HashTable implements Cloneable { } } - // last entry is now free + // Last entry is now free. nextTable[currEntry] = 0; --currEntry; } - final public void sort(Comparator c) { + public final void sort(Comparator c) { if (size() > 1) { quickSort(c, 0, size() - 1); rehash(); } } - final private void quickSort(Comparator c, int p, int r) { + + private void quickSort(Comparator c, int p, int r) { if (p < r) { int q = partition(c, p, r); if (p < q) quickSort(c, p, q); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunction.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunction.java index 5750bfd07cf..af24a0ee9b1 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunction.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunction.java @@ -61,7 +61,7 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil; import org.eclipse.core.runtime.PlatformObject; /** - * Binding for c++ function + * Binding for C++ function */ public class CPPFunction extends PlatformObject implements ICPPFunction, ICPPInternalFunction { public static final ICPPFunction UNINITIALIZED_FUNCTION = new CPPFunction(null); @@ -612,10 +612,6 @@ public class CPPFunction extends PlatformObject implements ICPPFunction, ICPPInt final ICPPParameter p = pars[i]; if (p.hasDefaultValue() || p.isParameterPack()) { result--; -// } else { -// if (pars.length == 1 && SemanticUtil.isVoidType(p.getType())) { -// return 0; -// } } } return result; diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunctionTemplate.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunctionTemplate.java index 5bbca8545d9..35cdc0089f1 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunctionTemplate.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunctionTemplate.java @@ -134,7 +134,7 @@ public class CPPFunctionTemplate extends CPPTemplateDefinition @Override public boolean hasParameterPack() { ICPPParameter[] pars= getParameters(); - return pars.length > 0 && pars[pars.length-1].isParameterPack(); + return pars.length > 0 && pars[pars.length - 1].isParameterPack(); } @Override @@ -205,7 +205,7 @@ public class CPPFunctionTemplate extends CPPTemplateDefinition int tdeclLen= decls == null ? 0 : decls.length; for (int i= -1; i < tdeclLen; i++) { IASTDeclarator tdecl; - if (i == -1) { + if (i < 0) { tdecl= getDeclaratorByName(getDefinition()); if (tdecl == null) continue; @@ -236,7 +236,7 @@ public class CPPFunctionTemplate extends CPPTemplateDefinition int tdeclLen= decls == null ? 0 : decls.length; for (int i= -1; i < tdeclLen && k < updateParams.length; i++) { IASTDeclarator tdecl; - if (i == -1) { + if (i < 0) { tdecl= getDeclaratorByName(getDefinition()); if (tdecl == null) continue; diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java index 37aaaa2a12b..3adc2734c23 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java @@ -708,27 +708,27 @@ public class CPPVisitor extends ASTQueries { IASTName name= declarator.getName().getLastName(); - // in case the binding was created starting from another name within the declarator. + // In case the binding was created starting from another name within the declarator. IBinding candidate= name.getBinding(); if (candidate != null) { return candidate; } - // function type + // Function type. if (parent instanceof IASTTypeId) return CPPSemantics.resolveBinding(name); - // function type for non-type template parameter + // Function type for non-type template parameter. ASTNodeProperty prop = parent.getPropertyInParent(); if (prop == ICPPASTTemplateDeclaration.PARAMETER || prop == ICPPASTTemplatedTypeTemplateParameter.PARAMETER) { return CPPTemplates.createBinding((ICPPASTTemplateParameter) parent); } - // explicit instantiations + // Explicit instantiations. if (prop == ICPPASTExplicitTemplateInstantiation.OWNED_DECLARATION) return CPPSemantics.resolveBinding(name); - // explicit specializations + // Explicit specializations. ICPPASTTemplateDeclaration tmplDecl= CPPTemplates.getTemplateDeclaration(name); if (tmplDecl instanceof ICPPASTTemplateSpecialization) { IBinding b= CPPSemantics.resolveBinding(name); @@ -740,13 +740,13 @@ public class CPPVisitor extends ASTQueries { return b; } - // parameter declarations + // Parameter declarations. if (parent instanceof ICPPASTParameterDeclaration) { ICPPASTParameterDeclaration param = (ICPPASTParameterDeclaration) parent; parent = param.getParent(); if (parent instanceof IASTStandardFunctionDeclarator) { IASTStandardFunctionDeclarator fdtor = (IASTStandardFunctionDeclarator) param.getParent(); - // Create parameter bindings only if the declarator declares a function + // Create parameter bindings only if the declarator declares a function. if (findTypeRelevantDeclarator(fdtor) != fdtor) return null; @@ -771,7 +771,7 @@ public class CPPVisitor extends ASTQueries { return new ProblemBinding(name, IProblemBinding.SEMANTIC_INVALID_TYPE); } - // function declaration/definition + // Function declaration/definition. IBinding binding= null; final boolean template= tmplDecl != null; boolean isFriendDecl= false; @@ -798,7 +798,7 @@ public class CPPVisitor extends ASTQueries { } else if (parent instanceof IASTSimpleDeclaration) { IASTSimpleDeclaration simpleDecl = (IASTSimpleDeclaration) parent; if (simpleDecl.getDeclSpecifier().getStorageClass() == IASTDeclSpecifier.sc_typedef) { - // Typedef declaration + // Typedef declaration. if (binding instanceof ICPPInternalBinding && binding instanceof ITypedef && name.isActive()) { IType t1 = ((ITypedef) binding).getType(); IType t2 = createType(declarator); @@ -808,26 +808,27 @@ public class CPPVisitor extends ASTQueries { } return new ProblemBinding(name, IProblemBinding.SEMANTIC_INVALID_REDECLARATION); } - // If we don't resolve the target type first, we get a problem binding in case the typedef - // redeclares the target type, otherwise it is safer to defer the resolution of the target type. + // If we don't resolve the target type first, we get a problem binding in case + // the typedef redeclares the target type, otherwise it is safer to defer + // the resolution of the target type. IType targetType= createType(declarator); CPPTypedef td= new CPPTypedef(name); td.setType(targetType); binding = td; } else if (typeRelevantDtor instanceof IASTFunctionDeclarator) { - // Function declaration via function declarator + // Function declaration via function declarator. isFunction= true; } else { - // Looks like a variable declaration + // Looks like a variable declaration. IType t1 = createType(declarator); if (SemanticUtil.getNestedType(t1, TDEF) instanceof IFunctionType) { // Function declaration via a typedef for a function type isFunction= true; } else if (binding instanceof IParameter) { - // Variable declaration redeclaring a parameter + // Variable declaration redeclaring a parameter. binding = new ProblemBinding(name, IProblemBinding.SEMANTIC_INVALID_REDECLARATION); } else { - // Variable declaration + // Variable declaration. IType t2= null; if (binding != null && binding instanceof IVariable && !(binding instanceof IIndexBinding)) { t2 = ((IVariable) binding).getType();