blob: 9345dad87c0cdfa6a452e04d4f599abb5e81e2d1 [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
67enum OutputFormatTy {
68 yaml,
69};
70
Julie Hockette78f3012018-06-06 16:13:17 +000071static llvm::cl::opt<OutputFormatTy> FormatEnum(
72 "format", llvm::cl::desc("Format for outputted docs."),
73 llvm::cl::values(clEnumVal(yaml, "Documentation in YAML format.")),
74 llvm::cl::init(yaml), llvm::cl::cat(ClangDocCategory));
Julie Hockettd0f9a872018-06-04 17:22:20 +000075
Julie Hockette975a472018-03-22 23:34:46 +000076static llvm::cl::opt<bool> DoxygenOnly(
77 "doxygen",
78 llvm::cl::desc("Use only doxygen-style comments to generate docs."),
79 llvm::cl::init(false), llvm::cl::cat(ClangDocCategory));
80
Julie Hockettd0f9a872018-06-04 17:22:20 +000081bool CreateDirectory(const Twine &DirName, bool ClearDirectory = false) {
82 std::error_code OK;
83 llvm::SmallString<128> DocsRootPath;
84 if (ClearDirectory) {
85 std::error_code RemoveStatus = llvm::sys::fs::remove_directories(DirName);
86 if (RemoveStatus != OK) {
87 llvm::errs() << "Unable to remove existing documentation directory for "
88 << DirName << ".\n";
89 return true;
90 }
91 }
92 std::error_code DirectoryStatus = llvm::sys::fs::create_directories(DirName);
93 if (DirectoryStatus != OK) {
94 llvm::errs() << "Unable to create documentation directories.\n";
95 return true;
96 }
97 return false;
98}
99
100bool DumpResultToFile(const Twine &DirName, const Twine &FileName,
101 StringRef Buffer, bool ClearDirectory = false) {
102 std::error_code OK;
103 llvm::SmallString<128> IRRootPath;
104 llvm::sys::path::native(OutDirectory, IRRootPath);
105 llvm::sys::path::append(IRRootPath, DirName);
106 if (CreateDirectory(IRRootPath, ClearDirectory))
107 return true;
108 llvm::sys::path::append(IRRootPath, FileName);
109 std::error_code OutErrorInfo;
110 llvm::raw_fd_ostream OS(IRRootPath, OutErrorInfo, llvm::sys::fs::F_None);
111 if (OutErrorInfo != OK) {
112 llvm::errs() << "Error opening documentation file.\n";
113 return true;
114 }
115 OS << Buffer;
116 OS.close();
117 return false;
118}
119
Julie Hockette78f3012018-06-06 16:13:17 +0000120llvm::Expected<llvm::SmallString<128>>
121getPath(StringRef Root, StringRef Ext, StringRef Name,
122 llvm::SmallVectorImpl<doc::Reference> &Namespaces) {
123 std::error_code OK;
124 llvm::SmallString<128> Path;
125 llvm::sys::path::native(Root, Path);
126 for (auto R = Namespaces.rbegin(), E = Namespaces.rend(); R != E; ++R)
127 llvm::sys::path::append(Path, R->Name);
128
129 if (CreateDirectory(Path))
130 return llvm::make_error<llvm::StringError>("Unable to create directory.\n",
131 llvm::inconvertibleErrorCode());
132
133 llvm::sys::path::append(Path, Name + Ext);
134 return Path;
135}
136
137std::string getFormatString(OutputFormatTy Ty) {
138 switch (Ty) {
139 case yaml:
140 return "yaml";
141 }
142}
143
Julie Hockette975a472018-03-22 23:34:46 +0000144int main(int argc, const char **argv) {
145 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
Julie Hockette78f3012018-06-06 16:13:17 +0000146 std::error_code OK;
147
148 // Fail early if an invalid format was provided.
149 std::string Format = getFormatString(FormatEnum);
150 auto G = doc::findGeneratorByName(Format);
151 if (!G) {
152 llvm::errs() << toString(G.takeError()) << "\n";
153 return 1;
154 }
Julie Hockette975a472018-03-22 23:34:46 +0000155
156 auto Exec = clang::tooling::createExecutorFromCommandLineArgs(
157 argc, argv, ClangDocCategory);
158
159 if (!Exec) {
160 llvm::errs() << toString(Exec.takeError()) << "\n";
161 return 1;
162 }
163
164 ArgumentsAdjuster ArgAdjuster;
165 if (!DoxygenOnly)
166 ArgAdjuster = combineAdjusters(
167 getInsertArgumentAdjuster("-fparse-all-comments",
168 tooling::ArgumentInsertPosition::END),
169 ArgAdjuster);
170
171 // Mapping phase
172 llvm::outs() << "Mapping decls...\n";
Julie Hockettd0f9a872018-06-04 17:22:20 +0000173 auto Err = Exec->get()->execute(
174 doc::newMapperActionFactory(Exec->get()->getExecutionContext()),
175 ArgAdjuster);
176 if (Err) {
Julie Hockette975a472018-03-22 23:34:46 +0000177 llvm::errs() << toString(std::move(Err)) << "\n";
Julie Hockettd0f9a872018-06-04 17:22:20 +0000178 return 1;
179 }
Julie Hockette975a472018-03-22 23:34:46 +0000180
181 if (DumpMapperResult) {
Julie Hockettd0f9a872018-06-04 17:22:20 +0000182 bool Err = false;
183 Exec->get()->getToolResults()->forEachResult(
184 [&](StringRef Key, StringRef Value) {
185 Err = DumpResultToFile("bc", Key + ".bc", Value);
186 });
187 if (Err)
188 llvm::errs() << "Error dumping map results.\n";
189 return Err;
190 }
191
192 // Collect values into output by key.
193 llvm::outs() << "Collecting infos...\n";
194 llvm::StringMap<std::vector<std::unique_ptr<doc::Info>>> MapOutput;
195
196 // In ToolResults, the Key is the hashed USR and the value is the
197 // bitcode-encoded representation of the Info object.
198 Exec->get()->getToolResults()->forEachResult([&](StringRef Key,
199 StringRef Value) {
200 llvm::BitstreamCursor Stream(Value);
201 doc::ClangDocBitcodeReader Reader(Stream);
202 auto Infos = Reader.readBitcode();
203 for (auto &I : Infos) {
204 auto R =
205 MapOutput.try_emplace(Key, std::vector<std::unique_ptr<doc::Info>>());
206 R.first->second.emplace_back(std::move(I));
207 }
208 });
209
Julie Hockette78f3012018-06-06 16:13:17 +0000210 // Reducing and generation phases
Julie Hockettd0f9a872018-06-04 17:22:20 +0000211 llvm::outs() << "Reducing " << MapOutput.size() << " infos...\n";
212 llvm::StringMap<std::unique_ptr<doc::Info>> ReduceOutput;
213 for (auto &Group : MapOutput) {
214 auto Reduced = doc::mergeInfos(Group.getValue());
215 if (!Reduced)
216 llvm::errs() << llvm::toString(Reduced.takeError());
217
218 if (DumpIntermediateResult) {
219 SmallString<4096> Buffer;
220 llvm::BitstreamWriter Stream(Buffer);
221 doc::ClangDocBitcodeWriter Writer(Stream);
222 Writer.dispatchInfoForWrite(Reduced.get().get());
Julie Hockette78f3012018-06-06 16:13:17 +0000223 if (DumpResultToFile("bc", Group.getKey() + ".bc", Buffer))
224 llvm::errs() << "Error dumping to bitcode.\n";
225 continue;
Julie Hockettd0f9a872018-06-04 17:22:20 +0000226 }
227
Julie Hockette78f3012018-06-06 16:13:17 +0000228 // Create the relevant ostream and emit the documentation for this decl.
229 doc::Info *I = Reduced.get().get();
230 auto InfoPath = getPath(OutDirectory, "." + Format, I->Name, I->Namespace);
231 if (!InfoPath) {
232 llvm::errs() << toString(InfoPath.takeError()) << "\n";
233 continue;
234 }
235 std::error_code FileErr;
236 llvm::raw_fd_ostream InfoOS(InfoPath.get(), FileErr, llvm::sys::fs::F_None);
237 if (FileErr != OK) {
238 llvm::errs() << "Error opening index file: " << FileErr.message() << "\n";
239 continue;
240 }
Julie Hockettd0f9a872018-06-04 17:22:20 +0000241
Julie Hockette78f3012018-06-06 16:13:17 +0000242 if (G->get()->generateDocForInfo(I, InfoOS))
243 llvm::errs() << "Unable to generate docs for info.\n";
Julie Hockette975a472018-03-22 23:34:46 +0000244 }
245
246 return 0;
247}