Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 347000 - error of "Open declaration" : find declaration of function goes wrong
Summary: error of "Open declaration" : find declaration of function goes wrong
Status: RESOLVED INVALID
Alias: None
Product: CDT
Classification: Tools
Component: cdt-core (show other bugs)
Version: 7.0.2   Edit
Hardware: PC Windows XP
: P3 major (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact: Doug Schaefer CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-05-24 11:17 EDT by jin huang CLA
Modified: 2011-05-26 02:37 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description jin huang CLA 2011-05-24 11:17:58 EDT
Build Identifier: eclipse 3.6.0

In a function of c code, when local variable has the same name as another function A called in this function, if we check the method A's declaration by clicking "Open declaration", it show the declaration of the local variable, but not the declaration of function A.

Reproducible: Always

Steps to Reproduce:
when we have the follow source code, in which a local variable has the same name as a function called,

----------------------------------
1  int methodA(int a, int b){
2  	return a+b;
3  }
4
5
6  int method41(int a, int b){
7  	int methodA = 2;
8  	return methodA(a,b);
9  }
10 
11 int method42(int a, int b){
12	return methodA(a,b);
13 }
----------------------------------

if we right click on the function reference "methodA(...)" in line 8, and click "Open declaration" to get its declaration, eclipse will show you the declaration "int methodA = 2" in line 7, which is a error.


Then, I use eclipse's "c/c++ search" function
1)search string: "methodA*"
2)search for: Function
3)limitedto: references
But I got nothing !!


Finally, I download the source code of CDT from svn, write programs as below to get callers of function with name "methodA", I still just got one function reference from function "method42". The function "method41" is missing.

----------------------------------
	public void doAnalysis(){
		
		//search callers in each project
		for(int i=0;i<scope.length;++i){
			try {
				IIndex index = CCorePlugin.getIndexManager().getIndex((ICProject)scope[i], IIndexManager.ADD_DEPENDENCIES | IIndexManager.ADD_DEPENDENT);
				index.acquireReadLock();
				try {
					IIndexBinding[] callees= index.findBindings(calleeName.toCharArray(), IndexFilter.ALL_DECLARED, new NullProgressMonitor());
					
					// find references for each binding of callee
					for (IIndexBinding callee : callees) {
						if (callee instanceof IFunction) {
							getCallers((ICProject)scope[i], index, callee);
							}
						}//for

				} finally {
					index.releaseReadLock();
				}

			} catch (InterruptedException e) {} 
			catch (CoreException e) {}
			
			System.out.print("End of seach in project : "+ i+ ((ICProject)scope[i]).getElementName() +"\n");
		}//for
		
		System.out.print("number of callers is : "+this.result.getElements().length+"\n");
	}//doAnalysis()
	
	void getCallers(ICProject project, IIndex index, IIndexBinding callee) throws CoreException
	{
		IIndexName[] callers = index.findReferences(callee);
		for (int i = 0; i < callers.length; i++) {
			IIndexName rname = callers[i];
			IIndexName caller = rname.getEnclosingDefinition();
			if (caller != null) {
				ICElement elem = IndexUI.getCElementForName(project, index, caller);
				if (elem != null) {
					result.add(elem, rname);
				}
			}
		}//for
	}//getCallers()
----------------------------------
Comment 1 Markus Schorn CLA 2011-05-25 02:10:03 EDT
The tooling is correct 'methodA' resolves to the local variable and the code does not compile because you cannot use it as a function. 
Also, the search for references of 'methodA' does report the reference in 'method42'.
Comment 2 jin huang CLA 2011-05-25 11:19:14 EDT
yes, you are right.
In a function, the function can not have the same name with a local variable.

Just the CDT do not give any tips!!

Thanks a lot for your explanation.
Comment 3 jin huang CLA 2011-05-25 13:59:11 EDT
Even though the program to be analyzed is supposed to be correct, the declaration from "Open declaration" is still be misleading. 

so I still think that CDT does not give the right tips for its users.
Comment 4 Markus Schorn CLA 2011-05-26 02:37:13 EDT
No it's not misleading, 'methodA' does denote the local variable and open declaration has to go there. This also gives you the chance to correct your
code.