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 | ac68cab | 2018-09-11 15:56:55 +0000 | [diff] [blame] | 37 | #include "llvm/Support/CommandLine.h" |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Error.h" |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 39 | #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 | |
| 46 | using namespace clang::ast_matchers; |
| 47 | using namespace clang::tooling; |
| 48 | using namespace clang; |
| 49 | |
| 50 | static llvm::cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); |
| 51 | static llvm::cl::OptionCategory ClangDocCategory("clang-doc options"); |
| 52 | |
| 53 | static 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 | |
| 58 | static llvm::cl::opt<bool> |
Julie Hockett | eb50a2e | 2018-07-20 18:49:55 +0000 | [diff] [blame] | 59 | PublicOnly("public", llvm::cl::desc("Document only public declarations."), |
| 60 | llvm::cl::init(false), llvm::cl::cat(ClangDocCategory)); |
| 61 | |
Julie Hockett | 229c63b | 2018-10-16 23:07:37 +0000 | [diff] [blame^] | 62 | static 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 Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 67 | enum OutputFormatTy { |
Julie Hockett | ac68cab | 2018-09-11 15:56:55 +0000 | [diff] [blame] | 68 | md, |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 69 | yaml, |
| 70 | }; |
| 71 | |
Julie Hockett | ac68cab | 2018-09-11 15:56:55 +0000 | [diff] [blame] | 72 | static 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 Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 80 | |
Julie Hockett | 229c63b | 2018-10-16 23:07:37 +0000 | [diff] [blame^] | 81 | std::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 Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 90 | |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 91 | bool 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 Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 110 | // 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 Hockett | e78f301 | 2018-06-06 16:13:17 +0000 | [diff] [blame] | 124 | llvm::Expected<llvm::SmallString<128>> |
Julie Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 125 | getInfoOutputFile(StringRef Root, |
| 126 | llvm::SmallVectorImpl<doc::Reference> &Namespaces, |
| 127 | StringRef Name, StringRef Ext) { |
Julie Hockett | e78f301 | 2018-06-06 16:13:17 +0000 | [diff] [blame] | 128 | 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 Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 138 | if (Name.empty()) |
| 139 | Name = "GlobalNamespace"; |
Julie Hockett | e78f301 | 2018-06-06 16:13:17 +0000 | [diff] [blame] | 140 | llvm::sys::path::append(Path, Name + Ext); |
| 141 | return Path; |
| 142 | } |
| 143 | |
Julie Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 144 | // Iterate through tool results and build string map of info vectors from the |
| 145 | // encoded bitstreams. |
| 146 | bool 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 Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 168 | int main(int argc, const char **argv) { |
| 169 | llvm::sys::PrintStackTraceOnErrorSignal(argv[0]); |
Julie Hockett | e78f301 | 2018-06-06 16:13:17 +0000 | [diff] [blame] | 170 | std::error_code OK; |
| 171 | |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 172 | 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 Hockett | ac68cab | 2018-09-11 15:56:55 +0000 | [diff] [blame] | 180 | // 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 Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 189 | 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 Hockett | eb50a2e | 2018-07-20 18:49:55 +0000 | [diff] [blame] | 198 | clang::doc::ClangDocContext CDCtx = {Exec->get()->getExecutionContext(), |
| 199 | PublicOnly}; |
| 200 | auto Err = |
| 201 | Exec->get()->execute(doc::newMapperActionFactory(CDCtx), ArgAdjuster); |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 202 | if (Err) { |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 203 | llvm::errs() << toString(std::move(Err)) << "\n"; |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 204 | return 1; |
| 205 | } |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 206 | |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 207 | // Collect values into output by key. |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 208 | // In ToolResults, the Key is the hashed USR and the value is the |
| 209 | // bitcode-encoded representation of the Info object. |
Julie Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 210 | 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 Hockett | a9cb2dd | 2018-08-02 18:01:37 +0000 | [diff] [blame] | 214 | |
Julie Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 215 | // 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 Hockett | a9cb2dd | 2018-08-02 18:01:37 +0000 | [diff] [blame] | 218 | auto Reduced = doc::mergeInfos(Group.getValue()); |
Julie Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 219 | if (!Reduced) { |
Julie Hockett | a9cb2dd | 2018-08-02 18:01:37 +0000 | [diff] [blame] | 220 | llvm::errs() << llvm::toString(Reduced.takeError()); |
Julie Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 221 | continue; |
| 222 | } |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 223 | |
Julie Hockett | a9cb2dd | 2018-08-02 18:01:37 +0000 | [diff] [blame] | 224 | doc::Info *I = Reduced.get().get(); |
Julie Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 225 | |
| 226 | auto InfoPath = |
| 227 | getInfoOutputFile(OutDirectory, I->Namespace, I->Name, "." + Format); |
Julie Hockett | e78f301 | 2018-06-06 16:13:17 +0000 | [diff] [blame] | 228 | 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 Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 235 | llvm::errs() << "Error opening info file: " << FileErr.message() << "\n"; |
Julie Hockett | e78f301 | 2018-06-06 16:13:17 +0000 | [diff] [blame] | 236 | continue; |
| 237 | } |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 238 | |
Julie Hockett | ac68cab | 2018-09-11 15:56:55 +0000 | [diff] [blame] | 239 | if (auto Err = G->get()->generateDocForInfo(I, InfoOS)) |
| 240 | llvm::errs() << toString(std::move(Err)) << "\n"; |
Julie Hockett | e975a47 | 2018-03-22 23:34:46 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | return 0; |
| 244 | } |