summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2017-08-14 08:28:13 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2017-08-14 09:14:39 +0200
commit0c3bd1d482ece1f2217cc87069179161f6a5bb56 (patch)
tree86bab8ab624fd60076838f068f010372321a204b
parentTest division and especially error handling (diff)
downloadcore-0c3bd1d482ece1f2217cc87069179161f6a5bb56.tar.gz
core-0c3bd1d482ece1f2217cc87069179161f6a5bb56.zip
new loplugin:droplong
The types 'long' and 'unsigned long' are different sizes on different platforms, making them wholy unsuitable for portable code. And when I mean different sizes, I mean 64bit Linux and 64bit Windows have different bit sizes. Change-Id: Id4d8cd709bf71a3f85ed6515fae2b0a21ae8c25d Reviewed-on: https://gerrit.libreoffice.org/41130 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
-rw-r--r--codemaker/source/cppumaker/cppuoptions.cxx2
-rw-r--r--codemaker/source/javamaker/javaoptions.cxx2
-rw-r--r--comphelper/source/misc/backupfilehelper.cxx8
-rw-r--r--compilerplugins/clang/droplong.cxx123
-rw-r--r--compilerplugins/clang/test/droplong.cxx20
-rw-r--r--jurt/source/pipe/com_sun_star_lib_connections_pipe_PipeConnection.c2
-rw-r--r--solenv/CompilerTest_compilerplugins_clang.mk1
-rw-r--r--soltools/cpp/_eval.c6
-rw-r--r--soltools/mkdepend/def.h2
9 files changed, 155 insertions, 11 deletions
diff --git a/codemaker/source/cppumaker/cppuoptions.cxx b/codemaker/source/cppumaker/cppuoptions.cxx
index d6f31e89aeee..b399de19147e 100644
--- a/codemaker/source/cppumaker/cppuoptions.cxx
+++ b/codemaker/source/cppumaker/cppuoptions.cxx
@@ -278,7 +278,7 @@ bool CppuOptions::initOptions(int ac, char* av[], bool bCmdFile)
ret = initOptions(rargc, rargv, bCmdFile);
- for (long j=0; j < rargc; j++)
+ for (int j=0; j < rargc; j++)
{
free(rargv[j]);
}
diff --git a/codemaker/source/javamaker/javaoptions.cxx b/codemaker/source/javamaker/javaoptions.cxx
index a09107a99b53..08c5a7f4dbf8 100644
--- a/codemaker/source/javamaker/javaoptions.cxx
+++ b/codemaker/source/javamaker/javaoptions.cxx
@@ -208,7 +208,7 @@ bool JavaOptions::initOptions(int ac, char* av[], bool bCmdFile)
ret = initOptions(rargc, rargv, bCmdFile);
- for (long j=0; j < rargc; j++)
+ for (int j=0; j < rargc; j++)
{
free(rargv[j]);
}
diff --git a/comphelper/source/misc/backupfilehelper.cxx b/comphelper/source/misc/backupfilehelper.cxx
index ebf93aeb8282..abc8579a542b 100644
--- a/comphelper/source/misc/backupfilehelper.cxx
+++ b/comphelper/source/misc/backupfilehelper.cxx
@@ -654,9 +654,9 @@ namespace
if (aList.is())
{
- const long nLength(aList->getLength());
+ const sal_Int32 nLength(aList->getLength());
- for (long a(0); a < nLength; a++)
+ for (sal_Int32 a(0); a < nLength; a++)
{
const uno::Reference< xml::dom::XElement > aChild(aList->item(a), uno::UNO_QUERY);
@@ -765,9 +765,9 @@ namespace
if (aList.is())
{
- const long nLength(aList->getLength());
+ const sal_Int32 nLength(aList->getLength());
- for (long a(0); a < nLength; a++)
+ for (sal_Int32 a(0); a < nLength; a++)
{
const uno::Reference< xml::dom::XElement > aChild(aList->item(a), uno::UNO_QUERY);
diff --git a/compilerplugins/clang/droplong.cxx b/compilerplugins/clang/droplong.cxx
new file mode 100644
index 000000000000..78129e78c323
--- /dev/null
+++ b/compilerplugins/clang/droplong.cxx
@@ -0,0 +1,123 @@
+/* -*- 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 <memory>
+#include <cassert>
+#include <string>
+#include <iostream>
+#include <fstream>
+#include <set>
+#include "plugin.hxx"
+#include "check.hxx"
+
+/**
+The types 'long' and 'unsigned long' are different sizes on different platforms, making them wholy unsuitable
+for portable code.
+And when I mean different sizes, I mean 64bit Linux and 64bit Windows have different sizes.
+ */
+namespace {
+
+static bool startswith(const std::string& rStr, const char* pSubStr) {
+ return rStr.compare(0, strlen(pSubStr), pSubStr) == 0;
+}
+
+class DropLong:
+ public RecursiveASTVisitor<DropLong>, public loplugin::Plugin
+{
+public:
+ explicit DropLong(InstantiationData const & data): Plugin(data) {}
+
+ virtual void run() override
+ {
+ std::string fn( compiler.getSourceManager().getFileEntryForID(
+ compiler.getSourceManager().getMainFileID())->getName() );
+ normalizeDotDotInFilePath(fn);
+ if (startswith(fn, SRCDIR "/sal/"))
+ return;
+ if (startswith(fn, SRCDIR "/desktop/unx/"))
+ return;
+ if (startswith(fn, SRCDIR "/bridges/"))
+ return;
+ TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
+ }
+
+ bool shouldVisitTemplateInstantiations () const { return true; }
+
+ bool VisitBinAssign(BinaryOperator const *);
+ bool VisitVarDecl(VarDecl const *);
+private:
+ bool check(QualType lhs, QualType rhs);
+};
+
+bool DropLong::VisitBinAssign(BinaryOperator const * expr)
+{
+ if (ignoreLocation(expr))
+ return true;
+ auto rhsType = expr->getRHS()->IgnoreCasts()->getType();
+ if (check(expr->getLHS()->getType(), rhsType))
+ {
+ report(
+ DiagnosticsEngine::Warning,
+ "rather replace long with %0",
+ expr->getExprLoc())
+ << rhsType
+ << expr->getSourceRange();
+// expr->getLHS()->getType()->dump();
+ }
+ return true;
+}
+
+bool DropLong::VisitVarDecl(VarDecl const * varDecl)
+{
+ if (ignoreLocation(varDecl))
+ return true;
+ if (!varDecl->hasInit())
+ return true;
+ auto rhsType = varDecl->getInit()->IgnoreCasts()->getType();
+ if (check(varDecl->getType(), rhsType))
+ {
+ report(
+ DiagnosticsEngine::Warning,
+ "rather replace long with %0",
+ varDecl->getLocation())
+ << rhsType
+ << varDecl->getSourceRange();
+// varDecl->getType()->dump();
+ }
+ return true;
+}
+
+bool DropLong::check(QualType lhs, QualType rhs)
+{
+ if (!lhs->isSpecificBuiltinType(BuiltinType::Kind::Long)
+ && !lhs->isSpecificBuiltinType(BuiltinType::Kind::ULong))
+ return false;
+
+ if (rhs->isSpecificBuiltinType(BuiltinType::Kind::Long)
+ || rhs->isSpecificBuiltinType(BuiltinType::Kind::ULong))
+ return false;
+
+ // Lots of stuff in the standard library and in sal/types.h is
+ // 'long' on Linux, so just ignore all typedefs.
+ if (loplugin::TypeCheck(lhs).Typedef())
+ return false;
+
+ if (isa<SubstTemplateTypeParmType>(lhs))
+ return false;
+ if (isa<AutoType>(lhs))
+ return false;
+
+ return true;
+}
+
+loplugin::Plugin::Registration< DropLong > X("droplong", false);
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/compilerplugins/clang/test/droplong.cxx b/compilerplugins/clang/test/droplong.cxx
new file mode 100644
index 000000000000..c38984342c24
--- /dev/null
+++ b/compilerplugins/clang/test/droplong.cxx
@@ -0,0 +1,20 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
+/*
+ * 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/.
+ */
+
+
+int main()
+{
+ int x = 1;
+ int y = 1;
+ long tmp = x + y; // expected-error {{rather replace long with 'int' [loplugin:droplong]}}
+ (void)tmp;
+ tmp = x + y; // expected-error {{rather replace long with 'int' [loplugin:droplong]}}
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/jurt/source/pipe/com_sun_star_lib_connections_pipe_PipeConnection.c b/jurt/source/pipe/com_sun_star_lib_connections_pipe_PipeConnection.c
index 5024274cb521..4652d64a5100 100644
--- a/jurt/source/pipe/com_sun_star_lib_connections_pipe_PipeConnection.c
+++ b/jurt/source/pipe/com_sun_star_lib_connections_pipe_PipeConnection.c
@@ -467,7 +467,7 @@ JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_writeJNI
short state = START;
oslPipe npipe; /* native pipe */
- long count; /* number of bytes has been written */
+ sal_Int32 count; /* number of bytes has been written */
jsize nwrite; /* number of bytes to write */
jbyte * nbuff = NULL; /* native buffer */
diff --git a/solenv/CompilerTest_compilerplugins_clang.mk b/solenv/CompilerTest_compilerplugins_clang.mk
index c19a9134dc72..a5ec62024a0e 100644
--- a/solenv/CompilerTest_compilerplugins_clang.mk
+++ b/solenv/CompilerTest_compilerplugins_clang.mk
@@ -15,6 +15,7 @@ $(eval $(call gb_CompilerTest_add_exception_objects,compilerplugins_clang, \
compilerplugins/clang/test/cppunitassertequals \
compilerplugins/clang/test/deadclass \
compilerplugins/clang/test/datamembershadow \
+ compilerplugins/clang/test/droplong \
compilerplugins/clang/test/externvar \
compilerplugins/clang/test/finalprotected \
compilerplugins/clang/test/loopvartoosmall \
diff --git a/soltools/cpp/_eval.c b/soltools/cpp/_eval.c
index e1eceb204371..498fda740e89 100644
--- a/soltools/cpp/_eval.c
+++ b/soltools/cpp/_eval.c
@@ -28,7 +28,7 @@
struct value
{
- long val;
+ int val;
int type;
};
@@ -407,7 +407,7 @@ int
{
struct value v1;
struct value v2 = { 0, UND };
- long rv1, rv2;
+ int rv1, rv2;
int rtype, oper;
rv2 = 0;
@@ -620,7 +620,7 @@ struct value
struct value v;
Nlist *np;
int i, base;
- unsigned long n;
+ unsigned int n;
uchar *p, c;
v.type = SGN;
diff --git a/soltools/mkdepend/def.h b/soltools/mkdepend/def.h
index 2ab9a4845770..587cd1580c2c 100644
--- a/soltools/mkdepend/def.h
+++ b/soltools/mkdepend/def.h
@@ -138,7 +138,7 @@ struct filepointer {
char *f_p;
char *f_base;
char *f_end;
- long f_line;
+ int f_line;
};
#ifndef X_NOT_STDC_ENV