|
| 1 | +#include <clang/AST/Decl.h> |
| 2 | +#include <clang/AST/RecursiveASTVisitor.h> |
| 3 | +#include <clang/AST/Type.h> |
| 4 | +#include <clang/Frontend/CompilerInstance.h> |
| 5 | +#include <clang/Tooling/CommonOptionsParser.h> |
| 6 | +#include <clang/Tooling/Tooling.h> |
| 7 | +#include <llvm/Support/raw_ostream.h> |
| 8 | + |
| 9 | +#include <filesystem> |
| 10 | +#include <fstream> |
| 11 | +#include <iostream> |
| 12 | + |
| 13 | +using namespace clang; |
| 14 | +using namespace tooling; |
| 15 | +namespace fs = std::filesystem; |
| 16 | + |
| 17 | +static llvm::cl::OptionCategory toolCategory("cxx-module-generator"); |
| 18 | +static llvm::cl::opt<std::string> moduleName("name", |
| 19 | + llvm::cl::desc("Module name"), |
| 20 | + llvm::cl::cat(toolCategory)); |
| 21 | +static llvm::cl::opt<std::string> output("o", |
| 22 | + llvm::cl::desc("Specify output path"), |
| 23 | + llvm::cl::cat(toolCategory)); |
| 24 | +static llvm::cl::opt<std::string> nsFilter( |
| 25 | + "namespace", llvm::cl::desc("Filter symbol by namespace"), |
| 26 | + llvm::cl::cat(toolCategory)); |
| 27 | + |
| 28 | +class ModuleWrapper { |
| 29 | + private: |
| 30 | + std::string content; |
| 31 | + |
| 32 | + public: |
| 33 | + ModuleWrapper(const fs::path &originalFile) { |
| 34 | + content += "module;\n#include \"" + originalFile.generic_string() + "\"\n"; |
| 35 | + content += "export module " + |
| 36 | + (!moduleName.empty() ? moduleName.getValue() |
| 37 | + : originalFile.stem().string()) + |
| 38 | + ";\n"; |
| 39 | + } |
| 40 | + |
| 41 | + void openNamespace(NamespaceDecl *decl) { |
| 42 | + content += "namespace " + decl->getNameAsString() + " {\n"; |
| 43 | + } |
| 44 | + |
| 45 | + void closeNamespace(NamespaceDecl *) { content += "}\n"; } |
| 46 | + |
| 47 | + void addSymbol(NamedDecl *decl) { |
| 48 | + if (decl != nullptr && decl->isFirstDecl()) |
| 49 | + content += "export using " + decl->getQualifiedNameAsString() + ";\n"; |
| 50 | + } |
| 51 | + |
| 52 | + const std::string &getContent() const { return content; } |
| 53 | +}; |
| 54 | + |
| 55 | +class FindAllSymbols : public RecursiveASTVisitor<FindAllSymbols> { |
| 56 | + private: |
| 57 | + using Base = RecursiveASTVisitor<FindAllSymbols>; |
| 58 | + |
| 59 | + ModuleWrapper &wrapper; |
| 60 | + |
| 61 | + public: |
| 62 | + FindAllSymbols(ModuleWrapper &wrapper) : wrapper(wrapper) {} |
| 63 | + |
| 64 | + bool TraverseNamespaceDecl(NamespaceDecl *decl) { |
| 65 | + if (nsFilter.find(decl->getName()) != std::string::npos) { |
| 66 | + wrapper.openNamespace(decl); |
| 67 | + Base::TraverseNamespaceDecl(decl); |
| 68 | + wrapper.closeNamespace(decl); |
| 69 | + } |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + bool TraverseDecl(Decl *decl) { |
| 74 | + switch (decl->getKind()) { |
| 75 | + case Decl::TranslationUnit: |
| 76 | + return TraverseTranslationUnitDecl( |
| 77 | + static_cast<TranslationUnitDecl *>(decl)); |
| 78 | + case Decl::Namespace: |
| 79 | + return TraverseNamespaceDecl(static_cast<NamespaceDecl *>(decl)); |
| 80 | + default: |
| 81 | + break; |
| 82 | + } |
| 83 | + switch (decl->getKind()) { |
| 84 | +#define ABSTRACT_DECL(DECL) |
| 85 | +#define DECL(CLASS, BASE) \ |
| 86 | + case Decl::CLASS: \ |
| 87 | + if (!WalkUpFrom##CLASS##Decl(static_cast<CLASS##Decl *>(decl))) \ |
| 88 | + return false; \ |
| 89 | + break; |
| 90 | +#include "clang/AST/DeclNodes.inc" |
| 91 | + } |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | +#define VISIT_DECL(TYPE) \ |
| 96 | + bool Visit##TYPE##Decl(TYPE##Decl *decl) { \ |
| 97 | + if (!decl->isImplicit() && \ |
| 98 | + decl->getQualifiedNameAsString().find(nsFilter) != std::string::npos) \ |
| 99 | + wrapper.addSymbol(decl); \ |
| 100 | + return true; \ |
| 101 | + } |
| 102 | + |
| 103 | + VISIT_DECL(Tag); |
| 104 | + VISIT_DECL(TypedefName); |
| 105 | + VISIT_DECL(Function); |
| 106 | +#undef VISIT_DECL |
| 107 | +}; |
| 108 | + |
| 109 | +class CreateModule : public ASTConsumer { |
| 110 | + private: |
| 111 | + fs::path path; |
| 112 | + |
| 113 | + public: |
| 114 | + CreateModule(const fs::path &path) : path(fs::canonical(path)) {} |
| 115 | + |
| 116 | + void HandleTranslationUnit(ASTContext &ctx) override { |
| 117 | + ModuleWrapper wrapper(path); |
| 118 | + FindAllSymbols visitor(wrapper); |
| 119 | + visitor.TraverseDecl(ctx.getTranslationUnitDecl()); |
| 120 | + auto modulePath = output.getValue() / path.stem() += ".cppm"; |
| 121 | + fs::create_directories(modulePath.parent_path()); |
| 122 | + std::ofstream os{modulePath, std::ios::binary}; |
| 123 | + os.exceptions(std::ios::failbit); |
| 124 | + os << wrapper.getContent(); |
| 125 | + } |
| 126 | +}; |
| 127 | + |
| 128 | +template <class Consumer> |
| 129 | +class SuppressIncludeNotFound : public ASTFrontendAction { |
| 130 | + public: |
| 131 | + std::unique_ptr<ASTConsumer> CreateASTConsumer( |
| 132 | + CompilerInstance &, llvm::StringRef path) override { |
| 133 | + return std::make_unique<Consumer>(path.str()); |
| 134 | + } |
| 135 | +}; |
| 136 | + |
| 137 | +int main(int argc, const char **argv) { |
| 138 | + output.setInitialValue(fs::current_path().generic_string()); |
| 139 | + auto expectedParser = CommonOptionsParser::create(argc, argv, toolCategory); |
| 140 | + if (!expectedParser) { |
| 141 | + llvm::errs() << expectedParser.takeError(); |
| 142 | + return 1; |
| 143 | + } |
| 144 | + CommonOptionsParser &op = expectedParser.get(); |
| 145 | + ClangTool tool(op.getCompilations(), op.getSourcePathList()); |
| 146 | + return tool.run( |
| 147 | + newFrontendActionFactory<SuppressIncludeNotFound<CreateModule>>().get()); |
| 148 | +} |
0 commit comments