use of org.eclipse.cdt.core.dom.ast.IASTDeclaration in project ch.hsr.ifs.cdttesting by IFS-HSR.
the class MyCodanChecker method processAst.
@Override
public void processAst(IASTTranslationUnit ast) {
String name = "unknown";
IASTDeclaration firstDecl = ast.getDeclarations()[0];
if (firstDecl instanceof IASTFunctionDefinition) {
IASTFunctionDefinition functionDecl = (IASTFunctionDefinition) firstDecl;
name = functionDecl.getDeclarator().getName().getRawSignature();
}
// note that the name-string is inserted into the
reportProblem(MyProblemId.EXAMPLE_ID.getId(), firstDecl, name);
// "messagePattern" by replacing the "{0}" of the pattern.
}
use of org.eclipse.cdt.core.dom.ast.IASTDeclaration in project ch.hsr.ifs.cdttesting by IFS-HSR.
the class ASTComparison method typeSensitiveNodeEquals.
private static <T extends IASTNode> boolean typeSensitiveNodeEquals(T expected, T actual, EnumSet<ComparisonArg> args) {
if (!expected.getClass().equals(actual.getClass())) {
return false;
}
if (expected instanceof IASTDeclaration) {
/* Terminal checks */
if (expected instanceof ICPPASTFunctionDefinition) {
ICPPASTFunctionDefinition et = as(expected);
ICPPASTFunctionDefinition at = as(actual);
return et.isDefaulted() == at.isDefaulted() && et.isDeleted() == at.isDeleted();
} else if (expected instanceof ICPPASTTemplateDeclaration) {
ICPPASTTemplateDeclaration et = as(expected);
ICPPASTTemplateDeclaration at = as(actual);
return et.isExported() == at.isExported();
} else if (expected instanceof ICPPASTNamespaceDefinition) {
ICPPASTNamespaceDefinition et = as(expected);
ICPPASTNamespaceDefinition at = as(actual);
return et.isInline() == at.isInline();
} else if (expected instanceof ICPPASTVisibilityLabel) {
ICPPASTVisibilityLabel et = as(expected);
ICPPASTVisibilityLabel at = as(actual);
return et.getVisibility() == at.getVisibility();
} else if (expected instanceof ICPPASTLinkageSpecification) {
ICPPASTLinkageSpecification et = as(expected);
ICPPASTLinkageSpecification at = as(actual);
return et.getLiteral().equals(at.getLiteral());
} else if (expected instanceof ICPPASTUsingDeclaration) {
ICPPASTUsingDeclaration et = as(expected);
ICPPASTUsingDeclaration at = as(actual);
return et.isTypename() == at.isTypename();
} else if (expected instanceof IASTSimpleDeclaration || expected instanceof ICPPASTAliasDeclaration || expected instanceof ICPPASTUsingDirective) {
/* All tokens are stored in the child-nodes */
return defaultHandler(expected, actual, args);
}
/* Continue comparing raw-signature */
} else if (expected instanceof ICPPASTTemplateParameter) {
/* Template Parameter */
ICPPASTTemplateParameter e = as(expected);
ICPPASTTemplateParameter a = as(actual);
if (e.isParameterPack() != a.isParameterPack())
return false;
if (expected instanceof ICPPASTSimpleTypeTemplateParameter) {
ICPPASTSimpleTypeTemplateParameter et = as(expected);
ICPPASTSimpleTypeTemplateParameter at = as(actual);
return et.getParameterType() == at.getParameterType();
} else if (expected instanceof ICPPASTParameterDeclaration || expected instanceof ICPPASTTemplatedTypeTemplateParameter) {
/* All tokens are stored in the child-nodes */
return defaultHandler(expected, actual, args);
}
} else if (expected instanceof ICPPASTDeclarator) {
/* Terminal checks */
if (expected instanceof ICPPASTFunctionDeclarator) {
ICPPASTFunctionDeclarator e = as(expected);
ICPPASTFunctionDeclarator a = as(actual);
return e.isConst() == a.isConst() && e.isFinal() == a.isFinal() && e.isPureVirtual() == a.isPureVirtual() && e.isVolatile() == a.isVolatile();
} else {
/* All tokens are stored in the child-nodes */
return defaultHandler(expected, actual, args);
}
/* Continue comparing raw-signature */
} else if (expected instanceof ICPPASTDeclSpecifier) {
/* DeclSpecifier */
ICPPASTDeclSpecifier e = as(expected);
ICPPASTDeclSpecifier a = as(actual);
if (e.isConst() != a.isConst() || e.isVirtual() != a.isVirtual() || e.isVolatile() != a.isVolatile() || e.isConstexpr() != a.isConstexpr() || e.isExplicit() != a.isExplicit() || e.isFriend() != a.isFriend() || e.isRestrict() != a.isRestrict() || e.isThreadLocal() != a.isThreadLocal() || e.getStorageClass() != a.getStorageClass())
return false;
/* Terminal checks */
if (expected instanceof ICPPASTNamedTypeSpecifier) {
ICPPASTNamedTypeSpecifier et = as(expected);
ICPPASTNamedTypeSpecifier at = as(actual);
return et.isTypename() == at.isTypename();
} else if (expected instanceof ICPPASTCompositeTypeSpecifier) {
ICPPASTCompositeTypeSpecifier et = as(expected);
ICPPASTCompositeTypeSpecifier at = as(actual);
return et.isFinal() == at.isFinal() && et.isVirtual() == at.isVirtual() && et.getKey() == at.getKey();
} else if (expected instanceof ICPPASTElaboratedTypeSpecifier) {
ICPPASTElaboratedTypeSpecifier et = as(expected);
ICPPASTElaboratedTypeSpecifier at = as(actual);
return et.getKind() == at.getKind();
} else if (expected instanceof ICPPASTEnumerationSpecifier) {
ICPPASTEnumerationSpecifier et = as(expected);
ICPPASTEnumerationSpecifier at = as(actual);
return et.isOpaque() == at.isOpaque() && et.isScoped() == at.isScoped();
} else {
return defaultHandler(expected, actual, args);
}
/* Continue comparing raw-signature */
} else if (expected instanceof IASTExpression) {
if (expected instanceof ICPPASTUnaryExpression) {
ICPPASTUnaryExpression et = as(expected);
ICPPASTUnaryExpression at = as(actual);
return et.getOperator() == at.getOperator();
} else if (expected instanceof ICPPASTBinaryExpression) {
ICPPASTBinaryExpression et = as(expected);
ICPPASTBinaryExpression at = as(actual);
return et.getOperator() == at.getOperator();
} else if (expected instanceof ICPPASTLiteralExpression) {
ICPPASTLiteralExpression et = as(expected);
ICPPASTLiteralExpression at = as(actual);
return et.getKind() == at.getKind() && equalsRaw(et, at, args);
} else if (expected instanceof ICPPASTCastExpression) {
ICPPASTCastExpression et = as(expected);
ICPPASTCastExpression at = as(actual);
return et.getOperator() == at.getOperator();
} else if (expected instanceof ICPPASTNewExpression) {
ICPPASTNewExpression et = as(expected);
ICPPASTNewExpression at = as(actual);
return et.isArrayAllocation() == at.isArrayAllocation() && et.isGlobal() == at.isGlobal() && et.isNewTypeId() == at.isNewTypeId();
} else if (expected instanceof ICPPASTDeleteExpression) {
ICPPASTDeleteExpression et = as(expected);
ICPPASTDeleteExpression at = as(actual);
return et.isVectored() == at.isVectored() && et.isGlobal() == at.isGlobal();
} else if (expected instanceof ICPPASTFieldReference) {
ICPPASTFieldReference et = as(expected);
ICPPASTFieldReference at = as(actual);
return et.isPointerDereference() == at.isPointerDereference() && et.isTemplate() == at.isTemplate();
} else if (expected instanceof ICPPASTLambdaExpression) {
ICPPASTLambdaExpression et = as(expected);
ICPPASTLambdaExpression at = as(actual);
return et.getCaptureDefault() == at.getCaptureDefault();
} else if (expected instanceof ICPPASTFunctionCallExpression || expected instanceof ICPPASTSimpleTypeConstructorExpression || expected instanceof ICPPASTLambdaExpression || expected instanceof ICPPASTPackExpansionExpression || expected instanceof IASTIdExpression || expected instanceof ICPPASTArraySubscriptExpression || expected instanceof ICPPASTExpressionList || expected instanceof IASTConditionalExpression) {
/* All tokens are stored in the child-nodes */
return defaultHandler(expected, actual, args);
}
/* Continue comparing raw-signature */
} else if (expected instanceof ICPPASTReferenceOperator) {
/* ReferenceOperator */
ICPPASTReferenceOperator et = as(expected);
ICPPASTReferenceOperator at = as(actual);
return et.isRValueReference() == at.isRValueReference();
} else if (expected instanceof IASTPointer) {
/* Pointers */
IASTPointer et = as(expected);
IASTPointer at = as(actual);
return et.isConst() == at.isConst() && et.isRestrict() == at.isRestrict() && et.isVolatile() == at.isVolatile();
} else if (expected instanceof IASTStatement) {
return defaultHandler(expected, actual, args);
} else if (expected instanceof ICPPASTPackExpandable) {
/* ICPPASTPackExpandable */
ICPPASTPackExpandable e = as(expected);
ICPPASTPackExpandable a = as(actual);
if (e.isPackExpansion() != a.isPackExpansion())
return false;
if (expected instanceof ICPPASTInitializerList) {
ICPPASTInitializerList et = as(expected);
ICPPASTInitializerList at = as(actual);
return et.getSize() == at.getSize();
} else if (expected instanceof ICPPASTBaseSpecifier) {
ICPPASTBaseSpecifier et = as(expected);
ICPPASTBaseSpecifier at = as(actual);
return et.isVirtual() == at.isVirtual() && et.getVisibility() == at.getVisibility();
} else if (expected instanceof ICPPASTCapture) {
ICPPASTCapture et = as(expected);
ICPPASTCapture at = as(actual);
return et.capturesThisPointer() == at.capturesThisPointer() && et.isByReference() == at.isByReference();
} else if (expected instanceof ICPPASTTypeId) {
return true;
} else {
return defaultHandler(expected, actual, args);
}
} else if (expected instanceof ICPPASTName) {
/* Names */
ICPPASTName e = as(expected);
ICPPASTName a = as(actual);
if (e.isQualified() != a.isQualified())
return false;
if (expected instanceof ICPPASTTemplateId) {
ICPPASTTemplateId et = as(expected);
ICPPASTTemplateId at = as(actual);
return et.isDeclaration() == at.isDeclaration();
} else if (expected instanceof ICPPASTQualifiedName) {
ICPPASTQualifiedName et = as(expected);
ICPPASTQualifiedName at = as(actual);
return et.isFullyQualified() == at.isFullyQualified();
} else if (expected instanceof ICPPASTTemplateId) {
/* Relevant information is contained in the children */
return defaultHandler(expected, actual, args);
}
} else {
/* OTHER */
if (expected instanceof IASTTranslationUnit || expected instanceof IASTArrayModifier || expected instanceof IASTInitializer) {
/* Relevant information is contained in the children */
return defaultHandler(expected, actual, args);
}
/* Continue comparing raw-signature */
}
/* Default case */
return equalsNormalizedRaw(expected, actual);
}
use of org.eclipse.cdt.core.dom.ast.IASTDeclaration 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, 0);
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(new Integer(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(new Integer(curHeaderLine))) {
localHeader += "extern \"C\" {" + NEWLINE;
localHeader += curInclude.getSource() + NEWLINE;
localHeader += "}" + NEWLINE;
} else {
localHeader += curInclude.getSource();
localHeader += NEWLINE;
}
}
break;
}
}
}
return localHeader;
}
Aggregations