1 //===- CTagsEmitter.cpp - Generate ctags-compatible index ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tablegen backend emits an index of definitions in ctags(1) format.
11 // A helper script, utils/TableGen/tdtags, provides an easier-to-use
12 // interface; run 'tdtags -H' for documentation.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Support/SourceMgr.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/TableGen/Error.h"
19 #include "llvm/TableGen/Record.h"
25 #define DEBUG_TYPE "ctags-emitter"
31 const std::string *Id;
34 Tag(const std::string &Name, const SMLoc Location)
35 : Id(&Name), Loc(Location) {}
36 int operator<(const Tag &B) const { return *Id < *B.Id; }
37 void emit(raw_ostream &OS) const {
38 const MemoryBuffer *CurMB =
39 SrcMgr.getMemoryBuffer(SrcMgr.FindBufferContainingLoc(Loc));
40 const char *BufferName = CurMB->getBufferIdentifier();
41 std::pair<unsigned, unsigned> LineAndColumn = SrcMgr.getLineAndColumn(Loc);
42 OS << *Id << "\t" << BufferName << "\t" << LineAndColumn.first << "\n";
48 RecordKeeper &Records;
50 CTagsEmitter(RecordKeeper &R) : Records(R) {}
52 void run(raw_ostream &OS);
55 static SMLoc locate(const Record *R);
58 } // End anonymous namespace.
60 SMLoc CTagsEmitter::locate(const Record *R) {
61 ArrayRef<SMLoc> Locs = R->getLoc();
69 void CTagsEmitter::run(raw_ostream &OS) {
70 const auto &Classes = Records.getClasses();
71 const auto &Defs = Records.getDefs();
72 std::vector<Tag> Tags;
74 Tags.reserve(Classes.size() + Defs.size());
75 for (const auto &C : Classes)
76 Tags.push_back(Tag(C.first, locate(C.second.get())));
77 for (const auto &D : Defs)
78 Tags.push_back(Tag(D.first, locate(D.second.get())));
80 std::sort(Tags.begin(), Tags.end());
81 OS << "!_TAG_FILE_FORMAT\t1\t/original ctags format/\n";
82 OS << "!_TAG_FILE_SORTED\t1\t/0=unsorted, 1=sorted, 2=foldcase/\n";
83 for (std::vector<Tag>::const_iterator I = Tags.begin(), E = Tags.end();
90 void EmitCTags(RecordKeeper &RK, raw_ostream &OS) { CTagsEmitter(RK).run(OS); }
92 } // End llvm namespace.