Reviewer changes and other polish:

    * Diagnostics not passed as an argument where SourceManager is.
    * Renamed applyReplacements to better indicate what it does.
    * Introduced the clang::replace namespace.
    * Various renaming of variables and updating of docs to not mention
      'documents' any more.

Hi klimek, djasper, silvas, tareqsiraj, arielbernal, Sarcasm,

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

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D1424?vs=3581&id=3628#toc

Files:
  CMakeLists.txt
  Makefile
  clang-replace/ApplyReplacements.cpp
  clang-replace/ApplyReplacements.h
  clang-replace/CMakeLists.txt
  clang-replace/Makefile
  clang-replace/tool/CMakeLists.txt
  clang-replace/tool/ClangReplaceMain.cpp
  clang-replace/tool/Makefile
  test/CMakeLists.txt
  test/clang-replace/conflict.cpp
  test/clang-replace/conflict/common.h
  test/clang-replace/conflict/expected.txt
  test/clang-replace/conflict/file1.yaml
  test/clang-replace/conflict/file2.yaml
  test/clang-replace/conflict/file3.yaml
Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt
+++ CMakeLists.txt
@@ -1,6 +1,7 @@
 add_subdirectory(remove-cstr-calls)
 add_subdirectory(tool-template)
 add_subdirectory(cpp11-migrate)
+add_subdirectory(clang-replace)
 add_subdirectory(modularize)
 add_subdirectory(clang-tidy)
 
Index: Makefile
===================================================================
--- Makefile
+++ Makefile
@@ -12,7 +12,7 @@
 include $(CLANG_LEVEL)/../../Makefile.config
 
 PARALLEL_DIRS := remove-cstr-calls tool-template modularize
-DIRS := cpp11-migrate clang-tidy unittests
+DIRS := cpp11-migrate clang-tidy clang-replace unittests
 
 include $(CLANG_LEVEL)/Makefile
 
Index: clang-replace/ApplyReplacements.cpp
===================================================================
--- /dev/null
+++ clang-replace/ApplyReplacements.cpp
@@ -0,0 +1,192 @@
+//===-- Core/ApplyChangeDescriptions.cpp ----------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// \brief This file provides the implementation for finding and applying change
+/// description files.
+///
+//===----------------------------------------------------------------------===//
+#include "ApplyReplacements.h"
+#include "clang/Basic/LangOptions.h"
+#include "clang/Basic/SourceManager.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/raw_ostream.h"
+
+using namespace llvm;
+using namespace clang;
+
+
+static void eatDiagnostics(const SMDiagnostic &, void *) {}
+
+namespace clang {
+namespace replace {
+
+error_code collectReplacementsFromDirectory(const StringRef Directory,
+                                            TUReplacementsVec &TUs,
+                                            DiagnosticsEngine &Diagnostics) {
+  using namespace llvm::sys::fs;
+  using namespace llvm::sys::path;
+
+  error_code ErrorCode;
+
+  for (recursive_directory_iterator I(Directory, ErrorCode), E;
+       I != E && !ErrorCode; I.increment(ErrorCode)) {
+    if (filename(I->path())[0] == '.') {
+      // Indicate not to descend into directories beginning with '.'
+      I.no_push();
+      continue;
+    }
+
+    if (extension(I->path()) != ".yaml")
+      continue;
+
+    OwningPtr<MemoryBuffer> Out;
+    error_code BufferError = MemoryBuffer::getFile(I->path(), Out);
+    if (BufferError) {
+      errs() << "Error reading " << I->path() << ": " << BufferError.message()
+             << "\n";
+      continue;
+    }
+
+    yaml::Input YIn(Out->getBuffer());
+    YIn.setDiagHandler(&eatDiagnostics);
+    tooling::TranslationUnitReplacements TU;
+    YIn >> TU;
+    if (YIn.error()) {
+      // File doesn't appear to be a header change description. Ignore it.
+      continue;
+    }
+
+    // Only keep files that properly parse.
+    TUs.push_back(TU);
+  }
+
+  return ErrorCode;
+}
+
+/// \brief Dumps information for a sequence of conflicting Replacements.
+///
+/// \param[in] File FileEntry for the file the conflicting Replacements are
+/// for.
+/// \param[in] ConflictingReplacements List of conflicting Replacements.
+/// \param[in] SM SourceManager used for reporting.
+static void reportConflict(
+    const FileEntry *File,
+    const llvm::ArrayRef<clang::tooling::Replacement> ConflictingReplacements,
+    SourceManager &SM) {
+  FileID FID = SM.translateFile(File);
+  if (FID.isInvalid())
+    FID = SM.createFileID(File, SourceLocation(), SrcMgr::C_User);
+
+  // FIXME: Output something a little more user-friendly (e.g. unified diff?)
+  errs() << "The following changes conflict:\n";
+  for (const tooling::Replacement *I = ConflictingReplacements.begin(),
+                                  *E = ConflictingReplacements.end();
+       I != E; ++I) {
+    if (I->getLength() == 0) {
+      errs() << "  Insert at " << SM.getLineNumber(FID, I->getOffset()) << ":"
+             << SM.getColumnNumber(FID, I->getOffset()) << " "
+             << I->getReplacementText() << "\n";
+    } else {
+      if (I->getReplacementText().empty())
+        errs() << "  Remove ";
+      else
+        errs() << "  Replace ";
+
+      errs() << SM.getLineNumber(FID, I->getOffset()) << ":"
+             << SM.getColumnNumber(FID, I->getOffset()) << "-"
+             << SM.getLineNumber(FID, I->getOffset() + I->getLength() - 1)
+             << ":"
+             << SM.getColumnNumber(FID, I->getOffset() + I->getLength() - 1);
+
+      if (I->getReplacementText().empty())
+        errs() << "\n";
+      else
+        errs() << " with \"" << I->getReplacementText() << "\"\n";
+    }
+  }
+}
+
+/// \brief Deduplicates and tests for conflicts among the replacements for each
+/// file in \c Replacements. Any conflicts found are reported.
+///
+/// \param[in,out] Replacements Container of all replacements grouped by file
+/// to be deduplicated and checked for conflicts.
+/// \param[in] SM SourceManager required for conflict reporting
+///
+/// \returns \li true if conflicts were detected
+///          \li false if no conflicts were detected
+static bool deduplicateAndDetectConflicts(FileToReplacementsMap &Replacements,
+                                          SourceManager &SM) {
+  bool conflictsFound = false;
+
+  for (FileToReplacementsMap::iterator I = Replacements.begin(),
+                                       E = Replacements.end();
+       I != E; ++I) {
+
+    const FileEntry *Entry = SM.getFileManager().getFile(I->getKey());
+    if (!Entry) {
+      errs() << "Described file '" << I->getKey()
+             << "' doesn't exist. Ignoring...\n";
+      continue;
+    }
+
+    std::vector<tooling::Range> Conflicts;
+    tooling::deduplicate(I->getValue(), Conflicts);
+
+    if (Conflicts.empty())
+      continue;
+
+    conflictsFound = true;
+
+    errs() << "There are conflicting changes to " << I->getKey() << ":\n";
+
+    for (std::vector<tooling::Range>::const_iterator
+             ConflictI = Conflicts.begin(),
+             ConflictE = Conflicts.end();
+         ConflictI != ConflictE; ++ConflictI) {
+      ArrayRef<tooling::Replacement> ConflictingReplacements(
+          &I->getValue()[ConflictI->getOffset()], ConflictI->getLength());
+      reportConflict(Entry, ConflictingReplacements, SM);
+    }
+  }
+
+  return conflictsFound;
+}
+
+bool mergeAndDeduplicate(const TUReplacementsVec &TUs,
+                         FileToReplacementsMap &GroupedReplacements,
+                         DiagnosticsEngine &Diagnostics) {
+
+  // FIXME: Use Diagnostics for output
+
+  // Group all replacements by target file.
+  for (TUReplacementsVec::const_iterator TUI = TUs.begin(), TUE = TUs.end();
+       TUI != TUE; ++TUI)
+    for (std::vector<tooling::Replacement>::const_iterator
+             RI = TUI->Replacements.begin(),
+             RE = TUI->Replacements.end();
+         RI != RE; ++RI)
+      GroupedReplacements[RI->getFilePath()].push_back(*RI);
+
+  FileManager Files((FileSystemOptions()));
+  SourceManager SM(Diagnostics, Files);
+
+  // Ask clang to deduplicate and report conflicts.
+  if (deduplicateAndDetectConflicts(GroupedReplacements, SM))
+    return false;
+
+  return true;
+}
+
+} // end namespace replace
+} // end namespace clang
Index: clang-replace/ApplyReplacements.h
===================================================================
--- /dev/null
+++ clang-replace/ApplyReplacements.h
@@ -0,0 +1,77 @@
+//===-- Core/ApplyChangeDescriptions.h --------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// \brief This file provides the interface for finding and applying change
+/// description files.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef CPP11_MIGRATE_APPLYCHANGEDESCRIPTIONS_H
+#define CPP11_MIGRATE_APPLYCHANGEDESCRIPTIONS_H
+
+#include "clang/Tooling/ReplacementsYaml.h"
+#include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/system_error.h"
+#include <vector>
+
+namespace clang {
+
+class DiagnosticsEngine;
+
+namespace replace {
+
+/// \brief Collection of TranslationUnitReplacements.
+typedef std::vector<clang::tooling::TranslationUnitReplacements>
+TUReplacementsVec;
+
+/// \brief Map mapping file name to Replacements targeting that file.
+typedef llvm::StringMap<std::vector<clang::tooling::Replacement> >
+FileToReplacementsMap;
+
+/// \brief Recursively descends through a directory structure rooted at \p
+/// Directory and attempts to deserialize *.yaml files as
+/// TranslationUnitReplacements. All docs that successfully deserialize are
+/// added to \p TUs.
+///
+/// Directories starting with '.' are ignored during traversal.
+///
+/// \param[in] Directory Directory to begin search for serialized
+/// TranslationUnitReplacements.
+/// \param[out] TUs Collection of all found and deserialized
+/// TranslationUnitReplacements.
+/// \param[in] Diagnostics DiagnosticsEngine used for error output.
+///
+/// \returns An error_code indicating success or failure in navigating the
+/// directory structure.
+llvm::error_code
+collectReplacementsFromDirectory(const llvm::StringRef Directory,
+                                 TUReplacementsVec &TUs,
+                                 clang::DiagnosticsEngine &Diagnostics);
+
+/// \brief Deduplicate, check for conflicts, and apply all Replacements stored
+/// in \c TUs. If conflicts occur, no Replacements are applied.
+///
+/// \param[in] TUs Collection of TranslationUnitReplacements to merge,
+/// deduplicate, and test for conflicts.
+/// \param[out] GroupedReplacements Container grouping all Replacements by the
+/// file they target.
+/// \param[in] Diagnostics DiagnosticsEngine used for error/warning output.
+///
+/// \returns \li true If all changes were applied successfully.
+///          \li false If there were conflicts.
+bool mergeAndDeduplicate(const TUReplacementsVec &TUs,
+                         FileToReplacementsMap &GroupedReplacements,
+                         clang::DiagnosticsEngine &Diagnostics);
+
+} // end namespace replace
+} // end namespace clang
+
+#endif // CPP11_MIGRATE_APPLYCHANGEDESCRIPTIONS_H
Index: clang-replace/CMakeLists.txt
===================================================================
--- /dev/null
+++ clang-replace/CMakeLists.txt
@@ -0,0 +1,19 @@
+set(LLVM_LINK_COMPONENTS
+  ${LLVM_TARGETS_TO_BUILD}
+  asmparser
+  bitreader
+  support
+  mc
+  )
+
+add_clang_library(clangReplace
+  ApplyReplacements.cpp
+  )
+target_link_libraries(clangReplace
+  clangTooling
+  clangBasic
+  clangRewriteFrontend
+  )
+
+include_directories(${CMAKE_CURRENT_SOURCE_DIR})
+add_subdirectory(tool)
Index: clang-replace/Makefile
===================================================================
--- /dev/null
+++ clang-replace/Makefile
@@ -0,0 +1,16 @@
+##===- clang-replace/Makefile ------------------------------*- Makefile -*-===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+CLANG_LEVEL := ../../..
+LIBRARYNAME := clangReplace
+include $(CLANG_LEVEL)/../../Makefile.config
+
+DIRS = tool
+
+include $(CLANG_LEVEL)/Makefile
Index: clang-replace/tool/CMakeLists.txt
===================================================================
--- /dev/null
+++ clang-replace/tool/CMakeLists.txt
@@ -0,0 +1,17 @@
+set(LLVM_LINK_COMPONENTS
+  ${LLVM_TARGETS_TO_BUILD}
+  asmparser
+  bitreader
+  support
+  mc
+  )
+
+add_clang_executable(clang-replace
+  ClangReplaceMain.cpp
+  )
+target_link_libraries(clang-replace
+  clangReplace
+  )
+
+install(TARGETS clang-replace
+  RUNTIME DESTINATION bin)
Index: clang-replace/tool/ClangReplaceMain.cpp
===================================================================
--- /dev/null
+++ clang-replace/tool/ClangReplaceMain.cpp
@@ -0,0 +1,50 @@
+//===-- ClangReplaceMain.cpp - Main file for clang-replace tool -----------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// \brief This file provides the main function for the clang-replace tool.
+///
+//===----------------------------------------------------------------------===//
+
+#include "ApplyReplacements.h"
+#include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/DiagnosticOptions.h"
+#include "llvm/Support/CommandLine.h"
+
+using namespace llvm;
+using namespace clang;
+using namespace clang::replace;
+
+static cl::opt<std::string> Directory(cl::Positional, cl::Required,
+                                      cl::desc("<Search Root Directory>"));
+
+int main(int argc, char **argv) {
+  cl::ParseCommandLineOptions(argc, argv);
+
+  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions());
+  DiagnosticsEngine Diagnostics(
+      IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
+      DiagOpts.getPtr());
+
+  TUReplacementsVec TUs;
+
+  error_code ErrorCode =
+      collectReplacementsFromDirectory(Directory, TUs, Diagnostics);
+
+  if (ErrorCode) {
+    errs() << "Trouble iterating over directory '" << Directory
+           << "': " << ErrorCode.message() << "\n";
+    return false;
+  }
+
+  FileToReplacementsMap GroupedReplacements;
+  if (mergeAndDeduplicate(TUs, GroupedReplacements, Diagnostics))
+    return 0;
+  return 1;
+}
Index: clang-replace/tool/Makefile
===================================================================
--- /dev/null
+++ clang-replace/tool/Makefile
@@ -0,0 +1,28 @@
+##===- clang-replace/tool/Makefile -------------------------*- Makefile -*-===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+CLANG_LEVEL := ../../../..
+include $(CLANG_LEVEL)/../../Makefile.config
+
+TOOLNAME = clang-replace
+
+# No plugins, optimize startup time.
+TOOL_NO_EXPORTS = 1
+
+SOURCES = ClangReplaceMain.cpp
+
+LINK_COMPONENTS := $(TARGETS_TO_BUILD) asmparser bitreader support mc mcparser option
+USEDLIBS = clangReplace.a clangFormat.a clangTooling.a clangFrontend.a \
+	   clangSerialization.a clangDriver.a clangRewriteFrontend.a \
+	   clangRewriteCore.a clangParse.a clangSema.a clangAnalysis.a \
+	   clangAST.a clangASTMatchers.a clangEdit.a clangLex.a clangBasic.a
+
+include $(CLANG_LEVEL)/Makefile
+
+CPP.Flags += -I$(PROJ_SRC_DIR)/..
Index: test/CMakeLists.txt
===================================================================
--- test/CMakeLists.txt
+++ test/CMakeLists.txt
@@ -27,7 +27,7 @@
   clang clang-headers FileCheck count not
 
   # Individual tools we test.
-  remove-cstr-calls cpp11-migrate modularize clang-tidy
+  remove-cstr-calls clang-replace cpp11-migrate modularize clang-tidy
 
   # Unit tests
   ExtraToolsUnitTests
Index: test/clang-replace/conflict.cpp
===================================================================
--- /dev/null
+++ test/clang-replace/conflict.cpp
@@ -0,0 +1,7 @@
+// RUN: mkdir -p %T/conflict
+// RUN: sed "s#\$(path)#%/S/conflict#" %S/conflict/file1.yaml > %T/conflict/file1.yaml
+// RUN: sed "s#\$(path)#%/S/conflict#" %S/conflict/file2.yaml > %T/conflict/file2.yaml
+// RUN: sed "s#\$(path)#%/S/conflict#" %S/conflict/file3.yaml > %T/conflict/file3.yaml
+// RUN: sed "s#\$(path)#%/S/conflict#" %S/conflict/expected.txt > %T/conflict/expected.txt
+// RUN: not clang-replace %T/conflict > %T/conflict/output.txt 2>&1
+// RUN: diff -b %T/conflict/output.txt %T/conflict/expected.txt
Index: test/clang-replace/conflict/common.h
===================================================================
--- /dev/null
+++ test/clang-replace/conflict/common.h
@@ -0,0 +1,17 @@
+#ifndef COMMON_H
+#define COMMON_H
+
+extern void ext(int (&)[5]);
+
+void func(int t) {
+  int ints[5];
+  for (unsigned i = 0; i < 5; ++i) {
+    ints[i] = t;
+  }
+
+  int *i = 0;
+
+  ext(ints);
+}
+
+#endif // COMMON_H
Index: test/clang-replace/conflict/expected.txt
===================================================================
--- /dev/null
+++ test/clang-replace/conflict/expected.txt
@@ -0,0 +1,11 @@
+There are conflicting changes to $(path)/common.h:
+The following changes conflict:
+  Replace 8:8-8:33 with "auto & i : ints"
+  Replace 8:8-8:33 with "int & elem : ints"
+The following changes conflict:
+  Replace 9:5-9:11 with "elem"
+  Replace 9:5-9:11 with "i"
+The following changes conflict:
+  Remove 12:3-12:14
+  Insert at 12:12 (int*)
+  Replace 12:12-12:12 with "nullptr"
Index: test/clang-replace/conflict/file1.yaml
===================================================================
--- /dev/null
+++ test/clang-replace/conflict/file1.yaml
@@ -0,0 +1,16 @@
+---
+MainSourceFile: "source1.cpp"
+Replacements:
+  - FilePath:        "$(path)/common.h"
+    Offset:          106
+    Length:          26
+    ReplacementText: "auto & i : ints"
+  - FilePath:        "$(path)/common.h"
+    Offset:          140
+    Length:          7
+    ReplacementText: "i"
+  - FilePath:        "$(path)/common.h"
+    Offset:          160
+    Length:          12
+    ReplacementText: ""
+...
Index: test/clang-replace/conflict/file2.yaml
===================================================================
--- /dev/null
+++ test/clang-replace/conflict/file2.yaml
@@ -0,0 +1,16 @@
+---
+MainSourceFile: "source2.cpp"
+Replacements:
+  - FilePath:        "$(path)/common.h"
+    Offset:          106
+    Length:          26
+    ReplacementText: "int & elem : ints"
+  - FilePath:        "$(path)/common.h"
+    Offset:          140
+    Length:          7
+    ReplacementText: "elem"
+  - FilePath:        "$(path)/common.h"
+    Offset:          169
+    Length:          1
+    ReplacementText: "nullptr"
+...
Index: test/clang-replace/conflict/file3.yaml
===================================================================
--- /dev/null
+++ test/clang-replace/conflict/file3.yaml
@@ -0,0 +1,8 @@
+---
+MainSourceFile: "source1.cpp"
+Replacements:
+  - FilePath:        "$(path)/common.h"
+    Offset:          169
+    Length:          0
+    ReplacementText: "(int*)"
+...
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to