CTagsEmitter.cpp revision 280031
1//===- CTagsEmitter.cpp - Generate ctags-compatible index ------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 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. 13// 14//===----------------------------------------------------------------------===// 15 16#include "llvm/Support/SourceMgr.h" 17#include "llvm/Support/MemoryBuffer.h" 18#include "llvm/TableGen/Error.h" 19#include "llvm/TableGen/Record.h" 20#include <algorithm> 21#include <string> 22#include <vector> 23using namespace llvm; 24 25#define DEBUG_TYPE "ctags-emitter" 26 27namespace llvm { extern SourceMgr SrcMgr; } 28 29namespace { 30 31class Tag { 32private: 33 const std::string *Id; 34 SMLoc Loc; 35public: 36 Tag(const std::string &Name, const SMLoc Location) 37 : Id(&Name), Loc(Location) {} 38 int operator<(const Tag &B) const { return *Id < *B.Id; } 39 void emit(raw_ostream &OS) const { 40 const MemoryBuffer *CurMB = 41 SrcMgr.getMemoryBuffer(SrcMgr.FindBufferContainingLoc(Loc)); 42 const char *BufferName = CurMB->getBufferIdentifier(); 43 std::pair<unsigned, unsigned> LineAndColumn = SrcMgr.getLineAndColumn(Loc); 44 OS << *Id << "\t" << BufferName << "\t" << LineAndColumn.first << "\n"; 45 } 46}; 47 48class CTagsEmitter { 49private: 50 RecordKeeper &Records; 51public: 52 CTagsEmitter(RecordKeeper &R) : Records(R) {} 53 54 void run(raw_ostream &OS); 55 56private: 57 static SMLoc locate(const Record *R); 58}; 59 60} // End anonymous namespace. 61 62SMLoc CTagsEmitter::locate(const Record *R) { 63 ArrayRef<SMLoc> Locs = R->getLoc(); 64 if (Locs.empty()) { 65 SMLoc NullLoc; 66 return NullLoc; 67 } 68 return Locs.front(); 69} 70 71void CTagsEmitter::run(raw_ostream &OS) { 72 const auto &Classes = Records.getClasses(); 73 const auto &Defs = Records.getDefs(); 74 std::vector<Tag> Tags; 75 // Collect tags. 76 Tags.reserve(Classes.size() + Defs.size()); 77 for (const auto &C : Classes) 78 Tags.push_back(Tag(C.first, locate(C.second.get()))); 79 for (const auto &D : Defs) 80 Tags.push_back(Tag(D.first, locate(D.second.get()))); 81 // Emit tags. 82 std::sort(Tags.begin(), Tags.end()); 83 OS << "!_TAG_FILE_FORMAT\t1\t/original ctags format/\n"; 84 OS << "!_TAG_FILE_SORTED\t1\t/0=unsorted, 1=sorted, 2=foldcase/\n"; 85 for (std::vector<Tag>::const_iterator I = Tags.begin(), E = Tags.end(); 86 I != E; ++I) 87 I->emit(OS); 88} 89 90namespace llvm { 91 92void EmitCTags(RecordKeeper &RK, raw_ostream &OS) { CTagsEmitter(RK).run(OS); } 93 94} // End llvm namespace. 95