consoleDoc.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "console/console.h"
  24. #include "core/tAlgorithm.h"
  25. #include "core/strings/findMatch.h"
  26. #include "console/consoleInternal.h"
  27. #include "console/consoleObject.h"
  28. #include "core/stream/fileStream.h"
  29. #include "core/frameAllocator.h"
  30. #include "console/engineAPI.h"
  31. //--- Information pertaining to this page... ------------------
  32. /// @file
  33. ///
  34. /// For specifics on using the consoleDoc functionality, see @ref console_autodoc
  35. ConsoleFunctionGroupBegin(ConsoleDoc, "Console self-documentation functions. These output psuedo C++ suitable for feeeding through Doxygen or another auto documentation tool.");
  36. DefineEngineFunction( dumpConsoleClasses, void, (bool dumpScript, bool dumpEngine), ( true, true ),
  37. "@brief Dumps all declared console classes to the console.\n\n"
  38. "@param dumpScript Optional parameter specifying whether or not classes defined in script should be dumped.\n"
  39. "@param dumpEngine Optional parameter specifying whether or not classes defined in the engine should be dumped.\n"
  40. "@ingroup Logging")
  41. {
  42. Namespace::dumpClasses( dumpScript, dumpEngine );
  43. }
  44. DefineEngineFunction(dumpConsoleFunctions, void, ( bool dumpScript, bool dumpEngine ), ( true, true ),
  45. "@brief Dumps all declared console functions to the console.\n"
  46. "@param dumpScript Optional parameter specifying whether or not functions defined in script should be dumped.\n"
  47. "@param dumpEngine Optional parameter specitying whether or not functions defined in the engine should be dumped.\n"
  48. "@ingroup Logging")
  49. {
  50. Namespace::dumpFunctions( dumpScript, dumpEngine );
  51. }
  52. ConsoleFunctionGroupEnd(ConsoleDoc);
  53. /// Helper table to convert type ids to human readable names.
  54. const char *typeNames[] =
  55. {
  56. "Script",
  57. "string",
  58. "int",
  59. "float",
  60. "void",
  61. "bool",
  62. "",
  63. "",
  64. "unknown_overload"
  65. };
  66. void printClassHeader(const char* usage, const char * className, const char * superClassName, const bool stub)
  67. {
  68. if(stub)
  69. {
  70. Con::printf("/// Stub class");
  71. Con::printf("/// ");
  72. Con::printf("/// @note This is a stub class to ensure a proper class hierarchy. No ");
  73. Con::printf("/// information was available for this class.");
  74. }
  75. if((usage != NULL) && strlen(usage))
  76. {
  77. // Copy Usage Document
  78. S32 usageLen = dStrlen( usage ) + 1;
  79. FrameTemp<char> usageStr( usageLen );
  80. dStrcpy( usageStr, usage, usageLen );
  81. // Print Header
  82. Con::printf( "/*!" );
  83. // Print line by line, skipping the @field lines.
  84. //
  85. // fetch first line end
  86. char *newLine = dStrchr( usageStr, '\n' );
  87. char *usagePtr = usageStr;
  88. do
  89. {
  90. // Copy of one line
  91. static char lineStr[2048] = {0};
  92. // Keyword will hold the last keyword (word following '@' or '\') encountered.
  93. static char keyword[8] = {0};
  94. S32 lineLen = 0;
  95. // If not the last line, increment pointer
  96. if( newLine != NULL )
  97. {
  98. *newLine = '\0';
  99. newLine ++;
  100. }
  101. // Copy line and update usagePtr
  102. dStrcpy( lineStr, usagePtr, 2048 );
  103. usagePtr = (newLine != NULL ) ? newLine : usagePtr;
  104. lineLen = dStrlen( lineStr );
  105. // Get the keyword. This is the first word after an '@' or '\'.
  106. const char* tempkw = dStrchr( lineStr, '@' );
  107. if( !tempkw )
  108. tempkw = dStrchr( lineStr, '\\' );
  109. // If we found a new keyword, set it, otherwise, keep using the
  110. // most recently found.
  111. if( tempkw )
  112. {
  113. dStrncpy( keyword, tempkw + 1, 5 );
  114. keyword[5] = '\0';
  115. }
  116. // Print all fields that aren't associated with the 'field' keyword.
  117. if( String::compare( keyword, "field" ) )
  118. Con::printf( "%s", lineStr ); // print lineStr as an unformatted string (otherwise '%' characters in the string could cause problems)
  119. // Fetch next line ending
  120. newLine = dStrchr( usagePtr, '\n' );
  121. } while( newLine != NULL );
  122. // DocBlock Footer
  123. Con::printf( " */" );
  124. }
  125. // Print out appropriate class header
  126. if(superClassName)
  127. Con::printf("class %s : public %s {", className, superClassName ? superClassName : "");
  128. else if(!className)
  129. Con::printf("namespace Global {");
  130. else
  131. Con::printf("class %s {", className);
  132. if(className)
  133. Con::printf(" public:");
  134. }
  135. void printClassMethod(const bool isVirtual, const char *retType, const char *methodName, const char* args, const char*usage)
  136. {
  137. if(usage && usage[0] != ';' && usage[0] != 0)
  138. Con::printf(" /*! %s */", usage);
  139. Con::printf(" %s%s %s(%s) {}", isVirtual ? "virtual " : "", retType, methodName, args);
  140. }
  141. void printGroupStart(const char * aName, const char * aDocs)
  142. {
  143. Con::printf("");
  144. Con::printf(" /*! @name %s", aName);
  145. if(aDocs)
  146. {
  147. Con::printf(" ");
  148. Con::printf(" %s", aDocs);
  149. }
  150. Con::printf(" @{ */");
  151. // Add a blank comment in order to make sure groups are parsed properly.
  152. Con::printf(" /*! */");
  153. }
  154. void printClassMember(const bool isDeprec, const char * aType, const char * aName, const char * aDocs, S32 aElementCount)
  155. {
  156. Con::printf(" /*!");
  157. if(aDocs)
  158. {
  159. Con::printf(" %s", aDocs);
  160. Con::printf(" ");
  161. }
  162. if(isDeprec)
  163. Con::printf(" @deprecated This member is deprecated, which means that its value is always undefined.");
  164. Con::printf(" */");
  165. if (aElementCount == 1)
  166. {
  167. Con::printf(" %s %s;", isDeprec ? "deprecated" : aType, aName);
  168. }
  169. else
  170. {
  171. Con::printf(" %s %s[%i];", isDeprec ? "deprecated" : aType, aName, aElementCount);
  172. }
  173. }
  174. void printGroupEnd()
  175. {
  176. Con::printf(" /// @}");
  177. Con::printf("");
  178. }
  179. void printClassFooter()
  180. {
  181. Con::printf("};");
  182. Con::printf("");
  183. }
  184. void Namespace::printNamespaceEntries(Namespace * g, bool dumpScript, bool dumpEngine )
  185. {
  186. static bool inGroup = false;
  187. // Go through all the entries.
  188. // Iterate through the methods of the namespace...
  189. for(Entry *ewalk = g->mEntryList; ewalk; ewalk = ewalk->mNext)
  190. {
  191. S32 eType = ewalk->mType;
  192. const char * funcName = ewalk->mFunctionName;
  193. if( ( eType == Entry::ConsoleFunctionType ) && !dumpScript )
  194. continue;
  195. if( ( eType != Entry::ConsoleFunctionType ) && !dumpEngine )
  196. continue;
  197. // If it's a function
  198. if( eType >= Entry::ConsoleFunctionType )
  199. {
  200. if (ewalk->mHeader != NULL)
  201. {
  202. // The function was defined with types, so we can print out the actual return type
  203. printClassMethod(true, ewalk->mHeader->mReturnString, funcName, ewalk->getArgumentsString().c_str(),
  204. ewalk->getDocString().c_str());
  205. }
  206. else
  207. {
  208. printClassMethod(true, typeNames[eType], funcName, (ewalk->getArgumentsString() + "...").c_str(),
  209. ewalk->getDocString().c_str());
  210. }
  211. }
  212. else if(ewalk->mType == Entry::GroupMarker)
  213. {
  214. if(!inGroup)
  215. printGroupStart(ewalk->cb.mGroupName, ewalk->mUsage);
  216. else
  217. printGroupEnd();
  218. inGroup = !inGroup;
  219. }
  220. else if(ewalk->mType == Entry::ScriptCallbackType)
  221. {
  222. // It's a script callback - emit some sort of appropriate info.
  223. Con::printf(" /*! %s */", ewalk->getDocString().c_str());
  224. Con::printf(" %s;", ewalk->getPrototypeString().c_str());
  225. Con::printf("");
  226. }
  227. else if(ewalk->mFunctionOffset) // If it's a builtin function...
  228. {
  229. String args = ewalk->mModule->getFunctionArgs(ewalk->mFunctionName, ewalk->mFunctionOffset);
  230. printClassMethod(false, typeNames[ewalk->mType], ewalk->mFunctionName, args, "");
  231. }
  232. else
  233. {
  234. Con::printf(" // got an unknown thing?? %d", ewalk->mType );
  235. }
  236. }
  237. }
  238. void Namespace::dumpClasses( bool dumpScript, bool dumpEngine )
  239. {
  240. VectorPtr<Namespace*> vec;
  241. trashCache();
  242. vec.reserve( 1024 );
  243. // We use mHashSequence to mark if we have traversed...
  244. // so mark all as zero to start.
  245. for(Namespace *walk = mNamespaceList; walk; walk = walk->mNext)
  246. walk->mHashSequence = 0;
  247. for(Namespace *walk = mNamespaceList; walk; walk = walk->mNext)
  248. {
  249. VectorPtr<Namespace*> stack;
  250. stack.reserve( 1024 );
  251. // Get all the parents of this namespace... (and mark them as we go)
  252. Namespace *parentWalk = walk;
  253. while(parentWalk)
  254. {
  255. if(parentWalk->mHashSequence != 0)
  256. break;
  257. if(parentWalk->mPackage == 0)
  258. {
  259. parentWalk->mHashSequence = 1; // Mark as traversed.
  260. stack.push_back(parentWalk);
  261. }
  262. parentWalk = parentWalk->mParent;
  263. }
  264. // Load stack into our results vector.
  265. while(stack.size())
  266. {
  267. vec.push_back(stack[stack.size() - 1]);
  268. stack.pop_back();
  269. }
  270. }
  271. // Go through previously discovered classes
  272. U32 i;
  273. for(i = 0; i < vec.size(); i++)
  274. {
  275. const char *className = vec[i]->mName;
  276. const char *superClassName = vec[i]->mParent ? vec[i]->mParent->mName : NULL;
  277. // Skip the global namespace, that gets dealt with in dumpFunctions
  278. if(!className) continue;
  279. // If we're just dumping script functions, then we don't want to dump
  280. // a class that only contains script functions. So, we iterate over all
  281. // the functions.
  282. if( !dumpScript )
  283. {
  284. bool found = false;
  285. for(Entry *ewalk = vec[i]->mEntryList; ewalk; ewalk = ewalk->mNext)
  286. {
  287. if( ewalk->mType != Entry::ConsoleFunctionType )
  288. {
  289. found = true;
  290. break;
  291. }
  292. }
  293. // If we don't have engine functions and the namespace name
  294. // doesn't match the class name... then its a script class.
  295. if ( !found && !vec[i]->isClass() )
  296. continue;
  297. }
  298. // And we do the same for engine functions.
  299. if( !dumpEngine )
  300. {
  301. bool found = false;
  302. for(Entry *ewalk = vec[i]->mEntryList; ewalk; ewalk = ewalk->mNext)
  303. {
  304. if( ewalk->mType == Entry::ConsoleFunctionType )
  305. {
  306. found = true;
  307. break;
  308. }
  309. }
  310. if( !found )
  311. continue;
  312. }
  313. // If we hit a class with no members and no classRep, do clever filtering.
  314. if(vec[i]->mEntryList == NULL && vec[i]->mClassRep == NULL)
  315. {
  316. // Print out a short stub so we get a proper class hierarchy.
  317. if(superClassName) { // Filter hack; we don't want non-inheriting classes...
  318. printClassHeader( NULL, className,superClassName, true);
  319. printClassFooter();
  320. }
  321. continue;
  322. }
  323. // Print the header for the class..
  324. printClassHeader(vec[i]->mUsage, className, superClassName, false);
  325. // Deal with entries.
  326. printNamespaceEntries(vec[i], dumpScript, dumpEngine);
  327. // Deal with the classRep (to get members)...
  328. AbstractClassRep *rep = vec[i]->mClassRep;
  329. AbstractClassRep::FieldList emptyList;
  330. AbstractClassRep::FieldList *parentList = &emptyList;
  331. AbstractClassRep::FieldList *fieldList = &emptyList;
  332. // Since all fields are defined in the engine, if we're not dumping
  333. // engine stuff, than we shouldn't dump the fields.
  334. if(dumpEngine && rep)
  335. {
  336. // Get information about the parent's fields...
  337. AbstractClassRep *parentRep = vec[i]->mParent ? vec[i]->mParent->mClassRep : NULL;
  338. if(parentRep)
  339. parentList = &(parentRep->mFieldList);
  340. // Get information about our fields
  341. fieldList = &(rep->mFieldList);
  342. // Go through all our fields...
  343. for(U32 j = 0; j < fieldList->size(); j++)
  344. {
  345. switch((*fieldList)[j].type)
  346. {
  347. case AbstractClassRep::StartArrayFieldType:
  348. case AbstractClassRep::EndArrayFieldType:
  349. break;
  350. case AbstractClassRep::StartGroupFieldType:
  351. printGroupStart((*fieldList)[j].pGroupname, (*fieldList)[j].pFieldDocs);
  352. break;
  353. case AbstractClassRep::EndGroupFieldType:
  354. printGroupEnd();
  355. break;
  356. default:
  357. case AbstractClassRep::DeprecatedFieldType:
  358. {
  359. // Skip over fields that are already defined in
  360. // our parent class.
  361. if ( parentRep && parentRep->findField( (*fieldList)[j].pFieldname ) )
  362. continue;
  363. bool isDeprecated = ((*fieldList)[j].type == AbstractClassRep::DeprecatedFieldType);
  364. if(isDeprecated)
  365. {
  366. printClassMember(
  367. true,
  368. "<deprecated>",
  369. (*fieldList)[j].pFieldname,
  370. (*fieldList)[j].pFieldDocs,
  371. (*fieldList)[j].elementCount
  372. );
  373. }
  374. else
  375. {
  376. ConsoleBaseType *cbt = ConsoleBaseType::getType((*fieldList)[j].type);
  377. printClassMember(
  378. false,
  379. cbt ? cbt->getTypeClassName() : "<unknown>",
  380. (*fieldList)[j].pFieldname,
  381. (*fieldList)[j].pFieldDocs,
  382. (*fieldList)[j].elementCount
  383. );
  384. }
  385. }
  386. }
  387. }
  388. }
  389. if( dumpScript )
  390. {
  391. // Print out fields defined in script docs for this namespace.
  392. // These fields are specified by the 'field' keyword in the usage
  393. // string.
  394. // The field type and name.
  395. char fieldName[256];
  396. char fieldDoc[1024];
  397. // Usage string iterator.
  398. const char* field = vec[i]->mUsage;
  399. while( field )
  400. {
  401. // Find the first field keyword.
  402. const char* tempField = dStrstr( field, "@field" );
  403. if( !tempField )
  404. tempField = dStrstr( field, "\\field" );
  405. field = tempField;
  406. if( !field )
  407. break;
  408. // Move to the field name.
  409. field += 7;
  410. // Copy the field type and name. These should both be followed by a
  411. // space so only in this case will we actually store it.
  412. S32 spaceCount = 0;
  413. S32 index = 0;
  414. bool valid = false;
  415. while( field && ( *field != '\n' ) )
  416. {
  417. if( index >= 255 )
  418. break;
  419. if( *field == ' ' )
  420. spaceCount++;
  421. if( spaceCount == 2 )
  422. {
  423. valid = true;
  424. break;
  425. }
  426. fieldName[index++] = *field;
  427. field++;
  428. }
  429. if( !valid )
  430. continue;
  431. fieldName[index] = '\0';
  432. // Now copy from field to the next keyword.
  433. const char* nextKeyword = dStrchr( field, '@' );
  434. if( !nextKeyword )
  435. nextKeyword = dStrchr( field, '\\' );
  436. // Grab the length of the doc string.
  437. S32 docLen = dStrlen( field );
  438. if( nextKeyword )
  439. docLen = nextKeyword - field;
  440. // Make sure it will fit in the buffer.
  441. if( docLen > 1023 )
  442. docLen = 1023;
  443. // Copy.
  444. dStrncpy( fieldDoc, field, docLen );
  445. fieldDoc[docLen] = '\0';
  446. field += docLen;
  447. // Print
  448. Con::printf( " /*!" );
  449. Con::printf( " %s", fieldDoc );
  450. Con::printf( " */" );
  451. Con::printf( " %s;", fieldName );
  452. }
  453. }
  454. // Close the class/namespace.
  455. printClassFooter();
  456. }
  457. }
  458. void Namespace::dumpFunctions( bool dumpScript, bool dumpEngine )
  459. {
  460. // Get the global namespace.
  461. Namespace* g = find(NULL); //->mParent;
  462. printClassHeader(NULL, NULL,NULL, false);
  463. while(g)
  464. {
  465. printNamespaceEntries(g, dumpScript, dumpEngine );
  466. g = g->mParent;
  467. }
  468. printClassFooter();
  469. }