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
|
/* -*- 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 class fields that are always cast to some subtype,
which indicates that they should probably just be declared to be that subtype.
TODO add checking for dynamic_cast/static_cast on
unique_ptr
shared_ptr
*/
namespace
{
struct MyFieldInfo
{
std::string parentClass;
std::string fieldName;
std::string fieldType;
std::string sourceLocation;
};
// try to limit the voluminous output a little
static std::unordered_multimap<const FieldDecl*, const CXXRecordDecl*> castMap;
class FieldCast : public loplugin::FilteringPlugin<FieldCast>
{
public:
explicit FieldCast(loplugin::InstantiationData const& data)
: FilteringPlugin(data)
{
}
virtual void run() override;
bool VisitCXXStaticCastExpr(const CXXStaticCastExpr*);
bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr*);
bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr*);
private:
MyFieldInfo niceName(const FieldDecl*);
void checkCast(const CXXNamedCastExpr*);
};
void FieldCast::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 : castMap)
{
MyFieldInfo s = niceName(pair.first);
output += "cast:\t" + s.parentClass //
+ "\t" + s.fieldName //
+ "\t" + s.fieldType //
+ "\t" + s.sourceLocation //
+ "\t" + pair.second->getQualifiedNameAsString() //
+ "\n";
}
std::ofstream myfile;
myfile.open(WORKDIR "/loplugin.fieldcast.log", std::ios::app | std::ios::out);
myfile << output;
myfile.close();
}
else
{
for (const auto& pair : castMap)
report(DiagnosticsEngine::Warning, "cast %0", pair.first->getBeginLoc())
<< pair.second->getQualifiedNameAsString();
}
}
MyFieldInfo FieldCast::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;
}
bool FieldCast::VisitCXXDynamicCastExpr(const CXXDynamicCastExpr* expr)
{
checkCast(expr);
return true;
}
bool FieldCast::VisitCXXStaticCastExpr(const CXXStaticCastExpr* expr)
{
checkCast(expr);
return true;
}
bool FieldCast::VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr* expr)
{
checkCast(expr);
return true;
}
void FieldCast::checkCast(const CXXNamedCastExpr* expr)
{
if (ignoreLocation(expr))
return;
if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(expr->getBeginLoc())))
return;
auto castToType = expr->getTypeAsWritten()->getPointeeCXXRecordDecl();
if (!castToType)
return;
const Expr* subExpr = compat::getSubExprAsWritten(expr);
const FieldDecl* fieldDecl = nullptr;
if (const MemberExpr* memberExpr = dyn_cast_or_null<MemberExpr>(subExpr->IgnoreImplicit()))
{
fieldDecl = dyn_cast_or_null<FieldDecl>(memberExpr->getMemberDecl());
}
else if (const CXXMemberCallExpr* memberCallExpr
= dyn_cast_or_null<CXXMemberCallExpr>(subExpr->IgnoreImplicit()))
{
if (!memberCallExpr->getMethodDecl()->getIdentifier()
|| memberCallExpr->getMethodDecl()->getName() != "get")
return;
const MemberExpr* memberExpr = dyn_cast_or_null<MemberExpr>(
memberCallExpr->getImplicitObjectArgument()->IgnoreImplicit());
if (!memberExpr)
return;
fieldDecl = dyn_cast_or_null<FieldDecl>(memberExpr->getMemberDecl());
}
if (!fieldDecl)
return;
if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(fieldDecl->getBeginLoc())))
return;
// ignore casting to a less specific type
auto castFromType = subExpr->getType()->getPointeeCXXRecordDecl();
if (castFromType && castFromType->isDerivedFrom(castToType))
return;
castMap.emplace(fieldDecl, castToType);
}
loplugin::Plugin::Registration<FieldCast> X("fieldcast", false);
}
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|