Summary: The messages for two of the warnings are misleading: * warn_for_range_const_reference_copy suggests that the initialization of the loop variable results in a copy. But that's not always true, we just know that some conversion happens, potentially invoking a constructor or conversion operator. The constructor might copy, as in the example that lead to this message [1], but it might also not. However, the constructed object is bound to a reference, which is potentially misleading, so we rewrite the message to emphasize that. We also make sure that we print the reference type into the warning message to clarify that this warning only appears when operator* returns a reference. * warn_for_range_variable_always_copy suggests that a reference type loop variable initialized from a temporary "is always a copy". But we don't know this, the range might just return temporary objects which aren't copies of anything. (Assuming RVO a copy constructor might never have been called.) The message for warn_for_range_copy is a bit repetitive: the type of a VarDecl and its initialization Expr are the same up to cv-qualifiers, because Sema will insert implicit casts or constructor calls to make them match. [1] https://bugs.llvm.org/show_bug.cgi?id=32823 Reviewers: aaron.ballman, Mordante, rtrieu Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D75613
90 lines
2.0 KiB
C++
90 lines
2.0 KiB
C++
// RUN: %clang_cc1 -fsyntax-only -std=c++11 -Wloop-analysis -verify %s
|
|
// RUN: %clang_cc1 -fsyntax-only -std=c++11 -Wrange-loop-analysis -verify %s
|
|
|
|
void test_POD_64_bytes() {
|
|
struct Record {
|
|
char a[64];
|
|
};
|
|
|
|
Record records[8];
|
|
for (const auto r : records)
|
|
(void)r;
|
|
}
|
|
|
|
void test_POD_65_bytes() {
|
|
struct Record {
|
|
char a[65];
|
|
};
|
|
|
|
// expected-warning@+3 {{loop variable 'r' creates a copy from type 'const Record'}}
|
|
// expected-note@+2 {{use reference type 'const Record &' to prevent copying}}
|
|
Record records[8];
|
|
for (const auto r : records)
|
|
(void)r;
|
|
}
|
|
|
|
void test_TriviallyCopyable_64_bytes() {
|
|
struct Record {
|
|
Record() {}
|
|
char a[64];
|
|
};
|
|
|
|
Record records[8];
|
|
for (const auto r : records)
|
|
(void)r;
|
|
}
|
|
|
|
void test_TriviallyCopyable_65_bytes() {
|
|
struct Record {
|
|
Record() {}
|
|
char a[65];
|
|
};
|
|
|
|
// expected-warning@+3 {{loop variable 'r' creates a copy from type 'const Record'}}
|
|
// expected-note@+2 {{use reference type 'const Record &' to prevent copying}}
|
|
Record records[8];
|
|
for (const auto r : records)
|
|
(void)r;
|
|
}
|
|
|
|
void test_NonTriviallyCopyable() {
|
|
struct Record {
|
|
Record() {}
|
|
~Record() {}
|
|
volatile int a;
|
|
int b;
|
|
};
|
|
|
|
// expected-warning@+3 {{loop variable 'r' creates a copy from type 'const Record'}}
|
|
// expected-note@+2 {{use reference type 'const Record &' to prevent copying}}
|
|
Record records[8];
|
|
for (const auto r : records)
|
|
(void)r;
|
|
}
|
|
|
|
void test_TrivialABI_64_bytes() {
|
|
struct [[clang::trivial_abi]] Record {
|
|
Record() {}
|
|
~Record() {}
|
|
char a[64];
|
|
};
|
|
|
|
Record records[8];
|
|
for (const auto r : records)
|
|
(void)r;
|
|
}
|
|
|
|
void test_TrivialABI_65_bytes() {
|
|
struct [[clang::trivial_abi]] Record {
|
|
Record() {}
|
|
~Record() {}
|
|
char a[65];
|
|
};
|
|
|
|
// expected-warning@+3 {{loop variable 'r' creates a copy from type 'const Record'}}
|
|
// expected-note@+2 {{use reference type 'const Record &' to prevent copying}}
|
|
Record records[8];
|
|
for (const auto r : records)
|
|
(void)r;
|
|
}
|