summaryrefslogtreecommitdiff
path: root/compilerplugins/clang/trivialconstructor.cxx
blob: c991d71ae70342afe904bc42a8ff63dea36129b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <cassert>
#include <string>
#include <iostream>
#include <unordered_map>
#include <unordered_set>

#include "plugin.hxx"
#include "check.hxx"
#include "config_clang.h"
#include "clang/AST/CXXInheritance.h"
#include "clang/AST/StmtVisitor.h"

// Look for declared constructors that can be trivial (and therefore don't need to be declared)

namespace
{
class TrivialConstructor : public loplugin::FilteringPlugin<TrivialConstructor>
{
public:
    explicit TrivialConstructor(loplugin::InstantiationData const& data)
        : FilteringPlugin(data)
    {
    }

    virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }

    bool VisitCXXConstructorDecl(CXXConstructorDecl const*);

private:
    bool HasTrivialConstructorBody(const CXXRecordDecl* BaseClassDecl,
                                   const CXXRecordDecl* MostDerivedClassDecl);
    bool FieldHasTrivialConstructorBody(const FieldDecl* Field);
};

bool TrivialConstructor::VisitCXXConstructorDecl(CXXConstructorDecl const* constructorDecl)
{
    if (ignoreLocation(constructorDecl))
        return true;
    if (!constructorDecl->hasTrivialBody())
        return true;
    if (constructorDecl->isExplicit())
        return true;
    if (!constructorDecl->isDefaultConstructor())
        return true;
    if (constructorDecl->getNumParams() != 0)
        return true;
    if (!constructorDecl->inits().empty())
        return true;
    if (constructorDecl->getExceptionSpecType() != EST_None)
        return true;
    if (constructorDecl->getAccess() != AS_public)
        return true;
    if (!constructorDecl->isThisDeclarationADefinition())
        return true;
    if (isInUnoIncludeFile(
            compiler.getSourceManager().getSpellingLoc(constructorDecl->getLocation())))
        return true;
    const CXXRecordDecl* recordDecl = constructorDecl->getParent();
    if (std::distance(recordDecl->ctor_begin(), recordDecl->ctor_end()) != 1)
        return true;
    // Constructor templates are not included in ctor_begin()..ctor_end() above, so also do a slow
    // check across all decls():
    for (auto d : recordDecl->decls())
    {
        if (auto const d2 = dyn_cast<FunctionTemplateDecl>(d))
        {
            if (isa<CXXConstructorDecl>(d2->getTemplatedDecl()))
            {
                return true;
            }
        }
    }
    if (!HasTrivialConstructorBody(recordDecl, recordDecl))
        return true;

    report(DiagnosticsEngine::Warning, "no need for explicit constructor decl",
           constructorDecl->getLocation())
        << constructorDecl->getSourceRange();
    if (constructorDecl->getCanonicalDecl() != constructorDecl)
    {
        constructorDecl = constructorDecl->getCanonicalDecl();
        report(DiagnosticsEngine::Warning, "no need for explicit constructor decl",
               constructorDecl->getLocation())
            << constructorDecl->getSourceRange();
    }
    return true;
}

bool TrivialConstructor::HasTrivialConstructorBody(const CXXRecordDecl* BaseClassDecl,
                                                   const CXXRecordDecl* MostDerivedClassDecl)
{
    if (BaseClassDecl != MostDerivedClassDecl && !BaseClassDecl->hasTrivialDefaultConstructor())
        return false;

    // Check fields.
    for (const auto* field : BaseClassDecl->fields())
        if (!FieldHasTrivialConstructorBody(field))
            return false;

    // Check non-virtual bases.
    for (const auto& I : BaseClassDecl->bases())
    {
        if (I.isVirtual())
            continue;
        if (!I.getType()->isRecordType())
            continue;
        const CXXRecordDecl* NonVirtualBase = I.getType()->getAsCXXRecordDecl();
        if (NonVirtualBase && !HasTrivialConstructorBody(NonVirtualBase, MostDerivedClassDecl))
            return false;
    }

    if (BaseClassDecl == MostDerivedClassDecl)
    {
        // Check virtual bases.
        for (const auto& I : BaseClassDecl->vbases())
        {
            if (!I.getType()->isRecordType())
                continue;
            const CXXRecordDecl* VirtualBase = I.getType()->getAsCXXRecordDecl();
            if (VirtualBase && !HasTrivialConstructorBody(VirtualBase, MostDerivedClassDecl))
                return false;
        }
    }
    return true;
}

bool TrivialConstructor::FieldHasTrivialConstructorBody(const FieldDecl* Field)
{
    QualType FieldBaseElementType = compiler.getASTContext().getBaseElementType(Field->getType());

    const RecordType* RT = FieldBaseElementType->getAs<RecordType>();
    if (!RT)
        return true;

    CXXRecordDecl* FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());

    // The constructor for an implicit anonymous union member is never invoked.
    if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion())
        return false;

    return FieldClassDecl->hasTrivialDefaultConstructor();
}

loplugin::Plugin::Registration<TrivialConstructor> X("trivialconstructor", true);
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */