https://github.com/steffenlarsen created https://github.com/llvm/llvm-project/pull/183478
To avoid modifications to global state that does not currently need to be modified, this patch makes a selection of trivial cases const. This aims to help preserve the intention of these variables and reduce the potential mutable global state surface of clang. >From a41f24b29f8d3b6056388a82120247c801b4319d Mon Sep 17 00:00:00 2001 From: Steffen Holst Larsen <[email protected]> Date: Wed, 25 Feb 2026 08:14:05 -0600 Subject: [PATCH] [Clang][NFCI] Make unchanged global state const To avoid modifications to global state that does not currently need to be modified, this patch makes a selection of trivial cases const. This aims to help preserve the intention of these variables and reduce the potential mutable global state surface of clang. Signed-off-by: Steffen Holst Larsen <[email protected]> --- clang/include/clang/Basic/TargetCXXABI.h | 4 ++-- .../StaticAnalyzer/Core/AnalyzerOptions.h | 18 ++++++++++-------- .../PathSensitive/RangedConstraintManager.h | 2 +- clang/lib/AST/TypePrinter.cpp | 8 +++++--- clang/lib/Basic/Targets/AVR.cpp | 2 +- clang/lib/CodeGen/CGBuiltin.cpp | 8 ++++---- clang/lib/CodeGen/ModuleBuilder.cpp | 2 +- clang/lib/Driver/Distro.cpp | 2 +- clang/lib/Options/DriverOptions.cpp | 2 +- .../Sema/SemaOpenACCClauseAppertainment.cpp | 2 +- .../StaticAnalyzer/Checkers/ErrnoModeling.cpp | 2 +- .../Core/RangeConstraintManager.cpp | 2 +- 12 files changed, 29 insertions(+), 25 deletions(-) diff --git a/clang/include/clang/Basic/TargetCXXABI.h b/clang/include/clang/Basic/TargetCXXABI.h index d204452afbf4b..aadda3852ed11 100644 --- a/clang/include/clang/Basic/TargetCXXABI.h +++ b/clang/include/clang/Basic/TargetCXXABI.h @@ -40,7 +40,7 @@ class TargetCXXABI { Kind TheKind; static const auto &getABIMap() { - static llvm::StringMap<Kind> ABIMap = { + static const llvm::StringMap<Kind> ABIMap = { #define CXXABI(Name, Str) {Str, Name}, #include "TargetCXXABI.def" }; @@ -48,7 +48,7 @@ class TargetCXXABI { } static const auto &getSpellingMap() { - static std::map<Kind, std::string> SpellingMap = { + static const std::map<Kind, std::string> SpellingMap = { #define CXXABI(Name, Str) {Name, Str}, #include "TargetCXXABI.def" }; diff --git a/clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h b/clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h index 7d0c2d8658f35..f57d3871fe75b 100644 --- a/clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h +++ b/clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h @@ -292,9 +292,10 @@ class AnalyzerOptions { #undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE bool isUnknownAnalyzerConfig(llvm::StringRef Name) { - static std::vector<llvm::StringLiteral> AnalyzerConfigCmdFlags = []() { - // Create an array of all -analyzer-config command line options. - std::vector<llvm::StringLiteral> AnalyzerConfigCmdFlags = { + static const std::vector<llvm::StringLiteral> AnalyzerConfigCmdFlags = + []() { + // Create an array of all -analyzer-config command line options. + std::vector<llvm::StringLiteral> AnalyzerConfigCmdFlags = { #define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC, \ SHALLOW_VAL, DEEP_VAL) \ ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, SHALLOW_VAL) @@ -305,11 +306,12 @@ class AnalyzerOptions { #include "clang/StaticAnalyzer/Core/AnalyzerOptions.def" #undef ANALYZER_OPTION #undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE - }; - // FIXME: Sort this at compile-time when we get constexpr sort (C++20). - llvm::sort(AnalyzerConfigCmdFlags); - return AnalyzerConfigCmdFlags; - }(); + }; + // FIXME: Sort this at compile-time when we get constexpr sort + // (C++20). + llvm::sort(AnalyzerConfigCmdFlags); + return AnalyzerConfigCmdFlags; + }(); return !llvm::binary_search(AnalyzerConfigCmdFlags, Name); } diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/RangedConstraintManager.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/RangedConstraintManager.h index 49ea006e27aa5..4ed4d3c738444 100644 --- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/RangedConstraintManager.h +++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/RangedConstraintManager.h @@ -301,7 +301,7 @@ class RangeSet { // Usually we deal with the same ranges and range sets over and over. // Here we track all created containers and try not to repeat ourselves. llvm::FoldingSet<ContainerType> Cache; - static ContainerType EmptySet; + static const ContainerType EmptySet; }; RangeSet(const RangeSet &) = default; diff --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp index d8a48af62bb75..60f319c699e91 100644 --- a/clang/lib/AST/TypePrinter.cpp +++ b/clang/lib/AST/TypePrinter.cpp @@ -1351,12 +1351,14 @@ void TypePrinter::printUnaryTransformBefore(const UnaryTransformType *T, raw_ostream &OS) { IncludeStrongLifetimeRAII Strong(Policy); - static llvm::DenseMap<int, const char *> Transformation = {{ + static const llvm::DenseMap<int, const char *> Transformation = { + { #define TRANSFORM_TYPE_TRAIT_DEF(Enum, Trait) \ {UnaryTransformType::Enum, "__" #Trait}, #include "clang/Basic/TransformTypeTraits.def" - }}; - OS << Transformation[T->getUTTKind()] << '('; + } + }; + OS << Transformation.lookup(T->getUTTKind()) << '('; print(T->getBaseType(), OS, StringRef()); OS << ')'; spaceBeforePlaceHolder(OS); diff --git a/clang/lib/Basic/Targets/AVR.cpp b/clang/lib/Basic/Targets/AVR.cpp index 90b4ac1b857cc..75144099b2bdd 100644 --- a/clang/lib/Basic/Targets/AVR.cpp +++ b/clang/lib/Basic/Targets/AVR.cpp @@ -29,7 +29,7 @@ struct LLVM_LIBRARY_VISIBILITY MCUInfo { }; // NOTE: This list has been synchronized with gcc-avr 5.4.0 and avr-libc 2.0.0. -static MCUInfo AVRMcus[] = { +static const MCUInfo AVRMcus[] = { {"avr1", nullptr, "1", 0}, {"at90s1200", "__AVR_AT90S1200__", "1", 0}, {"attiny11", "__AVR_ATtiny11__", "1", 0}, diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 850cc8d2c4c45..81d1effdd560b 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -187,7 +187,7 @@ llvm::Constant *CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD, // TODO: This list should be expanded or refactored after all GCC-compatible // std libcall builtins are implemented. - static SmallDenseMap<unsigned, StringRef, 64> F128Builtins{ + static const SmallDenseMap<unsigned, StringRef, 64> F128Builtins{ {Builtin::BI__builtin___fprintf_chk, "__fprintf_chkieee128"}, {Builtin::BI__builtin___printf_chk, "__printf_chkieee128"}, {Builtin::BI__builtin___snprintf_chk, "__snprintf_chkieee128"}, @@ -216,7 +216,7 @@ llvm::Constant *CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD, // The AIX library functions frexpl, ldexpl, and modfl are for 128-bit // IBM 'long double' (i.e. __ibm128). Map to the 'double' versions // if it is 64-bit 'long double' mode. - static SmallDenseMap<unsigned, StringRef, 4> AIXLongDouble64Builtins{ + static const SmallDenseMap<unsigned, StringRef, 4> AIXLongDouble64Builtins{ {Builtin::BI__builtin_frexpl, "frexp"}, {Builtin::BI__builtin_ldexpl, "ldexp"}, {Builtin::BI__builtin_modfl, "modf"}, @@ -233,12 +233,12 @@ llvm::Constant *CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD, if (getTriple().isPPC64() && &getTarget().getLongDoubleFormat() == &llvm::APFloat::IEEEquad() && F128Builtins.contains(BuiltinID)) - Name = F128Builtins[BuiltinID]; + Name = F128Builtins.lookup(BuiltinID); else if (getTriple().isOSAIX() && &getTarget().getLongDoubleFormat() == &llvm::APFloat::IEEEdouble() && AIXLongDouble64Builtins.contains(BuiltinID)) - Name = AIXLongDouble64Builtins[BuiltinID]; + Name = AIXLongDouble64Builtins.lookup(BuiltinID); else Name = Context.BuiltinInfo.getName(BuiltinID).substr(10); } diff --git a/clang/lib/CodeGen/ModuleBuilder.cpp b/clang/lib/CodeGen/ModuleBuilder.cpp index b4885d572c294..c364d5c31513c 100644 --- a/clang/lib/CodeGen/ModuleBuilder.cpp +++ b/clang/lib/CodeGen/ModuleBuilder.cpp @@ -393,7 +393,7 @@ namespace clang { namespace CodeGen { std::optional<std::pair<StringRef, StringRef>> DemangleTrapReasonInDebugInfo(StringRef FuncName) { - static auto TrapRegex = + static const auto TrapRegex = llvm::Regex(llvm::formatv("^{0}\\$(.*)\\$(.*)$", ClangTrapPrefix).str()); llvm::SmallVector<llvm::StringRef, 3> Matches; std::string *ErrorPtr = nullptr; diff --git a/clang/lib/Driver/Distro.cpp b/clang/lib/Driver/Distro.cpp index df10458d092d6..37be87b57b9a7 100644 --- a/clang/lib/Driver/Distro.cpp +++ b/clang/lib/Driver/Distro.cpp @@ -224,7 +224,7 @@ static Distro::DistroType GetDistro(llvm::vfs::FileSystem &VFS, if (onRealFS) { // If we're backed by a real file system, perform // the detection only once and save the result. - static Distro::DistroType LinuxDistro = DetectDistro(VFS); + static const Distro::DistroType LinuxDistro = DetectDistro(VFS); return LinuxDistro; } // This is mostly for passing tests which uses llvm::vfs::InMemoryFileSystem, diff --git a/clang/lib/Options/DriverOptions.cpp b/clang/lib/Options/DriverOptions.cpp index d91e9291fb2f6..c9c826a439123 100644 --- a/clang/lib/Options/DriverOptions.cpp +++ b/clang/lib/Options/DriverOptions.cpp @@ -46,6 +46,6 @@ class DriverOptTable : public PrecomputedOptTable { } // anonymous namespace const llvm::opt::OptTable &clang::getDriverOptTable() { - static DriverOptTable Table; + static const DriverOptTable Table; return Table; } diff --git a/clang/lib/Sema/SemaOpenACCClauseAppertainment.cpp b/clang/lib/Sema/SemaOpenACCClauseAppertainment.cpp index 5aef24512a6ec..9831ea2110d10 100644 --- a/clang/lib/Sema/SemaOpenACCClauseAppertainment.cpp +++ b/clang/lib/Sema/SemaOpenACCClauseAppertainment.cpp @@ -72,7 +72,7 @@ struct LLVMDirectiveClauseRelationships { #include "llvm/Frontend/OpenACC/ACC.inc" namespace { -LLVMDirectiveClauseRelationships Relations[] = +const LLVMDirectiveClauseRelationships Relations[] = #define GEN_CLANG_DIRECTIVE_CLAUSE_MAP #include "llvm/Frontend/OpenACC/ACC.inc" ; diff --git a/clang/lib/StaticAnalyzer/Checkers/ErrnoModeling.cpp b/clang/lib/StaticAnalyzer/Checkers/ErrnoModeling.cpp index 46c7a6cd9a4dc..47988b79ef32b 100644 --- a/clang/lib/StaticAnalyzer/Checkers/ErrnoModeling.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/ErrnoModeling.cpp @@ -40,7 +40,7 @@ const char *ErrnoVarName = "errno"; // Names of functions that return a location of the "errno" value. // FIXME: Are there other similar function names? -CallDescriptionSet ErrnoLocationCalls{ +const CallDescriptionSet ErrnoLocationCalls{ {CDM::CLibrary, {"__errno_location"}, 0, 0}, {CDM::CLibrary, {"___errno"}, 0, 0}, {CDM::CLibrary, {"__errno"}, 0, 0}, diff --git a/clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp b/clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp index 245a73047513b..0719820aa085e 100644 --- a/clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp +++ b/clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp @@ -109,7 +109,7 @@ class OperatorRelationsTable { // RangeSet implementation //===----------------------------------------------------------------------===// -RangeSet::ContainerType RangeSet::Factory::EmptySet{}; +const RangeSet::ContainerType RangeSet::Factory::EmptySet{}; RangeSet RangeSet::Factory::add(RangeSet LHS, RangeSet RHS) { ContainerType Result; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
