1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 22:52:11 +02:00

Bug 337677 - Warning about "void" in late-specified return type

This commit is contained in:
Marc-Andre Laperle 2011-03-11 05:30:11 +00:00
parent 5ef3de90b4
commit 63ec2d8b00
2 changed files with 61 additions and 2 deletions

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2009,2010 Alena Laskavaia
* Copyright (c) 2009, 2011 Alena Laskavaia
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@ -24,6 +24,7 @@ import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
@ -31,11 +32,14 @@ import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IBasicType;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.c.ICASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
@ -186,6 +190,10 @@ public class ReturnChecker extends AbstractAstFunctionChecker {
IASTFunctionDeclarator declarator = func.getDeclarator();
if (declarator.getPointerOperators().length == 0)
return true;
} else if (type == IASTSimpleDeclSpecifier.t_auto) {
if (isAutoVoid(func)) {
return true;
}
}
return false;
}
@ -235,4 +243,39 @@ public class ReturnChecker extends AbstractAstFunctionChecker {
addPreference(problem, PARAM_IMPLICIT, CheckersMessages.ReturnChecker_Param0, Boolean.FALSE);
}
}
/**
* Checks if a "void" return type is specified using the C++0x late-specified return type
* for a given function definition
*
* For example, auto f() -> void { } would return true
*
* @param functionDefinition
* @return True if the function has a void (late-specified) return type, False otherwise
*/
private boolean isAutoVoid(IASTFunctionDefinition functionDefinition) {
IASTFunctionDeclarator declarator = functionDefinition.getDeclarator();
if(declarator instanceof ICPPASTFunctionDeclarator) {
ICPPASTFunctionDeclarator functionDeclarator = (ICPPASTFunctionDeclarator) declarator;
IASTTypeId trailingReturnType = functionDeclarator.getTrailingReturnType();
if(trailingReturnType != null) {
IASTDeclarator abstractDeclarator = trailingReturnType.getAbstractDeclarator();
if(abstractDeclarator != null) {
if(abstractDeclarator.getPointerOperators().length > 0) {
return false;
}
IASTDeclSpecifier declSpecifier = trailingReturnType.getDeclSpecifier();
if(declSpecifier instanceof ICPPASTSimpleDeclSpecifier) {
ICPPASTSimpleDeclSpecifier simpleDeclSpecifier = (ICPPASTSimpleDeclSpecifier) declSpecifier;
if(simpleDeclSpecifier.getType() == IASTSimpleDeclSpecifier.t_void) {
return true;
}
}
}
}
}
return false;
}
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2009, 2010 Alena Laskavaia
* Copyright (c) 2009, 2011 Alena Laskavaia
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@ -156,4 +156,20 @@ public class ReturnCheckerTest extends CheckerTestCase {
loadCodeAndRunCpp(getAboveComment());
checkErrorLine(4);
}
// auto f() -> void
// {
// }
public void testVoidLateSpecifiedReturnType_Bug337677() {
loadCodeAndRunCpp(getAboveComment());
checkNoErrors();
}
// auto f() -> void*
// {
// }
public void testVoidPointerLateSpecifiedReturnType_Bug337677() {
loadCodeAndRunCpp(getAboveComment());
checkErrorLine(1);
}
}