Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-dev] PR 52864 : identification of translation units

All,

  I've been talking with some folks about PR 52864,
providing a way to customize how the core model
identifies a translation unit.

  Attached is a proposed solution.  It presents a
simple extension point and API for resolving file
types.

  If this proposal seems reasonable, I'd start by
implementing the API elements as part of the core
(in org.eclipse.cdt.core), and adding declarations
to the core plugin.xml for standard C/C++ file
types.

  Once the core API is in place, I'd take a look at
the next step - working on a preference page that
would allow the user to create, replace, update, or
delete file type resolution information.

  Comments?

-Samrobb






--------------------------------------------------------------------------------
1. Background
--------------------------------------------------------------------------------

  What follows is a proposed solution for PR 52864, which calls for a way for
  end users to customize how the core model identifies a translation unit.

  The problem can be summed up as:

    "MY projects use *.inc as an extension for inline source files.  I
     can't get CDT to recognize that these are source files.  How do I
     fix this?"

  The proposed type resolution solution should:

    - Provide extension points that allow plugins to identify supported file
      types (by file name or extension)
    - Allow a caller to identify the type of a file (ie, C source, C++ source,
      assembly source, etc.)
    - Provide an interface that allows an end user to create, replace, update,
      and delete file type mappings.

  Ideally, the implementation should be as effecient as possible, since the
  file type resolution operation may be invoked frequently.

--------------------------------------------------------------------------------
2. Desired Usage
--------------------------------------------------------------------------------

  It should be easy for a client of the API to identify the type of a particular
  file, based only on file name.

  For example:

    ICFileType type = CCorePlugin.getFileType("foo.cpp");

    if (type.isType("org.eclipse.cdt.core.file.type.cxx_source")) {
            /* do something with the C++ source file here */
    }

  ... or ...

    ICFileType type = CCorePlugin.getFileType("foo.cpp");

    if (type.isSource()) {
        /* do something with the generic source file here */
    }

--------------------------------------------------------------------------------
3. Schema
--------------------------------------------------------------------------------

  A schema element encodes potential file attributes and file language
  information as part of the file type.

  A. org.eclipse.cdt.core.file.type

    This extension point allows a plugin to identify a file type.

    The pattern attribute is a comma-seperated list(ex, "*.cpp,*.cxx,*.cc").

    The attributes that can be associated with a file type are limited to a well-
    known (hardcoded) list - "source,header,binary".  This should allow the
    implementation to encode them at load time to improve runtime access to the
    attribute values.
  
    <extension point='org.eclipse.cdt.core.file.type'>
        <type
            pattern='*.c'
            id='org.eclipse.cdt.core.file.type.c_source'
            attributes='source'
            description='C Source File'
            icon='cfile.ico' />
        <type
            pattern='*.h'
            id='org.eclipse.cdt.core.file.type.c_header'
            attributes='header'
            description='C Header File'
            icon='hfile.ico' />
        <type
            pattern='*.cpp,*.cxx,*.cc'
            id='org.eclipse.cdt.core.file.type.cxx_source'
            attributes='source'
            description='C++ Source File'
            icon='ccfile.ico' />
        <type
            pattern='*.hpp,*.hxx,*.hh'
            id='org.eclipse.cdt.core.file.type.cxx_header'
            attributes='header'
            description='C++ Header File'
            icon='hhfile.ico' />
        <type
            pattern='*.asm,*.s,*.S'
            id='org.eclipse.cdt.core.file.type.asm_source'
            attributes='source'
            description='Assembly Source File'
            icon='asmfile.ico' />
    </extension>

--------------------------------------------------------------------------------
4. API 
--------------------------------------------------------------------------------


/**
 * Constants that identify base attribute and language IDs
 */
public interface ICFileTypeConstants {

	public static String ID_PREFIX 		= "org.eclipse.cdt.core.file.type";
	
	public static String C_SOURCE		= ID_PREFIX + ".c_source";

	public static String C_HEADER		= ID_PREFIX + ".c_header";

	public static String CXX_HEADER		= ID_PREFIX + ".cxx_source";

	public static String CXX_SOURCE		= ID_PREFIX + ".cxx_header";

	public static String ASM_SOURCE		= ID_PREFIX + ".asm_source";
}

--------------------------------------------------------------------------------

/**
 * Corresponds to an org.eclipse.cdt.core.file.type entry
 * 
 * The intent is that the underlying mechanism will use file names
 * (primarily, file name extensions) to identify file types.
 * 
 * The current incarnation of the interface presumes that file types
 * consist of:
 *
 *   File name (ex, "iostream")
 *   File extension (ex, "cpp")
 *   File attributes (binary, source, header, etc.)
 *   File language (c, c++, asm, etc.)
 */
public interface ICFileType {
	/**
	 * Checks to see if this file type matches the provided
	 * file name.
	 *
	 * Matches are first checked based on file extension; if
	 * no match is found, then the method looks for a match
	 * based on the file name as a whole.
	 * 
	 * @return true if the file type matches the provided name.
	 */
	public boolean matches(String fileName);

	/**
         * @return File name patterns associated with this file type.
         */
        public String[] getPatterns();

	/**
         * @return Id associated with this file type.
         */
        public String getId();

	/**
         * @return Description of this file type.
         */
        public String getDescription();

        /**
         * @return Image descriptor for this file type.
         */
        String ImageDescriptor getImageDescriptor();

	/**
	 * @return True if this is a known binary file type.
	 */
	public boolean isBinary();

	/**
	 * @return True if this is a known source file type.
	 */
	public boolean isSource();
	
	/**
	 * @return True if this is a known header file type.
	 */
	public boolean isHeader();
	
	/**
	 * @return True if this is a known source or header file type.
	 */
	public boolean isTranslationUnit();
}

--------------------------------------------------------------------------------

/**
 * Data holder for file type resolution information.
 *
 * Accessed by ICFileTypeResolver and file type management UI.
 */
public interface ICFileTypeManager {
	/**
	 * @return Array containing all known file types.
	 */
	public ICFileType[] getFileTypes();  

	/**
	 * Add a new file type to the resolver's list.
	 * 
	 * @param type File type to add.
	 *
	 * @return True if the file type object was added.
	 */
	public boolean addFileType(ICFileType type);

	/**
	 * Remove the file specified file type object from the
	 * resolver's list.
	 *
	 * @param type File type to remove.
	 * 
	 * @return True if the file type was found and removed.
	 */
	public boolean removeFileType(ICFileType type);  
}


--------------------------------------------------------------------------------

  (method added to CCorePlugin)

	/**
	 * Returns the file type object corresponding to the provided
	 * file name.
	 * 
	 * The provided file name may be a full filename or file extension.
	 * 
	 * If no file type object exists, a default file type object with
	 * (ATTR_NONE, LANG_NONE) is returned.
	 * 
	 * The implementation checks for a match for the provided file name
         *   - By looking for an exact filename match ("iostream" == "iostream")
         *   - By looking for an extension match ("foo.c" == "*.c")
         *   - By looking for a pattern match ("libfoo.so.1.0" == "*.so*")
	 * 
	 * @param fileName Name of the file to resolve type infor for.
	 * 
	 * @return File type object for the provided file name.
	 */
	public ICFileType getFileType(String fileName);
 

Back to the top