From 63ec2d8b003765d419bedd14b6e978a99650f612 Mon Sep 17 00:00:00 2001 From: Marc-Andre Laperle Date: Fri, 11 Mar 2011 05:30:11 +0000 Subject: [PATCH] Bug 337677 - Warning about "void" in late-specified return type --- .../internal/checkers/ReturnChecker.java | 45 ++++++++++++++++++- .../internal/checkers/ReturnCheckerTest.java | 18 +++++++- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ReturnChecker.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ReturnChecker.java index b735461f610..5898623cc1e 100644 --- a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ReturnChecker.java +++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ReturnChecker.java @@ -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; + } } diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ReturnCheckerTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ReturnCheckerTest.java index d048137826c..40c14495ea0 100644 --- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ReturnCheckerTest.java +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ReturnCheckerTest.java @@ -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); + } } \ No newline at end of file