Search in sources :

Example 1 with ITranslationUnit

use of org.eclipse.cdt.core.model.ITranslationUnit in project ch.hsr.ifs.cdttesting by IFS-HSR.

the class SourceFileBaseTest method assertEqualsWithAST.

private void assertEqualsWithAST(final String testSourceFileName, EnumSet<ComparisonArg> args, int astStyle) {
    IIndex expectedIndex = null;
    IIndex currentIndex = null;
    try {
        // TODO parallelize creation of index and ast
        expectedIndex = CCorePlugin.getIndexManager().getIndex(getExpectedCProject(), IIndexManager.ADD_DEPENDENCIES & IIndexManager.ADD_DEPENDENT);
        currentIndex = CCorePlugin.getIndexManager().getIndex(getCurrentCProject(), IIndexManager.ADD_DEPENDENCIES & IIndexManager.ADD_DEPENDENT);
        expectedIndex.acquireReadLock();
        currentIndex.acquireReadLock();
        ITranslationUnit expectedTU = CoreModelUtil.findTranslationUnit(getExpectedIFile(testSourceFileName));
        ITranslationUnit currentTU = CoreModelUtil.findTranslationUnit(getCurrentIFile(testSourceFileName));
        // ASTComparison.assertEqualsAST(expectedTU.getAST(), currentTU.getAST(), args);
        ASTComparison.assertEqualsAST(expectedTU.getAST(expectedIndex, astStyle), currentTU.getAST(currentIndex, astStyle), args);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (expectedIndex != null)
            expectedIndex.releaseReadLock();
        if (currentIndex != null)
            currentIndex.releaseReadLock();
    }
}
Also used : IIndex(org.eclipse.cdt.core.index.IIndex) ITranslationUnit(org.eclipse.cdt.core.model.ITranslationUnit) IOException(java.io.IOException)

Example 2 with ITranslationUnit

use of org.eclipse.cdt.core.model.ITranslationUnit in project ch.hsr.ifs.cdttesting by IFS-HSR.

the class TestProjectHolder method formatFileAsync.

@Override
public ProjectHolderJob formatFileAsync(IPath path) {
    return ProjectHolderJob.create("Formatting project " + projectName, ITestProjectHolder.FORMATT_FILE_JOB_FAMILY, mon -> {
        if (!formattedDocuments.contains(path)) {
            final IDocument doc = getDocument(getFile(path));
            final Map<String, Object> options = new HashMap<>(cProject.getOptions(true));
            try {
                final ITranslationUnit tu = CoreModelUtil.findTranslationUnitForLocation(path, cProject);
                options.put(DefaultCodeFormatterConstants.FORMATTER_TRANSLATION_UNIT, tu);
                final CodeFormatter formatter = ToolFactory.createCodeFormatter(options);
                final TextEdit te = formatter.format(CodeFormatter.K_TRANSLATION_UNIT, path.toOSString(), 0, doc.getLength(), 0, NL);
                te.apply(doc);
                formattedDocuments.add(path);
            } catch (CModelException | MalformedTreeException | BadLocationException e) {
                e.printStackTrace();
            }
        }
    });
}
Also used : CodeFormatter(org.eclipse.cdt.core.formatter.CodeFormatter) HashMap(java.util.HashMap) TextEdit(org.eclipse.text.edits.TextEdit) CModelException(org.eclipse.cdt.core.model.CModelException) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) IDocument(org.eclipse.jface.text.IDocument) ITranslationUnit(org.eclipse.cdt.core.model.ITranslationUnit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 3 with ITranslationUnit

use of org.eclipse.cdt.core.model.ITranslationUnit in project linuxtools by eclipse.

the class CModelLabelsTest method testFileLabelsCPP.

@Test
public void testFileLabelsCPP() throws Exception {
    ILaunchConfiguration config = createConfiguration(proj.getProject());
    // $NON-NLS-1$
    doLaunch(config, "testFileLabelsCPP");
    CachegrindViewPart view = (CachegrindViewPart) ValgrindUIPlugin.getDefault().getView().getDynamicView();
    CachegrindOutput output = view.getOutputs()[0];
    // $NON-NLS-1$
    CachegrindFile file = getFileByName(output, "cpptest.cpp");
    assertTrue(file.getModel() instanceof ITranslationUnit);
    checkLabelProvider(file);
}
Also used : ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) CachegrindOutput(org.eclipse.linuxtools.internal.valgrind.cachegrind.model.CachegrindOutput) CachegrindFile(org.eclipse.linuxtools.internal.valgrind.cachegrind.model.CachegrindFile) CachegrindViewPart(org.eclipse.linuxtools.internal.valgrind.cachegrind.CachegrindViewPart) ITranslationUnit(org.eclipse.cdt.core.model.ITranslationUnit) Test(org.junit.Test)

Example 4 with ITranslationUnit

use of org.eclipse.cdt.core.model.ITranslationUnit in project linuxtools by eclipse.

the class CModelLabelsTest method testFileLabelsH.

@Test
public void testFileLabelsH() throws Exception {
    ILaunchConfiguration config = createConfiguration(proj.getProject());
    // $NON-NLS-1$
    doLaunch(config, "testFileLabelsH");
    CachegrindViewPart view = (CachegrindViewPart) ValgrindUIPlugin.getDefault().getView().getDynamicView();
    CachegrindOutput output = view.getOutputs()[0];
    // $NON-NLS-1$
    CachegrindFile file = getFileByName(output, "cpptest.h");
    assertTrue(file.getModel() instanceof ITranslationUnit);
    assertTrue(((ITranslationUnit) file.getModel()).isHeaderUnit());
    checkLabelProvider(file);
}
Also used : ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) CachegrindOutput(org.eclipse.linuxtools.internal.valgrind.cachegrind.model.CachegrindOutput) CachegrindFile(org.eclipse.linuxtools.internal.valgrind.cachegrind.model.CachegrindFile) CachegrindViewPart(org.eclipse.linuxtools.internal.valgrind.cachegrind.CachegrindViewPart) ITranslationUnit(org.eclipse.cdt.core.model.ITranslationUnit) Test(org.junit.Test)

Example 5 with ITranslationUnit

use of org.eclipse.cdt.core.model.ITranslationUnit in project linuxtools by eclipse.

the class SystemTapLaunchShortcut method getFunctionsFromBinary.

/**
 * Retrieves the names of all functions referenced by the binary. If
 * searchForResource is true, this function will return all function names
 * belonging to an element with name matching the String held by
 * resourceToSearchFor. Otherwise it will create a dialog prompting the user
 * to select from a list of files to profile, or select the only available
 * file if only one file is available.
 *
 * @param bin
 * @return
 */
protected String getFunctionsFromBinary(IBinary bin, String targetResource) {
    // $NON-NLS-1$
    String funcs = "";
    if (bin == null) {
        return funcs;
    }
    try {
        ArrayList<ICContainer> list = new ArrayList<>();
        TranslationUnitVisitor v = new TranslationUnitVisitor();
        for (ICElement b : bin.getCProject().getChildrenOfType(ICElement.C_CCONTAINER)) {
            ICContainer c = (ICContainer) b;
            for (ITranslationUnit tu : c.getTranslationUnits()) {
                if (searchForResource && tu.getElementName().contains(targetResource)) {
                    tu.accept(v);
                    funcs += v.getFunctions();
                    return funcs;
                } else {
                    if (!list.contains(c)) {
                        list.add(c);
                    }
                }
            }
            // Iterate down to all children, checking for more C_Containers
            while (c.getChildrenOfType(ICElement.C_CCONTAINER).size() > 0) {
                ICContainer e = null;
                for (ICElement d : c.getChildrenOfType(ICElement.C_CCONTAINER)) {
                    e = (ICContainer) d;
                    for (ITranslationUnit tu : e.getTranslationUnits()) {
                        if (searchForResource && tu.getElementName().contains(targetResource)) {
                            tu.accept(v);
                            funcs += (v.getFunctions());
                            return funcs;
                        } else {
                            if (!list.contains(c)) {
                                list.add(c);
                            }
                        }
                    }
                }
                c = e;
            }
        }
        int numberOfFiles = numberOfValidFiles(list.toArray());
        if (numberOfFiles == 1) {
            for (ICContainer c : list) {
                for (ITranslationUnit e : c.getTranslationUnits()) {
                    if (validElement(e)) {
                        e.accept(v);
                        funcs += v.getFunctions();
                    }
                }
            }
        } else {
            Object[] unitList = chooseUnit(list, numberOfFiles);
            if (unitList == null || unitList.length == 0) {
                return null;
            } else if (unitList.length == 1 && unitList[0].toString().equals(USER_SELECTED_ALL)) {
                // $NON-NLS-1$
                funcs = "*";
                return funcs;
            }
            StringBuilder tmpFunc = new StringBuilder();
            for (String item : getAllFunctions(bin.getCProject(), unitList)) {
                tmpFunc.append(item);
                // $NON-NLS-1$
                tmpFunc.append(" ");
            }
            funcs = tmpFunc.toString();
        }
        return funcs;
    } catch (CoreException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : ICContainer(org.eclipse.cdt.core.model.ICContainer) CoreException(org.eclipse.core.runtime.CoreException) ArrayList(java.util.ArrayList) ICElement(org.eclipse.cdt.core.model.ICElement) ITranslationUnit(org.eclipse.cdt.core.model.ITranslationUnit)

Aggregations

ITranslationUnit (org.eclipse.cdt.core.model.ITranslationUnit)8 ArrayList (java.util.ArrayList)3 CoreException (org.eclipse.core.runtime.CoreException)3 HashMap (java.util.HashMap)2 DOMException (org.eclipse.cdt.core.dom.ast.DOMException)2 IASTName (org.eclipse.cdt.core.dom.ast.IASTName)2 IIndex (org.eclipse.cdt.core.index.IIndex)2 IFunctionSummary (org.eclipse.cdt.ui.IFunctionSummary)2 ILaunchConfiguration (org.eclipse.debug.core.ILaunchConfiguration)2 CachegrindViewPart (org.eclipse.linuxtools.internal.valgrind.cachegrind.CachegrindViewPart)2 CachegrindFile (org.eclipse.linuxtools.internal.valgrind.cachegrind.model.CachegrindFile)2 CachegrindOutput (org.eclipse.linuxtools.internal.valgrind.cachegrind.model.CachegrindOutput)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 SortedMap (java.util.SortedMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1