1 /* extract doxygen comments from C++ header files
3 * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <clang-c/Index.h>
28 #include <clang-c/Documentation.h>
31 dox2js () : clang_argc (3), clang_argv (0), excl_argc (0), excl_argv (0)
33 excl_argv = (char**) calloc (1, sizeof (char*));
34 clang_argv = (char**) malloc (clang_argc * sizeof (char*));
35 clang_argv[0] = strdup ("-x");
36 clang_argv[1] = strdup ("c++");
37 clang_argv[2] = strdup ("-std=c++11");
41 for (int i = 0; i < clang_argc; ++i) {
44 for (int i = 0; excl_argv[i]; ++i) {
51 void add_clang_arg (const char *a) {
52 clang_argv = (char**) realloc (clang_argv, (clang_argc + 1) * sizeof (char*));
53 clang_argv[clang_argc++] = strdup (a);
56 void add_exclude (const char *a) {
57 excl_argv = (char**) realloc (excl_argv, (excl_argc + 2) * sizeof (char*));
58 excl_argv[excl_argc++] = strdup (a);
59 excl_argv[excl_argc] = NULL;
66 std::map <std::string, std::string> results;
70 kind_to_txt (CXCursor cursor)
72 CXCursorKind kind = clang_getCursorKind (cursor);
74 case CXCursor_StructDecl : return "Struct";
75 case CXCursor_EnumDecl : return "Enum";
76 case CXCursor_UnionDecl : return "Union";
77 case CXCursor_FunctionDecl : return "C Function";
78 case CXCursor_VarDecl : return "Variable";
79 case CXCursor_ClassDecl : return "C++ Class";
80 case CXCursor_CXXMethod : return "C++ Method";
81 case CXCursor_Namespace : return "C++ Namespace";
82 case CXCursor_Constructor : return "C++ Constructor";
83 case CXCursor_Destructor : return "C++ Destructor";
84 case CXCursor_FieldDecl : return "Data Member/Field";
91 escape_json (const std::string &s)
94 for (auto c = s.cbegin (); c != s.cend (); c++) {
96 case '"': o << "\\\""; break;
97 case '\\': o << "\\\\"; break;
98 case '\n': o << "\\n"; break;
99 case '\r': o << "\\r"; break;
100 case '\t': o << "\\t"; break;
102 if ('\x00' <= *c && *c <= '\x1f') {
103 o << "\\u" << std::hex << std::setw (4) << std::setfill ('0') << (int)*c;
113 recurse_parents (CXCursor cr) {
115 CXCursor pc = clang_getCursorSemanticParent (cr);
116 if (CXCursor_TranslationUnit == clang_getCursorKind (pc)) {
119 if (!clang_Cursor_isNull (pc)) {
120 rv += recurse_parents (pc);
121 rv += clang_getCString (clang_getCursorDisplayName (pc));
128 check_excludes (const std::string& decl, CXClientData d) {
129 struct dox2js* dj = (struct dox2js*) d;
130 char** excl = dj->excl_argv;
131 for (int i = 0; excl[i]; ++i) {
132 if (decl.compare (0, strlen (excl[i]), excl[i]) == 0) {
139 static enum CXChildVisitResult
140 traverse (CXCursor cr, CXCursor /*parent*/, CXClientData d)
142 struct dox2js* dj = (struct dox2js*) d;
143 CXComment c = clang_Cursor_getParsedComment (cr);
145 if (clang_Comment_getKind (c) != CXComment_Null
146 && clang_isDeclaration (clang_getCursorKind (cr))
147 && 0 != strlen (kind_to_txt (cr))
150 // TODO: resolve typedef enum { .. } name;
151 // use clang_getCursorDefinition (clang_getCanonicalCursor (cr)) ??
152 std::string decl = recurse_parents (cr);
153 decl += clang_getCString (clang_getCursorDisplayName (cr));
155 if (decl.empty () || check_excludes (decl, d)) {
156 return CXChildVisit_Recurse;
159 std::ostringstream o;
160 o << "{ \"decl\" : \"" << decl << "\",\n";
162 if (clang_Cursor_isVariadic (cr)) {
163 o << " \"variadic\" : true,\n";
166 CXSourceLocation loc = clang_getCursorLocation (cr);
167 CXFile file; unsigned line, col, off;
168 clang_getFileLocation (loc, &file, &line, &col, &off);
170 o << " \"kind\" : \"" << kind_to_txt (cr) << "\",\n"
171 << " \"src\" : \"" << clang_getCString (clang_getFileName (file)) << ":" << line << "\",\n"
172 << " \"doc\" : \"" << escape_json (clang_getCString (clang_FullComment_getAsHTML (c))) << "\"\n"
175 dj->results[decl] = o.str ();
177 return CXChildVisit_Recurse;
181 process_file (const char* fn, struct dox2js *dj, bool check)
184 fprintf (stderr, "--- %s ---\n", fn);
186 CXIndex index = clang_createIndex (0, check ? 1 : 0);
187 CXTranslationUnit tu = clang_createTranslationUnitFromSourceFile (index, fn, dj->clang_argc, dj->clang_argv, 0, 0);
190 fprintf (stderr, "Cannot create translation unit for src: %s\n", fn);
194 clang_visitChildren (clang_getTranslationUnitCursor (tu), traverse, (CXClientData) dj);
196 clang_disposeTranslationUnit (tu);
197 clang_disposeIndex (index);
203 printf ("doxy2json - extract doxygen doc from C++ headers.\n\n");
204 fprintf (stderr, "Usage: dox2json [-I path]* [-X exclude]* <filename> [filename]*\n");
208 int main (int argc, char** argv)
212 bool report_progress = false;
213 bool check_compile = false;
215 while (EOF != (c = getopt (argc, argv, "D:I:TX:"))) {
218 dj.add_clang_arg ("-I");
219 dj.add_clang_arg (optarg);
222 dj.add_clang_arg ("-D");
223 dj.add_clang_arg (optarg);
226 dj.add_exclude (optarg);
229 check_compile = true;
234 usage (EXIT_FAILURE);
239 if (optind >= argc) {
240 usage (EXIT_FAILURE);
243 const int total = (argc - optind);
244 if (total > 6 && !check_compile) {
245 report_progress = true;
248 for (int i = optind; i < argc; ++i) {
249 process_file (argv[i], &dj, check_compile);
250 if (report_progress) {
251 fprintf (stderr, "progress: %4.1f%% [%4d / %4d] decl: %ld \r",
252 100.f * (1.f + i - optind) / (float)total, i - optind, total,
258 if (!check_compile) {
260 for (std::map <std::string, std::string>::const_iterator i = dj.results.begin (); i != dj.results.end (); ++i) {
261 printf ("%s\n", (*i).second.c_str ());