summaryrefslogtreecommitdiff
path: root/tools/doxy2json/doxy2json.cc
blob: f8242d5d46dbf750b019460e81df4d31f9e40457 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* extract doxygen comments from C++ header files
 *
 * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <clang-c/Index.h>
#include <clang-c/Documentation.h>

static const char*
kind_to_txt (CXCursor cursor)
{
	CXCursorKind kind  = clang_getCursorKind (cursor);
	switch (kind) {
		case CXCursor_StructDecl   : return "Struct";
		case CXCursor_EnumDecl     : return "Enum";
		case CXCursor_UnionDecl    : return "Union";
		case CXCursor_FunctionDecl : return "C Function";
		case CXCursor_VarDecl      : return "Variable";
		case CXCursor_ClassDecl    : return "C++ Class";
		case CXCursor_CXXMethod    : return "C++ Method";
		case CXCursor_Namespace    : return "C++ Namespace";
		case CXCursor_Constructor  : return "C++ Constructor";
		case CXCursor_Destructor   : return "C++ Destructor";
		case CXCursor_FieldDecl    : return "Data Member/Field";
		default: break;
	}
	return "";
}

static std::string
escape_json (const std::string &s)
{
	std::ostringstream o;
	for (auto c = s.cbegin (); c != s.cend (); c++) {
		switch (*c) {
			case '"':  o << "\\\""; break;
			case '\\': o << "\\\\"; break;
			case '\n': o << "\\n"; break;
			case '\r': o << "\\r"; break;
			case '\t': o << "\\t"; break;
			default:
				if ('\x00' <= *c && *c <= '\x1f') {
					o << "\\u" << std::hex << std::setw (4) << std::setfill ('0') << (int)*c;
				} else {
				  o << *c;
				}
		}
	}
	return o.str ();
}

static void recurse_parents (CXCursor cr) {
	CXCursor pc = clang_getCursorSemanticParent (cr);
	if (CXCursor_TranslationUnit == clang_getCursorKind (pc)) {
		return;
	}
	if (!clang_Cursor_isNull (pc)) {
		recurse_parents (pc);
		printf ("%s::", clang_getCString (clang_getCursorDisplayName (pc)));
	}
}

static enum CXChildVisitResult
traverse (CXCursor cr, CXCursor /*parent*/, CXClientData)
{
	CXComment c = clang_Cursor_getParsedComment (cr);

	if (clang_Comment_getKind (c) != CXComment_Null
			&& clang_isDeclaration (clang_getCursorKind (cr))
			&& 0 != strlen (kind_to_txt (cr))
		 ) {

		printf ("{ \"decl\" : \"");
		recurse_parents (cr);

		// TODO: resolve typedef enum { .. } name;
		// use clang_getCursorDefinition (clang_getCanonicalCursor (cr)) ??
		printf ("%s\",\n", clang_getCString (clang_getCursorDisplayName (cr)));

		if (clang_Cursor_isVariadic (cr)) {
			printf ("  \"variadic\" : true,\n");
		}

		printf ("  \"kind\" : \"%s\",\n", kind_to_txt (cr));

		CXSourceLocation  loc = clang_getCursorLocation (cr);
		CXFile file; unsigned line, col, off;
		clang_getFileLocation (loc, &file, &line, &col, &off);

		printf ("  \"src\" : \"%s:%d\",\n",
				clang_getCString (clang_getFileName (file)), line);

		printf ("  \"doc\" : \"%s\"\n",
				escape_json (clang_getCString (clang_FullComment_getAsHTML (c))).c_str ());
		printf ("},\n");
	}
	return CXChildVisit_Recurse;
}

static void
process_file (int argc, char **args, const char *fn)
{
	CXIndex index = clang_createIndex (0, 0);
	CXTranslationUnit tu = clang_createTranslationUnitFromSourceFile (index, fn, argc, args, 0, 0);

	if (tu == NULL) {
		fprintf (stderr, "Cannot create translation unit for src: %s\n", fn);
		return;
	}

	clang_visitChildren (clang_getTranslationUnitCursor (tu), traverse, 0);

	clang_disposeTranslationUnit (tu);
	clang_disposeIndex (index);
}

static void
usage (int status)
{
	printf ("doxy2json - extract doxygen doc from C++ headers.\n\n");
	fprintf (stderr, "Usage: dox2json [-I path]* <filename> [filename]*\n");
	exit (status);
}

int main (int argc, char **argv)
{
	int cnt = 2;
	char **args = (char**) malloc (cnt * sizeof (char*));
	args[0] = strdup ("-x");
	args[1] = strdup ("c++");
  int c;
	while (EOF != (c = getopt (argc, argv, "I:"))) {
		switch (c) {
			case 'I':
				args = (char**) realloc (args, (cnt + 2) * sizeof (char*));
				args[cnt++] = strdup ("-I");
				args[cnt++] = strdup (optarg);
				break;
			case 'h':
				usage (0);
			default:
				usage (EXIT_FAILURE);
				break;
		}
	}

	if (optind >= argc) {
		usage (EXIT_FAILURE);
	}

	printf ("[\n");
	for (int i = optind; i < argc; ++i) {
		process_file (cnt, args, argv[i]);
	}
	printf ("{} ]\n");

	for (int i = 0; i < cnt; ++i) {
		free (args[i]);
	}
	free (args);

  return 0;
}