https://github.com/steffenlarsen created 
https://github.com/llvm/llvm-project/pull/184352

OptionCategory is immutable after construction, as all its members are private 
and const. This patch makes it so that the implementations that register and 
pass around OptionCategory objects use const references and const pointers. 
This allows the global OptionCategory objects to be defined as const themselves.

As a follow-up to this patch, the OptionCategory objects defined throughout the 
codebase should be made const as well.

>From e42f3c40b86c630a04c019f07685b4ede1b1c2bd Mon Sep 17 00:00:00 2001
From: Steffen Holst Larsen <[email protected]>
Date: Tue, 3 Mar 2026 08:31:27 -0600
Subject: [PATCH] [LLVM][Support][NFCI] Register OptionCategory as const

OptionCategory is immutable after construction, as all its members are
private and const. This patch makes it so that the implementations that
register and pass around OptionCategory objects use const references and
const pointers. This allows the global OptionCategory objects to be
defined as const themselves.

As a follow-up to this patch, the OptionCategory objects defined
throughout the codebase should be made const as well.

Signed-off-by: Steffen Holst Larsen <[email protected]>
---
 .../clang/Tooling/CommonOptionsParser.h       |  6 ++---
 clang/lib/Tooling/CommonOptionsParser.cpp     |  6 ++---
 llvm/include/llvm/Support/CommandLine.h       | 14 +++++-----
 llvm/lib/Support/CommandLine.cpp              | 27 ++++++++++---------
 4 files changed, 27 insertions(+), 26 deletions(-)

diff --git a/clang/include/clang/Tooling/CommonOptionsParser.h 
b/clang/include/clang/Tooling/CommonOptionsParser.h
index 5e2cdc6ac4589..aa1ad5f2003af 100644
--- a/clang/include/clang/Tooling/CommonOptionsParser.h
+++ b/clang/include/clang/Tooling/CommonOptionsParser.h
@@ -79,7 +79,7 @@ class CommonOptionsParser {
   ///
   /// It also allows calls to set the required number of positional parameters.
   CommonOptionsParser(
-      int &argc, const char **argv, llvm::cl::OptionCategory &Category,
+      int &argc, const char **argv, const llvm::cl::OptionCategory &Category,
       llvm::cl::NumOccurrencesFlag OccurrencesFlag = llvm::cl::OneOrMore,
       const char *Overview = nullptr);
 
@@ -87,7 +87,7 @@ class CommonOptionsParser {
   /// A factory method that is similar to the above constructor, except
   /// this returns an error instead exiting the program on error.
   static llvm::Expected<CommonOptionsParser>
-  create(int &argc, const char **argv, llvm::cl::OptionCategory &Category,
+  create(int &argc, const char **argv, const llvm::cl::OptionCategory 
&Category,
          llvm::cl::NumOccurrencesFlag OccurrencesFlag = llvm::cl::OneOrMore,
          const char *Overview = nullptr);
 
@@ -111,7 +111,7 @@ class CommonOptionsParser {
   CommonOptionsParser() = default;
 
   llvm::Error init(int &argc, const char **argv,
-                   llvm::cl::OptionCategory &Category,
+                   const llvm::cl::OptionCategory &Category,
                    llvm::cl::NumOccurrencesFlag OccurrencesFlag,
                    const char *Overview);
 
diff --git a/clang/lib/Tooling/CommonOptionsParser.cpp 
b/clang/lib/Tooling/CommonOptionsParser.cpp
index c8c3ca98323e2..6d65482267719 100644
--- a/clang/lib/Tooling/CommonOptionsParser.cpp
+++ b/clang/lib/Tooling/CommonOptionsParser.cpp
@@ -81,7 +81,7 @@ std::vector<CompileCommand> 
ArgumentsAdjustingCompilations::adjustCommands(
 }
 
 llvm::Error CommonOptionsParser::init(
-    int &argc, const char **argv, cl::OptionCategory &Category,
+    int &argc, const char **argv, const cl::OptionCategory &Category,
     llvm::cl::NumOccurrencesFlag OccurrencesFlag, const char *Overview) {
 
   static cl::opt<std::string> BuildPath("p", cl::desc("Build path"),
@@ -153,7 +153,7 @@ llvm::Error CommonOptionsParser::init(
 }
 
 llvm::Expected<CommonOptionsParser> CommonOptionsParser::create(
-    int &argc, const char **argv, llvm::cl::OptionCategory &Category,
+    int &argc, const char **argv, const llvm::cl::OptionCategory &Category,
     llvm::cl::NumOccurrencesFlag OccurrencesFlag, const char *Overview) {
   CommonOptionsParser Parser;
   llvm::Error Err =
@@ -164,7 +164,7 @@ llvm::Expected<CommonOptionsParser> 
CommonOptionsParser::create(
 }
 
 CommonOptionsParser::CommonOptionsParser(
-    int &argc, const char **argv, cl::OptionCategory &Category,
+    int &argc, const char **argv, const cl::OptionCategory &Category,
     llvm::cl::NumOccurrencesFlag OccurrencesFlag, const char *Overview) {
   llvm::Error Err = init(argc, argv, Category, OccurrencesFlag, Overview);
   if (Err) {
diff --git a/llvm/include/llvm/Support/CommandLine.h 
b/llvm/include/llvm/Support/CommandLine.h
index be754f3c159ca..ba4700065e9e3 100644
--- a/llvm/include/llvm/Support/CommandLine.h
+++ b/llvm/include/llvm/Support/CommandLine.h
@@ -182,7 +182,7 @@ class OptionCategory {
   StringRef const Name;
   StringRef const Description;
 
-  LLVM_ABI void registerCategory();
+  LLVM_ABI void registerCategory() const;
 
 public:
   OptionCategory(StringRef const Name,
@@ -196,7 +196,7 @@ class OptionCategory {
 };
 
 // The general Option Category (used as default category).
-LLVM_ABI OptionCategory &getGeneralCategory();
+LLVM_ABI const OptionCategory &getGeneralCategory();
 
 
//===----------------------------------------------------------------------===//
 //
@@ -283,7 +283,7 @@ class LLVM_ABI Option {
   StringRef ArgStr;   // The argument string itself (ex: "help", "o")
   StringRef HelpStr;  // The descriptive text message for -help
   StringRef ValueStr; // String describing what the value of this option is
-  SmallVector<OptionCategory *, 1>
+  SmallVector<const OptionCategory *, 1>
       Categories;                    // The Categories this option belongs to
   SmallPtrSet<SubCommand *, 1> Subs; // The subcommands this option belongs to.
 
@@ -329,7 +329,7 @@ class LLVM_ABI Option {
   void setFormattingFlag(enum FormattingFlags V) { Formatting = V; }
   void setMiscFlag(enum MiscFlags M) { Misc |= M; }
   void setPosition(unsigned pos) { Position = pos; }
-  void addCategory(OptionCategory &C);
+  void addCategory(const OptionCategory &C);
   void addSubCommand(SubCommand &S) { Subs.insert(&S); }
 
 protected:
@@ -467,9 +467,9 @@ template <class Ty> LocationClass<Ty> location(Ty &L) {
 
 // Specify the Option category for the command line argument to belong to.
 struct cat {
-  OptionCategory &Category;
+  const OptionCategory &Category;
 
-  cat(OptionCategory &c) : Category(c) {}
+  cat(const OptionCategory &c) : Category(c) {}
 
   template <class Opt> void apply(Opt &O) const { O.addCategory(Category); }
 };
@@ -2306,7 +2306,7 @@ LLVM_ABI bool expandResponseFiles(int Argc, const char 
*const *Argv,
 /// Some tools (like clang-format) like to be able to hide all options that are
 /// not specific to the tool. This function allows a tool to specify a single
 /// option category to display in the -help output.
-LLVM_ABI void HideUnrelatedOptions(cl::OptionCategory &Category,
+LLVM_ABI void HideUnrelatedOptions(const cl::OptionCategory &Category,
                                    SubCommand &Sub = 
SubCommand::getTopLevel());
 
 /// Mark all options not part of the categories as cl::ReallyHidden.
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp
index 98d80c62466c0..9f03957c76b13 100644
--- a/llvm/lib/Support/CommandLine.cpp
+++ b/llvm/lib/Support/CommandLine.cpp
@@ -179,7 +179,7 @@ class CommandLineParser {
   SmallVector<Option*, 4> DefaultOptions;
 
   // This collects the different option categories that have been registered.
-  SmallPtrSet<OptionCategory *, 16> RegisteredOptionCategories;
+  SmallPtrSet<const OptionCategory *, 16> RegisteredOptionCategories;
 
   // This collects the different subcommands that have been registered.
   SmallPtrSet<SubCommand *, 4> RegisteredSubCommands;
@@ -346,7 +346,7 @@ class CommandLineParser {
 
   void printOptionValues();
 
-  void registerCategory(OptionCategory *cat) {
+  void registerCategory(const OptionCategory *cat) {
     assert(count_if(RegisteredOptionCategories,
                     [cat](const OptionCategory *Category) {
              return cat->getName() == Category->getName();
@@ -465,7 +465,7 @@ void Option::setArgStr(StringRef S) {
     setMiscFlag(Grouping);
 }
 
-void Option::addCategory(OptionCategory &C) {
+void Option::addCategory(const OptionCategory &C) {
   assert(!Categories.empty() && "Categories cannot be empty.");
   // Maintain backward compatibility by replacing the default GeneralCategory
   // if it's still set.  Otherwise, just add the new one.  The GeneralCategory
@@ -483,7 +483,7 @@ void Option::reset() {
     removeArgument();
 }
 
-void OptionCategory::registerCategory() {
+void OptionCategory::registerCategory() const {
   GlobalParser->registerCategory(this);
 }
 
@@ -2453,8 +2453,8 @@ class CategorizedHelpPrinter : public HelpPrinter {
   // It shall return a negative value if A's name should be lexicographically
   // ordered before B's name. It returns a value greater than zero if B's name
   // should be ordered before A's name, and it returns 0 otherwise.
-  static int OptionCategoryCompare(OptionCategory *const *A,
-                                   OptionCategory *const *B) {
+  static int OptionCategoryCompare(const OptionCategory *const *A,
+                                   const OptionCategory *const *B) {
     return (*A)->getName().compare((*B)->getName());
   }
 
@@ -2463,8 +2463,8 @@ class CategorizedHelpPrinter : public HelpPrinter {
 
 protected:
   void printOptions(StrOptionPairVector &Opts, size_t MaxArgLen) override {
-    std::vector<OptionCategory *> SortedCategories;
-    DenseMap<OptionCategory *, std::vector<Option *>> CategorizedOptions;
+    std::vector<const OptionCategory *> SortedCategories;
+    DenseMap<const OptionCategory *, std::vector<Option *>> CategorizedOptions;
 
     // Collect registered option categories into vector in preparation for
     // sorting.
@@ -2481,7 +2481,7 @@ class CategorizedHelpPrinter : public HelpPrinter {
     // options within categories will also be alphabetically sorted.
     for (const auto &I : Opts) {
       Option *Opt = I.second;
-      for (OptionCategory *Cat : Opt->Categories) {
+      for (const OptionCategory *Cat : Opt->Categories) {
         assert(llvm::is_contained(SortedCategories, Cat) &&
                "Option has an unregistered category");
         CategorizedOptions[Cat].push_back(Opt);
@@ -2489,7 +2489,7 @@ class CategorizedHelpPrinter : public HelpPrinter {
     }
 
     // Now do printing.
-    for (OptionCategory *Category : SortedCategories) {
+    for (const OptionCategory *Category : SortedCategories) {
       // Hide empty categories for --help, but show for --help-hidden.
       const auto &CategoryOptions = CategorizedOptions[Category];
       if (CategoryOptions.empty())
@@ -2691,9 +2691,9 @@ static void initCommonOptions() {
   initRandomSeedOptions();
 }
 
-OptionCategory &cl::getGeneralCategory() {
+const OptionCategory &cl::getGeneralCategory() {
   // Initialise the general option category.
-  static OptionCategory GeneralCategory{"General options"};
+  static const OptionCategory GeneralCategory{"General options"};
   return GeneralCategory;
 }
 
@@ -2831,7 +2831,8 @@ cl::getRegisteredSubcommands() {
   return GlobalParser->getRegisteredSubcommands();
 }
 
-void cl::HideUnrelatedOptions(cl::OptionCategory &Category, SubCommand &Sub) {
+void cl::HideUnrelatedOptions(const cl::OptionCategory &Category,
+                              SubCommand &Sub) {
   initCommonOptions();
   for (auto &I : Sub.OptionsMap) {
     bool Unrelated = true;

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to