diff options
14 files changed, 223 insertions, 2 deletions
diff --git a/clang-tools-extra/clang-modernize/tool/ClangModernize.cpp b/clang-tools-extra/clang-modernize/tool/ClangModernize.cpp index 445d4b89627..49f28d3d80b 100644 --- a/clang-tools-extra/clang-modernize/tool/ClangModernize.cpp +++ b/clang-tools-extra/clang-modernize/tool/ClangModernize.cpp @@ -290,8 +290,21 @@ CompilationDatabase *autoDetectCompilations(std::string &ErrorMessage) { 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" }; diff --git a/clang-tools-extra/test/clang-modernize/Compilations/Inputs/compilations.cpp b/clang-tools-extra/test/clang-modernize/Compilations/Inputs/compilations.cpp new file mode 100644 index 00000000000..93ee7040861 --- /dev/null +++ b/clang-tools-extra/test/clang-modernize/Compilations/Inputs/compilations.cpp @@ -0,0 +1,3 @@ +void foo() { + int *p = 0; +} diff --git a/clang-tools-extra/test/clang-modernize/Compilations/Inputs/compilations_expected.cpp b/clang-tools-extra/test/clang-modernize/Compilations/Inputs/compilations_expected.cpp new file mode 100644 index 00000000000..459c9755434 --- /dev/null +++ b/clang-tools-extra/test/clang-modernize/Compilations/Inputs/compilations_expected.cpp @@ -0,0 +1,3 @@ +void foo() { + int *p = nullptr; +} diff --git a/clang-tools-extra/test/clang-modernize/Compilations/Inputs/compile_commands.json b/clang-tools-extra/test/clang-modernize/Compilations/Inputs/compile_commands.json new file mode 100644 index 00000000000..e3f35bf7f55 --- /dev/null +++ b/clang-tools-extra/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" +} +] diff --git a/clang-tools-extra/test/clang-modernize/Compilations/Inputs/cpp11.cpp b/clang-tools-extra/test/clang-modernize/Compilations/Inputs/cpp11.cpp new file mode 100644 index 00000000000..70aa0a2b84d --- /dev/null +++ b/clang-tools-extra/test/clang-modernize/Compilations/Inputs/cpp11.cpp @@ -0,0 +1,4 @@ +void foo() { + int *p = 0; + int *k = nullptr; +} diff --git a/clang-tools-extra/test/clang-modernize/Compilations/Inputs/cpp11_expected.cpp b/clang-tools-extra/test/clang-modernize/Compilations/Inputs/cpp11_expected.cpp new file mode 100644 index 00000000000..dd8ed716f46 --- /dev/null +++ b/clang-tools-extra/test/clang-modernize/Compilations/Inputs/cpp11_expected.cpp @@ -0,0 +1,4 @@ +void foo() { + int *p = nullptr; + int *k = nullptr; +} diff --git a/clang-tools-extra/test/clang-modernize/Compilations/compilation_inc.cpp b/clang-tools-extra/test/clang-modernize/Compilations/compilation_inc.cpp new file mode 100644 index 00000000000..745c3d47077 --- /dev/null +++ b/clang-tools-extra/test/clang-modernize/Compilations/compilation_inc.cpp @@ -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 diff --git a/clang-tools-extra/test/clang-modernize/Compilations/compilation_inc_sources.cpp b/clang-tools-extra/test/clang-modernize/Compilations/compilation_inc_sources.cpp new file mode 100644 index 00000000000..32c6715c2bf --- /dev/null +++ b/clang-tools-extra/test/clang-modernize/Compilations/compilation_inc_sources.cpp @@ -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 diff --git a/clang-tools-extra/test/clang-modernize/Compilations/compilation_not_inc.cpp b/clang-tools-extra/test/clang-modernize/Compilations/compilation_not_inc.cpp new file mode 100644 index 00000000000..b9be391107b --- /dev/null +++ b/clang-tools-extra/test/clang-modernize/Compilations/compilation_not_inc.cpp @@ -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 diff --git a/clang-tools-extra/test/clang-modernize/Compilations/detect_from_path.cpp b/clang-tools-extra/test/clang-modernize/Compilations/detect_from_path.cpp new file mode 100644 index 00000000000..baf88ec35ab --- /dev/null +++ b/clang-tools-extra/test/clang-modernize/Compilations/detect_from_path.cpp @@ -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 diff --git a/clang-tools-extra/test/clang-modernize/Compilations/detect_from_source.cpp b/clang-tools-extra/test/clang-modernize/Compilations/detect_from_source.cpp new file mode 100644 index 00000000000..42838d41da1 --- /dev/null +++ b/clang-tools-extra/test/clang-modernize/Compilations/detect_from_source.cpp @@ -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 diff --git a/clang-tools-extra/test/clang-modernize/Compilations/fixed_comp.cpp b/clang-tools-extra/test/clang-modernize/Compilations/fixed_comp.cpp new file mode 100644 index 00000000000..b057e57856a --- /dev/null +++ b/clang-tools-extra/test/clang-modernize/Compilations/fixed_comp.cpp @@ -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 diff --git a/clang-tools-extra/test/clang-modernize/Compilations/fixed_comp_inc.cpp b/clang-tools-extra/test/clang-modernize/Compilations/fixed_comp_inc.cpp new file mode 100644 index 00000000000..8d8831d2af8 --- /dev/null +++ b/clang-tools-extra/test/clang-modernize/Compilations/fixed_comp_inc.cpp @@ -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 diff --git a/clang-tools-extra/test/clang-modernize/Compilations/no_compilation.cpp b/clang-tools-extra/test/clang-modernize/Compilations/no_compilation.cpp new file mode 100644 index 00000000000..3884ee637bc --- /dev/null +++ b/clang-tools-extra/test/clang-modernize/Compilations/no_compilation.cpp @@ -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 |

