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
|
/* -*- 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 <memory>
#include <regex>
#include <string>
#include <set>
#include "plugin.hxx"
// Check for some basic naming mismatches which make the code harder to read
//
// This plugin is deliberately fairly lax, and only targets the most egregeriously faulty code,
// since we have a broad range of styles in our code and we don't want to generate unnecessary
// churn.
namespace {
class StylePolice :
public loplugin::FilteringPlugin<StylePolice>
{
public:
explicit StylePolice(InstantiationData const & data): FilteringPlugin(data) {}
virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
bool VisitVarDecl(const VarDecl *);
private:
StringRef getFilename(SourceLocation loc);
};
StringRef StylePolice::getFilename(SourceLocation loc)
{
SourceLocation spellingLocation = compiler.getSourceManager().getSpellingLoc(loc);
StringRef name { getFilenameOfLocation(spellingLocation) };
return name;
}
bool startswith(const std::string& rStr, const char* pSubStr) {
return rStr.compare(0, strlen(pSubStr), pSubStr) == 0;
}
bool isUpperLetter(char c) {
return c >= 'A' && c <= 'Z';
}
bool isLowerLetter(char c) {
return c >= 'a' && c <= 'z';
}
bool isIdentifierLetter(char c) {
return isUpperLetter(c) || isLowerLetter(c);
}
bool matchPointerVar(const std::string& s) {
return s.size() > 2 && s[0] == 'p' && isUpperLetter(s[1]);
}
bool matchRefCountedPointerVar(const std::string& s) {
return s.size() > 2 && s[0] == 'x' && isUpperLetter(s[1]);
}
bool matchMember(const std::string& s) {
return s.size() > 3 && s[0] == 'm'
&& ( ( strchr("abnprsx", s[1]) && isUpperLetter(s[2]) )
|| ( s[1] == '_' && isIdentifierLetter(s[2]) ) );
}
bool StylePolice::VisitVarDecl(const VarDecl * varDecl)
{
if (ignoreLocation(varDecl)) {
return true;
}
StringRef aFileName = getFilename(varDecl->getLocStart());
std::string name = varDecl->getName();
if (!varDecl->isLocalVarDecl()) {
return true;
}
if (matchMember(name))
{
// these names appear to be taken from some scientific paper
if (aFileName == SRCDIR "/scaddins/source/analysis/bessel.cxx" ) {
}
// lots of places where we are storing a "method id" here
else if (aFileName.startswith(SRCDIR "/connectivity/source/drivers/jdbc") && name.compare(0,3,"mID") == 0) {
}
else {
report(
DiagnosticsEngine::Warning,
"this local variable follows our member field naming convention, which is confusing",
varDecl->getLocation())
<< varDecl->getType() << varDecl->getSourceRange();
}
}
QualType qt = varDecl->getType().getDesugaredType(compiler.getASTContext()).getCanonicalType();
qt = qt.getNonReferenceType();
std::string typeName = qt.getAsString();
if (startswith(typeName, "const "))
typeName = typeName.substr(6);
if (startswith(typeName, "class "))
typeName = typeName.substr(6);
if (startswith(typeName, "struct "))
typeName = typeName.substr(7);
std::string aOriginalTypeName = varDecl->getType().getAsString();
if (startswith(aOriginalTypeName, "const "))
aOriginalTypeName = aOriginalTypeName.substr(6);
if (!qt->isPointerType() && !qt->isArrayType() && !qt->isFunctionPointerType() && !qt->isMemberPointerType()
&& matchPointerVar(name)
&& !startswith(typeName, "boost::intrusive_ptr")
&& !startswith(typeName, "std::optional")
&& !startswith(typeName, "boost::shared_ptr")
&& !startswith(typeName, "com::sun::star::uno::Reference")
&& !startswith(typeName, "cppu::OInterfaceIteratorHelper")
&& !startswith(typeName, "formula::FormulaCompiler::CurrentFactor")
&& aOriginalTypeName != "GLXPixmap"
&& !startswith(typeName, "rtl::Reference")
&& !startswith(typeName, "ScopedVclPtr")
&& typeName.find("::mem_fun") == std::string::npos
&& typeName.find("shared_ptr") == std::string::npos
&& typeName.find("unique_ptr") == std::string::npos
&& typeName.find("::weak_ptr") == std::string::npos
&& !startswith(typeName, "_LOKDocViewPrivate")
&& !startswith(typeName, "sw::UnoCursorPointer")
&& !startswith(typeName, "tools::SvRef")
&& !startswith(typeName, "VclPtr")
&& !startswith(typeName, "vcl::ScopedBitmapAccess")
// lots of the code seems to regard iterator objects as being "pointer-like"
&& typeName.find("iterator<") == std::string::npos
&& typeName.find("iter<") == std::string::npos
// libc++ std::__1::__wrap_iter<...>
&& aOriginalTypeName != "sal_IntPtr" )
{
if (aFileName.startswith(SRCDIR "/bridges/") ) {
} else if (aFileName.startswith(SRCDIR "/vcl/source/fontsubset/sft.cxx") ) {
} else {
report(
DiagnosticsEngine::Warning,
"this local variable of type '%0' follows our pointer naming convention, but it is not a pointer, %1",
varDecl->getLocation())
<< typeName << aOriginalTypeName << varDecl->getSourceRange();
}
}
if (matchRefCountedPointerVar(name)
&& !startswith(typeName, "boost::intrusive_ptr")
&& !startswith(typeName, "com::sun::star::uno::Reference")
&& !startswith(typeName, "com::sun::star::uno::Sequence")
&& !startswith(typeName, "com::sun::star::uno::WeakReference")
&& !startswith(typeName, "drawinglayer::primitive2d::Primitive2DContainer")
&& !startswith(typeName, "drawinglayer::primitive3d::Primitive3DContainer")
&& !startswith(typeName, "jfw::CXPathObjectPtr")
&& !startswith(typeName, "_LOKDocViewPrivate")
&& !startswith(typeName, "oox::dump::BinaryInputStreamRef")
&& !startswith(typeName, "oox::drawingml::chart::ModelRef")
&& !startswith(typeName, "rtl::Reference")
&& !startswith(typeName, "Reference")
&& !startswith(typeName, "SfxObjectShellLock")
&& !startswith(typeName, "store::PageHolderObject")
&& !startswith(typeName, "store::ResourceHolder")
&& !startswith(typeName, "store::OStoreHandle")
&& typeName.find("unique_ptr") == std::string::npos
&& typeName.find("shared_ptr") == std::string::npos
&& !startswith(typeName, "ScopedVclPtr")
&& !startswith(typeName, "svt::EmbeddedObjectRef")
&& !startswith(typeName, "tools::SvRef")
&& !startswith(typeName, "tools::WeakReference")
&& !startswith(typeName, "utl::SharedUNOComponent")
&& !startswith(typeName, "VclPtr")
&& !startswith(typeName, "vcl::DeleteOnDeinit")
&& !startswith(typeName, "vcl::DeleteUnoReferenceOnDeinit")
// there are lots of coordinate/position vars that start with "x"
&& !qt->isArithmeticType()
&& !startswith(typeName, "float [")
)
{
report(
DiagnosticsEngine::Warning,
"this local variable of type '%0' follows our ref-counted-pointer naming convention, but it is not a ref-counted-pointer, %1",
varDecl->getLocation())
<< typeName << aOriginalTypeName << varDecl->getSourceRange();
}
return true;
}
loplugin::Plugin::Registration< StylePolice > X("stylepolice");
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|