diff --git a/compilerplugins/clang/implicitboolconversion.cxx b/compilerplugins/clang/implicitboolconversion.cxx index e16b15517bc9..3adbf7197f47 100644 --- a/compilerplugins/clang/implicitboolconversion.cxx +++ b/compilerplugins/clang/implicitboolconversion.cxx @@ -1020,6 +1020,19 @@ void ImplicitBoolConversion::checkCXXConstructExpr( void ImplicitBoolConversion::reportWarning(ImplicitCastExpr const * expr) { if (compiler.getLangOpts().CPlusPlus) { + if (expr->getCastKind() == CK_ConstructorConversion) { + auto const t1 = expr->getType(); + if (auto const t2 = t1->getAs()) { + assert(t2->getNumArgs() >= 1); + auto const a = t2->getArg(0); + if (a.getKind() == TemplateArgument::Type && a.getAsType()->isBooleanType() + && (loplugin::TypeCheck(t1).TemplateSpecializationClass() + .ClassOrStruct("atomic").StdNamespace())) + { + return; + } + } + } report( DiagnosticsEngine::Warning, "implicit conversion (%0) from %1 to %2", compat::getBeginLoc(expr)) diff --git a/compilerplugins/clang/test/implicitboolconversion.cxx b/compilerplugins/clang/test/implicitboolconversion.cxx new file mode 100644 index 000000000000..c438822d3b88 --- /dev/null +++ b/compilerplugins/clang/test/implicitboolconversion.cxx @@ -0,0 +1,28 @@ +/* -*- 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/. + */ + +#include + +#include + +void f() +{ + // expected-error@+1 {{implicit conversion (IntegralCast) from 'bool' to 'int' [loplugin:implicitboolconversion]}} + int i = false; + (void)i; + std::atomic b = false; + (void)b; + //TODO: Emit only one diagnostic here: + // expected-error@+2 {{implicit conversion (ConstructorConversion) from 'bool' to 'std::atomic' [loplugin:implicitboolconversion]}} + // expected-error-re@+1 {{implicit conversion (IntegralCast) from 'bool' to {{.+}} [loplugin:implicitboolconversion]}} + std::atomic a = false; + (void)a; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/solenv/CompilerTest_compilerplugins_clang.mk b/solenv/CompilerTest_compilerplugins_clang.mk index 318bf8c69d0a..3c9969bd628f 100644 --- a/solenv/CompilerTest_compilerplugins_clang.mk +++ b/solenv/CompilerTest_compilerplugins_clang.mk @@ -41,6 +41,7 @@ $(eval $(call gb_CompilerTest_add_exception_objects,compilerplugins_clang, \ compilerplugins/clang/test/flatten \ compilerplugins/clang/test/fragiledestructor \ compilerplugins/clang/test/getstr \ + compilerplugins/clang/test/implicitboolconversion \ compilerplugins/clang/test/indentation \ compilerplugins/clang/test/intvsfloat \ compilerplugins/clang/test/logexceptionnicely \