AutoBinder.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // Copyright (c) 2008-2021 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <clang/ASTMatchers/ASTMatchFinder.h>
  23. #include <clang/Driver/Options.h>
  24. #include <clang/Tooling/CommonOptionsParser.h>
  25. #include <clang/Tooling/Tooling.h>
  26. #include <Mustache/mustache.hpp>
  27. #include <unordered_set>
  28. using namespace clang;
  29. using namespace clang::ast_matchers;
  30. using namespace clang::driver;
  31. using namespace clang::tooling;
  32. using namespace llvm;
  33. static cl::extrahelp commonHelp(CommonOptionsParser::HelpMessage);
  34. static cl::extrahelp moreHelp(
  35. "\tFor example, to run AutoBinder on all files in a subtree of the\n"
  36. "\tsource tree, use:\n"
  37. "\n"
  38. "\t find path/in/substree -name '*.cpp'|xargs AutoBinder -p build/path \\\n"
  39. "\t -t template/path -o output/path -s script0 -s script1\n"
  40. "\n"
  41. "\tNote, that path/in/subtree and current directory should follow the\n"
  42. "\trules described above.\n"
  43. "\n"
  44. "Most probably you want to invoke 'autobinder' built-in target instead of invoking this tool\n"
  45. "directly. The 'autobinder' target invokes this tool in a right context prepared by build system.\n"
  46. "\n"
  47. );
  48. static cl::OptionCategory autobinderCategory("AutoBinder options");
  49. static std::unique_ptr<opt::OptTable> options(createDriverOptTable());
  50. static cl::opt<std::string> templatePath("t", cl::desc("Template path"), cl::cat(autobinderCategory));
  51. static cl::opt<std::string> outputPath("o", cl::desc("Output path"), cl::cat(autobinderCategory));
  52. static cl::list<std::string> scripts("s", cl::desc("Script subsystems"), cl::cat(autobinderCategory));
  53. struct Data
  54. {
  55. std::unordered_set<std::string> symbols_;
  56. };
  57. static const std::string categories_[] = {"class", "enum"};
  58. static std::unordered_map<std::string, Data> categoryData_;
  59. class ExtractCallback : public MatchFinder::MatchCallback
  60. {
  61. public :
  62. virtual void run(const MatchFinder::MatchResult& result)
  63. {
  64. for (auto& i: categories_)
  65. {
  66. auto symbol = result.Nodes.getNodeAs<StringLiteral>(i);
  67. if (symbol)
  68. categoryData_[i].symbols_.insert(symbol->getString());
  69. }
  70. }
  71. virtual void onStartOfTranslationUnit()
  72. {
  73. static unsigned count = sizeof("Extracting") / sizeof(char) - 1;
  74. outs() << '.' << (++count % 100 ? "" : "\n"); // Sending a heart beat
  75. }
  76. };
  77. static int BindingGenerator()
  78. {
  79. // TODO: WIP
  80. return EXIT_SUCCESS;
  81. }
  82. int main(int argc, const char** argv)
  83. {
  84. // Parse the arguments and pass them to the the internal sub-tools
  85. CommonOptionsParser optionsParser(argc, argv, autobinderCategory);
  86. ClangTool bindingExtractor(optionsParser.getCompilations(), optionsParser.getSourcePathList());
  87. // Setup finder to match against AST nodes from Urho3D library source files
  88. ExtractCallback extractCallback;
  89. MatchFinder bindingFinder;
  90. // Find exported class declarations with Urho3D namespace
  91. bindingFinder.addMatcher(
  92. recordDecl(
  93. unless(hasAttr(attr::Annotate)),
  94. #ifndef _MSC_VER
  95. hasAttr(attr::Visibility),
  96. #else
  97. hasAttr(attr::DLLExport),
  98. #endif
  99. matchesName("^::Urho3D::")).bind("class"), &extractCallback);
  100. // Find enum declarations with Urho3D namespace
  101. bindingFinder.addMatcher(
  102. enumDecl(
  103. unless(hasAttr(attr::Annotate)),
  104. matchesName("^::Urho3D::")).bind("enum"), &extractCallback);
  105. // Unbuffered stdout stream to keep the Travis-CI's log flowing and thus prevent it from killing a potentially long running job
  106. outs().SetUnbuffered();
  107. // Success when both sub-tools are run successfully
  108. return (outs() << "Extracting", true) &&
  109. bindingExtractor.run(newFrontendActionFactory(&bindingFinder).get()) == EXIT_SUCCESS &&
  110. (outs() << "\nBinding", true) &&
  111. BindingGenerator() == EXIT_SUCCESS &&
  112. (outs() << "\n", true) ?
  113. EXIT_SUCCESS : EXIT_FAILURE;
  114. }