summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorNoel Grandin <noel@peralex.com>2015-08-07 08:35:12 +0200
committerNoel Grandin <noel@peralex.com>2015-08-11 08:25:33 +0200
commit9d307abeec2a80e59af0f0af10dc42d90cd7441a (patch)
treeab939f6c7d0d65d0419fbe305bbc1501f1faceb8 /compilerplugins
parent12fb9096f562281c7ca4b7fed6a3342dac9a91b7 (diff)
new loplugin: defaultparams
find places where we do not need to be passing a parameter to a function, because that function has a default value which matches the value we are passing. Change-Id: I04d1fd6275204dd4925e6563282464f461123632
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/defaultparams.cxx80
1 files changed, 80 insertions, 0 deletions
diff --git a/compilerplugins/clang/defaultparams.cxx b/compilerplugins/clang/defaultparams.cxx
new file mode 100644
index 000000000000..173cc5e11604
--- /dev/null
+++ b/compilerplugins/clang/defaultparams.cxx
@@ -0,0 +1,80 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <string>
+#include <set>
+
+#include "plugin.hxx"
+
+// Find places where we call a method with values == the values specified in the parameter defaults.
+// i.e. where the code might as well not specify anything.
+
+namespace {
+
+class DefaultParams:
+ public RecursiveASTVisitor<DefaultParams>, public loplugin::Plugin
+{
+public:
+ explicit DefaultParams(InstantiationData const & data): Plugin(data) {}
+
+ virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
+
+ bool VisitCallExpr(const CallExpr * callExpr);
+};
+
+bool DefaultParams::VisitCallExpr(const CallExpr * callExpr) {
+ if (ignoreLocation(callExpr)) {
+ return true;
+ }
+ if (callExpr->getNumArgs() == 0) {
+ return true;
+ }
+ if (callExpr->getDirectCallee() == nullptr) {
+ return true;
+ }
+ const FunctionDecl* functionDecl = callExpr->getDirectCallee()->getCanonicalDecl();
+ unsigned i = callExpr->getNumArgs() - 1;
+ const Expr* arg = callExpr->getArg(i);
+ // variadic functions
+ if (i >= functionDecl->getNumParams()) {
+ return true;
+ }
+ const ParmVarDecl* parmVarDecl = functionDecl->getParamDecl(i);
+ const Expr* defaultArgExpr = parmVarDecl->getDefaultArg();
+ if (!arg->isDefaultArgument() &&
+ arg->isIntegerConstantExpr(compiler.getASTContext()) &&
+ parmVarDecl->hasDefaultArg() &&
+ !parmVarDecl->hasUninstantiatedDefaultArg() &&
+ defaultArgExpr->isIntegerConstantExpr(compiler.getASTContext()))
+ {
+ APSInt x1, x2;
+ if (arg->EvaluateAsInt(x1, compiler.getASTContext()) &&
+ defaultArgExpr->EvaluateAsInt(x2, compiler.getASTContext()) &&
+ x1 == x2)
+ {
+ report(
+ DiagnosticsEngine::Warning,
+ "not necessary to pass this argument, it defaults to the same value",
+ callExpr->getSourceRange().getBegin())
+ << callExpr->getSourceRange();
+ report(
+ DiagnosticsEngine::Warning,
+ "default method parameter declaration here",
+ parmVarDecl->getSourceRange().getBegin())
+ << parmVarDecl->getSourceRange();
+ }
+ }
+ return true;
+}
+
+loplugin::Plugin::Registration< DefaultParams > X("defaultparams", false);
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */