Debian Codegraph @

addon/doxyapp/doxyapp.cpp

raw
1 /******************************************************************************
2 *
3 * Copyright (C) 1997-2015 by Dimitri van Heesch.
4 *
5 * Permission to use, copy, modify, and distribute this software and its
6 * documentation under the terms of the GNU General Public License is hereby
7 * granted. No representations are made about the suitability of this software
8 * for any purpose. It is provided "as is" without express or implied warranty.
9 * See the GNU General Public License for more details.
10 *
11 * Documents produced by Doxygen are derivative works derived from the
12 * input used in their production; they are not affected by this license.
13 *
14 */
15
16 /** @file
17 * @brief Example of how to use doxygen as part of another GPL applications
18 *
19 * This example shows how to configure and run doxygen programmatically from
20 * within an application without generating the usual output.
21 *
22 * If you are running a non-UNIX-like system you should specify `--tempdir`.
23 *
24 * This example shows how to use to code parser to get cross-references information
25 * and it also shows how to look up symbols in a program parsed by doxygen and
26 * show some information about them.
27 */
28
29 #include <stdlib.h>
30 #include <map>
31 #include <string>
32 #include "dir.h"
33 #include "doxygen.h"
34 #include "outputgen.h"
35 #include "outputlist.h"
36 #include "parserintf.h"
37 #include "classdef.h"
38 #include "namespacedef.h"
39 #include "filedef.h"
40 #include "util.h"
41 #include "classlist.h"
42 #include "config.h"
43 #include "filename.h"
44 #include "version.h"
45
46 class XRefDummyCodeGenerator : public OutputCodeIntf
47 {
48 public:
49 XRefDummyCodeGenerator(FileDef *fd) : m_fd(fd) {}
50 ~XRefDummyCodeGenerator() {}
51
52 // these are just null functions, they can be used to produce a syntax highlighted
53 // and cross-linked version of the source code, but who needs that anyway ;-)
54 OutputType type() const override { return OutputType::Extension; }
55 std::unique_ptr<OutputCodeIntf> clone() override { return std::make_unique<XRefDummyCodeGenerator>(m_fd); }
56 void codify(const QCString &) override {}
57 void stripCodeComments(bool) override {}
58 void startSpecialComment() override {}
59 void endSpecialComment() override {}
60 void setStripIndentAmount(size_t) override {}
61 void writeCodeLink(CodeSymbolType,const QCString &,const QCString &,const QCString &,const QCString &,const QCString &) override {}
62 void writeLineNumber(const QCString &,const QCString &,const QCString &,int,bool) override {}
63 virtual void writeTooltip(const QCString &,const DocLinkInfo &,
64 const QCString &,const QCString &,const SourceLinkInfo &,
65 const SourceLinkInfo &) override {}
66 void startCodeLine(int) override {}
67 void endCodeLine() override {}
68 void startFontClass(const QCString &) override {}
69 void endFontClass() override {}
70 void writeCodeAnchor(const QCString &) override {}
71 void startCodeFragment(const QCString &) override {}
72 void endCodeFragment(const QCString &) override {}
73 void startFold(int,const QCString &,const QCString &) override {}
74 void endFold() override {}
75
76 // here we are presented with the symbols found by the code parser
77 void linkableSymbol(int l, const char *sym,Definition *symDef,Definition *context)
78 {
79 QCString ctx;
80 if (context) // the context of the symbol is known
81 {
82 if (context->definitionType()==Definition::TypeMember) // it is inside a member
83 {
84 Definition *parentContext = context->getOuterScope();
85 if (parentContext && parentContext->definitionType()==Definition::TypeClass)
86 // it is inside a member of a class
87 {
88 ctx.sprintf("inside %s %s of %s %s",
89 (dynamic_cast<MemberDef*>(context))->memberTypeName().data(),
90 context->name().data(),
91 (dynamic_cast<ClassDef*>(parentContext))->compoundTypeString().data(),
92 parentContext->name().data());
93 }
94 else if (parentContext==Doxygen::globalScope) // it is inside a global member
95 {
96 ctx.sprintf("inside %s %s",
97 (dynamic_cast<MemberDef*>(context))->memberTypeName().data(),
98 context->name().data());
99 }
100 }
101 if (ctx.isEmpty()) // it is something else (class, or namespace member, ...)
102 {
103 ctx.sprintf("in %s",context->name().data());
104 }
105 }
106 printf("Found symbol %s at line %d of %s %s\n",
107 sym,l,m_fd->getDefFileName().data(),ctx.data());
108 if (symDef && context) // in this case the definition of the symbol is
109 // known to doxygen.
110 {
111 printf("-> defined at line %d of %s\n",
112 symDef->getDefLine(),symDef->getDefFileName().data());
113 }
114 }
115 private:
116 FileDef *m_fd;
117 };
118
119 static void findXRefSymbols(FileDef *fd)
120 {
121 // get the interface to a parser that matches the file extension
122 auto intf=Doxygen::parserManager->getCodeParser(fd->getDefFileExtension());
123
124 // get the programming language from the file name
125 SrcLangExt lang = getLanguageFromFileName(fd->name());
126
127 // reset the parsers state
128 intf->resetCodeParserState();
129
130 // create a new backend object
131 std::unique_ptr<OutputCodeIntf> xrefGen = std::make_unique<XRefDummyCodeGenerator>(fd);
132 OutputCodeList xrefList;
133 xrefList.add(std::move(xrefGen));
134
135 // parse the source code
136 intf->parseCode(xrefList,
137 QCString(),
138 fileToString(fd->absFilePath()),
139 lang,
140 FALSE,
141 FALSE,
142 QCString(),
143 fd);
144 }
145
146 static void listSymbol(Definition *d)
147 {
148 if (d!=Doxygen::globalScope && // skip the global namespace symbol
149 d->name().at(0)!='@' // skip anonymous stuff
150 )
151 {
152 printf("%s\n",
153 d->name().data());
154 }
155 }
156
157 static void listSymbols()
158 {
159 for (const auto &kv : *Doxygen::symbolMap)
160 {
161 for (const auto &def : kv.second)
162 {
163 listSymbol(def);
164 }
165 }
166 }
167
168 static void lookupSymbol(const Definition *d)
169 {
170 if (d!=Doxygen::globalScope && // skip the global namespace symbol
171 d->name().at(0)!='@' // skip anonymous stuff
172 )
173 {
174 printf("Symbol info\n");
175 printf("-----------\n");
176 printf("Name: %s\n",d->name().data());
177 printf("File: %s\n",d->getDefFileName().data());
178 printf("Line: %d\n",d->getDefLine());
179 // depending on the definition type we can case to the appropriate
180 // derived to get additional information
181 switch (d->definitionType())
182 {
183 case Definition::TypeClass:
184 {
185 const ClassDef *cd = dynamic_cast<const ClassDef*>(d);
186 printf("Kind: %s\n",cd->compoundTypeString().data());
187 }
188 break;
189 case Definition::TypeFile:
190 {
191 const FileDef *fd = dynamic_cast<const FileDef*>(d);
192 printf("Kind: File: #includes %zu other files\n",
193 fd->includeFileList().size());
194 }
195 break;
196 case Definition::TypeNamespace:
197 {
198 const NamespaceDef *nd = dynamic_cast<const NamespaceDef*>(d);
199 printf("Kind: Namespace: contains %zu classes and %zu namespaces\n",
200 nd->getClasses().size(),
201 nd->getNamespaces().size());
202 }
203 break;
204 case Definition::TypeMember:
205 {
206 const MemberDef *md = dynamic_cast<const MemberDef*>(d);
207 printf("Kind: %s\n",md->memberTypeName().data());
208 }
209 break;
210 default:
211 // ignore groups/pages/packages/dirs for now
212 break;
213 }
214 }
215 }
216
217 static void lookupSymbols(const QCString &sym)
218 {
219 if (!sym.isEmpty())
220 {
221 auto range = Doxygen::symbolMap->find(sym);
222 bool found=false;
223 for (const Definition *def : range)
224 {
225 lookupSymbol(def);
226 found=true;
227 }
228 if (!found)
229 {
230 printf("Unknown symbol\n");
231 }
232 }
233 }
234
235 template <typename Iter>
236 std::string join(Iter begin, Iter end, std::string const& separator)
237 {
238 std::ostringstream result;
239 if (begin != end)
240 result << *begin++;
241 while (begin != end)
242 result << separator << *begin++;
243 return result.str();
244 }
245
246 static auto symbolInfo(const Definition *def)
247 {
248 std::map<std::string, int> ret;
249 if (def->hasDocumentation())
250 {
251 if (def->hasBriefDescription())
252 ret["briefLine"] = def->briefLine();
253 ret["docLine"] = def->docLine();
254 }
255 ret["defLine"] = def->getDefLine();
256 ret["defColumn"] = def->getDefColumn();
257 ret["startDefLine"] = def->getStartDefLine();
258 ret["startBodyLine"] = def->getStartBodyLine();
259 ret["endBodyLine"] = def->getEndBodyLine();
260 ret["inbodyLine"] = def->inbodyLine();
261 return ret;
262 }
263
264 static void locateSymbols()
265 {
266 std::map<std::string, std::map<std::string, std::map<std::string, std::map<std::string, int>>>> ret;
267 for (const auto &kv : *Doxygen::symbolMap)
268 {
269 for (const auto &def : kv.second)
270 {
271 if (def == Doxygen::globalScope || def->name().at(0) == '@')
272 continue;
273
274 QCString args = "";
275 if (def->definitionType() == Definition::TypeMember)
276 {
277 const auto *md = dynamic_cast<MemberDef*>(def);
278 args = md->argsString();
279 }
280 ret[def->getDefFileName().data()][def->qualifiedName().data()][args.data()] = symbolInfo(def);
281 }
282 }
283
284 // print as json
285 std::vector<std::string> out;
286 for (const auto &[fname, qmap] : ret)
287 {
288 out.push_back(std::string(4, ' ') + "\"" + fname + "\": {\n");
289 std::vector<std::string> file;
290 for (const auto &[qname, arg_map] : qmap)
291 {
292 file.push_back(std::string(8, ' ') + "\"" + qname + "\": {\n");
293 std::vector<std::string> name;
294 for (const auto &[args, imap] : arg_map)
295 {
296 name.push_back(std::string(12, ' ') + "\"" + args + "\": {\n");
297 std::vector<std::string> item;
298 for (const auto &[key, value] : imap)
299 {
300 item.push_back(std::string(16, ' ') + "\"" + key + "\": " + std::to_string(value));
301 }
302 name.back() += join(item.begin(), item.end(), ",\n");
303 name.back() += "\n" + std::string(12, ' ') + "}";
304 }
305 file.back() += join(name.begin(), name.end(), ",\n");
306 file.back() += "\n" + std::string(8, ' ') + "}";
307 }
308 out.back() += join(file.begin(), file.end(), ",\n");
309 out.back() += "\n" + std::string(4, ' ') + "}";
310 }
311 std::cout << "{\n" << join(out.begin(), out.end(), ",\n") << "\n}\n";
312 }
313
314 int main(int argc,char **argv)
315 {
316 std::string tempdir = "/tmp/doxygen";
317 std::string usage = "Usage: %s [--version] [--help] [--list] [--locate] [--tempdir ARG] path [path...]\n";
318 StringVector inputList;
319 bool list = false;
320 bool locate = false;
321
322 for (size_t i = 1; i < argc; i++)
323 {
324 if (std::string(argv[i]) == "--version")
325 {
326 printf("%s version: %s\n",argv[0],getFullVersion().c_str());
327 exit(0);
328 }
329 if (std::string(argv[i]) == "--help")
330 {
331 printf(usage.c_str(), argv[0]);
332 exit(0);
333 }
334 if (std::string(argv[i]) == "--list")
335 {
336 if (locate)
337 {
338 printf(usage.c_str(), argv[0]);
339 exit(1);
340 }
341 list = true;
342 continue;
343 }
344 if (std::string(argv[i]) == "--locate")
345 {
346 if (list)
347 {
348 printf(usage.c_str(), argv[0]);
349 exit(1);
350 }
351 locate = true;
352 continue;
353 }
354 if (std::string(argv[i]) == "--tempdir")
355 {
356 if (i+1 >= argc)
357 {
358 printf(usage.c_str(), argv[0]);
359 exit(1);
360 }
361 tempdir = argv[i+1];
362 i++;
363 continue;
364 }
365 inputList.push_back(argv[i]);
366 }
367
368 // initialize data structures
369 initDoxygen();
370
371 // setup the non-default configuration options
372 checkConfiguration();
373 adjustConfiguration();
374 // we need a place to put intermediate files
375 Config_updateString(OUTPUT_DIRECTORY, tempdir.c_str());
376 // disable html output
377 Config_updateBool(GENERATE_HTML,FALSE);
378 // disable latex output
379 Config_updateBool(GENERATE_LATEX,FALSE);
380 // be quiet
381 Config_updateBool(QUIET,TRUE);
382 // turn off warnings
383 Config_updateBool(WARNINGS,FALSE);
384 Config_updateBool(WARN_IF_UNDOCUMENTED,FALSE);
385 Config_updateBool(WARN_IF_DOC_ERROR,FALSE);
386 Config_updateBool(WARN_IF_UNDOC_ENUM_VAL,FALSE);
387 // Extract as much as possible
388 Config_updateBool(EXTRACT_ALL,TRUE);
389 Config_updateBool(EXTRACT_STATIC,TRUE);
390 Config_updateBool(EXTRACT_PRIVATE,TRUE);
391 Config_updateBool(EXTRACT_LOCAL_METHODS,TRUE);
392 // Extract source browse information, needed
393 // to make doxygen gather the cross reference info
394 Config_updateBool(SOURCE_BROWSER,TRUE);
395 // In case of a directory take all files on directory and its subdirectories
396 Config_updateBool(RECURSIVE,TRUE);
397
398 // set the input
399 Config_updateList(INPUT,inputList);
400
401 // parse the files
402 parseInput();
403
404 // iterate over the input files
405 for (const auto &fn : *Doxygen::inputNameLinkedMap)
406 {
407 for (const auto &fd : *fn)
408 {
409 // get the references (linked and unlinked) found in this file
410 findXRefSymbols(fd.get());
411 }
412 }
413
414 // clean up after us
415 Dir().rmdir(tempdir.c_str());
416
417 if (list)
418 {
419 listSymbols();
420 exit(0);
421 }
422
423 if (locate)
424 {
425 locateSymbols();
426 exit(0);
427 }
428
429 char cmd[256];
430 while (1)
431 {
432 printf("> Type a symbol name or\n> .list for a list of symbols or\n> .quit to exit\n> ");
433 (void)fgets(cmd,256,stdin);
434 QCString s(cmd);
435 if (s.at(s.length()-1)=='\n') s=s.left(s.length()-1); // strip trailing \n
436 if (s==".list")
437 listSymbols();
438 else if (s==".locate")
439 locateSymbols();
440 else if (s==".quit")
441 exit(0);
442 else
443 lookupSymbols(s);
444 }
445 }
446