https://github.com/RIscRIpt created https://github.com/llvm/llvm-project/pull/182020
This PR reapplies #181367 with a fix (see fixup commit). >From a0554e049b1e400db36374e6374a3c6c29300f7e Mon Sep 17 00:00:00 2001 From: Richard Dzenis <[email protected]> Date: Wed, 18 Feb 2026 14:44:26 +0200 Subject: [PATCH 1/2] Reapply "[clang] Fix some static initialization race-conditions" (#181926) This reverts commit d4b574266132e08ff585facf2e0e01995082999f. --- clang/lib/AST/Stmt.cpp | 47 ++++++++++--------- clang/lib/Basic/ParsedAttrInfo.cpp | 17 ++++--- clang/lib/CodeGen/CodeGenAction.cpp | 4 +- .../Checkers/BasicObjCFoundationChecks.cpp | 16 +++---- 4 files changed, 44 insertions(+), 40 deletions(-) diff --git a/clang/lib/AST/Stmt.cpp b/clang/lib/AST/Stmt.cpp index 5b745dd3c43f5..14e6ea8d1d10e 100644 --- a/clang/lib/AST/Stmt.cpp +++ b/clang/lib/AST/Stmt.cpp @@ -40,6 +40,7 @@ #include "llvm/Support/MathExtras.h" #include "llvm/Support/raw_ostream.h" #include <algorithm> +#include <array> #include <cassert> #include <cstring> #include <optional> @@ -57,25 +58,23 @@ using namespace clang; #define ABSTRACT_STMT(STMT) #include "clang/AST/StmtNodes.inc" -static struct StmtClassNameTable { +struct StmtClassNameTable { const char *Name; unsigned Counter; unsigned Size; -} StmtClassInfo[Stmt::lastStmtConstant+1]; +}; static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) { - static bool Initialized = false; - if (Initialized) - return StmtClassInfo[E]; - - // Initialize the table on the first use. - Initialized = true; + static std::array<StmtClassNameTable, Stmt::lastStmtConstant + 1> + StmtClassInfo = [] { + std::array<StmtClassNameTable, Stmt::lastStmtConstant + 1> Table; #define ABSTRACT_STMT(STMT) -#define STMT(CLASS, PARENT) \ - StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \ - StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS); +#define STMT(CLASS, PARENT) \ + Table[static_cast<unsigned>(Stmt::CLASS##Class)].Name = #CLASS; \ + Table[static_cast<unsigned>(Stmt::CLASS##Class)].Size = sizeof(CLASS); #include "clang/AST/StmtNodes.inc" - + return Table; + }(); return StmtClassInfo[E]; } @@ -85,7 +84,7 @@ void *Stmt::operator new(size_t bytes, const ASTContext& C, } const char *Stmt::getStmtClassName() const { - return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name; + return getStmtInfoTableEntry(static_cast<StmtClass>(StmtBits.sClass)).Name; } // Check that no statement / expression class is polymorphic. LLVM style RTTI @@ -113,19 +112,25 @@ void Stmt::PrintStats() { unsigned sum = 0; llvm::errs() << "\n*** Stmt/Expr Stats:\n"; for (int i = 0; i != Stmt::lastStmtConstant+1; i++) { - if (StmtClassInfo[i].Name == nullptr) continue; - sum += StmtClassInfo[i].Counter; + const StmtClassNameTable &Entry = + getStmtInfoTableEntry(static_cast<Stmt::StmtClass>(i)); + if (Entry.Name == nullptr) + continue; + sum += Entry.Counter; } llvm::errs() << " " << sum << " stmts/exprs total.\n"; sum = 0; for (int i = 0; i != Stmt::lastStmtConstant+1; i++) { - if (StmtClassInfo[i].Name == nullptr) continue; - if (StmtClassInfo[i].Counter == 0) continue; - llvm::errs() << " " << StmtClassInfo[i].Counter << " " - << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size - << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size + const StmtClassNameTable &Entry = + getStmtInfoTableEntry(static_cast<Stmt::StmtClass>(i)); + if (Entry.Name == nullptr) + continue; + if (Entry.Counter == 0) + continue; + llvm::errs() << " " << Entry.Counter << " " << Entry.Name << ", " + << Entry.Size << " each (" << Entry.Counter * Entry.Size << " bytes)\n"; - sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size; + sum += Entry.Counter * Entry.Size; } llvm::errs() << "Total bytes = " << sum << "\n"; diff --git a/clang/lib/Basic/ParsedAttrInfo.cpp b/clang/lib/Basic/ParsedAttrInfo.cpp index 16fa314b642b9..d5b17b34b6e3a 100644 --- a/clang/lib/Basic/ParsedAttrInfo.cpp +++ b/clang/lib/Basic/ParsedAttrInfo.cpp @@ -20,13 +20,16 @@ using namespace clang; LLVM_INSTANTIATE_REGISTRY(ParsedAttrInfoRegistry) +static std::list<std::unique_ptr<ParsedAttrInfo>> instantiateEntries() { + std::list<std::unique_ptr<ParsedAttrInfo>> Instances; + for (const auto &It : ParsedAttrInfoRegistry::entries()) + Instances.emplace_back(It.instantiate()); + return Instances; +} + const std::list<std::unique_ptr<ParsedAttrInfo>> & clang::getAttributePluginInstances() { - static llvm::ManagedStatic<std::list<std::unique_ptr<ParsedAttrInfo>>> - PluginAttrInstances; - if (PluginAttrInstances->empty()) - for (const auto &It : ParsedAttrInfoRegistry::entries()) - PluginAttrInstances->emplace_back(It.instantiate()); - - return *PluginAttrInstances; + static std::list<std::unique_ptr<ParsedAttrInfo>> Instances = + instantiateEntries(); + return Instances; } diff --git a/clang/lib/CodeGen/CodeGenAction.cpp b/clang/lib/CodeGen/CodeGenAction.cpp index a5ef4ac9d361d..29dcabd1b0971 100644 --- a/clang/lib/CodeGen/CodeGenAction.cpp +++ b/clang/lib/CodeGen/CodeGenAction.cpp @@ -248,6 +248,8 @@ void BackendConsumer::HandleTranslationUnit(ASTContext &C) { LLVMContext &Ctx = getModule()->getContext(); std::unique_ptr<DiagnosticHandler> OldDiagnosticHandler = Ctx.getDiagnosticHandler(); + llvm::scope_exit RestoreDiagnosticHandler( + [&]() { Ctx.setDiagnosticHandler(std::move(OldDiagnosticHandler)); }); Ctx.setDiagnosticHandler(std::make_unique<ClangDiagnosticHandler>( CodeGenOpts, this)); @@ -311,8 +313,6 @@ void BackendConsumer::HandleTranslationUnit(ASTContext &C) { C.getTargetInfo().getDataLayoutString(), getModule(), Action, FS, std::move(AsmOutStream), this); - Ctx.setDiagnosticHandler(std::move(OldDiagnosticHandler)); - if (OptRecordFile) OptRecordFile->keep(); } diff --git a/clang/lib/StaticAnalyzer/Checkers/BasicObjCFoundationChecks.cpp b/clang/lib/StaticAnalyzer/Checkers/BasicObjCFoundationChecks.cpp index e682c4ef80896..f226f80aa441f 100644 --- a/clang/lib/StaticAnalyzer/Checkers/BasicObjCFoundationChecks.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/BasicObjCFoundationChecks.cpp @@ -70,16 +70,12 @@ enum FoundationClass { static FoundationClass findKnownClass(const ObjCInterfaceDecl *ID, bool IncludeSuperclasses = true) { - static llvm::StringMap<FoundationClass> Classes; - if (Classes.empty()) { - Classes["NSArray"] = FC_NSArray; - Classes["NSDictionary"] = FC_NSDictionary; - Classes["NSEnumerator"] = FC_NSEnumerator; - Classes["NSNull"] = FC_NSNull; - Classes["NSOrderedSet"] = FC_NSOrderedSet; - Classes["NSSet"] = FC_NSSet; - Classes["NSString"] = FC_NSString; - } + static const llvm::StringMap<FoundationClass> Classes{ + {"NSArray", FC_NSArray}, {"NSDictionary", FC_NSDictionary}, + {"NSEnumerator", FC_NSEnumerator}, {"NSNull", FC_NSNull}, + {"NSOrderedSet", FC_NSOrderedSet}, {"NSSet", FC_NSSet}, + {"NSString", FC_NSString}, + }; // FIXME: Should we cache this at all? FoundationClass result = Classes.lookup(ID->getIdentifier()->getName()); >From 6a06e07cbe63222a7da1c01e4d7f42437f917189 Mon Sep 17 00:00:00 2001 From: Richard Dzenis <[email protected]> Date: Wed, 18 Feb 2026 14:45:15 +0200 Subject: [PATCH 2/2] fixup! Reapply "[clang] Fix some static initialization race-conditions" (#181926) --- clang/lib/AST/Stmt.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/lib/AST/Stmt.cpp b/clang/lib/AST/Stmt.cpp index 14e6ea8d1d10e..15d0e6435aaf3 100644 --- a/clang/lib/AST/Stmt.cpp +++ b/clang/lib/AST/Stmt.cpp @@ -67,7 +67,7 @@ struct StmtClassNameTable { static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) { static std::array<StmtClassNameTable, Stmt::lastStmtConstant + 1> StmtClassInfo = [] { - std::array<StmtClassNameTable, Stmt::lastStmtConstant + 1> Table; + std::array<StmtClassNameTable, Stmt::lastStmtConstant + 1> Table{}; #define ABSTRACT_STMT(STMT) #define STMT(CLASS, PARENT) \ Table[static_cast<unsigned>(Stmt::CLASS##Class)].Name = #CLASS; \ _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
