tdf#130857 qt weld: Support "Insert Breaks" dialog
[LibreOffice.git] / compilerplugins / clang / logexceptionnicely.cxx
blob6cc66fdf02890dda9f1a7d47b0aeeed344e763c3
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 "config_clang.h"
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 if (loplugin::hasPathnamePrefix(fn, SRCDIR "/io/"))
50 return false;
51 if (loplugin::hasPathnamePrefix(fn, SRCDIR "/javaunohelper/"))
52 return false;
53 if (loplugin::hasPathnamePrefix(fn, SRCDIR "/stoc/"))
54 return false;
55 // can't do that here, don't have an Any
56 if (loplugin::hasPathnamePrefix(fn, SRCDIR
57 "/connectivity/source/drivers/hsqldb/HStorageMap.cxx"))
58 return false;
59 return true;
62 void run()
64 if (preRun())
65 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
68 static bool BaseCheckNotExceptionSubclass(const CXXRecordDecl* BaseDefinition)
70 if (!BaseDefinition)
71 return true;
72 auto tc = loplugin::TypeCheck(BaseDefinition);
73 if (tc.Class("Exception")
74 .Namespace("uno")
75 .Namespace("star")
76 .Namespace("sun")
77 .Namespace("com")
78 .GlobalNamespace())
79 return false;
80 return true;
83 bool isDerivedFromException(const CXXRecordDecl* decl)
85 if (!decl || !decl->hasDefinition())
86 return false;
87 auto tc = loplugin::TypeCheck(decl);
88 if (tc.Class("Exception")
89 .Namespace("uno")
90 .Namespace("star")
91 .Namespace("sun")
92 .Namespace("com")
93 .GlobalNamespace())
94 return true;
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))
99 return true;
101 return false;
104 bool VisitCXXOperatorCallExpr(const CXXOperatorCallExpr* operatorCallExpr)
106 if (ignoreLocation(operatorCallExpr))
107 return true;
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/"))
113 return true;
115 if (operatorCallExpr->getOperator() != OO_LessLess)
116 return true;
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();
123 if (!cxxRecordDecl)
124 cxxRecordDecl = type->getPointeeCXXRecordDecl();
125 if (!cxxRecordDecl)
126 return true;
127 if (!isDerivedFromException(cxxRecordDecl))
128 return true;
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)
132 return true;
133 report(DiagnosticsEngine::Warning,
134 "use TOOLS_WARN_EXCEPTION/TOOLS_INFO_EXCEPTION/exceptionToString to print "
135 "exception nicely",
136 loc)
137 << operatorCallExpr->getSourceRange();
138 return true;
140 return true;
144 static Plugin::Registration<LogExceptionNicely> logexceptionnicely("logexceptionnicely");
146 } // namespace
148 #endif // LO_CLANG_SHARED_PLUGINS
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */