1 //===- CTagsEmitter.cpp - Generate ctags-compatible index ------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This tablegen backend emits an index of definitions in ctags(1) format.
10 // A helper script, utils/TableGen/tdtags, provides an easier-to-use
11 // interface; run 'tdtags -H' for documentation.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Support/SourceMgr.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 #include "llvm/TableGen/Error.h"
18 #include "llvm/TableGen/Record.h"
24 #define DEBUG_TYPE "ctags-emitter"
30 const std::string *Id;
33 Tag(const std::string &Name, const SMLoc Location)
34 : Id(&Name), Loc(Location) {}
35 int operator<(const Tag &B) const { return *Id < *B.Id; }
36 void emit(raw_ostream &OS) const {
37 const MemoryBuffer *CurMB =
38 SrcMgr.getMemoryBuffer(SrcMgr.FindBufferContainingLoc(Loc));
39 auto BufferName = CurMB->getBufferIdentifier();
40 std::pair<unsigned, unsigned> LineAndColumn = SrcMgr.getLineAndColumn(Loc);
41 OS << *Id << "\t" << BufferName << "\t" << LineAndColumn.first << "\n";
47 RecordKeeper &Records;
49 CTagsEmitter(RecordKeeper &R) : Records(R) {}
51 void run(raw_ostream &OS);
54 static SMLoc locate(const Record *R);
57 } // End anonymous namespace.
59 SMLoc CTagsEmitter::locate(const Record *R) {
60 ArrayRef<SMLoc> Locs = R->getLoc();
61 return !Locs.empty() ? Locs.front() : SMLoc();
64 void CTagsEmitter::run(raw_ostream &OS) {
65 const auto &Classes = Records.getClasses();
66 const auto &Defs = Records.getDefs();
67 std::vector<Tag> Tags;
69 Tags.reserve(Classes.size() + Defs.size());
70 for (const auto &C : Classes)
71 Tags.push_back(Tag(C.first, locate(C.second.get())));
72 for (const auto &D : Defs)
73 Tags.push_back(Tag(D.first, locate(D.second.get())));
76 OS << "!_TAG_FILE_FORMAT\t1\t/original ctags format/\n";
77 OS << "!_TAG_FILE_SORTED\t1\t/0=unsorted, 1=sorted, 2=foldcase/\n";
78 for (const Tag &T : Tags)
84 void EmitCTags(RecordKeeper &RK, raw_ostream &OS) { CTagsEmitter(RK).run(OS); }
86 } // End llvm namespace.