Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 1 | //===-- 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 Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 21 | #include "BitcodeReader.h" |
| 22 | #include "BitcodeWriter.h" |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 23 | #include "ClangDoc.h" |
Julie Hockett | e78f301 | 2018-06-06 16:13:17 +0000 | [diff] [blame^] | 24 | #include "Generators.h" |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 25 | #include "Representation.h" |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 26 | #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 Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Error.h" |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 38 | #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 | |
| 45 | using namespace clang::ast_matchers; |
| 46 | using namespace clang::tooling; |
| 47 | using namespace clang; |
| 48 | |
| 49 | static llvm::cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); |
| 50 | static llvm::cl::OptionCategory ClangDocCategory("clang-doc options"); |
| 51 | |
| 52 | static 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 | |
| 57 | static 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 Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 62 | static 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 | |
| 67 | enum OutputFormatTy { |
| 68 | yaml, |
| 69 | }; |
| 70 | |
Julie Hockett | e78f301 | 2018-06-06 16:13:17 +0000 | [diff] [blame^] | 71 | static 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 Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 75 | |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 76 | static 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 Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 81 | bool 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 | |
| 100 | bool 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 Hockett | e78f301 | 2018-06-06 16:13:17 +0000 | [diff] [blame^] | 120 | llvm::Expected<llvm::SmallString<128>> |
| 121 | getPath(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 | |
| 137 | std::string getFormatString(OutputFormatTy Ty) { |
| 138 | switch (Ty) { |
| 139 | case yaml: |
| 140 | return "yaml"; |
| 141 | } |
| 142 | } |
| 143 | |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 144 | int main(int argc, const char **argv) { |
| 145 | llvm::sys::PrintStackTraceOnErrorSignal(argv[0]); |
Julie Hockett | e78f301 | 2018-06-06 16:13:17 +0000 | [diff] [blame^] | 146 | 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 Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 155 | |
| 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 Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 173 | auto Err = Exec->get()->execute( |
| 174 | doc::newMapperActionFactory(Exec->get()->getExecutionContext()), |
| 175 | ArgAdjuster); |
| 176 | if (Err) { |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 177 | llvm::errs() << toString(std::move(Err)) << "\n"; |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 178 | return 1; |
| 179 | } |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 180 | |
| 181 | if (DumpMapperResult) { |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 182 | 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 Hockett | e78f301 | 2018-06-06 16:13:17 +0000 | [diff] [blame^] | 210 | // Reducing and generation phases |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 211 | 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 Hockett | e78f301 | 2018-06-06 16:13:17 +0000 | [diff] [blame^] | 223 | if (DumpResultToFile("bc", Group.getKey() + ".bc", Buffer)) |
| 224 | llvm::errs() << "Error dumping to bitcode.\n"; |
| 225 | continue; |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Julie Hockett | e78f301 | 2018-06-06 16:13:17 +0000 | [diff] [blame^] | 228 | // 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 Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 241 | |
Julie Hockett | e78f301 | 2018-06-06 16:13:17 +0000 | [diff] [blame^] | 242 | if (G->get()->generateDocForInfo(I, InfoOS)) |
| 243 | llvm::errs() << "Unable to generate docs for info.\n"; |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | return 0; |
| 247 | } |