@@ -143,6 +143,23 @@ void assertSemaThrows(const std::string& errcontents, const std::string& text) {
143143 }
144144 ASSERT (threw);
145145}
146+ void assertSemaWarns (const std::string& warncontents, const std::string& text) {
147+ Parser parser (text);
148+ auto p = parser.parseFunction ();
149+ Sema sem;
150+ // Redirect std::cerr to buffer in order to capture warning message.
151+ std::stringstream buffer;
152+ std::streambuf* old = std::cerr.rdbuf (buffer.rdbuf ());
153+ sem.checkFunction (p);
154+ std::cerr.rdbuf (old);
155+ // Match the captured warning message with the expected one.
156+ // Empty warncontents denotes no warning should be raised.
157+ if (warncontents.empty ()) {
158+ ASSERT (buffer.str ().find (" WARNING:" ) == std::string::npos);
159+ } else {
160+ ASSERT (buffer.str ().find (warncontents) != std::string::npos);
161+ }
162+ }
146163TreeRef loadText (const std::string& text) {
147164 return Sema ().checkFunction (Parser (text).parseFunction ());
148165}
@@ -315,6 +332,57 @@ int main(int argc, char** argv) {
315332 O(i) = A(i,j)
316333 }
317334 )" );
335+ assertSemaWarns (
336+ " " , // Legal reduction with initialization.
337+ R"(
338+ def fun(float(M,N) A) -> (O) {
339+ O(i) +=! A(i,j)
340+ }
341+ )" );
342+ assertSemaWarns (
343+ " " , // Legal reduction that writes to an already initialized output.
344+ R"(
345+ def fun(float(M,N) A, float(M) B) -> (O) {
346+ O(i) = B(i)
347+ O(i) *= A(i,j)
348+ }
349+ )" );
350+ assertSemaWarns (
351+ " " , // Legal reduction that writes to an already initialized output.
352+ R"(
353+ def fun(float(M,N) A, float(M,N) B) -> (O) {
354+ O(i) max=! A(i,j)
355+ O(i) min= B(i,j)
356+ }
357+ )" );
358+ assertSemaWarns (
359+ " +=! instead of +=" ,
360+ R"(
361+ def fun(float(M,N) A) -> (O) {
362+ O(i) += A(i,j)
363+ }
364+ )" );
365+ assertSemaWarns (
366+ " *=! instead of *=" ,
367+ R"(
368+ def fun(float(M,N) A) -> (O) {
369+ O(i) *= A(i,j)
370+ }
371+ )" );
372+ assertSemaWarns (
373+ " max=! instead of max=" ,
374+ R"(
375+ def fun(float(M,N) A) -> (O) {
376+ O(i) max= A(i,j)
377+ }
378+ )" );
379+ assertSemaWarns (
380+ " min=! instead of min=" ,
381+ R"(
382+ def fun(float(M,N) A) -> (O) {
383+ O(i) min= A(i,j)
384+ }
385+ )" );
318386
319387 auto option_one = R"(
320388 def fun(float(B, N, M) X, float(B, M, K) Y) -> (Z)
0 commit comments