[clang] [llvm] [mlir] [TableGen] Add const variants of accessors for backend (PR #106658)

2024-09-04 Thread Rahul Joshi via cfe-commits


@@ -189,7 +189,7 @@ static StringRef NormalizeGNUAttrSpelling(StringRef 
AttrSpelling) {
 
 typedef std::vector> ParsedAttrMap;
 
-static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records,

jurahul wrote:

I propose the following next steps (once this is committed):

1. We need to send a PSA as current downstream backends may break due to this 
(fix is easy)
2. I'll work on migrating on of each MLIR. Clang, and may be another LLVM 
backends to use const.
3. For the rest, we need to chip away. May be we try to recruit volunteers?
4. Before we can deprecate the non-const functions completely, give enough time 
for downstream backends to change. Maybe this should be a part of the PSA in 
(1).
5. Delete the non-const functions here, and any other const vs non-const 
difference we may have introduced during the transition.

https://github.com/llvm/llvm-project/pull/106658
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [mlir] [TableGen] Add const variants of accessors for backend (PR #106658)

2024-09-04 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/106658
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [mlir] [TableGen] Add const variants of accessors for backend (PR #106658)

2024-09-04 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/106658
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [mlir] [TableGen] Add const variants of accessors for backend (PR #106658)

2024-09-04 Thread Rahul Joshi via cfe-commits


@@ -189,7 +189,7 @@ static StringRef NormalizeGNUAttrSpelling(StringRef 
AttrSpelling) {
 
 typedef std::vector> ParsedAttrMap;
 
-static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records,

jurahul wrote:

And here's for the entire MLIR tablegen code: 
https://github.com/llvm/llvm-project/commit/061076db7f668f67183e813db6d339a840a05414

So may be its not that involved. Note that this does not address all the const 
correctness issues, as const Record* function may themselves non-const 
pointers. So that needs to be fixed gradually as well. So I propose we get this 
commit in, and then the MLIR one as a follow on. I will see if I can do the 
clang one as well.

https://github.com/llvm/llvm-project/pull/106658
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [mlir] [TableGen] Add const variants of accessors for backend (PR #106658)

2024-09-04 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/106658
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [mlir] [TableGen] Add const variants of accessors for backend (PR #106658)

2024-09-04 Thread Rahul Joshi via cfe-commits


@@ -189,7 +189,7 @@ static StringRef NormalizeGNUAttrSpelling(StringRef 
AttrSpelling) {
 
 typedef std::vector> ParsedAttrMap;
 
-static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records,

jurahul wrote:

And for all clang-tablegen its here: 
https://github.com/llvm/llvm-project/commit/abf19e5d26ccbf0714fbb93dfc8c783da9e407a1

So looks like we may be able to migrate quickly, may be wait for a few days 
(weeks?) for downstream adoption and delete the non-const overloads.

https://github.com/llvm/llvm-project/pull/106658
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [mlir] [TableGen] Add const variants of accessors for backend (PR #106658)

2024-09-05 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul updated 
https://github.com/llvm/llvm-project/pull/106658

>From 664c9fa9ade11150d635b9dbfb4c1ada7e32e8cd Mon Sep 17 00:00:00 2001
From: Rahul Joshi 
Date: Thu, 29 Aug 2024 20:36:05 -0700
Subject: [PATCH] [TableGen] Add const variants of accessors for backend

Split RecordKeeper `getAllDerivedDefinitions` family of functions into
two variants: (a) non-const ones that return vectors of `Record *` and
(b) const ones, that return vectors of `const Record *`.

This will help gradual migration of backend to use `const RecordKeeper`
and by implication change code to work with const pointers.
---
 clang/utils/TableGen/ClangAttrEmitter.cpp |  4 +-
 clang/utils/TableGen/ClangSyntaxEmitter.cpp   |  2 +-
 llvm/include/llvm/TableGen/DirectiveEmitter.h |  5 +-
 llvm/include/llvm/TableGen/Record.h   | 34 --
 llvm/lib/TableGen/Record.cpp  | 65 +++
 .../TableGen/Basic/CodeGenIntrinsics.cpp  |  2 +-
 .../TableGen/Common/SubtargetFeatureInfo.cpp  |  2 +-
 .../TableGen/Common/SubtargetFeatureInfo.h|  2 +-
 llvm/utils/TableGen/ExegesisEmitter.cpp   |  2 +-
 llvm/utils/TableGen/GlobalISelEmitter.cpp |  2 +-
 llvm/utils/TableGen/SubtargetEmitter.cpp  |  2 +-
 llvm/utils/TableGen/TableGen.cpp  |  2 +-
 mlir/include/mlir/TableGen/GenInfo.h  |  6 +-
 mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp   | 28 
 mlir/tools/mlir-tblgen/OmpOpGen.cpp   |  2 +-
 mlir/tools/mlir-tblgen/OpDocGen.cpp   | 20 +++---
 mlir/tools/mlir-tblgen/OpInterfacesGen.cpp| 15 ++---
 mlir/tools/mlir-tblgen/RewriterGen.cpp|  8 +--
 .../tools/tblgen-to-irdl/OpDefinitionsGen.cpp |  7 +-
 19 files changed, 134 insertions(+), 76 deletions(-)

diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp 
b/clang/utils/TableGen/ClangAttrEmitter.cpp
index adbe6af62d5cbe..d24215d10f17c7 100644
--- a/clang/utils/TableGen/ClangAttrEmitter.cpp
+++ b/clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -189,7 +189,7 @@ static StringRef NormalizeGNUAttrSpelling(StringRef 
AttrSpelling) {
 
 typedef std::vector> ParsedAttrMap;
 
-static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records,
+static ParsedAttrMap getParsedAttrList(RecordKeeper &Records,
ParsedAttrMap *Dupes = nullptr,
bool SemaOnly = true) {
   std::vector Attrs = Records.getAllDerivedDefinitions("Attr");
@@ -4344,7 +4344,7 @@ static void GenerateAppertainsTo(const Record &Attr, 
raw_ostream &OS) {
 // written into OS and the checks for merging declaration attributes are
 // written into MergeOS.
 static void GenerateMutualExclusionsChecks(const Record &Attr,
-   const RecordKeeper &Records,
+   RecordKeeper &Records,
raw_ostream &OS,
raw_ostream &MergeDeclOS,
raw_ostream &MergeStmtOS) {
diff --git a/clang/utils/TableGen/ClangSyntaxEmitter.cpp 
b/clang/utils/TableGen/ClangSyntaxEmitter.cpp
index 9720d587318432..2a69e4c353b6b4 100644
--- a/clang/utils/TableGen/ClangSyntaxEmitter.cpp
+++ b/clang/utils/TableGen/ClangSyntaxEmitter.cpp
@@ -41,7 +41,7 @@ using llvm::formatv;
 // stable and useful way, where abstract Node subclasses correspond to ranges.
 class Hierarchy {
 public:
-  Hierarchy(const llvm::RecordKeeper &Records) {
+  Hierarchy(llvm::RecordKeeper &Records) {
 for (llvm::Record *T : Records.getAllDerivedDefinitions("NodeType"))
   add(T);
 for (llvm::Record *Derived : Records.getAllDerivedDefinitions("NodeType"))
diff --git a/llvm/include/llvm/TableGen/DirectiveEmitter.h 
b/llvm/include/llvm/TableGen/DirectiveEmitter.h
index 1121459be6ce7d..ca21c8fc101450 100644
--- a/llvm/include/llvm/TableGen/DirectiveEmitter.h
+++ b/llvm/include/llvm/TableGen/DirectiveEmitter.h
@@ -15,8 +15,7 @@ namespace llvm {
 // DirectiveBase.td and provides helper methods for accessing it.
 class DirectiveLanguage {
 public:
-  explicit DirectiveLanguage(const llvm::RecordKeeper &Records)
-  : Records(Records) {
+  explicit DirectiveLanguage(llvm::RecordKeeper &Records) : Records(Records) {
 const auto &DirectiveLanguages = getDirectiveLanguages();
 Def = DirectiveLanguages[0];
   }
@@ -71,7 +70,7 @@ class DirectiveLanguage {
 
 private:
   const llvm::Record *Def;
-  const llvm::RecordKeeper &Records;
+  llvm::RecordKeeper &Records;
 
   std::vector getDirectiveLanguages() const {
 return Records.getAllDerivedDefinitions("DirectiveLanguage");
diff --git a/llvm/include/llvm/TableGen/Record.h 
b/llvm/include/llvm/TableGen/Record.h
index ff596df94e4f5a..5d36fcf57e23e3 100644
--- a/llvm/include/llvm/TableGen/Record.h
+++ b/llvm/include/llvm/TableGen/Record.h
@@ -2057,19 +2057,28 @@ class RecordKeeper {
   
//===---

[clang] [llvm] [mlir] [TableGen] Add const variants of accessors for backend (PR #106658)

2024-09-05 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/106658
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [mlir] [TableGen] Add const variants of accessors for backend (PR #106658)

2024-09-05 Thread Rahul Joshi via cfe-commits

jurahul wrote:

> > I propose the following next steps (once this is committed):
> 
> this timeline looks fine, do you also want to add `[[deprecated]]` once the 
> PSA is out? Also, I can help on other LLVM TableGen backends if they haven't 
> been updated.

Yeah, once all the upstream backends are migrated, we should mark the non-const 
ones deprecated. As for other backends, thanks for the offer! I have diffs for 
clang and mlir, so I am hoping I can do any other remaining ones as well (lldb 
and libc seem to have one). But if its start to get delayed, I can ping you for 
help.

https://github.com/llvm/llvm-project/pull/106658
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [mlir] [TableGen] Add const variants of accessors for backend (PR #106658)

2024-09-05 Thread Rahul Joshi via cfe-commits

jurahul wrote:

I also did a minor update to the diff: I changed the const versions that used 
to return references to vectors to instead return `ArrayRef`. 
IMO this will prevent accidental vector copies in code like `auto X = 
RC.getAllDerivedDefinitions("yyy")`.

https://github.com/llvm/llvm-project/pull/106658
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [mlir] [TableGen] Add const variants of accessors for backend (PR #106658)

2024-09-05 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul updated 
https://github.com/llvm/llvm-project/pull/106658

>From 4e1d789d052c9ae7d0fc3be73307f9d77bc5fa9f Mon Sep 17 00:00:00 2001
From: Rahul Joshi 
Date: Thu, 29 Aug 2024 20:36:05 -0700
Subject: [PATCH] [TableGen] Add const variants of accessors for backend

Split RecordKeeper `getAllDerivedDefinitions` family of functions into
two variants: (a) non-const ones that return vectors of `Record *` and
(b) const ones, that return vectors of `const Record *`.

This will help gradual migration of backend to use `const RecordKeeper`
and by implication change code to work with const pointers.
---
 clang/utils/TableGen/ClangAttrEmitter.cpp |  4 +-
 clang/utils/TableGen/ClangSyntaxEmitter.cpp   |  2 +-
 llvm/include/llvm/TableGen/DirectiveEmitter.h |  5 +-
 llvm/include/llvm/TableGen/Record.h   | 34 --
 llvm/lib/TableGen/Record.cpp  | 65 +++
 .../TableGen/Basic/CodeGenIntrinsics.cpp  |  2 +-
 .../TableGen/Common/SubtargetFeatureInfo.cpp  |  2 +-
 .../TableGen/Common/SubtargetFeatureInfo.h|  2 +-
 llvm/utils/TableGen/ExegesisEmitter.cpp   |  2 +-
 llvm/utils/TableGen/GlobalISelEmitter.cpp |  2 +-
 llvm/utils/TableGen/SubtargetEmitter.cpp  |  2 +-
 llvm/utils/TableGen/TableGen.cpp  |  2 +-
 mlir/include/mlir/TableGen/GenInfo.h  |  6 +-
 mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp   | 28 
 mlir/tools/mlir-tblgen/OmpOpGen.cpp   |  2 +-
 mlir/tools/mlir-tblgen/OpDocGen.cpp   | 20 +++---
 mlir/tools/mlir-tblgen/OpInterfacesGen.cpp| 15 ++---
 mlir/tools/mlir-tblgen/RewriterGen.cpp|  8 +--
 .../tools/tblgen-to-irdl/OpDefinitionsGen.cpp |  7 +-
 19 files changed, 134 insertions(+), 76 deletions(-)

diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp 
b/clang/utils/TableGen/ClangAttrEmitter.cpp
index adbe6af62d5cbe..d24215d10f17c7 100644
--- a/clang/utils/TableGen/ClangAttrEmitter.cpp
+++ b/clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -189,7 +189,7 @@ static StringRef NormalizeGNUAttrSpelling(StringRef 
AttrSpelling) {
 
 typedef std::vector> ParsedAttrMap;
 
-static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records,
+static ParsedAttrMap getParsedAttrList(RecordKeeper &Records,
ParsedAttrMap *Dupes = nullptr,
bool SemaOnly = true) {
   std::vector Attrs = Records.getAllDerivedDefinitions("Attr");
@@ -4344,7 +4344,7 @@ static void GenerateAppertainsTo(const Record &Attr, 
raw_ostream &OS) {
 // written into OS and the checks for merging declaration attributes are
 // written into MergeOS.
 static void GenerateMutualExclusionsChecks(const Record &Attr,
-   const RecordKeeper &Records,
+   RecordKeeper &Records,
raw_ostream &OS,
raw_ostream &MergeDeclOS,
raw_ostream &MergeStmtOS) {
diff --git a/clang/utils/TableGen/ClangSyntaxEmitter.cpp 
b/clang/utils/TableGen/ClangSyntaxEmitter.cpp
index 9720d587318432..2a69e4c353b6b4 100644
--- a/clang/utils/TableGen/ClangSyntaxEmitter.cpp
+++ b/clang/utils/TableGen/ClangSyntaxEmitter.cpp
@@ -41,7 +41,7 @@ using llvm::formatv;
 // stable and useful way, where abstract Node subclasses correspond to ranges.
 class Hierarchy {
 public:
-  Hierarchy(const llvm::RecordKeeper &Records) {
+  Hierarchy(llvm::RecordKeeper &Records) {
 for (llvm::Record *T : Records.getAllDerivedDefinitions("NodeType"))
   add(T);
 for (llvm::Record *Derived : Records.getAllDerivedDefinitions("NodeType"))
diff --git a/llvm/include/llvm/TableGen/DirectiveEmitter.h 
b/llvm/include/llvm/TableGen/DirectiveEmitter.h
index 1121459be6ce7d..ca21c8fc101450 100644
--- a/llvm/include/llvm/TableGen/DirectiveEmitter.h
+++ b/llvm/include/llvm/TableGen/DirectiveEmitter.h
@@ -15,8 +15,7 @@ namespace llvm {
 // DirectiveBase.td and provides helper methods for accessing it.
 class DirectiveLanguage {
 public:
-  explicit DirectiveLanguage(const llvm::RecordKeeper &Records)
-  : Records(Records) {
+  explicit DirectiveLanguage(llvm::RecordKeeper &Records) : Records(Records) {
 const auto &DirectiveLanguages = getDirectiveLanguages();
 Def = DirectiveLanguages[0];
   }
@@ -71,7 +70,7 @@ class DirectiveLanguage {
 
 private:
   const llvm::Record *Def;
-  const llvm::RecordKeeper &Records;
+  llvm::RecordKeeper &Records;
 
   std::vector getDirectiveLanguages() const {
 return Records.getAllDerivedDefinitions("DirectiveLanguage");
diff --git a/llvm/include/llvm/TableGen/Record.h 
b/llvm/include/llvm/TableGen/Record.h
index ff596df94e4f5a..5d36fcf57e23e3 100644
--- a/llvm/include/llvm/TableGen/Record.h
+++ b/llvm/include/llvm/TableGen/Record.h
@@ -2057,19 +2057,28 @@ class RecordKeeper {
   
//===---

[clang] [llvm] [mlir] [TableGen] Add const variants of accessors for backend (PR #106658)

2024-09-05 Thread Rahul Joshi via cfe-commits

jurahul wrote:

I posted the PSA at: 
https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089

https://github.com/llvm/llvm-project/pull/106658
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Migrate clang-tblgen to use const RecordKeeper (PR #107533)

2024-09-06 Thread Rahul Joshi via cfe-commits

jurahul wrote:

This is a refactor for better const correctness in TableGen backends. Please 
see the discourse thread here: 
https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089

https://github.com/llvm/llvm-project/pull/107533
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Migrate clang-tblgen to use const RecordKeeper (PR #107533)

2024-09-06 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul ready_for_review 
https://github.com/llvm/llvm-project/pull/107533
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [TableGen] Change SetTheory set/vec to use const Record * (PR #107692)

2024-09-07 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul created 
https://github.com/llvm/llvm-project/pull/107692

Change SetTheory::RecSet/RecVec to use const Record pointers.

>From 4fc94659b3c245a30902c95be6d9c7bfd1d55f0a Mon Sep 17 00:00:00 2001
From: Rahul Joshi 
Date: Sat, 7 Sep 2024 04:33:09 -0700
Subject: [PATCH] [TableGen] Change SetTheory set/vec to use consr Record *

Change SetTheory::RecSet/RecVec to use const Record pointers.
---
 clang/utils/TableGen/NeonEmitter.cpp  |  2 +-
 llvm/include/llvm/TableGen/Record.h   |  6 +--
 llvm/include/llvm/TableGen/SetTheory.h|  4 +-
 llvm/lib/TableGen/Record.cpp  | 10 ++---
 llvm/utils/TableGen/AsmMatcherEmitter.cpp | 38 ++-
 .../TableGen/Common/CodeGenRegisters.cpp  | 29 +++---
 llvm/utils/TableGen/Common/CodeGenRegisters.h | 16 
 .../utils/TableGen/Common/CodeGenSchedule.cpp | 27 ++---
 llvm/utils/TableGen/Common/CodeGenSchedule.h  | 10 +++--
 llvm/utils/TableGen/RegisterInfoEmitter.cpp   | 30 +++
 llvm/utils/TableGen/TableGen.cpp  |  2 +-
 11 files changed, 88 insertions(+), 86 deletions(-)

diff --git a/clang/utils/TableGen/NeonEmitter.cpp 
b/clang/utils/TableGen/NeonEmitter.cpp
index e0d7b0db7f5780..4707ce1ea3b792 100644
--- a/clang/utils/TableGen/NeonEmitter.cpp
+++ b/clang/utils/TableGen/NeonEmitter.cpp
@@ -1636,7 +1636,7 @@ std::pair 
Intrinsic::DagEmitter::emitDagShuffle(DagInit *DI){
   int64_t VectorSize = cast(Expr->getArg(0))->getValue();
   VectorSize /= ElementSize;
 
-  std::vector Revved;
+  std::vector Revved;
   for (unsigned VI = 0; VI < Elts2.size(); VI += VectorSize) {
 for (int LI = VectorSize - 1; LI >= 0; --LI) {
   Revved.push_back(Elts2[VI + LI]);
diff --git a/llvm/include/llvm/TableGen/Record.h 
b/llvm/include/llvm/TableGen/Record.h
index 5d36fcf57e23e3..f6c751af6bb836 100644
--- a/llvm/include/llvm/TableGen/Record.h
+++ b/llvm/include/llvm/TableGen/Record.h
@@ -1323,7 +1323,7 @@ class DefInit : public TypedInit {
 return I->getKind() == IK_DefInit;
   }
 
-  static DefInit *get(Record*);
+  static DefInit *get(const Record *);
 
   Init *convertInitializerTo(RecTy *Ty) const override;
 
@@ -1673,7 +1673,7 @@ class Record {
   RecordKeeper &TrackedRecords;
 
   // The DefInit corresponding to this record.
-  DefInit *CorrespondingDefInit = nullptr;
+  mutable DefInit *CorrespondingDefInit = nullptr;
 
   // Unique record ID.
   unsigned ID;
@@ -1740,7 +1740,7 @@ class Record {
   RecordRecTy *getType();
 
   /// get the corresponding DefInit.
-  DefInit *getDefInit();
+  DefInit *getDefInit() const;
 
   bool isClass() const { return Kind == RK_Class; }
 
diff --git a/llvm/include/llvm/TableGen/SetTheory.h 
b/llvm/include/llvm/TableGen/SetTheory.h
index 954453b783d4d8..771dcff2f214c3 100644
--- a/llvm/include/llvm/TableGen/SetTheory.h
+++ b/llvm/include/llvm/TableGen/SetTheory.h
@@ -64,8 +64,8 @@ class Record;
 
 class SetTheory {
 public:
-  using RecVec = std::vector;
-  using RecSet = SmallSetVector;
+  using RecVec = std::vector;
+  using RecSet = SmallSetVector;
 
   /// Operator - A callback representing a DAG operator.
   class Operator {
diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp
index 17afa2f7eb1b99..79c5e071e55f23 100644
--- a/llvm/lib/TableGen/Record.cpp
+++ b/llvm/lib/TableGen/Record.cpp
@@ -2235,9 +2235,7 @@ Init *VarBitInit::resolveReferences(Resolver &R) const {
 DefInit::DefInit(Record *D)
 : TypedInit(IK_DefInit, D->getType()), Def(D) {}
 
-DefInit *DefInit::get(Record *R) {
-  return R->getDefInit();
-}
+DefInit *DefInit::get(const Record *R) { return R->getDefInit(); }
 
 Init *DefInit::convertInitializerTo(RecTy *Ty) const {
   if (auto *RRT = dyn_cast(Ty))
@@ -2808,10 +2806,10 @@ RecordRecTy *Record::getType() {
   return RecordRecTy::get(TrackedRecords, DirectSCs);
 }
 
-DefInit *Record::getDefInit() {
+DefInit *Record::getDefInit() const {
   if (!CorrespondingDefInit) {
-CorrespondingDefInit =
-new (TrackedRecords.getImpl().Allocator) DefInit(this);
+CorrespondingDefInit = new (TrackedRecords.getImpl().Allocator)
+DefInit(const_cast(this));
   }
   return CorrespondingDefInit;
 }
diff --git a/llvm/utils/TableGen/AsmMatcherEmitter.cpp 
b/llvm/utils/TableGen/AsmMatcherEmitter.cpp
index f351087ad212f7..574a3344a02f8d 100644
--- a/llvm/utils/TableGen/AsmMatcherEmitter.cpp
+++ b/llvm/utils/TableGen/AsmMatcherEmitter.cpp
@@ -140,7 +140,7 @@ class AsmMatcherInfo;
 // RegisterSets can be seen in the outputted AsmMatcher tables occasionally, 
and
 // can even affect compiler output (at least seen in diagnostics produced when
 // all matches fail). So we use a type that sorts them consistently.
-typedef std::set RegisterSet;
+typedef std::set RegisterSet;
 
 class AsmMatcherEmitter {
   RecordKeeper &Records;
@@ -242,7 +242,7 @@ struct ClassInfo {
   if (!isRegisterClass() || !RHS.isRegisterClass())
 return false;
 
-  s

[clang] [llvm] [TableGen] Change SetTheory set/vec to use const Record * (PR #107692)

2024-09-07 Thread Rahul Joshi via cfe-commits


@@ -2808,10 +2806,10 @@ RecordRecTy *Record::getType() {
   return RecordRecTy::get(TrackedRecords, DirectSCs);
 }
 
-DefInit *Record::getDefInit() {
+DefInit *Record::getDefInit() const {
   if (!CorrespondingDefInit) {
-CorrespondingDefInit =
-new (TrackedRecords.getImpl().Allocator) DefInit(this);
+CorrespondingDefInit = new (TrackedRecords.getImpl().Allocator)

jurahul wrote:

The only issue is here, where 

https://github.com/llvm/llvm-project/pull/107692
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [TableGen] Change SetTheory set/vec to use const Record * (PR #107692)

2024-09-07 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/107692
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [TableGen] Change SetTheory set/vec to use const Record * (PR #107692)

2024-09-07 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul ready_for_review 
https://github.com/llvm/llvm-project/pull/107692
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [TableGen] Change SetTheory set/vec to use const Record * (PR #107692)

2024-09-08 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul deleted 
https://github.com/llvm/llvm-project/pull/107692
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [TableGen] Change SetTheory set/vec to use const Record * (PR #107692)

2024-09-08 Thread Rahul Joshi via cfe-commits


@@ -2808,10 +2806,10 @@ RecordRecTy *Record::getType() {
   return RecordRecTy::get(TrackedRecords, DirectSCs);
 }
 
-DefInit *Record::getDefInit() {
+DefInit *Record::getDefInit() const {
   if (!CorrespondingDefInit) {
-CorrespondingDefInit =
-new (TrackedRecords.getImpl().Allocator) DefInit(this);
+CorrespondingDefInit = new (TrackedRecords.getImpl().Allocator)
+DefInit(const_cast(this));

jurahul wrote:

The only issue/smell is here, where CodeGenDAGPatterns.cpp and 
CodeGenRegisters.cpp call DefInit::get() on the result of a SetTheory::expand() 
returned record (which is now const *). I looked a little bit to see what they 
are doing, but decided to "cut off" the change here to keep it smaller. This is 
part of effort to have better const correctness w.r.t records and recordkeeper, 
so with this, the "smell" is atleast localized to this function and we can have 
const records otherwise.

https://github.com/llvm/llvm-project/pull/107692
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [TableGen] Change SetTheory set/vec to use const Record * (PR #107692)

2024-09-08 Thread Rahul Joshi via cfe-commits


@@ -2808,10 +2806,10 @@ RecordRecTy *Record::getType() {
   return RecordRecTy::get(TrackedRecords, DirectSCs);
 }
 
-DefInit *Record::getDefInit() {
+DefInit *Record::getDefInit() const {
   if (!CorrespondingDefInit) {
-CorrespondingDefInit =
-new (TrackedRecords.getImpl().Allocator) DefInit(this);
+CorrespondingDefInit = new (TrackedRecords.getImpl().Allocator)
+DefInit(const_cast(this));

jurahul wrote:

I did look into making the Def pointer in DefInit const. It's possible, but 
will spawn a bunch of other changes, so best done as a separate PR. I also 
checked if we can always instantiate the `CorrespondingDefInit` when a new 
Record is added to the `RecordKeeper` when parsing, it seems only a small 
fraction of Records have CorrespondingDefInit set to non-null (measured in 
RecordKeeper destructor) so unconditionally populating CorrespondingDefInit 
will likely cause memory footprint increase. So this seems like a reasonable 
thing, and will be followed by removing the const_cast<> (but 
CorrespondingDefInit will stay mutable, its like a cache).

https://github.com/llvm/llvm-project/pull/107692
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [TableGen] Change SetTheory set/vec to use const Record * (PR #107692)

2024-09-08 Thread Rahul Joshi via cfe-commits

jurahul wrote:

> lgtm assuming the const_cast goes away in a subsequent change

Thanks. Yeah as I said above, I think we can make the Record* in DefInit const 
and then get rid of the const_cast<>. But the mutable for CorrespondingDefInit  
will stay, with it serving as a cache.

https://github.com/llvm/llvm-project/pull/107692
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Migrate clang-tblgen to use const RecordKeeper (PR #107533)

2024-09-09 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/107533
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [TableGen] Change SetTheory set/vec to use const Record * (PR #107692)

2024-09-09 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul updated 
https://github.com/llvm/llvm-project/pull/107692

>From b11f8fbde5cc005e4667877ed60954f36abcec0c Mon Sep 17 00:00:00 2001
From: Rahul Joshi 
Date: Sat, 7 Sep 2024 04:33:09 -0700
Subject: [PATCH] [TableGen] Change SetTheory set/vec to use consr Record *

Change SetTheory::RecSet/RecVec to use const Record pointers.
---
 clang/utils/TableGen/NeonEmitter.cpp  |  2 +-
 llvm/include/llvm/TableGen/Record.h   |  4 +-
 llvm/include/llvm/TableGen/SetTheory.h|  4 +-
 llvm/lib/TableGen/Record.cpp  |  6 +--
 llvm/utils/TableGen/AsmMatcherEmitter.cpp | 38 ++-
 .../TableGen/Common/CodeGenRegisters.cpp  | 29 +++---
 llvm/utils/TableGen/Common/CodeGenRegisters.h | 16 
 .../utils/TableGen/Common/CodeGenSchedule.cpp | 27 ++---
 llvm/utils/TableGen/Common/CodeGenSchedule.h  | 10 +++--
 llvm/utils/TableGen/RegisterInfoEmitter.cpp   | 30 +++
 llvm/utils/TableGen/TableGen.cpp  |  2 +-
 11 files changed, 86 insertions(+), 82 deletions(-)

diff --git a/clang/utils/TableGen/NeonEmitter.cpp 
b/clang/utils/TableGen/NeonEmitter.cpp
index e0d7b0db7f5780..4707ce1ea3b792 100644
--- a/clang/utils/TableGen/NeonEmitter.cpp
+++ b/clang/utils/TableGen/NeonEmitter.cpp
@@ -1636,7 +1636,7 @@ std::pair 
Intrinsic::DagEmitter::emitDagShuffle(DagInit *DI){
   int64_t VectorSize = cast(Expr->getArg(0))->getValue();
   VectorSize /= ElementSize;
 
-  std::vector Revved;
+  std::vector Revved;
   for (unsigned VI = 0; VI < Elts2.size(); VI += VectorSize) {
 for (int LI = VectorSize - 1; LI >= 0; --LI) {
   Revved.push_back(Elts2[VI + LI]);
diff --git a/llvm/include/llvm/TableGen/Record.h 
b/llvm/include/llvm/TableGen/Record.h
index 98c8fd0ca051f9..dc86f28a453285 100644
--- a/llvm/include/llvm/TableGen/Record.h
+++ b/llvm/include/llvm/TableGen/Record.h
@@ -1669,7 +1669,7 @@ class Record {
   RecordKeeper &TrackedRecords;
 
   // The DefInit corresponding to this record.
-  DefInit *CorrespondingDefInit = nullptr;
+  mutable DefInit *CorrespondingDefInit = nullptr;
 
   // Unique record ID.
   unsigned ID;
@@ -1736,7 +1736,7 @@ class Record {
   RecordRecTy *getType() const;
 
   /// get the corresponding DefInit.
-  DefInit *getDefInit();
+  DefInit *getDefInit() const;
 
   bool isClass() const { return Kind == RK_Class; }
 
diff --git a/llvm/include/llvm/TableGen/SetTheory.h 
b/llvm/include/llvm/TableGen/SetTheory.h
index 954453b783d4d8..771dcff2f214c3 100644
--- a/llvm/include/llvm/TableGen/SetTheory.h
+++ b/llvm/include/llvm/TableGen/SetTheory.h
@@ -64,8 +64,8 @@ class Record;
 
 class SetTheory {
 public:
-  using RecVec = std::vector;
-  using RecSet = SmallSetVector;
+  using RecVec = std::vector;
+  using RecSet = SmallSetVector;
 
   /// Operator - A callback representing a DAG operator.
   class Operator {
diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp
index 85516b7b9f20aa..26f45d5bb8a3b7 100644
--- a/llvm/lib/TableGen/Record.cpp
+++ b/llvm/lib/TableGen/Record.cpp
@@ -2802,10 +2802,10 @@ RecordRecTy *Record::getType() const {
   return RecordRecTy::get(TrackedRecords, DirectSCs);
 }
 
-DefInit *Record::getDefInit() {
+DefInit *Record::getDefInit() const {
   if (!CorrespondingDefInit) {
-CorrespondingDefInit =
-new (TrackedRecords.getImpl().Allocator) DefInit(this);
+CorrespondingDefInit = new (TrackedRecords.getImpl().Allocator)
+DefInit(const_cast(this));
   }
   return CorrespondingDefInit;
 }
diff --git a/llvm/utils/TableGen/AsmMatcherEmitter.cpp 
b/llvm/utils/TableGen/AsmMatcherEmitter.cpp
index f351087ad212f7..574a3344a02f8d 100644
--- a/llvm/utils/TableGen/AsmMatcherEmitter.cpp
+++ b/llvm/utils/TableGen/AsmMatcherEmitter.cpp
@@ -140,7 +140,7 @@ class AsmMatcherInfo;
 // RegisterSets can be seen in the outputted AsmMatcher tables occasionally, 
and
 // can even affect compiler output (at least seen in diagnostics produced when
 // all matches fail). So we use a type that sorts them consistently.
-typedef std::set RegisterSet;
+typedef std::set RegisterSet;
 
 class AsmMatcherEmitter {
   RecordKeeper &Records;
@@ -242,7 +242,7 @@ struct ClassInfo {
   if (!isRegisterClass() || !RHS.isRegisterClass())
 return false;
 
-  std::vector Tmp;
+  std::vector Tmp;
   std::set_intersection(Registers.begin(), Registers.end(),
 RHS.Registers.begin(), RHS.Registers.end(),
 std::back_inserter(Tmp), LessRecordByID());
@@ -403,7 +403,7 @@ struct MatchableInfo {
 bool IsIsolatedToken;
 
 /// Register record if this token is singleton register.
-Record *SingletonReg;
+const Record *SingletonReg;
 
 explicit AsmOperand(bool IsIsolatedToken, StringRef T)
 : Token(T), Class(nullptr), SubOpIdx(-1),
@@ -582,7 +582,7 @@ struct MatchableInfo {
   void formTwoOperandAlias(StringRef Constraint);
 
   void initialize(const As

[clang] [llvm] [TableGen] Change SetTheory set/vec to use const Record * (PR #107692)

2024-09-09 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul closed 
https://github.com/llvm/llvm-project/pull/107692
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Migrate clang-tblgen to use const RecordKeeper (PR #107533)

2024-09-09 Thread Rahul Joshi via cfe-commits

jurahul wrote:

@Sirraide @Lukacma @SpencerAbson @mshockwave can you PTAL? Or suggest other 
reviewers? 

https://github.com/llvm/llvm-project/pull/107533
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Migrate clang-tblgen to use const RecordKeeper (PR #107533)

2024-09-10 Thread Rahul Joshi via cfe-commits

jurahul wrote:

@AaronBallman can you please help find appropriate folks to review this PR?

https://github.com/llvm/llvm-project/pull/107533
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Migrate clang-tblgen to use const RecordKeeper (PR #107533)

2024-09-10 Thread Rahul Joshi via cfe-commits

jurahul wrote:

Fair enough. The formatting changes are unintentional (I just happened to touch 
code that was not compliant and clang-format did its thing). How about I split 
these per-file/per-emitter/related sets of emitters? I have been doing the same 
on LLVM side, just that for clang it was feasible do all of the changes in a 
single go.

https://github.com/llvm/llvm-project/pull/107533
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change ASTTableGen to use const Record pointers (PR #108193)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul created 
https://github.com/llvm/llvm-project/pull/108193

Change ASTTableGen to use const Record pointers.

>From 2e365455982c5cd7405beace6807c0b72b2e06d3 Mon Sep 17 00:00:00 2001
From: Rahul Joshi 
Date: Wed, 11 Sep 2024 04:38:23 -0700
Subject: [PATCH] [clang][TableGen] Change ASTTableGen to use const Record
 pointers

---
 clang/utils/TableGen/ASTTableGen.cpp | 19 ++
 clang/utils/TableGen/ASTTableGen.h   | 37 ++--
 2 files changed, 27 insertions(+), 29 deletions(-)

diff --git a/clang/utils/TableGen/ASTTableGen.cpp 
b/clang/utils/TableGen/ASTTableGen.cpp
index 54288ff6a03be3..47344777e9311a 100644
--- a/clang/utils/TableGen/ASTTableGen.cpp
+++ b/clang/utils/TableGen/ASTTableGen.cpp
@@ -31,7 +31,8 @@ llvm::StringRef clang::tblgen::HasProperties::getName() const 
{
   }
 }
 
-static StringRef removeExpectedNodeNameSuffix(Record *node, StringRef suffix) {
+static StringRef removeExpectedNodeNameSuffix(const Record *node,
+  StringRef suffix) {
   StringRef nodeName = node->getName();
   if (!nodeName.ends_with(suffix)) {
 PrintFatalError(node->getLoc(),
@@ -105,8 +106,7 @@ static void visitASTNodeRecursive(ASTNode node, ASTNode 
base,
   }
 }
 
-static void visitHierarchy(RecordKeeper &records,
-   StringRef nodeClassName,
+static void visitHierarchy(const RecordKeeper &records, StringRef 
nodeClassName,
ASTNodeHierarchyVisitor visit) {
   // Check for the node class, just as a basic correctness check.
   if (!records.getClass(nodeClassName)) {
@@ -114,13 +114,10 @@ static void visitHierarchy(RecordKeeper &records,
   + nodeClassName);
   }
 
-  // Find all the nodes in the hierarchy.
-  auto nodes = records.getAllDerivedDefinitions(nodeClassName);
-
-  // Derive the child map.
+  // Derive the child map for all nodes in the hierarchy.
   ChildMap hierarchy;
   ASTNode root;
-  for (ASTNode node : nodes) {
+  for (ASTNode node : records.getAllDerivedDefinitions(nodeClassName)) {
 if (auto base = node.getBase())
   hierarchy.insert(std::make_pair(base, node));
 else if (root)
@@ -136,8 +133,8 @@ static void visitHierarchy(RecordKeeper &records,
   visitASTNodeRecursive(root, ASTNode(), hierarchy, visit);
 }
 
-void clang::tblgen::visitASTNodeHierarchyImpl(RecordKeeper &records,
-  StringRef nodeClassName,
-  ASTNodeHierarchyVisitor visit) {
+void clang::tblgen::visitASTNodeHierarchyImpl(
+const RecordKeeper &records, StringRef nodeClassName,
+ASTNodeHierarchyVisitor visit) {
   visitHierarchy(records, nodeClassName, visit);
 }
diff --git a/clang/utils/TableGen/ASTTableGen.h 
b/clang/utils/TableGen/ASTTableGen.h
index 41f78a6a3bbcdd..143d779a8a64f8 100644
--- a/clang/utils/TableGen/ASTTableGen.h
+++ b/clang/utils/TableGen/ASTTableGen.h
@@ -87,18 +87,18 @@ namespace clang {
 namespace tblgen {
 
 class WrappedRecord {
-  llvm::Record *Record;
+  const llvm::Record *Record;
 
 protected:
-  WrappedRecord(llvm::Record *record = nullptr) : Record(record) {}
+  WrappedRecord(const llvm::Record *record = nullptr) : Record(record) {}
 
-  llvm::Record *get() const {
+  const llvm::Record *get() const {
 assert(Record && "accessing null record");
 return Record;
   }
 
 public:
-  llvm::Record *getRecord() const { return Record; }
+  const llvm::Record *getRecord() const { return Record; }
 
   explicit operator bool() const { return Record != nullptr; }
 
@@ -144,7 +144,7 @@ class HasProperties : public WrappedRecord {
 public:
   static constexpr llvm::StringRef ClassName = HasPropertiesClassName;
 
-  HasProperties(llvm::Record *record = nullptr) : WrappedRecord(record) {}
+  HasProperties(const llvm::Record *record = nullptr) : WrappedRecord(record) 
{}
 
   llvm::StringRef getName() const;
 
@@ -157,7 +157,7 @@ class HasProperties : public WrappedRecord {
 /// in one of Clang's AST hierarchies.
 class ASTNode : public HasProperties {
 public:
-  ASTNode(llvm::Record *record = nullptr) : HasProperties(record) {}
+  ASTNode(const llvm::Record *record = nullptr) : HasProperties(record) {}
 
   llvm::StringRef getName() const {
 return get()->getName();
@@ -180,7 +180,7 @@ class ASTNode : public HasProperties {
 
 class DeclNode : public ASTNode {
 public:
-  DeclNode(llvm::Record *record = nullptr) : ASTNode(record) {}
+  DeclNode(const llvm::Record *record = nullptr) : ASTNode(record) {}
 
   llvm::StringRef getId() const;
   std::string getClassName() const;
@@ -202,7 +202,7 @@ class DeclNode : public ASTNode {
 
 class TypeNode : public ASTNode {
 public:
-  TypeNode(llvm::Record *record = nullptr) : ASTNode(record) {}
+  TypeNode(const llvm::Record *record = nullptr) : ASTNode(record) {}
 
   llvm::StringRef getId() const;
   llvm::StringRef getClassName() const;
@@ -224,7 +224,7 @@ class TypeNode : public ASTNode {
 
 

[clang] [clang][TableGen] Change ASTTableGen to use const Record pointers (PR #108193)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108193
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Migrate Builtins emitter to use const RecordKeeper (PR #108195)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul created 
https://github.com/llvm/llvm-project/pull/108195

Migrate Builtins emitter to use const RecordKeeper.


>From 903b02c1656c5bacfa0d97da83584876fa1f2999 Mon Sep 17 00:00:00 2001
From: Rahul Joshi 
Date: Wed, 11 Sep 2024 04:51:40 -0700
Subject: [PATCH] [clang][TableGen] Migrate Builtins emitter to use const
 RecordKeeper

---
 clang/utils/TableGen/ClangBuiltinsEmitter.cpp | 2 +-
 clang/utils/TableGen/TableGenBackends.h   | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/clang/utils/TableGen/ClangBuiltinsEmitter.cpp 
b/clang/utils/TableGen/ClangBuiltinsEmitter.cpp
index 94f12a08164fdc..4ae7600a392b8f 100644
--- a/clang/utils/TableGen/ClangBuiltinsEmitter.cpp
+++ b/clang/utils/TableGen/ClangBuiltinsEmitter.cpp
@@ -345,7 +345,7 @@ void EmitBuiltin(llvm::raw_ostream &OS, const Record 
*Builtin) {
 }
 } // namespace
 
-void clang::EmitClangBuiltins(llvm::RecordKeeper &Records,
+void clang::EmitClangBuiltins(const llvm::RecordKeeper &Records,
   llvm::raw_ostream &OS) {
   emitSourceFileHeader("List of builtins that Clang recognizes", OS);
 
diff --git a/clang/utils/TableGen/TableGenBackends.h 
b/clang/utils/TableGen/TableGenBackends.h
index 3a424c9c91fe71..35cc04d6ef31f4 100644
--- a/clang/utils/TableGen/TableGenBackends.h
+++ b/clang/utils/TableGen/TableGenBackends.h
@@ -73,7 +73,8 @@ void EmitClangAttrNodeTraverse(llvm::RecordKeeper &Records,
llvm::raw_ostream &OS);
 void EmitClangAttrDocTable(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
 
-void EmitClangBuiltins(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
+void EmitClangBuiltins(const llvm::RecordKeeper &Records,
+   llvm::raw_ostream &OS);
 
 void EmitClangDiagsDefs(llvm::RecordKeeper &Records, llvm::raw_ostream &OS,
 const std::string &Component);

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change comment command emitter to const RecordKeeper (PR #108199)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul created 
https://github.com/llvm/llvm-project/pull/108199

Change comment command emitter to const RecordKeeper.

>From 5e344e11844fc50ef231c8f8937f5251e76a1150 Mon Sep 17 00:00:00 2001
From: Rahul Joshi 
Date: Wed, 11 Sep 2024 04:59:50 -0700
Subject: [PATCH] [clang][TableGen] Change comment command emitter to const
 RecordKeeper

---
 .../TableGen/ClangCommentCommandInfoEmitter.cpp| 14 +++---
 clang/utils/TableGen/TableGenBackends.h|  4 ++--
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/clang/utils/TableGen/ClangCommentCommandInfoEmitter.cpp 
b/clang/utils/TableGen/ClangCommentCommandInfoEmitter.cpp
index aee7d38786a51c..1a2503dcf660cf 100644
--- a/clang/utils/TableGen/ClangCommentCommandInfoEmitter.cpp
+++ b/clang/utils/TableGen/ClangCommentCommandInfoEmitter.cpp
@@ -20,16 +20,16 @@
 
 using namespace llvm;
 
-void clang::EmitClangCommentCommandInfo(RecordKeeper &Records,
+void clang::EmitClangCommentCommandInfo(const RecordKeeper &Records,
 raw_ostream &OS) {
   emitSourceFileHeader("A list of commands useable in documentation comments",
OS, Records);
 
   OS << "namespace {\n"
 "const CommandInfo Commands[] = {\n";
-  std::vector Tags = Records.getAllDerivedDefinitions("Command");
+  ArrayRef Tags = Records.getAllDerivedDefinitions("Command");
   for (size_t i = 0, e = Tags.size(); i != e; ++i) {
-Record &Tag = *Tags[i];
+const Record &Tag = *Tags[i];
 OS << "  { "
<< "\"" << Tag.getValueAsString("Name") << "\", "
<< "\"" << Tag.getValueAsString("EndCommandName") << "\", " << i << ", "
@@ -62,7 +62,7 @@ void clang::EmitClangCommentCommandInfo(RecordKeeper &Records,
 
   std::vector Matches;
   for (size_t i = 0, e = Tags.size(); i != e; ++i) {
-Record &Tag = *Tags[i];
+const Record &Tag = *Tags[i];
 std::string Name = std::string(Tag.getValueAsString("Name"));
 std::string Return;
 raw_string_ostream(Return) << "return &Commands[" << i << "];";
@@ -112,7 +112,7 @@ static std::string MangleName(StringRef Str) {
   return Mangled;
 }
 
-void clang::EmitClangCommentCommandList(RecordKeeper &Records,
+void clang::EmitClangCommentCommandList(const RecordKeeper &Records,
 raw_ostream &OS) {
   emitSourceFileHeader("A list of commands useable in documentation comments",
OS, Records);
@@ -121,9 +121,9 @@ void clang::EmitClangCommentCommandList(RecordKeeper 
&Records,
  << "#  define COMMENT_COMMAND(NAME)\n"
  << "#endif\n";
 
-  std::vector Tags = Records.getAllDerivedDefinitions("Command");
+  ArrayRef Tags = Records.getAllDerivedDefinitions("Command");
   for (size_t i = 0, e = Tags.size(); i != e; ++i) {
-Record &Tag = *Tags[i];
+const Record &Tag = *Tags[i];
 std::string MangledName = MangleName(Tag.getValueAsString("Name"));
 
 OS << "COMMENT_COMMAND(" << MangledName << ")\n";
diff --git a/clang/utils/TableGen/TableGenBackends.h 
b/clang/utils/TableGen/TableGenBackends.h
index 3a424c9c91fe71..2ead14045329bd 100644
--- a/clang/utils/TableGen/TableGenBackends.h
+++ b/clang/utils/TableGen/TableGenBackends.h
@@ -90,9 +90,9 @@ void EmitClangCommentHTMLTagsProperties(llvm::RecordKeeper 
&Records,
 void EmitClangCommentHTMLNamedCharacterReferences(llvm::RecordKeeper &Records,
   llvm::raw_ostream &OS);
 
-void EmitClangCommentCommandInfo(llvm::RecordKeeper &Records,
+void EmitClangCommentCommandInfo(const llvm::RecordKeeper &Records,
  llvm::raw_ostream &OS);
-void EmitClangCommentCommandList(llvm::RecordKeeper &Records,
+void EmitClangCommentCommandList(const llvm::RecordKeeper &Records,
  llvm::raw_ostream &OS);
 void EmitClangOpcodes(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
 

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change HTML Emitter to use const RecordKeeper (PR #108201)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul created 
https://github.com/llvm/llvm-project/pull/108201

None

>From 2e0b2207c6cff650edee1db9da00ef6734366cd2 Mon Sep 17 00:00:00 2001
From: Rahul Joshi 
Date: Wed, 11 Sep 2024 05:05:26 -0700
Subject: [PATCH] [clang][TableGen] Change HTML Emitter to use const
 RecordKeeper

---
 ...CommentHTMLNamedCharacterReferenceEmitter.cpp | 16 ++--
 clang/utils/TableGen/TableGenBackends.h  |  4 ++--
 2 files changed, 8 insertions(+), 12 deletions(-)

diff --git 
a/clang/utils/TableGen/ClangCommentHTMLNamedCharacterReferenceEmitter.cpp 
b/clang/utils/TableGen/ClangCommentHTMLNamedCharacterReferenceEmitter.cpp
index f1cd9af0519d1b..bd75b3f6b652a1 100644
--- a/clang/utils/TableGen/ClangCommentHTMLNamedCharacterReferenceEmitter.cpp
+++ b/clang/utils/TableGen/ClangCommentHTMLNamedCharacterReferenceEmitter.cpp
@@ -46,21 +46,17 @@ static bool translateCodePointToUTF8(unsigned CodePoint,
   return true;
 }
 
-void clang::EmitClangCommentHTMLNamedCharacterReferences(RecordKeeper &Records,
- raw_ostream &OS) {
-  std::vector Tags = Records.getAllDerivedDefinitions("NCR");
+void clang::EmitClangCommentHTMLNamedCharacterReferences(
+const RecordKeeper &Records, raw_ostream &OS) {
   std::vector NameToUTF8;
   SmallString<32> CLiteral;
-  for (std::vector::iterator I = Tags.begin(), E = Tags.end();
-   I != E; ++I) {
-Record &Tag = **I;
-std::string Spelling = std::string(Tag.getValueAsString("Spelling"));
-uint64_t CodePoint = Tag.getValueAsInt("CodePoint");
+  for (const Record *Tag : Records.getAllDerivedDefinitions("NCR")) {
+std::string Spelling = std::string(Tag->getValueAsString("Spelling"));
+uint64_t CodePoint = Tag->getValueAsInt("CodePoint");
 CLiteral.clear();
 CLiteral.append("return ");
 if (!translateCodePointToUTF8(CodePoint, CLiteral)) {
-  SrcMgr.PrintMessage(Tag.getLoc().front(),
-  SourceMgr::DK_Error,
+  SrcMgr.PrintMessage(Tag->getLoc().front(), SourceMgr::DK_Error,
   Twine("invalid code point"));
   continue;
 }
diff --git a/clang/utils/TableGen/TableGenBackends.h 
b/clang/utils/TableGen/TableGenBackends.h
index 3a424c9c91fe71..5dc9fbd3586ec7 100644
--- a/clang/utils/TableGen/TableGenBackends.h
+++ b/clang/utils/TableGen/TableGenBackends.h
@@ -87,8 +87,8 @@ void EmitClangCommentHTMLTags(llvm::RecordKeeper &Records,
   llvm::raw_ostream &OS);
 void EmitClangCommentHTMLTagsProperties(llvm::RecordKeeper &Records,
 llvm::raw_ostream &OS);
-void EmitClangCommentHTMLNamedCharacterReferences(llvm::RecordKeeper &Records,
-  llvm::raw_ostream &OS);
+void EmitClangCommentHTMLNamedCharacterReferences(
+const llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
 
 void EmitClangCommentCommandInfo(llvm::RecordKeeper &Records,
  llvm::raw_ostream &OS);

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change HTML Tags emitter to use const RecordKeeper (PR #108202)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul created 
https://github.com/llvm/llvm-project/pull/108202

None

>From fa77d999f72a5faff0cbab7f4609eab7910dc3d8 Mon Sep 17 00:00:00 2001
From: Rahul Joshi 
Date: Wed, 11 Sep 2024 05:09:13 -0700
Subject: [PATCH] [clang][TableGen] Change HTML Tags emitter to use const
 RecordKeeper

---
 .../utils/TableGen/ClangCommentHTMLTagsEmitter.cpp  | 13 +++--
 clang/utils/TableGen/TableGenBackends.h |  4 ++--
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/clang/utils/TableGen/ClangCommentHTMLTagsEmitter.cpp 
b/clang/utils/TableGen/ClangCommentHTMLTagsEmitter.cpp
index 3dc1098753e0bf..a457315bc62c5c 100644
--- a/clang/utils/TableGen/ClangCommentHTMLTagsEmitter.cpp
+++ b/clang/utils/TableGen/ClangCommentHTMLTagsEmitter.cpp
@@ -19,10 +19,11 @@
 
 using namespace llvm;
 
-void clang::EmitClangCommentHTMLTags(RecordKeeper &Records, raw_ostream &OS) {
-  std::vector Tags = Records.getAllDerivedDefinitions("Tag");
+void clang::EmitClangCommentHTMLTags(const RecordKeeper &Records,
+ raw_ostream &OS) {
+  ArrayRef Tags = Records.getAllDerivedDefinitions("Tag");
   std::vector Matches;
-  for (Record *Tag : Tags) {
+  for (const Record *Tag : Tags) {
 Matches.emplace_back(std::string(Tag->getValueAsString("Spelling")),
  "return true;");
   }
@@ -35,12 +36,12 @@ void clang::EmitClangCommentHTMLTags(RecordKeeper &Records, 
raw_ostream &OS) {
  << "}\n\n";
 }
 
-void clang::EmitClangCommentHTMLTagsProperties(RecordKeeper &Records,
+void clang::EmitClangCommentHTMLTagsProperties(const RecordKeeper &Records,
raw_ostream &OS) {
-  std::vector Tags = Records.getAllDerivedDefinitions("Tag");
+  ArrayRef Tags = Records.getAllDerivedDefinitions("Tag");
   std::vector MatchesEndTagOptional;
   std::vector MatchesEndTagForbidden;
-  for (Record *Tag : Tags) {
+  for (const Record *Tag : Tags) {
 std::string Spelling = std::string(Tag->getValueAsString("Spelling"));
 StringMatcher::StringPair Match(Spelling, "return true;");
 if (Tag->getValueAsBit("EndTagOptional"))
diff --git a/clang/utils/TableGen/TableGenBackends.h 
b/clang/utils/TableGen/TableGenBackends.h
index 3a424c9c91fe71..a3e01952f99289 100644
--- a/clang/utils/TableGen/TableGenBackends.h
+++ b/clang/utils/TableGen/TableGenBackends.h
@@ -83,9 +83,9 @@ void EmitClangDiagsIndexName(llvm::RecordKeeper &Records,
 
 void EmitClangSACheckers(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
 
-void EmitClangCommentHTMLTags(llvm::RecordKeeper &Records,
+void EmitClangCommentHTMLTags(const llvm::RecordKeeper &Records,
   llvm::raw_ostream &OS);
-void EmitClangCommentHTMLTagsProperties(llvm::RecordKeeper &Records,
+void EmitClangCommentHTMLTagsProperties(const llvm::RecordKeeper &Records,
 llvm::raw_ostream &OS);
 void EmitClangCommentHTMLNamedCharacterReferences(llvm::RecordKeeper &Records,
   llvm::raw_ostream &OS);

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change DataCollector to use const RecordKeeper (PR #108203)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul created 
https://github.com/llvm/llvm-project/pull/108203

None

>From 3f98162db22fffbeb5d4c140e83c502aef049f9c Mon Sep 17 00:00:00 2001
From: Rahul Joshi 
Date: Wed, 11 Sep 2024 05:11:29 -0700
Subject: [PATCH] [clang][TableGen] Change DataCollector to use const
 RecordKeeper

---
 clang/utils/TableGen/ClangDataCollectorsEmitter.cpp | 2 +-
 clang/utils/TableGen/TableGenBackends.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/utils/TableGen/ClangDataCollectorsEmitter.cpp 
b/clang/utils/TableGen/ClangDataCollectorsEmitter.cpp
index 45082935c1f794..dae6710d752358 100644
--- a/clang/utils/TableGen/ClangDataCollectorsEmitter.cpp
+++ b/clang/utils/TableGen/ClangDataCollectorsEmitter.cpp
@@ -4,7 +4,7 @@
 
 using namespace llvm;
 
-void clang::EmitClangDataCollectors(RecordKeeper &RK, raw_ostream &OS) {
+void clang::EmitClangDataCollectors(const RecordKeeper &RK, raw_ostream &OS) {
   const auto &Defs = RK.getClasses();
   for (const auto &Entry : Defs) {
 Record &R = *Entry.second;
diff --git a/clang/utils/TableGen/TableGenBackends.h 
b/clang/utils/TableGen/TableGenBackends.h
index 3a424c9c91fe71..6e5508c6f68a5c 100644
--- a/clang/utils/TableGen/TableGenBackends.h
+++ b/clang/utils/TableGen/TableGenBackends.h
@@ -151,7 +151,7 @@ void EmitClangOpenCLBuiltinHeader(llvm::RecordKeeper 
&Records,
 void EmitClangOpenCLBuiltinTests(llvm::RecordKeeper &Records,
  llvm::raw_ostream &OS);
 
-void EmitClangDataCollectors(llvm::RecordKeeper &Records,
+void EmitClangDataCollectors(const llvm::RecordKeeper &Records,
  llvm::raw_ostream &OS);
 
 void EmitTestPragmaAttributeSupportedAttributes(llvm::RecordKeeper &Records,

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clangl[TableGen] Change Diagnostic Emitter to use const RecordKeeper (PR #108209)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul created 
https://github.com/llvm/llvm-project/pull/108209

None

>From 34a33e2b31ef10ecad91197dcae67164f5163ace Mon Sep 17 00:00:00 2001
From: Rahul Joshi 
Date: Wed, 11 Sep 2024 05:28:22 -0700
Subject: [PATCH] [clangl[TableGen] Change Diagnostic Emitter to use const
 RecordKeeper

---
 .../TableGen/ClangDiagnosticsEmitter.cpp  | 98 ++-
 clang/utils/TableGen/TableGenBackends.h   | 12 ++-
 2 files changed, 59 insertions(+), 51 deletions(-)

diff --git a/clang/utils/TableGen/ClangDiagnosticsEmitter.cpp 
b/clang/utils/TableGen/ClangDiagnosticsEmitter.cpp
index 6ca24a8c74b2ff..773668caa75747 100644
--- a/clang/utils/TableGen/ClangDiagnosticsEmitter.cpp
+++ b/clang/utils/TableGen/ClangDiagnosticsEmitter.cpp
@@ -39,12 +39,13 @@ using namespace llvm;
 
 namespace {
 class DiagGroupParentMap {
-  RecordKeeper &Records;
-  std::map > Mapping;
+  const RecordKeeper &Records;
+  std::map> Mapping;
+
 public:
-  DiagGroupParentMap(RecordKeeper &records) : Records(records) {
-std::vector DiagGroups
-  = Records.getAllDerivedDefinitions("DiagGroup");
+  DiagGroupParentMap(const RecordKeeper &records) : Records(records) {
+ArrayRef DiagGroups =
+Records.getAllDerivedDefinitions("DiagGroup");
 for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
   std::vector SubGroups =
 DiagGroups[i]->getValueAsListOfDefs("SubGroups");
@@ -53,7 +54,7 @@ class DiagGroupParentMap {
 }
   }
 
-  const std::vector &getParents(const Record *Group) {
+  const std::vector &getParents(const Record *Group) {
 return Mapping[Group];
   }
 };
@@ -68,7 +69,8 @@ getCategoryFromDiagGroup(const Record *Group,
 
   // The diag group may the subgroup of one or more other diagnostic groups,
   // check these for a category as well.
-  const std::vector &Parents = DiagGroupParents.getParents(Group);
+  const std::vector &Parents =
+  DiagGroupParents.getParents(Group);
   for (unsigned i = 0, e = Parents.size(); i != e; ++i) {
 CatName = getCategoryFromDiagGroup(Parents[i], DiagGroupParents);
 if (!CatName.empty()) return CatName;
@@ -94,19 +96,19 @@ static std::string getDiagnosticCategory(const Record *R,
 
 namespace {
   class DiagCategoryIDMap {
-RecordKeeper &Records;
+const RecordKeeper &Records;
 StringMap CategoryIDs;
 std::vector CategoryStrings;
   public:
-DiagCategoryIDMap(RecordKeeper &records) : Records(records) {
+DiagCategoryIDMap(const RecordKeeper &records) : Records(records) {
   DiagGroupParentMap ParentInfo(Records);
 
   // The zero'th category is "".
   CategoryStrings.push_back("");
   CategoryIDs[""] = 0;
 
-  std::vector Diags =
-  Records.getAllDerivedDefinitions("Diagnostic");
+  ArrayRef Diags =
+  Records.getAllDerivedDefinitions("Diagnostic");
   for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
 std::string Category = getDiagnosticCategory(Diags[i], ParentInfo);
 if (Category.empty()) continue;  // Skip diags with no category.
@@ -153,8 +155,8 @@ static bool diagGroupBeforeByName(const Record *LHS, const 
Record *RHS) {
 
 /// Invert the 1-[0/1] mapping of diags to group into a one to many
 /// mapping of groups to diags in the group.
-static void groupDiagnostics(const std::vector &Diags,
- const std::vector &DiagGroups,
+static void groupDiagnostics(ArrayRef Diags,
+ ArrayRef DiagGroups,
  std::map &DiagsInGroup) {
 
   for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
@@ -172,7 +174,7 @@ static void groupDiagnostics(const std::vector 
&Diags,
   // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
   // groups (these are warnings that GCC supports that clang never produces).
   for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
-Record *Group = DiagGroups[i];
+const Record *Group = DiagGroups[i];
 GroupInfo &GI =
 DiagsInGroup[std::string(Group->getValueAsString("GroupName"))];
 GI.GroupName = Group->getName();
@@ -255,20 +257,18 @@ class InferPedantic {
   GMap;
 
   DiagGroupParentMap &DiagGroupParents;
-  const std::vector &Diags;
-  const std::vector DiagGroups;
+  ArrayRef Diags;
+  const std::vector DiagGroups;
   std::map &DiagsInGroup;
   llvm::DenseSet DiagsSet;
   GMap GroupCount;
 public:
   InferPedantic(DiagGroupParentMap &DiagGroupParents,
-const std::vector &Diags,
-const std::vector &DiagGroups,
+ArrayRef Diags,
+ArrayRef DiagGroups,
 std::map &DiagsInGroup)
-  : DiagGroupParents(DiagGroupParents),
-  Diags(Diags),
-  DiagGroups(DiagGroups),
-  DiagsInGroup(DiagsInGroup) {}
+  : DiagGroupParents(DiagGroupParents), Diags(Diags),
+DiagGroups(DiagGroups), DiagsInGroup(DiagsInGroup) {}
 
   /// Compute the set of diagnostics and groups that are immediately
   /// in -Wpedantic

[clang] [clang][TableGen] Change Opcode Emitter to use const RecordKeeper (PR #108211)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul created 
https://github.com/llvm/llvm-project/pull/108211

None

>From 49650f70c36c37747da6d73865ebcb7b7a94da2f Mon Sep 17 00:00:00 2001
From: Rahul Joshi 
Date: Wed, 11 Sep 2024 05:33:50 -0700
Subject: [PATCH] [clang][TableGen] Change Opcode Emitter to use const
 RecordKeeper

---
 clang/utils/TableGen/ClangOpcodesEmitter.cpp | 6 +++---
 clang/utils/TableGen/TableGenBackends.h  | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/utils/TableGen/ClangOpcodesEmitter.cpp 
b/clang/utils/TableGen/ClangOpcodesEmitter.cpp
index 120e1e2efa32b4..7e426d59359a87 100644
--- a/clang/utils/TableGen/ClangOpcodesEmitter.cpp
+++ b/clang/utils/TableGen/ClangOpcodesEmitter.cpp
@@ -20,11 +20,11 @@ using namespace llvm;
 
 namespace {
 class ClangOpcodesEmitter {
-  RecordKeeper &Records;
+  const RecordKeeper &Records;
   unsigned NumTypes;
 
 public:
-  ClangOpcodesEmitter(RecordKeeper &R)
+  ClangOpcodesEmitter(const RecordKeeper &R)
   : Records(R), NumTypes(Records.getAllDerivedDefinitions("Type").size()) 
{}
 
   void run(raw_ostream &OS);
@@ -404,6 +404,6 @@ void ClangOpcodesEmitter::PrintTypes(raw_ostream &OS,
   OS << ">";
 }
 
-void clang::EmitClangOpcodes(RecordKeeper &Records, raw_ostream &OS) {
+void clang::EmitClangOpcodes(const RecordKeeper &Records, raw_ostream &OS) {
   ClangOpcodesEmitter(Records).run(OS);
 }
diff --git a/clang/utils/TableGen/TableGenBackends.h 
b/clang/utils/TableGen/TableGenBackends.h
index 3a424c9c91fe71..5050b764b09fb8 100644
--- a/clang/utils/TableGen/TableGenBackends.h
+++ b/clang/utils/TableGen/TableGenBackends.h
@@ -94,7 +94,7 @@ void EmitClangCommentCommandInfo(llvm::RecordKeeper &Records,
  llvm::raw_ostream &OS);
 void EmitClangCommentCommandList(llvm::RecordKeeper &Records,
  llvm::raw_ostream &OS);
-void EmitClangOpcodes(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
+void EmitClangOpcodes(const llvm::RecordKeeper &Records, llvm::raw_ostream 
&OS);
 
 void EmitClangSyntaxNodeList(llvm::RecordKeeper &Records,
  llvm::raw_ostream &OS);

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change OpenCL emitter to use const RecordKeeper (PR #108213)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul created 
https://github.com/llvm/llvm-project/pull/108213

None

>From b70f6ccab04237caf4e956deee4893dc13557088 Mon Sep 17 00:00:00 2001
From: Rahul Joshi 
Date: Wed, 11 Sep 2024 05:40:16 -0700
Subject: [PATCH] [clang][TableGen] Change OpenCL emitter to use const
 RecordKeeper

---
 .../TableGen/ClangOpenCLBuiltinEmitter.cpp| 46 ++-
 clang/utils/TableGen/TableGenBackends.h   |  6 +--
 2 files changed, 28 insertions(+), 24 deletions(-)

diff --git a/clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp 
b/clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
index 74c3a856ab6937..d68dcc472a7bdb 100644
--- a/clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
+++ b/clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
@@ -87,7 +87,7 @@ struct BuiltinTableEntries {
 //
 class BuiltinNameEmitter {
 public:
-  BuiltinNameEmitter(RecordKeeper &Records, raw_ostream &OS)
+  BuiltinNameEmitter(const RecordKeeper &Records, raw_ostream &OS)
   : Records(Records), OS(OS) {}
 
   // Entrypoint to generate the functions and structures for checking
@@ -100,7 +100,7 @@ class BuiltinNameEmitter {
 
   // Contains OpenCL builtin functions and related information, stored as
   // Record instances. They are coming from the associated TableGen file.
-  RecordKeeper &Records;
+  const RecordKeeper &Records;
 
   // The output file.
   raw_ostream &OS;
@@ -113,7 +113,7 @@ class BuiltinNameEmitter {
   // \param Output (out) String containing the enums to emit in the output 
file.
   // \param List (out) List containing the extracted Types, except the Types in
   //TypesSeen.
-  void ExtractEnumTypes(std::vector &Types,
+  void ExtractEnumTypes(ArrayRef Types,
 StringMap &TypesSeen, std::string &Output,
 std::vector &List);
 
@@ -237,7 +237,7 @@ class BuiltinNameEmitter {
 /// Base class for emitting a file (e.g. header or test) from OpenCLBuiltins.td
 class OpenCLBuiltinFileEmitterBase {
 public:
-  OpenCLBuiltinFileEmitterBase(RecordKeeper &Records, raw_ostream &OS)
+  OpenCLBuiltinFileEmitterBase(const RecordKeeper &Records, raw_ostream &OS)
   : Records(Records), OS(OS) {}
   virtual ~OpenCLBuiltinFileEmitterBase() = default;
 
@@ -305,7 +305,7 @@ class OpenCLBuiltinFileEmitterBase {
 
   // Contains OpenCL builtin functions and related information, stored as
   // Record instances. They are coming from the associated TableGen file.
-  RecordKeeper &Records;
+  const RecordKeeper &Records;
 
   // The output file.
   raw_ostream &OS;
@@ -316,7 +316,7 @@ class OpenCLBuiltinFileEmitterBase {
 // builtin function described in the .td input.
 class OpenCLBuiltinTestEmitter : public OpenCLBuiltinFileEmitterBase {
 public:
-  OpenCLBuiltinTestEmitter(RecordKeeper &Records, raw_ostream &OS)
+  OpenCLBuiltinTestEmitter(const RecordKeeper &Records, raw_ostream &OS)
   : OpenCLBuiltinFileEmitterBase(Records, OS) {}
 
   // Entrypoint to generate the functions for testing all OpenCL builtin
@@ -329,7 +329,7 @@ class OpenCLBuiltinTestEmitter : public 
OpenCLBuiltinFileEmitterBase {
 // prototype for each builtin function described in the .td input.
 class OpenCLBuiltinHeaderEmitter : public OpenCLBuiltinFileEmitterBase {
 public:
-  OpenCLBuiltinHeaderEmitter(RecordKeeper &Records, raw_ostream &OS)
+  OpenCLBuiltinHeaderEmitter(const RecordKeeper &Records, raw_ostream &OS)
   : OpenCLBuiltinFileEmitterBase(Records, OS) {}
 
   // Entrypoint to generate the header.
@@ -362,7 +362,7 @@ void BuiltinNameEmitter::Emit() {
   EmitQualTypeFinder();
 }
 
-void BuiltinNameEmitter::ExtractEnumTypes(std::vector &Types,
+void BuiltinNameEmitter::ExtractEnumTypes(ArrayRef Types,
   StringMap &TypesSeen,
   std::string &Output,
   std::vector &List) {
@@ -392,11 +392,11 @@ void BuiltinNameEmitter::EmitDeclarations() {
   // Extract generic types and non-generic types separately, to keep
   // gentypes at the end of the enum which simplifies the special handling
   // for gentypes in SemaLookup.
-  std::vector GenTypes =
+  ArrayRef GenTypes =
   Records.getAllDerivedDefinitions("GenericType");
   ExtractEnumTypes(GenTypes, TypesSeen, GenTypeEnums, GenTypeList);
 
-  std::vector Types = Records.getAllDerivedDefinitions("Type");
+  ArrayRef Types = Records.getAllDerivedDefinitions("Type");
   ExtractEnumTypes(Types, TypesSeen, TypeEnums, TypeList);
 
   OS << TypeEnums;
@@ -499,7 +499,7 @@ static void VerifySignature(const std::vector 
&Signature,
 
 void BuiltinNameEmitter::GetOverloads() {
   // Populate the TypeMap.
-  std::vector Types = Records.getAllDerivedDefinitions("Type");
+  ArrayRef Types = Records.getAllDerivedDefinitions("Type");
   unsigned I = 0;
   for (const auto &T : Types) {
 TypeMap.insert(std::make_pair(T, I++));
@@ -507,7 +507,8 @@ void BuiltinNameEmitter::GetOverloads() {
 
   // Populate the

[clang] [clang][TableGene] Change OptionDoc Emitter to use const RecordKeeper (PR #108216)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul created 
https://github.com/llvm/llvm-project/pull/108216

None

>From f45b440523b4d1043f42b969246900c24025871a Mon Sep 17 00:00:00 2001
From: Rahul Joshi 
Date: Wed, 11 Sep 2024 05:51:13 -0700
Subject: [PATCH] [clang][TableGene] Change OptionDoc Emitter to use const
 RecordKeeper

---
 .../utils/TableGen/ClangOptionDocEmitter.cpp  | 46 +--
 clang/utils/TableGen/TableGenBackends.h   |  2 +-
 2 files changed, 24 insertions(+), 24 deletions(-)

diff --git a/clang/utils/TableGen/ClangOptionDocEmitter.cpp 
b/clang/utils/TableGen/ClangOptionDocEmitter.cpp
index 86835611b84218..8c32f0218e761b 100644
--- a/clang/utils/TableGen/ClangOptionDocEmitter.cpp
+++ b/clang/utils/TableGen/ClangOptionDocEmitter.cpp
@@ -24,8 +24,8 @@ using namespace llvm;
 
 namespace {
 struct DocumentedOption {
-  Record *Option;
-  std::vector Aliases;
+  const Record *Option;
+  std::vector Aliases;
 };
 struct DocumentedGroup;
 struct Documentation {
@@ -37,7 +37,7 @@ struct Documentation {
   }
 };
 struct DocumentedGroup : Documentation {
-  Record *Group;
+  const Record *Group;
 };
 
 static bool hasFlag(const Record *Option, StringRef OptionFlag,
@@ -63,25 +63,25 @@ static bool isOptionVisible(const Record *Option, const 
Record *DocInfo) {
 }
 
 // Reorganize the records into a suitable form for emitting documentation.
-Documentation extractDocumentation(RecordKeeper &Records,
+Documentation extractDocumentation(const RecordKeeper &Records,
const Record *DocInfo) {
   Documentation Result;
 
   // Build the tree of groups. The root in the tree is the fake option group
   // (Record*)nullptr, which contains all top-level groups and options.
-  std::map > OptionsInGroup;
-  std::map > GroupsInGroup;
-  std::map > Aliases;
+  std::map> OptionsInGroup;
+  std::map> GroupsInGroup;
+  std::map> Aliases;
 
-  std::map OptionsByName;
-  for (Record *R : Records.getAllDerivedDefinitions("Option"))
+  std::map OptionsByName;
+  for (const Record *R : Records.getAllDerivedDefinitions("Option"))
 OptionsByName[std::string(R->getValueAsString("Name"))] = R;
 
-  auto Flatten = [](Record *R) {
+  auto Flatten = [](const Record *R) {
 return R->getValue("DocFlatten") && R->getValueAsBit("DocFlatten");
   };
 
-  auto SkipFlattened = [&](Record *R) -> Record* {
+  auto SkipFlattened = [&](const Record *R) -> const Record * {
 while (R && Flatten(R)) {
   auto *G = dyn_cast(R->getValueInit("Group"));
   if (!G)
@@ -91,17 +91,17 @@ Documentation extractDocumentation(RecordKeeper &Records,
 return R;
   };
 
-  for (Record *R : Records.getAllDerivedDefinitions("OptionGroup")) {
+  for (const Record *R : Records.getAllDerivedDefinitions("OptionGroup")) {
 if (Flatten(R))
   continue;
 
-Record *Group = nullptr;
+const Record *Group = nullptr;
 if (auto *G = dyn_cast(R->getValueInit("Group")))
   Group = SkipFlattened(G->getDef());
 GroupsInGroup[Group].push_back(R);
   }
 
-  for (Record *R : Records.getAllDerivedDefinitions("Option")) {
+  for (const Record *R : Records.getAllDerivedDefinitions("Option")) {
 if (auto *A = dyn_cast(R->getValueInit("Alias"))) {
   Aliases[A->getDef()].push_back(R);
   continue;
@@ -120,33 +120,33 @@ Documentation extractDocumentation(RecordKeeper &Records,
   }
 }
 
-Record *Group = nullptr;
+const Record *Group = nullptr;
 if (auto *G = dyn_cast(R->getValueInit("Group")))
   Group = SkipFlattened(G->getDef());
 OptionsInGroup[Group].push_back(R);
   }
 
-  auto CompareByName = [](Record *A, Record *B) {
+  auto CompareByName = [](const Record *A, const Record *B) {
 return A->getValueAsString("Name") < B->getValueAsString("Name");
   };
 
-  auto CompareByLocation = [](Record *A, Record *B) {
+  auto CompareByLocation = [](const Record *A, const Record *B) {
 return A->getLoc()[0].getPointer() < B->getLoc()[0].getPointer();
   };
 
-  auto DocumentationForOption = [&](Record *R) -> DocumentedOption {
+  auto DocumentationForOption = [&](const Record *R) -> DocumentedOption {
 auto &A = Aliases[R];
 llvm::sort(A, CompareByName);
 return {R, std::move(A)};
   };
 
-  std::function DocumentationForGroup =
-  [&](Record *R) -> Documentation {
+  std::function DocumentationForGroup =
+  [&](const Record *R) -> Documentation {
 Documentation D;
 
 auto &Groups = GroupsInGroup[R];
 llvm::sort(Groups, CompareByLocation);
-for (Record *G : Groups) {
+for (const Record *G : Groups) {
   D.Groups.emplace_back();
   D.Groups.back().Group = G;
   Documentation &Base = D.Groups.back();
@@ -157,7 +157,7 @@ Documentation extractDocumentation(RecordKeeper &Records,
 
 auto &Options = OptionsInGroup[R];
 llvm::sort(Options, CompareByName);
-for (Record *O : Options)
+for (const Record *O : Options)
   if (isOptionVisible(O, DocInfo))
 D.Options.push_back(DocumentationForOptio

[clang] [clang][TableGen] Migrate clang-tblgen to use const RecordKeeper (PR #107533)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul converted_to_draft 
https://github.com/llvm/llvm-project/pull/107533
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change ASTTableGen to use const Record pointers (PR #108193)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul ready_for_review 
https://github.com/llvm/llvm-project/pull/108193
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change ASTTableGen to use const Record pointers (PR #108193)

2024-09-11 Thread Rahul Joshi via cfe-commits

jurahul wrote:

@AaronBallman I am adding you as a reviewer, but if there is someone else who 
can help review these, please let me know.

https://github.com/llvm/llvm-project/pull/108193
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Migrate Builtins emitter to use const RecordKeeper (PR #108195)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul ready_for_review 
https://github.com/llvm/llvm-project/pull/108195
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Migrate Builtins emitter to use const RecordKeeper (PR #108195)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108195
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change HTML Emitter to use const RecordKeeper (PR #108201)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108201
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change HTML Tags emitter to use const RecordKeeper (PR #108202)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108202
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change DataCollector to use const RecordKeeper (PR #108203)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108203
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change Opcode Emitter to use const RecordKeeper (PR #108211)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108211
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clangl[TableGen] Change Diagnostic Emitter to use const RecordKeeper (PR #108209)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108209
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change OpenCL emitter to use const RecordKeeper (PR #108213)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108213
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGene] Change OptionDoc Emitter to use const RecordKeeper (PR #108216)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108216
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change OptionDoc Emitter to use const RecordKeeper (PR #108216)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108216
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change OptionDoc Emitter to use const RecordKeeper (PR #108216)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108216
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change OpenCL emitter to use const RecordKeeper (PR #108213)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108213
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change Opcode Emitter to use const RecordKeeper (PR #108211)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108211
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clangl[TableGen] Change Diagnostic Emitter to use const RecordKeeper (PR #108209)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108209
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change DataCollector to use const RecordKeeper (PR #108203)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108203
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change HTML Tags emitter to use const RecordKeeper (PR #108202)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108202
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change comment command emitter to const RecordKeeper (PR #108199)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108199
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change comment command emitter to const RecordKeeper (PR #108199)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul ready_for_review 
https://github.com/llvm/llvm-project/pull/108199
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change HTML Emitter to use const RecordKeeper (PR #108201)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul ready_for_review 
https://github.com/llvm/llvm-project/pull/108201
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change HTML Tags emitter to use const RecordKeeper (PR #108202)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul ready_for_review 
https://github.com/llvm/llvm-project/pull/108202
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change DataCollector to use const RecordKeeper (PR #108203)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul ready_for_review 
https://github.com/llvm/llvm-project/pull/108203
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clangl[TableGen] Change Diagnostic Emitter to use const RecordKeeper (PR #108209)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul ready_for_review 
https://github.com/llvm/llvm-project/pull/108209
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change Opcode Emitter to use const RecordKeeper (PR #108211)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul ready_for_review 
https://github.com/llvm/llvm-project/pull/108211
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change OptionDoc Emitter to use const RecordKeeper (PR #108216)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul ready_for_review 
https://github.com/llvm/llvm-project/pull/108216
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clangl[TableGen] Change Diagnostic Emitter to use const RecordKeeper (PR #108209)

2024-09-11 Thread Rahul Joshi via cfe-commits


@@ -68,7 +69,8 @@ getCategoryFromDiagGroup(const Record *Group,
 
   // The diag group may the subgroup of one or more other diagnostic groups,
   // check these for a category as well.
-  const std::vector &Parents = DiagGroupParents.getParents(Group);
+  const std::vector &Parents =

jurahul wrote:

We could, I was trying to not change more than what's necessary for this 
change. I think there are other cleanup like this that can done for all the 
clang tablegen backends (like using ArrayRef instead of const ref to vectors, 
range based for loops etc). I was thinking that those can be done in a separate 
NFC (I can do that once this series of patches goes in). 

https://github.com/llvm/llvm-project/pull/108209
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clangl[TableGen] Change Diagnostic Emitter to use const RecordKeeper (PR #108209)

2024-09-11 Thread Rahul Joshi via cfe-commits


@@ -255,20 +257,18 @@ class InferPedantic {
   GMap;
 
   DiagGroupParentMap &DiagGroupParents;
-  const std::vector &Diags;
-  const std::vector DiagGroups;
+  ArrayRef Diags;

jurahul wrote:

Yes. If you look at 
https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089

one of the changes in that RecordKeeper::getAllDerivedDefinitions() returns an 
ArrayRef to an internal cached vector, so this memory is owned by RecordKeeper, 
which for all TableGen backends is guaranteed to be live.

Here, Diags is set using  getAllDerivedDefinitions(), so should be safe. 

Note that previous code also uses a reference to a std::vector for Diags. 
However, in the earlier version getAllDerivedDefinitions() returned a vector by 
value. So, this is a reference to an unnamed temporary. I think C++ requires 
that the compiler extends the lifetime of the unnamed temp is it sees a 
reference to it being used, I am assuming that is correct even in the older 
version when we had the reference pointing to an unnamed temp return value of 
getAllDerivedDefinitions().

https://github.com/llvm/llvm-project/pull/108209
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clangl[TableGen] Change Diagnostic Emitter to use const RecordKeeper (PR #108209)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108209
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clangl[TableGen] Change Diagnostic Emitter to use const RecordKeeper (PR #108209)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108209
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clangl[TableGen] Change Diagnostic Emitter to use const RecordKeeper (PR #108209)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108209
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clangl[TableGen] Change Diagnostic Emitter to use const RecordKeeper (PR #108209)

2024-09-11 Thread Rahul Joshi via cfe-commits


@@ -39,12 +39,13 @@ using namespace llvm;
 
 namespace {
 class DiagGroupParentMap {
-  RecordKeeper &Records;
-  std::map > Mapping;
+  const RecordKeeper &Records;
+  std::map> Mapping;

jurahul wrote:

Sound good, I'll change in the follow on.

https://github.com/llvm/llvm-project/pull/108209
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clangl[TableGen] Change Diagnostic Emitter to use const RecordKeeper (PR #108209)

2024-09-11 Thread Rahul Joshi via cfe-commits


@@ -255,20 +257,18 @@ class InferPedantic {
   GMap;
 
   DiagGroupParentMap &DiagGroupParents;
-  const std::vector &Diags;
-  const std::vector DiagGroups;
+  ArrayRef Diags;

jurahul wrote:

Let me know if that answers your question. This PR will unblock some other PR's 
of similar nature that depend on this one.

https://github.com/llvm/llvm-project/pull/108209
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clangl[TableGen] Change Diagnostic Emitter to use const RecordKeeper (PR #108209)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul deleted 
https://github.com/llvm/llvm-project/pull/108209
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change ASTTableGen to use const Record pointers (PR #108193)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul closed 
https://github.com/llvm/llvm-project/pull/108193
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change OpenCL emitter to use const RecordKeeper (PR #108213)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul ready_for_review 
https://github.com/llvm/llvm-project/pull/108213
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change Builtins emitter to use const RecordKeeper (PR #108195)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108195
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change Builtins emitter to use const RecordKeeper (PR #108195)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108195
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change Builtins emitter to use const RecordKeeper (PR #108195)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul closed 
https://github.com/llvm/llvm-project/pull/108195
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change comment command emitter to const RecordKeeper (PR #108199)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul closed 
https://github.com/llvm/llvm-project/pull/108199
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change HTML Emitter to use const RecordKeeper (PR #108201)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul closed 
https://github.com/llvm/llvm-project/pull/108201
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change HTML Tags emitter to use const RecordKeeper (PR #108202)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul closed 
https://github.com/llvm/llvm-project/pull/108202
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change DataCollector to use const RecordKeeper (PR #108203)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul closed 
https://github.com/llvm/llvm-project/pull/108203
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change Opcode Emitter to use const RecordKeeper (PR #108211)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul closed 
https://github.com/llvm/llvm-project/pull/108211
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change OpenCL emitter to use const RecordKeeper (PR #108213)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul closed 
https://github.com/llvm/llvm-project/pull/108213
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change OptionDoc Emitter to use const RecordKeeper (PR #108216)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul closed 
https://github.com/llvm/llvm-project/pull/108216
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clangl[TableGen] Change Diagnostic Emitter to use const RecordKeeper (PR #108209)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul updated 
https://github.com/llvm/llvm-project/pull/108209

>From df228c650d4816eacc5b20771d8be294ab8ee134 Mon Sep 17 00:00:00 2001
From: Rahul Joshi 
Date: Wed, 11 Sep 2024 05:28:22 -0700
Subject: [PATCH] [clangl[TableGen] Change Diagnostic Emitter to use const
 RecordKeeper

---
 .../TableGen/ClangDiagnosticsEmitter.cpp  | 98 ++-
 clang/utils/TableGen/TableGenBackends.h   | 12 ++-
 2 files changed, 59 insertions(+), 51 deletions(-)

diff --git a/clang/utils/TableGen/ClangDiagnosticsEmitter.cpp 
b/clang/utils/TableGen/ClangDiagnosticsEmitter.cpp
index 6ca24a8c74b2ff..773668caa75747 100644
--- a/clang/utils/TableGen/ClangDiagnosticsEmitter.cpp
+++ b/clang/utils/TableGen/ClangDiagnosticsEmitter.cpp
@@ -39,12 +39,13 @@ using namespace llvm;
 
 namespace {
 class DiagGroupParentMap {
-  RecordKeeper &Records;
-  std::map > Mapping;
+  const RecordKeeper &Records;
+  std::map> Mapping;
+
 public:
-  DiagGroupParentMap(RecordKeeper &records) : Records(records) {
-std::vector DiagGroups
-  = Records.getAllDerivedDefinitions("DiagGroup");
+  DiagGroupParentMap(const RecordKeeper &records) : Records(records) {
+ArrayRef DiagGroups =
+Records.getAllDerivedDefinitions("DiagGroup");
 for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
   std::vector SubGroups =
 DiagGroups[i]->getValueAsListOfDefs("SubGroups");
@@ -53,7 +54,7 @@ class DiagGroupParentMap {
 }
   }
 
-  const std::vector &getParents(const Record *Group) {
+  const std::vector &getParents(const Record *Group) {
 return Mapping[Group];
   }
 };
@@ -68,7 +69,8 @@ getCategoryFromDiagGroup(const Record *Group,
 
   // The diag group may the subgroup of one or more other diagnostic groups,
   // check these for a category as well.
-  const std::vector &Parents = DiagGroupParents.getParents(Group);
+  const std::vector &Parents =
+  DiagGroupParents.getParents(Group);
   for (unsigned i = 0, e = Parents.size(); i != e; ++i) {
 CatName = getCategoryFromDiagGroup(Parents[i], DiagGroupParents);
 if (!CatName.empty()) return CatName;
@@ -94,19 +96,19 @@ static std::string getDiagnosticCategory(const Record *R,
 
 namespace {
   class DiagCategoryIDMap {
-RecordKeeper &Records;
+const RecordKeeper &Records;
 StringMap CategoryIDs;
 std::vector CategoryStrings;
   public:
-DiagCategoryIDMap(RecordKeeper &records) : Records(records) {
+DiagCategoryIDMap(const RecordKeeper &records) : Records(records) {
   DiagGroupParentMap ParentInfo(Records);
 
   // The zero'th category is "".
   CategoryStrings.push_back("");
   CategoryIDs[""] = 0;
 
-  std::vector Diags =
-  Records.getAllDerivedDefinitions("Diagnostic");
+  ArrayRef Diags =
+  Records.getAllDerivedDefinitions("Diagnostic");
   for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
 std::string Category = getDiagnosticCategory(Diags[i], ParentInfo);
 if (Category.empty()) continue;  // Skip diags with no category.
@@ -153,8 +155,8 @@ static bool diagGroupBeforeByName(const Record *LHS, const 
Record *RHS) {
 
 /// Invert the 1-[0/1] mapping of diags to group into a one to many
 /// mapping of groups to diags in the group.
-static void groupDiagnostics(const std::vector &Diags,
- const std::vector &DiagGroups,
+static void groupDiagnostics(ArrayRef Diags,
+ ArrayRef DiagGroups,
  std::map &DiagsInGroup) {
 
   for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
@@ -172,7 +174,7 @@ static void groupDiagnostics(const std::vector 
&Diags,
   // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
   // groups (these are warnings that GCC supports that clang never produces).
   for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
-Record *Group = DiagGroups[i];
+const Record *Group = DiagGroups[i];
 GroupInfo &GI =
 DiagsInGroup[std::string(Group->getValueAsString("GroupName"))];
 GI.GroupName = Group->getName();
@@ -255,20 +257,18 @@ class InferPedantic {
   GMap;
 
   DiagGroupParentMap &DiagGroupParents;
-  const std::vector &Diags;
-  const std::vector DiagGroups;
+  ArrayRef Diags;
+  const std::vector DiagGroups;
   std::map &DiagsInGroup;
   llvm::DenseSet DiagsSet;
   GMap GroupCount;
 public:
   InferPedantic(DiagGroupParentMap &DiagGroupParents,
-const std::vector &Diags,
-const std::vector &DiagGroups,
+ArrayRef Diags,
+ArrayRef DiagGroups,
 std::map &DiagsInGroup)
-  : DiagGroupParents(DiagGroupParents),
-  Diags(Diags),
-  DiagGroups(DiagGroups),
-  DiagsInGroup(DiagsInGroup) {}
+  : DiagGroupParents(DiagGroupParents), Diags(Diags),
+DiagGroups(DiagGroups), DiagsInGroup(DiagsInGroup) {}
 
   /// Compute the set of diagnostics and groups that are immediately
   /// in -Wpedantic.
@@ -

[clang] [clang][TableGen] Change AttrEmitter to use const RecordKeeper (PR #108269)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul created 
https://github.com/llvm/llvm-project/pull/108269

Change AttrEmitter to use const RecordKeeper.

>From 5c7d69a1595af72b48dc417f8d3aa89404fbf265 Mon Sep 17 00:00:00 2001
From: Rahul Joshi 
Date: Wed, 11 Sep 2024 11:32:36 -0700
Subject: [PATCH] [clang][TableGen] Change AttrEmitter to use const
 RecordKeeper

---
 clang/utils/TableGen/ClangAttrEmitter.cpp | 230 ++
 clang/utils/TableGen/TableGenBackends.h   |  51 ++---
 2 files changed, 133 insertions(+), 148 deletions(-)

diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp 
b/clang/utils/TableGen/ClangAttrEmitter.cpp
index d24215d10f17c7..9b2249ac90bc5c 100644
--- a/clang/utils/TableGen/ClangAttrEmitter.cpp
+++ b/clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -189,13 +189,12 @@ static StringRef NormalizeGNUAttrSpelling(StringRef 
AttrSpelling) {
 
 typedef std::vector> ParsedAttrMap;
 
-static ParsedAttrMap getParsedAttrList(RecordKeeper &Records,
+static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records,
ParsedAttrMap *Dupes = nullptr,
bool SemaOnly = true) {
-  std::vector Attrs = Records.getAllDerivedDefinitions("Attr");
   std::set Seen;
   ParsedAttrMap R;
-  for (const auto *Attr : Attrs) {
+  for (const Record *Attr : Records.getAllDerivedDefinitions("Attr")) {
 if (!SemaOnly || Attr->getValueAsBit("SemaHandler")) {
   std::string AN;
   if (Attr->isSubClassOf("TargetSpecificAttr") &&
@@ -1911,12 +1910,10 @@ static LateAttrParseKind getLateAttrParseKind(const 
Record *Attr) {
 }
 
 // Emits the LateParsed property for attributes.
-static void emitClangAttrLateParsedListImpl(RecordKeeper &Records,
+static void emitClangAttrLateParsedListImpl(const RecordKeeper &Records,
 raw_ostream &OS,
 LateAttrParseKind LateParseMode) {
-  std::vector Attrs = Records.getAllDerivedDefinitions("Attr");
-
-  for (const auto *Attr : Attrs) {
+  for (const auto *Attr : Records.getAllDerivedDefinitions("Attr")) {
 if (LateAttrParseKind LateParsed = getLateAttrParseKind(Attr);
 LateParsed != LateParseMode)
   continue;
@@ -1932,14 +1929,14 @@ static void 
emitClangAttrLateParsedListImpl(RecordKeeper &Records,
   }
 }
 
-static void emitClangAttrLateParsedList(RecordKeeper &Records,
+static void emitClangAttrLateParsedList(const RecordKeeper &Records,
 raw_ostream &OS) {
   OS << "#if defined(CLANG_ATTR_LATE_PARSED_LIST)\n";
   emitClangAttrLateParsedListImpl(Records, OS, LateAttrParseKind::Standard);
   OS << "#endif // CLANG_ATTR_LATE_PARSED_LIST\n\n";
 }
 
-static void emitClangAttrLateParsedExperimentalList(RecordKeeper &Records,
+static void emitClangAttrLateParsedExperimentalList(const RecordKeeper 
&Records,
 raw_ostream &OS) {
   OS << "#if defined(CLANG_ATTR_LATE_PARSED_EXPERIMENTAL_EXT_LIST)\n";
   emitClangAttrLateParsedListImpl(Records, OS,
@@ -2066,7 +2063,7 @@ struct PragmaClangAttributeSupport {
   };
   llvm::DenseMap SubjectsToRules;
 
-  PragmaClangAttributeSupport(RecordKeeper &Records);
+  PragmaClangAttributeSupport(const RecordKeeper &Records);
 
   bool isAttributedSupported(const Record &Attribute);
 
@@ -2105,9 +2102,7 @@ static bool doesDeclDeriveFrom(const Record *D, const 
Record *Base) {
 }
 
 PragmaClangAttributeSupport::PragmaClangAttributeSupport(
-RecordKeeper &Records) {
-  std::vector MetaSubjects =
-  Records.getAllDerivedDefinitions("AttrSubjectMatcherRule");
+const RecordKeeper &Records) {
   auto MapFromSubjectsToRules = [this](const Record *SubjectContainer,
const Record *MetaSubject,
const Record *Constraint) {
@@ -2127,7 +2122,8 @@ PragmaClangAttributeSupport::PragmaClangAttributeSupport(
   }
 }
   };
-  for (const auto *MetaSubject : MetaSubjects) {
+  for (const auto *MetaSubject :
+   Records.getAllDerivedDefinitions("AttrSubjectMatcherRule")) {
 MapFromSubjectsToRules(MetaSubject, MetaSubject, /*Constraints=*/nullptr);
 std::vector Constraints =
 MetaSubject->getValueAsListOfDefs("Constraints");
@@ -2135,11 +2131,10 @@ 
PragmaClangAttributeSupport::PragmaClangAttributeSupport(
   MapFromSubjectsToRules(Constraint, MetaSubject, Constraint);
   }
 
-  std::vector Aggregates =
-  Records.getAllDerivedDefinitions("AttrSubjectMatcherAggregateRule");
-  std::vector DeclNodes =
-Records.getAllDerivedDefinitions(DeclNodeClassName);
-  for (const auto *Aggregate : Aggregates) {
+  ArrayRef DeclNodes =
+  Records.getAllDerivedDefinitions(DeclNodeClassName);
+  for (const auto *Aggregate :
+   Records.getAllDerivedDefinitions("AttrSubjectMatcherAggregateRule")) {
 Record *SubjectDecl = Aggregate->getValueAsDef("Subject");
 
 // Gather sub-classe

[clang] [clang][TableGen] Change AttrEmitter to use const RecordKeeper (PR #108269)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108269
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][TableGen] Change ASTProperties Emitter to use const RecordKeeper (PR #108274)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul created 
https://github.com/llvm/llvm-project/pull/108274

Change ASTProperties Emitter to use const RecordKeeper.

>From d5396fdc12a6a24960b42ec9f50556b00e42e08e Mon Sep 17 00:00:00 2001
From: Rahul Joshi 
Date: Wed, 11 Sep 2024 11:51:51 -0700
Subject: [PATCH] [clang][TableGen] Change ASTProperties Emitter to use const
 RecordKeeper

---
 .../TableGen/ClangASTPropertiesEmitter.cpp| 38 ++-
 clang/utils/TableGen/TableGenBackends.h   | 12 --
 2 files changed, 28 insertions(+), 22 deletions(-)

diff --git a/clang/utils/TableGen/ClangASTPropertiesEmitter.cpp 
b/clang/utils/TableGen/ClangASTPropertiesEmitter.cpp
index de8dda60681ff8..7cf437a4a2aa9b 100644
--- a/clang/utils/TableGen/ClangASTPropertiesEmitter.cpp
+++ b/clang/utils/TableGen/ClangASTPropertiesEmitter.cpp
@@ -89,21 +89,21 @@ struct CasedTypeInfo {
 
 class ASTPropsEmitter {
raw_ostream &Out;
-   RecordKeeper &Records;
-   std::map NodeInfos;
+const RecordKeeper &Records;
+std::map NodeInfos;
   std::vector AllPropertyTypes;
   std::map CasedTypeInfos;
 
 public:
-   ASTPropsEmitter(RecordKeeper &records, raw_ostream &out)
-   : Out(out), Records(records) {
-
-   // Find all the properties.
-   for (Property property :
-   records.getAllDerivedDefinitions(PropertyClassName)) {
-   HasProperties node = property.getClass();
-   NodeInfos[node].Properties.push_back(property);
-   }
+  ASTPropsEmitter(const RecordKeeper &records, raw_ostream &out)
+  : Out(out), Records(records) {
+
+// Find all the properties.
+for (Property property :
+ records.getAllDerivedDefinitions(PropertyClassName)) {
+  HasProperties node = property.getClass();
+  NodeInfos[node].Properties.push_back(property);
+}
 
 // Find all the creation rules.
 for (CreationRule creationRule :
@@ -177,7 +177,7 @@ class ASTPropsEmitter {
 }
 
 Validator(*this).validate();
-   }
+  }
 
   void visitAllProperties(HasProperties derived, const NodeInfo &derivedInfo,
   function_ref visit) {
@@ -591,28 +591,28 @@ void ASTPropsEmitter::emitWriteOfProperty(StringRef 
writerName,
 /// Emit an .inc file that defines the AbstractFooReader class
 /// for the given AST class hierarchy.
 template 
-static void emitASTReader(RecordKeeper &records, raw_ostream &out,
+static void emitASTReader(const RecordKeeper &records, raw_ostream &out,
   StringRef description) {
   emitSourceFileHeader(description, out, records);
 
   ASTPropsEmitter(records, out).emitNodeReaderClass();
 }
 
-void clang::EmitClangTypeReader(RecordKeeper &records, raw_ostream &out) {
+void clang::EmitClangTypeReader(const RecordKeeper &records, raw_ostream &out) 
{
   emitASTReader(records, out, "A CRTP reader for Clang Type nodes");
 }
 
 /// Emit an .inc file that defines the AbstractFooWriter class
 /// for the given AST class hierarchy.
 template 
-static void emitASTWriter(RecordKeeper &records, raw_ostream &out,
+static void emitASTWriter(const RecordKeeper &records, raw_ostream &out,
   StringRef description) {
   emitSourceFileHeader(description, out, records);
 
   ASTPropsEmitter(records, out).emitNodeWriterClass();
 }
 
-void clang::EmitClangTypeWriter(RecordKeeper &records, raw_ostream &out) {
+void clang::EmitClangTypeWriter(const RecordKeeper &records, raw_ostream &out) 
{
   emitASTWriter(records, out, "A CRTP writer for Clang Type nodes");
 }
 
@@ -851,7 +851,8 @@ void ASTPropsEmitter::emitBasicReaderWriterFile(const 
ReaderWriterInfo &info) {
 
 /// Emit an .inc file that defines some helper classes for reading
 /// basic values.
-void clang::EmitClangBasicReader(RecordKeeper &records, raw_ostream &out) {
+void clang::EmitClangBasicReader(const RecordKeeper &records,
+ raw_ostream &out) {
   emitSourceFileHeader("Helper classes for BasicReaders", out, records);
 
   // Use any property, we won't be using those properties.
@@ -861,7 +862,8 @@ void clang::EmitClangBasicReader(RecordKeeper &records, 
raw_ostream &out) {
 
 /// Emit an .inc file that defines some helper classes for writing
 /// basic values.
-void clang::EmitClangBasicWriter(RecordKeeper &records, raw_ostream &out) {
+void clang::EmitClangBasicWriter(const RecordKeeper &records,
+ raw_ostream &out) {
   emitSourceFileHeader("Helper classes for BasicWriters", out, records);
 
   // Use any property, we won't be using those properties.
diff --git a/clang/utils/TableGen/TableGenBackends.h 
b/clang/utils/TableGen/TableGenBackends.h
index fe55ef2f423afe..afff329c0aadc1 100644
--- a/clang/utils/TableGen/TableGenBackends.h
+++ b/clang/utils/TableGen/TableGenBackends.h
@@ -35,11 +35,15 @@ void EmitClangDeclContext(llvm::RecordKeeper &RK, 
llvm::raw_ostream &OS);
 void EmitClangASTNodes(llvm::Reco

[clang] [clang][TableGen] Change ASTProperties Emitter to use const RecordKeeper (PR #108274)

2024-09-11 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul edited 
https://github.com/llvm/llvm-project/pull/108274
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [Support] Validate number of arguments passed to formatv() (PR #105745)

2024-08-26 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul updated 
https://github.com/llvm/llvm-project/pull/105745

>From 7b8c1794d4f2510502349d1151c8266c8b234ac0 Mon Sep 17 00:00:00 2001
From: Rahul Joshi 
Date: Thu, 22 Aug 2024 08:47:02 -0700
Subject: [PATCH 1/2] [Support] Detect invalid formatv() calls

- Detect formatv() calls where the number of replacement parameters
  expected after parsing the format string does not match the number
  provides in the formatv() call.
- assert() in debug builds, and fail the formatv() call in release
  builds by just emitting an error message in the stream.
---
 llvm/include/llvm/Support/FormatVariadic.h| 21 -
 llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp|  3 +-
 llvm/lib/Support/FormatVariadic.cpp   | 10 ++-
 llvm/unittests/Support/FormatVariadicTest.cpp | 79 +--
 4 files changed, 81 insertions(+), 32 deletions(-)

diff --git a/llvm/include/llvm/Support/FormatVariadic.h 
b/llvm/include/llvm/Support/FormatVariadic.h
index 595f2cf559a428..c0c931b3541040 100644
--- a/llvm/include/llvm/Support/FormatVariadic.h
+++ b/llvm/include/llvm/Support/FormatVariadic.h
@@ -83,7 +83,20 @@ class formatv_object_base {
 
 public:
   void format(raw_ostream &S) const {
-for (auto &R : parseFormatString(Fmt)) {
+const auto [Replacements, NumExpectedParams] = parseFormatString(Fmt);
+// Fail formatv() call if the number of replacement parameters provided
+// does not match the expected number after parsing the format string.
+// Assert in debug builds.
+assert(NumExpectedParams == Adapters.size() &&
+   "Mismatch between replacement parameters expected and provided");
+if (NumExpectedParams != Adapters.size()) {
+  S << "formatv() error: " << NumExpectedParams
+<< " replacement parameters expected, but " << Adapters.size()
+<< " provided";
+  return;
+}
+
+for (const auto &R : Replacements) {
   if (R.Type == ReplacementType::Empty)
 continue;
   if (R.Type == ReplacementType::Literal) {
@@ -101,7 +114,11 @@ class formatv_object_base {
   Align.format(S, R.Options);
 }
   }
-  static SmallVector parseFormatString(StringRef Fmt);
+
+  // Parse format string and return the array of replacement items as well as
+  // the number of values we expect to be supplied to the formatv() call.
+  static std::pair, size_t>
+  parseFormatString(StringRef Fmt);
 
   static std::optional parseReplacementItem(StringRef Spec);
 
diff --git a/llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp 
b/llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp
index 77e8ece9439cf9..eb2751ab30ac50 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp
@@ -1518,8 +1518,7 @@ DWARFVerifier::verifyNameIndexAbbrevs(const 
DWARFDebugNames::NameIndex &NI) {
 error() << formatv("NameIndex @ {0:x}: Indexing multiple compile units 
"
"and abbreviation {1:x} has no DW_IDX_compile_unit "
"or DW_IDX_type_unit attribute.\n",
-   NI.getUnitOffset(), Abbrev.Code,
-   dwarf::DW_IDX_compile_unit);
+   NI.getUnitOffset(), Abbrev.Code);
   });
   ++NumErrors;
 }
diff --git a/llvm/lib/Support/FormatVariadic.cpp 
b/llvm/lib/Support/FormatVariadic.cpp
index e25d036cdf1e8c..c355aa519b8c53 100644
--- a/llvm/lib/Support/FormatVariadic.cpp
+++ b/llvm/lib/Support/FormatVariadic.cpp
@@ -35,8 +35,7 @@ bool formatv_object_base::consumeFieldLayout(StringRef &Spec, 
AlignStyle &Where,
 
   if (Spec.size() > 1) {
 // A maximum of 2 characters at the beginning can be used for something
-// other
-// than the width.
+// other than the width.
 // If Spec[1] is a loc char, then Spec[0] is a pad char and Spec[2:...]
 // contains the width.
 // Otherwise, if Spec[0] is a loc char, then Spec[1:...] contains the 
width.
@@ -143,16 +142,19 @@ formatv_object_base::splitLiteralAndReplacement(StringRef 
Fmt) {
   return std::make_pair(ReplacementItem{Fmt}, StringRef());
 }
 
-SmallVector
+std::pair, size_t>
 formatv_object_base::parseFormatString(StringRef Fmt) {
   SmallVector Replacements;
+  size_t NumExpectedParams = 0;
   ReplacementItem I;
   while (!Fmt.empty()) {
 std::tie(I, Fmt) = splitLiteralAndReplacement(Fmt);
 if (I.Type != ReplacementType::Empty)
   Replacements.push_back(I);
+if (I.Type == ReplacementType::Format)
+  NumExpectedParams = std::max(NumExpectedParams, I.Index + 1);
   }
-  return Replacements;
+  return {Replacements, NumExpectedParams};
 }
 
 void support::detail::format_adapter::anchor() {}
diff --git a/llvm/unittests/Support/FormatVariadicTest.cpp 
b/llvm/unittests/Support/FormatVariadicTest.cpp
index a78b25c53d7e43..b94c432f73b41a 100644
--- a/llvm/unittests/Support/FormatVariadicTest.cpp
+++ b/llvm/unittests/Support/FormatVariadicTest.cpp
@@ -36,13 +36,15 @@ static_assert(uses_missing_provider::value, "");
 }
 
 

[clang] [llvm] [mlir] [Support] Validate number of arguments passed to formatv() (PR #105745)

2024-08-27 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul updated 
https://github.com/llvm/llvm-project/pull/105745

>From 5cfd34a7f0177b52d4616f3c3e03e2ae14bfaae8 Mon Sep 17 00:00:00 2001
From: Rahul Joshi 
Date: Thu, 22 Aug 2024 08:47:02 -0700
Subject: [PATCH] [Support] Detect invalid formatv() calls

- Detect formatv() calls where the number of replacement parameters
  expected after parsing the format string does not match the number
  provides in the formatv() call.
- assert() in debug builds, and fail the formatv() call in release
  builds by just emitting an error message in the stream.
---
 .../Checkers/CheckPlacementNew.cpp|   2 +-
 .../Checkers/StdLibraryFunctionsChecker.cpp   |   2 +-
 llvm/include/llvm/Support/FormatVariadic.h|  54 --
 llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp|   3 +-
 .../Orc/CompileOnDemandLayer.cpp  |   8 +-
 llvm/lib/Support/FormatVariadic.cpp   | 172 +++---
 .../tools/llvm-pdbutil/ExplainOutputStyle.cpp |   2 +-
 llvm/unittests/Support/FormatVariadicTest.cpp | 115 +---
 llvm/utils/TableGen/IntrinsicEmitter.cpp  |   4 +-
 mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp   |  11 +-
 10 files changed, 240 insertions(+), 133 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Checkers/CheckPlacementNew.cpp 
b/clang/lib/StaticAnalyzer/Checkers/CheckPlacementNew.cpp
index 99e11a15c08dc2..1b89951397cfb1 100644
--- a/clang/lib/StaticAnalyzer/Checkers/CheckPlacementNew.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/CheckPlacementNew.cpp
@@ -131,7 +131,7 @@ bool PlacementNewChecker::checkPlaceCapacityIsSufficient(
 "Storage provided to placement new is only {0} bytes, "
 "whereas the allocated array type requires more space for "
 "internal needs",
-SizeOfPlaceCI->getValue(), SizeOfTargetCI->getValue()));
+SizeOfPlaceCI->getValue()));
   else
 Msg = std::string(llvm::formatv(
 "Storage provided to placement new is only {0} bytes, "
diff --git a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp 
b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
index 8f4bd17afc8581..60c035612dcd44 100644
--- a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
@@ -1401,7 +1401,7 @@ void StdLibraryFunctionsChecker::checkPostCall(const 
CallEvent &Call,
 ErrnoNote =
 llvm::formatv("After calling '{0}' {1}", FunctionName, ErrnoNote);
 } else {
-  CaseNote = llvm::formatv(Case.getNote().str().c_str(), FunctionName);
+  CaseNote = llvm::formatvv(Case.getNote().str().c_str(), FunctionName);
 }
 const SVal RV = Call.getReturnValue();
 
diff --git a/llvm/include/llvm/Support/FormatVariadic.h 
b/llvm/include/llvm/Support/FormatVariadic.h
index 595f2cf559a428..b42e24646b31b5 100644
--- a/llvm/include/llvm/Support/FormatVariadic.h
+++ b/llvm/include/llvm/Support/FormatVariadic.h
@@ -66,24 +66,24 @@ struct ReplacementItem {
 class formatv_object_base {
 protected:
   StringRef Fmt;
+  bool Validate;
   ArrayRef Adapters;
 
-  static bool consumeFieldLayout(StringRef &Spec, AlignStyle &Where,
- size_t &Align, char &Pad);
-
-  static std::pair
-  splitLiteralAndReplacement(StringRef Fmt);
-
-  formatv_object_base(StringRef Fmt,
+  formatv_object_base(StringRef Fmt, bool Validate,
   ArrayRef Adapters)
-  : Fmt(Fmt), Adapters(Adapters) {}
+  : Fmt(Fmt), Validate(Validate), Adapters(Adapters) {}
 
   formatv_object_base(formatv_object_base const &rhs) = delete;
   formatv_object_base(formatv_object_base &&rhs) = default;
 
 public:
   void format(raw_ostream &S) const {
-for (auto &R : parseFormatString(Fmt)) {
+const auto [Replacements, IsValid] =
+parseFormatString(S, Fmt, Adapters.size(), Validate);
+if (!IsValid)
+  return;
+
+for (const auto &R : Replacements) {
   if (R.Type == ReplacementType::Empty)
 continue;
   if (R.Type == ReplacementType::Literal) {
@@ -101,9 +101,13 @@ class formatv_object_base {
   Align.format(S, R.Options);
 }
   }
-  static SmallVector parseFormatString(StringRef Fmt);
 
-  static std::optional parseReplacementItem(StringRef Spec);
+  // Parse format string and return the array of replacement items. If there is
+  // an error in the format string, return false for the second member of the
+  // pair, and print the error message to `S`.
+  static std::pair, bool>
+  parseFormatString(raw_ostream &S, StringRef Fmt, size_t NumArgs,
+bool Validate);
 
   std::string str() const {
 std::string Result;
@@ -149,8 +153,8 @@ template  class formatv_object : public 
formatv_object_base {
   };
 
 public:
-  formatv_object(StringRef Fmt, Tuple &&Params)
-  : formatv_object_base(Fmt, ParameterPointers),
+  formatv_object(StringRef Fmt, bool ValidateNumArgs, Tuple &&Params)
+  : formatv

[clang] [llvm] [mlir] [Support] Validate number of arguments passed to formatv() (PR #105745)

2024-08-27 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul updated 
https://github.com/llvm/llvm-project/pull/105745

>From d834ebbc8f34143ccaf905c82257742dea61ea9e Mon Sep 17 00:00:00 2001
From: Rahul Joshi 
Date: Thu, 22 Aug 2024 08:47:02 -0700
Subject: [PATCH] [Support] Detect invalid formatv() calls

- Detect formatv() calls where the number of replacement parameters
  expected after parsing the format string does not match the number
  provides in the formatv() call.
- assert() in debug builds, and fail the formatv() call in release
  builds by just emitting an error message in the stream.
---
 .../Checkers/CheckPlacementNew.cpp|   2 +-
 .../Checkers/StdLibraryFunctionsChecker.cpp   |   2 +-
 llvm/include/llvm/Support/FormatVariadic.h|  54 --
 llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp|   3 +-
 .../Orc/CompileOnDemandLayer.cpp  |   8 +-
 llvm/lib/Support/FormatVariadic.cpp   | 172 +++---
 .../tools/llvm-pdbutil/ExplainOutputStyle.cpp |   4 +-
 llvm/unittests/Support/FormatVariadicTest.cpp | 115 +---
 llvm/utils/TableGen/IntrinsicEmitter.cpp  |   4 +-
 mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp   |  11 +-
 10 files changed, 241 insertions(+), 134 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Checkers/CheckPlacementNew.cpp 
b/clang/lib/StaticAnalyzer/Checkers/CheckPlacementNew.cpp
index 99e11a15c08dc2..1b89951397cfb1 100644
--- a/clang/lib/StaticAnalyzer/Checkers/CheckPlacementNew.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/CheckPlacementNew.cpp
@@ -131,7 +131,7 @@ bool PlacementNewChecker::checkPlaceCapacityIsSufficient(
 "Storage provided to placement new is only {0} bytes, "
 "whereas the allocated array type requires more space for "
 "internal needs",
-SizeOfPlaceCI->getValue(), SizeOfTargetCI->getValue()));
+SizeOfPlaceCI->getValue()));
   else
 Msg = std::string(llvm::formatv(
 "Storage provided to placement new is only {0} bytes, "
diff --git a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp 
b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
index 8f4bd17afc8581..60c035612dcd44 100644
--- a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
@@ -1401,7 +1401,7 @@ void StdLibraryFunctionsChecker::checkPostCall(const 
CallEvent &Call,
 ErrnoNote =
 llvm::formatv("After calling '{0}' {1}", FunctionName, ErrnoNote);
 } else {
-  CaseNote = llvm::formatv(Case.getNote().str().c_str(), FunctionName);
+  CaseNote = llvm::formatvv(Case.getNote().str().c_str(), FunctionName);
 }
 const SVal RV = Call.getReturnValue();
 
diff --git a/llvm/include/llvm/Support/FormatVariadic.h 
b/llvm/include/llvm/Support/FormatVariadic.h
index 595f2cf559a428..b42e24646b31b5 100644
--- a/llvm/include/llvm/Support/FormatVariadic.h
+++ b/llvm/include/llvm/Support/FormatVariadic.h
@@ -66,24 +66,24 @@ struct ReplacementItem {
 class formatv_object_base {
 protected:
   StringRef Fmt;
+  bool Validate;
   ArrayRef Adapters;
 
-  static bool consumeFieldLayout(StringRef &Spec, AlignStyle &Where,
- size_t &Align, char &Pad);
-
-  static std::pair
-  splitLiteralAndReplacement(StringRef Fmt);
-
-  formatv_object_base(StringRef Fmt,
+  formatv_object_base(StringRef Fmt, bool Validate,
   ArrayRef Adapters)
-  : Fmt(Fmt), Adapters(Adapters) {}
+  : Fmt(Fmt), Validate(Validate), Adapters(Adapters) {}
 
   formatv_object_base(formatv_object_base const &rhs) = delete;
   formatv_object_base(formatv_object_base &&rhs) = default;
 
 public:
   void format(raw_ostream &S) const {
-for (auto &R : parseFormatString(Fmt)) {
+const auto [Replacements, IsValid] =
+parseFormatString(S, Fmt, Adapters.size(), Validate);
+if (!IsValid)
+  return;
+
+for (const auto &R : Replacements) {
   if (R.Type == ReplacementType::Empty)
 continue;
   if (R.Type == ReplacementType::Literal) {
@@ -101,9 +101,13 @@ class formatv_object_base {
   Align.format(S, R.Options);
 }
   }
-  static SmallVector parseFormatString(StringRef Fmt);
 
-  static std::optional parseReplacementItem(StringRef Spec);
+  // Parse format string and return the array of replacement items. If there is
+  // an error in the format string, return false for the second member of the
+  // pair, and print the error message to `S`.
+  static std::pair, bool>
+  parseFormatString(raw_ostream &S, StringRef Fmt, size_t NumArgs,
+bool Validate);
 
   std::string str() const {
 std::string Result;
@@ -149,8 +153,8 @@ template  class formatv_object : public 
formatv_object_base {
   };
 
 public:
-  formatv_object(StringRef Fmt, Tuple &&Params)
-  : formatv_object_base(Fmt, ParameterPointers),
+  formatv_object(StringRef Fmt, bool ValidateNumArgs, Tuple &&Params)
+  : formatv

[clang] [lldb] [llvm] [mlir] [Support] Validate number of arguments passed to formatv() (PR #105745)

2024-08-27 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul updated 
https://github.com/llvm/llvm-project/pull/105745

>From 9ae8a0361b0d26cf29cc4547658baec0c2654c89 Mon Sep 17 00:00:00 2001
From: Rahul Joshi 
Date: Thu, 22 Aug 2024 08:47:02 -0700
Subject: [PATCH] [Support] Detect invalid formatv() calls

- Detect formatv() calls where the number of replacement parameters
  expected after parsing the format string does not match the number
  provides in the formatv() call.
- assert() in debug builds, and fail the formatv() call in release
  builds by just emitting an error message in the stream.
---
 .../Checkers/CheckPlacementNew.cpp|   2 +-
 .../Checkers/StdLibraryFunctionsChecker.cpp   |   2 +-
 .../Plugins/SymbolFile/DWARF/DWARFUnit.cpp|   2 +-
 llvm/include/llvm/Support/FormatVariadic.h|  54 --
 llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp|   3 +-
 .../Orc/CompileOnDemandLayer.cpp  |   8 +-
 llvm/lib/Support/FormatVariadic.cpp   | 172 +++---
 .../tools/llvm-pdbutil/ExplainOutputStyle.cpp |   4 +-
 llvm/unittests/Support/FormatVariadicTest.cpp | 115 +---
 llvm/utils/TableGen/IntrinsicEmitter.cpp  |   4 +-
 .../mlir-linalg-ods-yaml-gen.cpp  |   4 +-
 .../tools/mlir-tblgen/LLVMIRConversionGen.cpp |   2 +-
 mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp   |  13 +-
 mlir/tools/mlir-tblgen/OpFormatGen.cpp|  14 +-
 mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp  |   3 +-
 15 files changed, 253 insertions(+), 149 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Checkers/CheckPlacementNew.cpp 
b/clang/lib/StaticAnalyzer/Checkers/CheckPlacementNew.cpp
index 99e11a15c08dc2..1b89951397cfb1 100644
--- a/clang/lib/StaticAnalyzer/Checkers/CheckPlacementNew.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/CheckPlacementNew.cpp
@@ -131,7 +131,7 @@ bool PlacementNewChecker::checkPlaceCapacityIsSufficient(
 "Storage provided to placement new is only {0} bytes, "
 "whereas the allocated array type requires more space for "
 "internal needs",
-SizeOfPlaceCI->getValue(), SizeOfTargetCI->getValue()));
+SizeOfPlaceCI->getValue()));
   else
 Msg = std::string(llvm::formatv(
 "Storage provided to placement new is only {0} bytes, "
diff --git a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp 
b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
index 8f4bd17afc8581..60c035612dcd44 100644
--- a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
@@ -1401,7 +1401,7 @@ void StdLibraryFunctionsChecker::checkPostCall(const 
CallEvent &Call,
 ErrnoNote =
 llvm::formatv("After calling '{0}' {1}", FunctionName, ErrnoNote);
 } else {
-  CaseNote = llvm::formatv(Case.getNote().str().c_str(), FunctionName);
+  CaseNote = llvm::formatvv(Case.getNote().str().c_str(), FunctionName);
 }
 const SVal RV = Call.getReturnValue();
 
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp 
b/lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp
index feb72f6244a18c..cdb266f70f0e3e 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp
@@ -655,7 +655,7 @@ DWARFUnit::GetDIE(dw_offset_t die_offset) {
 
   if (!ContainsDIEOffset(die_offset)) {
 GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
-"GetDIE for DIE {0:x16} is outside of its CU {0:x16}", die_offset,
+"GetDIE for DIE {0:x16} is outside of its CU {1:x16}", die_offset,
 GetOffset());
 return DWARFDIE(); // Not found
   }
diff --git a/llvm/include/llvm/Support/FormatVariadic.h 
b/llvm/include/llvm/Support/FormatVariadic.h
index 595f2cf559a428..b42e24646b31b5 100644
--- a/llvm/include/llvm/Support/FormatVariadic.h
+++ b/llvm/include/llvm/Support/FormatVariadic.h
@@ -66,24 +66,24 @@ struct ReplacementItem {
 class formatv_object_base {
 protected:
   StringRef Fmt;
+  bool Validate;
   ArrayRef Adapters;
 
-  static bool consumeFieldLayout(StringRef &Spec, AlignStyle &Where,
- size_t &Align, char &Pad);
-
-  static std::pair
-  splitLiteralAndReplacement(StringRef Fmt);
-
-  formatv_object_base(StringRef Fmt,
+  formatv_object_base(StringRef Fmt, bool Validate,
   ArrayRef Adapters)
-  : Fmt(Fmt), Adapters(Adapters) {}
+  : Fmt(Fmt), Validate(Validate), Adapters(Adapters) {}
 
   formatv_object_base(formatv_object_base const &rhs) = delete;
   formatv_object_base(formatv_object_base &&rhs) = default;
 
 public:
   void format(raw_ostream &S) const {
-for (auto &R : parseFormatString(Fmt)) {
+const auto [Replacements, IsValid] =
+parseFormatString(S, Fmt, Adapters.size(), Validate);
+if (!IsValid)
+  return;
+
+for (const auto &R : Replacements) {
   if (R.Type == ReplacementType::Empty)
 continue;
  

[clang] [lldb] [llvm] [mlir] [Support] Validate number of arguments passed to formatv() (PR #105745)

2024-08-27 Thread Rahul Joshi via cfe-commits

https://github.com/jurahul ready_for_review 
https://github.com/llvm/llvm-project/pull/105745
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [lldb] [llvm] [mlir] [Support] Validate number of arguments passed to formatv() (PR #105745)

2024-08-27 Thread Rahul Joshi via cfe-commits

jurahul wrote:

Hi all, this is related to 
https://discourse.llvm.org/t/adding-argument-count-validation-for-formatv/80876/1

I still have formatv() and formatvv() functions in the code, but only a handful 
instances. So, if this looks ok overall, I will go ahead and rename formatvv() 
to formatv_unchecked() (or if there are other better suggestions for that 
version). Also, let me know if it would make sense to split this into 2 
changes, one NFC one that fixes formatv() current uses that are invalid, and 
then the next one that adds validation and changes the handful few that need to 
be to formatvv().

https://github.com/llvm/llvm-project/pull/105745
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [lldb] [llvm] [mlir] [Support] Validate number of arguments passed to formatv() (PR #105745)

2024-08-27 Thread Rahul Joshi via cfe-commits

jurahul wrote:

> CI failed FYI.

Yeah, it looked like an unrelated failure. Windows CI passed.

https://github.com/llvm/llvm-project/pull/105745
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [lldb] [llvm] [mlir] [Support] Validate number of arguments passed to formatv() (PR #105745)

2024-08-27 Thread Rahul Joshi via cfe-commits


@@ -67,92 +68,123 @@ formatv_object_base::parseReplacementItem(StringRef Spec) {
   StringRef Options;
   size_t Index = 0;
   RepString = RepString.trim();
-  if (RepString.consumeInteger(0, Index)) {
-assert(false && "Invalid replacement sequence index!");
-return ReplacementItem{};
-  }
+  if (RepString.consumeInteger(0, Index))
+return "Invalid replacement sequence index!";
   RepString = RepString.trim();
   if (RepString.consume_front(",")) {
 if (!consumeFieldLayout(RepString, Where, Align, Pad))
-  assert(false && "Invalid replacement field layout specification!");
+  return "Invalid replacement field layout specification!";
   }
   RepString = RepString.trim();
   if (RepString.consume_front(":")) {
 Options = RepString.trim();
 RepString = StringRef();
   }
   RepString = RepString.trim();
-  if (!RepString.empty()) {
-assert(false && "Unexpected characters found in replacement string!");
-  }
-
+  if (!RepString.empty())
+return "Unexpected character found in replacement string!";
   return ReplacementItem{Spec, Index, Align, Where, Pad, Options};
 }
 
-std::pair
-formatv_object_base::splitLiteralAndReplacement(StringRef Fmt) {
-  while (!Fmt.empty()) {
-// Everything up until the first brace is a literal.
-if (Fmt.front() != '{') {
-  std::size_t BO = Fmt.find_first_of('{');
-  return std::make_pair(ReplacementItem{Fmt.substr(0, BO)}, 
Fmt.substr(BO));
-}
-
-StringRef Braces = Fmt.take_while([](char C) { return C == '{'; });
-// If there is more than one brace, then some of them are escaped.  Treat
-// these as replacements.
-if (Braces.size() > 1) {
-  size_t NumEscapedBraces = Braces.size() / 2;
-  StringRef Middle = Fmt.take_front(NumEscapedBraces);
-  StringRef Right = Fmt.drop_front(NumEscapedBraces * 2);
-  return std::make_pair(ReplacementItem{Middle}, Right);
-}
-// An unterminated open brace is undefined. Assert to indicate that this is
-// undefined and that we consider it an error. When asserts are disabled,
-// build a replacement item with an error message.
-std::size_t BC = Fmt.find_first_of('}');
-if (BC == StringRef::npos) {
-  assert(
-  false &&
-  "Unterminated brace sequence. Escape with {{ for a literal brace.");
-  return std::make_pair(
-  ReplacementItem{"Unterminated brace sequence. Escape with {{ for a "
-  "literal brace."},
-  StringRef());
-}
-
-// Even if there is a closing brace, if there is another open brace before
-// this closing brace, treat this portion as literal, and try again with 
the
-// next one.
-std::size_t BO2 = Fmt.find_first_of('{', 1);
-if (BO2 < BC)
-  return std::make_pair(ReplacementItem{Fmt.substr(0, BO2)},
-Fmt.substr(BO2));
-
-StringRef Spec = Fmt.slice(1, BC);
-StringRef Right = Fmt.substr(BC + 1);
-
-auto RI = parseReplacementItem(Spec);
-if (RI)
-  return std::make_pair(*RI, Right);
+static std::variant, StringRef>
+splitLiteralAndReplacement(StringRef Fmt) {
+  // Everything up until the first brace is a literal.
+  if (Fmt.front() != '{') {
+std::size_t BO = Fmt.find_first_of('{');
+return std::make_pair(ReplacementItem(Fmt.substr(0, BO)), Fmt.substr(BO));
+  }
 
-// If there was an error parsing the replacement item, treat it as an
-// invalid replacement spec, and just continue.
-Fmt = Fmt.drop_front(BC + 1);
+  StringRef Braces = Fmt.take_while([](char C) { return C == '{'; });
+  // If there is more than one brace, then some of them are escaped.  Treat
+  // these as replacements.
+  if (Braces.size() > 1) {
+size_t NumEscapedBraces = Braces.size() / 2;
+StringRef Middle = Fmt.take_front(NumEscapedBraces);
+StringRef Right = Fmt.drop_front(NumEscapedBraces * 2);
+return std::make_pair(ReplacementItem(Middle), Right);
   }
-  return std::make_pair(ReplacementItem{Fmt}, StringRef());
+  // An unterminated open brace is undefined. Assert to indicate that this is
+  // undefined and that we consider it an error. When asserts are disabled,
+  // build a replacement item with an error message.
+  std::size_t BC = Fmt.find_first_of('}');
+  if (BC == StringRef::npos)
+return "Unterminated brace sequence. Escape with {{ for a literal brace.";
+
+  // Even if there is a closing brace, if there is another open brace before
+  // this closing brace, treat this portion as literal, and try again with the
+  // next one.
+  std::size_t BO2 = Fmt.find_first_of('{', 1);
+  if (BO2 < BC)
+return std::make_pair(ReplacementItem{Fmt.substr(0, BO2)}, 
Fmt.substr(BO2));
+
+  StringRef Spec = Fmt.slice(1, BC);
+  StringRef Right = Fmt.substr(BC + 1);
+
+  auto RI = parseReplacementItem(Spec);
+  if (const StringRef *ErrMsg = std::get_if<1>(&RI))
+return *ErrMsg;
+
+  return std::make_pair(std::get<0>(RI), Right);
 }
 
-SmallVector
-format

[clang] [lldb] [llvm] [mlir] [Support] Validate number of arguments passed to formatv() (PR #105745)

2024-08-27 Thread Rahul Joshi via cfe-commits

jurahul wrote:

> compile-time tests with clang for example

I can check. How do I run them?

https://github.com/llvm/llvm-project/pull/105745
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


  1   2   3   4   5   >