aboutsummaryrefslogtreecommitdiffstats
path: root/src/TypeUtils.cpp
blob: 9098028ba70c7f79057d07c0daf25eb5caf0618c (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
157
158
159
160
161
/*
   This file is part of the clazy static checker.

  Copyright (C) 2016-2017 Sergio Martins <smartins@kde.org>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/

#include "TypeUtils.h"
#include "Utils.h"
#include "StmtBodyRange.h"
#include "ClazyContext.h"
#include <HierarchyUtils.h>
#include <StringUtils.h>

#include <clang/Frontend/CompilerInstance.h>
#include <clang/AST/ASTContext.h>

using namespace clang;

bool TypeUtils::classifyQualType(const ClazyContext *context, const VarDecl *varDecl, QualTypeClassification &classif, clang::Stmt *body)
{
    if (!varDecl)
        return false;

    QualType qualType = TypeUtils::unrefQualType(varDecl->getType());
    const Type *paramType = qualType.getTypePtrOrNull();
    if (!paramType || paramType->isIncompleteType())
        return false;

    if (isUndeducibleAuto(paramType))
        return false;

    classif.size_of_T = context->astContext.getTypeSize(qualType) / 8;
    classif.isBig = classif.size_of_T > 16;
    CXXRecordDecl *recordDecl = paramType->getAsCXXRecordDecl();
    classif.isNonTriviallyCopyable = recordDecl && (recordDecl->hasNonTrivialCopyConstructor() || recordDecl->hasNonTrivialDestructor());
    classif.isReference = varDecl->getType()->isLValueReferenceType();
    classif.isConst = qualType.isConstQualified();

    if (varDecl->getType()->isRValueReferenceType()) // && ref, nothing to do here
        return true;

    if (classif.isConst && !classif.isReference) {
        classif.passNonTriviallyCopyableByConstRef = classif.isNonTriviallyCopyable;
        if (classif.isBig) {
            classif.passBigTypeByConstRef = true;
        }
    } else if (classif.isConst && classif.isReference && !classif.isNonTriviallyCopyable && !classif.isBig) {
        classif.passSmallTrivialByValue = true;
    } else if (!classif.isConst && !classif.isReference && (classif.isBig || classif.isNonTriviallyCopyable)) {
        if (body && (Utils::containsNonConstMemberCall(context->parentMap, body, varDecl) || Utils::isPassedToFunction(StmtBodyRange(body), varDecl, /*byrefonly=*/ true)))
            return true;

        classif.passNonTriviallyCopyableByConstRef = classif.isNonTriviallyCopyable;
        if (classif.isBig) {
            classif.passBigTypeByConstRef = true;
        }
    }

    return true;
}

void TypeUtils::heapOrStackAllocated(Expr *arg, const std::string &type,
                                     const clang::LangOptions &lo,
                                     bool &isStack, bool &isHeap)
{
    isStack = false;
    isHeap = false;
    if (isa<CXXNewExpr>(arg)) {
        isHeap = true;
        return;
    }

    std::vector<DeclRefExpr*> declrefs;
    clazy::getChilds(arg, declrefs, 3);

    std::vector<DeclRefExpr*> interestingDeclRefs;
    for (auto declref : declrefs) {
        auto t = declref->getType().getTypePtrOrNull();
        if (!t)
            continue;

        // Remove the '*' if it's a pointer
        QualType qt = t->isPointerType() ? t->getPointeeType()
                                         : declref->getType();

        if (t && type == clazy::simpleTypeName(qt, lo)) {
            interestingDeclRefs.push_back(declref);
        }
    }

    if (interestingDeclRefs.size() > 1) {
        // Too complex
        return;
    }

    if (!interestingDeclRefs.empty()) {
        auto declref = interestingDeclRefs[0];
        isStack = !declref->getType().getTypePtr()->isPointerType();
        isHeap = !isStack;
    }
}

bool TypeUtils::derivesFrom(const CXXRecordDecl *derived, const CXXRecordDecl *possibleBase,
                            std::vector<CXXRecordDecl*> *baseClasses)
{
    if (!derived || !possibleBase || derived == possibleBase)
        return false;

    for (auto base : derived->bases()) {
        const Type *type = base.getType().getTypePtrOrNull();
        if (!type) continue;
        CXXRecordDecl *baseDecl = type->getAsCXXRecordDecl();
        baseDecl = baseDecl ? baseDecl->getCanonicalDecl() : nullptr;

        if (possibleBase == baseDecl || derivesFrom(baseDecl, possibleBase, baseClasses)) {
            if (baseClasses)
                baseClasses->push_back(baseDecl);
            return true;
        }
    }

    return false;
}

bool TypeUtils::derivesFrom(const clang::CXXRecordDecl *derived, const std::string &possibleBase)
{
    if (!derived || !derived->hasDefinition())
        return false;

    if (derived->getQualifiedNameAsString() == possibleBase)
        return true;

    for (auto base : derived->bases()) {
        if (derivesFrom(recordFromBaseSpecifier(base), possibleBase))
            return true;
    }

    return false;
}

bool TypeUtils::derivesFrom(QualType derivedQT, const std::string &possibleBase)
{
    derivedQT = pointeeQualType(derivedQT);
    const auto t = derivedQT.getTypePtrOrNull();
    return t ? derivesFrom(t->getAsCXXRecordDecl(), possibleBase) : false;
}