[cfe-commits] r90307 - in /cfe/trunk: include/clang/Basic/DiagnosticFrontendKinds.td include/clang/Frontend/PreprocessorOptions.h lib/Frontend/CompilerInstance.cpp test/Misc/Inputs/ test/Misc/Inputs/remapped-file test/Misc/remap-file.c tools/clang-cc/Options.cpp

Douglas Gregor dgregor at apple.com
Wed Dec 2 00:08:40 PST 2009


Author: dgregor
Date: Wed Dec  2 02:08:39 2009
New Revision: 90307

URL: http://llvm.org/viewvc/llvm-project?rev=90307&view=rev
Log:
Introduce a new clang-cc option 

  -remap-file=from;to

which takes the file "from" and transparently replaces its contents
with the contents of the file "to" from the source manager's
perspective. This is the moral equivalent of

  cp from saved
  cp to from
  <call clang>
  cp saved from
  rm saved

without all of the pesky file copying.


Added:
    cfe/trunk/test/Misc/Inputs/
    cfe/trunk/test/Misc/Inputs/remapped-file
    cfe/trunk/test/Misc/remap-file.c   (with props)
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td
    cfe/trunk/include/clang/Frontend/PreprocessorOptions.h
    cfe/trunk/lib/Frontend/CompilerInstance.cpp
    cfe/trunk/tools/clang-cc/Options.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td?rev=90307&r1=90306&r2=90307&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td Wed Dec  2 02:08:39 2009
@@ -33,6 +33,10 @@
     "unable to handle compilation, expected exactly one compiler job in '%0'">;
 def err_fe_expected_clang_command : Error<
     "expected a clang compiler command">;
+def err_fe_remap_missing_to_file : Error<
+    "could not remap file '%0' to the contents of file '%1'">, DefaultFatal;
+def err_fe_remap_missing_from_file : Error<
+    "could not remap from missing file '%0'">, DefaultFatal;
 
 def err_verify_bogus_characters : Error<
     "bogus characters before '{{' in expected string">;

Modified: cfe/trunk/include/clang/Frontend/PreprocessorOptions.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PreprocessorOptions.h?rev=90307&r1=90306&r2=90307&view=diff

==============================================================================
--- cfe/trunk/include/clang/Frontend/PreprocessorOptions.h (original)
+++ cfe/trunk/include/clang/Frontend/PreprocessorOptions.h Wed Dec  2 02:08:39 2009
@@ -13,6 +13,7 @@
 #include "llvm/ADT/StringRef.h"
 #include <cassert>
 #include <string>
+#include <utility>
 #include <vector>
 
 namespace clang {
@@ -41,6 +42,21 @@
   /// If given, a PTH cache file to use for speeding up header parsing.
   std::string TokenCache;
 
+  /// \brief The set of file remappings, which take existing files on
+  /// the system (the first part of each pair) and gives them the
+  /// contents of other files on the system (the second part of each
+  /// pair).
+  std::vector<std::pair<std::string, std::string> >  RemappedFiles;
+
+  typedef std::vector<std::pair<std::string, std::string> >::const_iterator
+    remapped_file_iterator;
+  remapped_file_iterator remapped_file_begin() const { 
+    return RemappedFiles.begin();
+  }
+  remapped_file_iterator remapped_file_end() const { 
+    return RemappedFiles.end();
+  }
+
 public:
   PreprocessorOptions() : UsePredefines(true) {}
 
@@ -50,6 +66,9 @@
   void addMacroUndef(llvm::StringRef Name) {
     Macros.push_back(std::make_pair(Name, true));
   }
+  void addRemappedFile(llvm::StringRef From, llvm::StringRef To) {
+    RemappedFiles.push_back(std::make_pair(From, To));
+  }
 };
 
 } // end namespace clang

Modified: cfe/trunk/lib/Frontend/CompilerInstance.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInstance.cpp?rev=90307&r1=90306&r2=90307&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInstance.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInstance.cpp Wed Dec  2 02:08:39 2009
@@ -190,6 +190,45 @@
     PP->setPTHManager(PTHMgr);
   }
 
+  // Remap files in the source manager.
+  for (PreprocessorOptions::remapped_file_iterator
+         Remap = PPOpts.remapped_file_begin(),
+         RemapEnd = PPOpts.remapped_file_end();
+       Remap != RemapEnd;
+       ++Remap) {
+    // Find the file that we're mapping to.
+    const FileEntry *ToFile = FileMgr.getFile(Remap->second);
+    if (!ToFile) {
+      Diags.Report(diag::err_fe_remap_missing_to_file)
+        << Remap->first << Remap->second;
+      continue;
+    }
+
+    // Find the file that we're mapping from.
+    const FileEntry *FromFile = FileMgr.getFile(Remap->first);
+    if (!FromFile) {
+      // FIXME: We could actually recover from this, by faking a
+      // FileEntry based on the "ToFile".
+      Diags.Report(diag::err_fe_remap_missing_from_file)
+        << Remap->first;
+      continue;
+    }
+
+    // Load the contents of the file we're mapping to.
+    std::string ErrorStr;
+    const llvm::MemoryBuffer *Buffer
+      = llvm::MemoryBuffer::getFile(ToFile->getName(), &ErrorStr);
+    if (!Buffer) {
+      Diags.Report(diag::err_fe_error_opening)
+        << Remap->second << ErrorStr;
+      continue;
+    }
+
+    // Override the contents of the "from" file with the contents of
+    // the "to" file.
+    SourceMgr.overrideFileContents(FromFile, Buffer);
+  }
+
   InitializePreprocessor(*PP, PPOpts, HSOpts);
 
   // Handle generating dependencies, if requested.

Added: cfe/trunk/test/Misc/Inputs/remapped-file
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Misc/Inputs/remapped-file?rev=90307&view=auto

==============================================================================
--- cfe/trunk/test/Misc/Inputs/remapped-file (added)
+++ cfe/trunk/test/Misc/Inputs/remapped-file Wed Dec  2 02:08:39 2009
@@ -0,0 +1 @@
+int *f(float *fp) { return fp; }

Added: cfe/trunk/test/Misc/remap-file.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Misc/remap-file.c?rev=90307&view=auto

==============================================================================
--- cfe/trunk/test/Misc/remap-file.c (added)
+++ cfe/trunk/test/Misc/remap-file.c Wed Dec  2 02:08:39 2009
@@ -0,0 +1,5 @@
+// RUN: clang-cc -remap-file="%s;%S/Inputs/remapped-file" -fsyntax-only %s 2>&1 | FileCheck %s
+
+// CHECK: remap-file.c:1:28: warning: incompatible pointer types
+
+int

Propchange: cfe/trunk/test/Misc/remap-file.c

------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/Misc/remap-file.c

------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/Misc/remap-file.c

------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: cfe/trunk/tools/clang-cc/Options.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-cc/Options.cpp?rev=90307&r1=90306&r2=90307&view=diff

==============================================================================
--- cfe/trunk/tools/clang-cc/Options.cpp (original)
+++ cfe/trunk/tools/clang-cc/Options.cpp Wed Dec  2 02:08:39 2009
@@ -673,6 +673,10 @@
 UndefMacros("undef", llvm::cl::value_desc("macro"),
             llvm::cl::desc("undef all system defines"));
 
+static llvm::cl::list<std::string>
+RemappedFiles("remap-file", llvm::cl::value_desc("<from>;<to>"),
+              llvm::cl::desc("replace the contents of the <from> file with the contents of the <to> file"));
+
 }
 
 //===----------------------------------------------------------------------===//
@@ -1071,6 +1075,20 @@
 
   for (unsigned i = 0, e = OrderedPaths.size(); i != e; ++i)
     Opts.Includes.push_back(*OrderedPaths[i].second);
+
+  // Handle file remapping.
+  for (unsigned i = 0, e = RemappedFiles.size(); i != e; ++i) {
+    std::string::size_type Semi = RemappedFiles[i].find(';');
+    if (Semi == std::string::npos) {
+      // FIXME: Don't fail like this.
+      fprintf(stderr, 
+              "error: -remap-file not of the form <from-file>;<to-file>\n");
+      continue;
+    }
+
+    Opts.addRemappedFile(llvm::StringRef(RemappedFiles[i].c_str(), Semi),
+                         llvm::StringRef(RemappedFiles[i].c_str() + Semi + 1));
+  }
 }
 
 void clang::InitializeLangOptions(LangOptions &Options,





More information about the cfe-commits mailing list