Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] Getting translation unit from ICProject

Hi Muhammad,

You can use the following code which gets all the TranslationUnits of a
project by recursively traversing the source directory and any
sub-directories.

/**
	 * Returns as List all the translation units for the given project.
	 * @param cproject the current C/C++ project
	 */
	public static List<ITranslationUnit> getProjectTranslationUnits (ICProject
cproject) {
		List<ITranslationUnit> tuList = new ArrayList<ITranslationUnit>();
		
		//get source folders
		try {
			for (ISourceRoot sourceRoot : cproject. getSourceRoots()){
				//get all elements
				for (ICElement element : sourceRoot.getChildren()){
					//if it is a container (i.e., a source folder)
					if (element.getElementType() == ICElement.C_CCONTAINER){
						recursiveContainerTraversal((ICContainer)element, tuList);
					}
					else{
						ITranslationUnit tu		= (ITranslationUnit) element;
						tuList.add(tu);
					}
				}
			}
		} catch (CModelException e) {
			e.printStackTrace();
		}
		return tuList;
	}


	private static void recursiveContainerTraversal (ICContainer container,
List<ITranslationUnit> tuList) throws CModelException{
		for (ICContainer inContainer : container.getCContainers()){
			recursiveContainerTraversal(inContainer, tuList);
		}
		
		for (ITranslationUnit tu : container.getTranslationUnits()){
			tuList.add(tu);			
		}
	}



Regards,
Simos



--
View this message in context: http://eclipse.1072660.n5.nabble.com/Getting-translation-unit-from-ICProject-tp191362p191364.html
Sent from the Eclipse CDT - Development mailing list archive at Nabble.com.


Back to the top