Skip to content

Commit

Permalink
Refactor tool execution
Browse files Browse the repository at this point in the history
  • Loading branch information
vinniefalco committed Apr 7, 2023
1 parent caab7c1 commit eba53fa
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 178 deletions.
2 changes: 0 additions & 2 deletions include/mrdox/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ struct Config

std::unique_ptr<tooling::CommonOptionsParser> options;

std::unique_ptr<tooling::ToolExecutor> Executor;

tooling::ArgumentsAdjuster ArgAdjuster;

// Name of project being documented.
Expand Down
34 changes: 5 additions & 29 deletions include/mrdox/Corpus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,10 @@ namespace mrdox {
*/
struct Corpus
{
Corpus();
Corpus(Corpus const&) = delete;
Corpus() = default;
Corpus(Corpus&&) noexcept = default;
Corpus& operator=(Corpus const&) = delete;

/** Holds the results of visiting the AST.
This is a table of key/value pairs where
the key is the SHA1 digest of the USR and
the value is the bitcode-encoded representation
of the Info object.
*/
std::unique_ptr<tooling::ToolResults> toolResults;

Index Idx;

/** Table of Info keyed on USR.
Expand All @@ -49,27 +40,12 @@ struct Corpus
std::unique_ptr<mrdox::Info>> USRToInfo;
};

/** Return a Corpus built using the specified configuration.
*/
Result<Corpus>
std::unique_ptr<Corpus>
buildCorpus(
Config const& config,
tooling::ToolExecutor& ex,
Config const& cfg,
Reporter& R);

llvm::Error
doMapping(
Corpus& corpus,
Config const& cfg);

/** Build the internal index of the program under analysis.
This must happen before generating docs.
*/
llvm::Error
buildIndex(
Corpus& corpus,
Config const& cfg);

} // mrdox
} // clang

Expand Down
10 changes: 4 additions & 6 deletions include/mrdox/Visitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include "Representation.h"
#include <mrdox/Config.hpp>
#include <mrdox/Corpus.hpp>
#include <clang/Tooling/Execution.h>
#include <clang/AST/ASTConsumer.h>
#include <clang/AST/RecursiveASTVisitor.h>
#include <utility>
Expand All @@ -38,14 +38,14 @@ class Visitor
: public RecursiveASTVisitor<Visitor>
, public ASTConsumer
{
Corpus& corpus_;
tooling::ExecutionContext& exc_;
Config const& cfg_;

public:
Visitor(
Corpus& corpus,
tooling::ExecutionContext& exc,
Config const& cfg) noexcept
: corpus_(corpus)
: exc_(exc)
, cfg_(cfg)
{
}
Expand All @@ -61,8 +61,6 @@ class Visitor
bool VisitTypeAliasDecl(TypeAliasDecl const* D);

private:
void reportResult(StringRef Key, StringRef Value);

template <typename T>
bool mapDecl(T const* D);

Expand Down
33 changes: 16 additions & 17 deletions source/lib/ClangDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ namespace {

//------------------------------------------------

struct action
struct Action
: public clang::ASTFrontendAction
{
action(
Corpus& corpus,
Action(
tooling::ExecutionContext& exc,
Config const& cfg) noexcept
: corpus_(corpus)
: exc_(exc)
, cfg_(cfg)
{
}
Expand All @@ -43,49 +43,48 @@ struct action
clang::CompilerInstance& Compiler,
llvm::StringRef InFile) override
{
return std::make_unique<Visitor>(corpus_, cfg_);
return std::make_unique<Visitor>(exc_, cfg_);
}

private:
Corpus& corpus_;
tooling::ExecutionContext& exc_;
Config const& cfg_;
};

//------------------------------------------------

struct factory
struct Factory
: public tooling::FrontendActionFactory
{
factory(
Corpus& corpus,
Factory(
tooling::ExecutionContext& exc,
Config const& cfg) noexcept
: corpus_(corpus)
: exc_(exc)
, cfg_(cfg)
{
}

std::unique_ptr<FrontendAction>
create() override
{
return std::make_unique<action>(corpus_, cfg_);
return std::make_unique<Action>(exc_, cfg_);
}

private:
Corpus& corpus_;
tooling::ExecutionContext& exc_;
Config const& cfg_;
};

} // (anon)

//------------------------------------------------

std::unique_ptr<
tooling::FrontendActionFactory>
newMapperActionFactory(
Corpus& corpus,
std::unique_ptr<tooling::FrontendActionFactory>
makeToolFactory(
tooling::ExecutionContext& exc,
Config const& cfg)
{
return std::make_unique<factory>(corpus, cfg);
return std::make_unique<Factory>(exc, cfg);
}

} // mrdox
Expand Down
15 changes: 7 additions & 8 deletions source/lib/ClangDoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_CLANGDOC_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_CLANGDOC_H

#include "Representation.h"
#include <mrdox/Config.hpp>
#include <clang/Tooling/Execution.h>
#include <clang/Tooling/Tooling.h>

namespace clang {
namespace mrdox {

std::unique_ptr<
tooling::FrontendActionFactory>
newMapperActionFactory(
Corpus& corpus,
std::unique_ptr<tooling::FrontendActionFactory>
makeToolFactory(
tooling::ExecutionContext& exc,
Config const& cfg);

} // namespace mrdox
} // namespace clang
} // mrdox
} // clang

#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_CLANGDOC_H
#endif
12 changes: 2 additions & 10 deletions source/lib/Visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ HandleTranslationUnit(
TraverseDecl(Context.getTranslationUnitDecl());
}

void
Visitor::
reportResult(
StringRef Key, StringRef Value)
{
corpus_.toolResults->addResult(Key, Value);
}

template<typename T>
bool
Visitor::
Expand Down Expand Up @@ -73,11 +65,11 @@ mapDecl(T const* D)
// A null in place of I indicates that the serializer is skipping this decl
// for some reason (e.g. we're only reporting public decls).
if (I.first)
reportResult(
exc_.reportResult(
llvm::toHex(llvm::toStringRef(I.first->USR)),
serialize::serialize(I.first));
if (I.second)
reportResult(
exc_.reportResult(
llvm::toHex(llvm::toStringRef(I.second->USR)),
serialize::serialize(I.second));

Expand Down
Loading

0 comments on commit eba53fa

Please sign in to comment.