1 //===- CTagsEmitter.cpp - Generate ctags-compatible index ------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This tablegen backend emits an index of definitions in ctags(1) format. 10 // A helper script, utils/TableGen/tdtags, provides an easier-to-use 11 // interface; run 'tdtags -H' for documentation. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/Support/SourceMgr.h" 16 #include "llvm/Support/MemoryBuffer.h" 17 #include "llvm/TableGen/Error.h" 18 #include "llvm/TableGen/Record.h" 19 #include <algorithm> 20 #include <string> 21 #include <vector> 22 using namespace llvm; 23 24 #define DEBUG_TYPE "ctags-emitter" 25 26 namespace { 27 28 class Tag { 29 private: 30 StringRef Id; 31 StringRef BufferIdentifier; 32 unsigned Line; 33 public: 34 Tag(StringRef Name, const SMLoc Location) : Id(Name) { 35 const MemoryBuffer *CurMB = 36 SrcMgr.getMemoryBuffer(SrcMgr.FindBufferContainingLoc(Location)); 37 BufferIdentifier = CurMB->getBufferIdentifier(); 38 auto LineAndColumn = SrcMgr.getLineAndColumn(Location); 39 Line = LineAndColumn.first; 40 } 41 int operator<(const Tag &B) const { 42 return std::make_tuple(Id, BufferIdentifier, Line) < std::make_tuple(B.Id, B.BufferIdentifier, B.Line); 43 } 44 void emit(raw_ostream &OS) const { 45 OS << Id << "\t" << BufferIdentifier << "\t" << Line << "\n"; 46 } 47 }; 48 49 class CTagsEmitter { 50 private: 51 RecordKeeper &Records; 52 public: 53 CTagsEmitter(RecordKeeper &R) : Records(R) {} 54 55 void run(raw_ostream &OS); 56 57 private: 58 static SMLoc locate(const Record *R); 59 }; 60 61 } // End anonymous namespace. 62 63 SMLoc CTagsEmitter::locate(const Record *R) { 64 ArrayRef<SMLoc> Locs = R->getLoc(); 65 return !Locs.empty() ? Locs.front() : SMLoc(); 66 } 67 68 void CTagsEmitter::run(raw_ostream &OS) { 69 const auto &Classes = Records.getClasses(); 70 const auto &Defs = Records.getDefs(); 71 std::vector<Tag> Tags; 72 // Collect tags. 73 Tags.reserve(Classes.size() + Defs.size()); 74 for (const auto &C : Classes) { 75 Tags.push_back(Tag(C.first, locate(C.second.get()))); 76 for (SMLoc FwdLoc : C.second->getForwardDeclarationLocs()) 77 Tags.push_back(Tag(C.first, FwdLoc)); 78 } 79 for (const auto &D : Defs) 80 Tags.push_back(Tag(D.first, locate(D.second.get()))); 81 // Emit tags. 82 llvm::sort(Tags); 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 (const Tag &T : Tags) 86 T.emit(OS); 87 } 88 89 namespace llvm { 90 91 void EmitCTags(RecordKeeper &RK, raw_ostream &OS) { CTagsEmitter(RK).run(OS); } 92 93 } // End llvm namespace. 94