Skip to content

Commit

Permalink
chore: tidy up config construction
Browse files Browse the repository at this point in the history
  • Loading branch information
vinniefalco committed Jun 11, 2023
1 parent c740a9a commit 993bfa1
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 24 deletions.
3 changes: 1 addition & 2 deletions include/mrdox/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#include <mrdox/Platform.hpp>
#include <mrdox/Support/Error.hpp>
#include <mrdox/Support/Thread.hpp>
#include <functional>
#include <memory>
#include <string>
Expand All @@ -26,7 +25,7 @@
namespace clang {
namespace mrdox {

class ConfigImpl;
class ThreadPool;

/** Configuration used to generate the Corpus and Docs
Expand Down
16 changes: 16 additions & 0 deletions include/mrdox/Support/Thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,29 @@ class MRDOX_VISIBLE
MRDOX_DECL
~ThreadPool();

/** Constructor.
Default constructed thread pools may only
be reset or destroyed.
*/
MRDOX_DECL
explicit
ThreadPool();

/** Constructor.
*/
MRDOX_DECL
explicit
ThreadPool(
unsigned concurrency);

/** Reset the pool to the specified concurrency.
*/
MRDOX_DECL
void
reset(
unsigned concurrency);

/** Return the number of threads in the pool.
*/
MRDOX_DECL
Expand Down
1 change: 1 addition & 0 deletions source/-adoc/AdocPagesBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <mrdox/Corpus.hpp>
#include <mrdox/MetadataFwd.hpp>
#include <mrdox/Support/Error.hpp>
#include <mrdox/Support/Thread.hpp>
#include <llvm/ADT/SmallString.h>

namespace clang {
Expand Down
1 change: 1 addition & 0 deletions source/-bitcode/BitcodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "Support/SafeNames.hpp"
#include "AST/Bitcode.hpp"
#include <mrdox/Support/Report.hpp>
#include <mrdox/Support/Thread.hpp>
#include <mrdox/Metadata.hpp>

namespace clang {
Expand Down
41 changes: 23 additions & 18 deletions source/ConfigImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ struct llvm::yaml::MappingTraits<
namespace clang {
namespace mrdox {

Error
ConfigImpl::
construct(
ConfigImpl(
llvm::StringRef workingDir_,
llvm::StringRef configYaml_,
llvm::StringRef extraYaml_)
Expand All @@ -78,7 +77,7 @@ construct(
namespace path = llvm::sys::path;

if(! files::isAbsolute(workingDir_))
return Error("path \"{}\" is not absolute", workingDir_);
throw Error("path \"{}\" is not absolute", workingDir_);
workingDir = files::makeDirsy(files::normalizePath(workingDir_));
configYaml = configYaml_;
extraYaml = extraYaml_;
Expand All @@ -89,14 +88,14 @@ construct(
yin.setAllowUnknownKeys(true);
yin >> *this;
if(auto ec = yin.error())
return Error(ec);
throw Error(ec);
}
{
llvm::yaml::Input yin(extraYaml, this, yamlDiagnostic);
yin.setAllowUnknownKeys(true);
yin >> *this;
if(auto ec = yin.error())
return Error(ec);
throw Error(ec);
}

// Post-process as needed
Expand All @@ -112,13 +111,7 @@ construct(
name = files::makePosixStyle(
files::makeAbsolute(name, workingDir));

return Error::success();
}

ConfigImpl::
ConfigImpl()
: threadPool_(0)
{
threadPool_.reset(concurrency);
}

//------------------------------------------------
Expand Down Expand Up @@ -174,10 +167,16 @@ createConfigFromYAML(
llvm::StringRef configYaml,
llvm::StringRef extraYaml)
{
auto config = std::make_shared<ConfigImpl>();
if(auto err = config->construct(workingDir, configYaml, extraYaml))
try
{
auto config = std::make_shared<ConfigImpl>(
workingDir, configYaml, extraYaml);
return config;
}
catch(Error err)
{
return err;
return config;
}
}

Expected<std::shared_ptr<ConfigImpl const>>
Expand All @@ -202,10 +201,16 @@ loadConfigFile(
auto workingDir = files::getParentDir(*absPath);

// attempt to create the config
auto config = std::make_shared<ConfigImpl>();
if(auto err = config->construct(workingDir, *text, extraYaml))
try
{
auto config = std::make_shared<ConfigImpl>(
workingDir, *text, extraYaml);
return config;
}
catch(Error err)
{
return err;
return config;
}
}

} // mrdox
Expand Down
6 changes: 2 additions & 4 deletions source/ConfigImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,12 @@ class ConfigImpl
template<class T>
friend struct llvm::yaml::MappingTraits;

Error construct(
public:
ConfigImpl(
llvm::StringRef workingDir,
llvm::StringRef configYaml,
llvm::StringRef extraYaml);

public:
ConfigImpl();

ThreadPool&
threadPool() const noexcept override
{
Expand Down
11 changes: 11 additions & 0 deletions source/Support/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,20 @@ ThreadPool::
{
}

ThreadPool::
ThreadPool() = default;

ThreadPool::
ThreadPool(
unsigned concurrency)
{
reset(concurrency);
}

void
ThreadPool::
reset(
unsigned concurrency)
{
llvm::ThreadPoolStrategy S;
S.ThreadsRequested = concurrency;
Expand Down

0 comments on commit 993bfa1

Please sign in to comment.