[PATCH] Transform all files in a compilation database if no source files are provided.

Ariel Bernal ariel.j.bernal at intel.com
Tue Aug 27 23:22:48 PDT 2013


  Addressed comments

Hi revane, Sarcasm, tareqsiraj,

http://llvm-reviews.chandlerc.com/D1517

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D1517?vs=3755&id=3853#toc

Files:
  cpp11-migrate/Core/IncludeExcludeInfo.h
  cpp11-migrate/Core/Transform.cpp
  cpp11-migrate/Core/Transform.h
  cpp11-migrate/tool/Cpp11Migrate.cpp
  unittests/cpp11-migrate/TransformTest.cpp

Index: cpp11-migrate/Core/IncludeExcludeInfo.h
===================================================================
--- cpp11-migrate/Core/IncludeExcludeInfo.h
+++ cpp11-migrate/Core/IncludeExcludeInfo.h
@@ -48,6 +48,8 @@
   /// operators were removed.
   bool isFileIncluded(llvm::StringRef FilePath) const;
 
+  bool isIncludeListEmpty() const { return IncludeList.empty(); }
+
 private:
   std::vector<std::string> IncludeList;
   std::vector<std::string> ExcludeList;
Index: cpp11-migrate/Core/Transform.cpp
===================================================================
--- cpp11-migrate/Core/Transform.cpp
+++ cpp11-migrate/Core/Transform.cpp
@@ -94,7 +94,7 @@
   if (!FE)
     return false;
 
-  return GlobalOptions.ModifiableHeaders.isFileIncluded(FE->getName());
+  return GlobalOptions.ModifiableFiles.isFileIncluded(FE->getName());
 }
 
 bool Transform::handleBeginSource(CompilerInstance &CI, StringRef Filename) {
Index: cpp11-migrate/Core/Transform.h
===================================================================
--- cpp11-migrate/Core/Transform.h
+++ cpp11-migrate/Core/Transform.h
@@ -61,13 +61,13 @@
   bool EnableTiming;
 
   /// \brief Allow changes to headers included from the main source file.
-  /// Transform sub-classes should use ModifiableHeaders to determine which
+  /// Transform sub-classes should use ModifiableFiles to determine which
   /// headers are modifiable and which are not.
   bool EnableHeaderModifications;
 
-  /// \brief Contains information on which headers are safe to transform and
+  /// \brief Contains information on which files are safe to transform and
   /// which aren't.
-  IncludeExcludeInfo ModifiableHeaders;
+  IncludeExcludeInfo ModifiableFiles;
 
   /// \brief Maximum allowed level of risk.
   RiskLevel MaxRiskLevel;
Index: cpp11-migrate/tool/Cpp11Migrate.cpp
===================================================================
--- cpp11-migrate/tool/Cpp11Migrate.cpp
+++ cpp11-migrate/tool/Cpp11Migrate.cpp
@@ -39,7 +39,7 @@
 static cl::opt<std::string> BuildPath(
     "p", cl::desc("Build Path"), cl::Optional);
 static cl::list<std::string> SourcePaths(
-    cl::Positional, cl::desc("<source0> [... <sourceN>]"), cl::OneOrMore);
+    cl::Positional, cl::desc("[<source0> ... <sourceN>]"), cl::Optional);
 static cl::extrahelp MoreHelp(
     "EXAMPLES:\n\n"
     "Apply all transforms on a given file, no compilation database:\n\n"
@@ -236,11 +236,29 @@
       FixedCompilationDatabase::loadFromCommandLine(argc, argv));
   cl::ParseCommandLineOptions(argc, argv);
 
+  // Populate the ModifiableFiles structure.
+  GlobalOptions.ModifiableFiles.readListFromString(IncludePaths, ExcludePaths);
+  GlobalOptions.ModifiableFiles
+      .readListFromFile(IncludeFromFile, ExcludeFromFile);
+
   if (!Compilations) {
     std::string ErrorMessage;
     if (BuildPath.getNumOccurrences() > 0) {
       Compilations.reset(CompilationDatabase::autoDetectFromDirectory(
           BuildPath, ErrorMessage));
+      if (SourcePaths.empty()) {
+        // Use source paths from the compilation database.
+        std::vector<std::string> Files = Compilations->getAllFiles();
+        for (std::vector<std::string>::iterator I = Files.begin(),
+                                                E = Files.end();
+            I != E; ++I) {
+          // We only transform a file if include paths are not specified or
+          // if the file is included when include/exclude paths are specified.
+          if (GlobalOptions.ModifiableFiles.isIncludeListEmpty() ||
+              GlobalOptions.ModifiableFiles.isFileIncluded(*I))
+            SourcePaths.addValue(*I);
+        }
+      }
     } else {
       Compilations.reset(CompilationDatabase::autoDetectFromSource(
           SourcePaths[0], ErrorMessage));
@@ -255,6 +273,22 @@
       llvm::report_fatal_error(ErrorMessage);
   }
 
+  // Populate include paths with paths from the compilation database.
+  if (GlobalOptions.ModifiableFiles.isIncludeListEmpty()) {
+    std::vector<std::string> Files = Compilations->getAllFiles();
+    std::string IncludeString;
+    for (std::vector<std::string>::iterator I = Files.begin(),
+                                            E = Files.end();
+        I != E; ++I) {
+      llvm::SmallString<64> PathString(*I);
+      llvm::sys::path::remove_filename(PathString);
+      if (!IncludeString.empty())
+        IncludeString += ",";
+      IncludeString += PathString.str();
+    }
+    GlobalOptions.ModifiableFiles.readListFromString(IncludeString, "");
+  }
+
   // Since ExecutionTimeDirectoryName could be an empty string we compare
   // against the default value when the command line option is not specified.
   GlobalOptions.EnableTiming = (TimingDirectoryName != NoTiming);
@@ -269,15 +303,6 @@
   if (CmdSwitchError)
     return 1;
 
-  // Populate the ModifiableHeaders structure if header modifications are
-  // enabled.
-  if (GlobalOptions.EnableHeaderModifications) {
-    GlobalOptions.ModifiableHeaders
-        .readListFromString(IncludePaths, ExcludePaths);
-    GlobalOptions.ModifiableHeaders
-        .readListFromFile(IncludeFromFile, ExcludeFromFile);
-  }
-
   TransformManager.createSelectedTransforms(GlobalOptions, RequiredVersions);
 
   if (TransformManager.begin() == TransformManager.end()) {
Index: unittests/cpp11-migrate/TransformTest.cpp
===================================================================
--- unittests/cpp11-migrate/TransformTest.cpp
+++ unittests/cpp11-migrate/TransformTest.cpp
@@ -267,7 +267,7 @@
   StringRef ExcludeDir = llvm::sys::path::parent_path(HeaderBFile);
 
   IncludeExcludeInfo IncInfo;
-  Options.ModifiableHeaders.readListFromString(CurrentDir, ExcludeDir);
+  Options.ModifiableFiles.readListFromString(CurrentDir, ExcludeDir);
 
   tooling::FixedCompilationDatabase Compilations(CurrentDir.str(),
                                                  std::vector<std::string>());
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1517.2.patch
Type: text/x-patch
Size: 5923 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130827/2e755270/attachment.bin>


More information about the cfe-commits mailing list