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 #define DEBUG_TYPE "ctags-emitter"
18 #include "llvm/Support/SourceMgr.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/TableGen/Error.h"
21 #include "llvm/TableGen/Record.h"
22 #include "llvm/TableGen/TableGenBackend.h"
28 namespace llvm { extern SourceMgr SrcMgr; }
34 const std::string *Id;
37 Tag(const std::string &Name, const SMLoc Location)
38 : Id(&Name), Loc(Location) {}
39 int operator<(const Tag &B) const { return *Id < *B.Id; }
40 void emit(raw_ostream &OS) const {
41 int BufferID = SrcMgr.FindBufferContainingLoc(Loc);
42 MemoryBuffer *CurMB = SrcMgr.getBufferInfo(BufferID).Buffer;
43 const char *BufferName = CurMB->getBufferIdentifier();
44 std::pair<unsigned, unsigned> LineAndColumn = SrcMgr.getLineAndColumn(Loc);
45 OS << *Id << "\t" << BufferName << "\t" << LineAndColumn.first << "\n";
51 RecordKeeper &Records;
53 CTagsEmitter(RecordKeeper &R) : Records(R) {}
55 void run(raw_ostream &OS);
58 static SMLoc locate(const Record *R);
61 } // End anonymous namespace.
63 SMLoc CTagsEmitter::locate(const Record *R) {
64 ArrayRef<SMLoc> Locs = R->getLoc();
72 void CTagsEmitter::run(raw_ostream &OS) {
73 const std::map<std::string, Record *> &Classes = Records.getClasses();
74 const std::map<std::string, Record *> &Defs = Records.getDefs();
75 std::vector<Tag> Tags;
77 Tags.reserve(Classes.size() + Defs.size());
78 for (std::map<std::string, Record *>::const_iterator I = Classes.begin(),
81 Tags.push_back(Tag(I->first, locate(I->second)));
82 for (std::map<std::string, Record *>::const_iterator I = Defs.begin(),
85 Tags.push_back(Tag(I->first, locate(I->second)));
87 std::sort(Tags.begin(), Tags.end());
88 OS << "!_TAG_FILE_FORMAT\t1\t/original ctags format/\n";
89 OS << "!_TAG_FILE_SORTED\t1\t/0=unsorted, 1=sorted, 2=foldcase/\n";
90 for (std::vector<Tag>::const_iterator I = Tags.begin(), E = Tags.end();
97 void EmitCTags(RecordKeeper &RK, raw_ostream &OS) { CTagsEmitter(RK).run(OS); }
99 } // End llvm namespace.