hanchenye-llvm-project/clang-tools-extra/test/clang-modernize/UseAuto/iterator.cpp

179 lines
5.4 KiB
C++
Raw Normal View History

// RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp
// RUN: clang-modernize -use-auto %t.cpp -- --std=c++11 -I %S/Inputs
// RUN: FileCheck -input-file=%t.cpp %s
//
// RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp
// RUN: clang-modernize -use-auto %t.cpp -- --std=c++11 -I %S/Inputs \
// RUN: -DUSE_INLINE_NAMESPACE=1
// RUN: FileCheck -input-file=%t.cpp %s
Switch from autogenerating tests to using the preprocessor. NOTE: You may need to run 'make clean' or 'ninja -t clean' etc!!! This is due to really nasty bug/interactions between CMake/configure/make/Ninja/LIT... This commit tries to back out the support for generating test cases as part of the build system due to the issues I brought up in post-commit review: 1) It adds a *lot* of complexity and fragility to the build system. See the number of commits required to try to get all the bots happy. 2) It isn't really necessary -- we can already run scripts to generate things with the RUN lines of a test. 3) It makes the tests somewhat harder to debug as they cross between more domains. 4) In almost all cases it isn't really needed or it can be done directly using the preprocessor. I should have been more proactive reviewing this, and I'm really sorry about the churn here. =/ To help keep track of what commits are going where, this backs out most of the non-test-changes from these revisions: r176397 r176373 r176293 r176184 r175744 r175624 r175545 r175544 There were several trivial or cleanup changes to the lit files or other files. Some of these looked ok, but I didn't try to tease them apart... Edwin, if you know what to look for, please carry on with the cleanups there, and sorry for hosing stuff here but I'm not much of a Python person, and so I was erring on the side of cautiously backing out the change. I've tried to preserve the test changes everywhere I could, but review is appreciated here in case I missed some. I then re-wrote the tests to use the preprocessor rather than python to expand to the various bits of code. The nicest part of this is that now all the files are just C++ code. They edit and behave like C++ code, etc. RUN lines with different -D flags are used to run the same test over multiple different configurations, and includes bracketed in special defines are used to flesh out a collection of standard interface stubs to test interactions between pieces. These probably aren't perfect yet, but I think its an improvement (at least in terms of build system complexity) and will hopefully be a useful demonstration of the technique I prefer for these types of tests. llvm-svn: 176627
2013-03-07 18:09:47 +08:00
#define CONTAINER array
#include "test_std_container.h"
#undef CONTAINER
#define CONTAINER vector
#include "test_std_container.h"
#undef CONTAINER
#define CONTAINER unordered_map
#define USE_BASE_CLASS_ITERATORS 1
#include "test_std_container.h"
#undef USE_BASE_CLASS_ITERATORS
#undef CONTAINER
typedef std::vector<int>::iterator int_iterator;
namespace foo {
template <typename T>
class vector {
public:
class iterator {};
iterator begin() { return iterator(); }
};
} // namespace foo
int main(int argc, char **argv) {
std::vector<int> Vec;
// CHECK: std::vector<int> Vec;
std::unordered_map<int> Map;
// CHECK: std::unordered_map<int> Map;
// Types with more sugar should work. Types with less should not.
{
int_iterator more_sugar = Vec.begin();
// CHECK: auto more_sugar = Vec.begin();
internal::iterator_wrapper<std::vector<int>, 0> less_sugar = Vec.begin();
// CHECK: internal::iterator_wrapper<std::vector<int>, 0> less_sugar = Vec.begin();
}
// Initialization from initializer lists isn't allowed. Using 'auto'
// would result in std::initializer_list being deduced for the type.
{
std::unordered_map<int>::iterator I{Map.begin()};
// CHECK: std::unordered_map<int>::iterator I{Map.begin()};
std::unordered_map<int>::iterator I2 = {Map.begin()};
// CHECK: std::unordered_map<int>::iterator I2 = {Map.begin()};
}
// Various forms of construction. Default constructors and constructors with
// all-default parameters shouldn't get transformed. Construction from other
// types is also not allowed.
{
std::unordered_map<int>::iterator copy(Map.begin());
// CHECK: auto copy(Map.begin());
std::unordered_map<int>::iterator def;
// CHECK: std::unordered_map<int>::iterator def;
// const_iterator has no default constructor, just one that has >0 params
// with defaults.
std::unordered_map<int>::const_iterator constI;
// CHECK: std::unordered_map<int>::const_iterator constI;
// Uses iterator_provider::const_iterator's conversion constructor.
std::unordered_map<int>::const_iterator constI2 = def;
// CHECK: std::unordered_map<int>::const_iterator constI2 = def;
std::unordered_map<int>::const_iterator constI3(def);
// CHECK: std::unordered_map<int>::const_iterator constI3(def);
// Explicit use of conversion constructor
std::unordered_map<int>::const_iterator constI4 = std::unordered_map<int>::const_iterator(def);
// CHECK: auto constI4 = std::unordered_map<int>::const_iterator(def);
// Uses iterator_provider::iterator's const_iterator conversion operator.
std::unordered_map<int>::iterator I = constI;
// CHECK: std::unordered_map<int>::iterator I = constI;
std::unordered_map<int>::iterator I2(constI);
// CHECK: std::unordered_map<int>::iterator I2(constI);
}
// Weird cases of pointers and references to iterators are not transformed.
{
int_iterator I = Vec.begin();
int_iterator *IPtr = &I;
// CHECK: int_iterator *IPtr = &I;
int_iterator &IRef = I;
// CHECK: int_iterator &IRef = I;
}
{
// Variable declarations in iteration statements.
for (std::vector<int>::iterator I = Vec.begin(); I != Vec.end(); ++I) {
// CHECK: for (auto I = Vec.begin(); I != Vec.end(); ++I) {
}
// Range-based for loops.
std::array<std::vector<int>::iterator> iter_arr;
for (std::vector<int>::iterator I: iter_arr) {
// CHECK: for (auto I: iter_arr) {
}
// Test with init-declarator-list.
for (int_iterator I = Vec.begin(),
E = Vec.end(); I != E; ++I) {
// CHECK: for (auto I = Vec.begin(),
// CHECK-NEXT: E = Vec.end(); I != E; ++I) {
}
}
// Only std containers should be changed.
{
using namespace foo;
vector<int> foo_vec;
vector<int>::iterator I = foo_vec.begin();
// CHECK: vector<int>::iterator I = foo_vec.begin();
}
// Ensure using directives don't interfere with replacement.
{
using namespace std;
vector<int> std_vec;
vector<int>::iterator I = std_vec.begin();
// CHECK: auto I = std_vec.begin();
}
// Make sure references and cv qualifiers don't get removed (i.e. replaced
// with just 'auto').
{
const auto & I = Vec.begin();
// CHECK: const auto & I = Vec.begin();
auto && I2 = Vec.begin();
// CHECK: auto && I2 = Vec.begin();
}
// Passing a string as an argument to introduce a temporary object
// that will create an expression with cleanups. Bugzilla: 15550
{
std::unordered_map<int> MapFind;
std::unordered_map<int>::iterator I = MapFind.find("foo");
// CHECK: auto I = MapFind.find("foo");
}
// Test for declaration lists
{
// Ensusre declaration lists that matches the declaration type with written
// no-list initializer are transformed.
std::vector<int>::iterator I = Vec.begin(), E = Vec.end();
// CHECK: auto I = Vec.begin(), E = Vec.end();
// Declaration lists with non-initialized variables should not be
// transformed.
std::vector<int>::iterator J = Vec.begin(), K;
// CHECK: std::vector<int>::iterator J = Vec.begin(), K;
}
return 0;
}