Bug 178265 - CModelManager#createTranslationUnitFrom only works for external header files, not source
Summary: CModelManager#createTranslationUnitFrom only works for external header files,...
Status: RESOLVED FIXED
Alias: None
Product: CDT
Classification: Tools
Component: cdt-core (show other bugs)
Version: 4.0   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 4.0 RC0   Edit
Assignee: Anton Leherbauer CLA
QA Contact:
URL:
Whiteboard:
Keywords: contributed
Depends on:
Blocks: 176036
  Show dependency tree
 
Reported: 2007-03-20 10:16 EDT by Warren Paul CLA
Modified: 2008-06-20 12:05 EDT (History)
0 users

See Also:


Attachments
patch file (1.14 KB, patch)
2007-03-20 10:17 EDT, Warren Paul CLA
no flags Details | Diff
Updated patch (3.64 KB, patch)
2007-03-28 07:54 EDT, Anton Leherbauer CLA
bjorn.freeman-benson: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Warren Paul CLA 2007-03-20 10:16:38 EDT
Build ID: M20060921-0945

Steps To Reproduce:
1. Create a std make project such that the root of the project does not contain all sources.
2. Add some error in one of these external sources.
3. Build the project.
4. From the error in the problems view, right click and select "Open external file".
5. The file is opened but is not a C/C++ source file - note the editor icon and no content in the outline view.


More information:
CModelManager#createTranslationUnitFrom only works for external header files, not source files.  The steps to reproduce is how I first saw the problem, but if you search for refererences on createTranslationUnitFrom you'll see that many places will have the same problem.

I understand that the typical case is that all sources are under the project root and are hence IFile's, but that's not always the case.

Originally I had that of changing the code in EditorUtility#getEditorInputForLocation like this:

			if (cproject != null) {
				ITranslationUnit unit = CoreModel.getDefault().createTranslationUnitFrom(cproject, location);
				if (unit == null) {
					String id = CoreModel.getRegistedContentTypeId(cproject.getProject(), location.lastSegment());
					if (id != null) {
						unit = new ExternalTranslationUnit(cproject, location, id);
					}
				}
				if (unit != null) {
					return new ExternalEditorInput(unit, new FileStorage(location));
				}


But that would miss the other places that try to get a translation unit with a valid external path and project reference.  So it seems the logical place to add this check is in CModelManager#createTranslationUnitFrom.  Something like the attached patch.

I'm not sure though if this is the best place for the change, or if it would have any bad side effects or not.  Or if external source files were intentionally left out.  But I can't think of a reason why they would be right now.
Comment 1 Warren Paul CLA 2007-03-20 10:17:21 EDT
Created attachment 61383 [details]
patch file
Comment 2 Anton Leherbauer CLA 2007-03-27 05:56:10 EDT
I am basically OK with the patch and don't see a problem, but it should be implemented as a fallback to the current behaviour of searching the include path first instead of overriding it. Otherwise, translation units for referenced header files might get different ICElement parents depending on how they are created.
Comment 3 Warren Paul CLA 2007-03-27 12:11:30 EDT
(In reply to comment #2)
> I am basically OK with the patch and don't see a problem, but it should be
> implemented as a fallback to the current behaviour of searching the include
> path first instead of overriding it. Otherwise, translation units for
> referenced header files might get different ICElement parents depending on how
> they are created.

So just move the if (path.toFile().exists()) block I added below the first try/catch block.  Sounds good to me.
Comment 4 Anton Leherbauer CLA 2007-03-28 03:53:25 EDT
I'll commit post M6.
Comment 5 Anton Leherbauer CLA 2007-03-28 07:54:11 EDT
Created attachment 62214 [details]
Updated patch

I changed the patch as indicated and made sure the content type id is computed only once (potentially expensive).
Comment 6 Anton Leherbauer CLA 2007-04-02 06:52:08 EDT
Applied patch to HEAD.