[clang] b675369 - [Clang][NFCI] Make unchanged global state const (#183478)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 3 00:07:13 PST 2026
Author: Steffen Larsen
Date: 2026-03-03T09:07:08+01:00
New Revision: b67536954eb1ce439b203073ba297f557a153eaa
URL: https://github.com/llvm/llvm-project/commit/b67536954eb1ce439b203073ba297f557a153eaa
DIFF: https://github.com/llvm/llvm-project/commit/b67536954eb1ce439b203073ba297f557a153eaa.diff
LOG: [Clang][NFCI] Make unchanged global state const (#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.
---------
Signed-off-by: Steffen Holst Larsen <sholstla at amd.com>
Added:
Modified:
clang/include/clang/Basic/TargetCXXABI.h
clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h
clang/include/clang/StaticAnalyzer/Core/PathSensitive/RangedConstraintManager.h
clang/lib/AST/TypePrinter.cpp
clang/lib/Basic/Targets/AVR.cpp
clang/lib/CodeGen/CGBuiltin.cpp
clang/lib/CodeGen/ModuleBuilder.cpp
clang/lib/Driver/Distro.cpp
clang/lib/Options/DriverOptions.cpp
clang/lib/Sema/SemaOpenACCClauseAppertainment.cpp
clang/lib/StaticAnalyzer/Checkers/ErrnoModeling.cpp
clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp
Removed:
################################################################################
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..f0fa68008fc4d 100644
--- a/clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h
+++ b/clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h
@@ -292,9 +292,9 @@ class AnalyzerOptions {
#undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE
bool isUnknownAnalyzerConfig(llvm::StringRef Name) {
- static std::vector<llvm::StringLiteral> AnalyzerConfigCmdFlags = []() {
+ static const auto AnalyzerConfigCmdFlags = []() {
// Create an array of all -analyzer-config command line options.
- std::vector<llvm::StringLiteral> AnalyzerConfigCmdFlags = {
+ std::array AnalyzerConfigCmdFlags{
#define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC, \
SHALLOW_VAL, DEEP_VAL) \
ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, SHALLOW_VAL)
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..07a0c39bd93f1 100644
--- a/clang/lib/AST/TypePrinter.cpp
+++ b/clang/lib/AST/TypePrinter.cpp
@@ -1351,12 +1351,12 @@ 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 38010cad75244..6fb43d5cb0fbf 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;
More information about the cfe-commits
mailing list