From fe40840486f0f8a14bf150051da6e7ec614b26e6 Mon Sep 17 00:00:00 2001 From: Martchus Date: Fri, 27 Oct 2017 17:38:57 +0200 Subject: [PATCH] Don't call generator when declarations already deleted --- generator/codefactory.cpp | 28 ++++++++++++++-------------- generator/codefactory.h | 12 +++++++++--- generator/codegenerator.cpp | 2 +- generator/consumer.cpp | 7 +++++++ generator/consumer.h | 5 ----- generator/main.cpp | 12 +++--------- 6 files changed, 34 insertions(+), 32 deletions(-) diff --git a/generator/codefactory.cpp b/generator/codefactory.cpp index b0f1691..6b441b9 100644 --- a/generator/codefactory.cpp +++ b/generator/codefactory.cpp @@ -66,20 +66,7 @@ void CodeFactory::addDeclaration(clang::Decl *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(). + * \brief Generates code based on the added declarations. */ bool CodeFactory::generate() const { @@ -89,4 +76,17 @@ bool CodeFactory::generate() const return true; } +/*! + * \brief Reads (relevent) AST elements using Clang and generates code. + */ +bool CodeFactory::run() +{ + // lazy initialize Clang tool invocation + if (!m_toolInvocation) { + m_toolInvocation = make_unique(*this); + } + // run Clang + return m_toolInvocation->invocation.run(); +} + } // namespace ReflectiveRapidJSON diff --git a/generator/codefactory.h b/generator/codefactory.h index e80e522..619ae19 100644 --- a/generator/codefactory.h +++ b/generator/codefactory.h @@ -14,6 +14,9 @@ class CompilerInstance; namespace ReflectiveRapidJSON { +class Consumer; +class Visitor; + /*! * \brief The CodeFactory class produces additional (reflection) code for a specified list of C++ source files. * \remarks @@ -21,6 +24,9 @@ namespace ReflectiveRapidJSON { * - The CodeFactory class is constituted by its underlying CodeGenerator instances. */ class CodeFactory { + friend class Consumer; + friend class Visitor; + public: CodeFactory( const char *applicationPath, const std::vector &sourceFiles, const std::vector &clangOptions, std::ostream &os); @@ -29,15 +35,15 @@ public: const std::vector> &generators() const; template void addGenerator(); - void addDeclaration(clang::Decl *decl); - bool readAST(); - bool generate() const; + bool run(); clang::CompilerInstance *compilerInstance(); void setCompilerInstance(clang::CompilerInstance *compilerInstance); private: struct ToolInvocation; + void addDeclaration(clang::Decl *decl); + bool generate() const; std::vector makeClangArgs() const; const char *const m_applicationPath; diff --git a/generator/codegenerator.cpp b/generator/codegenerator.cpp index 3f6007b..3b3efa8 100644 --- a/generator/codegenerator.cpp +++ b/generator/codegenerator.cpp @@ -123,7 +123,7 @@ std::vector JSONSerializa { vector relevantBaseClasses; for (const RelevantClass &otherClass : m_relevantClasses) { - if (relevantClass.record->isDerivedFrom(otherClass.record)) { + if (relevantClass.record != otherClass.record && relevantClass.record->isDerivedFrom(otherClass.record)) { relevantBaseClasses.push_back(&otherClass); } } diff --git a/generator/consumer.cpp b/generator/consumer.cpp index 080d8a7..1101ffe 100644 --- a/generator/consumer.cpp +++ b/generator/consumer.cpp @@ -1,4 +1,5 @@ #include "./consumer.h" +#include "./codefactory.h" #include #include @@ -29,6 +30,12 @@ bool Consumer::HandleTopLevelDecl(clang::DeclGroupRef groupRefDecl) return clang::ASTConsumer::HandleTopLevelDecl(groupRefDecl); } +void Consumer::HandleTranslationUnit(clang::ASTContext &context) +{ + m_visitor.TraverseDecl(context.getTranslationUnitDecl()); + m_factory.generate(); +} + void DiagConsumer::BeginSourceFile(const clang::LangOptions &langOpts, const clang::Preprocessor *pp) { m_proxy->BeginSourceFile(langOpts, pp); diff --git a/generator/consumer.h b/generator/consumer.h index 9993985..68b730e 100644 --- a/generator/consumer.h +++ b/generator/consumer.h @@ -44,11 +44,6 @@ inline Consumer::Consumer(CodeFactory &factory, clang::CompilerInstance &compile { } -inline void Consumer::HandleTranslationUnit(clang::ASTContext &context) -{ - m_visitor.TraverseDecl(context.getTranslationUnitDecl()); -} - /*! * \brief The DiagConsumer class changes most errors into warnings. * \remarks This class is based on MocDiagConsumer from https://github.com/woboq/moc-ng. diff --git a/generator/main.cpp b/generator/main.cpp index 436efbf..c6ebcf2 100644 --- a/generator/main.cpp +++ b/generator/main.cpp @@ -77,18 +77,12 @@ int main(int argc, char *argv[]) factory.addGenerator(); } - // read AST elements from input files - if (!factory.readAST()) { - cerr << Phrases::Error << "Errors occured when parsing the input file." << Phrases::EndFlush; + // read AST elements from input files and run the code generator + if (!factory.run()) { + cerr << Phrases::Error << "Errors occured." << Phrases::EndFlush; return -2; } - // run the code generator - if (!factory.generate()) { - cerr << Phrases::Error << "Errors occured when during code generation." << Phrases::EndFlush; - return -3; - } - } catch (...) { catchIoFailure(); const char *errorMessage;