[clang-tools-extra] [clang-tidy] Add new check 'misc-static-initialization-cycle' (PR #175342)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Fri Feb 6 13:50:00 PST 2026
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>,
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>,
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>,
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>,
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>,
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>,
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>,
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/175342 at github.com>
================
@@ -0,0 +1,355 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "StaticInitializationCycleCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/DynamicRecursiveASTVisitor.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Analysis/CallGraph.h"
+#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/SCCIterator.h"
+
+using namespace clang;
+using namespace clang::ast_matchers;
+
+// Compute (for the purpose of this check) if the value of a DeclRefExpr is used
+// (at runtime).
+// The value is not used if it appears at LHS of an assignment or it appears
+// inside a compile-time constant expression (like 'sizeof').
+static bool isUnusedValue(const DeclRefExpr *DRE, ASTContext &ACtx) {
+ ParentMapContext &PMC = ACtx.getParentMapContext();
+ DynTypedNodeList Parents = PMC.getParents(*DRE);
+ const BinaryOperator *ParentBO = nullptr;
+ while (!Parents.empty()) {
+ if (const Expr *E = Parents[0].get<Expr>()) {
+ if (E->isIntegerConstantExpr(ACtx))
+ return true;
+ if ((ParentBO = dyn_cast<BinaryOperator>(E)))
+ break;
+ }
+ Parents = PMC.getParents(Parents[0]);
+ }
----------------
vbvictor wrote:
Could we stop traversing up when some condition is met, e.g. when we hit `CompountStmt`. Walking up to `TranslationUnitDecl` for each `DeclRefExpr` seems like a very expensive operation.
https://github.com/llvm/llvm-project/pull/175342
More information about the cfe-commits
mailing list