1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +02:00

Bug 506668 - Name resolution problem with namespace alias in macro

expansion

Change-Id: I15a0198c588cbb0e044392f47367679bb90180a6
This commit is contained in:
Sergey Prigogin 2016-10-27 20:14:27 -07:00
parent 64bd99d429
commit d69b51acc8
2 changed files with 26 additions and 8 deletions

View file

@ -30,8 +30,6 @@ import java.io.StringReader;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import junit.framework.TestSuite;
import org.eclipse.cdt.core.dom.IName; import org.eclipse.cdt.core.dom.IName;
import org.eclipse.cdt.core.dom.ast.ASTTypeUtil; import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
import org.eclipse.cdt.core.dom.ast.EScopeKind; import org.eclipse.cdt.core.dom.ast.EScopeKind;
@ -157,6 +155,8 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.cdt.internal.core.index.IndexCPPSignatureUtil; import org.eclipse.cdt.internal.core.index.IndexCPPSignatureUtil;
import org.eclipse.cdt.internal.core.parser.ParserException; import org.eclipse.cdt.internal.core.parser.ParserException;
import junit.framework.TestSuite;
public class AST2CPPTests extends AST2TestBase { public class AST2CPPTests extends AST2TestBase {
public AST2CPPTests() { public AST2CPPTests() {
@ -3742,6 +3742,17 @@ public class AST2CPPTests extends AST2TestBase {
assertTrue(alias.isGloballyQualified()); assertTrue(alias.isGloballyQualified());
} }
// namespace ns { struct A {}; }
//
// #define MACRO []() { namespace waldo = ::ns; waldo::A x; return x; }()
//
// void test() {
// MACRO;
// }
public void testNamespaceAliasInMacroExpansion_506668() throws Exception {
parseAndCheckBindings();
}
// class A{}; // class A{};
// class B : public A { // class B : public A {
// B () : A() {} // B () : A() {}

View file

@ -1930,6 +1930,7 @@ public class CPPSemantics {
} }
int pointOfDecl= -1; int pointOfDecl= -1;
boolean pointOfDeclIsEndOffset = false;
if (obj instanceof ICPPInternalBinding) { if (obj instanceof ICPPInternalBinding) {
ICPPInternalBinding cpp = (ICPPInternalBinding) obj; ICPPInternalBinding cpp = (ICPPInternalBinding) obj;
IASTNode[] n = cpp.getDeclarations(); IASTNode[] n = cpp.getDeclarations();
@ -1937,8 +1938,7 @@ public class CPPSemantics {
nd = (ASTNode) n[0]; nd = (ASTNode) n[0];
} }
ASTNode def = (ASTNode) cpp.getDefinition(); ASTNode def = (ASTNode) cpp.getDefinition();
if (def != null) { if (def != null && (nd == null || def.getOffset() < nd.getOffset())) {
if (nd == null || def.getOffset() < nd.getOffset())
nd = def; nd = def;
} }
if (nd == null) if (nd == null)
@ -1974,10 +1974,12 @@ public class CPPSemantics {
// because function parameter declarations implement ICPPASTTemplateParameter too. // because function parameter declarations implement ICPPASTTemplateParameter too.
boolean isTemplateParameter = dtor.getParent() instanceof ICPPASTTemplateParameter boolean isTemplateParameter = dtor.getParent() instanceof ICPPASTTemplateParameter
&& dtor.getParent().getPropertyInParent() == ICPPASTTemplateDeclaration.PARAMETER; && dtor.getParent().getPropertyInParent() == ICPPASTTemplateDeclaration.PARAMETER;
if (init != null && !isTemplateParameter) if (init != null && !isTemplateParameter) {
pointOfDecl = ((ASTNode) init).getOffset() - 1; pointOfDecl = ((ASTNode) init).getOffset() - 1;
else } else {
pointOfDecl = ((ASTNode) dtor).getOffset() + ((ASTNode) dtor).getLength(); pointOfDecl = ((ASTNode) dtor).getOffset() + ((ASTNode) dtor).getLength();
pointOfDeclIsEndOffset = true;
}
} else if (prop == IASTEnumerator.ENUMERATOR_NAME) { } else if (prop == IASTEnumerator.ENUMERATOR_NAME) {
// Point of declaration for an enumerator is immediately after it // Point of declaration for an enumerator is immediately after it
// enumerator-definition // enumerator-definition
@ -1988,17 +1990,20 @@ public class CPPSemantics {
} else { } else {
pointOfDecl = nd.getOffset() + nd.getLength(); pointOfDecl = nd.getOffset() + nd.getLength();
} }
pointOfDeclIsEndOffset = true;
} else if (prop == ICPPASTUsingDeclaration.NAME) { } else if (prop == ICPPASTUsingDeclaration.NAME) {
nd = (ASTNode) nd.getParent(); nd = (ASTNode) nd.getParent();
pointOfDecl = nd.getOffset(); pointOfDecl = nd.getOffset();
} else if (prop == ICPPASTNamespaceAlias.ALIAS_NAME) { } else if (prop == ICPPASTNamespaceAlias.ALIAS_NAME) {
nd = (ASTNode) nd.getParent(); nd = (ASTNode) nd.getParent();
pointOfDecl = nd.getOffset() + nd.getLength(); pointOfDecl = nd.getOffset() + nd.getLength();
pointOfDeclIsEndOffset = true;
} else if (prop == ICPPASTAliasDeclaration.ALIAS_NAME) { } else if (prop == ICPPASTAliasDeclaration.ALIAS_NAME) {
// [basic.scope.pdecl]/p3: The point of declaration of an alias or alias template // [basic.scope.pdecl]/p3: The point of declaration of an alias or alias template
// immediately follows the type-id to which the alias refers. // immediately follows the type-id to which the alias refers.
ASTNode targetType = (ASTNode) ((ICPPASTAliasDeclaration) nd.getParent()).getMappingTypeId(); ASTNode targetType = (ASTNode) ((ICPPASTAliasDeclaration) nd.getParent()).getMappingTypeId();
pointOfDecl = targetType.getOffset() + targetType.getLength(); pointOfDecl = targetType.getOffset() + targetType.getLength();
pointOfDeclIsEndOffset = true;
} else if (prop == ICPPASTSimpleTypeTemplateParameter.PARAMETER_NAME } else if (prop == ICPPASTSimpleTypeTemplateParameter.PARAMETER_NAME
|| prop == ICPPASTTemplatedTypeTemplateParameter.PARAMETER_NAME) { || prop == ICPPASTTemplatedTypeTemplateParameter.PARAMETER_NAME) {
// [basic.scope.pdecl]/p9: The point of declaration for a template parameter // [basic.scope.pdecl]/p9: The point of declaration for a template parameter
@ -2008,11 +2013,13 @@ public class CPPSemantics {
// case above. // case above.
nd = (ASTNode) nd.getParent(); nd = (ASTNode) nd.getParent();
pointOfDecl = nd.getOffset() + nd.getLength(); pointOfDecl = nd.getOffset() + nd.getLength();
pointOfDeclIsEndOffset = true;
} else { } else {
pointOfDecl = nd.getOffset() + nd.getLength(); pointOfDecl = nd.getOffset() + nd.getLength();
pointOfDeclIsEndOffset = true;
} }
} }
return (pointOfDecl < pointOfRef); return pointOfDecl < pointOfRef || (pointOfDecl == pointOfRef && pointOfDeclIsEndOffset);
} }
private static boolean acceptDeclaredAfter(ICPPInternalBinding cpp) { private static boolean acceptDeclaredAfter(ICPPInternalBinding cpp) {