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#define DEBUG_TYPE "ctags-emitter" 17 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" 23#include <algorithm> 24#include <string> 25#include <vector> 26using namespace llvm; 27 28namespace llvm { extern SourceMgr SrcMgr; } 29 30namespace { 31 32class Tag { 33private: 34 const std::string *Id; 35 SMLoc Loc; 36public: 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"; 46 } 47}; 48 49class CTagsEmitter { 50private: 51 RecordKeeper &Records; 52public: 53 CTagsEmitter(RecordKeeper &R) : Records(R) {} 54 55 void run(raw_ostream &OS); 56 57private: 58 static SMLoc locate(const Record *R); 59}; 60 61} // End anonymous namespace. 62 63SMLoc CTagsEmitter::locate(const Record *R) { 64 ArrayRef<SMLoc> Locs = R->getLoc(); 65 if (Locs.empty()) { 66 SMLoc NullLoc; 67 return NullLoc; 68 } 69 return Locs.front(); 70} 71 72void 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; 76 // Collect tags. 77 Tags.reserve(Classes.size() + Defs.size()); 78 for (std::map<std::string, Record *>::const_iterator I = Classes.begin(), 79 E = Classes.end(); 80 I != E; ++I) 81 Tags.push_back(Tag(I->first, locate(I->second))); 82 for (std::map<std::string, Record *>::const_iterator I = Defs.begin(), 83 E = Defs.end(); 84 I != E; ++I) 85 Tags.push_back(Tag(I->first, locate(I->second))); 86 // Emit tags. 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(); 91 I != E; ++I) 92 I->emit(OS); 93} 94 95namespace llvm { 96 97void EmitCTags(RecordKeeper &RK, raw_ostream &OS) { CTagsEmitter(RK).run(OS); } 98 99} // End llvm namespace. 100