]> granicus.if.org Git - clang/commitdiff
Remove warnings of constant operands of logical operators from template instantiation...
authorRichard Trieu <rtrieu@google.com>
Fri, 15 Jul 2011 00:00:51 +0000 (00:00 +0000)
committerRichard Trieu <rtrieu@google.com>
Fri, 15 Jul 2011 00:00:51 +0000 (00:00 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@135222 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaExpr.cpp
test/SemaCXX/expressions.cpp

index 22185ed69ecb86fbfa4eae27e92949c14a3f5ac6..a99a5d2cc429db6a2fb28e6ef259ec8689d0781a 100644 (file)
@@ -6528,8 +6528,8 @@ inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
   // 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.
index 95ece48e51feb2dd15115b649440a0417dd276f9..8a294face3806ba843300560581f9ee2cd44d554 100644 (file)
@@ -63,3 +63,25 @@ int test2(int x) {
   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();
+}