blob: 71ca2d91ac60f45b76b8c289b60290ba637169f3 [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"
Julie Hockette975a472018-03-22 23:34:46 +000034#include "clang/Tooling/Tooling.h"
35#include "llvm/ADT/APFloat.h"
Julie Hockettac68cab2018-09-11 15:56:55 +000036#include "llvm/Support/CommandLine.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>
Julie Hocketteb50a2e2018-07-20 18:49:55 +000058 PublicOnly("public", llvm::cl::desc("Document only public declarations."),
59 llvm::cl::init(false), llvm::cl::cat(ClangDocCategory));
60
Julie Hockett229c63b2018-10-16 23:07:37 +000061static llvm::cl::opt<bool> DoxygenOnly(
62 "doxygen",
63 llvm::cl::desc("Use only doxygen-style comments to generate docs."),
64 llvm::cl::init(false), llvm::cl::cat(ClangDocCategory));
65
Julie Hockettd0f9a872018-06-04 17:22:20 +000066enum OutputFormatTy {
Julie Hockettac68cab2018-09-11 15:56:55 +000067 md,
Julie Hockettd0f9a872018-06-04 17:22:20 +000068 yaml,
69};
70
Julie Hockettac68cab2018-09-11 15:56:55 +000071static llvm::cl::opt<OutputFormatTy>
72 FormatEnum("format", llvm::cl::desc("Format for outputted docs."),
73 llvm::cl::values(clEnumValN(OutputFormatTy::yaml, "yaml",
74 "Documentation in YAML format."),
75 clEnumValN(OutputFormatTy::md, "md",
76 "Documentation in MD format.")),
77 llvm::cl::init(OutputFormatTy::yaml),
78 llvm::cl::cat(ClangDocCategory));
Julie Hockettd0f9a872018-06-04 17:22:20 +000079
Julie Hockett229c63b2018-10-16 23:07:37 +000080std::string getFormatString() {
81 switch (FormatEnum) {
82 case OutputFormatTy::yaml:
83 return "yaml";
84 case OutputFormatTy::md:
85 return "md";
86 }
87 llvm_unreachable("Unknown OutputFormatTy");
88}
Julie Hockette975a472018-03-22 23:34:46 +000089
Julie Hockettd0f9a872018-06-04 17:22:20 +000090bool CreateDirectory(const Twine &DirName, bool ClearDirectory = false) {
91 std::error_code OK;
92 llvm::SmallString<128> DocsRootPath;
93 if (ClearDirectory) {
94 std::error_code RemoveStatus = llvm::sys::fs::remove_directories(DirName);
95 if (RemoveStatus != OK) {
96 llvm::errs() << "Unable to remove existing documentation directory for "
97 << DirName << ".\n";
98 return true;
99 }
100 }
101 std::error_code DirectoryStatus = llvm::sys::fs::create_directories(DirName);
102 if (DirectoryStatus != OK) {
103 llvm::errs() << "Unable to create documentation directories.\n";
104 return true;
105 }
106 return false;
107}
108
Julie Hockett8899c292018-08-02 20:10:17 +0000109// A function to extract the appropriate path name for a given info's
110// documentation. The path returned is a composite of the parent namespaces as
111// directories plus the decl name as the filename.
112//
113// Example: Given the below, the <ext> path for class C will be <
114// root>/A/B/C.<ext>
115//
116// namespace A {
117// namesapce B {
118//
119// class C {};
120//
121// }
122// }
Julie Hockette78f3012018-06-06 16:13:17 +0000123llvm::Expected<llvm::SmallString<128>>
Julie Hockett8899c292018-08-02 20:10:17 +0000124getInfoOutputFile(StringRef Root,
125 llvm::SmallVectorImpl<doc::Reference> &Namespaces,
126 StringRef Name, StringRef Ext) {
Julie Hockette78f3012018-06-06 16:13:17 +0000127 std::error_code OK;
128 llvm::SmallString<128> Path;
129 llvm::sys::path::native(Root, Path);
130 for (auto R = Namespaces.rbegin(), E = Namespaces.rend(); R != E; ++R)
131 llvm::sys::path::append(Path, R->Name);
132
133 if (CreateDirectory(Path))
134 return llvm::make_error<llvm::StringError>("Unable to create directory.\n",
135 llvm::inconvertibleErrorCode());
136
Julie Hockett8899c292018-08-02 20:10:17 +0000137 if (Name.empty())
138 Name = "GlobalNamespace";
Julie Hockette78f3012018-06-06 16:13:17 +0000139 llvm::sys::path::append(Path, Name + Ext);
140 return Path;
141}
142
Julie Hockett8899c292018-08-02 20:10:17 +0000143// Iterate through tool results and build string map of info vectors from the
144// encoded bitstreams.
145bool bitcodeResultsToInfos(
146 tooling::ToolResults &Results,
147 llvm::StringMap<std::vector<std::unique_ptr<doc::Info>>> &Output) {
148 bool Err = false;
149 Results.forEachResult([&](StringRef Key, StringRef Value) {
150 llvm::BitstreamCursor Stream(Value);
151 doc::ClangDocBitcodeReader Reader(Stream);
152 auto Infos = Reader.readBitcode();
153 if (!Infos) {
154 llvm::errs() << toString(Infos.takeError()) << "\n";
155 Err = true;
156 return;
157 }
158 for (auto &I : Infos.get()) {
159 auto R =
160 Output.try_emplace(Key, std::vector<std::unique_ptr<doc::Info>>());
161 R.first->second.emplace_back(std::move(I));
162 }
163 });
164 return Err;
165}
166
Julie Hockette975a472018-03-22 23:34:46 +0000167int main(int argc, const char **argv) {
168 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
Julie Hockette78f3012018-06-06 16:13:17 +0000169 std::error_code OK;
170
Julie Hockett9e22b4c2018-10-26 19:11:34 +0000171 ExecutorName.setInitialValue("all-TUs");
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}