Debian Codegraph @

addon/doxyparse/doxyparse.cpp

raw
1 /******************************************************************************
2 *
3 * Copyright (C) 2009-2015 by Joenio Costa.
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 Code parse based on doxyapp by Dimitri van Heesch
18 *
19 */
20
21 #if !defined(_WIN32) || defined(__CYGWIN__)
22 #include <unistd.h>
23 #else
24 #include <windows.h>
25 #endif
26 #include "version.h"
27 #include "doxygen.h"
28 #include "outputgen.h"
29 #include "outputlist.h"
30 #include "parserintf.h"
31 #include "classlist.h"
32 #include "config.h"
33 #include "filedef.h"
34 #include "util.h"
35 #include "filename.h"
36 #include "arguments.h"
37 #include "memberlist.h"
38 #include "types.h"
39 #include <string>
40 #include <cstdlib>
41 #include <sstream>
42 #include <map>
43 #include <algorithm>
44 #include <filesystem>
45 #include "qcstring.h"
46 #include "namespacedef.h"
47 #include "portable.h"
48 #include "dir.h"
49
50 class Doxyparse : public OutputCodeIntf
51 {
52 public:
53 Doxyparse(const FileDef *fd) : m_fd(fd) {}
54 ~Doxyparse() {}
55
56 // these are just null functions, they can be used to produce a syntax highlighted
57 // and cross-linked version of the source code, but who needs that anyway ;-)
58 OutputType type() const override { return OutputType::Extension; }
59 std::unique_ptr<OutputCodeIntf> clone() override { return std::make_unique<Doxyparse>(m_fd); }
60 void codify(const QCString &) override {}
61 void stripCodeComments(bool) override {}
62 void startSpecialComment() override {}
63 void endSpecialComment() override {}
64 void setStripIndentAmount(size_t) override {}
65 void writeCodeLink(CodeSymbolType,const QCString &,const QCString &,const QCString &,const QCString &,const QCString &) override {}
66 void startCodeLine(int) override {}
67 void endCodeLine() override {}
68 void writeCodeAnchor(const QCString &) override {}
69 void startFontClass(const QCString &) override {}
70 void endFontClass() override {}
71 void writeLineNumber(const QCString &,const QCString &,const QCString &,int,bool) override {}
72 virtual void writeTooltip(const QCString &,const DocLinkInfo &,
73 const QCString &,const QCString &,const SourceLinkInfo &,
74 const SourceLinkInfo &) override {}
75 void startCodeFragment(const QCString &) override {}
76 void endCodeFragment(const QCString &) override {}
77 void startFold(int,const QCString &,const QCString &) override {}
78 void endFold() override {}
79
80 void linkableSymbol(int l, const char *sym, Definition *symDef, Definition *context)
81 {
82 if (!symDef) {
83 // in this case we have a local or external symbol
84
85 // TODO record use of external symbols
86 // TODO must have a way to differentiate external symbols from local variables
87 }
88 }
89
90 private:
91 const FileDef *m_fd;
92 };
93
94 static bool is_c_code = true;
95
96 static void findXRefSymbols(FileDef *fd)
97 {
98 // get the interface to a parser that matches the file extension
99 auto intf=Doxygen::parserManager->getCodeParser(fd->getDefFileExtension());
100
101 // get the programming language from the file name
102 SrcLangExt lang = getLanguageFromFileName(fd->name());
103
104 // reset the parsers state
105 intf->resetCodeParserState();
106
107 // create a new backend object
108 std::unique_ptr<OutputCodeIntf> parse = std::make_unique<Doxyparse>(fd);
109 OutputCodeList parseList;
110 parseList.add(std::move(parse));
111
112 // parse the source code
113 intf->parseCode(parseList, QCString(), fileToString(fd->absFilePath()), lang,
114 FALSE, FALSE, QCString(), fd);
115 }
116
117 static bool ignoreStaticExternalCall(const MemberDef *context, const MemberDef *md) {
118 if (!md->isStatic()) return false;
119 if (!md->getFileDef() || !context->getFileDef()) return false;
120
121 // TODO ignore prefix of file
122 return md->getFileDef()->getOutputFileBase() != context->getFileDef()->getOutputFileBase();
123 }
124
125 static void startYamlDocument() {
126 printf("---\n");
127 }
128 static void printFile(std::string file) {
129 printf("%s:\n", file.c_str());
130 }
131 static void printModule(std::string module) {
132 printf(" \"%s\":\n", unescapeCharsInString(module.c_str()).data());
133 }
134 static void printClassInformation(std::string information) {
135 printf(" information: %s\n", information.c_str());
136 }
137 static void printInherits() {
138 printf(" inherits:\n");
139 }
140 static void printInheritance(std::string base_class) {
141 printf(" - \"%s\"\n", base_class.c_str());
142 }
143 static void printDefines() {
144 printf(" defines:\n");
145 }
146 static void printDefinition(std::string type, std::string signature, int line) {
147 printf(" - \"%s\":\n", signature.substr(0, 1022).c_str());
148 printf(" type: %s\n", type.c_str());
149 printf(" line: %d\n", line);
150 }
151 static void printProtection(std::string protection) {
152 printf(" protection: %s\n", protection.c_str());
153 }
154 static void printPrototypeYes() {
155 printf(" prototype: yes\n");
156 }
157 static void printNumberOfLines(int lines) {
158 printf(" lines_of_code: %d\n", lines);
159 }
160 static void printNumberOfArguments(size_t arguments) {
161 printf(" parameters: %zu\n", arguments);
162 }
163 static void printUses() {
164 printf(" uses:\n");
165 }
166 static void printReferenceTo(std::string type, std::string signature, std::string defined_in) {
167 printf(" - \"%s\":\n", signature.substr(0, 1022).c_str());
168 printf(" type: %s\n", type.c_str());
169 printf(" defined_in: \"%s\"\n", unescapeCharsInString(defined_in.c_str()).data());
170 }
171 static void printNumberOfConditionalPaths(const MemberDef* md) {
172 printf(" conditional_paths: %d\n", md->numberOfFlowKeyWords());
173 }
174
175 static int isPartOfCStruct(const MemberDef * md) {
176 return is_c_code && md->getClassDef() != nullptr;
177 }
178
179 [[nodiscard]] std::string sanitizeString(std::string data)
180 {
181 data.erase(std::remove_if(data.begin(), data.end(), [](unsigned char c) {
182 return c == '"' || c == '\'';
183 }),
184 data.end());
185 return data;
186 }
187
188 std::string argumentData(const Argument &argument) {
189 std::string data = "";
190 if (argument.type.size() > 1)
191 data = sanitizeString(argument.type.data());
192 else if (!argument.name.isEmpty())
193 data = sanitizeString(argument.name.data());
194 return data;
195 }
196
197 std::string functionSignature(const MemberDef* md) {
198 std::string signature = sanitizeString(md->name().data());
199 if(md->isFunction()){
200 const ArgumentList &argList = md->argumentList();
201 signature += "(";
202 auto it = argList.begin();
203 if(it!=argList.end()) {
204 signature += argumentData(*it);
205 for(++it; it!=argList.end(); ++it) {
206 signature += std::string(",") + argumentData(*it);
207 }
208 }
209 signature += ")";
210 }
211 return signature;
212 }
213
214 static void referenceTo(const MemberDef* md) {
215 std::string type = md->memberTypeName().data();
216 std::string defined_in = "";
217 std::string signature = "";
218 if (isPartOfCStruct(md)) {
219 signature = md->getClassDef()->name().data() + std::string("::") + functionSignature(md);
220 defined_in = md->getClassDef()->getFileDef()->getOutputFileBase().data();
221 }
222 else {
223 signature = functionSignature(md);
224 if (md->getClassDef()) {
225 defined_in = md->getClassDef()->name().data();
226 }
227 else if (md->getFileDef()) {
228 defined_in = md->getFileDef()->getOutputFileBase().data();
229 }
230 else if (md->getNamespaceDef()) {
231 defined_in = md->getNamespaceDef()->name().data();
232 }
233 }
234 printReferenceTo(type, signature, defined_in);
235 }
236
237 void protectionInformation(Protection protection) {
238 if (protection == Protection::Public) {
239 printProtection("public");
240 }
241 else if (protection == Protection::Protected) {
242 printProtection("protected");
243 }
244 else if (protection == Protection::Private) {
245 printProtection("private");
246 }
247 else if (protection == Protection::Package) {
248 printProtection("package");
249 }
250 }
251
252 void cModule(const ClassDef* cd) {
253 const MemberList* ml = cd->getMemberList(MemberListType::VariableMembers());
254 if (ml) {
255 const FileDef *fd = cd->getFileDef();
256 const MemberList *fd_ml = fd->getMemberList(MemberListType::AllMembersList());
257 if (!fd_ml || fd_ml->size() == 0) {
258 printModule(fd->getOutputFileBase().data());
259 printDefines();
260 }
261 for (const auto &md : *ml) {
262 printDefinition("variable", cd->name().data() + std::string("::") + md->name().data(), md->getDefLine());
263 protectionInformation(md->protection());
264 }
265 }
266 }
267
268 static bool checkOverrideArg(const ArgumentList &argList, const MemberDef *md) {
269 if(!md->isFunction() || argList.empty()){
270 return false;
271 }
272
273 for (const Argument &argument : argList) {
274 if(md->name() == argument.name) {
275 return true;
276 }
277 }
278
279 return false;
280 }
281
282 void functionInformation(const MemberDef* md) {
283 std::string temp = "";
284 int size = md->getEndBodyLine() - md->getStartBodyLine() + 1;
285 printNumberOfLines(size);
286 const ArgumentList &argList = md->argumentList();
287 if (!argList.empty())
288 {
289 temp = argumentData(argList.front());
290 // TODO: This is a workaround; better not include "void" in argList, in the first place.
291 if (temp!="void")
292 {
293 printNumberOfArguments(argList.size());
294 }
295 }
296
297 printNumberOfConditionalPaths(md);
298 auto refList = md->getReferencesMembers();
299 if (!refList.empty()) {
300 printUses();
301 for (const auto &rmd : refList) {
302 if (rmd->definitionType() == Definition::TypeMember && !ignoreStaticExternalCall(md, rmd) && !checkOverrideArg(argList, rmd)) {
303 referenceTo(rmd);
304 }
305 }
306 }
307 }
308
309 void prototypeInformation(const MemberDef* md) {
310 printPrototypeYes();
311 const ArgumentList &argList = md->argumentList();
312 printNumberOfArguments(argList.size());
313 }
314
315 static void lookupSymbol(const Definition *d) {
316 if (d->definitionType() == Definition::TypeMember) {
317 const MemberDef *md = dynamic_cast<const MemberDef*>(d);
318 std::string type = md->memberTypeName().data();
319 std::string signature = functionSignature(md);
320 printDefinition(type, signature, md->getDefLine());
321 protectionInformation(md->protection());
322 if (md->isFunction() && md->isPrototype()) {
323 prototypeInformation(md);
324 }
325 else if (md->isFunction()) {
326 functionInformation(md);
327 }
328 }
329 }
330
331 void listMembers(const MemberList *ml) {
332 if (ml) {
333 for (const auto &md : *ml) {
334 lookupSymbol((Definition*) md);
335 }
336 }
337 }
338
339 void listAllMembers(const ClassDef* cd) {
340 // methods
341 listMembers(cd->getMemberList(MemberListType::FunctionMembers()));
342 // constructors
343 listMembers(cd->getMemberList(MemberListType::Constructors()));
344 // attributes
345 listMembers(cd->getMemberList(MemberListType::VariableMembers()));
346 }
347
348 static void classInformation(const ClassDef* cd) {
349 if (is_c_code) {
350 cModule(cd);
351 } else {
352 printModule(cd->name().data());
353 if (!cd->baseClasses().empty()) {
354 printInherits();
355 for (const auto &bcd : cd->baseClasses()) {
356 printInheritance(sanitizeString(bcd.classDef->name().data()));
357 }
358 }
359 if(cd->isAbstract()) {
360 printClassInformation("abstract class");
361 }
362 printDefines();
363 listAllMembers(cd);
364 }
365 }
366
367 static bool checkLanguage(std::string& filename, std::string extension) {
368 if (filename.find(extension, filename.size() - extension.size()) != std::string::npos) {
369 return true;
370 } else {
371 return false;
372 }
373 }
374
375 /* Detects the programming language of the project. Actually, we only care
376 * about whether it is a C project or not. */
377 static void detectProgrammingLanguage(FileNameLinkedMap &fnli) {
378 for (const auto &fn : fnli) {
379 std::string filename = fn->fileName().str();
380 if (
381 checkLanguage(filename, ".cc") ||
382 checkLanguage(filename, ".cxx") ||
383 checkLanguage(filename, ".cpp") ||
384 checkLanguage(filename, ".java") ||
385 checkLanguage(filename, ".py") ||
386 checkLanguage(filename, ".pyw") ||
387 checkLanguage(filename, ".cs")
388 ) {
389 is_c_code = false;
390 }
391 }
392 }
393
394 static void listSymbols() {
395 detectProgrammingLanguage(*Doxygen::inputNameLinkedMap);
396
397 // iterate over the input files
398 for (const auto &fn : *Doxygen::inputNameLinkedMap) {
399 for (const auto &fd : *fn) {
400 printFile(fd->absFilePath().data());
401 MemberList *ml = fd->getMemberList(MemberListType::AllMembersList());
402 if (ml && ml->size() > 0) {
403 printModule(fd->getOutputFileBase().data());
404 printDefines();
405 listMembers(ml);
406 }
407
408 ClassDefSet visitedClasses;
409 for (const auto &cd : fd->getClasses()) {
410 if (visitedClasses.find(cd)==visitedClasses.end()) {
411 classInformation(cd);
412 visitedClasses.insert(cd);
413 }
414 }
415 }
416 }
417 // TODO print external symbols referenced
418 }
419
420 int main(int argc,char **argv) {
421 int locArgc = argc;
422
423 if (locArgc == 2)
424 {
425 if (!strcmp(argv[1],"--help"))
426 {
427 printf("Usage: %s [source_file | source_dir]\n",argv[0]);
428 exit(0);
429 }
430 else if (!strcmp(argv[1],"--version"))
431 {
432 printf("%s version: %s\n",argv[0],getFullVersion().c_str());
433 exit(0);
434 }
435 }
436
437 if (locArgc!=2)
438 {
439 printf("Usage: %s [source_file | source_dir]\n",argv[0]);
440 exit(1);
441 }
442
443 // initialize data structures
444 initDoxygen();
445
446 // check and finalize the configuration
447 checkConfiguration();
448 adjustConfiguration();
449
450 // setup the non-default configuration options
451
452 // we need a place to put intermediate files
453 std::ostringstream tmpdir;
454 unsigned int pid = Portable::pid();
455 if (!Portable::getenv("TMP").isEmpty())
456 tmpdir << Portable::getenv("TMP") << "/doxyparse-" << pid;
457 else if (!Portable::getenv("TEMP").isEmpty())
458 tmpdir << Portable::getenv("TEMP") << "/doxyparse-" << pid;
459 else
460 tmpdir << "doxyparse-" << pid;
461
462 Config_updateString(OUTPUT_DIRECTORY,tmpdir.str().c_str());
463 // enable HTML (fake) output to omit warning about missing output format
464 Config_updateBool(GENERATE_HTML,TRUE);
465 // disable latex output
466 Config_updateBool(GENERATE_LATEX,FALSE);
467 // be quiet
468 Config_updateBool(QUIET,TRUE);
469 // turn off warnings
470 Config_updateBool(WARNINGS,FALSE);
471 Config_updateBool(WARN_IF_UNDOCUMENTED,FALSE);
472 Config_updateBool(WARN_IF_DOC_ERROR,FALSE);
473 Config_updateBool(WARN_IF_UNDOC_ENUM_VAL,FALSE);
474 // Extract as much as possible
475 Config_updateBool(EXTRACT_ALL,TRUE);
476 Config_updateBool(EXTRACT_STATIC,TRUE);
477 Config_updateBool(EXTRACT_PRIVATE,TRUE);
478 Config_updateBool(EXTRACT_LOCAL_METHODS,TRUE);
479 Config_updateBool(EXTRACT_PACKAGE,TRUE);
480 // Extract source browse information, needed
481 // to make doxygen gather the cross reference info
482 Config_updateBool(SOURCE_BROWSER,TRUE);
483 // find functions call between modules
484 Config_updateBool(CALL_GRAPH,TRUE);
485 // loop recursive over input files
486 Config_updateBool(RECURSIVE,TRUE);
487 // add file extensions
488 Config_updateList(FILE_PATTERNS, { "*.cc", "*.cxx", "*.cpp", "*.java",
489 "*.py", "*.pyw", "*.cs", "*.c", "*.h", "*.hh", "*.hpp"});
490 // set the input
491 StringVector inputList;
492 for (int i = 1; i < argc; i++) {
493 if (strcmp(argv[i], "-") == 0) {
494 char filename[1024];
495 while (1) {
496 (void)scanf("%s[^\n]", filename);
497 if (feof(stdin)) {
498 break;
499 }
500 inputList.push_back(filename);
501 }
502 } else {
503 inputList.push_back(argv[i]);
504 }
505 }
506 Config_updateList(INPUT,inputList);
507 if (inputList.empty()) {
508 exit(0);
509 }
510
511 // parse the files
512 parseInput();
513
514 // iterate over the input files
515 for (const auto &fn : *Doxygen::inputNameLinkedMap) {
516 for (const auto &fd : *fn) {
517 // get the references (linked and unlinked) found in this file
518 findXRefSymbols(fd.get());
519 }
520 }
521
522 Dir thisDir;
523 // remove temporary files
524 if (!Doxygen::filterDBFileName.isEmpty()) thisDir.remove(Doxygen::filterDBFileName.str());
525
526 // clean up after us
527 thisDir.rmdir(Config_getString(OUTPUT_DIRECTORY).str());
528
529 startYamlDocument();
530 listSymbols();
531
532 std::error_code ec;
533 std::filesystem::remove_all(tmpdir.str(), ec);
534 return 0;
535 }