2018-01-12 05:17:43 +08:00
|
|
|
//===--- StaticallyConstructedObjectsCheck.cpp - clang-tidy----------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-01-12 05:17:43 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "StaticallyConstructedObjectsCheck.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace fuchsia {
|
2018-02-19 03:02:35 +08:00
|
|
|
|
|
|
|
namespace {
|
2018-01-12 05:17:43 +08:00
|
|
|
AST_MATCHER(Expr, isConstantInitializer) {
|
|
|
|
return Node.isConstantInitializer(Finder->getASTContext(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
AST_MATCHER(VarDecl, isGlobalStatic) {
|
|
|
|
return Node.getStorageDuration() == SD_Static && !Node.isLocalVarDecl();
|
|
|
|
}
|
2018-02-19 03:02:35 +08:00
|
|
|
} // namespace
|
|
|
|
|
2018-01-12 05:17:43 +08:00
|
|
|
void StaticallyConstructedObjectsCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
// Constructing global, non-trivial objects with static storage is
|
2020-12-11 07:52:35 +08:00
|
|
|
// disallowed, unless the object is statically initialized with a constexpr
|
2018-01-12 05:17:43 +08:00
|
|
|
// constructor or has no explicit constructor.
|
2019-11-12 23:15:56 +08:00
|
|
|
Finder->addMatcher(
|
2020-12-11 07:52:35 +08:00
|
|
|
traverse(TK_AsIs,
|
2019-11-12 23:15:56 +08:00
|
|
|
varDecl(
|
|
|
|
// Match global, statically stored objects...
|
|
|
|
isGlobalStatic(),
|
|
|
|
// ... that have C++ constructors...
|
|
|
|
hasDescendant(cxxConstructExpr(unless(allOf(
|
|
|
|
// ... unless it is constexpr ...
|
|
|
|
hasDeclaration(cxxConstructorDecl(isConstexpr())),
|
|
|
|
// ... and is statically initialized.
|
|
|
|
isConstantInitializer())))))
|
|
|
|
.bind("decl")),
|
|
|
|
this);
|
2018-01-12 05:17:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StaticallyConstructedObjectsCheck::check(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
if (const auto *D = Result.Nodes.getNodeAs<VarDecl>("decl"))
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(D->getBeginLoc(), "static objects are disallowed; if possible, use a "
|
2018-01-12 05:17:43 +08:00
|
|
|
"constexpr constructor instead");
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace fuchsia
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|