Skip to content

Commit

Permalink
Merge pull request #3566 from davemarchevsky/davemarchevsky_remapped_…
Browse files Browse the repository at this point in the history
…refactor

ClangLoader: Pull out common remapped file operations
  • Loading branch information
davemarchevsky authored Aug 6, 2021
2 parents 10dac5f + c8d65dc commit b768015
Show file tree
Hide file tree
Showing 2 changed files with 37 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
6 changes: 6 additions & 0 deletions src/cc/frontends/clang/loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <memory>
#include <string>

#include <clang/Frontend/CompilerInvocation.h>

#include "table_storage.h"

namespace llvm {
Expand Down Expand Up @@ -69,6 +71,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 b768015

Please sign in to comment.