[PATCH] D129935: [TableGen] Add a location for a class definition that was forward-declared
Roman Rusyaev via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sat Jul 16 11:17:15 PDT 2022
rusyaev-roman created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
rusyaev-roman requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
This change makes ctags generation for tablegen files more carefully.
For the following example
class A;
class A {
int a;
}
tags were generated only for a forward declaration of class 'A'.
This patch allows generating tags for the forward declaration
and further definition of class 'A'.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D129935
Files:
llvm/lib/TableGen/TGParser.cpp
llvm/test/TableGen/GenTags.td
llvm/utils/TableGen/CTagsEmitter.cpp
Index: llvm/utils/TableGen/CTagsEmitter.cpp
===================================================================
--- llvm/utils/TableGen/CTagsEmitter.cpp
+++ llvm/utils/TableGen/CTagsEmitter.cpp
@@ -28,17 +28,20 @@
class Tag {
private:
const std::string *Id;
- SMLoc Loc;
+ ArrayRef<SMLoc> Locs;
+
public:
- Tag(const std::string &Name, const SMLoc Location)
- : Id(&Name), Loc(Location) {}
+ Tag(const std::string &Name, ArrayRef<SMLoc> Locs) : Id(&Name), Locs(Locs) {}
int operator<(const Tag &B) const { return *Id < *B.Id; }
void emit(raw_ostream &OS) const {
- const MemoryBuffer *CurMB =
- SrcMgr.getMemoryBuffer(SrcMgr.FindBufferContainingLoc(Loc));
- auto BufferName = CurMB->getBufferIdentifier();
- std::pair<unsigned, unsigned> LineAndColumn = SrcMgr.getLineAndColumn(Loc);
- OS << *Id << "\t" << BufferName << "\t" << LineAndColumn.first << "\n";
+ for (auto Loc : Locs) {
+ const MemoryBuffer *CurMB =
+ SrcMgr.getMemoryBuffer(SrcMgr.FindBufferContainingLoc(Loc));
+ auto BufferName = CurMB->getBufferIdentifier();
+ std::pair<unsigned, unsigned> LineAndColumn =
+ SrcMgr.getLineAndColumn(Loc);
+ OS << *Id << "\t" << BufferName << "\t" << LineAndColumn.first << "\n";
+ }
}
};
@@ -51,16 +54,10 @@
void run(raw_ostream &OS);
private:
- static SMLoc locate(const Record *R);
};
} // End anonymous namespace.
-SMLoc CTagsEmitter::locate(const Record *R) {
- ArrayRef<SMLoc> Locs = R->getLoc();
- return !Locs.empty() ? Locs.front() : SMLoc();
-}
-
void CTagsEmitter::run(raw_ostream &OS) {
const auto &Classes = Records.getClasses();
const auto &Defs = Records.getDefs();
@@ -68,9 +65,9 @@
// Collect tags.
Tags.reserve(Classes.size() + Defs.size());
for (const auto &C : Classes)
- Tags.push_back(Tag(C.first, locate(C.second.get())));
+ Tags.push_back(Tag(C.first, C.second->getLoc()));
for (const auto &D : Defs)
- Tags.push_back(Tag(D.first, locate(D.second.get())));
+ Tags.push_back(Tag(D.first, D.second->getLoc()));
// Emit tags.
llvm::sort(Tags);
OS << "!_TAG_FILE_FORMAT\t1\t/original ctags format/\n";
Index: llvm/test/TableGen/GenTags.td
===================================================================
--- /dev/null
+++ llvm/test/TableGen/GenTags.td
@@ -0,0 +1,9 @@
+// RUN: llvm-tblgen --gen-ctags %s | FileCheck %s -DFILE=%s
+
+// CHECK: A [[FILE]] [[@LINE+1]]
+class A;
+
+// CHECK: A [[FILE]] [[@LINE+1]]
+class A {
+ string name = "A";
+}
Index: llvm/lib/TableGen/TGParser.cpp
===================================================================
--- llvm/lib/TableGen/TGParser.cpp
+++ llvm/lib/TableGen/TGParser.cpp
@@ -3391,6 +3391,9 @@
!CurRec->getTemplateArgs().empty())
return TokError("Class '" + CurRec->getNameInitAsString() +
"' already defined");
+
+ // Add a location for the class definition.
+ CurRec->appendLoc(Lex.getLoc());
} else {
// If this is the first reference to this class, create and add it.
auto NewRec =
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129935.445244.patch
Type: text/x-patch
Size: 3064 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220716/32117989/attachment.bin>
More information about the llvm-commits
mailing list