1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
7 * This file is distributed under the University of Illinois Open Source
8 * License. See LICENSE.TXT for details.
11 #ifndef LO_CLANG_SHARED_PLUGINS
15 #include "config_clang.h"
17 #include <unordered_set>
22 Check that we are using exceptionToString when printing exceptions inside SAL_WARN, so that we
23 get nicely formatted exception details in our logs.
26 class LogExceptionNicely
: public loplugin::FilteringPlugin
<LogExceptionNicely
>
28 std::unordered_set
<SourceLocation
> m_visited
;
31 LogExceptionNicely(const InstantiationData
& data
)
32 : FilteringPlugin(data
)
38 std::string
fn(handler
.getMainFileName());
39 loplugin::normalizeDotDotInFilePath(fn
);
40 // these are below tools in the module hierarchy, so we can't use the pretty printing
41 if (loplugin::hasPathnamePrefix(fn
, SRCDIR
"/cppuhelper/"))
43 if (loplugin::hasPathnamePrefix(fn
, SRCDIR
"/ucbhelper/"))
45 if (loplugin::hasPathnamePrefix(fn
, SRCDIR
"/binaryurp/"))
47 if (loplugin::hasPathnamePrefix(fn
, SRCDIR
"/comphelper/"))
49 if (loplugin::hasPathnamePrefix(fn
, SRCDIR
"/io/"))
51 if (loplugin::hasPathnamePrefix(fn
, SRCDIR
"/javaunohelper/"))
53 if (loplugin::hasPathnamePrefix(fn
, SRCDIR
"/stoc/"))
55 // can't do that here, don't have an Any
56 if (loplugin::hasPathnamePrefix(fn
, SRCDIR
57 "/connectivity/source/drivers/hsqldb/HStorageMap.cxx"))
65 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
68 static bool BaseCheckNotExceptionSubclass(const CXXRecordDecl
* BaseDefinition
)
72 auto tc
= loplugin::TypeCheck(BaseDefinition
);
73 if (tc
.Class("Exception")
83 bool isDerivedFromException(const CXXRecordDecl
* decl
)
85 if (!decl
|| !decl
->hasDefinition())
87 auto tc
= loplugin::TypeCheck(decl
);
88 if (tc
.Class("Exception")
95 if ( // not sure what hasAnyDependentBases() does,
96 // but it avoids classes we don't want, e.g. WeakAggComponentImplHelper1
97 !decl
->hasAnyDependentBases() && !decl
->forallBases(BaseCheckNotExceptionSubclass
))
104 bool VisitCXXOperatorCallExpr(const CXXOperatorCallExpr
* operatorCallExpr
)
106 if (ignoreLocation(operatorCallExpr
))
109 StringRef fn
= getFilenameOfLocation(
110 compiler
.getSourceManager().getExpansionLoc(operatorCallExpr
->getBeginLoc()));
111 // these are below tools in the module hierarchy, so we can't use the pretty printing
112 if (loplugin::hasPathnamePrefix(fn
, SRCDIR
"/include/comphelper/"))
115 if (operatorCallExpr
->getOperator() != OO_LessLess
)
117 auto expr
= operatorCallExpr
->getArg(1)->IgnoreImplicit();
118 if (auto declRefExpr
= dyn_cast
<DeclRefExpr
>(expr
))
119 if (auto varDecl
= dyn_cast
<VarDecl
>(declRefExpr
->getDecl()))
121 const clang::Type
* type
= varDecl
->getType()->getUnqualifiedDesugaredType();
122 const CXXRecordDecl
* cxxRecordDecl
= type
->getAsCXXRecordDecl();
124 cxxRecordDecl
= type
->getPointeeCXXRecordDecl();
127 if (!isDerivedFromException(cxxRecordDecl
))
129 auto loc
= operatorCallExpr
->getBeginLoc();
130 // for some reason, I'm warning multiple times? so just check if I've warned already
131 if (!m_visited
.insert(compiler
.getSourceManager().getExpansionLoc(loc
)).second
)
133 report(DiagnosticsEngine::Warning
,
134 "use TOOLS_WARN_EXCEPTION/TOOLS_INFO_EXCEPTION/exceptionToString to print "
137 << operatorCallExpr
->getSourceRange();
144 static Plugin::Registration
<LogExceptionNicely
> logexceptionnicely("logexceptionnicely");
148 #endif // LO_CLANG_SHARED_PLUGINS
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */