[cfe-commits] r147006 - in /cfe/trunk: include/clang/Basic/DiagnosticParseKinds.td lib/Parse/ParseStmt.cpp test/Parser/cxx0x-for-range.cpp
Richard Smith
richard-llvm at metafoo.co.uk
Tue Dec 20 14:56:20 PST 2011
Author: rsmith
Date: Tue Dec 20 16:56:20 2011
New Revision: 147006
URL: http://llvm.org/viewvc/llvm-project?rev=147006&view=rev
Log:
PR11297: Provide a better diagnostic for code which contains a
reasonable-looking but ill-formed for-range statement of the form:
for (expression : expression)
Added:
cfe/trunk/test/Parser/cxx0x-for-range.cpp
Modified:
cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td
cfe/trunk/lib/Parse/ParseStmt.cpp
Modified: cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td?rev=147006&r1=147005&r2=147006&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td Tue Dec 20 16:56:20 2011
@@ -244,6 +244,8 @@
def warn_cxx98_compat_for_range : Warning<
"range-based for loop is incompatible with C++98">,
InGroup<CXX98Compat>, DefaultIgnore;
+def err_for_range_expected_decl : Error<
+ "for range declaration must declare a variable">;
def err_argument_required_after_attribute : Error<
"argument required after attribute">;
def err_missing_param : Error<"expected parameter declarator">;
Modified: cfe/trunk/lib/Parse/ParseStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseStmt.cpp?rev=147006&r1=147005&r2=147006&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseStmt.cpp (original)
+++ cfe/trunk/lib/Parse/ParseStmt.cpp Tue Dec 20 16:56:20 2011
@@ -1371,6 +1371,14 @@
return StmtError();
}
Collection = ParseExpression();
+ } else if (getLang().CPlusPlus0x && Tok.is(tok::colon) &&
+ !FirstPart.isInvalid()) {
+ // User tried to write the reasonable, but ill-formed, for-range-statement
+ // for (expr : expr) { ... }
+ Diag(Tok, diag::err_for_range_expected_decl)
+ << FirstPart.get()->getSourceRange();
+ SkipUntil(tok::r_paren, false, true);
+ SecondPartIsInvalid = true;
} else {
if (!Value.isInvalid()) {
Diag(Tok, diag::err_expected_semi_for);
Added: cfe/trunk/test/Parser/cxx0x-for-range.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/cxx0x-for-range.cpp?rev=147006&view=auto
==============================================================================
--- cfe/trunk/test/Parser/cxx0x-for-range.cpp (added)
+++ cfe/trunk/test/Parser/cxx0x-for-range.cpp Tue Dec 20 16:56:20 2011
@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
+
+template<typename T, typename U>
+struct pair {};
+
+template<typename T, typename U>
+struct map {
+ typedef pair<T,U> *iterator;
+ iterator begin();
+ iterator end();
+};
+
+template<typename T, typename U>
+pair<T,U> &tie(T &, U &);
+
+int foo(map<char*,int> &m) {
+ char *p;
+ int n;
+
+ for (pair<char*,int> x : m) {
+ (void)x;
+ }
+
+ for (tie(p, n) : m) { // expected-error {{for range declaration must declare a variable}}
+ (void)p;
+ (void)n;
+ }
+
+ return n;
+}
More information about the cfe-commits
mailing list