1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #ifndef LO_CLANG_SHARED_PLUGINS
17 #include "config_clang.h"
21 Check for places where we declare a block directly inside a block
26 public loplugin::FilteringPlugin
<BlockBlock
>
29 explicit BlockBlock(loplugin::InstantiationData
const & data
):
30 FilteringPlugin(data
) {}
32 virtual bool preRun() override
34 StringRef
fn(handler
.getMainFileName());
35 if (loplugin::isSamePathname(fn
, SRCDIR
"/sal/osl/unx/file_misc.cxx"))
42 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
46 bool VisitCompoundStmt(CompoundStmt
const * );
47 bool VisitCaseStmt(CaseStmt
const * );
50 bool BlockBlock::VisitCompoundStmt(CompoundStmt
const * compound
)
52 if (ignoreLocation(compound
))
54 if (compound
->size() != 1)
56 auto inner
= *compound
->body_begin();
57 if (!isa
<CompoundStmt
>(inner
))
59 if (compiler
.getSourceManager().isMacroBodyExpansion(compound
->getBeginLoc()))
61 if (compiler
.getSourceManager().isMacroBodyExpansion(inner
->getBeginLoc()))
63 if (containsPreprocessingConditionalInclusion(compound
->getSourceRange())) {
67 DiagnosticsEngine::Warning
,
68 "block directly inside block",
69 compound
->getBeginLoc())
70 << compound
->getSourceRange();
72 DiagnosticsEngine::Note
,
75 << inner
->getSourceRange();
79 bool BlockBlock::VisitCaseStmt(CaseStmt
const * caseStmt
)
81 if (ignoreLocation(caseStmt
))
83 auto compoundStmt
= dyn_cast
<CompoundStmt
>(caseStmt
->getSubStmt());
86 if (compoundStmt
->size() != 2)
88 auto it
= compoundStmt
->body_begin();
90 if (!isa
<CompoundStmt
>(inner1
))
93 if (!isa
<BreakStmt
>(*it
))
96 DiagnosticsEngine::Warning
,
97 "block directly inside block",
98 compoundStmt
->getBeginLoc())
99 << compoundStmt
->getSourceRange();
103 loplugin::Plugin::Registration
< BlockBlock
> blockblock("blockblock", true);
107 #endif // LO_CLANG_SHARED_PLUGINS
109 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */