[clang-tools-extra] r188371 - cpp11-migrate: Adding -yaml-only option

Edwin Vane edwin.vane at intel.com
Wed Aug 14 07:52:45 PDT 2013


Author: revane
Date: Wed Aug 14 09:52:45 2013
New Revision: 188371

URL: http://llvm.org/viewvc/llvm-project?rev=188371&view=rev
Log:
cpp11-migrate: Adding -yaml-only option

For use with -headers, -yaml-only will cause cpp11-migrate to not write header
changes to disk and instead write them as header change description files. This
option facilitiates upcoming functionality to properly support changing headers
as part of migration.

Differential Revision: http://llvm-reviews.chandlerc.com/D1385


Modified:
    clang-tools-extra/trunk/cpp11-migrate/tool/Cpp11Migrate.cpp
    clang-tools-extra/trunk/test/cpp11-migrate/HeaderReplacements/main.cpp

Modified: clang-tools-extra/trunk/cpp11-migrate/tool/Cpp11Migrate.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/cpp11-migrate/tool/Cpp11Migrate.cpp?rev=188371&r1=188370&r2=188371&view=diff
==============================================================================
--- clang-tools-extra/trunk/cpp11-migrate/tool/Cpp11Migrate.cpp (original)
+++ clang-tools-extra/trunk/cpp11-migrate/tool/Cpp11Migrate.cpp Wed Aug 14 09:52:45 2013
@@ -118,6 +118,13 @@ static cl::opt<bool, /*ExternalStorage=*
     cl::location(GlobalOptions.EnableHeaderModifications),
     cl::init(false));
 
+static cl::opt<bool> YAMLOnly("yaml-only",
+                              cl::Hidden, // Associated with -headers
+                              cl::desc("Don't change headers on disk. Write "
+                                       "changes to change description files "
+                                       "only."),
+                              cl::init(false));
+
 cl::opt<std::string> SupportedCompilers(
     "for-compilers", cl::value_desc("string"),
     cl::desc("Select transforms targeting the intersection of\n"
@@ -257,6 +264,13 @@ int main(int argc, const char **argv) {
     return 1;
   }
 
+  if (std::distance(TransformManager.begin(), TransformManager.end()) > 1) {
+    llvm::errs() << "Header change description files requested for multiple "
+                    "transforms.\nChanges from only one transform can be "
+                    "recorded in a change description file.\n";
+    return 1;
+  }
+
   // if reformatting is enabled we wants to track file changes so that it's
   // possible to reformat them.
   bool TrackReplacements = static_cast<bool>(ChangesReformatter);
@@ -317,48 +331,48 @@ int main(int argc, const char **argv) {
       FileStream << Overrides.getMainFileContent();
     }
 
-    // FIXME: The Migrator shouldn't be responsible for writing headers to disk.
-    // Instead, it should write replacement info and another tool should take
-    // all replacement info for a header from possibly many other migration
-    // processes and merge it into a final form. For now, the updated header is
-    // written to disk for testing purposes.
     for (HeaderOverrides::const_iterator HeaderI = Overrides.headers_begin(),
                                          HeaderE = Overrides.headers_end();
          HeaderI != HeaderE; ++HeaderI) {
-      assert(!HeaderI->second.getContentOverride().empty() &&
-             "A header override should not be empty");
       std::string ErrorInfo;
-      std::string HeaderFileName = HeaderI->getKey();
-      llvm::raw_fd_ostream HeaderStream(HeaderFileName.c_str(), ErrorInfo,
-                                        llvm::sys::fs::F_Binary);
-      if (!ErrorInfo.empty()) {
-        llvm::errs() << "Error opening file: " << ErrorInfo << "\n";
-        continue;
-      }
-      HeaderStream << HeaderI->second.getContentOverride();
-
-      // Replacements for header files need to be written in a YAML file for
-      // every transform and will be merged together with an external tool.
-      llvm::SmallString<128> ReplacementsHeaderName;
-      llvm::SmallString<64> Error;
-      bool Result = generateReplacementsFileName(I->getKey(), HeaderFileName,
-                                                 ReplacementsHeaderName, Error);
-      if (!Result) {
-        llvm::errs() << "Failed to generate replacements filename:" << Error
-                     << "\n";
-        continue;
-      }
-
-      llvm::raw_fd_ostream ReplacementsFile(ReplacementsHeaderName.c_str(),
-                                            ErrorInfo,
-                                            llvm::sys::fs::F_Binary);
-      if (!ErrorInfo.empty()) {
-        llvm::errs() << "Error opening file: " << ErrorInfo << "\n";
-        continue;
+      if (YAMLOnly) {
+        // Replacements for header files need to be written in a YAML file for
+        // every transform and will be merged together with an external tool.
+        llvm::SmallString<128> ReplacementsHeaderName;
+        llvm::SmallString<64> Error;
+        bool Result =
+            generateReplacementsFileName(I->getKey(), HeaderI->getKey().data(),
+                                         ReplacementsHeaderName, Error);
+        if (!Result) {
+          llvm::errs() << "Failed to generate replacements filename:" << Error
+                       << "\n";
+          continue;
+        }
+
+        llvm::raw_fd_ostream ReplacementsFile(
+            ReplacementsHeaderName.c_str(), ErrorInfo, llvm::sys::fs::F_Binary);
+        if (!ErrorInfo.empty()) {
+          llvm::errs() << "Error opening file: " << ErrorInfo << "\n";
+          continue;
+        }
+        llvm::yaml::Output YAML(ReplacementsFile);
+        YAML << const_cast<HeaderChangeDocument &>(
+                    HeaderI->getValue().getHeaderChangeDoc());
+      } else {
+        // If -yaml-only was not specified, then change headers on disk.
+        // FIXME: This is transitional behaviour. Remove this functionality
+        // when header change description tool is ready.
+        assert(!HeaderI->second.getContentOverride().empty() &&
+               "A header override should not be empty");
+        std::string HeaderFileName = HeaderI->getKey();
+        llvm::raw_fd_ostream HeaderStream(HeaderFileName.c_str(), ErrorInfo,
+                                          llvm::sys::fs::F_Binary);
+        if (!ErrorInfo.empty()) {
+          llvm::errs() << "Error opening file: " << ErrorInfo << "\n";
+          continue;
+        }
+        HeaderStream << HeaderI->second.getContentOverride();
       }
-      llvm::yaml::Output YAML(ReplacementsFile);
-      YAML << const_cast<HeaderChangeDocument &>(
-                  HeaderI->getValue().getHeaderChangeDoc());
     }
   }
 

Modified: clang-tools-extra/trunk/test/cpp11-migrate/HeaderReplacements/main.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/cpp11-migrate/HeaderReplacements/main.cpp?rev=188371&r1=188370&r2=188371&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/cpp11-migrate/HeaderReplacements/main.cpp (original)
+++ clang-tools-extra/trunk/test/cpp11-migrate/HeaderReplacements/main.cpp Wed Aug 14 09:52:45 2013
@@ -4,7 +4,7 @@
 // RUN: rm -rf %t/Test
 // RUN: mkdir -p %t/Test
 // RUN: cp %S/main.cpp %S/common.cpp %S/common.h %t/Test
-// RUN: cpp11-migrate -loop-convert -headers -include=%t/Test %t/Test/main.cpp %t/Test/common.cpp --
+// RUN: cpp11-migrate -loop-convert -headers -yaml-only -include=%t/Test %t/Test/main.cpp %t/Test/common.cpp --
 // Check that only 1 file is generated per translation unit and header file.
 // RUN: ls -1 %t/Test | FileCheck %s --check-prefix=MAIN_CPP
 // RUN: ls -1 %t/Test | FileCheck %s --check-prefix=COMMON_CPP
@@ -22,7 +22,7 @@
 // RUN: rm -rf %t/Test
 // RUN: mkdir -p %t/Test
 // RUN: cp %S/main.cpp %S/common.cpp %S/common.h %t/Test
-// RUN: cpp11-migrate -loop-convert -headers -include=%t/Test %t/Test/main.cpp --
+// RUN: cpp11-migrate -loop-convert -headers -yaml-only -include=%t/Test %t/Test/main.cpp --
 // RUN: cpp11-migrate -loop-convert %t/Test/common.cpp --
 // Check that only one YAML file is generated from main.cpp and common.h and not from common.cpp and common.h since -header is not specified
 // RUN: ls -1 %t/Test | FileCheck %s --check-prefix=MAIN_CPP





More information about the cfe-commits mailing list