clang-tools 23.0.0git
Utils.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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/// \file
10/// This file contains the implementations of general utility functions used
11/// across the clang-doc tool.
12///
13//===----------------------------------------------------------------------===//
14
15#include "Utils.h"
16#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/Support/FileSystem.h"
19#include "llvm/Support/Path.h"
20
21using namespace llvm;
22
23SmallString<128> appendPathNative(StringRef Base, StringRef Path) {
24 SmallString<128> Default;
25 sys::path::native(Base, Default);
26 sys::path::append(Default, Path);
27 return Default;
28}
29
30SmallString<128> appendPathPosix(StringRef Base, StringRef Path) {
31 SmallString<128> Default;
32 sys::path::native(Base, Default, sys::path::Style::posix);
33 sys::path::append(Default, Path);
34 return Default;
35}
36
37void getHtmlFiles(StringRef AssetsPath, clang::doc::ClangDocContext &CDCtx) {
38 assert(!AssetsPath.empty());
39 assert(sys::fs::is_directory(AssetsPath));
40
41 // TODO: Allow users to override default templates with their own. We would
42 // similarly have to check if a template file already exists in CDCtx.
43 if (CDCtx.UserStylesheets.empty()) {
44 SmallString<128> DefaultStylesheet =
45 appendPathPosix(AssetsPath, "clang-doc-mustache.css");
46 CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
47 DefaultStylesheet.c_str());
48 }
49
50 if (CDCtx.JsScripts.empty()) {
51 SmallString<128> IndexJS = appendPathPosix(AssetsPath, "mustache-index.js");
52 CDCtx.JsScripts.insert(CDCtx.JsScripts.begin(), IndexJS.c_str());
53 }
54
55 SmallString<128> NamespaceTemplate =
56 appendPathPosix(AssetsPath, "namespace-template.mustache");
57 SmallString<128> ClassTemplate =
58 appendPathPosix(AssetsPath, "class-template.mustache");
59 SmallString<128> EnumTemplate =
60 appendPathPosix(AssetsPath, "enum-template.mustache");
61 SmallString<128> FunctionTemplate =
62 appendPathPosix(AssetsPath, "function-template.mustache");
63 SmallString<128> CommentTemplate =
64 appendPathPosix(AssetsPath, "comment-template.mustache");
65 SmallString<128> HeadTemplate =
66 appendPathPosix(AssetsPath, "head-template.mustache");
67 SmallString<128> NavbarTemplate =
68 appendPathPosix(AssetsPath, "navbar-template.mustache");
69 SmallString<128> IndexTemplate =
70 appendPathPosix(AssetsPath, "index-template.mustache");
71 SmallString<128> AliasTemplate =
72 appendPathPosix(AssetsPath, "alias-template.mustache");
73
74 CDCtx.MustacheTemplates.insert(
75 {"namespace-template", NamespaceTemplate.c_str()});
76 CDCtx.MustacheTemplates.insert({"class-template", ClassTemplate.c_str()});
77 CDCtx.MustacheTemplates.insert({"enum-template", EnumTemplate.c_str()});
78 CDCtx.MustacheTemplates.insert(
79 {"function-template", FunctionTemplate.c_str()});
80 CDCtx.MustacheTemplates.insert({"comment-template", CommentTemplate.c_str()});
81 CDCtx.MustacheTemplates.insert({"head-template", HeadTemplate.c_str()});
82 CDCtx.MustacheTemplates.insert({"navbar-template", NavbarTemplate.c_str()});
83 CDCtx.MustacheTemplates.insert({"index-template", IndexTemplate.c_str()});
84 CDCtx.MustacheTemplates.insert({"alias-template", AliasTemplate.c_str()});
85}
86
87void getMdFiles(llvm::StringRef AssetsPath,
89 assert(!AssetsPath.empty());
90 assert(sys::fs::is_directory(AssetsPath));
91
92 SmallString<128> ClassTemplate =
93 appendPathPosix(AssetsPath, "class-template.mustache");
94 SmallString<128> NamespaceTemplate =
95 appendPathPosix(AssetsPath, "namespace-template.mustache");
96 SmallString<128> AllFilesTemplate =
97 appendPathPosix(AssetsPath, "all-files-template.mustache");
98 SmallString<128> IndexTemplate =
99 appendPathPosix(AssetsPath, "index-template.mustache");
100 SmallString<128> CommentsTemplate =
101 appendPathPosix(AssetsPath, "comments-partial.mustache");
102
103 CDCtx.MustacheTemplates.insert({"class-template", ClassTemplate.c_str()});
104 CDCtx.MustacheTemplates.insert(
105 {"namespace-template", NamespaceTemplate.c_str()});
106 CDCtx.MustacheTemplates.insert({"all-files", AllFilesTemplate.c_str()});
107 CDCtx.MustacheTemplates.insert({"index", IndexTemplate.c_str()});
108 CDCtx.MustacheTemplates.insert({"comments", CommentsTemplate.c_str()});
109}
SmallString< 128 > appendPathNative(StringRef Base, StringRef Path)
Definition Utils.cpp:23
void getHtmlFiles(StringRef AssetsPath, clang::doc::ClangDocContext &CDCtx)
Definition Utils.cpp:37
SmallString< 128 > appendPathPosix(StringRef Base, StringRef Path)
Definition Utils.cpp:30
void getMdFiles(llvm::StringRef AssetsPath, clang::doc::ClangDocContext &CDCtx)
Definition Utils.cpp:87
This file contains general utility functions and helpers used across the clang-doc tool,...
Some operations such as code completion produce a set of candidates.
Definition Generators.h:152
std::vector< std::string > UserStylesheets
llvm::StringMap< std::string > MustacheTemplates
std::vector< std::string > JsScripts