Skip to content

Commit 4fb586d

Browse files
committed
chore: ToolExecutor refactoring
1 parent 668e4e9 commit 4fb586d

File tree

5 files changed

+71
-36
lines changed

5 files changed

+71
-36
lines changed

include/mrdox/Support/Error.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,14 @@ class [[nodiscard]] MRDOX_DECL
9696
source_location loc =
9797
source_location::current());
9898

99-
explicit
99+
/** Constructor.
100+
101+
This constructs a new error from a list
102+
of zero or more errors. If the list is empty,
103+
or if all of the errors in the list indicate
104+
success, then newly constructed object will
105+
indicate success.
106+
*/
100107
Error(
101108
std::vector<Error> const& errors,
102109
source_location loc =

source/Tool/GenerateAction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
// Official repository: https://github.com/cppalliance/mrdox
1010
//
1111

12-
#include "ToolArgs.hpp"
1312
#include "ConfigImpl.hpp"
1413
#include "CorpusImpl.hpp"
14+
#include "ToolArgs.hpp"
15+
#include "ToolExecutor.hpp"
1516
#include "AST/AbsoluteCompilationDatabase.hpp"
16-
#include "AST/ToolExecutor.hpp"
1717
#include <mrdox/Generators.hpp>
1818
#include <mrdox/Support/Report.hpp>
1919
#include <mrdox/Support/Path.hpp>

source/Tool/TestAction.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
// Official repository: https://github.com/cppalliance/mrdox
99
//
1010

11-
#include "SingleFileDB.hpp"
12-
#include "ToolArgs.hpp"
1311
#include "ConfigImpl.hpp"
1412
#include "CorpusImpl.hpp"
13+
#include "SingleFileDB.hpp"
14+
#include "ToolArgs.hpp"
15+
#include "ToolExecutor.hpp"
1516
#include "Support/Error.hpp"
1617
#include <mrdox/Config.hpp>
1718
#include <mrdox/Generators.hpp>
@@ -197,7 +198,7 @@ handleFile(
197198
std::unique_ptr<Corpus> corpus;
198199
{
199200
SingleFileDB db(dirPath, filePath);
200-
tooling::StandaloneToolExecutor ex(db, { std::string(filePath) });
201+
ToolExecutor ex(*config, db);
201202
auto result = CorpusImpl::build(ex, config);
202203
if(! result)
203204
{

source/AST/ToolExecutor.cpp renamed to source/Tool/ToolExecutor.cpp

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111

1212
#include "ToolExecutor.hpp"
13+
#include <mrdox/Support/Report.hpp>
1314
#include <mrdox/Support/ThreadPool.hpp>
1415
#include <clang/Tooling/ToolExecutorPluginRegistry.h>
1516
#include <llvm/Support/Regex.h>
@@ -112,49 +113,75 @@ execute(
112113
llvm::errs() << Msg.str() << "\n";
113114
};
114115

115-
auto Files = Compilations.getAllFiles();
116+
// Get a copy of the filename strings
117+
std::vector<std::string> Files = Compilations.getAllFiles();
116118

117119
// Add a counter to track the progress.
118-
const std::string TotalNumStr = std::to_string(Files.size());
120+
auto const TotalNumStr = std::to_string(Files.size());
119121
unsigned Counter = 0;
120122
auto Count = [&]()
121123
{
122124
std::unique_lock<std::mutex> LockGuard(TUMutex);
123125
return ++Counter;
124126
};
125127

126-
auto& Action = Actions.front();
128+
auto const& Action = Actions.front();
127129

128-
TaskGroup taskGroup(config_.threadPool());
130+
auto const processFile =
131+
[&](std::string Path)
132+
{
133+
if(config_.verboseOutput)
134+
Log("[" + std::to_string(Count()) + "/" + TotalNumStr + "] Processing file " + Path);
135+
136+
// Each thread gets an independent copy of a VFS to allow different
137+
// concurrent working directories.
138+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS =
139+
llvm::vfs::createPhysicalFileSystem();
140+
141+
tooling::ClangTool Tool( Compilations, { Path },
142+
std::make_shared<PCHContainerOperations>(), FS);
143+
Tool.appendArgumentsAdjuster(Action.second);
144+
Tool.appendArgumentsAdjuster(getDefaultArgumentsAdjusters());
145+
146+
for (const auto& FileAndContent : OverlayFiles)
147+
Tool.mapVirtualFile(FileAndContent.first(),
148+
FileAndContent.second);
149+
150+
// VFALCO This needs to be tested
151+
if (Tool.run(Action.first.get()))
152+
AppendError(llvm::Twine("Failed to run action on ") + Path + "\n");
153+
};
129154

130-
for (std::string File : Files)
155+
// Run the action on all files in the database
156+
std::vector<Error> errors;
157+
if(Files.size() > 1)
158+
{
159+
TaskGroup taskGroup(config_.threadPool());
160+
// VFALCO is File move-constructed?
161+
for(std::string File : std::move(Files))
162+
{
163+
taskGroup.async(
164+
[&, Path = std::move(File)]()
165+
{
166+
processFile(std::move(Path));
167+
});
168+
}
169+
errors = taskGroup.wait();
170+
}
171+
else
131172
{
132-
taskGroup.async(
133-
[&, Path = std::move(File)]()
173+
try
174+
{
175+
processFile(std::move(Files.front()));
176+
}
177+
catch(Exception const& ex)
134178
{
135-
if(config_.verboseOutput)
136-
Log("[" + std::to_string(Count()) + "/" + TotalNumStr + "] Processing file " + Path);
137-
138-
// Each thread gets an independent copy of a VFS to allow different
139-
// concurrent working directories.
140-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS =
141-
llvm::vfs::createPhysicalFileSystem();
142-
143-
tooling::ClangTool Tool( Compilations, { Path },
144-
std::make_shared<PCHContainerOperations>(), FS);
145-
Tool.appendArgumentsAdjuster(Action.second);
146-
Tool.appendArgumentsAdjuster(getDefaultArgumentsAdjusters());
147-
148-
for (const auto& FileAndContent : OverlayFiles)
149-
Tool.mapVirtualFile(FileAndContent.first(),
150-
FileAndContent.second);
151-
152-
if (Tool.run(Action.first.get()))
153-
AppendError(llvm::Twine("Failed to run action on ") + Path + "\n");
154-
});
179+
errors.push_back(ex.error());
180+
}
155181
}
156182

157-
auto errors = taskGroup.wait();
183+
if(! errors.empty())
184+
reportError(errors, "Could not run the tool executor");
158185

159186
if (!ErrorMsg.empty())
160187
return make_string_error(ErrorMsg);

source/AST/ToolExecutor.hpp renamed to source/Tool/ToolExecutor.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
// Official repository: https://github.com/cppalliance/mrdox
1010
//
1111

12-
#ifndef MRDOX_TOOL_AST_EXECUTION_HPP
13-
#define MRDOX_TOOL_AST_EXECUTION_HPP
12+
#ifndef MRDOX_TOOL_TOOL_TOOLEXECUTOR_HPP
13+
#define MRDOX_TOOL_TOOL_TOOLEXECUTOR_HPP
1414

1515
#include <mrdox/Config.hpp>
1616
#include <clang/Tooling/ArgumentsAdjusters.h>

0 commit comments

Comments
 (0)