ClangLoader: Pull out common remapped file operations
authorDave Marchevsky <davemarchevsky@fb.com>
Fri, 6 Aug 2021 05:12:17 +0000 (22:12 -0700)
committerDave Marchevsky <davemarchevsky@fb.com>
Fri, 6 Aug 2021 05:48:02 +0000 (22:48 -0700)
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.

src/cc/frontends/clang/loader.cc
src/cc/frontends/clang/loader.h

index 7809e45ebafa977cc589899beca7d8f5e176adb0..240569793e3b61b5964aef3f4fb211c362e95c39 100644 (file)
@@ -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
 {
 
@@ -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;
 
@@ -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();
@@ -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
index 176fc7efc8c23805b62d6e55722b968182c6092e..05db08cbfc9ddabeed6ef557572612f00c5da4af 100644 (file)
@@ -20,6 +20,8 @@
 #include <memory>
 #include <string>
 
+#include <clang/Frontend/CompilerInvocation.h>
+
 #include "table_storage.h"
 
 namespace llvm {
@@ -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_;