summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2017-03-20 09:01:33 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2017-03-22 06:47:35 +0000
commit7299481834b15c920f996f4b0f3b5f821a82a10d (patch)
tree6cbc8a64399046dd2c83e4a4ef778c65ec00a34a /compilerplugins
parente9c7d259e8ed3144d4226aef7c3de351e4706b79 (diff)
loplugin:redundantcast find redundant c-style enum casts
Change-Id: I2dab376d87804521aed6b6bd41ad7762830fa349 Reviewed-on: https://gerrit.libreoffice.org/35467 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/check.cxx10
-rw-r--r--compilerplugins/clang/check.hxx2
-rw-r--r--compilerplugins/clang/redundantcast.cxx20
-rw-r--r--compilerplugins/clang/test/redundantcast.cxx5
4 files changed, 37 insertions, 0 deletions
diff --git a/compilerplugins/clang/check.cxx b/compilerplugins/clang/check.cxx
index 22f58ebd7ac9..f7647ac8c9c9 100644
--- a/compilerplugins/clang/check.cxx
+++ b/compilerplugins/clang/check.cxx
@@ -103,6 +103,16 @@ TypeCheck TypeCheck::Pointer() const {
return TypeCheck();
}
+TerminalCheck TypeCheck::Enum() const {
+ if (!type_.isNull()) {
+ auto const t = type_->getAs<clang::EnumType>();
+ if (t != nullptr) {
+ return TerminalCheck(true);
+ }
+ }
+ return TerminalCheck(false);
+}
+
TypeCheck TypeCheck::Typedef() const {
if (!type_.isNull()) {
if (auto const t = type_->getAs<clang::TypedefType>()) {
diff --git a/compilerplugins/clang/check.hxx b/compilerplugins/clang/check.hxx
index 8c3edf773ea1..cf91dfd2aee2 100644
--- a/compilerplugins/clang/check.hxx
+++ b/compilerplugins/clang/check.hxx
@@ -53,6 +53,8 @@ public:
TypeCheck Pointer() const;
+ TerminalCheck Enum() const;
+
TypeCheck LvalueReference() const;
template<std::size_t N> inline ContextCheck Class(char const (& id)[N])
diff --git a/compilerplugins/clang/redundantcast.cxx b/compilerplugins/clang/redundantcast.cxx
index 0525fa51f584..4bef116f481b 100644
--- a/compilerplugins/clang/redundantcast.cxx
+++ b/compilerplugins/clang/redundantcast.cxx
@@ -138,6 +138,8 @@ public:
bool VisitCXXDeleteExpr(CXXDeleteExpr const * expr);
+ bool VisitCStyleCastExpr(CStyleCastExpr const * expr);
+
bool VisitBinSub(BinaryOperator const * expr)
{ return visitBinOp(expr); }
@@ -288,6 +290,24 @@ bool RedundantCast::VisitImplicitCastExpr(const ImplicitCastExpr * expr) {
return true;
}
+bool RedundantCast::VisitCStyleCastExpr(CStyleCastExpr const * expr) {
+ if (ignoreLocation(expr)) {
+ return true;
+ }
+ if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(expr->getLocStart()))) {
+ return true;
+ }
+ auto t1 = getSubExprAsWritten(expr)->getType();
+ auto t2 = expr->getTypeAsWritten();
+ if (loplugin::TypeCheck(t1).Enum() && loplugin::TypeCheck(t2).Enum() && t1 == t2) {
+ report(
+ DiagnosticsEngine::Warning,
+ "redundant cstyle enum cast from %0 to %1", expr->getExprLoc())
+ << t1 << t2 << expr->getSourceRange();
+ }
+ return true;
+}
+
bool RedundantCast::VisitCXXStaticCastExpr(CXXStaticCastExpr const * expr) {
if (ignoreLocation(expr)) {
return true;
diff --git a/compilerplugins/clang/test/redundantcast.cxx b/compilerplugins/clang/test/redundantcast.cxx
index e8be3013cd86..a7516c1a058f 100644
--- a/compilerplugins/clang/test/redundantcast.cxx
+++ b/compilerplugins/clang/test/redundantcast.cxx
@@ -10,6 +10,8 @@
void f1(char *) {}
void f2(char const *) {}
+enum Enum1 { X };
+
int main() {
char * p1;
char const * p2;
@@ -27,6 +29,9 @@ int main() {
f2(const_cast<char * const>(p2)); // expected-error {{redundant const_cast from 'const char *' to 'char *', result is implictly cast to 'const char *' [loplugin:redundantcast]}}
f2(const_cast<char const *>(p2)); // expected-error {{redundant const_cast from 'const char *' to 'const char *' [loplugin:redundantcast]}}
f2(const_cast<char const * const>(p2)); // expected-error {{redundant const_cast from 'const char *' to 'const char *const' [loplugin:redundantcast]}}
+
+ Enum1 e = (Enum1)Enum1::X; // expected-error {{redundant cstyle enum cast from 'Enum1' to 'Enum1' [loplugin:redundantcast]}}
+ (void)e;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */