Skip to content

Commit

Permalink
ClangLoader: Pull out common remapped file operations
Browse files Browse the repository at this point in the history
I'm making some larger modifications to the loader. While reading
through the `do_compile` code I noticed that the common "remapped file"
operations - telling various CompilerInvocations about 'virtual'
includes and the virtual main c file - could be factored out to enhance
clarity.

This patch doesn't change functionality at all, nor does it try to make
any opinionated refactoring changes.
  • Loading branch information
davemarchevsky committed Aug 6, 2021
1 parent 10dac5f commit 2e4bb09
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
61 changes: 31 additions & 30 deletions src/cc/frontends/clang/loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,31 @@ ClangLoader::ClangLoader(llvm::LLVMContext *ctx, unsigned flags)

ClangLoader::~ClangLoader() {}

void ClangLoader::add_remapped_includes(clang::CompilerInvocation& invocation)
{
// This option instructs clang whether or not to free the file buffers that we
// give to it. Since the embedded header files should be copied fewer times
// and reused if possible, set this flag to true.
invocation.getPreprocessorOpts().RetainRemappedFileBuffers = true;
for (const auto &f : remapped_headers_)
invocation.getPreprocessorOpts().addRemappedFile(f.first, &*f.second);
for (const auto &f : remapped_footers_)
invocation.getPreprocessorOpts().addRemappedFile(f.first, &*f.second);
}

void ClangLoader::add_main_input(clang::CompilerInvocation& invocation,
const std::string& main_path,
llvm::MemoryBuffer *main_buf)
{
invocation.getPreprocessorOpts().addRemappedFile(main_path, main_buf);
invocation.getFrontendOpts().Inputs.clear();
invocation.getFrontendOpts().Inputs.push_back(
clang::FrontendInputFile(
main_path,
clang::FrontendOptions::getInputKindForExtension("c"))
);
}

namespace
{

Expand Down Expand Up @@ -375,17 +400,10 @@ int ClangLoader::do_compile(unique_ptr<llvm::Module> *mod, TableStorage &ts,
if (!CreateFromArgs(invocation0, ccargs, diags))
return -1;

invocation0.getPreprocessorOpts().RetainRemappedFileBuffers = true;
for (const auto &f : remapped_headers_)
invocation0.getPreprocessorOpts().addRemappedFile(f.first, &*f.second);
for (const auto &f : remapped_footers_)
invocation0.getPreprocessorOpts().addRemappedFile(f.first, &*f.second);
add_remapped_includes(invocation0);

if (in_memory) {
invocation0.getPreprocessorOpts().addRemappedFile(main_path, &*main_buf);
invocation0.getFrontendOpts().Inputs.clear();
invocation0.getFrontendOpts().Inputs.push_back(FrontendInputFile(
main_path, FrontendOptions::getInputKindForExtension("c")));
add_main_input(invocation0, main_path, &*main_buf);
}
invocation0.getFrontendOpts().DisableFree = false;

Expand All @@ -404,18 +422,8 @@ int ClangLoader::do_compile(unique_ptr<llvm::Module> *mod, TableStorage &ts,
if (!CreateFromArgs( invocation1, ccargs, diags))
return -1;

// This option instructs clang whether or not to free the file buffers that we
// give to it. Since the embedded header files should be copied fewer times
// and reused if possible, set this flag to true.
invocation1.getPreprocessorOpts().RetainRemappedFileBuffers = true;
for (const auto &f : remapped_headers_)
invocation1.getPreprocessorOpts().addRemappedFile(f.first, &*f.second);
for (const auto &f : remapped_footers_)
invocation1.getPreprocessorOpts().addRemappedFile(f.first, &*f.second);
invocation1.getPreprocessorOpts().addRemappedFile(main_path, &*out_buf);
invocation1.getFrontendOpts().Inputs.clear();
invocation1.getFrontendOpts().Inputs.push_back(FrontendInputFile(
main_path, FrontendOptions::getInputKindForExtension("c")));
add_remapped_includes(invocation1);
add_main_input(invocation1, main_path, &*out_buf);
invocation1.getFrontendOpts().DisableFree = false;

compiler1.createDiagnostics();
Expand All @@ -435,15 +443,8 @@ int ClangLoader::do_compile(unique_ptr<llvm::Module> *mod, TableStorage &ts,
if (!CreateFromArgs(invocation2, ccargs, diags))
return -1;

invocation2.getPreprocessorOpts().RetainRemappedFileBuffers = true;
for (const auto &f : remapped_headers_)
invocation2.getPreprocessorOpts().addRemappedFile(f.first, &*f.second);
for (const auto &f : remapped_footers_)
invocation2.getPreprocessorOpts().addRemappedFile(f.first, &*f.second);
invocation2.getPreprocessorOpts().addRemappedFile(main_path, &*out_buf1);
invocation2.getFrontendOpts().Inputs.clear();
invocation2.getFrontendOpts().Inputs.push_back(FrontendInputFile(
main_path, FrontendOptions::getInputKindForExtension("c")));
add_remapped_includes(invocation2);
add_main_input(invocation2, main_path, &*out_buf1);
invocation2.getFrontendOpts().DisableFree = false;
invocation2.getCodeGenOpts().DisableFree = false;
// Resort to normal inlining. In -O0 the default is OnlyAlwaysInlining and
Expand Down
4 changes: 4 additions & 0 deletions src/cc/frontends/clang/loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ class ClangLoader {
const std::string &maps_ns,
fake_fd_map_def &fake_fd_map,
std::map<std::string, std::vector<std::string>> &perf_events);
void add_remapped_includes(clang::CompilerInvocation& invocation);
void add_main_input(clang::CompilerInvocation& invocation,
const std::string& main_path,
llvm::MemoryBuffer *main_buf);

private:
std::map<std::string, std::unique_ptr<llvm::MemoryBuffer>> remapped_headers_;
Expand Down

0 comments on commit 2e4bb09

Please sign in to comment.