diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTTranslationUnit.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTTranslationUnit.java index baab11bdb0d..b7bc864054e 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTTranslationUnit.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTTranslationUnit.java @@ -6,8 +6,8 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Doug Schaefer (IBM) - Initial API and implementation - * Markus Schorn (Wind River Systems) + * Doug Schaefer (IBM) - Initial API and implementation + * Markus Schorn (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.core.dom.ast; @@ -30,7 +30,6 @@ import org.eclipse.core.runtime.IAdaptable; * @noimplement This interface is not intended to be implemented by clients. */ public interface IASTTranslationUnit extends IASTDeclarationListOwner, IFileNomination, IAdaptable { - /** * OWNED_DECLARATION represents the relationship between an IASTTranslationUnit and * it's nested IASTDeclaration's. @@ -107,7 +106,7 @@ public interface IASTTranslationUnit extends IASTDeclarationListOwner, IFileNomi /** * Returns the array of definitions in this translation unit for the given binding. * The array contains the IASTName nodes that define the binding. - * These are part of the AST no definitions are pulled in from the index. + * These are part of the AST, no definitions are pulled in from the index. * * @param binding * @return Array of IASTName nodes for the binding's declaration @@ -117,7 +116,7 @@ public interface IASTTranslationUnit extends IASTDeclarationListOwner, IFileNomi /** * Returns the list of references in this translation unit to the given * binding. This list contains the IASTName nodes that represent a use of - * the binding. These are part of the AST no definitions are pulled in from + * the binding. These are part of the AST, no definitions are pulled in from * the index. * * @param binding diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTQueries.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTQueries.java index eccb9bf1a2c..fe6042abc05 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTQueries.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTQueries.java @@ -6,7 +6,7 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Markus Schorn - initial API and implementation + * Markus Schorn - initial API and implementation *******************************************************************************/ package org.eclipse.cdt.internal.core.dom.parser; @@ -31,22 +31,27 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor; public class ASTQueries { private static class NameSearch extends ASTVisitor { private boolean fFound; + NameSearch() { super(false); shouldVisitAmbiguousNodes= true; shouldVisitNames= true; } + public void reset() { fFound= false; } + public boolean foundName() { return fFound; } + @Override public int visit(IASTName name) { fFound= true; return PROCESS_ABORT; } + @Override public int visit(ASTAmbiguousNode node) { IASTNode[] alternatives= node.getNodes(); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTFunctionDeclarator.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTFunctionDeclarator.java index 72e03dc67d1..2bf3dfffe82 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTFunctionDeclarator.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTFunctionDeclarator.java @@ -6,8 +6,8 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * John Camelon (IBM Rational Software) - Initial API and implementation - * Markus Schorn (Wind River Systems) + * John Camelon (IBM Rational Software) - Initial API and implementation + * Markus Schorn (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.core.dom.parser.c; @@ -24,15 +24,14 @@ import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.internal.core.dom.parser.ASTQueries; /** - * Models function declarators for plain c. + * A function declarator for plain C. */ public class CASTFunctionDeclarator extends CASTDeclarator implements IASTStandardFunctionDeclarator { - - private IASTParameterDeclaration [] parameters = null; - private int parametersPos=-1; + private IASTParameterDeclaration[] parameters; + private int parametersPos= -1; private boolean varArgs; private IScope scope; - + public CASTFunctionDeclarator() { } @@ -50,20 +49,22 @@ public class CASTFunctionDeclarator extends CASTDeclarator implements IASTStanda CASTFunctionDeclarator copy = new CASTFunctionDeclarator(); copyBaseDeclarator(copy, style); copy.varArgs = varArgs; - - for(IASTParameterDeclaration param : getParameters()) + + for (IASTParameterDeclaration param : getParameters()) { copy.addParameterDeclaration(param == null ? null : param.copy(style)); - + } + if (style == CopyStyle.withLocations) { copy.setCopyLocation(this); } return copy; } - + @Override public IASTParameterDeclaration[] getParameters() { - if( parameters == null ) return IASTParameterDeclaration.EMPTY_PARAMETERDECLARATION_ARRAY; - parameters = ArrayUtil.trimAt( IASTParameterDeclaration.class, parameters, parametersPos ); + if (parameters == null) + return IASTParameterDeclaration.EMPTY_PARAMETERDECLARATION_ARRAY; + parameters = ArrayUtil.trimAt(IASTParameterDeclaration.class, parameters, parametersPos); return parameters; } @@ -73,8 +74,8 @@ public class CASTFunctionDeclarator extends CASTDeclarator implements IASTStanda if (parameter != null) { parameter.setParent(this); parameter.setPropertyInParent(FUNCTION_PARAMETER); - parameters = ArrayUtil.appendAt( IASTParameterDeclaration.class, parameters, ++parametersPos, parameter ); - } + parameters = ArrayUtil.appendAt(IASTParameterDeclaration.class, parameters, ++parametersPos, parameter); + } } @Override @@ -100,7 +101,7 @@ public class CASTFunctionDeclarator extends CASTDeclarator implements IASTStanda @Override public void replace(IASTNode child, IASTNode other) { - if( parameters != null ) { + if (parameters != null) { for (int i = 0; i < parameters.length; ++i) { if (child == parameters[i]) { other.setPropertyInParent(child.getPropertyInParent()); @@ -112,22 +113,22 @@ public class CASTFunctionDeclarator extends CASTDeclarator implements IASTStanda } super.replace(child, other); } - + @Override public IScope getFunctionScope() { if (scope != null) return scope; - + // introduce a scope for function declarations and definitions, only. IASTNode node= getParent(); - while(!(node instanceof IASTDeclaration)) { + while (!(node instanceof IASTDeclaration)) { if (node==null) return null; node= node.getParent(); } if (node instanceof IASTParameterDeclaration) return null; - + if (node instanceof IASTFunctionDefinition) { scope= ((IASTFunctionDefinition) node).getScope(); } else if (ASTQueries.findTypeRelevantDeclarator(this) == this) { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionProposalComputer.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionProposalComputer.java index 969cabaeda9..e225d62efcf 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionProposalComputer.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionProposalComputer.java @@ -6,11 +6,11 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * QNX - Initial API and implementation - * Markus Schorn (Wind River Systems) - * Anton Leherbauer (Wind River Systems) - * Sergey Prigogin (Google) - * Jens Elmenthaler - http://bugs.eclipse.org/173458 (camel case completion) + * QNX - Initial API and implementation + * Markus Schorn (Wind River Systems) + * Anton Leherbauer (Wind River Systems) + * Sergey Prigogin (Google) + * Jens Elmenthaler - http://bugs.eclipse.org/173458 (camel case completion) *******************************************************************************/ package org.eclipse.cdt.internal.ui.text.contentassist; @@ -85,7 +85,7 @@ import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider; /** * Searches the DOM (both the AST and the index) for completion proposals. - * + * * @author Bryan Wilkinson */ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer { @@ -95,14 +95,13 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer */ public DOMCompletionProposalComputer() { } - + @Override protected List computeCompletionProposals( CContentAssistInvocationContext context, IASTCompletionNode completionNode, String prefix) { - List proposals = new ArrayList(); - + if (inPreprocessorDirective(context)) { if (!inPreprocessorKeyword(context)) { // add only macros @@ -123,7 +122,7 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer if (name.getTranslationUnit() == null) // The node isn't properly hooked up, must have backtracked out of this node continue; - + IASTCompletionContext astContext = name.getCompletionContext(); if (astContext == null) { continue; @@ -132,9 +131,9 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer // handle macros only if there is a prefix handleMacros = prefix.length() > 0; } - + IBinding[] bindings = astContext.findBindings(name, !context.isContextInformationStyle()); - + if (bindings != null) { AccessContext accessibilityContext = new AccessContext(name); for (IBinding binding : bindings) { @@ -147,20 +146,20 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer if (handleMacros) addMacroProposals(context, prefix, proposals); } - + return proposals; } /** * Test whether the invocation offset is inside or before the preprocessor directive keyword. - * + * * @param context the invocation context - * @return true if the invocation offset is inside or before the directive keyword + * @return true if the invocation offset is inside or before the directive keyword */ private boolean inPreprocessorKeyword(CContentAssistInvocationContext context) { IDocument doc = context.getDocument(); int offset = context.getInvocationOffset(); - + try { final ITypedRegion partition= TextUtilities.getPartition(doc, ICPartitions.C_PARTITIONING, offset, true); @@ -171,7 +170,7 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer return true; } } - + } catch (BadLocationException exc) { } return false; @@ -179,21 +178,21 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer /** * Check if the invocation offset is inside a preprocessor directive. - * + * * @param context the content asist invocation context * @return true if invocation offset is inside a preprocessor directive */ private boolean inPreprocessorDirective(CContentAssistInvocationContext context) { IDocument doc = context.getDocument(); int offset = context.getInvocationOffset(); - + try { final ITypedRegion partition= TextUtilities.getPartition(doc, ICPartitions.C_PARTITIONING, offset, true); if (ICPartitions.C_PREPROCESSOR.equals(partition.getType())) { return true; } - + } catch (BadLocationException exc) { } return false; @@ -202,10 +201,10 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer private void addMacroProposals(CContentAssistInvocationContext context, String prefix, List proposals) { IASTCompletionNode completionNode = context.getCompletionNode(); - addMacroProposals(context, prefix, proposals, completionNode.getTranslationUnit() - .getMacroDefinitions()); - addMacroProposals(context, prefix, proposals, completionNode.getTranslationUnit() - .getBuiltinMacroDefinitions()); + addMacroProposals(context, prefix, proposals, + completionNode.getTranslationUnit().getMacroDefinitions()); + addMacroProposals(context, prefix, proposals, + completionNode.getTranslationUnit().getBuiltinMacroDefinitions()); } private void addMacroProposals(CContentAssistInvocationContext context, String prefix, @@ -231,22 +230,22 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer } } } - + private void handleMacro(IASTPreprocessorMacroDefinition macro, CContentAssistInvocationContext context, String prefix, List proposals) { final String macroName = macro.getName().toString(); final int baseRelevance= computeBaseRelevance(prefix, macroName); Image image = getImage(CElementImageProvider.getMacroImageDescriptor()); - + if (macro instanceof IASTPreprocessorFunctionStyleMacroDefinition) { IASTPreprocessorFunctionStyleMacroDefinition functionMacro = (IASTPreprocessorFunctionStyleMacroDefinition) macro; - + StringBuilder repStringBuff = new StringBuilder(); repStringBuff.append(macroName); repStringBuff.append('('); - + StringBuilder args = new StringBuilder(); IASTFunctionStyleMacroParameter[] params = functionMacro.getParameters(); @@ -258,15 +257,15 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer } } String argString = args.toString(); - + StringBuilder descStringBuff = new StringBuilder(repStringBuff.toString()); descStringBuff.append(argString); descStringBuff.append(')'); - + repStringBuff.append(')'); String repString = repStringBuff.toString(); String descString = descStringBuff.toString(); - + CCompletionProposal proposal = createProposal(repString, descString, prefix.length(), image, baseRelevance + RelevanceConstants.MACRO_TYPE_RELEVANCE, context); if (!context.isContextInformationStyle()) { @@ -276,13 +275,13 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer proposal.setCursorPosition(repString.length()); } } - + if (argString.length() > 0) { CProposalContextInformation info = new CProposalContextInformation(image, descString, argString); info.setContextInformationPosition(context.getContextInformationOffset()); proposal.setContextInformation(info); } - + proposals.add(proposal); } else { proposals.add(createProposal(macroName, macroName, prefix.length(), image, @@ -290,7 +289,7 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer } } - protected void handleBinding(IBinding binding, CContentAssistInvocationContext cContext, String prefix, + protected void handleBinding(IBinding binding, CContentAssistInvocationContext cContext, String prefix, IASTCompletionContext astContext, List proposals) { if ((binding instanceof CPPImplicitFunction || binding instanceof CPPImplicitTypedef @@ -332,7 +331,7 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer } } } - + private boolean isAnonymousBinding(IBinding binding) { char[] name= binding.getNameCharArray(); return name.length == 0 || name[0] == '{'; @@ -369,15 +368,15 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer baseRelevance + RelevanceConstants.CLASS_TYPE_RELEVANCE, context)); } } - + private void handleFunction(IFunction function, CContentAssistInvocationContext context, - int baseRelevance, List proposals) { + int baseRelevance, List proposals) { Image image = getImage(function); - + StringBuilder repStringBuff = new StringBuilder(); repStringBuff.append(function.getName()); repStringBuff.append('('); - + StringBuilder dispargs = new StringBuilder(); // for the dispargString StringBuilder idargs = new StringBuilder(); // for the idargString boolean hasArgs = true; @@ -399,7 +398,7 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer dispargs.append(paramName); } } - + if (function.takesVarArgs()) { if (params.length > 0) { dispargs.append(','); @@ -420,7 +419,7 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer } hasArgs = ASTTypeUtil.functionTakesParameters(function); - + String dispargString = dispargs.toString(); String idargString = idargs.toString(); String contextDispargString = hasArgs ? dispargString : null; @@ -437,7 +436,7 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer idStringBuff.append(idargString); idStringBuff.append(')'); String idString = idStringBuff.toString(); - + repStringBuff.append(')'); String repString = repStringBuff.toString(); @@ -449,17 +448,17 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer int cursorPosition = hasArgs ? (repString.length() - 1) : repString.length(); proposal.setCursorPosition(cursorPosition); } - + if (contextDispargString != null) { CProposalContextInformation info = new CProposalContextInformation(image, dispString, contextDispargString); info.setContextInformationPosition(context.getContextInformationOffset()); proposal.setContextInformation(info); } - + proposals.add(proposal); } - + private void handleVariable(IVariable variable, CContentAssistInvocationContext context, int baseRelevance, List proposals) { if (context.isContextInformationStyle()) { @@ -473,16 +472,16 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer } } return; - } + } StringBuilder repStringBuff = new StringBuilder(); repStringBuff.append(variable.getName()); - + String returnTypeStr = ""; //$NON-NLS-1$ IType varType = variable.getType(); if (varType != null) returnTypeStr = ASTTypeUtil.getType(varType, false); - + StringBuilder dispStringBuff = new StringBuilder(repStringBuff.toString()); if (returnTypeStr != null) { dispStringBuff.append(" : "); //$NON-NLS-1$ @@ -492,24 +491,25 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer StringBuilder idStringBuff = new StringBuilder(repStringBuff.toString()); String idString = idStringBuff.toString(); - + String repString = repStringBuff.toString(); Image image = getImage(variable); - final int relevance = isLocalVariable(variable) - ? RelevanceConstants.LOCAL_VARIABLE_TYPE_RELEVANCE - : isField(variable) - ? RelevanceConstants.FIELD_TYPE_RELEVANCE - : RelevanceConstants.VARIABLE_TYPE_RELEVANCE; + final int relevance = isLocalVariable(variable) ? + RelevanceConstants.LOCAL_VARIABLE_TYPE_RELEVANCE : + isField(variable) ? + RelevanceConstants.FIELD_TYPE_RELEVANCE : + RelevanceConstants.VARIABLE_TYPE_RELEVANCE; CCompletionProposal proposal = createProposal(repString, dispString, idString, context.getCompletionNode().getLength(), image, baseRelevance + relevance, context); proposals.add(proposal); } - + private IType unwindTypedefs(final IType t) { IType r= t; - while (r instanceof ITypedef) + while (r instanceof ITypedef) { r= ((ITypedef) r).getType(); + } return r != null ? r : t; } @@ -541,38 +541,35 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer return false; } - private void handleNamespace(ICPPNamespace namespace, - IASTCompletionContext astContext, - CContentAssistInvocationContext cContext, - int baseRelevance, + private void handleNamespace(ICPPNamespace namespace, IASTCompletionContext astContext, + CContentAssistInvocationContext cContext, int baseRelevance, List proposals) { - if (astContext instanceof ICPPASTQualifiedName) { IASTCompletionContext parent = ((ICPPASTQualifiedName) astContext) .getCompletionContext(); handleNamespace(namespace, parent, cContext, baseRelevance, proposals); return; } - + StringBuilder repStringBuff = new StringBuilder(); repStringBuff.append(namespace.getName()); - + if (!(astContext instanceof ICPPASTUsingDeclaration) && !(astContext instanceof ICPPASTUsingDirective)) { repStringBuff.append("::"); //$NON-NLS-1$ } - + String repString = repStringBuff.toString(); proposals.add(createProposal(repString, namespace.getName(), getImage(namespace), baseRelevance + RelevanceConstants.NAMESPACE_TYPE_RELEVANCE, cContext)); } - + private CCompletionProposal createProposal(String repString, String dispString, Image image, int relevance, CContentAssistInvocationContext context) { return createProposal(repString, dispString, null, context.getCompletionNode().getLength(), image, relevance, context); } - + private CCompletionProposal createProposal(String repString, String dispString, int prefixLength, Image image, int relevance, CContentAssistInvocationContext context) { return createProposal(repString, dispString, null, prefixLength, image, relevance, context); @@ -583,11 +580,11 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer int parseOffset = context.getParseOffset(); int invocationOffset = context.getInvocationOffset(); boolean doReplacement = !context.isContextInformationStyle(); - + int repLength = doReplacement ? prefixLength : 0; int repOffset = doReplacement ? parseOffset - repLength : invocationOffset; repString = doReplacement ? repString : ""; //$NON-NLS-1$ - + return new CCompletionProposal(repString, repOffset, repLength, image, dispString, idString, relevance, context.getViewer()); } @@ -595,10 +592,10 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer private Image getImage(ImageDescriptor desc) { return desc != null ? CUIPlugin.getImageDescriptorRegistry().get(desc) : null; } - + private Image getImage(IBinding binding) { ImageDescriptor imageDescriptor = null; - + if (binding instanceof ITypedef) { imageDescriptor = CElementImageProvider.getTypedefImageDescriptor(); } else if (binding instanceof ICompositeType) { @@ -651,9 +648,8 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer if (delegates.length > 0) return getImage(delegates[0]); } - - return imageDescriptor != null - ? CUIPlugin.getImageDescriptorRegistry().get(imageDescriptor) - : null; + + return imageDescriptor != null ? + CUIPlugin.getImageDescriptorRegistry().get(imageDescriptor) : null; } }