2017-10-25 15:38:45 +02:00
|
|
|
#include "./codefactory.h"
|
|
|
|
#include "./frontendaction.h"
|
|
|
|
|
|
|
|
#include <clang/Basic/FileManager.h>
|
|
|
|
#include <clang/Frontend/FrontendActions.h>
|
|
|
|
#include <clang/Tooling/Tooling.h>
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-11-17 21:42:49 +01:00
|
|
|
CodeFactory::CodeFactory(const char *applicationPath, const std::vector<const char *> &sourceFiles, const std::vector<string> &clangOptions, std::ostream &os)
|
2017-10-25 15:38:45 +02:00
|
|
|
: 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<string> CodeFactory::makeClangArgs() const
|
|
|
|
{
|
|
|
|
static const initializer_list<const char *> flags
|
|
|
|
= { m_applicationPath, "-x", "c++", "-fPIE", "-fPIC", "-Wno-microsoft", "-Wno-pragma-once-outside-header", "-std=c++14", "-fsyntax-only" };
|
|
|
|
vector<string> 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2017-10-27 17:38:57 +02:00
|
|
|
* \brief Generates code based on the added declarations.
|
2017-10-25 15:38:45 +02:00
|
|
|
*/
|
2017-10-27 17:38:57 +02:00
|
|
|
bool CodeFactory::generate() const
|
2017-10-25 15:38:45 +02:00
|
|
|
{
|
2017-10-27 17:38:57 +02:00
|
|
|
for (const auto &generator : m_generators) {
|
|
|
|
generator->generate(m_os);
|
2017-10-25 15:38:45 +02:00
|
|
|
}
|
2017-10-27 17:38:57 +02:00
|
|
|
return true;
|
2017-10-25 15:38:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2017-10-27 17:38:57 +02:00
|
|
|
* \brief Reads (relevent) AST elements using Clang and generates code.
|
2017-10-25 15:38:45 +02:00
|
|
|
*/
|
2017-10-27 17:38:57 +02:00
|
|
|
bool CodeFactory::run()
|
2017-10-25 15:38:45 +02:00
|
|
|
{
|
2017-10-27 17:38:57 +02:00
|
|
|
// lazy initialize Clang tool invocation
|
|
|
|
if (!m_toolInvocation) {
|
|
|
|
m_toolInvocation = make_unique<ToolInvocation>(*this);
|
2017-10-25 15:38:45 +02:00
|
|
|
}
|
2017-10-27 17:38:57 +02:00
|
|
|
// run Clang
|
|
|
|
return m_toolInvocation->invocation.run();
|
2017-10-25 15:38:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ReflectiveRapidJSON
|