Don't call generator when declarations already deleted

This commit is contained in:
Martchus 2017-10-27 17:38:57 +02:00
parent 31c37a8ee4
commit fe40840486
6 changed files with 34 additions and 32 deletions

View File

@ -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<ToolInvocation>(*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<ToolInvocation>(*this);
}
// run Clang
return m_toolInvocation->invocation.run();
}
} // namespace ReflectiveRapidJSON

View File

@ -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<const char *> &sourceFiles, const std::vector<const char *> &clangOptions, std::ostream &os);
@ -29,15 +35,15 @@ public:
const std::vector<std::unique_ptr<CodeGenerator>> &generators() const;
template <typename GeneratorType> 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<std::string> makeClangArgs() const;
const char *const m_applicationPath;

View File

@ -123,7 +123,7 @@ std::vector<const JSONSerializationCodeGenerator::RelevantClass *> JSONSerializa
{
vector<const RelevantClass *> 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);
}
}

View File

@ -1,4 +1,5 @@
#include "./consumer.h"
#include "./codefactory.h"
#include <clang/AST/ASTContext.h>
#include <clang/AST/DeclCXX.h>
@ -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);

View File

@ -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.

View File

@ -77,18 +77,12 @@ int main(int argc, char *argv[])
factory.addGenerator<JSONSerializationCodeGenerator>();
}
// 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;