Author: rtrieu
Date: Fri Aug  5 16:02:34 2016
New Revision: 277866

URL: http://llvm.org/viewvc/llvm-project?rev=277866&view=rev
Log:
Fix false positive in -Wunsequenced and templates.

For builtin logical operators, there is a well-defined ordering of argument
evaluation.  For overloaded operator of the same type, there is no argument
evaluation order, similar to other function calls.  When both are present,
uninstantiated templates with an operator&& is treated as an unresolved
function call.  Unresolved function calls are treated as normal function calls,
and may result in false positives when the builtin logical operator is used.
Have the unsequenced checker ignore dependent expressions to avoid this
false positive.  The check also happens in template instantiations to catch
when the overloaded operator is used.

Modified:
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/SemaCXX/warn-unsequenced.cpp

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=277866&r1=277865&r2=277866&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Fri Aug  5 16:02:34 2016
@@ -9427,7 +9427,8 @@ void Sema::CheckUnsequencedOperations(Ex
 void Sema::CheckCompletedExpr(Expr *E, SourceLocation CheckLoc,
                               bool IsConstexpr) {
   CheckImplicitConversions(E, CheckLoc);
-  CheckUnsequencedOperations(E);
+  if (!E->isInstantiationDependent())
+    CheckUnsequencedOperations(E);
   if (!IsConstexpr && !E->isValueDependent())
     CheckForIntOverflow(E);
 }

Modified: cfe/trunk/test/SemaCXX/warn-unsequenced.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unsequenced.cpp?rev=277866&r1=277865&r2=277866&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unsequenced.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-unsequenced.cpp Fri Aug  5 16:02:34 2016
@@ -113,3 +113,58 @@ void test() {
   (__builtin_object_size(&(++a, a), 0) ? 1 : 0) + ++a; // ok
   (__builtin_expect(++a, 0) ? 1 : 0) + ++a; // expected-warning {{multiple 
unsequenced modifications}}
 }
+
+namespace templates {
+
+template <typename T>
+struct Bar {
+  T get() { return 0; }
+};
+
+template <typename X>
+struct Foo {
+  int Run();
+  Bar<int> bar;
+};
+
+enum E {e1, e2};
+bool operator&&(E, E);
+
+void foo(int, int);
+
+template <typename X>
+int Foo<X>::Run() {
+  char num = 0;
+
+  // Before instantiation, Clang may consider the builtin operator here as
+  // unresolved function calls, and treat the arguments as unordered when
+  // the builtin operator evaluatation is well-ordered.  Waiting until
+  // instantiation to check these expressions will prevent false positives.
+  if ((num = bar.get()) < 5 && num < 10) { }
+  if ((num = bar.get()) < 5 || num < 10) { }
+  if (static_cast<E>((num = bar.get()) < 5) || static_cast<E>(num < 10)) { }
+
+  if (static_cast<E>((num = bar.get()) < 5) && static_cast<E>(num < 10)) { }
+  // expected-warning@-1 {{unsequenced modification and access to 'num'}}
+
+  foo(num++, num++);
+  // expected-warning@-1 2{{multiple unsequenced modifications to 'num'}}
+  return 1;
+}
+
+int x = Foo<int>().Run();
+// expected-note@-1 {{in instantiation of member function 
'templates::Foo<int>::Run'}}
+
+
+template <typename T>
+int Run2() {
+  T t = static_cast<T>(0);
+  return (t = static_cast<T>(1)) && t;
+  // expected-warning@-1 {{unsequenced modification and access to 't'}}
+}
+
+int y = Run2<bool>();
+int z = Run2<E>();
+// expected-note@-1{{in instantiation of function template specialization 
'templates::Run2<templates::E>' requested here}}
+
+}


_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to