Author: rtrieu
Date: Thu Jul 14 19:00:51 2011
New Revision: 135222

URL: http://llvm.org/viewvc/llvm-project?rev=135222&view=rev
Log:
Remove warnings of constant operands of logical operators from template 
instantiations.  Upon instantiation of template, value-dependent parameters are 
replaced by equivalent literals, so code like:

template<unsigned int A, unsigned int B> struct S {
  int foo() {
    int x = A && B;
  }
}

will not warn on A && B on every instantiation.  This will still warn on other 
cases inside templates, which will be caught on checking the template 
definition.

Modified:
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/SemaCXX/expressions.cpp

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=135222&r1=135221&r2=135222&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Thu Jul 14 19:00:51 2011
@@ -6528,8 +6528,8 @@
   // is a constant.
   if (lex.get()->getType()->isIntegerType() && 
!lex.get()->getType()->isBooleanType() &&
       rex.get()->getType()->isIntegerType() && !rex.get()->isValueDependent() 
&&
-      // Don't warn in macros.
-      !Loc.isMacroID()) {
+      // Don't warn in macros or template instantiations.
+      !Loc.isMacroID() && ActiveTemplateInstantiations.empty()) {
     // If the RHS can be constant folded, and if it constant folds to something
     // that isn't 0 or 1 (which indicate a potential logical operation that
     // happened to fold to true/false) then warn.

Modified: cfe/trunk/test/SemaCXX/expressions.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/expressions.cpp?rev=135222&r1=135221&r2=135222&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/expressions.cpp (original)
+++ cfe/trunk/test/SemaCXX/expressions.cpp Thu Jul 14 19:00:51 2011
@@ -63,3 +63,25 @@
   return x && (-1); // expected-warning {{use of logical && with constant 
operand; switch to bitwise & or remove constant}}
   return x && (5); // expected-warning {{use of logical && with constant 
operand; switch to bitwise & or remove constant}}
 }
+
+template<unsigned int A, unsigned int B> struct S
+{
+  enum {
+    e1 = A && B,
+    e2 = A && 7      // expected-warning {{use of logical && with constant 
operand; switch to bitwise & or remove constant}}
+  };
+
+  int foo() {
+    int x = A && B;
+    int y = B && 3;  // expected-warning {{use of logical && with constant 
operand; switch to bitwise & or remove constant}}
+
+    return x + y;
+  }
+};
+
+void test3() {
+  S<5, 8> s1;
+  S<2, 7> s2;
+  (void)s1.foo();
+  (void)s2.foo();
+}


_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to