bump product version to 7.2.5.1
[LibreOffice.git] / compilerplugins / clang / logexceptionnicely.cxx
bloba262d276b88c35f316b76a9505a0c638ff05b7b0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * Based on LLVM/Clang.
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
13 #include "plugin.hxx"
14 #include "check.hxx"
15 #include "compat.hxx"
16 #include <fstream>
17 #include <unordered_set>
19 namespace loplugin
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;
30 public:
31 LogExceptionNicely(const InstantiationData& data)
32 : FilteringPlugin(data)
36 bool preRun()
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/"))
42 return false;
43 if (loplugin::hasPathnamePrefix(fn, SRCDIR "/ucbhelper/"))
44 return false;
45 if (loplugin::hasPathnamePrefix(fn, SRCDIR "/binaryurp/"))
46 return false;
47 if (loplugin::hasPathnamePrefix(fn, SRCDIR "/comphelper/"))
48 return false;
49 // can't do that here, don't have an Any
50 if (loplugin::hasPathnamePrefix(fn, SRCDIR
51 "/connectivity/source/drivers/hsqldb/HStorageMap.cxx"))
52 return false;
53 return true;
56 void run()
58 if (preRun())
59 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
62 static bool BaseCheckNotExceptionSubclass(const CXXRecordDecl* BaseDefinition)
64 if (!BaseDefinition)
65 return true;
66 auto tc = loplugin::TypeCheck(BaseDefinition);
67 if (tc.Class("Exception")
68 .Namespace("uno")
69 .Namespace("star")
70 .Namespace("sun")
71 .Namespace("com")
72 .GlobalNamespace())
73 return false;
74 return true;
77 bool isDerivedFromException(const CXXRecordDecl* decl)
79 if (!decl || !decl->hasDefinition())
80 return false;
81 auto tc = loplugin::TypeCheck(decl);
82 if (tc.Class("Exception")
83 .Namespace("uno")
84 .Namespace("star")
85 .Namespace("sun")
86 .Namespace("com")
87 .GlobalNamespace())
88 return true;
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))
93 return true;
95 return false;
98 bool VisitCXXOperatorCallExpr(const CXXOperatorCallExpr* operatorCallExpr)
100 if (ignoreLocation(operatorCallExpr))
101 return true;
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/"))
107 return true;
109 if (operatorCallExpr->getOperator() != OO_LessLess)
110 return true;
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();
117 if (!cxxRecordDecl)
118 cxxRecordDecl = type->getPointeeCXXRecordDecl();
119 if (!cxxRecordDecl)
120 return true;
121 if (!isDerivedFromException(cxxRecordDecl))
122 return true;
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)
126 return true;
127 report(DiagnosticsEngine::Warning,
128 "use TOOLS_WARN_EXCEPTION/TOOLS_INFO_EXCEPTION/exceptionToString to print "
129 "exception nicely",
130 loc)
131 << operatorCallExpr->getSourceRange();
132 return true;
134 return true;
138 static Plugin::Registration<LogExceptionNicely> logexceptionnicely("logexceptionnicely");
140 } // namespace
142 #endif // LO_CLANG_SHARED_PLUGINS
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */