blob: d8559aae2b5ac2f9007f328b6c6484ac26f36abf [file] [log] [blame]
Julie Hockette975a472018-03-22 23:34:46 +00001//===-- ClangDocMain.cpp - ClangDoc -----------------------------*- C++ -*-===//
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 tool for generating C and C++ documenation from source code
11// and comments. Generally, it runs a LibTooling FrontendAction on source files,
12// mapping each declaration in those files to its USR and serializing relevant
13// information into LLVM bitcode. It then runs a pass over the collected
14// declaration information, reducing by USR. There is an option to dump this
15// intermediate result to bitcode. Finally, it hands the reduced information
16// off to a generator, which does the final parsing from the intermediate
17// representation to the desired output format.
18//
19//===----------------------------------------------------------------------===//
20
Julie Hockettd0f9a872018-06-04 17:22:20 +000021#include "BitcodeReader.h"
22#include "BitcodeWriter.h"
Julie Hockette975a472018-03-22 23:34:46 +000023#include "ClangDoc.h"
Julie Hockette78f3012018-06-06 16:13:17 +000024#include "Generators.h"
Julie Hockettd0f9a872018-06-04 17:22:20 +000025#include "Representation.h"
Julie Hockette975a472018-03-22 23:34:46 +000026#include "clang/AST/AST.h"
27#include "clang/AST/Decl.h"
28#include "clang/ASTMatchers/ASTMatchFinder.h"
29#include "clang/ASTMatchers/ASTMatchersInternal.h"
30#include "clang/Driver/Options.h"
31#include "clang/Frontend/FrontendActions.h"
32#include "clang/Tooling/CommonOptionsParser.h"
33#include "clang/Tooling/Execution.h"
34#include "clang/Tooling/StandaloneExecution.h"
35#include "clang/Tooling/Tooling.h"
36#include "llvm/ADT/APFloat.h"
Julie Hockettac68cab2018-09-11 15:56:55 +000037#include "llvm/Support/CommandLine.h"
Julie Hockettd0f9a872018-06-04 17:22:20 +000038#include "llvm/Support/Error.h"
Julie Hockette975a472018-03-22 23:34:46 +000039#include "llvm/Support/FileSystem.h"
40#include "llvm/Support/Path.h"
41#include "llvm/Support/Process.h"
42#include "llvm/Support/Signals.h"
43#include "llvm/Support/raw_ostream.h"
44#include <string>
45
46using namespace clang::ast_matchers;
47using namespace clang::tooling;
48using namespace clang;
49
50static llvm::cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
51static llvm::cl::OptionCategory ClangDocCategory("clang-doc options");
52
53static llvm::cl::opt<std::string>
54 OutDirectory("output",
55 llvm::cl::desc("Directory for outputting generated files."),
56 llvm::cl::init("docs"), llvm::cl::cat(ClangDocCategory));
57
58static llvm::cl::opt<bool>
Julie Hocketteb50a2e2018-07-20 18:49:55 +000059 PublicOnly("public", llvm::cl::desc("Document only public declarations."),
60 llvm::cl::init(false), llvm::cl::cat(ClangDocCategory));
61
Julie Hockett229c63b2018-10-16 23:07:37 +000062static llvm::cl::opt<bool> DoxygenOnly(
63 "doxygen",
64 llvm::cl::desc("Use only doxygen-style comments to generate docs."),
65 llvm::cl::init(false), llvm::cl::cat(ClangDocCategory));
66
Julie Hockettd0f9a872018-06-04 17:22:20 +000067enum OutputFormatTy {
Julie Hockettac68cab2018-09-11 15:56:55 +000068 md,
Julie Hockettd0f9a872018-06-04 17:22:20 +000069 yaml,
70};
71
Julie Hockettac68cab2018-09-11 15:56:55 +000072static llvm::cl::opt<OutputFormatTy>
73 FormatEnum("format", llvm::cl::desc("Format for outputted docs."),
74 llvm::cl::values(clEnumValN(OutputFormatTy::yaml, "yaml",
75 "Documentation in YAML format."),
76 clEnumValN(OutputFormatTy::md, "md",
77 "Documentation in MD format.")),
78 llvm::cl::init(OutputFormatTy::yaml),
79 llvm::cl::cat(ClangDocCategory));
Julie Hockettd0f9a872018-06-04 17:22:20 +000080
Julie Hockett229c63b2018-10-16 23:07:37 +000081std::string getFormatString() {
82 switch (FormatEnum) {
83 case OutputFormatTy::yaml:
84 return "yaml";
85 case OutputFormatTy::md:
86 return "md";
87 }
88 llvm_unreachable("Unknown OutputFormatTy");
89}
Julie Hockette975a472018-03-22 23:34:46 +000090
Julie Hockettd0f9a872018-06-04 17:22:20 +000091bool CreateDirectory(const Twine &DirName, bool ClearDirectory = false) {
92 std::error_code OK;
93 llvm::SmallString<128> DocsRootPath;
94 if (ClearDirectory) {
95 std::error_code RemoveStatus = llvm::sys::fs::remove_directories(DirName);
96 if (RemoveStatus != OK) {
97 llvm::errs() << "Unable to remove existing documentation directory for "
98 << DirName << ".\n";
99 return true;
100 }
101 }
102 std::error_code DirectoryStatus = llvm::sys::fs::create_directories(DirName);
103 if (DirectoryStatus != OK) {
104 llvm::errs() << "Unable to create documentation directories.\n";
105 return true;
106 }
107 return false;
108}
109
Julie Hockett8899c292018-08-02 20:10:17 +0000110// A function to extract the appropriate path name for a given info's
111// documentation. The path returned is a composite of the parent namespaces as
112// directories plus the decl name as the filename.
113//
114// Example: Given the below, the <ext> path for class C will be <
115// root>/A/B/C.<ext>
116//
117// namespace A {
118// namesapce B {
119//
120// class C {};
121//
122// }
123// }
Julie Hockette78f3012018-06-06 16:13:17 +0000124llvm::Expected<llvm::SmallString<128>>
Julie Hockett8899c292018-08-02 20:10:17 +0000125getInfoOutputFile(StringRef Root,
126 llvm::SmallVectorImpl<doc::Reference> &Namespaces,
127 StringRef Name, StringRef Ext) {
Julie Hockette78f3012018-06-06 16:13:17 +0000128 std::error_code OK;
129 llvm::SmallString<128> Path;
130 llvm::sys::path::native(Root, Path);
131 for (auto R = Namespaces.rbegin(), E = Namespaces.rend(); R != E; ++R)
132 llvm::sys::path::append(Path, R->Name);
133
134 if (CreateDirectory(Path))
135 return llvm::make_error<llvm::StringError>("Unable to create directory.\n",
136 llvm::inconvertibleErrorCode());
137
Julie Hockett8899c292018-08-02 20:10:17 +0000138 if (Name.empty())
139 Name = "GlobalNamespace";
Julie Hockette78f3012018-06-06 16:13:17 +0000140 llvm::sys::path::append(Path, Name + Ext);
141 return Path;
142}
143
Julie Hockett8899c292018-08-02 20:10:17 +0000144// Iterate through tool results and build string map of info vectors from the
145// encoded bitstreams.
146bool bitcodeResultsToInfos(
147 tooling::ToolResults &Results,
148 llvm::StringMap<std::vector<std::unique_ptr<doc::Info>>> &Output) {
149 bool Err = false;
150 Results.forEachResult([&](StringRef Key, StringRef Value) {
151 llvm::BitstreamCursor Stream(Value);
152 doc::ClangDocBitcodeReader Reader(Stream);
153 auto Infos = Reader.readBitcode();
154 if (!Infos) {
155 llvm::errs() << toString(Infos.takeError()) << "\n";
156 Err = true;
157 return;
158 }
159 for (auto &I : Infos.get()) {
160 auto R =
161 Output.try_emplace(Key, std::vector<std::unique_ptr<doc::Info>>());
162 R.first->second.emplace_back(std::move(I));
163 }
164 });
165 return Err;
166}
167
Julie Hockette975a472018-03-22 23:34:46 +0000168int main(int argc, const char **argv) {
169 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
Julie Hockette78f3012018-06-06 16:13:17 +0000170 std::error_code OK;
171
Julie Hockette975a472018-03-22 23:34:46 +0000172 auto Exec = clang::tooling::createExecutorFromCommandLineArgs(
173 argc, argv, ClangDocCategory);
174
175 if (!Exec) {
176 llvm::errs() << toString(Exec.takeError()) << "\n";
177 return 1;
178 }
179
Julie Hockettac68cab2018-09-11 15:56:55 +0000180 // Fail early if an invalid format was provided.
181 std::string Format = getFormatString();
182 llvm::outs() << "Emiting docs in " << Format << " format.\n";
183 auto G = doc::findGeneratorByName(Format);
184 if (!G) {
185 llvm::errs() << toString(G.takeError()) << "\n";
186 return 1;
187 }
188
Julie Hockette975a472018-03-22 23:34:46 +0000189 ArgumentsAdjuster ArgAdjuster;
190 if (!DoxygenOnly)
191 ArgAdjuster = combineAdjusters(
192 getInsertArgumentAdjuster("-fparse-all-comments",
193 tooling::ArgumentInsertPosition::END),
194 ArgAdjuster);
195
196 // Mapping phase
197 llvm::outs() << "Mapping decls...\n";
Julie Hocketteb50a2e2018-07-20 18:49:55 +0000198 clang::doc::ClangDocContext CDCtx = {Exec->get()->getExecutionContext(),
199 PublicOnly};
200 auto Err =
201 Exec->get()->execute(doc::newMapperActionFactory(CDCtx), ArgAdjuster);
Julie Hockettd0f9a872018-06-04 17:22:20 +0000202 if (Err) {
Julie Hockette975a472018-03-22 23:34:46 +0000203 llvm::errs() << toString(std::move(Err)) << "\n";
Julie Hockettd0f9a872018-06-04 17:22:20 +0000204 return 1;
205 }
Julie Hockette975a472018-03-22 23:34:46 +0000206
Julie Hockettd0f9a872018-06-04 17:22:20 +0000207 // Collect values into output by key.
Julie Hockettd0f9a872018-06-04 17:22:20 +0000208 // In ToolResults, the Key is the hashed USR and the value is the
209 // bitcode-encoded representation of the Info object.
Julie Hockett8899c292018-08-02 20:10:17 +0000210 llvm::outs() << "Collecting infos...\n";
211 llvm::StringMap<std::vector<std::unique_ptr<doc::Info>>> USRToInfos;
212 if (bitcodeResultsToInfos(*Exec->get()->getToolResults(), USRToInfos))
213 return 1;
Julie Hocketta9cb2dd2018-08-02 18:01:37 +0000214
Julie Hockett8899c292018-08-02 20:10:17 +0000215 // First reducing phase (reduce all decls into one info per decl).
216 llvm::outs() << "Reducing " << USRToInfos.size() << " infos...\n";
217 for (auto &Group : USRToInfos) {
Julie Hocketta9cb2dd2018-08-02 18:01:37 +0000218 auto Reduced = doc::mergeInfos(Group.getValue());
Julie Hockett8899c292018-08-02 20:10:17 +0000219 if (!Reduced) {
Julie Hocketta9cb2dd2018-08-02 18:01:37 +0000220 llvm::errs() << llvm::toString(Reduced.takeError());
Julie Hockett8899c292018-08-02 20:10:17 +0000221 continue;
222 }
Julie Hockettd0f9a872018-06-04 17:22:20 +0000223
Julie Hocketta9cb2dd2018-08-02 18:01:37 +0000224 doc::Info *I = Reduced.get().get();
Julie Hockett8899c292018-08-02 20:10:17 +0000225
226 auto InfoPath =
227 getInfoOutputFile(OutDirectory, I->Namespace, I->Name, "." + Format);
Julie Hockette78f3012018-06-06 16:13:17 +0000228 if (!InfoPath) {
229 llvm::errs() << toString(InfoPath.takeError()) << "\n";
230 continue;
231 }
232 std::error_code FileErr;
233 llvm::raw_fd_ostream InfoOS(InfoPath.get(), FileErr, llvm::sys::fs::F_None);
234 if (FileErr != OK) {
Julie Hockett8899c292018-08-02 20:10:17 +0000235 llvm::errs() << "Error opening info file: " << FileErr.message() << "\n";
Julie Hockette78f3012018-06-06 16:13:17 +0000236 continue;
237 }
Julie Hockettd0f9a872018-06-04 17:22:20 +0000238
Julie Hockettac68cab2018-09-11 15:56:55 +0000239 if (auto Err = G->get()->generateDocForInfo(I, InfoOS))
240 llvm::errs() << toString(std::move(Err)) << "\n";
Julie Hockette975a472018-03-22 23:34:46 +0000241 }
242
243 return 0;
244}