1 //===--- StaticallyConstructedObjectsCheck.cpp - clang-tidy----------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "StaticallyConstructedObjectsCheck.h" 10 11 using namespace clang::ast_matchers; 12 13 namespace clang { 14 namespace tidy { 15 namespace fuchsia { 16 17 namespace { 18 AST_MATCHER(Expr, isConstantInitializer) { 19 return Node.isConstantInitializer(Finder->getASTContext(), false); 20 } 21 22 AST_MATCHER(VarDecl, isGlobalStatic) { 23 return Node.getStorageDuration() == SD_Static && !Node.isLocalVarDecl(); 24 } 25 } // namespace 26 27 void StaticallyConstructedObjectsCheck::registerMatchers(MatchFinder *Finder) { 28 // Constructing global, non-trivial objects with static storage is 29 // disallowed, unless the object is statically initialized with a constexpr 30 // constructor or has no explicit constructor. 31 Finder->addMatcher( 32 traverse(TK_AsIs, 33 varDecl( 34 // Match global, statically stored objects... 35 isGlobalStatic(), 36 // ... that have C++ constructors... 37 hasDescendant(cxxConstructExpr(unless(allOf( 38 // ... unless it is constexpr ... 39 hasDeclaration(cxxConstructorDecl(isConstexpr())), 40 // ... and is statically initialized. 41 isConstantInitializer()))))) 42 .bind("decl")), 43 this); 44 } 45 46 void StaticallyConstructedObjectsCheck::check( 47 const MatchFinder::MatchResult &Result) { 48 if (const auto *D = Result.Nodes.getNodeAs<VarDecl>("decl")) 49 diag(D->getBeginLoc(), "static objects are disallowed; if possible, use a " 50 "constexpr constructor instead"); 51 } 52 53 } // namespace fuchsia 54 } // namespace tidy 55 } // namespace clang 56