1/****************************************************************************
2 * Copyright (C) 2012-2016 Woboq GmbH
3 * Olivier Goffart <contact at woboq.com>
4 * https://woboq.com/codebrowser.html
5 *
6 * This file is part of the Woboq Code Browser.
7 *
8 * Commercial License Usage:
9 * Licensees holding valid commercial licenses provided by Woboq may use
10 * this file in accordance with the terms contained in a written agreement
11 * between the licensee and Woboq.
12 * For further information see https://woboq.com/codebrowser.html
13 *
14 * Alternatively, this work may be used under a Creative Commons
15 * Attribution-NonCommercial-ShareAlike 3.0 (CC-BY-NC-SA 3.0) License.
16 * http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US
17 * This license does not allow you to use the code browser to assist the
18 * development of your commercial software. If you intent to do so, consider
19 * purchasing a commercial licence.
20 ****************************************************************************/
21
22#pragma once
23
24#include <string>
25#include <set>
26#include <map>
27#include <vector>
28#include <unordered_set>
29#include <llvm/ADT/SmallString.h>
30
31namespace llvm {
32class raw_ostream;
33}
34
35
36/* This class generate the HTML out of a file with the said tags.
37 */
38class Generator {
39
40 struct Tag {
41 std::string name;
42 std::string attributes;
43 int pos;
44 int len;
45 bool operator<(const Tag &other) const {
46 //This is the order of the opening tag. Order first by position, then by length
47 // (in the reverse order) with the exception of length of 0 which always goes first.
48 // Ordered first by position, and then by lenth (reverse order)
49 return (pos != other.pos) ? pos < other.pos
50 : len == 0 || (other.len != 0 && len > other.len);
51 }
52 bool operator==(const Tag &other) const {
53 return std::tie(pos, len, name, attributes) ==
54 std::tie(other.pos, other.len, other.name, other.attributes);
55 }
56 void open(llvm::raw_ostream& myfile) const;
57 void close(llvm::raw_ostream& myfile) const;
58 };
59
60 std::multiset<Tag> tags;
61
62 std::map<std::string, std::string> projects;
63
64public:
65
66 void addTag(std::string name, std::string attributes, int pos, int len) {
67 if (len < 0) {
68 return;
69 }
70 Tag t = {std::move(name), std::move(attributes), pos, len};
71 auto it = tags.find(t);
72 if (it != tags.end() && *it == t) return; //Hapens in macro for example
73 tags.insert(std::move(t));
74 }
75 void addProject(std::string a, std::string b) {
76 projects.insert({std::move(a), std::move(b) });
77 }
78
79 void generate(llvm::StringRef outputPrefix, std::string dataPath, const std::string &filename,
80 const char* begin, const char* end, llvm::StringRef footer, llvm::StringRef warningMessage,
81 const std::set<std::string> &interestingDefitions);
82
83 static llvm::StringRef escapeAttr(llvm::StringRef, llvm::SmallVectorImpl<char> &buffer);
84
85 /**
86 * This function takes a reference name that was already processed by `escapeAttr` and
87 * replaces additional chars which are not allowed in filenames on all platforms.
88 *
89 * @param s already escaped ref
90 * @param buffer
91 * @return
92 */
93 static llvm::StringRef escapeAttrForFilename(llvm::StringRef s, llvm::SmallVectorImpl< char >& buffer);
94 static void escapeAttr(llvm::raw_ostream& os, llvm::StringRef s);
95
96 struct EscapeAttr { llvm::StringRef value; };
97};
98
99inline llvm::raw_ostream& operator<<(llvm::raw_ostream& os, Generator::EscapeAttr s)
100{ Generator::escapeAttr(os, s.value); return os; }
101
102