use of org.eclipse.cdt.core.dom.ast.IASTNode in project linuxtools by eclipse.
the class WrongDeallocationResolution method apply.
@Override
public void apply(IMarker marker, IDocument document) {
try {
IASTNode astNode = getIASTNode(marker, document);
if (astNode != null) {
int nodeLength = astNode.getFileLocation().getNodeLength();
int nodeOffset = astNode.getFileLocation().getNodeOffset();
String content = document.get(nodeOffset, nodeLength);
if (content.contains(DELETE)) {
String allocFunction = getAllocFunction(marker, document);
if (allocFunction.contains(NEW)) {
// $NON-NLS-1$
content = document.get(nodeOffset, nodeLength).replace(DELETE, DELETE + "[]");
document.replace(nodeOffset, nodeLength, content);
} else {
addParentheses(astNode, document);
if (content.contains("[")) {
// $NON-NLS-1$
removeBrackets(astNode, document);
}
content = document.get(nodeOffset, nodeLength).replace(DELETE, FREE);
document.replace(nodeOffset, nodeLength, content);
}
} else if (content.contains(FREE)) {
if (getAllocFunction(marker, document).contains("[")) {
// $NON-NLS-1$
// $NON-NLS-1$
content = content.concat("[]");
}
content = content.replace(FREE, DELETE);
document.replace(nodeOffset, nodeLength, content);
}
IValgrindMessage message = getMessage(marker);
removeMessage(message.getParent());
ValgrindStackFrame nestedStackFrame = getStackBottom(getNestedStack(message.getParent()));
int nestedLine = nestedStackFrame.getLine();
String nestedFile = nestedStackFrame.getFile();
removeMarker(nestedFile, nestedLine, marker.getType());
marker.delete();
}
} catch (BadLocationException | CoreException e) {
Status status = new Status(IStatus.ERROR, ValgrindUIPlugin.PLUGIN_ID, null, e);
// $NON-NLS-1$
String title = Messages.getString("ValgrindMemcheckQuickFixes.Valgrind_error_title");
// $NON-NLS-1$
String message = Messages.getString("ValgrindMemcheckQuickFixes.Error_applying_quickfix");
showErrorMessage(title, message, status);
}
}
use of org.eclipse.cdt.core.dom.ast.IASTNode in project linuxtools by eclipse.
the class WrongDeallocationResolution method addParentheses.
/**
* Adds parentheses to a function call (if necessary)
* @param node {@link IASTNode} containing the function call
* @throws BadLocationException
*/
private void addParentheses(IASTNode node, IDocument document) throws BadLocationException {
IASTNode[] children = node.getChildren();
if (children.length > 0 && !children[0].getRawSignature().contains("(")) {
// $NON-NLS-1$
IASTNode childNode = children[0];
int childNodeLength = childNode.getFileLocation().getNodeLength();
int childNodeOffset = childNode.getFileLocation().getNodeOffset();
String childContent = document.get(childNodeOffset, childNodeLength);
// $NON-NLS-1$//$NON-NLS-2$
String newChild = "(".concat(childContent).concat(")");
// Skewed 1 char to left to remove space before parentheses
document.replace(childNodeOffset - 1, childNodeLength + 1, newChild);
}
}
use of org.eclipse.cdt.core.dom.ast.IASTNode in project linuxtools by eclipse.
the class CParser method parseCurrentFunction.
@Override
public String parseCurrentFunction(IEditorInput input, int offset) throws CoreException {
String currentElementName;
if (input instanceof IFileEditorInput) {
// Get the working copy and connect to input.
IWorkingCopyManager manager = CUIPlugin.getDefault().getWorkingCopyManager();
manager.connect(input);
// Retrieve the C/C++ Element in question.
IWorkingCopy workingCopy = manager.getWorkingCopy(input);
ICElement method = workingCopy.getElementAtOffset(offset);
manager.disconnect(input);
// no element selected
if (method == null)
return "";
// Get the current element name, to test it.
currentElementName = method.getElementName();
// Element doesn't have a name. Can go no further.
if (currentElementName == null) {
// element doesn't have a name
return "";
}
// Get the Element Type to test.
int elementType = method.getElementType();
switch(elementType) {
case ICElement.C_FIELD:
case ICElement.C_METHOD:
case ICElement.C_FUNCTION:
break;
case ICElement.C_MODEL:
return "";
// So it's not a method, field, function, or model. Where are we?
default:
ICElement tmpMethodType;
if (((tmpMethodType = method.getAncestor(ICElement.C_FUNCTION)) == null) && ((tmpMethodType = method.getAncestor(ICElement.C_METHOD)) == null) && ((tmpMethodType = method.getAncestor(ICElement.C_CLASS)) == null)) {
return "";
} else {
// In a class, but not in a method. Return class name instead.
method = tmpMethodType;
currentElementName = method.getElementName();
}
}
// Build all ancestor classes.
// Append all ancestor class names to string
ICElement tmpParent = method.getParent();
while (tmpParent != null) {
ICElement tmpParentClass = tmpParent.getAncestor(ICElement.C_CLASS);
if (tmpParentClass != null) {
String tmpParentClassName = tmpParentClass.getElementName();
if (tmpParentClassName == null)
return currentElementName;
currentElementName = tmpParentClassName + "." + currentElementName;
} else
return currentElementName;
tmpParent = tmpParentClass.getParent();
}
return currentElementName;
} else if (input instanceof IStorageEditorInput) {
// Get the working copy and connect to input.
// don't follow inclusions
currentElementName = "";
IStorageEditorInput sei = (IStorageEditorInput) input;
// don't follow inclusions
IncludeFileContentProvider contentProvider = IncludeFileContentProvider.getEmptyFilesProvider();
// empty scanner info
IScannerInfo scanInfo = new ScannerInfo();
IStorage ancestorStorage = sei.getStorage();
if (ancestorStorage == null)
return "";
InputStream stream = ancestorStorage.getContents();
byte[] buffer = new byte[100];
String data = "";
int read = 0;
try {
do {
read = stream.read(buffer);
if (read > 0) {
String tmp = new String(buffer, 0, read);
data = data.concat(tmp);
}
} while (read == 100);
stream.close();
} catch (IOException e) {
// do nothing
}
// $NON-NLS-1$
FileContent content = FileContent.create("<text>", data.toCharArray());
// determine the language
ILanguage language = GPPLanguage.getDefault();
try {
IASTTranslationUnit ast;
int options = 0;
ast = language.getASTTranslationUnit(content, scanInfo, contentProvider, null, options, ParserUtil.getParserLogService());
IASTNodeSelector n = ast.getNodeSelector(null);
IASTNode node = n.findFirstContainedNode(offset, 100);
while (node != null && !(node instanceof IASTTranslationUnit)) {
if (node instanceof IASTFunctionDefinition) {
IASTFunctionDefinition fd = (IASTFunctionDefinition) node;
IASTFunctionDeclarator d = fd.getDeclarator();
currentElementName = new String(d.getName().getSimpleID());
break;
}
node = node.getParent();
}
} catch (CoreException exc) {
currentElementName = "";
CUIPlugin.log(exc);
}
return currentElementName;
}
return "";
}
use of org.eclipse.cdt.core.dom.ast.IASTNode in project linuxtools by eclipse.
the class AbstractValgrindMarkerResolution method getIASTNode.
/**
* Returns the enclosed AST node in the given marker.
* @param marker The {@link IMarker} containing the {@link IASTNode}
* @param document - document which is used to calculate offset
* @return the enclosed {@link IASTNode}
*/
protected IASTNode getIASTNode(IMarker marker, IDocument document) {
int offset = this.getOffset(marker, document);
int length = this.getLength(marker, document);
IASTNode node = null;
IASTTranslationUnit ast = getASTTranslationUnit(marker);
IASTNodeSelector nodeSelector = ast.getNodeSelector(marker.getResource().getLocationURI().getPath());
node = nodeSelector.findFirstContainedNode(offset, length);
return node;
}
Aggregations