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

Ariel Bernal ariel.j.bernal at intel.com
Thu Sep 5 13:10:35 PDT 2013


  Addressed comments, I'll be uploading a new patch with changes to documentation shortly.

Hi revane, Sarcasm, tareqsiraj,

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

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

Files:
  clang-modernize/Core/IncludeExcludeInfo.cpp
  clang-modernize/Core/IncludeExcludeInfo.h
  clang-modernize/Core/Transform.cpp
  clang-modernize/Core/Transform.h
  clang-modernize/tool/ClangModernize.cpp
  unittests/clang-modernize/TransformTest.cpp

Index: clang-modernize/Core/IncludeExcludeInfo.cpp
===================================================================
--- clang-modernize/Core/IncludeExcludeInfo.cpp
+++ clang-modernize/Core/IncludeExcludeInfo.cpp
@@ -157,13 +157,17 @@
   if (!InIncludeList)
     return false;
 
+  // If the file is in the included list but not is not explicitly excluded,
+  // then it is safe to transform.
+  return !isFileExplicitlyExcluded(FilePath);
+}
+
+bool IncludeExcludeInfo::isFileExplicitlyExcluded(StringRef FilePath) const {
   for (std::vector<std::string>::const_iterator I = ExcludeList.begin(),
                                                 E = ExcludeList.end();
-       I != E; ++I)
+      I != E; ++I)
     if (fileHasPathPrefix(FilePath, *I))
-      return false;
+      return true;
 
-  // If the file is in the included list but not in the excluded list, then
-  // it is safe to transform.
-  return true;
+  return false;
 }
Index: clang-modernize/Core/IncludeExcludeInfo.h
===================================================================
--- clang-modernize/Core/IncludeExcludeInfo.h
+++ clang-modernize/Core/IncludeExcludeInfo.h
@@ -48,6 +48,15 @@
   /// operators were removed.
   bool isFileIncluded(llvm::StringRef FilePath) const;
 
+  /// \brief Determine if a file path was explicitly excluded.
+  bool isFileExplicitlyExcluded(llvm::StringRef FilePath) const;
+
+  /// \brief Determine if a list of include paths was provided.
+  bool isIncludeListEmpty() const { return IncludeList.empty(); }
+
+  /// \brief Determine if a list of exclude paths was provided.
+  bool isExcludeListEmpty() const { return ExcludeList.empty(); }
+
 private:
   std::vector<std::string> IncludeList;
   std::vector<std::string> ExcludeList;
Index: clang-modernize/Core/Transform.cpp
===================================================================
--- clang-modernize/Core/Transform.cpp
+++ clang-modernize/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: clang-modernize/Core/Transform.h
===================================================================
--- clang-modernize/Core/Transform.h
+++ clang-modernize/Core/Transform.h
@@ -67,13 +67,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: clang-modernize/tool/ClangModernize.cpp
===================================================================
--- clang-modernize/tool/ClangModernize.cpp
+++ clang-modernize/tool/ClangModernize.cpp
@@ -44,7 +44,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("[<sources>...]"), cl::ZeroOrMore);
 static cl::extrahelp MoreHelp(
     "EXAMPLES:\n\n"
     "Apply all transforms on a given file, no compilation database:\n\n"
@@ -303,12 +303,23 @@
       FixedCompilationDatabase::loadFromCommandLine(argc, argv));
   cl::ParseCommandLineOptions(argc, argv);
 
+  // Populate the ModifiableFiles structure.
+  GlobalOptions.ModifiableFiles.readListFromString(IncludePaths, ExcludePaths);
+  GlobalOptions.ModifiableFiles.readListFromFile(IncludeFromFile,
+                                                 ExcludeFromFile);
+
+  // If no fixed compilation database is provided then we try to autodetect a
+  // compilation database from a directory (-p) or from the first source.
   if (!Compilations) {
     std::string ErrorMessage;
     if (BuildPath.getNumOccurrences() > 0) {
+      // Detect compilation database from a Directory.
       Compilations.reset(CompilationDatabase::autoDetectFromDirectory(
           BuildPath, ErrorMessage));
-    } else {
+      if (!Compilations)
+        llvm::report_fatal_error(ErrorMessage);
+    } else if (!SourcePaths.empty()) {
+      // Detect compilation database from the first source path.
       Compilations.reset(CompilationDatabase::autoDetectFromSource(
           SourcePaths[0], ErrorMessage));
       // If no compilation database can be detected from source then we create
@@ -318,10 +329,38 @@
         Compilations.reset(new FixedCompilationDatabase(".", CommandLine));
       }
     }
-    if (!Compilations)
-      llvm::report_fatal_error(ErrorMessage);
   }
 
+  if (!SourcePaths.empty()) {
+    if(!GlobalOptions.ModifiableFiles.isExcludeListEmpty())
+      // Remove explicitly provided sources that are in the exclude list.
+      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;
+  } else {
+    // Use source paths from the compilation database.
+    std::vector<std::string> Files = Compilations->getAllFiles();
+    for (std::vector<std::string>::const_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);
+    }
+  }
+
+  // There is no source to transform.
+  if (SourcePaths.empty())
+    llvm::report_fatal_error(
+        "No sources or compilation database were provided.");
+
   // 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);
@@ -336,15 +375,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);
 
   llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagOpts(
Index: unittests/clang-modernize/TransformTest.cpp
===================================================================
--- unittests/clang-modernize/TransformTest.cpp
+++ unittests/clang-modernize/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.4.patch
Type: text/x-patch
Size: 7860 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130905/d4dbcba5/attachment.bin>


More information about the cfe-commits mailing list