constantparam.cxx 12.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* -*- 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 <string>
#include <set>
#include <iostream>
#include <fstream>

#include "plugin.hxx"
#include "compat.hxx"
17
#include "check.hxx"
18
#include "functionaddress.hxx"
19 20

/*
21
  Find params on methods where the param is only ever passed as a single constant value.
22 23 24

 The process goes something like this:
  $ make check
Noel Grandin's avatar
Noel Grandin committed
25
  $ make FORCE_COMPILE_ALL=1 COMPILER_PLUGIN_TOOL='constantparam' check
26
  $ ./compilerplugins/clang/constantparam.py
27 28

  TODO look for OUString and OString params and check for call-params that are always either "" or default constructed
29 30 31

  FIXME this plugin manages to trigger crashes inside clang, when calling EvaluateAsInt, so I end up disabling it for a handful of files
     here and there.
32 33 34 35 36 37 38 39 40
*/

namespace {

struct MyCallSiteInfo
{
    std::string returnType;
    std::string nameAndParams;
    std::string paramName;
Noel Grandin's avatar
Noel Grandin committed
41
    std::string paramType;
42
    int paramIndex; // because in some declarations the names are empty
43 44 45 46 47
    std::string callValue;
    std::string sourceLocation;
};
bool operator < (const MyCallSiteInfo &lhs, const MyCallSiteInfo &rhs)
{
48 49
    return std::tie(lhs.sourceLocation, lhs.paramIndex, lhs.callValue)
         < std::tie(rhs.sourceLocation, rhs.paramIndex, rhs.callValue);
50 51 52 53 54 55 56
}


// try to limit the voluminous output a little
static std::set<MyCallSiteInfo> callSet;

class ConstantParam:
57
    public loplugin::FunctionAddress<ConstantParam>
58 59
{
public:
60
    explicit ConstantParam(loplugin::InstantiationData const & data): loplugin::FunctionAddress<ConstantParam>(data) {}
61 62 63

    virtual void run() override
    {
64
        // ignore some files that make clang crash inside EvaluateAsInt
65
        std::string fn(handler.getMainFileName());
66
        loplugin::normalizeDotDotInFilePath(fn);
67 68
        if (loplugin::isSamePathname(fn, SRCDIR "/basegfx/source/matrix/b2dhommatrix.cxx")
            || loplugin::isSamePathname(fn, SRCDIR "/basegfx/source/matrix/b3dhommatrix.cxx"))
69 70
             return;

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

73 74 75 76 77 78 79
        // this catches places that take the address of a method
        for (auto functionDecl : getFunctionsWithAddressTaken())
        {
            for (unsigned i = 0; i < functionDecl->getNumParams(); ++i)
                addToCallSet(functionDecl, i, functionDecl->getParamDecl(i)->getName(), "unknown3");
        }

80 81 82 83 84 85
        // 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;
        for (const MyCallSiteInfo & s : callSet)
            output += s.returnType + "\t" + s.nameAndParams + "\t" + s.sourceLocation + "\t"
Noel Grandin's avatar
Noel Grandin committed
86
                        + s.paramName + "\t" + s.paramType + "\t" + s.callValue + "\n";
87
        std::ofstream myfile;
88
        myfile.open( WORKDIR "/loplugin.constantparam.log", std::ios::app | std::ios::out);
89 90 91 92 93
        myfile << output;
        myfile.close();
    }

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

96 97
    bool VisitCallExpr( const CallExpr* );
    bool VisitCXXConstructExpr( const CXXConstructExpr* );
98
private:
Noel Grandin's avatar
Noel Grandin committed
99
    void addToCallSet(const FunctionDecl* functionDecl, int paramIndex, llvm::StringRef paramName, const std::string& callValue);
100
    std::string getCallValue(const Expr* arg);
101 102
};

Noel Grandin's avatar
Noel Grandin committed
103
void ConstantParam::addToCallSet(const FunctionDecl* functionDecl, int paramIndex, llvm::StringRef paramName, const std::string& callValue)
104 105 106
{
    if (functionDecl->getInstantiatedFromMemberFunction())
        functionDecl = functionDecl->getInstantiatedFromMemberFunction();
107
#if CLANG_VERSION < 90000
108 109
    else if (functionDecl->getClassScopeSpecializationPattern())
        functionDecl = functionDecl->getClassScopeSpecializationPattern();
110
#endif
111 112
    else if (functionDecl->getTemplateInstantiationPattern())
        functionDecl = functionDecl->getTemplateInstantiationPattern();
Noel Grandin's avatar
Noel Grandin committed
113 114 115

    if (!functionDecl->getNameInfo().getLoc().isValid())
        return;
116 117
    if (functionDecl->isVariadic())
        return;
Noel Grandin's avatar
Noel Grandin committed
118 119 120
    if (ignoreLocation(functionDecl))
        return;
    // ignore stuff that forms part of the stable URE interface
121
    if (isInUnoIncludeFile(functionDecl))
Noel Grandin's avatar
Noel Grandin committed
122 123 124
        return;
    SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( functionDecl->getLocation() );
    StringRef filename = compiler.getSourceManager().getFilename(expansionLoc);
125
    if (!loplugin::hasPathnamePrefix(filename, SRCDIR "/"))
Noel Grandin's avatar
Noel Grandin committed
126 127 128 129
        return;
    filename = filename.substr(strlen(SRCDIR)+1);


130
    MyCallSiteInfo aInfo;
131
    aInfo.returnType = functionDecl->getReturnType().getCanonicalType().getAsString();
132 133 134 135 136 137 138 139

    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;
140
    for (const ParmVarDecl *pParmVarDecl : functionDecl->parameters()) {
141 142 143 144 145 146 147 148 149 150
        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";
    }
151
    aInfo.paramName = paramName;
152
    aInfo.paramIndex = paramIndex;
Noel Grandin's avatar
Noel Grandin committed
153 154
    if (paramIndex < (int)functionDecl->getNumParams())
        aInfo.paramType = functionDecl->getParamDecl(paramIndex)->getType().getCanonicalType().getAsString();
155 156
    aInfo.callValue = callValue;

Noel Grandin's avatar
Noel Grandin committed
157
    aInfo.sourceLocation = filename.str() + ":" + std::to_string(compiler.getSourceManager().getSpellingLineNumber(expansionLoc));
158
    loplugin::normalizeDotDotInFilePath(aInfo.sourceLocation);
159

Noel Grandin's avatar
Noel Grandin committed
160
    callSet.insert(aInfo);
161 162
}

163 164
std::string ConstantParam::getCallValue(const Expr* arg)
{
165 166 167 168
    arg = arg->IgnoreParenCasts();
    if (isa<CXXDefaultArgExpr>(arg)) {
        arg = dyn_cast<CXXDefaultArgExpr>(arg)->getExpr();
    }
169 170 171
    arg = arg->IgnoreParenCasts();
    // ignore this, it seems to trigger an infinite recursion
    if (isa<UnaryExprOrTypeTraitExpr>(arg)) {
172
        return "unknown1";
173 174
    }
    APSInt x1;
175
    if (compat::EvaluateAsInt(arg, x1, compiler.getASTContext()))
176 177 178
    {
        return x1.toString(10);
    }
179 180 181
    if (isa<CXXNullPtrLiteralExpr>(arg)) {
        return "0";
    }
Noel Grandin's avatar
Noel Grandin committed
182 183 184
    if (isa<MaterializeTemporaryExpr>(arg))
    {
        const CXXBindTemporaryExpr* strippedArg = dyn_cast_or_null<CXXBindTemporaryExpr>(arg->IgnoreParenCasts());
185
        if (strippedArg)
Noel Grandin's avatar
Noel Grandin committed
186
        {
187 188 189 190 191 192 193 194 195 196 197
            auto temp = dyn_cast<CXXTemporaryObjectExpr>(strippedArg->getSubExpr());
            if (temp->getNumArgs() == 0)
            {
                if (loplugin::TypeCheck(temp->getType()).Class("OUString").Namespace("rtl").GlobalNamespace()) {
                    return "\"\"";
                }
                if (loplugin::TypeCheck(temp->getType()).Class("OString").Namespace("rtl").GlobalNamespace()) {
                    return "\"\"";
                }
                return "defaultConstruct";
            }
Noel Grandin's avatar
Noel Grandin committed
198 199
        }
    }
200 201 202 203

    // Get the expression contents.
    // This helps us find params which are always initialised with something like "OUString()".
    SourceManager& SM = compiler.getSourceManager();
204 205
    SourceLocation startLoc = compat::getBeginLoc(arg);
    SourceLocation endLoc = compat::getEndLoc(arg);
206 207 208 209 210 211 212 213 214 215 216
    const char *p1 = SM.getCharacterData( startLoc );
    const char *p2 = SM.getCharacterData( endLoc );
    if (!p1 || !p2 || (p2 - p1) < 0 || (p2 - p1) > 40) {
        return "unknown";
    }
    unsigned n = Lexer::MeasureTokenLength( endLoc, SM, compiler.getLangOpts());
    std::string s( p1, p2 - p1 + n);
    // strip linefeed and tab characters so they don't interfere with the parsing of the log file
    std::replace( s.begin(), s.end(), '\r', ' ');
    std::replace( s.begin(), s.end(), '\n', ' ');
    std::replace( s.begin(), s.end(), '\t', ' ');
217 218 219 220 221 222

    // now normalize the value. For some params, like OUString, we can pass it as OUString() or "" and they are the same thing
    if (s == "OUString()")
        s = "\"\"";
    else if (s == "OString()")
        s = "\"\"";
223 224 225 226 227
    else if (s == "aEmptyOUStr") //sw
        s = "\"\"";
    else if (s == "EMPTY_OUSTRING")//sc
        s = "\"\"";
    else if (s == "GetEmptyOUString()") //sc
228
        s = "\"\"";
229
    return s;
230 231 232
}

bool ConstantParam::VisitCallExpr(const CallExpr * callExpr) {
233 234 235 236 237 238 239 240 241 242
    if (ignoreLocation(callExpr)) {
        return true;
    }
    const FunctionDecl* functionDecl;
    if (isa<CXXMemberCallExpr>(callExpr)) {
        functionDecl = dyn_cast<CXXMemberCallExpr>(callExpr)->getMethodDecl();
    }
    else {
        functionDecl = callExpr->getDirectCallee();
    }
243
    if (!functionDecl)
244 245
        return true;
    functionDecl = functionDecl->getCanonicalDecl();
Andrea Gelmini's avatar
Andrea Gelmini committed
246
    // method overrides don't always specify the same default params (although they probably should)
247 248 249 250 251 252 253 254 255 256
    // so we need to work our way up to the root method
    while (isa<CXXMethodDecl>(functionDecl)) {
        const CXXMethodDecl* methodDecl = dyn_cast<CXXMethodDecl>(functionDecl);
        if (methodDecl->size_overridden_methods()==0)
            break;
        functionDecl = *methodDecl->begin_overridden_methods();
    }
    // work our way back to the root definition for template methods
    if (functionDecl->getInstantiatedFromMemberFunction())
        functionDecl = functionDecl->getInstantiatedFromMemberFunction();
257
#if CLANG_VERSION < 90000
258 259
    else if (functionDecl->getClassScopeSpecializationPattern())
        functionDecl = functionDecl->getClassScopeSpecializationPattern();
260
#endif
261 262 263
    else if (functionDecl->getTemplateInstantiationPattern())
        functionDecl = functionDecl->getTemplateInstantiationPattern();

264 265 266 267 268 269 270 271 272 273 274 275 276 277
    unsigned len = std::max(callExpr->getNumArgs(), functionDecl->getNumParams());
    for (unsigned i = 0; i < len; ++i) {
        const Expr* valExpr;
        if (i < callExpr->getNumArgs())
            valExpr = callExpr->getArg(i);
        else if (i < functionDecl->getNumParams() && functionDecl->getParamDecl(i)->hasDefaultArg())
            valExpr = functionDecl->getParamDecl(i)->getDefaultArg();
        else
            // can happen in template code
            continue;
        std::string callValue = getCallValue(valExpr);
        std::string paramName = i < functionDecl->getNumParams()
                                ? functionDecl->getParamDecl(i)->getName()
                                : llvm::StringRef("###" + std::to_string(i));
Noel Grandin's avatar
Noel Grandin committed
278
        addToCallSet(functionDecl, i, paramName, callValue);
279 280 281 282
    }
    return true;
}

283 284 285 286 287
bool ConstantParam::VisitCXXConstructExpr( const CXXConstructExpr* constructExpr )
{
    const CXXConstructorDecl* constructorDecl = constructExpr->getConstructor();
    constructorDecl = constructorDecl->getCanonicalDecl();

288 289 290 291 292 293 294 295 296 297 298 299 300 301
    unsigned len = std::max(constructExpr->getNumArgs(), constructorDecl->getNumParams());
    for (unsigned i = 0; i < len; ++i) {
        const Expr* valExpr;
        if (i < constructExpr->getNumArgs())
            valExpr = constructExpr->getArg(i);
        else if (i < constructorDecl->getNumParams() && constructorDecl->getParamDecl(i)->hasDefaultArg())
            valExpr = constructorDecl->getParamDecl(i)->getDefaultArg();
        else
            // can happen in template code
            continue;
        std::string callValue = getCallValue(valExpr);
        std::string paramName = i < constructorDecl->getNumParams()
                                ? constructorDecl->getParamDecl(i)->getName()
                                : llvm::StringRef("###" + std::to_string(i));
Noel Grandin's avatar
Noel Grandin committed
302
        addToCallSet(constructorDecl, i, paramName, callValue);
303
    }
304 305 306
    return true;
}

307

308 309 310 311 312
loplugin::Plugin::Registration< ConstantParam > X("constantparam", false);

}

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