[PATCH] Added testing to test migration of files in a compilation database

Ariel Bernal ariel.j.bernal at intel.com
Thu Sep 19 10:22:00 PDT 2013


  Addressed comments and added a fix for the case when a compilation database is auto-detected from a source but then it can't be found. So we assume the detected compilation database is not relevant to the file and we use -std=c++11 as the only compilation option.
  @revane I haven't tested this patch in windows yet.

Hi revane, tareqsiraj, Sarcasm,

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

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D1710?vs=4368&id=4398#toc

Files:
  clang-modernize/tool/ClangModernize.cpp
  test/clang-modernize/Compilations/Inputs/compilations.cpp
  test/clang-modernize/Compilations/Inputs/compilations_expected.cpp
  test/clang-modernize/Compilations/Inputs/compile_commands.json
  test/clang-modernize/Compilations/Inputs/cpp11.cpp
  test/clang-modernize/Compilations/Inputs/cpp11_expected.cpp
  test/clang-modernize/Compilations/main.cpp

Index: clang-modernize/tool/ClangModernize.cpp
===================================================================
--- clang-modernize/tool/ClangModernize.cpp
+++ clang-modernize/tool/ClangModernize.cpp
@@ -290,8 +290,21 @@
   if (!SourcePaths.empty()) {
     if (CompilationDatabase *Compilations =
             CompilationDatabase::autoDetectFromSource(SourcePaths[0],
-                                                      ErrorMessage))
-      return Compilations;
+                                                      ErrorMessage)) {
+      // FIXME: just pass SourcePaths[0] once getCompileCommands supports
+      // non-absolute paths.
+      SmallString<64> Path(SourcePaths[0]);
+      llvm::sys::fs::make_absolute(Path);
+      std::vector<CompileCommand> Commands =
+          Compilations->getCompileCommands(Path);
+      // Ignore a detected compilation database that doesn't contain source0
+      // since it is probably an unrelated compilation database.
+      if (!Commands.empty())
+        return Compilations;
+    }
+    // Reset ErrorMessage since a fix compilation database will be created if
+    // it fails to detect one from source.
+    ErrorMessage = "";
     // If no compilation database can be detected from source then we create a
     // fixed compilation database with c++11 support.
     std::string CommandLine[] = { "-std=c++11" };
Index: test/clang-modernize/Compilations/Inputs/compilations.cpp
===================================================================
--- /dev/null
+++ test/clang-modernize/Compilations/Inputs/compilations.cpp
@@ -0,0 +1,3 @@
+void foo() {
+  int *p = 0;
+}
Index: test/clang-modernize/Compilations/Inputs/compilations_expected.cpp
===================================================================
--- /dev/null
+++ test/clang-modernize/Compilations/Inputs/compilations_expected.cpp
@@ -0,0 +1,3 @@
+void foo() {
+  int *p = nullptr;
+}
Index: test/clang-modernize/Compilations/Inputs/compile_commands.json
===================================================================
--- /dev/null
+++ test/clang-modernize/Compilations/Inputs/compile_commands.json
@@ -0,0 +1,17 @@
+[
+{
+  "directory": "$(path)/a1/",
+  "command": "clang++ -o compilations.o -c $(path)/a1/compilations.cpp -std=c++11",
+  "file": "$(path)/a1/compilations.cpp"
+},
+{
+  "directory": "$(path)/a2/",
+  "command": "clang++ -o compilations.o -c $(path)/a2/compilations.cpp -std=c++11",
+  "file": "$(path)/a2/compilations.cpp"
+},
+{
+  "directory": "$(path)/a3/",
+  "command": "clang++ -o compilations.o -c $(path)/a3/compilations.cpp -std=c++11",
+  "file": "$(path)/a3/compilations.cpp"
+}
+]
Index: test/clang-modernize/Compilations/Inputs/cpp11.cpp
===================================================================
--- /dev/null
+++ test/clang-modernize/Compilations/Inputs/cpp11.cpp
@@ -0,0 +1,4 @@
+void foo() {
+  int *p = 0;
+  int *k = nullptr;
+}
Index: test/clang-modernize/Compilations/Inputs/cpp11_expected.cpp
===================================================================
--- /dev/null
+++ test/clang-modernize/Compilations/Inputs/cpp11_expected.cpp
@@ -0,0 +1,4 @@
+void foo() {
+  int *p = nullptr;
+  int *k = nullptr;
+}
Index: test/clang-modernize/Compilations/main.cpp
===================================================================
--- /dev/null
+++ test/clang-modernize/Compilations/main.cpp
@@ -0,0 +1,79 @@
+// The following block tests:
+//   - A compilation database is detected from build path specified by -p.
+//   - A compilation database is detected from source0.
+//   - A fixed compilation database is provided.
+//   - Uses of -include/exclude.
+
+// Create directory structure
+// a1, a2  and a3 are specified paths for files in the compilation database but
+// not a4.
+// RUN: rm -rf %T/SourcesTest
+// RUN: mkdir -p %T/SourcesTest
+// RUN: mkdir -p %T/SourcesTest/a1
+// RUN: mkdir -p %T/SourcesTest/a2
+// RUN: mkdir -p %T/SourcesTest/a3
+// RUN: mkdir -p %T/SourcesTest/a4
+
+// RUN: cp %S/Inputs/compilations_expected.cpp %T
+// RUN: cp %S/Inputs/cpp11_expected.cpp %T
+
+// Check that files are transformed when -- is specified.
+// RUN: echo %T/SourcesTest/a1 %T/SourcesTest/a2 %T/SourcesTest/a3 | xargs -n 1 cp %S/Inputs/compilations.cpp
+// RUN: clang-modernize -use-nullptr %T/SourcesTest/a1/compilations.cpp %T/SourcesTest/a3/compilations.cpp --
+// RUN: diff -b %T/compilations_expected.cpp %T/SourcesTest/a1/compilations.cpp
+// RUN: not diff -b %T/compilations_expected.cpp %T/SourcesTest/a2/compilations.cpp
+// RUN: diff -b %T/compilations_expected.cpp %T/SourcesTest/a3/compilations.cpp
+
+// Check that only files not explicitly excluded are transformed.
+// RUN: echo %T/SourcesTest/a1 %T/SourcesTest/a2 %T/SourcesTest/a3 | xargs -n 1 cp %S/Inputs/compilations.cpp
+// RUN: cp %S/Inputs/compilations_expected.cpp %T/SourcesTest
+// RUN: clang-modernize -use-nullptr %T/SourcesTest/a1/compilations.cpp %T/SourcesTest/a2/compilations.cpp %T/SourcesTest/a3/compilations.cpp -exclude=%T/SourcesTest/a2 --
+// RUN: diff -b %T/compilations_expected.cpp %T/SourcesTest/a1/compilations.cpp
+// RUN: not diff -b %T/compilations_expected.cpp %T/SourcesTest/a2/compilations.cpp
+// RUN: diff -b %T/compilations_expected.cpp %T/SourcesTest/a3/compilations.cpp
+
+// The following test will make use of a compilation database
+// RUN: sed -e 's#$(path)#%/T/SourcesTest#g' %S/Inputs/compile_commands.json > %T/compile_commands.json
+
+// Check that a compilation database can be auto-detected from source0
+// RUN: echo %T/SourcesTest/a1 %T/SourcesTest/a2 %T/SourcesTest/a3 | xargs -n 1 cp %S/Inputs/compilations.cpp
+// RUN: clang-modernize -use-nullptr %T/SourcesTest/a1/compilations.cpp %T/SourcesTest/a3/compilations.cpp
+// RUN: diff -b %T/compilations_expected.cpp %T/SourcesTest/a1/compilations.cpp 
+// RUN: not diff -b %T/compilations_expected.cpp %T/SourcesTest/a2/compilations.cpp
+// RUN: diff -b %T/compilations_expected.cpp %T/SourcesTest/a3/compilations.cpp 
+
+// Check that if neither -p nor -- was specified and a compilation database was 
+// detected from source0 but the file wasn't found in the compilation database
+// then it's transformed using default -- -std=c++11.
+// RUN: cp %S/Inputs/cpp11.cpp %T/SourcesTest/a4
+// RUN: clang-modernize -use-nullptr %T/SourcesTest/a4/cpp11.cpp
+// RUN: diff -b %T/cpp11_expected.cpp %T/SourcesTest/a4/cpp11.cpp
+
+// Check that files are tranformed when -p is provided and files are specified. 
+// RUN: echo %T/SourcesTest/a1 %T/SourcesTest/a2 %T/SourcesTest/a3 | xargs -n 1 cp %S/Inputs/compilations.cpp
+// RUN: clang-modernize -use-nullptr -p=%T %T/SourcesTest/a1/compilations.cpp %T/SourcesTest/a3/compilations.cpp
+// RUN: diff -b %T/compilations_expected.cpp %T/SourcesTest/a1/compilations.cpp 
+// RUN: not diff -b %T/compilations_expected.cpp %T/SourcesTest/a2/compilations.cpp
+// RUN: diff -b %T/compilations_expected.cpp %T/SourcesTest/a3/compilations.cpp 
+
+// Check that files are tranformed when -p and -include are specified.
+// RUN: echo %T/SourcesTest/a1 %T/SourcesTest/a2 %T/SourcesTest/a3 | xargs -n 1 cp %S/Inputs/compilations.cpp
+// RUN: clang-modernize -use-nullptr -p=%T -include=%T/SourcesTest/a1,%T/SourcesTest/a3
+// RUN: diff -b %T/compilations_expected.cpp %T/SourcesTest/a1/compilations.cpp 
+// RUN: not diff -b %T/compilations_expected.cpp %T/SourcesTest/a2/compilations.cpp
+// RUN: diff -b %T/compilations_expected.cpp %T/SourcesTest/a3/compilations.cpp
+
+// Check that no files are tranformed when -p is specified but not -include.
+// RUN: echo %T/SourcesTest/a1 %T/SourcesTest/a2 %T/SourcesTest/a3 | xargs -n 1 cp %S/Inputs/compilations.cpp
+// RUN: not clang-modernize -use-nullptr -p=%T
+// RUN: not diff -b %T/compilations_expected.cpp %T/SourcesTest/a1/compilations.cpp
+// RUN: not diff -b %T/compilations_expected.cpp %T/SourcesTest/a2/compilations.cpp
+// RUN: not diff -b %T/compilations_expected.cpp %T/SourcesTest/a3/compilations.cpp
+
+// Check that only specified sources are tranformed when -p and -include are
+// specified along with sources.
+// RUN: echo %T/SourcesTest/a1 %T/SourcesTest/a2 %T/SourcesTest/a3 | xargs -n 1 cp %S/Inputs/compilations.cpp
+// RUN: clang-modernize -use-nullptr -p=%T -include=%T/SourcesTest %T/SourcesTest/a2/compilations.cpp
+// RUN: not diff -b %T/compilations_expected.cpp %T/SourcesTest/a1/compilations.cpp
+// RUN: diff -b %T/compilations_expected.cpp %T/SourcesTest/a2/compilations.cpp
+// RUN: not diff -b %T/compilations_expected.cpp %T/SourcesTest/a3/compilations.cpp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1710.2.patch
Type: text/x-patch
Size: 8533 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130919/935169f0/attachment.bin>


More information about the cfe-commits mailing list