Search in sources :

Example 1 with IMacro

use of org.eclipse.cdt.core.model.IMacro in project arduino-eclipse-plugin by Sloeber.

the class PdePreprocessor method extendHeaderForFile.

private static String extendHeaderForFile(String header, IIndex index, ITranslationUnit tu) throws CoreException {
    String localHeader = header;
    // Locate All lines that are extern "C"
    HashMap<Integer, Integer> externCLines = new HashMap<>();
    IASTTranslationUnit astTuTest = tu.getAST(index, ITranslationUnit.AST_SKIP_ALL_HEADERS);
    IASTDeclaration[] topDeclaratons = astTuTest.getDeclarations();
    for (IASTDeclaration curTopDeclaration : topDeclaratons) {
        ICPPASTLinkageSpecification test = curTopDeclaration instanceof ICPPASTLinkageSpecification ? (ICPPASTLinkageSpecification) curTopDeclaration : null;
        if (test != null) {
            if (test.getLiteral().equals("\"C\"")) {
                Path curFile = new Path(curTopDeclaration.getContainingFilename());
                if (curFile.equals(tu.getFile().getLocation())) {
                    int startLine = test.getFileLocation().getStartingLineNumber();
                    int endLine = test.getFileLocation().getEndingLineNumber();
                    for (int curline = startLine; curline <= endLine; curline++) {
                        externCLines.put(Integer.valueOf(curline), null);
                    }
                }
            }
        }
    }
    // find the last line containing a include
    IInclude[] includes = tu.getIncludes();
    int lastHeaderLine = 0;
    for (IInclude include : includes) {
        int curHeaderLine = include.getSourceRange().getEndLine();
        lastHeaderLine = Math.max(lastHeaderLine, curHeaderLine);
    }
    // parse line by line until all includes have been parsed
    for (int curline = 1; curline <= lastHeaderLine; curline++) {
        ICElement curElement = tu.getElementAtLine(curline);
        if (curElement != null) {
            switch(curElement.getElementType()) {
                case ICElement.C_MACRO:
                    IMacro curMacro = (IMacro) curElement;
                    if (curMacro.isActive()) {
                        localHeader += curMacro.getSource() + NEWLINE;
                    }
                    break;
                case ICElement.C_VARIABLE:
                    IVariable curVardeclar = (IVariable) curElement;
                    if (curVardeclar.isActive()) {
                        String fullTypeName = curVardeclar.getTypeName();
                        // ignore double arrays
                        if (fullTypeName.indexOf('[') == fullTypeName.lastIndexOf('[')) {
                            String typeName = fullTypeName.replace('[', ' ').replace(']', ' ').trim();
                            String typeExtensions = fullTypeName.replace(typeName, "");
                            localHeader += "extern " + typeName + " " + curVardeclar.getElementName() + typeExtensions + ";" + NEWLINE;
                        }
                    }
                    break;
                case ICElement.C_INCLUDE:
                    IInclude curInclude = (IInclude) curElement;
                    int curHeaderLine = curInclude.getSourceRange().getStartLine();
                    if (curInclude.isActive()) {
                        if (externCLines.containsKey(Integer.valueOf(curHeaderLine))) {
                            localHeader += "extern \"C\" {" + NEWLINE;
                            localHeader += curInclude.getSource() + NEWLINE;
                            localHeader += "}" + NEWLINE;
                        } else {
                            localHeader += curInclude.getSource();
                            localHeader += NEWLINE;
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }
    return localHeader;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) HashMap(java.util.HashMap) IASTDeclaration(org.eclipse.cdt.core.dom.ast.IASTDeclaration) IVariable(org.eclipse.cdt.core.model.IVariable) IInclude(org.eclipse.cdt.core.model.IInclude) ICPPASTLinkageSpecification(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLinkageSpecification) ICElement(org.eclipse.cdt.core.model.ICElement) IASTTranslationUnit(org.eclipse.cdt.core.dom.ast.IASTTranslationUnit) IMacro(org.eclipse.cdt.core.model.IMacro)

Aggregations

HashMap (java.util.HashMap)1 IASTDeclaration (org.eclipse.cdt.core.dom.ast.IASTDeclaration)1 IASTTranslationUnit (org.eclipse.cdt.core.dom.ast.IASTTranslationUnit)1 ICPPASTLinkageSpecification (org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLinkageSpecification)1 ICElement (org.eclipse.cdt.core.model.ICElement)1 IInclude (org.eclipse.cdt.core.model.IInclude)1 IMacro (org.eclipse.cdt.core.model.IMacro)1 IVariable (org.eclipse.cdt.core.model.IVariable)1 IPath (org.eclipse.core.runtime.IPath)1 Path (org.eclipse.core.runtime.Path)1