From f8678f2c3fcdf49d0340c20badb9c807a751364f Mon Sep 17 00:00:00 2001 From: Martchus Date: Wed, 25 Oct 2017 15:38:45 +0200 Subject: [PATCH] Split generator.h and generator.cpp --- moc/CMakeLists.txt | 6 +- moc/codefactory.cpp | 92 ++++++++++++++++++++++++ moc/codefactory.h | 74 +++++++++++++++++++ moc/{generator.cpp => codegenerator.cpp} | 86 +--------------------- moc/{generator.h => codegenerator.h} | 63 +--------------- moc/frontendaction.cpp | 2 +- moc/main.cpp | 2 +- moc/tests/overall.cpp | 2 +- moc/visitor.cpp | 2 +- 9 files changed, 178 insertions(+), 151 deletions(-) create mode 100644 moc/codefactory.cpp create mode 100644 moc/codefactory.h rename moc/{generator.cpp => codegenerator.cpp} (59%) rename moc/{generator.h => codegenerator.h} (50%) diff --git a/moc/CMakeLists.txt b/moc/CMakeLists.txt index 7f3ff46..fcdf6a8 100644 --- a/moc/CMakeLists.txt +++ b/moc/CMakeLists.txt @@ -7,14 +7,16 @@ set(LINK_TESTS_AGAINST_APP_TARGET ON) # add project files set(HEADER_FILES - generator.h + codegenerator.h + codefactory.h frontendaction.h consumer.h visitor.h clangversionabstraction.h ) set(SRC_FILES - generator.cpp + codegenerator.cpp + codefactory.cpp frontendaction.cpp consumer.cpp clangversionabstraction.cpp diff --git a/moc/codefactory.cpp b/moc/codefactory.cpp new file mode 100644 index 0000000..b0f1691 --- /dev/null +++ b/moc/codefactory.cpp @@ -0,0 +1,92 @@ +#include "./codefactory.h" +#include "./frontendaction.h" + +#include +#include +#include + +#include + +using namespace std; + +namespace ReflectiveRapidJSON { + +struct CodeFactory::ToolInvocation { + ToolInvocation(CodeFactory &factory); + + clang::FileManager fileManager; + clang::tooling::ToolInvocation invocation; +}; + +CodeFactory::ToolInvocation::ToolInvocation(CodeFactory &factory) + : fileManager({ "." }) + , invocation(factory.makeClangArgs(), new FrontendAction(factory), &fileManager) +{ + fileManager.Retain(); +} + +CodeFactory::CodeFactory( + const char *applicationPath, const std::vector &sourceFiles, const std::vector &clangOptions, std::ostream &os) + : m_applicationPath(applicationPath) + , m_sourceFiles(sourceFiles) + , m_clangOptions(clangOptions) + , m_os(os) + , m_compilerInstance(nullptr) +{ +} + +CodeFactory::~CodeFactory() +{ +} + +/*! + * \brief Constructs arguments for the Clang tool invocation. + */ +std::vector CodeFactory::makeClangArgs() const +{ + static const initializer_list flags + = { m_applicationPath, "-x", "c++", "-fPIE", "-fPIC", "-Wno-microsoft", "-Wno-pragma-once-outside-header", "-std=c++14", "-fsyntax-only" }; + vector clangArgs; + clangArgs.reserve(flags.size() + m_clangOptions.size() + m_sourceFiles.size()); + clangArgs.insert(clangArgs.end(), flags.begin(), flags.end()); + clangArgs.insert(clangArgs.end(), m_clangOptions.cbegin(), m_clangOptions.cend()); + clangArgs.insert(clangArgs.end(), m_sourceFiles.cbegin(), m_sourceFiles.cend()); + return clangArgs; +} + +/*! + * \brief Adds the specified \a decl to all underlying code generators. The generators might ignore irrelevant declarations. + * \remarks Supposed to be called by assigned generators inside readAST(). + */ +void CodeFactory::addDeclaration(clang::Decl *decl) +{ + for (const auto &generator : m_generators) { + generator->addDeclaration(decl); + } +} + +/*! + * \brief Reads (relevent) AST elements using Clang. + */ +bool CodeFactory::readAST() +{ + // lazy initialize Clang tool invocation + if (!m_toolInvocation) { + m_toolInvocation = make_unique(*this); + } + // run Clang + return m_toolInvocation->invocation.run(); +} + +/*! + * \brief Generates code based on the AST elements which have been read by invoking readAST(). + */ +bool CodeFactory::generate() const +{ + for (const auto &generator : m_generators) { + generator->generate(m_os); + } + return true; +} + +} // namespace ReflectiveRapidJSON diff --git a/moc/codefactory.h b/moc/codefactory.h new file mode 100644 index 0000000..e80e522 --- /dev/null +++ b/moc/codefactory.h @@ -0,0 +1,74 @@ +#ifndef REFLECTIVE_RAPIDJSON_CODE_FACTORY_H +#define REFLECTIVE_RAPIDJSON_CODE_FACTORY_H + +#include "./codegenerator.h" + +#include +#include +#include +#include + +namespace clang { +class CompilerInstance; +} // namespace clang + +namespace ReflectiveRapidJSON { + +/*! + * \brief The CodeFactory class produces additional (reflection) code for a specified list of C++ source files. + * \remarks + * - The code is written to a specified std::ostream instance. + * - The CodeFactory class is constituted by its underlying CodeGenerator instances. + */ +class CodeFactory { +public: + CodeFactory( + const char *applicationPath, const std::vector &sourceFiles, const std::vector &clangOptions, std::ostream &os); + ~CodeFactory(); + + const std::vector> &generators() const; + template void addGenerator(); + + void addDeclaration(clang::Decl *decl); + bool readAST(); + bool generate() const; + clang::CompilerInstance *compilerInstance(); + void setCompilerInstance(clang::CompilerInstance *compilerInstance); + +private: + struct ToolInvocation; + + std::vector makeClangArgs() const; + + const char *const m_applicationPath; + const std::vector &m_sourceFiles; + const std::vector &m_clangOptions; + std::ostream &m_os; + std::vector> m_generators; + std::unique_ptr m_toolInvocation; + clang::CompilerInstance *m_compilerInstance; +}; + +template void CodeFactory::addGenerator() +{ + m_generators.emplace_back(std::make_unique(*this)); +} + +inline const std::vector> &CodeFactory::generators() const +{ + return m_generators; +} + +inline clang::CompilerInstance *CodeFactory::compilerInstance() +{ + return m_compilerInstance; +} + +inline void CodeFactory::setCompilerInstance(clang::CompilerInstance *compilerInstance) +{ + m_compilerInstance = compilerInstance; +} + +} // namespace ReflectiveRapidJSON + +#endif // REFLECTIVE_RAPIDJSON_CODE_FACTORY_H diff --git a/moc/generator.cpp b/moc/codegenerator.cpp similarity index 59% rename from moc/generator.cpp rename to moc/codegenerator.cpp index 66a2d55..a18327f 100644 --- a/moc/generator.cpp +++ b/moc/codegenerator.cpp @@ -1,21 +1,15 @@ -#include "./generator.h" -#include "./frontendaction.h" +#include "./codegenerator.h" #include "../lib/jsonserializable.h" #include -#include #include -#include -#include -#include #include #include using namespace std; -using namespace ConversionUtilities; namespace ReflectiveRapidJSON { @@ -112,82 +106,4 @@ void JSONSerializationCodeGenerator::generate(ostream &os) const "} // namespace ReflectiveRapidJSON\n"; } -struct CodeFactory::ToolInvocation { - ToolInvocation(CodeFactory &factory); - - clang::FileManager fileManager; - clang::tooling::ToolInvocation invocation; -}; - -CodeFactory::ToolInvocation::ToolInvocation(CodeFactory &factory) - : fileManager({ "." }) - , invocation(factory.makeClangArgs(), new FrontendAction(factory), &fileManager) -{ - fileManager.Retain(); -} - -CodeFactory::CodeFactory( - const char *applicationPath, const std::vector &sourceFiles, const std::vector &clangOptions, std::ostream &os) - : m_applicationPath(applicationPath) - , m_sourceFiles(sourceFiles) - , m_clangOptions(clangOptions) - , m_os(os) - , m_compilerInstance(nullptr) -{ -} - -CodeFactory::~CodeFactory() -{ -} - -/*! - * \brief Constructs arguments for the Clang tool invocation. - */ -std::vector CodeFactory::makeClangArgs() const -{ - static const initializer_list flags - = { m_applicationPath, "-x", "c++", "-fPIE", "-fPIC", "-Wno-microsoft", "-Wno-pragma-once-outside-header", "-std=c++14", "-fsyntax-only" }; - vector clangArgs; - clangArgs.reserve(flags.size() + m_clangOptions.size() + m_sourceFiles.size()); - clangArgs.insert(clangArgs.end(), flags.begin(), flags.end()); - clangArgs.insert(clangArgs.end(), m_clangOptions.cbegin(), m_clangOptions.cend()); - clangArgs.insert(clangArgs.end(), m_sourceFiles.cbegin(), m_sourceFiles.cend()); - return clangArgs; -} - -/*! - * \brief Adds the specified \a decl to all underlying code generators. The generators might ignore irrelevant declarations. - * \remarks Supposed to be called by assigned generators inside readAST(). - */ -void CodeFactory::addDeclaration(clang::Decl *decl) -{ - for (const auto &generator : m_generators) { - generator->addDeclaration(decl); - } -} - -/*! - * \brief Reads (relevent) AST elements using Clang. - */ -bool CodeFactory::readAST() -{ - // lazy initialize Clang tool invocation - if (!m_toolInvocation) { - m_toolInvocation = make_unique(*this); - } - // run Clang - return m_toolInvocation->invocation.run(); -} - -/*! - * \brief Generates code based on the AST elements which have been read by invoking readAST(). - */ -bool CodeFactory::generate() const -{ - for (const auto &generator : m_generators) { - generator->generate(m_os); - } - return true; -} - } // namespace ReflectiveRapidJSON diff --git a/moc/generator.h b/moc/codegenerator.h similarity index 50% rename from moc/generator.h rename to moc/codegenerator.h index 83549f4..109014e 100644 --- a/moc/generator.h +++ b/moc/codegenerator.h @@ -1,8 +1,7 @@ -#ifndef REFLECTIVE_RAPIDJSON_GENERATOR_H -#define REFLECTIVE_RAPIDJSON_GENERATOR_H +#ifndef REFLECTIVE_RAPIDJSON_CODE_GENERATOR_H +#define REFLECTIVE_RAPIDJSON_CODE_GENERATOR_H #include -#include #include #include @@ -10,7 +9,6 @@ namespace clang { class Decl; class NamedDecl; class CXXRecordDecl; -class CompilerInstance; } // namespace clang namespace ReflectiveRapidJSON { @@ -81,61 +79,6 @@ inline JSONSerializationCodeGenerator::RelevantClass::RelevantClass(const std::s { } -/*! - * \brief The CodeFactory class produces additional (reflection) code for a specified list of C++ source files. - * \remarks - * - The code is written to a specified std::ostream instance. - * - The CodeFactory class is constituted by its underlying CodeGenerator instances. - */ -class CodeFactory { -public: - CodeFactory( - const char *applicationPath, const std::vector &sourceFiles, const std::vector &clangOptions, std::ostream &os); - ~CodeFactory(); - - const std::vector> &generators() const; - template void addGenerator(); - - void addDeclaration(clang::Decl *decl); - bool readAST(); - bool generate() const; - clang::CompilerInstance *compilerInstance(); - void setCompilerInstance(clang::CompilerInstance *compilerInstance); - -private: - struct ToolInvocation; - - std::vector makeClangArgs() const; - - const char *const m_applicationPath; - const std::vector &m_sourceFiles; - const std::vector &m_clangOptions; - std::ostream &m_os; - std::vector> m_generators; - std::unique_ptr m_toolInvocation; - clang::CompilerInstance *m_compilerInstance; -}; - -template void CodeFactory::addGenerator() -{ - m_generators.emplace_back(std::make_unique(*this)); -} - -inline const std::vector> &CodeFactory::generators() const -{ - return m_generators; -} - -inline clang::CompilerInstance *CodeFactory::compilerInstance() -{ - return m_compilerInstance; -} - -inline void CodeFactory::setCompilerInstance(clang::CompilerInstance *compilerInstance) -{ - m_compilerInstance = compilerInstance; -} - } // namespace ReflectiveRapidJSON -#endif // REFLECTIVE_RAPIDJSON_GENERATOR_H +#endif // REFLECTIVE_RAPIDJSON_CODE_GENERATOR_H diff --git a/moc/frontendaction.cpp b/moc/frontendaction.cpp index 94d7d7b..da301f3 100644 --- a/moc/frontendaction.cpp +++ b/moc/frontendaction.cpp @@ -1,6 +1,6 @@ #include "./frontendaction.h" +#include "./codefactory.h" #include "./consumer.h" -#include "./generator.h" #include diff --git a/moc/main.cpp b/moc/main.cpp index 414c0ed..cf12bc1 100644 --- a/moc/main.cpp +++ b/moc/main.cpp @@ -1,4 +1,4 @@ -#include "./generator.h" +#include "./codefactory.h" #include "resources/config.h" diff --git a/moc/tests/overall.cpp b/moc/tests/overall.cpp index 8605b85..42ccbd9 100644 --- a/moc/tests/overall.cpp +++ b/moc/tests/overall.cpp @@ -1,4 +1,4 @@ -#include "../generator.h" +#include "../codefactory.h" #include "../../lib/jsonserializable.h" diff --git a/moc/visitor.cpp b/moc/visitor.cpp index e766402..b144ecf 100644 --- a/moc/visitor.cpp +++ b/moc/visitor.cpp @@ -1,5 +1,5 @@ #include "./visitor.h" -#include "./generator.h" +#include "./codefactory.h" #include