Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 351422

Summary: CModelUtil.getSourceFolder returns non-source folders
Product: [Tools] CDT Reporter: Mohamed Hussein <mohamed_hussein>
Component: cdt-coreAssignee: Nobody - feel free to take it <nobody>
Status: RESOLVED FIXED QA Contact: Doug Schaefer <cdtdoug>
Severity: normal    
Priority: P3 CC: marc.khouzam, nobody
Version: 7.0   
Target Milestone: 8.1.0   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
Patch to make getSourceFolder only returns source folders
none
Patch to make getSourceFolder only returns source folders only
cdtdoug: iplog+
Modified patch. cdtdoug: iplog-

Description Mohamed Hussein CLA 2011-07-07 07:15:53 EDT
Created attachment 199245 [details]
Patch to make getSourceFolder only returns source folders

The method org.eclipse.cdt.internal.corext.util.CModelUtil.getSourceFolder(ICElement) will return the passed folder even if it is not a source folder.

The Java doc of the method says:
	/**
	 * Returns the source folder of <code>ICElement</code>. If the given
	 * element is already a source folder, the element itself is returned.
	 */
	public static ICContainer getSourceFolder(ICElement element) {

Though inside the method it allows returning the first folder using the check:
if (curr instanceof ICContainer && folder == null) {
    folder = (ICContainer)curr;
}

Changing the condition to
if (curr instanceof ISourceRoot) {

ensures that the method always returns source folders only.
Comment 1 Doug Schaefer CLA 2011-07-13 11:14:30 EDT
The patch you've provided isn't in a format I can work with. It has to be generated from egit and in a format so I can Apply patch with it.
Comment 2 Mohamed Hussein CLA 2011-07-13 14:22:35 EDT
Created attachment 199610 [details]
Patch to make getSourceFolder only returns source folders only

git format patch
Comment 3 Nobody - feel free to take it CLA 2011-08-09 20:00:37 EDT
Created attachment 201201 [details]
Modified patch.
Comment 4 Nobody - feel free to take it CLA 2011-08-09 20:03:25 EDT
Mohamed, I modified your patch a little bit. I'll commit it if my modifications are OK with you.
Comment 5 Mohamed Hussein CLA 2011-08-11 05:22:06 EDT
(In reply to comment #4)
> Mohamed, I modified your patch a little bit. I'll commit it if my modifications
> are OK with you.

Hi Mikhail, Your change performs the same function, so I am ok with using it instead of mine.

Minor point:
I understand you want to minimize the code changes, but I guess you can also move
foundSourceRoot = (curr instanceof ISourceRoot);
Inside the if condition and change it to =true
Comment 6 Nobody - feel free to take it CLA 2011-08-11 10:44:26 EDT
(In reply to comment #5)
> Minor point:
> I understand you want to minimize the code changes, but I guess you can also
> move
> foundSourceRoot = (curr instanceof ISourceRoot);
> Inside the if condition and change it to =true

It's not my intention to minimize the code changes, I just want to simplify the old code. I don't think we need 'foundSourceRoot' flag, we can simply check 'curr instanceof ISourceRoot' directly in the while condition.
Comment 7 Nobody - feel free to take it CLA 2011-08-15 12:32:58 EDT
Applied to the HEAD branch.
Comment 8 CDT Genie CLA 2011-08-15 13:23:02 EDT
*** cdt git genie on behalf of Mohamed Hussein ***

    Bug 351422 - CModelUtil.getSourceFolder returns non-source folders.

[*] http://git.eclipse.org/c/cdt/org.eclipse.cdt.git/commit/?id=e0b1937020793057ce96ee45591a9c35d1fe8704