update credits
[LibreOffice.git] / compilerplugins / clang / simplifyconstruct.cxx
blobd9eff0b3af89288d82084c969e9bb27ed6879f80
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 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #ifndef LO_CLANG_SHARED_PLUGINS
12 #include <memory>
13 #include <cassert>
14 #include <string>
15 #include <iostream>
16 #include <fstream>
17 #include <set>
18 #include "plugin.hxx"
19 #include "check.hxx"
21 namespace
23 class SimplifyConstruct : public loplugin::FilteringPlugin<SimplifyConstruct>
25 public:
26 explicit SimplifyConstruct(loplugin::InstantiationData const& data)
27 : FilteringPlugin(data)
31 virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
33 bool VisitCXXConstructExpr(CXXConstructExpr const*);
34 bool VisitVarDecl(VarDecl const*);
36 // ignore some contexts within which nullptr is fine
37 bool TraverseReturnStmt(ReturnStmt*) { return true; }
38 bool TraverseInitListExpr(InitListExpr*) { return true; }
39 bool TraverseCXXBindTemporaryExpr(CXXBindTemporaryExpr*) { return true; }
40 // ignore them for the shared visitor too
41 bool PreTraverseReturnStmt(ReturnStmt*) { return false; }
42 bool PreTraverseInitListExpr(InitListExpr*) { return false; }
43 bool PreTraverseCXXBindTemporaryExpr(CXXBindTemporaryExpr*) { return false; }
46 bool SimplifyConstruct::VisitCXXConstructExpr(CXXConstructExpr const* constructExpr)
48 if (ignoreLocation(constructExpr))
49 return true;
50 auto tc = loplugin::TypeCheck(constructExpr->getType());
51 if (!tc.Class("unique_ptr").StdNamespace() && !tc.Class("shared_ptr").StdNamespace()
52 && !tc.Class("SvRef").Namespace("tools").GlobalNamespace()
53 && !tc.Class("Reference").Namespace("rtl").GlobalNamespace()
54 && !tc.Class("Reference")
55 .Namespace("uno")
56 .Namespace("star")
57 .Namespace("sun")
58 .Namespace("com")
59 .GlobalNamespace())
60 return true;
61 if (constructExpr->getNumArgs() == 1
62 && isa<CXXNullPtrLiteralExpr>(constructExpr->getArg(0)->IgnoreParenImpCasts()))
64 report(DiagnosticsEngine::Warning,
65 "no need to explicitly init an instance of %0 with nullptr, just use default "
66 "constructor",
67 constructExpr->getSourceRange().getBegin())
68 << constructExpr->getType() << constructExpr->getSourceRange();
70 return true;
73 bool SimplifyConstruct::VisitVarDecl(VarDecl const* varDecl)
75 if (ignoreLocation(varDecl))
76 return true;
77 // cannot use OUString s("xxx") style syntax in a parameter
78 if (isa<ParmVarDecl>(varDecl))
79 return true;
80 varDecl = varDecl->getCanonicalDecl();
81 if (!varDecl->getInit())
82 return true;
83 if (varDecl->getInitStyle() != VarDecl::InitializationStyle::CInit)
84 return true;
85 if (!varDecl->getType()->isRecordType())
86 return true;
87 if (isa<AutoType>(varDecl->getType()))
88 return true;
90 auto init = varDecl->getInit();
91 auto const e1 = init->IgnoreImplicit();
92 if (!isa<CXXFunctionalCastExpr>(e1) && !isa<CXXTemporaryObjectExpr>(e1))
93 return true;
95 // e.g. the LANGUAGE_DONTKNOW defines
96 if (compiler.getSourceManager().isMacroBodyExpansion(init->getBeginLoc()))
97 return true;
99 report(DiagnosticsEngine::Warning, "simplify", varDecl->getLocation())
100 << varDecl->getSourceRange();
102 return true;
105 loplugin::Plugin::Registration<SimplifyConstruct> simplifyconstruct("simplifyconstruct", true);
108 #endif // LO_CLANG_SHARED_PLUGINS
110 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */