use of org.eclipse.cdt.core.model.IVariable 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;
}
Aggregations