diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2019-06-13 11:58:52 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2019-06-13 13:39:08 +0200 |
commit | b6086804af2b112f529952852ac55a2ffe7bd931 (patch) | |
tree | 84b0a77c12c22c52167423134b0468d731957bef /compilerplugins | |
parent | f7157f04fab298423e2c4f6a7e5f8e361164b15f (diff) |
new loplugin logexceptionnicely
to be used to find places to use new TOOLS_WARN_EXCEPTION,etc macros
Change-Id: I213ab47efa82075435bb800736ee0937aea0486c
Reviewed-on: https://gerrit.libreoffice.org/73950
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins')
-rw-r--r-- | compilerplugins/clang/logexceptionnicely.cxx | 136 | ||||
-rw-r--r-- | compilerplugins/clang/stringconcat.cxx | 11 | ||||
-rw-r--r-- | compilerplugins/clang/test/logexceptionnicely.cxx | 52 |
3 files changed, 199 insertions, 0 deletions
diff --git a/compilerplugins/clang/logexceptionnicely.cxx b/compilerplugins/clang/logexceptionnicely.cxx new file mode 100644 index 000000000000..efcf7060bf70 --- /dev/null +++ b/compilerplugins/clang/logexceptionnicely.cxx @@ -0,0 +1,136 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * Based on LLVM/Clang. + * + * This file is distributed under the University of Illinois Open Source + * License. See LICENSE.TXT for details. + * + */ + +#include "plugin.hxx" +#include "check.hxx" +#include "compat.hxx" +#include <fstream> +#include <unordered_set> + +namespace loplugin +{ +/* +Check that we are using exceptionToString when printing exceptions inside SAL_WARN, so that we +get nicely formatted exception details in our logs. +*/ + +class LogExceptionNicely : public loplugin::FilteringPlugin<LogExceptionNicely> +{ + std::unordered_set<SourceLocation> m_visited; + +public: + LogExceptionNicely(const InstantiationData& data) + : FilteringPlugin(data) + { + } + + void run() + { + std::string fn(handler.getMainFileName()); + loplugin::normalizeDotDotInFilePath(fn); + // these are below tools in the module hierarchy, so we can't use the pretty printing + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/cppuhelper/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/ucbhelper/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/binaryurp/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/comphelper/")) + return; + // can't do that here, don't have an Any + if (loplugin::hasPathnamePrefix(fn, SRCDIR + "/connectivity/source/drivers/hsqldb/HStorageMap.cxx")) + return; + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + + static bool BaseCheckNotExceptionSubclass(const CXXRecordDecl* BaseDefinition) + { + if (!BaseDefinition) + return true; + auto tc = loplugin::TypeCheck(BaseDefinition); + if (tc.Class("Exception") + .Namespace("uno") + .Namespace("star") + .Namespace("sun") + .Namespace("com") + .GlobalNamespace()) + return false; + return true; + } + + bool isDerivedFromException(const CXXRecordDecl* decl) + { + if (!decl || !decl->hasDefinition()) + return false; + auto tc = loplugin::TypeCheck(decl); + if (tc.Class("Exception") + .Namespace("uno") + .Namespace("star") + .Namespace("sun") + .Namespace("com") + .GlobalNamespace()) + return true; + if ( // not sure what hasAnyDependentBases() does, + // but it avoids classes we don't want, e.g. WeakAggComponentImplHelper1 + !decl->hasAnyDependentBases() + && !decl->forallBases(BaseCheckNotExceptionSubclass, true)) + { + return true; + } + return false; + } + + bool VisitCXXOperatorCallExpr(const CXXOperatorCallExpr* operatorCallExpr) + { + if (ignoreLocation(operatorCallExpr)) + return true; + + StringRef fn = getFileNameOfSpellingLoc( + compiler.getSourceManager().getExpansionLoc(compat::getBeginLoc(operatorCallExpr))); + // these are below tools in the module hierarchy, so we can't use the pretty printing + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/include/comphelper/")) + return true; + + if (operatorCallExpr->getOperator() != OO_LessLess) + return true; + auto expr = operatorCallExpr->getArg(1)->IgnoreImplicit(); + if (auto declRefExpr = dyn_cast<DeclRefExpr>(expr)) + if (auto varDecl = dyn_cast<VarDecl>(declRefExpr->getDecl())) + { + const clang::Type* type = varDecl->getType()->getUnqualifiedDesugaredType(); + const CXXRecordDecl* cxxRecordDecl = type->getAsCXXRecordDecl(); + if (!cxxRecordDecl) + cxxRecordDecl = type->getPointeeCXXRecordDecl(); + if (!cxxRecordDecl) + return true; + if (!isDerivedFromException(cxxRecordDecl)) + return true; + auto loc = compat::getBeginLoc(operatorCallExpr); + // for some reason, I'm warning multiple times? so just check if I've warned already + if (!m_visited.insert(compiler.getSourceManager().getExpansionLoc(loc)).second) + return true; + report(DiagnosticsEngine::Warning, + "use TOOLS_WARN_EXCEPTION/TOOLS_INFO_EXCEPTION/exceptionToString to print " + "exception nicely", + loc) + << operatorCallExpr->getSourceRange(); + return true; + } + return true; + } +}; + +static Plugin::Registration<LogExceptionNicely> X("logexceptionnicely", false); + +} // namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/stringconcat.cxx b/compilerplugins/clang/stringconcat.cxx index 9cef9866db61..da4b212e9f73 100644 --- a/compilerplugins/clang/stringconcat.cxx +++ b/compilerplugins/clang/stringconcat.cxx @@ -100,6 +100,17 @@ bool StringConcat::VisitCallExpr(CallExpr const * expr) { } leftLoc = compat::getBeginLoc(left->getArg(1)); } + + // We add an extra " " in the TOOLS_WARN_EXCEPTION macro, which triggers this plugin + if (loplugin::isSamePathname( + compiler.getSourceManager().getFilename( + compiler.getSourceManager().getSpellingLoc( + compiler.getSourceManager().getImmediateMacroCallerLoc( + compiler.getSourceManager().getImmediateMacroCallerLoc( + compat::getBeginLoc(expr))))), + SRCDIR "/include/tools/diagnose_ex.h")) + return true; + StringRef name { getFileNameOfSpellingLoc( compiler.getSourceManager().getSpellingLoc(compat::getBeginLoc(expr))) }; diff --git a/compilerplugins/clang/test/logexceptionnicely.cxx b/compilerplugins/clang/test/logexceptionnicely.cxx new file mode 100644 index 000000000000..7348ae3e7415 --- /dev/null +++ b/compilerplugins/clang/test/logexceptionnicely.cxx @@ -0,0 +1,52 @@ +/* -*- 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 <tools/diagnose_ex.h> +#include <sal/log.hxx> +#include <com/sun/star/lang/IndexOutOfBoundsException.hpp> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/uno/Any.hxx> + +void func1(); + +int main() +{ + // no warning excepted + try + { + func1(); + } + catch (css::uno::Exception const&) + { + css::uno::Any ex(cppu::getCaughtException()); + SAL_WARN("avmedia", "exception: " << exceptionToString(ex)); + } + + try + { + func1(); + } + catch (css::uno::Exception const& ex) + { + SAL_WARN("xmloff", "message " << ex); + // expected-error@-1 {{use TOOLS_WARN_EXCEPTION/TOOLS_INFO_EXCEPTION/exceptionToString to print exception nicely [loplugin:logexceptionnicely]}} + } + + try + { + func1(); + } + catch (const css::lang::IndexOutOfBoundsException& ex) + { + SAL_WARN("xmloff", "message " << ex); + // expected-error@-1 {{use TOOLS_WARN_EXCEPTION/TOOLS_INFO_EXCEPTION/exceptionToString to print exception nicely [loplugin:logexceptionnicely]}} + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |