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
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 // can't do that here, don't have an Any
50 if (loplugin::hasPathnamePrefix(fn
, SRCDIR
51 "/connectivity/source/drivers/hsqldb/HStorageMap.cxx"))
59 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
62 static bool BaseCheckNotExceptionSubclass(const CXXRecordDecl
* BaseDefinition
)
66 auto tc
= loplugin::TypeCheck(BaseDefinition
);
67 if (tc
.Class("Exception")
77 bool isDerivedFromException(const CXXRecordDecl
* decl
)
79 if (!decl
|| !decl
->hasDefinition())
81 auto tc
= loplugin::TypeCheck(decl
);
82 if (tc
.Class("Exception")
89 if ( // not sure what hasAnyDependentBases() does,
90 // but it avoids classes we don't want, e.g. WeakAggComponentImplHelper1
91 !decl
->hasAnyDependentBases() && !decl
->forallBases(BaseCheckNotExceptionSubclass
))
98 bool VisitCXXOperatorCallExpr(const CXXOperatorCallExpr
* operatorCallExpr
)
100 if (ignoreLocation(operatorCallExpr
))
103 StringRef fn
= getFilenameOfLocation(
104 compiler
.getSourceManager().getExpansionLoc(compat::getBeginLoc(operatorCallExpr
)));
105 // these are below tools in the module hierarchy, so we can't use the pretty printing
106 if (loplugin::hasPathnamePrefix(fn
, SRCDIR
"/include/comphelper/"))
109 if (operatorCallExpr
->getOperator() != OO_LessLess
)
111 auto expr
= operatorCallExpr
->getArg(1)->IgnoreImplicit();
112 if (auto declRefExpr
= dyn_cast
<DeclRefExpr
>(expr
))
113 if (auto varDecl
= dyn_cast
<VarDecl
>(declRefExpr
->getDecl()))
115 const clang::Type
* type
= varDecl
->getType()->getUnqualifiedDesugaredType();
116 const CXXRecordDecl
* cxxRecordDecl
= type
->getAsCXXRecordDecl();
118 cxxRecordDecl
= type
->getPointeeCXXRecordDecl();
121 if (!isDerivedFromException(cxxRecordDecl
))
123 auto loc
= compat::getBeginLoc(operatorCallExpr
);
124 // for some reason, I'm warning multiple times? so just check if I've warned already
125 if (!m_visited
.insert(compiler
.getSourceManager().getExpansionLoc(loc
)).second
)
127 report(DiagnosticsEngine::Warning
,
128 "use TOOLS_WARN_EXCEPTION/TOOLS_INFO_EXCEPTION/exceptionToString to print "
131 << operatorCallExpr
->getSourceRange();
138 static Plugin::Registration
<LogExceptionNicely
> logexceptionnicely("logexceptionnicely");
142 #endif // LO_CLANG_SHARED_PLUGINS
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */