Skip to content

Commit 76bfc5c

Browse files
committed
Recert: revert changes about function template run_clang.
1 parent 620bfbe commit 76bfc5c

File tree

1 file changed

+29
-39
lines changed

1 file changed

+29
-39
lines changed

src/Compiler/Compilation.cpp

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ class ProxyAction final : public clang::WrapperFrontendAction {
6868
ProxyAction(std::unique_ptr<clang::FrontendAction> action,
6969
bool collect_top_level_decls,
7070
std::shared_ptr<std::atomic_bool> stop) :
71-
clang::WrapperFrontendAction(std::move(action)),
72-
collect_top_level_decls(collect_top_level_decls), stop(std::move(stop)) {}
71+
clang::WrapperFrontendAction(std::move(action)), need_collect(collect_top_level_decls),
72+
stop(std::move(stop)) {}
7373

7474
auto CreateASTConsumer(clang::CompilerInstance& instance, llvm::StringRef file)
7575
-> std::unique_ptr<clang::ASTConsumer> final {
7676
return std::make_unique<ProxyASTConsumer>(
7777
WrapperFrontendAction::CreateASTConsumer(instance, file),
7878
instance,
79-
collect_top_level_decls ? &top_level_decls : nullptr,
79+
need_collect ? &top_level_decls : nullptr,
8080
std::move(stop));
8181
}
8282

@@ -88,7 +88,8 @@ class ProxyAction final : public clang::WrapperFrontendAction {
8888
}
8989

9090
private:
91-
bool collect_top_level_decls;
91+
/// Whether we need to collect top level declarations.
92+
bool need_collect;
9293
std::vector<clang::Decl*> top_level_decls;
9394
std::shared_ptr<std::atomic_bool> stop;
9495
};
@@ -147,16 +148,15 @@ auto create_invocation(CompilationParams& params,
147148
return invocation;
148149
}
149150

150-
/// Adjust the compiler instance before compile source code.
151-
using BeforeExecuteHook = llvm::unique_function<void(clang::CompilerInstance& instance)>;
152-
153-
/// Function to post process CompilationUnit .
154-
using AfterExecuteHook = llvm::unique_function<void(CompilationUnit&)>;
151+
/// Do nothing before or after compile state.
152+
constexpr static auto NoHook = [](auto& /*ignore*/) {
153+
};
155154

155+
template <typename Action>
156156
CompilationResult run_clang(CompilationParams& params,
157-
std::unique_ptr<ProxyAction> action,
158-
BeforeExecuteHook before,
159-
AfterExecuteHook after) {
157+
auto before_execute = NoHook,
158+
auto after_execute = NoHook,
159+
bool collect = false) {
160160
auto diagnostics =
161161
params.diagnostics ? params.diagnostics : std::make_shared<std::vector<Diagnostic>>();
162162
auto diagnostic_engine =
@@ -184,10 +184,9 @@ CompilationResult run_clang(CompilationParams& params,
184184
}
185185

186186
/// Adjust the compiler instance, for example, set preamble or modules.
187-
if(before) {
188-
before(*instance);
189-
}
187+
before_execute(*instance);
190188

189+
auto action = std::make_unique<ProxyAction>(std::make_unique<Action>(), collect, params.stop);
191190
if(!action->BeginSourceFile(*instance, instance->getFrontendOpts().Inputs[0])) {
192191
return std::unexpected("Fail to begin source file");
193192
}
@@ -262,33 +261,21 @@ CompilationResult run_clang(CompilationParams& params,
262261
};
263262

264263
CompilationUnit unit(CompilationUnit::SyntaxOnly, impl);
265-
if(after) {
266-
after(unit);
267-
}
264+
after_execute(unit);
268265
return unit;
269266
}
270267

271-
/// Separate the template and non-template parts of the function to speed up instantiation.
272-
template <typename Action>
273-
CompilationResult run_clang(CompilationParams& params,
274-
BeforeExecuteHook before = {},
275-
AfterExecuteHook after = {},
276-
bool collect_top_level_decls = false) {
277-
// rename to `collect` for pretty format.
278-
const bool collect = collect_top_level_decls;
279-
auto proxy = std::make_unique<ProxyAction>(std::make_unique<Action>(), collect, params.stop);
280-
return run_clang(params, std::move(proxy), std::move(before), std::move(after));
281-
}
282-
283268
} // namespace
284269

285270
CompilationResult preprocess(CompilationParams& params) {
286-
return run_clang<clang::PreprocessOnlyAction>(params);
271+
return run_clang<clang::PreprocessOnlyAction>(params,
272+
/*before_execute=*/NoHook,
273+
/*after_execute=*/NoHook);
287274
}
288275

289276
CompilationResult compile(CompilationParams& params) {
290277
const bool collect_top_level_decls = params.output_file.empty();
291-
return run_clang<clang::SyntaxOnlyAction>(params, {}, {}, collect_top_level_decls);
278+
return run_clang<clang::SyntaxOnlyAction>(params, NoHook, NoHook, collect_top_level_decls);
292279
}
293280

294281
CompilationResult compile(CompilationParams& params, PCHInfo& out) {
@@ -364,13 +351,16 @@ CompilationResult complete(CompilationParams& params, clang::CodeCompleteConsume
364351
column += 1;
365352
}
366353

367-
return run_clang<clang::SyntaxOnlyAction>(params, [&](clang::CompilerInstance& instance) {
368-
/// Set options to run code completion.
369-
instance.getFrontendOpts().CodeCompletionAt.FileName = std::move(file);
370-
instance.getFrontendOpts().CodeCompletionAt.Line = line;
371-
instance.getFrontendOpts().CodeCompletionAt.Column = column;
372-
instance.setCodeCompletionConsumer(consumer);
373-
});
354+
return run_clang<clang::SyntaxOnlyAction>(
355+
params,
356+
[&](clang::CompilerInstance& instance) {
357+
/// Set options to run code completion.
358+
instance.getFrontendOpts().CodeCompletionAt.FileName = std::move(file);
359+
instance.getFrontendOpts().CodeCompletionAt.Line = line;
360+
instance.getFrontendOpts().CodeCompletionAt.Column = column;
361+
instance.setCodeCompletionConsumer(consumer);
362+
},
363+
/*after_execute=*/NoHook);
374364
}
375365

376366
} // namespace clice

0 commit comments

Comments
 (0)