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

Ariel Bernal ariel.j.bernal at intel.com
Wed Sep 4 09:11:09 PDT 2013


  Addressed comments
  * If a user explicitly provided files only those files are transformed. If -exclude was provided then we filter out files matching the exclude list and a warning is generated.
  * If no explicitly file was provided and a compilation database was specified by -p then we transform all files in the database unless -include/exclude is provided.
  * Since SourcePaths is not mandatory we have to check for the case where neither a compilation database nor a source was provided.

Hi revane, Sarcasm, tareqsiraj,

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

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

Files:
  cpp11-migrate/Core/IncludeExcludeInfo.cpp
  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.cpp
===================================================================
--- cpp11-migrate/Core/IncludeExcludeInfo.cpp
+++ cpp11-migrate/Core/IncludeExcludeInfo.cpp
@@ -167,3 +167,13 @@
   // it is safe to transform.
   return true;
 }
+
+bool IncludeExcludeInfo::isFileExplicitlyExcluded(StringRef FilePath) const {
+  for (std::vector<std::string>::const_iterator I = ExcludeList.begin(),
+                                                E = ExcludeList.end();
+      I != E; ++I)
+    if (fileHasPathPrefix(FilePath, *I))
+      return true;
+
+  return false;
+}
Index: cpp11-migrate/Core/IncludeExcludeInfo.h
===================================================================
--- cpp11-migrate/Core/IncludeExcludeInfo.h
+++ cpp11-migrate/Core/IncludeExcludeInfo.h
@@ -48,6 +48,11 @@
   /// operators were removed.
   bool isFileIncluded(llvm::StringRef FilePath) const;
 
+  bool isFileExplicitlyExcluded(llvm::StringRef FilePath) const;
+
+  bool isIncludeListEmpty() const { return IncludeList.empty(); }
+  bool isExcludeListEmpty() const { return ExcludeList.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::ZeroOrMore);
 static cl::extrahelp MoreHelp(
     "EXAMPLES:\n\n"
     "Apply all transforms on a given file, no compilation database:\n\n"
@@ -236,21 +236,52 @@
       FixedCompilationDatabase::loadFromCommandLine(argc, argv));
   cl::ParseCommandLineOptions(argc, argv);
 
+  // Populate the ModifiableFiles structure.
+  GlobalOptions.ModifiableFiles.readListFromString(IncludePaths, ExcludePaths);
+  GlobalOptions.ModifiableFiles
+      .readListFromFile(IncludeFromFile, ExcludeFromFile);
+
+  // Remove explicitly provided sources that are in the exclude list.
+  if (!SourcePaths.empty() &&
+      !GlobalOptions.ModifiableFiles.isExcludeListEmpty())
+    for (std::vector<std::string>::iterator I = SourcePaths.begin();
+       I != SourcePaths.end();)
+      if (GlobalOptions.ModifiableFiles.isFileExplicitlyExcluded(*I)) {
+        llvm::errs() << "Warning \"" << *I << "\" will not be transformed "
+                     << "because it's in the excluded list.\n";
+        I = SourcePaths.erase(I);
+      } else
+        ++I;
+
   if (!Compilations) {
     std::string ErrorMessage;
     if (BuildPath.getNumOccurrences() > 0) {
       Compilations.reset(CompilationDatabase::autoDetectFromDirectory(
           BuildPath, ErrorMessage));
-    } else {
+      if (Compilations && 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 if (!SourcePaths.empty()) {
       Compilations.reset(CompilationDatabase::autoDetectFromSource(
           SourcePaths[0], ErrorMessage));
       // If no compilation database can be detected from source then we create
       // a new FixedCompilationDatabase with c++11 support.
       if (!Compilations) {
         std::string CommandLine[] = {"-std=c++11"};
         Compilations.reset(new FixedCompilationDatabase(".", CommandLine));
       }
-    }
+    } else
+      ErrorMessage = "No sources or compilation database were provided.";
     if (!Compilations)
       llvm::report_fatal_error(ErrorMessage);
   }
@@ -269,15 +300,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.3.patch
Type: text/x-patch
Size: 6780 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130904/3cb8212c/attachment.bin>


More information about the cfe-commits mailing list