blob: 6e4a92d7b06effa0222bcf1e0489cf3a6488a2ac [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 Hockettd0f9a872018-06-04 17:22:20 +000037#include "llvm/Support/Error.h"
Julie Hockette975a472018-03-22 23:34:46 +000038#include "llvm/Support/FileSystem.h"
39#include "llvm/Support/Path.h"
40#include "llvm/Support/Process.h"
41#include "llvm/Support/Signals.h"
42#include "llvm/Support/raw_ostream.h"
43#include <string>
44
45using namespace clang::ast_matchers;
46using namespace clang::tooling;
47using namespace clang;
48
49static llvm::cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
50static llvm::cl::OptionCategory ClangDocCategory("clang-doc options");
51
52static llvm::cl::opt<std::string>
53 OutDirectory("output",
54 llvm::cl::desc("Directory for outputting generated files."),
55 llvm::cl::init("docs"), llvm::cl::cat(ClangDocCategory));
56
57static llvm::cl::opt<bool>
58 DumpMapperResult("dump-mapper",
59 llvm::cl::desc("Dump mapper results to bitcode file."),
60 llvm::cl::init(false), llvm::cl::cat(ClangDocCategory));
61
Julie Hockettd0f9a872018-06-04 17:22:20 +000062static llvm::cl::opt<bool> DumpIntermediateResult(
63 "dump-intermediate",
64 llvm::cl::desc("Dump intermediate results to bitcode file."),
65 llvm::cl::init(false), llvm::cl::cat(ClangDocCategory));
66
Julie Hocketteb50a2e2018-07-20 18:49:55 +000067static llvm::cl::opt<bool>
68 PublicOnly("public", llvm::cl::desc("Document only public declarations."),
69 llvm::cl::init(false), llvm::cl::cat(ClangDocCategory));
70
Julie Hockettd0f9a872018-06-04 17:22:20 +000071enum OutputFormatTy {
72 yaml,
73};
74
Julie Hockette78f3012018-06-06 16:13:17 +000075static llvm::cl::opt<OutputFormatTy> FormatEnum(
76 "format", llvm::cl::desc("Format for outputted docs."),
77 llvm::cl::values(clEnumVal(yaml, "Documentation in YAML format.")),
78 llvm::cl::init(yaml), llvm::cl::cat(ClangDocCategory));
Julie Hockettd0f9a872018-06-04 17:22:20 +000079
Julie Hockette975a472018-03-22 23:34:46 +000080static llvm::cl::opt<bool> DoxygenOnly(
81 "doxygen",
82 llvm::cl::desc("Use only doxygen-style comments to generate docs."),
83 llvm::cl::init(false), llvm::cl::cat(ClangDocCategory));
84
Julie Hockettd0f9a872018-06-04 17:22:20 +000085bool CreateDirectory(const Twine &DirName, bool ClearDirectory = false) {
86 std::error_code OK;
87 llvm::SmallString<128> DocsRootPath;
88 if (ClearDirectory) {
89 std::error_code RemoveStatus = llvm::sys::fs::remove_directories(DirName);
90 if (RemoveStatus != OK) {
91 llvm::errs() << "Unable to remove existing documentation directory for "
92 << DirName << ".\n";
93 return true;
94 }
95 }
96 std::error_code DirectoryStatus = llvm::sys::fs::create_directories(DirName);
97 if (DirectoryStatus != OK) {
98 llvm::errs() << "Unable to create documentation directories.\n";
99 return true;
100 }
101 return false;
102}
103
104bool DumpResultToFile(const Twine &DirName, const Twine &FileName,
105 StringRef Buffer, bool ClearDirectory = false) {
106 std::error_code OK;
107 llvm::SmallString<128> IRRootPath;
108 llvm::sys::path::native(OutDirectory, IRRootPath);
109 llvm::sys::path::append(IRRootPath, DirName);
110 if (CreateDirectory(IRRootPath, ClearDirectory))
111 return true;
112 llvm::sys::path::append(IRRootPath, FileName);
113 std::error_code OutErrorInfo;
114 llvm::raw_fd_ostream OS(IRRootPath, OutErrorInfo, llvm::sys::fs::F_None);
115 if (OutErrorInfo != OK) {
116 llvm::errs() << "Error opening documentation file.\n";
117 return true;
118 }
119 OS << Buffer;
120 OS.close();
121 return false;
122}
123
Julie Hockett8899c292018-08-02 20:10:17 +0000124// A function to extract the appropriate path name for a given info's
125// documentation. The path returned is a composite of the parent namespaces as
126// directories plus the decl name as the filename.
127//
128// Example: Given the below, the <ext> path for class C will be <
129// root>/A/B/C.<ext>
130//
131// namespace A {
132// namesapce B {
133//
134// class C {};
135//
136// }
137// }
Julie Hockette78f3012018-06-06 16:13:17 +0000138llvm::Expected<llvm::SmallString<128>>
Julie Hockett8899c292018-08-02 20:10:17 +0000139getInfoOutputFile(StringRef Root,
140 llvm::SmallVectorImpl<doc::Reference> &Namespaces,
141 StringRef Name, StringRef Ext) {
Julie Hockette78f3012018-06-06 16:13:17 +0000142 std::error_code OK;
143 llvm::SmallString<128> Path;
144 llvm::sys::path::native(Root, Path);
145 for (auto R = Namespaces.rbegin(), E = Namespaces.rend(); R != E; ++R)
146 llvm::sys::path::append(Path, R->Name);
147
148 if (CreateDirectory(Path))
149 return llvm::make_error<llvm::StringError>("Unable to create directory.\n",
150 llvm::inconvertibleErrorCode());
151
Julie Hockett8899c292018-08-02 20:10:17 +0000152 if (Name.empty())
153 Name = "GlobalNamespace";
Julie Hockette78f3012018-06-06 16:13:17 +0000154 llvm::sys::path::append(Path, Name + Ext);
155 return Path;
156}
157
158std::string getFormatString(OutputFormatTy Ty) {
159 switch (Ty) {
160 case yaml:
161 return "yaml";
162 }
Simon Pilgrim98b7f392018-06-06 19:31:39 +0000163 llvm_unreachable("Unknown OutputFormatTy");
Julie Hockette78f3012018-06-06 16:13:17 +0000164}
165
Julie Hockett8899c292018-08-02 20:10:17 +0000166// Iterate through tool results and build string map of info vectors from the
167// encoded bitstreams.
168bool bitcodeResultsToInfos(
169 tooling::ToolResults &Results,
170 llvm::StringMap<std::vector<std::unique_ptr<doc::Info>>> &Output) {
171 bool Err = false;
172 Results.forEachResult([&](StringRef Key, StringRef Value) {
173 llvm::BitstreamCursor Stream(Value);
174 doc::ClangDocBitcodeReader Reader(Stream);
175 auto Infos = Reader.readBitcode();
176 if (!Infos) {
177 llvm::errs() << toString(Infos.takeError()) << "\n";
178 Err = true;
179 return;
180 }
181 for (auto &I : Infos.get()) {
182 auto R =
183 Output.try_emplace(Key, std::vector<std::unique_ptr<doc::Info>>());
184 R.first->second.emplace_back(std::move(I));
185 }
186 });
187 return Err;
188}
189
Julie Hockette975a472018-03-22 23:34:46 +0000190int main(int argc, const char **argv) {
191 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
Julie Hockette78f3012018-06-06 16:13:17 +0000192 std::error_code OK;
193
194 // Fail early if an invalid format was provided.
195 std::string Format = getFormatString(FormatEnum);
196 auto G = doc::findGeneratorByName(Format);
197 if (!G) {
198 llvm::errs() << toString(G.takeError()) << "\n";
199 return 1;
200 }
Julie Hockette975a472018-03-22 23:34:46 +0000201
202 auto Exec = clang::tooling::createExecutorFromCommandLineArgs(
203 argc, argv, ClangDocCategory);
204
205 if (!Exec) {
206 llvm::errs() << toString(Exec.takeError()) << "\n";
207 return 1;
208 }
209
210 ArgumentsAdjuster ArgAdjuster;
211 if (!DoxygenOnly)
212 ArgAdjuster = combineAdjusters(
213 getInsertArgumentAdjuster("-fparse-all-comments",
214 tooling::ArgumentInsertPosition::END),
215 ArgAdjuster);
216
217 // Mapping phase
218 llvm::outs() << "Mapping decls...\n";
Julie Hocketteb50a2e2018-07-20 18:49:55 +0000219 clang::doc::ClangDocContext CDCtx = {Exec->get()->getExecutionContext(),
220 PublicOnly};
221 auto Err =
222 Exec->get()->execute(doc::newMapperActionFactory(CDCtx), ArgAdjuster);
Julie Hockettd0f9a872018-06-04 17:22:20 +0000223 if (Err) {
Julie Hockette975a472018-03-22 23:34:46 +0000224 llvm::errs() << toString(std::move(Err)) << "\n";
Julie Hockettd0f9a872018-06-04 17:22:20 +0000225 return 1;
226 }
Julie Hockette975a472018-03-22 23:34:46 +0000227
228 if (DumpMapperResult) {
Julie Hockettd0f9a872018-06-04 17:22:20 +0000229 bool Err = false;
230 Exec->get()->getToolResults()->forEachResult(
231 [&](StringRef Key, StringRef Value) {
232 Err = DumpResultToFile("bc", Key + ".bc", Value);
233 });
234 if (Err)
235 llvm::errs() << "Error dumping map results.\n";
236 return Err;
237 }
238
239 // Collect values into output by key.
Julie Hockettd0f9a872018-06-04 17:22:20 +0000240 // In ToolResults, the Key is the hashed USR and the value is the
241 // bitcode-encoded representation of the Info object.
Julie Hockett8899c292018-08-02 20:10:17 +0000242 llvm::outs() << "Collecting infos...\n";
243 llvm::StringMap<std::vector<std::unique_ptr<doc::Info>>> USRToInfos;
244 if (bitcodeResultsToInfos(*Exec->get()->getToolResults(), USRToInfos))
245 return 1;
Julie Hocketta9cb2dd2018-08-02 18:01:37 +0000246
Julie Hockett8899c292018-08-02 20:10:17 +0000247 // First reducing phase (reduce all decls into one info per decl).
248 llvm::outs() << "Reducing " << USRToInfos.size() << " infos...\n";
249 for (auto &Group : USRToInfos) {
Julie Hocketta9cb2dd2018-08-02 18:01:37 +0000250 auto Reduced = doc::mergeInfos(Group.getValue());
Julie Hockett8899c292018-08-02 20:10:17 +0000251 if (!Reduced) {
Julie Hocketta9cb2dd2018-08-02 18:01:37 +0000252 llvm::errs() << llvm::toString(Reduced.takeError());
Julie Hockett8899c292018-08-02 20:10:17 +0000253 continue;
254 }
Julie Hockettd0f9a872018-06-04 17:22:20 +0000255
256 if (DumpIntermediateResult) {
257 SmallString<4096> Buffer;
258 llvm::BitstreamWriter Stream(Buffer);
259 doc::ClangDocBitcodeWriter Writer(Stream);
260 Writer.dispatchInfoForWrite(Reduced.get().get());
Julie Hockette78f3012018-06-06 16:13:17 +0000261 if (DumpResultToFile("bc", Group.getKey() + ".bc", Buffer))
262 llvm::errs() << "Error dumping to bitcode.\n";
263 continue;
Julie Hockettd0f9a872018-06-04 17:22:20 +0000264 }
Julie Hocketta9cb2dd2018-08-02 18:01:37 +0000265 doc::Info *I = Reduced.get().get();
Julie Hockett8899c292018-08-02 20:10:17 +0000266
267 auto InfoPath =
268 getInfoOutputFile(OutDirectory, I->Namespace, I->Name, "." + Format);
Julie Hockette78f3012018-06-06 16:13:17 +0000269 if (!InfoPath) {
270 llvm::errs() << toString(InfoPath.takeError()) << "\n";
271 continue;
272 }
273 std::error_code FileErr;
274 llvm::raw_fd_ostream InfoOS(InfoPath.get(), FileErr, llvm::sys::fs::F_None);
275 if (FileErr != OK) {
Julie Hockett8899c292018-08-02 20:10:17 +0000276 llvm::errs() << "Error opening info file: " << FileErr.message() << "\n";
Julie Hockette78f3012018-06-06 16:13:17 +0000277 continue;
278 }
Julie Hockettd0f9a872018-06-04 17:22:20 +0000279
Julie Hockette78f3012018-06-06 16:13:17 +0000280 if (G->get()->generateDocForInfo(I, InfoOS))
281 llvm::errs() << "Unable to generate docs for info.\n";
Julie Hockette975a472018-03-22 23:34:46 +0000282 }
283
284 return 0;
285}