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> 23 using namespace llvm; 24 25 #define DEBUG_TYPE "ctags-emitter" 26 27 namespace llvm { extern SourceMgr SrcMgr; } 28 29 namespace { 30 31 class Tag { 32 private: 33 const std::string *Id; 34 SMLoc Loc; 35 public: 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 48 class CTagsEmitter { 49 private: 50 RecordKeeper &Records; 51 public: 52 CTagsEmitter(RecordKeeper &R) : Records(R) {} 53 54 void run(raw_ostream &OS); 55 56 private: 57 static SMLoc locate(const Record *R); 58 }; 59 60 } // End anonymous namespace. 61 62 SMLoc 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 71 void CTagsEmitter::run(raw_ostream &OS) { 72 const std::map<std::string, Record *> &Classes = Records.getClasses(); 73 const std::map<std::string, Record *> &Defs = Records.getDefs(); 74 std::vector<Tag> Tags; 75 // Collect tags. 76 Tags.reserve(Classes.size() + Defs.size()); 77 for (std::map<std::string, Record *>::const_iterator I = Classes.begin(), 78 E = Classes.end(); 79 I != E; ++I) 80 Tags.push_back(Tag(I->first, locate(I->second))); 81 for (std::map<std::string, Record *>::const_iterator I = Defs.begin(), 82 E = Defs.end(); 83 I != E; ++I) 84 Tags.push_back(Tag(I->first, locate(I->second))); 85 // Emit tags. 86 std::sort(Tags.begin(), Tags.end()); 87 OS << "!_TAG_FILE_FORMAT\t1\t/original ctags format/\n"; 88 OS << "!_TAG_FILE_SORTED\t1\t/0=unsorted, 1=sorted, 2=foldcase/\n"; 89 for (std::vector<Tag>::const_iterator I = Tags.begin(), E = Tags.end(); 90 I != E; ++I) 91 I->emit(OS); 92 } 93 94 namespace llvm { 95 96 void EmitCTags(RecordKeeper &RK, raw_ostream &OS) { CTagsEmitter(RK).run(OS); } 97 98 } // End llvm namespace. 99