gn build: Merge r372267
[llvm-complete.git] / utils / TableGen / CTagsEmitter.cpp
blobccb7f3300dde4b86bd88cc633db588b513ea097a
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.
13 //===----------------------------------------------------------------------===//
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;
24 #define DEBUG_TYPE "ctags-emitter"
26 namespace {
28 class Tag {
29 private:
30 const std::string *Id;
31 SMLoc Loc;
32 public:
33 Tag(const std::string &Name, const SMLoc Location)
34 : Id(&Name), Loc(Location) {}
35 int operator<(const Tag &B) const { return *Id < *B.Id; }
36 void emit(raw_ostream &OS) const {
37 const MemoryBuffer *CurMB =
38 SrcMgr.getMemoryBuffer(SrcMgr.FindBufferContainingLoc(Loc));
39 auto BufferName = CurMB->getBufferIdentifier();
40 std::pair<unsigned, unsigned> LineAndColumn = SrcMgr.getLineAndColumn(Loc);
41 OS << *Id << "\t" << BufferName << "\t" << LineAndColumn.first << "\n";
45 class CTagsEmitter {
46 private:
47 RecordKeeper &Records;
48 public:
49 CTagsEmitter(RecordKeeper &R) : Records(R) {}
51 void run(raw_ostream &OS);
53 private:
54 static SMLoc locate(const Record *R);
57 } // End anonymous namespace.
59 SMLoc CTagsEmitter::locate(const Record *R) {
60 ArrayRef<SMLoc> Locs = R->getLoc();
61 return !Locs.empty() ? Locs.front() : SMLoc();
64 void CTagsEmitter::run(raw_ostream &OS) {
65 const auto &Classes = Records.getClasses();
66 const auto &Defs = Records.getDefs();
67 std::vector<Tag> Tags;
68 // Collect tags.
69 Tags.reserve(Classes.size() + Defs.size());
70 for (const auto &C : Classes)
71 Tags.push_back(Tag(C.first, locate(C.second.get())));
72 for (const auto &D : Defs)
73 Tags.push_back(Tag(D.first, locate(D.second.get())));
74 // Emit tags.
75 llvm::sort(Tags);
76 OS << "!_TAG_FILE_FORMAT\t1\t/original ctags format/\n";
77 OS << "!_TAG_FILE_SORTED\t1\t/0=unsorted, 1=sorted, 2=foldcase/\n";
78 for (const Tag &T : Tags)
79 T.emit(OS);
82 namespace llvm {
84 void EmitCTags(RecordKeeper &RK, raw_ostream &OS) { CTagsEmitter(RK).run(OS); }
86 } // End llvm namespace.