File indexing completed on 2024-05-12 05:41:00

0001 /*
0002     SPDX-FileCopyrightText: 2016 Sergio Martins <smartins@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "returning-void-expression.h"
0008 #include "ClazyContext.h"
0009 #include "ContextUtils.h"
0010 #include "clazy_stl.h"
0011 
0012 #include <clang/AST/Decl.h>
0013 #include <clang/AST/Expr.h>
0014 #include <clang/AST/Stmt.h>
0015 #include <clang/AST/Type.h>
0016 #include <clang/Basic/LLVM.h>
0017 #include <llvm/Support/Casting.h>
0018 
0019 namespace clang
0020 {
0021 class DeclContext;
0022 } // namespace clang
0023 
0024 using namespace clang;
0025 
0026 ReturningVoidExpression::ReturningVoidExpression(const std::string &name, ClazyContext *context)
0027     : CheckBase(name, context, Option_CanIgnoreIncludes)
0028 {
0029 }
0030 
0031 void ReturningVoidExpression::VisitStmt(clang::Stmt *stmt)
0032 {
0033     auto *ret = dyn_cast<ReturnStmt>(stmt);
0034     if (!ret || !clazy::hasChildren(ret)) {
0035         return;
0036     }
0037 
0038     QualType qt = ret->getRetValue()->getType();
0039     if (qt.isNull() || !qt->isVoidType()) {
0040         return;
0041     }
0042 
0043     DeclContext *context = clazy::contextForDecl(m_context->lastDecl);
0044     if (!context) {
0045         return;
0046     }
0047 
0048     auto *func = dyn_cast<FunctionDecl>(context);
0049     // A function template returning T won't bailout in the void check above, do it properly now:
0050     if (!func || !func->getReturnType()->isVoidType()) {
0051         return;
0052     }
0053 
0054     emitWarning(stmt, "Returning a void expression");
0055 }