summaryrefslogtreecommitdiffstats
path: root/compilerplugins/clang/fieldcanbelocal.cxx
blob: ae1af187befb07a22ead5b734d79b03a99581ad1 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* -*- 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/.
 */

#if !defined _WIN32 //TODO, #include <sys/file.h>

#include <cassert>
#include <string>
#include <iostream>
#include <fstream>
#include <unordered_set>
#include <vector>
#include <algorithm>
#include <sys/file.h>
#include <unistd.h>

#include "config_clang.h"

#include "plugin.hxx"
#include "compat.hxx"
#include "check.hxx"

#include "clang/AST/ParentMapContext.h"

/**
 Look for fields on objects that can be local variables.
 Not a particularly smart plugin, generates a lot of false positives, and requires review of the output.
 Mostly looks for fields that are only accessed within a single method.
*/

namespace
{
struct MyFuncInfo
{
    std::string returnType;
    std::string nameAndParams;
    std::string sourceLocation;
};

struct MyFieldInfo
{
    std::string parentClass;
    std::string fieldName;
    std::string fieldType;
    std::string sourceLocation;
};

// try to limit the voluminous output a little
// if the value is nullptr, that indicates that we touched that field from more than one function
static std::unordered_map<const FieldDecl*, const FunctionDecl*> touchedMap;

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

    virtual void run() override;

    bool shouldVisitTemplateInstantiations() const { return true; }
    bool shouldVisitImplicitCode() const { return true; }

    bool TraverseCXXConstructorDecl(CXXConstructorDecl*);
    bool TraverseCXXMethodDecl(CXXMethodDecl*);
    bool TraverseFunctionDecl(FunctionDecl*);

    bool VisitMemberExpr(const MemberExpr*);
    bool VisitDeclRefExpr(const DeclRefExpr*);
    bool VisitInitListExpr(const InitListExpr*);
    bool VisitCXXConstructorDecl(const CXXConstructorDecl*);

private:
    MyFieldInfo niceName(const FieldDecl*);
    MyFuncInfo niceName(const FunctionDecl*);
    std::string toString(SourceLocation loc);
    void checkTouched(const FieldDecl* fieldDecl, const FunctionDecl*);
    bool isSomeKindOfConstant(const Expr* arg);

    RecordDecl* insideMoveOrCopyOrCloneDeclParent = nullptr;
    RecordDecl* insideStreamOutputOperator = nullptr;
    // For reasons I do not understand, parentFunctionDecl() is not reliable, so
    // we store the parent function on the way down the AST.
    FunctionDecl* insideFunctionDecl = nullptr;
};

void FieldCanBeLocal::run()
{
    handler.enableTreeWideAnalysisMode();

    TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());

    if (!isUnitTestMode())
    {
        // dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes
        // writing to the same logfile
        std::string output;
        output.reserve(64 * 1024);
        for (const auto& pair : touchedMap)
        {
            if (pair.first->getParent()->isLambda())
                continue;
            MyFieldInfo s = niceName(pair.first);
            output += "definition:\t" + s.parentClass //
                      + "\t" + s.fieldName //
                      + "\t" + s.fieldType //
                      + "\t" + s.sourceLocation //
                      + "\n";
            // we have to output a negative, in case, in some other file, it is touched only once
            if (!pair.second)
                output += "touched:\t" + s.parentClass //
                          + "\t" + s.fieldName //
                          + "\tNegative" //
                          + "\tnowhere.cxx" //
                          + "\n";
            else
            {
                MyFuncInfo s2 = niceName(pair.second);
                output += "touched:\t" + s.parentClass //
                          + "\t" + s.fieldName //
                          + "\t" + s2.returnType + " " + s2.nameAndParams //
                          + "\t" + s2.sourceLocation //
                          + "\n";
            }
        }
        std::ofstream myfile;
        myfile.open(WORKDIR "/loplugin.fieldcanbelocal.log", std::ios::app | std::ios::out);
        myfile << output;
        myfile.close();
    }
    else
    {
        //        for (const MyFieldInfo & s : readFromSet)
        //            report(
        //                DiagnosticsEngine::Warning,
        //                "read %0",
        //                s.parentRecord->getBeginLoc())
        //                << s.fieldName;
    }
}

MyFieldInfo FieldCanBeLocal::niceName(const FieldDecl* fieldDecl)
{
    MyFieldInfo aInfo;

    const RecordDecl* recordDecl = fieldDecl->getParent();

    if (const CXXRecordDecl* cxxRecordDecl = dyn_cast<CXXRecordDecl>(recordDecl))
    {
        if (cxxRecordDecl->getTemplateInstantiationPattern())
            cxxRecordDecl = cxxRecordDecl->getTemplateInstantiationPattern();
        aInfo.parentClass = cxxRecordDecl->getQualifiedNameAsString();
    }
    else
    {
        aInfo.parentClass = recordDecl->getQualifiedNameAsString();
    }

    aInfo.fieldName = fieldDecl->getNameAsString();
    // sometimes the name (if it's an anonymous thing) contains the full path of the build folder, which we don't need
    size_t idx = aInfo.fieldName.find(SRCDIR);
    if (idx != std::string::npos)
    {
        aInfo.fieldName = aInfo.fieldName.replace(idx, strlen(SRCDIR), "");
    }
    aInfo.fieldType = fieldDecl->getType().getAsString();

    SourceLocation expansionLoc
        = compiler.getSourceManager().getExpansionLoc(fieldDecl->getLocation());
    StringRef name = getFilenameOfLocation(expansionLoc);
    aInfo.sourceLocation
        = std::string(name.substr(strlen(SRCDIR) + 1)) + ":"
          + std::to_string(compiler.getSourceManager().getSpellingLineNumber(expansionLoc));
    loplugin::normalizeDotDotInFilePath(aInfo.sourceLocation);

    return aInfo;
}

MyFuncInfo FieldCanBeLocal::niceName(const FunctionDecl* functionDecl)
{
    if (functionDecl->getInstantiatedFromMemberFunction())
        functionDecl = functionDecl->getInstantiatedFromMemberFunction();
    else if (functionDecl->getTemplateInstantiationPattern())
        functionDecl = functionDecl->getTemplateInstantiationPattern();

    MyFuncInfo aInfo;
    if (!isa<CXXConstructorDecl>(functionDecl))
    {
        aInfo.returnType = functionDecl->getReturnType().getCanonicalType().getAsString();
    }
    else
    {
        aInfo.returnType = "";
    }

    if (isa<CXXMethodDecl>(functionDecl))
    {
        const CXXRecordDecl* recordDecl = dyn_cast<CXXMethodDecl>(functionDecl)->getParent();
        aInfo.nameAndParams += recordDecl->getQualifiedNameAsString();
        aInfo.nameAndParams += "::";
    }
    aInfo.nameAndParams += functionDecl->getNameAsString() + "(";
    bool bFirst = true;
    for (const ParmVarDecl* pParmVarDecl : functionDecl->parameters())
    {
        if (bFirst)
            bFirst = false;
        else
            aInfo.nameAndParams += ",";
        aInfo.nameAndParams += pParmVarDecl->getType().getCanonicalType().getAsString();
    }
    aInfo.nameAndParams += ")";
    if (isa<CXXMethodDecl>(functionDecl) && dyn_cast<CXXMethodDecl>(functionDecl)->isConst())
    {
        aInfo.nameAndParams += " const";
    }

    aInfo.sourceLocation = toString(functionDecl->getLocation());

    return aInfo;
}

std::string FieldCanBeLocal::toString(SourceLocation loc)
{
    SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc(loc);
    StringRef name = getFilenameOfLocation(expansionLoc);
    std::string sourceLocation
        = std::string(name.substr(strlen(SRCDIR) + 1)) + ":"
          + std::to_string(compiler.getSourceManager().getSpellingLineNumber(expansionLoc));
    loplugin::normalizeDotDotInFilePath(sourceLocation);
    return sourceLocation;
}

bool FieldCanBeLocal::TraverseCXXConstructorDecl(CXXConstructorDecl* cxxConstructorDecl)
{
    auto copy = insideMoveOrCopyOrCloneDeclParent;
    if (!ignoreLocation(cxxConstructorDecl->getBeginLoc())
        && cxxConstructorDecl->isThisDeclarationADefinition())
    {
        if (cxxConstructorDecl->isCopyOrMoveConstructor())
            insideMoveOrCopyOrCloneDeclParent = cxxConstructorDecl->getParent();
    }
    bool ret = RecursiveASTVisitor::TraverseCXXConstructorDecl(cxxConstructorDecl);
    insideMoveOrCopyOrCloneDeclParent = copy;
    return ret;
}

bool FieldCanBeLocal::TraverseCXXMethodDecl(CXXMethodDecl* cxxMethodDecl)
{
    auto copy1 = insideMoveOrCopyOrCloneDeclParent;
    auto copy2 = insideFunctionDecl;
    if (!ignoreLocation(cxxMethodDecl->getBeginLoc())
        && cxxMethodDecl->isThisDeclarationADefinition())
    {
        if (cxxMethodDecl->isCopyAssignmentOperator() || cxxMethodDecl->isMoveAssignmentOperator()
            || (cxxMethodDecl->getIdentifier()
                && (cxxMethodDecl->getName().startswith("Clone")
                    || cxxMethodDecl->getName().startswith("clone")
                    || cxxMethodDecl->getName().startswith("createClone"))))
            insideMoveOrCopyOrCloneDeclParent = cxxMethodDecl->getParent();
        // these are similar in that they tend to simply enumerate all the fields of an object without putting
        // them to some useful purpose
        auto op = cxxMethodDecl->getOverloadedOperator();
        if (op == OO_EqualEqual || op == OO_ExclaimEqual)
            insideMoveOrCopyOrCloneDeclParent = cxxMethodDecl->getParent();
    }
    insideFunctionDecl = cxxMethodDecl;
    bool ret = RecursiveASTVisitor::TraverseCXXMethodDecl(cxxMethodDecl);
    insideMoveOrCopyOrCloneDeclParent = copy1;
    insideFunctionDecl = copy2;
    return ret;
}

bool FieldCanBeLocal::TraverseFunctionDecl(FunctionDecl* functionDecl)
{
    auto copy1 = insideStreamOutputOperator;
    auto copy2 = insideFunctionDecl;
    auto copy3 = insideMoveOrCopyOrCloneDeclParent;
    if (functionDecl->getLocation().isValid() && !ignoreLocation(functionDecl->getBeginLoc())
        && functionDecl->isThisDeclarationADefinition())
    {
        auto op = functionDecl->getOverloadedOperator();
        if (op == OO_LessLess && functionDecl->getNumParams() == 2)
        {
            QualType qt = functionDecl->getParamDecl(1)->getType();
            insideStreamOutputOperator
                = qt.getNonReferenceType().getUnqualifiedType()->getAsCXXRecordDecl();
        }
        // these are similar in that they tend to simply enumerate all the fields of an object without putting
        // them to some useful purpose
        if (op == OO_EqualEqual || op == OO_ExclaimEqual)
        {
            QualType qt = functionDecl->getParamDecl(1)->getType();
            insideMoveOrCopyOrCloneDeclParent
                = qt.getNonReferenceType().getUnqualifiedType()->getAsCXXRecordDecl();
        }
    }
    insideFunctionDecl = functionDecl;
    bool ret = RecursiveASTVisitor::TraverseFunctionDecl(functionDecl);
    insideStreamOutputOperator = copy1;
    insideFunctionDecl = copy2;
    insideMoveOrCopyOrCloneDeclParent = copy3;
    return ret;
}

bool FieldCanBeLocal::VisitMemberExpr(const MemberExpr* memberExpr)
{
    const ValueDecl* decl = memberExpr->getMemberDecl();
    const FieldDecl* fieldDecl = dyn_cast<FieldDecl>(decl);
    if (!fieldDecl)
    {
        return true;
    }
    fieldDecl = fieldDecl->getCanonicalDecl();
    if (ignoreLocation(fieldDecl->getBeginLoc()))
    {
        return true;
    }
    // ignore stuff that forms part of the stable URE interface
    if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(fieldDecl->getLocation())))
    {
        return true;
    }

    if (insideMoveOrCopyOrCloneDeclParent || insideStreamOutputOperator)
    {
        RecordDecl const* cxxRecordDecl1 = fieldDecl->getParent();
        // we don't care about reads from a field when inside the copy/move constructor/operator= for that field
        if (cxxRecordDecl1 && (cxxRecordDecl1 == insideMoveOrCopyOrCloneDeclParent))
            return true;
        // we don't care about reads when the field is being used in an output operator, this is normally
        // debug stuff
        if (cxxRecordDecl1 && (cxxRecordDecl1 == insideStreamOutputOperator))
            return true;
    }

    checkTouched(fieldDecl, insideFunctionDecl);

    return true;
}

bool FieldCanBeLocal::VisitDeclRefExpr(const DeclRefExpr* declRefExpr)
{
    const Decl* decl = declRefExpr->getDecl();
    const FieldDecl* fieldDecl = dyn_cast<FieldDecl>(decl);
    if (!fieldDecl)
    {
        return true;
    }
    fieldDecl = fieldDecl->getCanonicalDecl();
    if (ignoreLocation(fieldDecl->getBeginLoc()))
    {
        return true;
    }
    // ignore stuff that forms part of the stable URE interface
    if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(fieldDecl->getLocation())))
    {
        return true;
    }

    checkTouched(fieldDecl, insideFunctionDecl);

    return true;
}

// fields that are assigned via member initialisers do not get visited in VisitDeclRef, so
// have to do it here
bool FieldCanBeLocal::VisitCXXConstructorDecl(const CXXConstructorDecl* cxxConstructorDecl)
{
    if (ignoreLocation(cxxConstructorDecl->getBeginLoc()))
    {
        return true;
    }
    // ignore stuff that forms part of the stable URE interface
    if (isInUnoIncludeFile(
            compiler.getSourceManager().getSpellingLoc(cxxConstructorDecl->getLocation())))
    {
        return true;
    }

    // templates make EvaluateAsInt crash inside clang
    if (cxxConstructorDecl->isDependentContext())
        return true;

    // we don't care about writes to a field when inside the copy/move constructor/operator= for that field
    if (insideMoveOrCopyOrCloneDeclParent
        && cxxConstructorDecl->getParent() == insideMoveOrCopyOrCloneDeclParent)
        return true;

    for (auto it = cxxConstructorDecl->init_begin(); it != cxxConstructorDecl->init_end(); ++it)
    {
        const CXXCtorInitializer* init = *it;
        const FieldDecl* fieldDecl = init->getMember();
        if (!fieldDecl)
            continue;
        if (init->getInit() && isSomeKindOfConstant(init->getInit()))
            checkTouched(fieldDecl, cxxConstructorDecl);
        else
            touchedMap[fieldDecl] = nullptr;
    }
    return true;
}

// Fields that are assigned via init-list-expr do not get visited in VisitDeclRef, so
// have to do it here.
bool FieldCanBeLocal::VisitInitListExpr(const InitListExpr* initListExpr)
{
    if (ignoreLocation(initListExpr->getBeginLoc()))
        return true;

    QualType varType = initListExpr->getType().getDesugaredType(compiler.getASTContext());
    auto recordType = varType->getAs<RecordType>();
    if (!recordType)
        return true;

    auto recordDecl = recordType->getDecl();
    for (auto it = recordDecl->field_begin(); it != recordDecl->field_end(); ++it)
    {
        checkTouched(*it, insideFunctionDecl);
    }

    return true;
}

void FieldCanBeLocal::checkTouched(const FieldDecl* fieldDecl, const FunctionDecl* functionDecl)
{
    auto methodDecl = dyn_cast_or_null<CXXMethodDecl>(functionDecl);
    if (!methodDecl)
    {
        touchedMap[fieldDecl] = nullptr;
        return;
    }
    if (methodDecl->getParent() != fieldDecl->getParent())
    {
        touchedMap[fieldDecl] = nullptr;
        return;
    }
    auto it = touchedMap.find(fieldDecl);
    if (it == touchedMap.end())
        touchedMap.emplace(fieldDecl, functionDecl);
    else if (it->second != functionDecl)
        it->second = nullptr;
}

bool FieldCanBeLocal::isSomeKindOfConstant(const Expr* arg)
{
    assert(arg);
    if (arg->isValueDependent())
        return false;
    return arg->isCXX11ConstantExpr(compiler.getASTContext());
}

loplugin::Plugin::Registration<FieldCanBeLocal> X("fieldcanbelocal", false);
}

#endif

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