[clang-tools-extra] r191213 - Added tests for testing migration of files in a compilation database.

Ariel J. Bernal ariel.j.bernal at intel.com
Mon Sep 23 11:37:32 PDT 2013


Author: ajbernal
Date: Mon Sep 23 13:37:32 2013
New Revision: 191213

URL: http://llvm.org/viewvc/llvm-project?rev=191213&view=rev
Log:
Added tests for testing migration of files in a compilation database.

This patch also fixes the case where a compilation database is autodetected from
source but the file itself cannot be found in the compilation database, it then
ignores the compilation database and transforms the file with c++11 support.

Added:
    clang-tools-extra/trunk/test/clang-modernize/Compilations/
    clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/
    clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/compilations.cpp
    clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/compilations_expected.cpp
    clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/compile_commands.json
    clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/cpp11.cpp
    clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/cpp11_expected.cpp
    clang-tools-extra/trunk/test/clang-modernize/Compilations/compilation_inc.cpp
    clang-tools-extra/trunk/test/clang-modernize/Compilations/compilation_inc_sources.cpp
    clang-tools-extra/trunk/test/clang-modernize/Compilations/compilation_not_inc.cpp
    clang-tools-extra/trunk/test/clang-modernize/Compilations/detect_from_path.cpp
    clang-tools-extra/trunk/test/clang-modernize/Compilations/detect_from_source.cpp
    clang-tools-extra/trunk/test/clang-modernize/Compilations/fixed_comp.cpp
    clang-tools-extra/trunk/test/clang-modernize/Compilations/fixed_comp_inc.cpp
    clang-tools-extra/trunk/test/clang-modernize/Compilations/no_compilation.cpp
Modified:
    clang-tools-extra/trunk/clang-modernize/tool/ClangModernize.cpp

Modified: clang-tools-extra/trunk/clang-modernize/tool/ClangModernize.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-modernize/tool/ClangModernize.cpp?rev=191213&r1=191212&r2=191213&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-modernize/tool/ClangModernize.cpp (original)
+++ clang-tools-extra/trunk/clang-modernize/tool/ClangModernize.cpp Mon Sep 23 13:37:32 2013
@@ -290,8 +290,21 @@ CompilationDatabase *autoDetectCompilati
   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" };

Added: clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/compilations.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/compilations.cpp?rev=191213&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/compilations.cpp (added)
+++ clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/compilations.cpp Mon Sep 23 13:37:32 2013
@@ -0,0 +1,3 @@
+void foo() {
+  int *p = 0;
+}

Added: clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/compilations_expected.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/compilations_expected.cpp?rev=191213&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/compilations_expected.cpp (added)
+++ clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/compilations_expected.cpp Mon Sep 23 13:37:32 2013
@@ -0,0 +1,3 @@
+void foo() {
+  int *p = nullptr;
+}

Added: clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/compile_commands.json
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/compile_commands.json?rev=191213&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/compile_commands.json (added)
+++ clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/compile_commands.json Mon Sep 23 13:37:32 2013
@@ -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"
+}
+]

Added: clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/cpp11.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/cpp11.cpp?rev=191213&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/cpp11.cpp (added)
+++ clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/cpp11.cpp Mon Sep 23 13:37:32 2013
@@ -0,0 +1,4 @@
+void foo() {
+  int *p = 0;
+  int *k = nullptr;
+}

Added: clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/cpp11_expected.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/cpp11_expected.cpp?rev=191213&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/cpp11_expected.cpp (added)
+++ clang-tools-extra/trunk/test/clang-modernize/Compilations/Inputs/cpp11_expected.cpp Mon Sep 23 13:37:32 2013
@@ -0,0 +1,4 @@
+void foo() {
+  int *p = nullptr;
+  int *k = nullptr;
+}

Added: clang-tools-extra/trunk/test/clang-modernize/Compilations/compilation_inc.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-modernize/Compilations/compilation_inc.cpp?rev=191213&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-modernize/Compilations/compilation_inc.cpp (added)
+++ clang-tools-extra/trunk/test/clang-modernize/Compilations/compilation_inc.cpp Mon Sep 23 13:37:32 2013
@@ -0,0 +1,24 @@
+// The following block tests:
+//   - A compilation database is detected from build path specified by -p and
+//     -include was provided.
+
+// Create directory structure
+// a1, a2  and a3 are specified paths for files in the compilation database.
+// RUN: rm -rf %T/CompilationInc
+// RUN: mkdir -p %T/CompilationInc
+// RUN: mkdir -p %T/CompilationInc/a1
+// RUN: mkdir -p %T/CompilationInc/a2
+// RUN: mkdir -p %T/CompilationInc/a3
+
+// This test uses a compilation database
+// RUN: sed -e 's#$(path)#%/T/CompilationInc#g' %S/Inputs/compile_commands.json > %T/CompilationInc/compile_commands.json
+
+// Check that files are tranformed when -p and -include are specified.
+// RUN: cp %S/Inputs/compilations.cpp %T/CompilationInc/a1
+// RUN: cp %S/Inputs/compilations.cpp %T/CompilationInc/a2
+// RUN: cp %S/Inputs/compilations.cpp %T/CompilationInc/a3
+
+// RUN: clang-modernize -use-nullptr -p=%T/CompilationInc -include=%T/CompilationInc/a1,%T/CompilationInc/a3
+// RUN: diff -b %S/Inputs/compilations_expected.cpp %T/CompilationInc/a1/compilations.cpp
+// RUN: not diff -b %S/Inputs/compilations_expected.cpp %T/CompilationInc/a2/compilations.cpp
+// RUN: diff -b %S/Inputs/compilations_expected.cpp %T/CompilationInc/a3/compilations.cpp

Added: clang-tools-extra/trunk/test/clang-modernize/Compilations/compilation_inc_sources.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-modernize/Compilations/compilation_inc_sources.cpp?rev=191213&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-modernize/Compilations/compilation_inc_sources.cpp (added)
+++ clang-tools-extra/trunk/test/clang-modernize/Compilations/compilation_inc_sources.cpp Mon Sep 23 13:37:32 2013
@@ -0,0 +1,22 @@
+// Test that only specified sources are transformed when -p and -include are
+// specified along with sources.
+
+// Create directory structure
+// a1, a2  and a3 are specified paths for files in the compilation database.
+// RUN: rm -rf %T/CompilationIncSources
+// RUN: mkdir -p %T/CompilationIncSources
+// RUN: mkdir -p %T/CompilationIncSources/a1
+// RUN: mkdir -p %T/CompilationIncSources/a2
+// RUN: mkdir -p %T/CompilationIncSources/a3
+
+// This test uses a compilation database
+// RUN: sed -e 's#$(path)#%/T/CompilationIncSources#g' %S/Inputs/compile_commands.json > %T/CompilationIncSources/compile_commands.json
+
+// RUN: cp %S/Inputs/compilations.cpp %T/CompilationIncSources/a1
+// RUN: cp %S/Inputs/compilations.cpp %T/CompilationIncSources/a2
+// RUN: cp %S/Inputs/compilations.cpp %T/CompilationIncSources/a3
+
+// RUN: clang-modernize -use-nullptr -p=%T/CompilationIncSources -include=%T/CompilationIncSources %T/CompilationIncSources/a2/compilations.cpp
+// RUN: not diff -b %S/Inputs/compilations_expected.cpp %T/CompilationIncSources/a1/compilations.cpp
+// RUN: diff -b %S/Inputs/compilations_expected.cpp %T/CompilationIncSources/a2/compilations.cpp
+// RUN: not diff -b %S/Inputs/compilations_expected.cpp %T/CompilationIncSources/a3/compilations.cpp

Added: clang-tools-extra/trunk/test/clang-modernize/Compilations/compilation_not_inc.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-modernize/Compilations/compilation_not_inc.cpp?rev=191213&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-modernize/Compilations/compilation_not_inc.cpp (added)
+++ clang-tools-extra/trunk/test/clang-modernize/Compilations/compilation_not_inc.cpp Mon Sep 23 13:37:32 2013
@@ -0,0 +1,24 @@
+// The following block tests:
+//   - A compilation database is detected from build path specified by -p but
+//     neither sources nor -include was provided.
+
+// Create directory structure
+// a1, a2  and a3 are specified paths for files in the compilation database.
+// RUN: rm -rf %T/CompilationNotInc
+// RUN: mkdir -p %T/CompilationNotInc
+// RUN: mkdir -p %T/CompilationNotInc/a1
+// RUN: mkdir -p %T/CompilationNotInc/a2
+// RUN: mkdir -p %T/CompilationNotInc/a3
+
+// This test uses a compilation database
+// RUN: sed -e 's#$(path)#%/T/CompilationNotInc#g' %S/Inputs/compile_commands.json > %T/CompilationNotInc/compile_commands.json
+
+// Check that no files are tranformed when -p is specified but not -include.
+// RUN: cp %S/Inputs/compilations.cpp %T/DetectFromSource/a1
+// RUN: cp %S/Inputs/compilations.cpp %T/DetectFromSource/a2
+// RUN: cp %S/Inputs/compilations.cpp %T/DetectFromSource/a3
+
+// RUN: not clang-modernize -use-nullptr -p=%T/CompilationNotInc
+// RUN: not diff -b %T/compilations_expected.cpp %T/CompilationNotInc/a1/compilations.cpp
+// RUN: not diff -b %T/compilations_expected.cpp %T/CompilationNotInc/a2/compilations.cpp
+// RUN: not diff -b %T/compilations_expected.cpp %T/CompilationNotInc/a3/compilations.cpp

Added: clang-tools-extra/trunk/test/clang-modernize/Compilations/detect_from_path.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-modernize/Compilations/detect_from_path.cpp?rev=191213&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-modernize/Compilations/detect_from_path.cpp (added)
+++ clang-tools-extra/trunk/test/clang-modernize/Compilations/detect_from_path.cpp Mon Sep 23 13:37:32 2013
@@ -0,0 +1,24 @@
+// The following block tests:
+//   - A compilation database is detected from build path specified by -p and
+//     files are provided.
+
+// Create directory structure
+// a1, a2  and a3 are specified paths for files in the compilation database.
+// RUN: rm -rf %T/DetectFromPath
+// RUN: mkdir -p %T/DetectFromPath
+// RUN: mkdir -p %T/DetectFromPath/a1
+// RUN: mkdir -p %T/DetectFromPath/a2
+// RUN: mkdir -p %T/DetectFromPath/a3
+
+// This test uses a compilation database
+// RUN: sed -e 's#$(path)#%/T/DetectFromPath#g' %S/Inputs/compile_commands.json > %T/DetectFromPath/compile_commands.json
+
+// Check that files are tranformed when -p is provided and files are specified. 
+// RUN: cp %S/Inputs/compilations.cpp %T/DetectFromPath/a1
+// RUN: cp %S/Inputs/compilations.cpp %T/DetectFromPath/a2
+// RUN: cp %S/Inputs/compilations.cpp %T/DetectFromPath/a3
+
+// RUN: clang-modernize -use-nullptr -p=%T/DetectFromPath %T/DetectFromPath/a1/compilations.cpp %T/DetectFromPath/a3/compilations.cpp
+// RUN: diff -b %S/Inputs/compilations_expected.cpp %T/DetectFromPath/a1/compilations.cpp
+// RUN: not diff -b %S/Inputs/compilations_expected.cpp %T/DetectFromPath/a2/compilations.cpp
+// RUN: diff -b %S/Inputs/compilations_expected.cpp %T/DetectFromPath/a3/compilations.cpp

Added: clang-tools-extra/trunk/test/clang-modernize/Compilations/detect_from_source.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-modernize/Compilations/detect_from_source.cpp?rev=191213&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-modernize/Compilations/detect_from_source.cpp (added)
+++ clang-tools-extra/trunk/test/clang-modernize/Compilations/detect_from_source.cpp Mon Sep 23 13:37:32 2013
@@ -0,0 +1,23 @@
+// The following block tests:
+//   - A compilation database is detected from source0.
+
+// Create directory structure
+// a1, a2  and a3 are specified paths for files in the compilation database.
+// RUN: rm -rf %T/DetectFromSource
+// RUN: mkdir -p %T/DetectFromSource
+// RUN: mkdir -p %T/DetectFromSource/a1
+// RUN: mkdir -p %T/DetectFromSource/a2
+// RUN: mkdir -p %T/DetectFromSource/a3
+
+// This test uses a compilation database
+// RUN: sed -e 's#$(path)#%/T/DetectFromSource#g' %S/Inputs/compile_commands.json > %T/DetectFromSource/compile_commands.json
+
+// Check that a compilation database can be auto-detected from source0
+// RUN: cp %S/Inputs/compilations.cpp %T/DetectFromSource/a1
+// RUN: cp %S/Inputs/compilations.cpp %T/DetectFromSource/a2
+// RUN: cp %S/Inputs/compilations.cpp %T/DetectFromSource/a3
+
+// RUN: clang-modernize -use-nullptr %T/DetectFromSource/a1/compilations.cpp %T/DetectFromSource/a3/compilations.cpp
+// RUN: diff -b %S/Inputs/compilations_expected.cpp %T/DetectFromSource/a1/compilations.cpp
+// RUN: not diff -b %S/Inputs/compilations_expected.cpp %T/DetectFromSource/a2/compilations.cpp
+// RUN: diff -b %S/Inputs/compilations_expected.cpp %T/DetectFromSource/a3/compilations.cpp

Added: clang-tools-extra/trunk/test/clang-modernize/Compilations/fixed_comp.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-modernize/Compilations/fixed_comp.cpp?rev=191213&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-modernize/Compilations/fixed_comp.cpp (added)
+++ clang-tools-extra/trunk/test/clang-modernize/Compilations/fixed_comp.cpp Mon Sep 23 13:37:32 2013
@@ -0,0 +1,18 @@
+// The following block tests that files are transformed when -- is specified.
+
+// Create directory structure
+// a1, a2  and a3 are specified paths for files in the compilation database.
+// RUN: rm -rf %T/FixedComp
+// RUN: mkdir -p %T/FixedComp
+// RUN: mkdir -p %T/FixedComp/a1
+// RUN: mkdir -p %T/FixedComp/a2
+// RUN: mkdir -p %T/FixedComp/a3
+
+// RUN: cp %S/Inputs/compilations.cpp %T/FixedComp/a1
+// RUN: cp %S/Inputs/compilations.cpp %T/FixedComp/a2
+// RUN: cp %S/Inputs/compilations.cpp %T/FixedComp/a3
+
+// RUN: clang-modernize -use-nullptr %T/FixedComp/a1/compilations.cpp %T/FixedComp/a3/compilations.cpp --
+// RUN: diff -b %S/Inputs/compilations_expected.cpp %T/FixedComp/a1/compilations.cpp
+// RUN: not diff -b %S/Inputs/compilations_expected.cpp %T/FixedComp/a2/compilations.cpp
+// RUN: diff -b %S/Inputs/compilations_expected.cpp %T/FixedComp/a3/compilations.cpp

Added: clang-tools-extra/trunk/test/clang-modernize/Compilations/fixed_comp_inc.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-modernize/Compilations/fixed_comp_inc.cpp?rev=191213&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-modernize/Compilations/fixed_comp_inc.cpp (added)
+++ clang-tools-extra/trunk/test/clang-modernize/Compilations/fixed_comp_inc.cpp Mon Sep 23 13:37:32 2013
@@ -0,0 +1,20 @@
+// The following block tests:
+//   - A fixed compilation database is provided and -exclude was also used.
+
+// Create directory structure
+// a1, a2  and a3 are specified paths for files in the compilation database.
+// RUN: rm -rf %T/FixedCompInc
+// RUN: mkdir -p %T/FixedCompInc
+// RUN: mkdir -p %T/FixedCompInc/a1
+// RUN: mkdir -p %T/FixedCompInc/a2
+// RUN: mkdir -p %T/FixedCompInc/a3
+
+// Check that only files not explicitly excluded are transformed.
+// RUN: cp %S/Inputs/compilations.cpp %T/FixedCompInc/a1
+// RUN: cp %S/Inputs/compilations.cpp %T/FixedCompInc/a2
+// RUN: cp %S/Inputs/compilations.cpp %T/FixedCompInc/a3
+
+// RUN: clang-modernize -use-nullptr %T/FixedCompInc/a1/compilations.cpp %T/FixedCompInc/a2/compilations.cpp %T/FixedCompInc/a3/compilations.cpp -exclude=%T/FixedCompInc/a2 --
+// RUN: diff -b %S/Inputs/compilations_expected.cpp %T/FixedCompInc/a1/compilations.cpp
+// RUN: not diff -b %S/Inputs/compilations_expected.cpp %T/FixedCompInc/a2/compilations.cpp
+// RUN: diff -b %S/Inputs/compilations_expected.cpp %T/FixedCompInc/a3/compilations.cpp

Added: clang-tools-extra/trunk/test/clang-modernize/Compilations/no_compilation.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-modernize/Compilations/no_compilation.cpp?rev=191213&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-modernize/Compilations/no_compilation.cpp (added)
+++ clang-tools-extra/trunk/test/clang-modernize/Compilations/no_compilation.cpp Mon Sep 23 13:37:32 2013
@@ -0,0 +1,22 @@
+// The following block tests:
+//   - Neither -p nor -- was specified and a compilation database is detected
+//     from source0 but the file isn't found the compilation database then
+//     it's transformed using a fixed compilation database with c++11 support.
+//     (-- -std=c++11).
+
+// Create directory structure
+// a1, a2  and a3 are specified paths for files in the compilation database but
+// not a4.
+// RUN: rm -rf %T/NoCompilation
+// RUN: mkdir -p %T/NoCompilation
+// RUN: mkdir -p %T/NoCompilation/a1
+// RUN: mkdir -p %T/NoCompilation/a2
+// RUN: mkdir -p %T/NoCompilation/a3
+// RUN: mkdir -p %T/NoCompilation/a4
+
+// This test uses of a compilation database
+// RUN: sed -e 's#$(path)#%/T/NoCompilation#g' %S/Inputs/compile_commands.json > %T/NoCompilation/compile_commands.json
+
+// RUN: cp %S/Inputs/cpp11.cpp %T/NoCompilation/a4
+// RUN: clang-modernize -use-nullptr %T/NoCompilation/a4/cpp11.cpp
+// RUN: diff -b %S/Inputs/cpp11_expected.cpp %T/NoCompilation/a4/cpp11.cpp





More information about the cfe-commits mailing list