blockblock.cxx 2.22 KB
Newer Older
Noel Grandin's avatar
Noel Grandin committed
1 2 3 4 5 6 7 8 9
/* -*- 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/.
 */

10 11
#ifndef LO_CLANG_SHARED_PLUGINS

Noel Grandin's avatar
Noel Grandin committed
12 13 14 15 16 17 18 19 20 21 22 23 24
#include <cassert>
#include <string>
#include <iostream>
#include <fstream>
#include <set>
#include "plugin.hxx"

/**
  Check for places where we declare a block directly inside a block
 */
namespace {

class BlockBlock:
25
    public loplugin::FilteringPlugin<BlockBlock>
Noel Grandin's avatar
Noel Grandin committed
26 27
{
public:
28
    explicit BlockBlock(loplugin::InstantiationData const & data):
29
        FilteringPlugin(data) {}
Noel Grandin's avatar
Noel Grandin committed
30

31
    virtual bool preRun() override
Noel Grandin's avatar
Noel Grandin committed
32
    {
33
        StringRef fn(handler.getMainFileName());
34
        if (loplugin::isSamePathname(fn, SRCDIR "/sal/osl/unx/file_misc.cxx"))
35 36 37
             return false;
        return true;
    }
Noel Grandin's avatar
Noel Grandin committed
38

39 40 41 42
    void run() override {
        if (preRun()) {
            TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
        }
Noel Grandin's avatar
Noel Grandin committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56
    }

    bool VisitCompoundStmt(CompoundStmt const * );
};

bool BlockBlock::VisitCompoundStmt(CompoundStmt const * compound)
{
    if (ignoreLocation(compound))
        return true;
    if (compound->size() != 1)
        return true;
    auto inner = *compound->body_begin();
    if (!isa<CompoundStmt>(inner))
        return true;
57
    if (compiler.getSourceManager().isMacroBodyExpansion(compat::getBeginLoc(compound)))
Noel Grandin's avatar
Noel Grandin committed
58
        return true;
59
    if (compiler.getSourceManager().isMacroBodyExpansion(compat::getBeginLoc(inner)))
Noel Grandin's avatar
Noel Grandin committed
60
        return true;
61 62 63
    if (containsPreprocessingConditionalInclusion(compound->getSourceRange())) {
        return true;
    }
Noel Grandin's avatar
Noel Grandin committed
64 65 66
    report(
        DiagnosticsEngine::Warning,
        "block directly inside block",
67
         compat::getBeginLoc(compound))
Noel Grandin's avatar
Noel Grandin committed
68 69 70 71
        << compound->getSourceRange();
    report(
        DiagnosticsEngine::Note,
        "inner block here",
72
         compat::getBeginLoc(inner))
Noel Grandin's avatar
Noel Grandin committed
73 74 75 76
        << inner->getSourceRange();
    return true;
}

77
loplugin::Plugin::Registration< BlockBlock > blockblock("blockblock", true);
Noel Grandin's avatar
Noel Grandin committed
78 79 80

}

81 82
#endif // LO_CLANG_SHARED_PLUGINS

Noel Grandin's avatar
Noel Grandin committed
83
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */