summaryrefslogtreecommitdiff
path: root/compilerplugins/clang/store/stylepolice.cxx
blob: 96b5f72c00a764288f6823eba9e8133300d097eb (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
/* -*- 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 <regex>
#include <string>
#include <set>

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

// Check for some basic naming mismatches which make the code harder to read

namespace {

static const std::regex aMemberRegex("^m([abnprsx]?[A-Z]|[_][a-zA-Z])");

class StylePolice :
    public RecursiveASTVisitor<StylePolice>, public loplugin::Plugin
{
public:
    explicit StylePolice(InstantiationData const & data): Plugin(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 { compiler.getSourceManager().getFilename(spellingLocation) };
    return name;
}

bool StylePolice::VisitVarDecl(const VarDecl * varDecl)
{
    if (ignoreLocation(varDecl)) {
        return true;
    }
    StringRef aFileName = getFilename(varDecl->getLocStart());
    std::string name = varDecl->getName();

    // these names appear to be taken from some scientific paper
    if (aFileName == SRCDIR "/scaddins/source/analysis/bessel.cxx" ) {
        return true;
    }
    // lots of places where we are storing a "method id" here
    if (aFileName.startswith(SRCDIR "/connectivity/source/drivers/jdbc") && name.compare(0,3,"mID") == 0) {
        return true;
    }

    if (!varDecl->isLocalVarDecl()) {
        return true;
    }

    if (std::regex_search(name, aMemberRegex))
    {
        report(
            DiagnosticsEngine::Warning,
            "this local variable follows our member field naming convention, which is confusing",
            varDecl->getLocation())
             << varDecl->getType() << varDecl->getSourceRange();
    }
    return true;
}


loplugin::Plugin::Registration< StylePolice > X("stylepolice");

}

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